ImathFun.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMATHFUN_H
  35. #define INCLUDED_IMATHFUN_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Miscellaneous utility functions
  39. //
  40. //-----------------------------------------------------------------------------
  41. #include "ImathExport.h"
  42. #include "ImathLimits.h"
  43. #include "ImathInt64.h"
  44. #include "ImathNamespace.h"
  45. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  46. template <class T>
  47. inline T
  48. abs (T a)
  49. {
  50. return (a > T(0)) ? a : -a;
  51. }
  52. template <class T>
  53. inline int
  54. sign (T a)
  55. {
  56. return (a > T(0))? 1 : ((a < T(0)) ? -1 : 0);
  57. }
  58. template <class T, class Q>
  59. inline T
  60. lerp (T a, T b, Q t)
  61. {
  62. return (T) (a * (1 - t) + b * t);
  63. }
  64. template <class T, class Q>
  65. inline T
  66. ulerp (T a, T b, Q t)
  67. {
  68. return (T) ((a > b)? (a - (a - b) * t): (a + (b - a) * t));
  69. }
  70. template <class T>
  71. inline T
  72. lerpfactor(T m, T a, T b)
  73. {
  74. //
  75. // Return how far m is between a and b, that is return t such that
  76. // if:
  77. // t = lerpfactor(m, a, b);
  78. // then:
  79. // m = lerp(a, b, t);
  80. //
  81. // If a==b, return 0.
  82. //
  83. T d = b - a;
  84. T n = m - a;
  85. if (abs(d) > T(1) || abs(n) < limits<T>::max() * abs(d))
  86. return n / d;
  87. return T(0);
  88. }
  89. template <class T>
  90. inline T
  91. clamp (T a, T l, T h)
  92. {
  93. return (a < l)? l : ((a > h)? h : a);
  94. }
  95. template <class T>
  96. inline int
  97. cmp (T a, T b)
  98. {
  99. return IMATH_INTERNAL_NAMESPACE::sign (a - b);
  100. }
  101. template <class T>
  102. inline int
  103. cmpt (T a, T b, T t)
  104. {
  105. return (IMATH_INTERNAL_NAMESPACE::abs (a - b) <= t)? 0 : cmp (a, b);
  106. }
  107. template <class T>
  108. inline bool
  109. iszero (T a, T t)
  110. {
  111. return (IMATH_INTERNAL_NAMESPACE::abs (a) <= t) ? 1 : 0;
  112. }
  113. template <class T1, class T2, class T3>
  114. inline bool
  115. equal (T1 a, T2 b, T3 t)
  116. {
  117. return IMATH_INTERNAL_NAMESPACE::abs (a - b) <= t;
  118. }
  119. template <class T>
  120. inline int
  121. floor (T x)
  122. {
  123. return (x >= 0)? int (x): -(int (-x) + (-x > int (-x)));
  124. }
  125. template <class T>
  126. inline int
  127. ceil (T x)
  128. {
  129. return -floor (-x);
  130. }
  131. template <class T>
  132. inline int
  133. trunc (T x)
  134. {
  135. return (x >= 0) ? int(x) : -int(-x);
  136. }
  137. //
  138. // Integer division and remainder where the
  139. // remainder of x/y has the same sign as x:
  140. //
  141. // divs(x,y) == (abs(x) / abs(y)) * (sign(x) * sign(y))
  142. // mods(x,y) == x - y * divs(x,y)
  143. //
  144. inline int
  145. divs (int x, int y)
  146. {
  147. return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)):
  148. ((y >= 0)? -(-x / y): (-x / -y));
  149. }
  150. inline int
  151. mods (int x, int y)
  152. {
  153. return (x >= 0)? ((y >= 0)? ( x % y): ( x % -y)):
  154. ((y >= 0)? -(-x % y): -(-x % -y));
  155. }
  156. //
  157. // Integer division and remainder where the
  158. // remainder of x/y is always positive:
  159. //
  160. // divp(x,y) == floor (double(x) / double (y))
  161. // modp(x,y) == x - y * divp(x,y)
  162. //
  163. inline int
  164. divp (int x, int y)
  165. {
  166. return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)):
  167. ((y >= 0)? -((y-1-x) / y): ((-y-1-x) / -y));
  168. }
  169. inline int
  170. modp (int x, int y)
  171. {
  172. return x - y * divp (x, y);
  173. }
  174. //----------------------------------------------------------
  175. // Successor and predecessor for floating-point numbers:
  176. //
  177. // succf(f) returns float(f+e), where e is the smallest
  178. // positive number such that float(f+e) != f.
  179. //
  180. // predf(f) returns float(f-e), where e is the smallest
  181. // positive number such that float(f-e) != f.
  182. //
  183. // succd(d) returns double(d+e), where e is the smallest
  184. // positive number such that double(d+e) != d.
  185. //
  186. // predd(d) returns double(d-e), where e is the smallest
  187. // positive number such that double(d-e) != d.
  188. //
  189. // Exceptions: If the input value is an infinity or a nan,
  190. // succf(), predf(), succd(), and predd() all
  191. // return the input value without changing it.
  192. //
  193. //----------------------------------------------------------
  194. IMATH_EXPORT float succf (float f);
  195. IMATH_EXPORT float predf (float f);
  196. IMATH_EXPORT double succd (double d);
  197. IMATH_EXPORT double predd (double d);
  198. //
  199. // Return true if the number is not a NaN or Infinity.
  200. //
  201. inline bool
  202. finitef (float f)
  203. {
  204. union {float f; int i;} u;
  205. u.f = f;
  206. return (u.i & 0x7f800000) != 0x7f800000;
  207. }
  208. inline bool
  209. finited (double d)
  210. {
  211. union {double d; Int64 i;} u;
  212. u.d = d;
  213. return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL;
  214. }
  215. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  216. #endif // INCLUDED_IMATHFUN_H