35 #ifndef OPENVDB_MATH_HAS_BEEN_INCLUDED
36 #define OPENVDB_MATH_HAS_BEEN_INCLUDED
45 #include <boost/numeric/conversion/conversion_traits.hpp>
46 #include <boost/math/special_functions/cbrt.hpp>
47 #include <boost/random/mersenne_twister.hpp>
48 #include <boost/random/uniform_01.hpp>
49 #include <boost/random/uniform_int.hpp>
50 #include <boost/version.hpp>
51 #include <openvdb/Platform.h>
52 #include <openvdb/version.h>
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)")
73 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
74 #define OPENVDB_NO_FP_EQUALITY_WARNING_END
85 template<
typename T>
inline T
zeroVal() {
return T(0); }
87 template<>
inline std::string zeroVal<std::string>() {
return ""; }
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; }
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; }
115 template<
typename T>
struct Tolerance {
static T value() {
return zeroVal<T>(); } };
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; } };
133 template<
typename FloatType =
double,
typename EngineType = boost::mt19937>
138 boost::uniform_01<FloatType> mRand;
145 Rand01(
const EngineType& engine): mEngine(engine) {}
149 Rand01(
unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
154 mEngine.seed(static_cast<typename EngineType::result_type>(seed));
158 const EngineType&
engine()
const {
return mEngine; }
169 template<
typename IntType =
int,
typename EngineType = boost::mt19937>
173 #if BOOST_VERSION >= 104700
174 typedef boost::random::uniform_int_distribution<IntType> Distr;
176 typedef boost::uniform_int<IntType> Distr;
185 RandInt(
const EngineType& engine, IntType imin, IntType imax):
187 mRand(std::
min(imin, imax), std::
max(imin, imax))
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))
207 mEngine.seed(static_cast<typename EngineType::result_type>(seed));
211 const EngineType&
engine()
const {
return mEngine; }
221 #if BOOST_VERSION >= 104700
222 return mRand(mEngine,
typename Distr::param_type(lo, hi));
224 return Distr(lo, hi)(mEngine);
235 template<
typename Type>
240 return x > min ? x < max ? x : max :
min;
245 template<
typename Type>
247 Clamp01(Type x) {
return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0); }
251 template<
typename Type>
255 if (x >= Type(0) && x <= Type(1))
return false;
256 x = x < Type(0) ? Type(0) : Type(1);
261 template<
typename Type>
265 return x > 0 ? x < 1 ? (3-2*x)*x*x : Type(1) : Type(0);
270 template<
typename Type>
283 inline int32_t
Abs(int32_t i) {
return abs(i); }
285 inline int64_t
Abs(int64_t i)
288 return (i < int64_t(0) ? -i : i);
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; }
299 #if defined(__APPLE__) || defined(MACOSX)
300 inline size_t Abs(
size_t i) {
return i; }
312 template<
typename Type>
317 return x == zeroVal<Type>();
324 template<
typename Type>
329 return x < tolerance && x > -tolerance;
333 template<
typename Type>
337 return x < tolerance && x > -tolerance;
342 template<
typename Type>
352 template<
typename Type>
357 return !(
Abs(a - b) > tolerance);
362 template<
typename Type>
366 return !(
Abs(a - b) > tolerance);
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; } \
380 template<typename Type>
384 return (b - a < tolerance);
389 template<
typename T0,
typename T1>
399 template<
typename Type>
405 if (!(
Abs(a - b) > absTol))
return true;
412 relError =
Abs((a - b) / b);
414 relError =
Abs((a - b) / a);
416 return (relError <= relTol);
433 union FloatOrInt32 {
float floatValue; int32_t int32Value; };
434 const FloatOrInt32* foi =
reinterpret_cast<const FloatOrInt32*
>(&aFloatValue);
435 return foi->int32Value;
442 union DoubleOrInt64 {
double doubleValue; int64_t int64Value; };
443 const DoubleOrInt64* dol =
reinterpret_cast<const DoubleOrInt64*
>(&aDoubleValue);
444 return dol->int64Value;
453 isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
458 longLeft = INT64_C(0x8000000000000000) - longLeft;
464 longRight = INT64_C(0x8000000000000000) - longRight;
467 int64_t difference = labs(longLeft - longRight);
468 return (difference <= aUnitsInLastPlace);
472 isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
477 intLeft = 0x80000000 - intLeft;
483 intRight = 0x80000000 - intRight;
486 int32_t difference = abs(intLeft - intRight);
487 return (difference <= aUnitsInLastPlace);
497 template<
typename Type>
498 inline Type
Pow2(Type x) {
return x*x; }
501 template<
typename Type>
502 inline Type
Pow3(Type x) {
return x*x*x; }
505 template<
typename Type>
509 template<
typename Type>
518 while (n--) ans *= x;
527 assert( b >= 0.0f &&
"Pow(float,float): base is negative" );
534 assert( b >= 0.0 &&
"Pow(double,double): base is negative" );
543 template<
typename Type>
545 Max(
const Type& a,
const Type& b)
551 template<
typename Type>
553 Max(
const Type& a,
const Type& b,
const Type& c)
559 template<
typename Type>
561 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
567 template<
typename Type>
569 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
575 template<
typename Type>
577 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
583 template<
typename 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)
592 template<
typename 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)
604 template<
typename Type>
609 template<
typename Type>
614 template<
typename Type>
616 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
622 template<
typename Type>
624 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
630 template<
typename Type>
632 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
638 template<
typename 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)
647 template<
typename 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)
659 template<
typename Type>
660 inline Type
Exp(
const Type& x) {
return std::exp(x); }
667 template <
typename Type>
668 inline int Sign(
const Type &x) {
return (zeroVal<Type>() < x) - (x < zeroVal<Type>()); }
673 template <
typename Type>
677 return ( (a<zeroVal<Type>()) ^ (b<zeroVal<Type>()) );
683 template <
typename Type>
687 return a * b <= zeroVal<Type>();
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); }
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); }
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); }
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>
729 return remainder ? x-remainder+base : x;
734 inline float RoundDown(
float x) {
return floorf(x); }
737 inline long double RoundDown(
long double x) {
return floorl(x); }
739 template<
typename Type>
745 return remainder ? x-remainder : x;
759 template<
typename Type>
765 template<
typename Type>
773 template<
typename Type>
787 inline int Ceil(
float x) {
return int(
RoundUp(x)); }
795 template<
typename Type>
796 inline Type
Chop(Type x, Type delta) {
return (
Abs(x) < delta ? zeroVal<Type>() : x); }
800 template<
typename Type>
804 Type tenth =
Pow(10,digits);
813 template<
typename Type>
841 template <
typename S,
typename T>
843 typedef typename boost::numeric::conversion_traits<S, T>::supertype
type;
854 template<
typename Vec3T>
858 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics
861 const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
862 const size_t hashKey =
863 ((v[0] < v[1]) << 2) + ((v[0] < v[2]) << 1) + (v[1] < v[2]);
864 return hashTable[hashKey];
875 template<
typename Vec3T>
879 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics
882 const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
883 const size_t hashKey =
884 ((v[0] > v[1]) << 2) + ((v[0] > v[2]) << 1) + (v[1] > v[2]);
885 return hashTable[hashKey];
892 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED
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
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
Type FractionalPart(Type x)
Return the fractional part of x.
Definition: Math.h:775
Axis
Definition: Math.h:822
#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
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
#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