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>(); } };
135 template<
typename FloatType =
double,
typename EngineType = boost::mt19937>
140 boost::uniform_01<FloatType> mRand;
147 Rand01(
unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
158 template<
typename EngineType = boost::mt19937>
162 #if BOOST_VERSION >= 104700
163 typedef boost::random::uniform_int_distribution<int> Distr;
165 typedef boost::uniform_int<int> Distr;
174 RandInt(
unsigned int seed,
int imin,
int imax):
175 mEngine(static_cast<typename EngineType::result_type>(seed)),
176 mRand(std::
min(imin, imax), std::
max(imin, imax))
189 #if BOOST_VERSION >= 104700
190 return mRand(mEngine, Distr::param_type(lo, hi));
192 return Distr(lo, hi)(mEngine);
203 template<
typename Type>
208 return x > min ? x < max ? x : max :
min;
213 template<
typename Type>
215 Clamp01(Type x) {
return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0); }
219 template<
typename Type>
223 if (x >= Type(0) && x <= Type(1))
return false;
224 x = x < Type(0) ? Type(0) : Type(1);
231 template<
typename Type>
236 const Type t = (x-
min)/(max-min);
237 return t > 0 ? t < 1 ? (3-2*t)*t*t : Type(1) : Type(0);
245 inline int32_t
Abs(int32_t i) {
return abs(i); }
247 inline int64_t
Abs(int64_t i)
250 return (i < int64_t(0) ? -i : i);
255 inline float Abs(
float x) {
return fabs(x); }
256 inline double Abs(
double x) {
return fabs(x); }
257 inline long double Abs(
long double x) {
return fabs(x); }
258 inline uint32_t
Abs(uint32_t i) {
return i; }
259 inline uint64_t
Abs(uint64_t i) {
return i; }
261 #if defined(__APPLE__) || defined(MACOSX)
262 inline size_t Abs(
size_t i) {
return i; }
274 template<
typename Type>
279 return x == zeroVal<Type>();
286 template<
typename Type>
291 return x < tolerance && x > -tolerance;
295 template<
typename Type>
299 return x < tolerance && x > -tolerance;
304 template<
typename Type>
314 template<
typename Type>
319 return !(
Abs(a - b) > tolerance);
324 template<
typename Type>
328 return !(
Abs(a - b) > tolerance);
331 #define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \
332 template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \
333 template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \
342 template<typename Type>
346 return (b - a < tolerance);
351 template<
typename T0,
typename T1>
361 template<
typename Type>
367 if (!(
Abs(a - b) > absTol))
return true;
374 relError =
Abs((a - b) / b);
376 relError =
Abs((a - b) / a);
378 return (relError <= relTol);
395 union FloatOrInt32 {
float floatValue; int32_t int32Value; };
396 const FloatOrInt32* foi =
reinterpret_cast<const FloatOrInt32*
>(&aFloatValue);
397 return foi->int32Value;
404 union DoubleOrInt64 {
double doubleValue; int64_t int64Value; };
405 const DoubleOrInt64* dol =
reinterpret_cast<const DoubleOrInt64*
>(&aDoubleValue);
406 return dol->int64Value;
415 isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
420 longLeft = INT64_C(0x8000000000000000) - longLeft;
426 longRight = INT64_C(0x8000000000000000) - longRight;
429 int64_t difference = labs(longLeft - longRight);
430 return (difference <= aUnitsInLastPlace);
434 isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
439 intLeft = 0x80000000 - intLeft;
445 intRight = 0x80000000 - intRight;
448 int32_t difference = abs(intLeft - intRight);
449 return (difference <= aUnitsInLastPlace);
459 template<
typename Type>
460 inline Type
Pow2(Type x) {
return x*x; }
463 template<
typename Type>
464 inline Type
Pow3(Type x) {
return x*x*x; }
467 template<
typename Type>
471 template<
typename Type>
480 while (n--) ans *= x;
489 assert( b >= 0.0f &&
"Pow(float,float): base is negative" );
496 assert( b >= 0.0 &&
"Pow(double,double): base is negative" );
505 template<
typename Type>
507 Max(
const Type& a,
const Type& b)
513 template<
typename Type>
515 Max(
const Type& a,
const Type& b,
const Type& c)
521 template<
typename Type>
523 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
529 template<
typename Type>
531 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
537 template<
typename Type>
539 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
545 template<
typename Type>
547 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
548 const Type& e,
const Type& f,
const Type& g)
554 template<
typename Type>
556 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
557 const Type& e,
const Type& f,
const Type& g,
const Type& h)
566 template<
typename Type>
571 template<
typename Type>
576 template<
typename Type>
578 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
584 template<
typename Type>
586 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
592 template<
typename Type>
594 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
600 template<
typename Type>
602 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
603 const Type& e,
const Type& f,
const Type& g)
609 template<
typename Type>
611 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
612 const Type& e,
const Type& f,
const Type& g,
const Type& h)
622 template <
typename Type>
623 inline int Sign(
const Type &x) {
return (zeroVal<Type>() < x) - (x < zeroVal<Type>()); }
628 template <
typename Type>
632 return ( (a<zeroVal<Type>()) ^ (b<zeroVal<Type>()) );
638 template <
typename Type>
642 return a * b <= zeroVal<Type>();
647 inline float Sqrt(
float x) {
return sqrtf(x); }
649 inline double Sqrt(
double x) {
return sqrt(x); }
650 inline long double Sqrt(
long double x) {
return sqrtl(x); }
655 inline float Cbrt(
float x) {
return boost::math::cbrt(x); }
657 inline double Cbrt(
double x) {
return boost::math::cbrt(x); }
658 inline long double Cbrt(
long double x) {
return boost::math::cbrt(x); }
663 inline int Mod(
int x,
int y) {
return (x % y); }
665 inline float Mod(
float x,
float y) {
return fmodf(x,y); }
666 inline double Mod(
double x,
double y) {
return fmod(x,y); }
667 inline long double Mod(
long double x,
long double y) {
return fmodl(x,y); }
668 template<
typename Type>
inline Type
Remainder(Type x, Type y) {
return Mod(x,y); }
673 inline float RoundUp(
float x) {
return ceilf(x); }
675 inline double RoundUp(
double x) {
return ceil(x); }
676 inline long double RoundUp(
long double x) {
return ceill(x); }
678 template<
typename Type>
684 return remainder ? x-remainder+base : x;
689 inline float RoundDown(
float x) {
return floorf(x); }
692 inline long double RoundDown(
long double x) {
return floorl(x); }
695 template<
typename Type>
701 return remainder ? x-remainder : x;
706 template<
typename Type>
714 template<
typename Type>
727 inline int Ceil(
float x) {
return (
int)
RoundUp(x); }
735 template<
typename Type>
736 inline Type
Chop(Type x, Type delta) {
return (
Abs(x) < delta ? zeroVal<Type>() : x); }
740 template<
typename Type>
744 Type tenth =
Pow(10,digits);
753 template<
typename Type>
781 template <
typename S,
typename T>
783 typedef typename boost::numeric::conversion_traits<S, T>::supertype
type;
794 template<
typename Vec3T>
798 static const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
799 const size_t hashKey =
800 ((v[0] < v[1]) << 2) + ((v[0] < v[2]) << 1) + (v[1] < v[2]);
801 return hashTable[hashKey];
812 template<
typename Vec3T>
816 static const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
817 const size_t hashKey =
818 ((v[0] > v[1]) << 2) + ((v[0] > v[2]) << 1) + (v[1] > v[2]);
819 return hashTable[hashKey];
826 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED
int Ceil(float x)
Return the ceiling of x.
Definition: Math.h:728
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
int operator()()
Return a randomly-generated integer in the current range.
Definition: Math.h:183
Type Remainder(Type x, Type y)
Return the remainder of x / y.
Definition: Math.h:668
int64_t doubleToInt64(const double aDoubleValue)
Definition: Math.h:402
Type Pow3(Type x)
Return .
Definition: Math.h:464
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
Definition: Math.h:288
int operator()(int imin, int imax)
Return a randomly-generated integer in the new range [imin, imax], without changing the current range...
Definition: Math.h:186
Type FractionalPart(Type x)
Return the fractional part of x.
Definition: Math.h:715
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:648
float Cbrt(float x)
Return the cube root of a floating-point value.
Definition: Math.h:656
Type Pow2(Type x)
Return .
Definition: Math.h:460
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition: Math.h:507
OPENVDB_DEPRECATED double randUniform()
Definition: Math.h:130
Type Chop(Type x, Type delta)
Return x if it is greater in magnitude than delta. Otherwise, return zero.
Definition: Math.h:736
Rand01(unsigned int seed)
Initialize the generator.
Definition: Math.h:147
Tolerance for floating-point comparison.
Definition: Math.h:116
int32_t floatToInt32(const float aFloatValue)
Definition: Math.h:393
Type Clamp01(Type x)
Return x clamped to [0, 1].
Definition: Math.h:215
T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:85
Type SmoothUnitStep(Type x, Type min, Type max)
Return 0 if x < min, 1 if x > max or else , where .
Definition: Math.h:233
Type Round(Type x)
Return x rounded down to the nearest integer.
Definition: Math.h:693
Type Inv(Type x)
Return the inverse of x.
Definition: Math.h:755
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:107
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:306
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:814
#define OPENVDB_VERSION_NAME
Definition: version.h:45
float RoundDown(float x)
Return x rounded down to the nearest integer.
Definition: Math.h:690
RotationOrder
Definition: Math.h:769
bool ClampTest01(Type &x)
Return true if x is outside [0,1].
Definition: Math.h:221
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:276
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:136
static double value()
Definition: Math.h:118
bool isNegative< bool >(const bool &)
Return false, since bool values are never less than zero.
Definition: Math.h:309
FloatType operator()()
Return a uniformly distributed random number in the range [0, 1).
Definition: Math.h:150
Rand01< double, boost::mt19937 > Random01
Definition: Math.h:153
Type Clamp(Type x, Type min, Type max)
Return x clamped to [min, max].
Definition: Math.h:205
Vec3< typename promote< T, typename Coord::ValueType >::type > operator+(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be added to or subtracted from a Vec3.
Definition: Coord.h:382
bool SignChange(const Type &a, const Type &b)
Return true if a and b have different signs.
Definition: Math.h:630
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
void setRange(int imin, int imax)
Change the range over which integers are distributed to [imin, imax].
Definition: Math.h:180
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition: Math.h:74
OPENVDB_DEPRECATED void randSeed(unsigned int seed)
Definition: Math.h:126
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:796
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition: Math.h:363
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:353
RandInt(unsigned int seed, int imin, int imax)
Initialize the generator.
Definition: Math.h:174
float RoundUp(float x)
Return x rounded up to the nearest integer.
Definition: Math.h:674
int Sign(const Type &x)
Return the sign of the given value as an integer (either -1, 0 or 1).
Definition: Math.h:623
FloatType ValueType
Definition: Math.h:143
Type Pow4(Type x)
Return .
Definition: Math.h:468
static float value()
Definition: Math.h:117
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition: Math.h:73
Type IntegerPart(Type x)
Return the integer part of x.
Definition: Math.h:708
int Mod(int x, int y)
Return the remainder of x / y.
Definition: Math.h:664
int32_t Abs(int32_t i)
Return the absolute value of the given quantity.
Definition: Math.h:246
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:344
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:640
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:742
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:56
Axis
Definition: Math.h:762
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:568
bool isApproxEqual(const Hermite &lhs, const Hermite &rhs)
Definition: Hermite.h:470
bool isUlpsEqual(const double aLeft, const double aRight, const int64_t aUnitsInLastPlace)
Definition: Math.h:415
Type Pow(Type x, int n)
Return .
Definition: Math.h:473
RandInt< boost::mt19937 > RandomInt
Definition: Math.h:197
int Floor(float x)
Return the floor of x.
Definition: Math.h:720
#define OPENVDB_EXACT_IS_APPROX_EQUAL(T)
Definition: Math.h:331
Simple random integer generator.
Definition: Math.h:159
bool zeroVal< bool >()
Return the bool value that corresponds to zero.
Definition: Math.h:89