OpenVDB  5.0.0
Math.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2017 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 <openvdb/Platform.h>
39 #include <openvdb/version.h>
40 #include <boost/numeric/conversion/conversion_traits.hpp>
41 #include <algorithm> // for std::max()
42 #include <cassert>
43 #include <cmath> // for std::ceil(), std::fabs(), std::pow(), std::sqrt(), etc.
44 #include <cstdlib> // for abs(int)
45 #include <random>
46 #include <string>
47 #include <type_traits> // for std::is_arithmetic
48 
49 
50 // Compile pragmas
51 
52 // Intel(r) compiler fires remark #1572: floating-point equality and inequality
53 // comparisons are unrealiable when == or != is used with floating point operands.
54 #if defined(__INTEL_COMPILER)
55  #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
56  _Pragma("warning (push)") \
57  _Pragma("warning (disable:1572)")
58  #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
59  _Pragma("warning (pop)")
60 #elif defined(__clang__)
61  #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
62  PRAGMA(clang diagnostic push) \
63  PRAGMA(clang diagnostic ignored "-Wfloat-equal")
64  #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
65  PRAGMA(clang diagnostic pop)
66 #else
67  // For GCC, #pragma GCC diagnostic ignored "-Wfloat-equal"
68  // isn't working until gcc 4.2+,
69  // Trying
70  // #pragma GCC system_header
71  // creates other problems, most notably "warning: will never be executed"
72  // in from templates, unsure of how to work around.
73  // If necessary, could use integer based comparisons for equality
74  #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
75  #define OPENVDB_NO_FP_EQUALITY_WARNING_END
76 #endif
77 
78 namespace openvdb {
80 namespace OPENVDB_VERSION_NAME {
81 
86 template<typename T> inline T zeroVal() { return T(0); }
88 template<> inline std::string zeroVal<std::string>() { return ""; }
90 template<> inline bool zeroVal<bool>() { return false; }
91 
93 
94 inline std::string operator+(const std::string& s, bool) { return s; }
97 inline std::string operator+(const std::string& s, int) { return s; }
98 inline std::string operator+(const std::string& s, float) { return s; }
99 inline std::string operator+(const std::string& s, double) { return s; }
101 
102 
103 namespace math {
104 
108 template<typename T> inline T negative(const T& val) { return T(-val); }
110 template<> inline bool negative(const bool& val) { return !val; }
112 template<> inline std::string negative(const std::string& val) { return val; }
113 
114 
116 template<typename T> struct Tolerance { static T value() { return zeroVal<T>(); } };
118 template<> struct Tolerance<float> { static float value() { return 1e-8f; } };
119 template<> struct Tolerance<double> { static double value() { return 1e-15; } };
121 
123 template<typename T> struct Delta { static T value() { return zeroVal<T>(); } };
125 template<> struct Delta<float> { static float value() { return 1e-5f; } };
126 template<> struct Delta<double> { static double value() { return 1e-9; } };
128 
129 
130 // ==========> Random Values <==================
131 
134 template<typename FloatType = double, typename EngineType = std::mt19937>
135 class Rand01
136 {
137 private:
138  EngineType mEngine;
139  std::uniform_real_distribution<FloatType> mRand;
140 
141 public:
142  using ValueType = FloatType;
143 
146  Rand01(const EngineType& engine): mEngine(engine) {}
147 
150  Rand01(unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
151 
153  void setSeed(unsigned int seed)
154  {
155  mEngine.seed(static_cast<typename EngineType::result_type>(seed));
156  }
157 
159  const EngineType& engine() const { return mEngine; }
160 
162  FloatType operator()() { return mRand(mEngine); }
163 };
164 
166 
167 
170 template<typename IntType = int, typename EngineType = std::mt19937>
171 class RandInt
172 {
173 private:
174  using Distr = std::uniform_int_distribution<IntType>;
175  EngineType mEngine;
176  Distr mRand;
177 
178 public:
182  RandInt(const EngineType& engine, IntType imin, IntType imax):
183  mEngine(engine),
184  mRand(std::min(imin, imax), std::max(imin, imax))
185  {}
186 
190  RandInt(unsigned int seed, IntType imin, IntType imax):
191  mEngine(static_cast<typename EngineType::result_type>(seed)),
192  mRand(std::min(imin, imax), std::max(imin, imax))
193  {}
194 
196  void setRange(IntType imin, IntType imax)
197  {
198  mRand = Distr(std::min(imin, imax), std::max(imin, imax));
199  }
200 
202  void setSeed(unsigned int seed)
203  {
204  mEngine.seed(static_cast<typename EngineType::result_type>(seed));
205  }
206 
208  const EngineType& engine() const { return mEngine; }
209 
211  IntType operator()() { return mRand(mEngine); }
212 
215  IntType operator()(IntType imin, IntType imax)
216  {
217  const IntType lo = std::min(imin, imax), hi = std::max(imin, imax);
218  return mRand(mEngine, typename Distr::param_type(lo, hi));
219  }
220 };
221 
223 
224 
225 // ==========> Clamp <==================
226 
228 template<typename Type>
229 inline Type
230 Clamp(Type x, Type min, Type max)
231 {
232  assert( !(min>max) );
233  return x > min ? x < max ? x : max : min;
234 }
235 
236 
238 template<typename Type>
239 inline Type
240 Clamp01(Type x) { return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0); }
241 
242 
244 template<typename Type>
245 inline bool
246 ClampTest01(Type &x)
247 {
248  if (x >= Type(0) && x <= Type(1)) return false;
249  x = x < Type(0) ? Type(0) : Type(1);
250  return true;
251 }
252 
254 template<typename Type>
255 inline Type
257 {
258  return x > 0 ? x < 1 ? (3-2*x)*x*x : Type(1) : Type(0);
259 }
260 
263 template<typename Type>
264 inline Type
265 SmoothUnitStep(Type x, Type min, Type max)
266 {
267  assert(min < max);
268  return SmoothUnitStep((x-min)/(max-min));
269 }
270 
271 
272 // ==========> Absolute Value <==================
273 
274 
276 inline int32_t Abs(int32_t i) { return abs(i); }
278 inline int64_t Abs(int64_t i)
279 {
280 #ifdef _MSC_VER
281  return (i < int64_t(0) ? -i : i);
282 #else
283  return labs(i);
284 #endif
285 }
286 inline float Abs(float x) { return std::fabs(x); }
287 inline double Abs(double x) { return std::fabs(x); }
288 inline long double Abs(long double x) { return std::fabs(x); }
289 inline uint32_t Abs(uint32_t i) { return i; }
290 inline uint64_t Abs(uint64_t i) { return i; }
291 inline bool Abs(bool b) { return b; }
292 // On OSX size_t and uint64_t are different types
293 #if defined(__APPLE__) || defined(MACOSX)
294 inline size_t Abs(size_t i) { return i; }
295 #endif
296 
297 
298 
300 
301 
302 // ==========> Value Comparison <==================
303 
304 
306 template<typename Type>
307 inline bool
308 isZero(const Type& x)
309 {
311  return x == zeroVal<Type>();
313 }
314 
315 
318 template<typename Type>
319 inline bool
320 isApproxZero(const Type& x)
321 {
322  const Type tolerance = Type(zeroVal<Type>() + Tolerance<Type>::value());
323  return !(x > tolerance) && !(x < -tolerance);
324 }
325 
327 template<typename Type>
328 inline bool
329 isApproxZero(const Type& x, const Type& tolerance)
330 {
331  return !(x > tolerance) && !(x < -tolerance);
332 }
333 
334 
336 template<typename Type>
337 inline bool
338 isNegative(const Type& x) { return x < zeroVal<Type>(); }
339 
341 template<> inline bool isNegative<bool>(const bool&) { return false; }
342 
343 
345 template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value, int>::type = 0>
346 inline bool
347 isFinite(const Type& x) { return std::isfinite(x); }
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 the reference 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 std::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 // ============> Sin <==================
663 
665 inline float Sin(const float& x) { return std::sin(x); }
667 
668 inline double Sin(const double& x) { return std::sin(x); }
670 
671 // ============> Cos <==================
672 
674 inline float Cos(const float& x) { return std::cos(x); }
676 
677 inline double Cos(const double& x) { return std::cos(x); }
679 
680 
682 
683 
685 template <typename Type>
686 inline int Sign(const Type &x) { return (zeroVal<Type>() < x) - (x < zeroVal<Type>()); }
687 
688 
691 template <typename Type>
692 inline bool
693 SignChange(const Type& a, const Type& b)
694 {
695  return ( (a<zeroVal<Type>()) ^ (b<zeroVal<Type>()) );
696 }
697 
698 
701 template <typename Type>
702 inline bool
703 ZeroCrossing(const Type& a, const Type& b)
704 {
705  return a * b <= zeroVal<Type>();
706 }
707 
708 
710 inline float Sqrt(float x) { return std::sqrt(x); }
712 inline double Sqrt(double x) { return std::sqrt(x); }
713 inline long double Sqrt(long double x) { return std::sqrt(x); }
715 
716 
718 inline float Cbrt(float x) { return std::cbrt(x); }
720 inline double Cbrt(double x) { return std::cbrt(x); }
721 inline long double Cbrt(long double x) { return std::cbrt(x); }
723 
724 
726 inline int Mod(int x, int y) { return (x % y); }
728 inline float Mod(float x, float y) { return std::fmod(x, y); }
729 inline double Mod(double x, double y) { return std::fmod(x, y); }
730 inline long double Mod(long double x, long double y) { return std::fmod(x, y); }
731 template<typename Type> inline Type Remainder(Type x, Type y) { return Mod(x, y); }
733 
734 
736 inline float RoundUp(float x) { return std::ceil(x); }
738 inline double RoundUp(double x) { return std::ceil(x); }
739 inline long double RoundUp(long double x) { return std::ceil(x); }
741 template<typename Type>
743 inline Type
744 RoundUp(Type x, Type base)
745 {
746  Type remainder = Remainder(x, base);
747  return remainder ? x-remainder+base : x;
748 }
749 
750 
752 inline float RoundDown(float x) { return std::floor(x); }
754 inline double RoundDown(double x) { return std::floor(x); }
755 inline long double RoundDown(long double x) { return std::floor(x); }
757 template<typename Type>
759 inline Type
760 RoundDown(Type x, Type base)
761 {
762  Type remainder = Remainder(x, base);
763  return remainder ? x-remainder : x;
764 }
765 
766 
768 inline float Round(float x) { return RoundDown(x + 0.5f); }
770 inline double Round(double x) { return RoundDown(x + 0.5); }
771 inline long double Round(long double x) { return RoundDown(x + 0.5l); }
773 
774 
777 template<typename Type>
778 inline Type
779 EuclideanRemainder(Type x) { return x - RoundDown(x); }
780 
781 
783 template<typename Type>
784 inline Type
785 IntegerPart(Type x)
786 {
787  return (x > 0 ? RoundDown(x) : RoundUp(x));
788 }
789 
791 template<typename Type>
792 inline Type
793 FractionalPart(Type x) { return Mod(x,Type(1)); }
794 
795 
797 inline int Floor(float x) { return int(RoundDown(x)); }
799 inline int Floor(double x) { return int(RoundDown(x)); }
800 inline int Floor(long double x) { return int(RoundDown(x)); }
802 
803 
805 inline int Ceil(float x) { return int(RoundUp(x)); }
807 inline int Ceil(double x) { return int(RoundUp(x)); }
808 inline int Ceil(long double x) { return int(RoundUp(x)); }
810 
811 
813 template<typename Type>
814 inline Type Chop(Type x, Type delta) { return (Abs(x) < delta ? zeroVal<Type>() : x); }
815 
816 
818 template<typename Type>
819 inline Type
820 Truncate(Type x, unsigned int digits)
821 {
822  Type tenth = Pow(10,digits);
823  return RoundDown(x*tenth+0.5)/tenth;
824 }
825 
826 
828 
829 
832 template<typename T>
833 inline auto PrintCast(const T& val) -> typename std::enable_if<!std::is_same<T, int8_t>::value
834  && !std::is_same<T, uint8_t>::value, const T&>::type { return val; }
835 inline int32_t PrintCast(int8_t val) { return int32_t(val); }
836 inline uint32_t PrintCast(uint8_t val) { return uint32_t(val); }
837 
838 
840 
841 
843 template<typename Type>
844 inline Type
845 Inv(Type x)
846 {
847  assert(x);
848  return Type(1)/x;
849 }
850 
851 
852 enum Axis {
853  X_AXIS = 0,
854  Y_AXIS = 1,
855  Z_AXIS = 2
856 };
857 
858 // enum values are consistent with their historical mx analogs.
868 };
869 
870 
871 template <typename S, typename T>
872 struct promote {
873  using type = typename boost::numeric::conversion_traits<S, T>::supertype;
874 };
875 
876 
884 template<typename Vec3T>
885 size_t
886 MinIndex(const Vec3T& v)
887 {
888 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics
889  static
890 #endif
891  const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };//9 is a dummy value
892  const size_t hashKey =
893  ((v[0] < v[1]) << 2) + ((v[0] < v[2]) << 1) + (v[1] < v[2]);// ?*4+?*2+?*1
894  return hashTable[hashKey];
895 }
896 
897 
905 template<typename Vec3T>
906 size_t
907 MaxIndex(const Vec3T& v)
908 {
909 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics
910  static
911 #endif
912  const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };//9 is a dummy value
913  const size_t hashKey =
914  ((v[0] > v[1]) << 2) + ((v[0] > v[2]) << 1) + (v[1] > v[2]);// ?*4+?*2+?*1
915  return hashTable[hashKey];
916 }
917 
918 } // namespace math
919 } // namespace OPENVDB_VERSION_NAME
920 } // namespace openvdb
921 
922 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED
923 
924 // Copyright (c) 2012-2017 DreamWorks Animation LLC
925 // All rights reserved. This software is distributed under the
926 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
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
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:907
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:215
Definition: Math.h:867
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:391
bool ClampTest01(Type &x)
Return true if x is outside [0,1].
Definition: Math.h:246
Delta for small floating-point offsets.
Definition: Math.h:124
long double Mod(long double x, long double y)
Return the remainder of x / y.
Definition: Math.h:730
Type SmoothUnitStep(Type x, Type min, Type max)
Return 0 if x < min, 1 if x > max or else (3 − 2 t) t², where t = (x − min)/(max − min)...
Definition: Math.h:265
int Sign(const Type &x)
Return the sign of the given value as an integer (either -1, 0 or 1).
Definition: Math.h:686
Rand01(unsigned int seed)
Initialize the generator.
Definition: Math.h:150
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
Definition: Math.h:872
RotationOrder
Definition: Math.h:859
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:338
Type RoundUp(Type x, Type base)
Return x rounded up to the nearest multiple of base.
Definition: Math.h:744
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:135
Definition: Math.h:863
Type EuclideanRemainder(Type x)
Definition: Math.h:779
Definition: Math.h:862
FloatType operator()()
Return a uniformly distributed random number in the range [0, 1).
Definition: Math.h:162
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
Definition: Math.h:861
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:886
static float value()
Definition: Math.h:118
Definition: Math.h:860
Type Pow2(Type x)
Return x2.
Definition: Math.h:498
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:129
bool SignChange(const Type &a, const Type &b)
Return true if a and b have different signs.
Definition: Math.h:693
int Floor(long double x)
Return the floor of x.
Definition: Math.h:800
int32_t floatToInt32(const float aFloatValue)
Definition: Math.h:431
T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:86
Definition: Math.h:866
bool isNegative< bool >(const bool &)
Return false, since bool values are never less than zero.
Definition: Math.h:341
Simple random integer generator.
Definition: Math.h:171
Type Remainder(Type x, Type y)
Return the remainder of x / y.
Definition: Math.h:731
Type Pow4(Type x)
Return x4.
Definition: Math.h:506
Type Clamp(Type x, Type min, Type max)
Return x clamped to [min, max].
Definition: Math.h:230
Tolerance for floating-point comparison.
Definition: Math.h:117
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
double Cos(const double &x)
Return cos x.
Definition: Math.h:677
int Ceil(long double x)
Return the ceiling of x.
Definition: Math.h:808
Type FractionalPart(Type x)
Return the fractional part of x.
Definition: Math.h:793
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
Rand01(const EngineType &engine)
Initialize the generator.
Definition: Math.h:146
Axis
Definition: Math.h:852
Definition: Math.h:865
long double Sqrt(long double x)
Return the square root of a floating-point value.
Definition: Math.h:713
Type RoundDown(Type x, Type base)
Return x rounded down to the nearest multiple of base.
Definition: Math.h:760
IntType operator()()
Return a randomly-generated integer in the current range.
Definition: Math.h:211
double Sin(const double &x)
Return sin x.
Definition: Math.h:668
Type Chop(Type x, Type delta)
Return x if it is greater or equal in magnitude than delta. Otherwise, return zero.
Definition: Math.h:814
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:202
RandInt(const EngineType &engine, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:182
Definition: Exceptions.h:39
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:208
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:703
bool isRelOrApproxEqual(const bool &a, const bool &b, const bool &, const bool &)
Definition: Math.h:421
uint32_t PrintCast(uint8_t val)
Definition: Math.h:836
void setRange(IntType imin, IntType imax)
Change the range over which integers are distributed to [imin, imax].
Definition: Math.h:196
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition: Math.h:75
double Pow(double b, double e)
Return be.
Definition: Math.h:532
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
Type IntegerPart(Type x)
Return the integer part of x.
Definition: Math.h:785
typename boost::numeric::conversion_traits< S, T >::supertype type
Definition: Math.h:873
Definition: Math.h:855
static double value()
Definition: Math.h:126
bool isUlpsEqual(const float aLeft, const float aRight, const int32_t aUnitsInLastPlace)
Definition: Math.h:472
long double Cbrt(long double x)
Return the cube root of a floating-point value.
Definition: Math.h:721
Library and file format version numbers.
Definition: Math.h:854
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308
std::string negative(const std::string &val)
Return the "negation" of the given string.
Definition: Math.h:112
bool zeroVal< bool >()
Return the bool value that corresponds to zero.
Definition: Math.h:90
bool Abs(bool b)
Return the absolute value of the given quantity.
Definition: Math.h:291
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition: Math.h:74
Definition: Math.h:864
Type Exp(const Type &x)
Return ex.
Definition: Math.h:660
Type Pow3(Type x)
Return x3.
Definition: Math.h:502
RandInt(unsigned int seed, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:190
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:820
static float value()
Definition: Math.h:125
static double value()
Definition: Math.h:119
std::string operator+(const std::string &s, double)
Needed to support the (zeroVal<ValueType>() + val) idiom when ValueType is std::string.
Definition: Math.h:99
Definition: Math.h:853
int64_t doubleToInt64(const double aDoubleValue)
Definition: Math.h:440
bool isFinite(const Type &x)
Return true if x is finite.
Definition: Math.h:347
bool isApproxZero(const Type &x, const Type &tolerance)
Return true if x is equal to zero to within the given tolerance.
Definition: Math.h:329
Type Inv(Type x)
Return the inverse of x.
Definition: Math.h:845
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:153
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
long double Round(long double x)
Return x rounded to the nearest integer.
Definition: Math.h:771
Type Clamp01(Type x)
Return x clamped to [0, 1].
Definition: Math.h:240
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:159
#define OPENVDB_EXACT_IS_APPROX_EQUAL(T)
Definition: Math.h:369