OpenVDB  3.0.0
Math.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2014 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 //
34 
35 #ifndef OPENVDB_MATH_HAS_BEEN_INCLUDED
36 #define OPENVDB_MATH_HAS_BEEN_INCLUDED
37 
38 #include <assert.h>
39 #include <algorithm> // for std::max()
40 #include <cmath> // for floor(), ceil() and sqrt()
41 #include <math.h> // for pow(), fabs() etc
42 #include <cstdlib> // for srand(), abs(int)
43 #include <limits> // for std::numeric_limits<Type>::max()
44 #include <string>
45 #include <boost/numeric/conversion/conversion_traits.hpp>
46 #include <boost/math/special_functions/cbrt.hpp>
47 #include <boost/random/mersenne_twister.hpp> // for boost::random::mt19937
48 #include <boost/random/uniform_01.hpp>
49 #include <boost/random/uniform_int.hpp>
50 #include <boost/version.hpp> // for BOOST_VERSION
51 #include <openvdb/Platform.h>
52 #include <openvdb/version.h>
53 
54 
55 // Compile pragmas
56 
57 // Intel(r) compiler fires remark #1572: floating-point equality and inequality
58 // comparisons are unrealiable when == or != is used with floating point operands.
59 #if defined(__INTEL_COMPILER)
60  #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
61  _Pragma("warning (push)") \
62  _Pragma("warning (disable:1572)")
63  #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
64  _Pragma("warning (pop)")
65 #else
66  // For GCC, #pragma GCC diagnostic ignored "-Wfloat-equal"
67  // isn't working until gcc 4.2+,
68  // Trying
69  // #pragma GCC system_header
70  // creates other problems, most notably "warning: will never be executed"
71  // in from templates, unsure of how to work around.
72  // If necessary, could use integer based comparisons for equality
73  #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
74  #define OPENVDB_NO_FP_EQUALITY_WARNING_END
75 #endif
76 
77 namespace openvdb {
79 namespace OPENVDB_VERSION_NAME {
80 
85 template<typename T> inline T zeroVal() { return T(0); }
87 template<> inline std::string zeroVal<std::string>() { return ""; }
89 template<> inline bool zeroVal<bool>() { return false; }
90 
92 
93 inline std::string operator+(const std::string& s, bool) { return s; }
96 inline std::string operator+(const std::string& s, int) { return s; }
97 inline std::string operator+(const std::string& s, float) { return s; }
98 inline std::string operator+(const std::string& s, double) { return s; }
100 
101 
102 namespace math {
103 
107 template<typename T> inline T negative(const T& val) { return T(-val); }
109 template<> inline bool negative(const bool& val) { return !val; }
111 template<> inline std::string negative(const std::string& val) { return val; }
112 
113 
115 template<typename T> struct Tolerance { static T value() { return zeroVal<T>(); } };
117 template<> struct Tolerance<float> { static float value() { return 1e-8f; } };
118 template<> struct Tolerance<double> { static double value() { return 1e-15; } };
120 
122 template<typename T> struct Delta { static T value() { return zeroVal<T>(); } };
124 template<> struct Delta<float> { static float value() { return 1e-5f; } };
125 template<> struct Delta<double> { static double value() { return 1e-9; } };
127 
128 
129 // ==========> Random Values <==================
130 
133 template<typename FloatType = double, typename EngineType = boost::mt19937>
134 class Rand01
135 {
136 private:
137  EngineType mEngine;
138  boost::uniform_01<FloatType> mRand;
139 
140 public:
141  typedef FloatType ValueType;
142 
145  Rand01(const EngineType& engine): mEngine(engine) {}
146 
149  Rand01(unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
150 
152  void setSeed(unsigned int seed)
153  {
154  mEngine.seed(static_cast<typename EngineType::result_type>(seed));
155  }
156 
158  const EngineType& engine() const { return mEngine; }
159 
161  FloatType operator()() { return mRand(mEngine); }
162 };
163 
165 
166 
169 template<typename IntType = int, typename EngineType = boost::mt19937>
170 class RandInt
171 {
172 private:
173 #if BOOST_VERSION >= 104700
174  typedef boost::random::uniform_int_distribution<IntType> Distr;
175 #else
176  typedef boost::uniform_int<IntType> Distr;
177 #endif
178  EngineType mEngine;
179  Distr mRand;
180 
181 public:
185  RandInt(const EngineType& engine, IntType imin, IntType imax):
186  mEngine(engine),
187  mRand(std::min(imin, imax), std::max(imin, imax))
188  {}
189 
193  RandInt(unsigned int seed, IntType imin, IntType imax):
194  mEngine(static_cast<typename EngineType::result_type>(seed)),
195  mRand(std::min(imin, imax), std::max(imin, imax))
196  {}
197 
199  void setRange(IntType imin, IntType imax)
200  {
201  mRand = Distr(std::min(imin, imax), std::max(imin, imax));
202  }
203 
205  void setSeed(unsigned int seed)
206  {
207  mEngine.seed(static_cast<typename EngineType::result_type>(seed));
208  }
209 
211  const EngineType& engine() const { return mEngine; }
212 
214  IntType operator()() { return mRand(mEngine); }
215 
218  IntType operator()(IntType imin, IntType imax)
219  {
220  const IntType lo = std::min(imin, imax), hi = std::max(imin, imax);
221 #if BOOST_VERSION >= 104700
222  return mRand(mEngine, typename Distr::param_type(lo, hi));
223 #else
224  return Distr(lo, hi)(mEngine);
225 #endif
226  }
227 };
228 
230 
231 
232 // ==========> Clamp <==================
233 
235 template<typename Type>
236 inline Type
237 Clamp(Type x, Type min, Type max)
238 {
239  assert(min<max);
240  return x > min ? x < max ? x : max : min;
241 }
242 
243 
245 template<typename Type>
246 inline Type
247 Clamp01(Type x) { return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0); }
248 
249 
251 template<typename Type>
252 inline bool
253 ClampTest01(Type &x)
254 {
255  if (x >= Type(0) && x <= Type(1)) return false;
256  x = x < Type(0) ? Type(0) : Type(1);
257  return true;
258 }
259 
261 template<typename Type>
262 inline Type
264 {
265  return x > 0 ? x < 1 ? (3-2*x)*x*x : Type(1) : Type(0);
266 }
267 
270 template<typename Type>
271 inline Type
272 SmoothUnitStep(Type x, Type min, Type max)
273 {
274  assert(min < max);
275  return SmoothUnitStep((x-min)/(max-min));
276 }
277 
278 
279 // ==========> Absolute Value <==================
280 
281 
283 inline int32_t Abs(int32_t i) { return abs(i); }
285 inline int64_t Abs(int64_t i)
286 {
287 #ifdef _MSC_VER
288  return (i < int64_t(0) ? -i : i);
289 #else
290  return labs(i);
291 #endif
292 }
293 inline float Abs(float x) { return fabsf(x); }
294 inline double Abs(double x) { return fabs(x); }
295 inline long double Abs(long double x) { return fabsl(x); }
296 inline uint32_t Abs(uint32_t i) { return i; }
297 inline uint64_t Abs(uint64_t i) { return i; }
298 // On OSX size_t and uint64_t are different types
299 #if defined(__APPLE__) || defined(MACOSX)
300 inline size_t Abs(size_t i) { return i; }
301 #endif
302 
303 
304 
306 
307 
308 // ==========> Value Comparison <==================
309 
310 
312 template<typename Type>
313 inline bool
314 isZero(const Type& x)
315 {
317  return x == zeroVal<Type>();
319 }
320 
321 
324 template<typename Type>
325 inline bool
326 isApproxZero(const Type& x)
327 {
328  const Type tolerance = Type(zeroVal<Type>() + Tolerance<Type>::value());
329  return x < tolerance && x > -tolerance;
330 }
331 
333 template<typename Type>
334 inline bool
335 isApproxZero(const Type& x, const Type& tolerance)
336 {
337  return x < tolerance && x > -tolerance;
338 }
339 
340 
342 template<typename Type>
343 inline bool
344 isNegative(const Type& x) { return x < zeroVal<Type>(); }
345 
347 template<> inline bool isNegative<bool>(const bool&) { return false; }
348 
349 
352 template<typename Type>
353 inline bool
354 isApproxEqual(const Type& a, const Type& b)
355 {
356  const Type tolerance = Type(zeroVal<Type>() + Tolerance<Type>::value());
357  return !(Abs(a - b) > tolerance);
358 }
359 
360 
362 template<typename Type>
363 inline bool
364 isApproxEqual(const Type& a, const Type& b, const Type& tolerance)
365 {
366  return !(Abs(a - b) > tolerance);
367 }
368 
369 #define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \
370  template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \
371  template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \
372 
373 
376 
377 
380 template<typename Type>
381 inline bool
382 isApproxLarger(const Type& a, const Type& b, const Type& tolerance)
383 {
384  return (b - a < tolerance);
385 }
386 
387 
389 template<typename T0, typename T1>
390 inline bool
391 isExactlyEqual(const T0& a, const T1& b)
392 {
394  return a == b;
396 }
397 
398 
399 template<typename Type>
400 inline bool
401 isRelOrApproxEqual(const Type& a, const Type& b, const Type& absTol, const Type& relTol)
402 {
403  // First check to see if we are inside the absolute tolerance
404  // Necessary for numbers close to 0
405  if (!(Abs(a - b) > absTol)) return true;
406 
407  // Next check to see if we are inside the relative tolerance
408  // to handle large numbers that aren't within the abs tolerance
409  // but could be the closest floating point representation
410  double relError;
411  if (Abs(b) > Abs(a)) {
412  relError = Abs((a - b) / b);
413  } else {
414  relError = Abs((a - b) / a);
415  }
416  return (relError <= relTol);
417 }
418 
419 template<>
420 inline bool
421 isRelOrApproxEqual(const bool& a, const bool& b, const bool&, const bool&)
422 {
423  return (a == b);
424 }
425 
426 
427 // Avoid strict aliasing issues by using type punning
428 // http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html
429 // Using "casting through a union(2)"
430 inline int32_t
431 floatToInt32(const float aFloatValue)
432 {
433  union FloatOrInt32 { float floatValue; int32_t int32Value; };
434  const FloatOrInt32* foi = reinterpret_cast<const FloatOrInt32*>(&aFloatValue);
435  return foi->int32Value;
436 }
437 
438 
439 inline int64_t
440 doubleToInt64(const double aDoubleValue)
441 {
442  union DoubleOrInt64 { double doubleValue; int64_t int64Value; };
443  const DoubleOrInt64* dol = reinterpret_cast<const DoubleOrInt64*>(&aDoubleValue);
444  return dol->int64Value;
445 }
446 
447 
448 // aUnitsInLastPlace is the allowed difference between the least significant digits
449 // of the numbers' floating point representation
450 // Please read refernce paper before trying to use isUlpsEqual
451 // http://www.cygnus-software.com/papers/comparingFloats/comparingFloats.htm
452 inline bool
453 isUlpsEqual(const double aLeft, const double aRight, const int64_t aUnitsInLastPlace)
454 {
455  int64_t longLeft = doubleToInt64(aLeft);
456  // Because of 2's complement, must restore lexicographical order
457  if (longLeft < 0) {
458  longLeft = INT64_C(0x8000000000000000) - longLeft;
459  }
460 
461  int64_t longRight = doubleToInt64(aRight);
462  // Because of 2's complement, must restore lexicographical order
463  if (longRight < 0) {
464  longRight = INT64_C(0x8000000000000000) - longRight;
465  }
466 
467  int64_t difference = labs(longLeft - longRight);
468  return (difference <= aUnitsInLastPlace);
469 }
470 
471 inline bool
472 isUlpsEqual(const float aLeft, const float aRight, const int32_t aUnitsInLastPlace)
473 {
474  int32_t intLeft = floatToInt32(aLeft);
475  // Because of 2's complement, must restore lexicographical order
476  if (intLeft < 0) {
477  intLeft = 0x80000000 - intLeft;
478  }
479 
480  int32_t intRight = floatToInt32(aRight);
481  // Because of 2's complement, must restore lexicographical order
482  if (intRight < 0) {
483  intRight = 0x80000000 - intRight;
484  }
485 
486  int32_t difference = abs(intLeft - intRight);
487  return (difference <= aUnitsInLastPlace);
488 }
489 
490 
492 
493 
494 // ==========> Pow <==================
495 
497 template<typename Type>
498 inline Type Pow2(Type x) { return x*x; }
499 
501 template<typename Type>
502 inline Type Pow3(Type x) { return x*x*x; }
503 
505 template<typename Type>
506 inline Type Pow4(Type x) { return Pow2(Pow2(x)); }
507 
509 template<typename Type>
510 Type
511 Pow(Type x, int n)
512 {
513  Type ans = 1;
514  if (n < 0) {
515  n = -n;
516  x = Type(1)/x;
517  }
518  while (n--) ans *= x;
519  return ans;
520 }
521 
523 inline float
525 Pow(float b, float e)
526 {
527  assert( b >= 0.0f && "Pow(float,float): base is negative" );
528  return powf(b,e);
529 }
530 
531 inline double
532 Pow(double b, double e)
533 {
534  assert( b >= 0.0 && "Pow(double,double): base is negative" );
535  return pow(b,e);
536 }
538 
539 
540 // ==========> Max <==================
541 
543 template<typename Type>
544 inline const Type&
545 Max(const Type& a, const Type& b)
546 {
547  return std::max(a,b) ;
548 }
549 
551 template<typename Type>
552 inline const Type&
553 Max(const Type& a, const Type& b, const Type& c)
554 {
555  return std::max( std::max(a,b), c ) ;
556 }
557 
559 template<typename Type>
560 inline const Type&
561 Max(const Type& a, const Type& b, const Type& c, const Type& d)
562 {
563  return std::max(std::max(a,b), std::max(c,d));
564 }
565 
567 template<typename Type>
568 inline const Type&
569 Max(const Type& a, const Type& b, const Type& c, const Type& d, const Type& e)
570 {
571  return std::max(std::max(a,b), Max(c,d,e));
572 }
573 
575 template<typename Type>
576 inline const Type&
577 Max(const Type& a, const Type& b, const Type& c, const Type& d, const Type& e, const Type& f)
578 {
579  return std::max(Max(a,b,c), Max(d,e,f));
580 }
581 
583 template<typename Type>
584 inline const Type&
585 Max(const Type& a, const Type& b, const Type& c, const Type& d,
586  const Type& e, const Type& f, const Type& g)
587 {
588  return std::max(Max(a,b,c,d), Max(e,f,g));
589 }
590 
592 template<typename Type>
593 inline const Type&
594 Max(const Type& a, const Type& b, const Type& c, const Type& d,
595  const Type& e, const Type& f, const Type& g, const Type& h)
596 {
597  return std::max(Max(a,b,c,d), Max(e,f,g,h));
598 }
599 
600 
601 // ==========> Min <==================
602 
604 template<typename Type>
605 inline const Type&
606 Min(const Type& a, const Type& b) { return std::min(a, b); }
607 
609 template<typename Type>
610 inline const Type&
611 Min(const Type& a, const Type& b, const Type& c) { return std::min(std::min(a, b), c); }
612 
614 template<typename Type>
615 inline const Type&
616 Min(const Type& a, const Type& b, const Type& c, const Type& d)
617 {
618  return std::min(std::min(a, b), std::min(c, d));
619 }
620 
622 template<typename Type>
623 inline const Type&
624 Min(const Type& a, const Type& b, const Type& c, const Type& d, const Type& e)
625 {
626  return std::min(std::min(a,b), Min(c,d,e));
627 }
628 
630 template<typename Type>
631 inline const Type&
632 Min(const Type& a, const Type& b, const Type& c, const Type& d, const Type& e, const Type& f)
633 {
634  return std::min(Min(a,b,c), Min(d,e,f));
635 }
636 
638 template<typename Type>
639 inline const Type&
640 Min(const Type& a, const Type& b, const Type& c, const Type& d,
641  const Type& e, const Type& f, const Type& g)
642 {
643  return std::min(Min(a,b,c,d), Min(e,f,g));
644 }
645 
647 template<typename Type>
648 inline const Type&
649 Min(const Type& a, const Type& b, const Type& c, const Type& d,
650  const Type& e, const Type& f, const Type& g, const Type& h)
651 {
652  return std::min(Min(a,b,c,d), Min(e,f,g,h));
653 }
654 
655 
656 // ============> Exp <==================
657 
659 template<typename Type>
660 inline Type Exp(const Type& x) { return std::exp(x); }
661 
662 
664 
665 
667 template <typename Type>
668 inline int Sign(const Type &x) { return (zeroVal<Type>() < x) - (x < zeroVal<Type>()); }
669 
670 
673 template <typename Type>
674 inline bool
675 SignChange(const Type& a, const Type& b)
676 {
677  return ( (a<zeroVal<Type>()) ^ (b<zeroVal<Type>()) );
678 }
679 
680 
683 template <typename Type>
684 inline bool
685 ZeroCrossing(const Type& a, const Type& b)
686 {
687  return a * b <= zeroVal<Type>();
688 }
689 
690 
692 inline float Sqrt(float x) { return sqrtf(x); }
694 inline double Sqrt(double x) { return sqrt(x); }
695 inline long double Sqrt(long double x) { return sqrtl(x); }
697 
698 
700 inline float Cbrt(float x) { return boost::math::cbrt(x); }
702 inline double Cbrt(double x) { return boost::math::cbrt(x); }
703 inline long double Cbrt(long double x) { return boost::math::cbrt(x); }
705 
706 
708 inline int Mod(int x, int y) { return (x % y); }
710 inline float Mod(float x, float y) { return fmodf(x,y); }
711 inline double Mod(double x, double y) { return fmod(x,y); }
712 inline long double Mod(long double x, long double y) { return fmodl(x,y); }
713 template<typename Type> inline Type Remainder(Type x, Type y) { return Mod(x,y); }
715 
716 
718 inline float RoundUp(float x) { return ceilf(x); }
720 inline double RoundUp(double x) { return ceil(x); }
721 inline long double RoundUp(long double x) { return ceill(x); }
723 template<typename Type>
725 inline Type
726 RoundUp(Type x, Type base)
727 {
728  Type remainder = Remainder(x, base);
729  return remainder ? x-remainder+base : x;
730 }
731 
732 
734 inline float RoundDown(float x) { return floorf(x); }
736 inline double RoundDown(double x) { return floor(x); }
737 inline long double RoundDown(long double x) { return floorl(x); }
739 template<typename Type>
741 inline Type
742 RoundDown(Type x, Type base)
743 {
744  Type remainder = Remainder(x, base);
745  return remainder ? x-remainder : x;
746 }
747 
748 
750 inline float Round(float x) { return RoundDown(x + 0.5f); }
752 inline double Round(double x) { return RoundDown(x + 0.5); }
753 inline long double Round(long double x) { return RoundDown(x + 0.5l); }
755 
756 
759 template<typename Type>
760 inline Type
761 EuclideanRemainder(Type x) { return x - RoundDown(x); }
762 
763 
765 template<typename Type>
766 inline Type
767 IntegerPart(Type x)
768 {
769  return (x > 0 ? RoundDown(x) : RoundUp(x));
770 }
771 
773 template<typename Type>
774 inline Type
775 FractionalPart(Type x) { return Mod(x,Type(1)); }
776 
777 
779 inline int Floor(float x) { return int(RoundDown(x)); }
781 inline int Floor(double x) { return int(RoundDown(x)); }
782 inline int Floor(long double x) { return int(RoundDown(x)); }
784 
785 
787 inline int Ceil(float x) { return int(RoundUp(x)); }
789 inline int Ceil(double x) { return int(RoundUp(x)); }
790 inline int Ceil(long double x) { return int(RoundUp(x)); }
792 
793 
795 template<typename Type>
796 inline Type Chop(Type x, Type delta) { return (Abs(x) < delta ? zeroVal<Type>() : x); }
797 
798 
800 template<typename Type>
801 inline Type
802 Truncate(Type x, unsigned int digits)
803 {
804  Type tenth = Pow(10,digits);
805  return RoundDown(x*tenth+0.5)/tenth;
806 }
807 
808 
810 
811 
813 template<typename Type>
814 inline Type
815 Inv(Type x)
816 {
817  assert(x);
818  return Type(1)/x;
819 }
820 
821 
822 enum Axis {
823  X_AXIS = 0,
824  Y_AXIS = 1,
825  Z_AXIS = 2
826 };
827 
828 // enum values are consistent with their historical mx analogs.
838 };
839 
840 
841 template <typename S, typename T>
842 struct promote {
843  typedef typename boost::numeric::conversion_traits<S, T>::supertype type;
844 };
845 
846 
854 template<typename Vec3T>
855 size_t
856 MinIndex(const Vec3T& v)
857 {
858 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics
859  static
860 #endif
861  const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };//9 is a dummy value
862  const size_t hashKey =
863  ((v[0] < v[1]) << 2) + ((v[0] < v[2]) << 1) + (v[1] < v[2]);// ?*4+?*2+?*1
864  return hashTable[hashKey];
865 }
866 
867 
875 template<typename Vec3T>
876 size_t
877 MaxIndex(const Vec3T& v)
878 {
879 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics
880  static
881 #endif
882  const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };//9 is a dummy value
883  const size_t hashKey =
884  ((v[0] > v[1]) << 2) + ((v[0] > v[2]) << 1) + (v[1] > v[2]);// ?*4+?*2+?*1
885  return hashTable[hashKey];
886 }
887 
888 } // namespace math
889 } // namespace OPENVDB_VERSION_NAME
890 } // namespace openvdb
891 
892 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED
893 
894 // Copyright (c) 2012-2014 DreamWorks Animation LLC
895 // All rights reserved. This software is distributed under the
896 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:344
bool isUlpsEqual(const float aLeft, const float aRight, const int32_t aUnitsInLastPlace)
Definition: Math.h:472
const Type & Min(const Type &a, const Type &b, const Type &c, const Type &d, const Type &e, const Type &f, const Type &g, const Type &h)
Return the minimum of eight values.
Definition: Math.h:649
bool isNegative< bool >(const bool &)
Return false, since bool values are never less than zero.
Definition: Math.h:347
Delta for small floating-point offsets.
Definition: Math.h:123
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:152
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:134
bool ClampTest01(Type &x)
Return true if x is outside [0,1].
Definition: Math.h:253
Type Clamp01(Type x)
Return x clamped to [0, 1].
Definition: Math.h:247
Type Pow2(Type x)
Return .
Definition: Math.h:498
Type EuclideanRemainder(Type x)
Definition: Math.h:761
int64_t doubleToInt64(const double aDoubleValue)
Definition: Math.h:440
Type Remainder(Type x, Type y)
Return the remainder of x / y.
Definition: Math.h:713
long double Mod(long double x, long double y)
Return the remainder of x / y.
Definition: Math.h:712
FloatType operator()()
Return a uniformly distributed random number in the range [0, 1).
Definition: Math.h:161
static float value()
Definition: Math.h:117
bool isRelOrApproxEqual(const bool &a, const bool &b, const bool &, const bool &)
Definition: Math.h:421
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:877
Rand01(unsigned int seed)
Initialize the generator.
Definition: Math.h:149
static float value()
Definition: Math.h:124
bool zeroVal< bool >()
Return the bool value that corresponds to zero.
Definition: Math.h:89
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:205
Type RoundDown(Type x, Type base)
Return x rounded down to the nearest multiple of base.
Definition: Math.h:742
long double Cbrt(long double x)
Return the cube root of a floating-point value.
Definition: Math.h:703
long double Sqrt(long double x)
Return the square root of a floating-point value.
Definition: Math.h:695
int Sign(const Type &x)
Return the sign of the given value as an integer (either -1, 0 or 1).
Definition: Math.h:668
double Pow(double b, double e)
Return .
Definition: Math.h:532
bool ZeroCrossing(const Type &a, const Type &b)
Return true if the interval [a, b] includes zero, i.e., if either a or b is zero or if they have diff...
Definition: Math.h:685
#define OPENVDB_VERSION_NAME
Definition: version.h:43
const Type & Max(const Type &a, const Type &b, const Type &c, const Type &d, const Type &e, const Type &f, const Type &g, const Type &h)
Return the maximum of eight values.
Definition: Math.h:594
std::string operator+(const std::string &s, double)
Needed to support the (zeroVal() + val) idiom when ValueType is std::string.
Definition: Math.h:98
static double value()
Definition: Math.h:125
void setRange(IntType imin, IntType imax)
Change the range over which integers are distributed to [imin, imax].
Definition: Math.h:199
Definition: Math.h:842
Type Chop(Type x, Type delta)
Return x if it is greater in magnitude than delta. Otherwise, return zero.
Definition: Math.h:796
bool isApproxLarger(const Type &a, const Type &b, const Type &tolerance)
Return true if a is larger than b to within the given tolerance, i.e., if b - a < tolerance...
Definition: Math.h:382
Simple random integer generator.
Definition: Math.h:170
int Floor(long double x)
Return the floor of x.
Definition: Math.h:782
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Definition: Math.h:364
IntType operator()()
Return a randomly-generated integer in the current range.
Definition: Math.h:214
Type Pow4(Type x)
Return .
Definition: Math.h:506
Definition: Exceptions.h:39
RotationOrder
Definition: Math.h:829
bool SignChange(const Type &a, const Type &b)
Return true if a and b have different signs.
Definition: Math.h:675
IntType operator()(IntType imin, IntType imax)
Return a randomly-generated integer in the new range [imin, imax], without changing the current range...
Definition: Math.h:218
Type Pow3(Type x)
Return .
Definition: Math.h:502
RandInt< int, boost::mt19937 > RandomInt
Definition: Math.h:229
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:856
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition: Math.h:74
FloatType ValueType
Definition: Math.h:141
boost::numeric::conversion_traits< S, T >::supertype type
Definition: Math.h:843
Type FractionalPart(Type x)
Return the fractional part of x.
Definition: Math.h:775
Axis
Definition: Math.h:822
Definition: Math.h:823
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition: Math.h:73
Rand01< double, boost::mt19937 > Random01
Definition: Math.h:164
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:211
static double value()
Definition: Math.h:118
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
long double Round(long double x)
Return x rounded to the nearest integer.
Definition: Math.h:753
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:391
Definition: Math.h:824
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:802
std::string negative(const std::string &val)
Return the "negation" of the given string.
Definition: Math.h:111
RandInt(unsigned int seed, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:193
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Type SmoothUnitStep(Type x, Type min, Type max)
Return 0 if x < min, 1 if x > max or else , where .
Definition: Math.h:272
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:158
Rand01(const EngineType &engine)
Initialize the generator.
Definition: Math.h:145
int32_t floatToInt32(const float aFloatValue)
Definition: Math.h:431
Type Exp(const Type &x)
Return .
Definition: Math.h:660
int Ceil(long double x)
Return the ceiling of x.
Definition: Math.h:790
bool isApproxZero(const Type &x, const Type &tolerance)
Return true if x is equal to zero to within the given tolerance.
Definition: Math.h:335
T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:85
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:314
Type Inv(Type x)
Return the inverse of x.
Definition: Math.h:815
Type RoundUp(Type x, Type base)
Return x rounded up to the nearest multiple of base.
Definition: Math.h:726
RandInt(const EngineType &engine, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:185
Definition: Math.h:825
#define OPENVDB_EXACT_IS_APPROX_EQUAL(T)
Definition: Math.h:369
Type Clamp(Type x, Type min, Type max)
Return x clamped to [min, max].
Definition: Math.h:237
uint64_t Abs(uint64_t i)
Return the absolute value of the given quantity.
Definition: Math.h:297
Type IntegerPart(Type x)
Return the integer part of x.
Definition: Math.h:767
Tolerance for floating-point comparison.
Definition: Math.h:116