8 #ifndef OPENVDB_MATH_HAS_BEEN_INCLUDED 9 #define OPENVDB_MATH_HAS_BEEN_INCLUDED 13 #include <boost/numeric/conversion/conversion_traits.hpp> 20 #include <type_traits> 27 #if defined(__INTEL_COMPILER) 28 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \ 29 _Pragma("warning (push)") \ 30 _Pragma("warning (disable:1572)") 31 #define OPENVDB_NO_FP_EQUALITY_WARNING_END \ 32 _Pragma("warning (pop)") 33 #elif defined(__clang__) 34 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \ 35 PRAGMA(clang diagnostic push) \ 36 PRAGMA(clang diagnostic ignored "-Wfloat-equal") 37 #define OPENVDB_NO_FP_EQUALITY_WARNING_END \ 38 PRAGMA(clang diagnostic pop) 47 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN 48 #define OPENVDB_NO_FP_EQUALITY_WARNING_END 59 template<
typename T>
inline T
zeroVal() {
return T(0); }
61 template<>
inline std::string zeroVal<std::string>() {
return ""; }
67 inline std::string
operator+(
const std::string& s,
bool) {
return s; }
70 inline std::string
operator+(
const std::string& s,
int) {
return s; }
71 inline std::string
operator+(
const std::string& s,
float) {
return s; }
72 inline std::string
operator+(
const std::string& s,
double) {
return s; }
81 template<
typename T>
inline T
negative(
const T& val) {
return T(-val); }
83 template<>
inline bool negative(
const bool& val) {
return !val; }
85 template<>
inline std::string
negative(
const std::string& val) {
return val; }
89 template<
typename T>
struct Tolerance {
static T value() {
return zeroVal<T>(); } };
91 template<>
struct Tolerance<float> {
static float value() {
return 1e-8f; } };
92 template<>
struct Tolerance<double> {
static double value() {
return 1e-15; } };
96 template<
typename T>
struct Delta {
static T value() {
return zeroVal<T>(); } };
98 template<>
struct Delta<float> {
static float value() {
return 1e-5f; } };
99 template<>
struct Delta<double> {
static double value() {
return 1e-9; } };
107 template<
typename FloatType =
double,
typename EngineType = std::mt19937>
112 std::uniform_real_distribution<FloatType> mRand;
119 Rand01(
const EngineType& engine): mEngine(engine) {}
123 Rand01(
unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
128 mEngine.seed(static_cast<typename EngineType::result_type>(seed));
132 const EngineType&
engine()
const {
return mEngine; }
143 template<
typename IntType =
int,
typename EngineType = std::mt19937>
147 using Distr = std::uniform_int_distribution<IntType>;
155 RandInt(
const EngineType& engine, IntType imin, IntType imax):
163 RandInt(
unsigned int seed, IntType imin, IntType imax):
164 mEngine(static_cast<typename EngineType::result_type>(seed)),
177 mEngine.seed(static_cast<typename EngineType::result_type>(seed));
181 const EngineType&
engine()
const {
return mEngine; }
191 return mRand(mEngine,
typename Distr::param_type(lo, hi));
201 template<
typename Type>
211 template<
typename Type>
213 Clamp01(Type x) {
return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0); }
217 template<
typename Type>
221 if (x >= Type(0) && x <= Type(1))
return false;
222 x = x < Type(0) ? Type(0) : Type(1);
227 template<
typename Type>
231 return x > 0 ? x < 1 ? (3-2*x)*x*x : Type(1) : Type(0);
236 template<
typename Type>
249 inline int32_t
Abs(int32_t i) {
return abs(i); }
251 inline int64_t
Abs(int64_t i)
254 return (i < int64_t(0) ? -i : i);
259 inline float Abs(
float x) {
return std::fabs(x); }
260 inline double Abs(
double x) {
return std::fabs(x); }
261 inline long double Abs(
long double x) {
return std::fabs(x); }
262 inline uint32_t
Abs(uint32_t i) {
return i; }
263 inline uint64_t
Abs(uint64_t i) {
return i; }
264 inline bool Abs(
bool b) {
return b; }
266 #if defined(__APPLE__) || defined(MACOSX) 267 inline size_t Abs(
size_t i) {
return i; }
279 template<
typename Type>
284 return x == zeroVal<Type>();
291 template<
typename Type>
296 return !(x > tolerance) && !(x < -tolerance);
300 template<
typename Type>
304 return !(x > tolerance) && !(x < -tolerance);
309 template<
typename Type>
319 isFinite(
const float x) {
return std::isfinite(x); }
322 template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
324 isFinite(
const Type& x) {
return std::isfinite(static_cast<double>(x)); }
332 template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
334 isInfinite(
const Type& x) {
return std::isinf(static_cast<double>(x)); }
339 isNan(
const float x) {
return std::isnan(x); }
342 template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
344 isNan(
const Type& x) {
return std::isnan(static_cast<double>(x)); }
349 template<
typename Type>
354 return !(
Abs(a - b) > tolerance);
359 template<
typename Type>
363 return !(
Abs(a - b) > tolerance);
366 #define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \ 367 template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \ 368 template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \ 377 template<typename Type>
381 return (b - a < tolerance);
386 template<
typename T0,
typename T1>
396 template<
typename Type>
402 if (!(
Abs(a - b) > absTol))
return true;
409 relError =
Abs((a - b) / b);
411 relError =
Abs((a - b) / a);
413 return (relError <= relTol);
430 union FloatOrInt32 {
float floatValue; int32_t int32Value; };
431 const FloatOrInt32* foi =
reinterpret_cast<const FloatOrInt32*
>(&aFloatValue);
432 return foi->int32Value;
439 union DoubleOrInt64 {
double doubleValue; int64_t int64Value; };
440 const DoubleOrInt64* dol =
reinterpret_cast<const DoubleOrInt64*
>(&aDoubleValue);
441 return dol->int64Value;
450 isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
455 longLeft = INT64_C(0x8000000000000000) - longLeft;
461 longRight = INT64_C(0x8000000000000000) - longRight;
464 int64_t difference = labs(longLeft - longRight);
465 return (difference <= aUnitsInLastPlace);
469 isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
474 intLeft = 0x80000000 - intLeft;
480 intRight = 0x80000000 - intRight;
483 int32_t difference = abs(intLeft - intRight);
484 return (difference <= aUnitsInLastPlace);
494 template<
typename Type>
495 inline Type
Pow2(Type x) {
return x*x; }
498 template<
typename Type>
499 inline Type
Pow3(Type x) {
return x*x*x; }
502 template<
typename Type>
506 template<
typename Type>
515 while (n--) ans *= x;
524 assert( b >= 0.0f &&
"Pow(float,float): base is negative" );
531 assert( b >= 0.0 &&
"Pow(double,double): base is negative" );
532 return std::pow(b,e);
540 template<
typename Type>
542 Max(
const Type& a,
const Type& b)
548 template<
typename Type>
550 Max(
const Type& a,
const Type& b,
const Type& c)
556 template<
typename Type>
558 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
564 template<
typename Type>
566 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
572 template<
typename Type>
574 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
580 template<
typename Type>
582 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
583 const Type& e,
const Type& f,
const Type& g)
589 template<
typename Type>
591 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
592 const Type& e,
const Type& f,
const Type& g,
const Type& h)
601 template<
typename Type>
606 template<
typename Type>
611 template<
typename Type>
613 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
619 template<
typename Type>
621 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
627 template<
typename Type>
629 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
635 template<
typename Type>
637 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
638 const Type& e,
const Type& f,
const Type& g)
644 template<
typename Type>
646 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
647 const Type& e,
const Type& f,
const Type& g,
const Type& h)
656 template<
typename Type>
657 inline Type
Exp(
const Type& x) {
return std::exp(x); }
662 inline float Sin(
const float& x) {
return std::sin(x); }
665 inline double Sin(
const double& x) {
return std::sin(x); }
671 inline float Cos(
const float& x) {
return std::cos(x); }
674 inline double Cos(
const double& x) {
return std::cos(x); }
682 template <
typename Type>
683 inline int Sign(
const Type &x) {
return (zeroVal<Type>() < x) - (x < zeroVal<Type>()); }
688 template <
typename Type>
692 return ( (a<zeroVal<Type>()) ^ (b<zeroVal<Type>()) );
698 template <
typename Type>
702 return a * b <= zeroVal<Type>();
707 inline float Sqrt(
float x) {
return std::sqrt(x); }
709 inline double Sqrt(
double x) {
return std::sqrt(x); }
710 inline long double Sqrt(
long double x) {
return std::sqrt(x); }
715 inline float Cbrt(
float x) {
return std::cbrt(x); }
717 inline double Cbrt(
double x) {
return std::cbrt(x); }
718 inline long double Cbrt(
long double x) {
return std::cbrt(x); }
723 inline int Mod(
int x,
int y) {
return (x % y); }
725 inline float Mod(
float x,
float y) {
return std::fmod(x, y); }
726 inline double Mod(
double x,
double y) {
return std::fmod(x, y); }
727 inline long double Mod(
long double x,
long double y) {
return std::fmod(x, y); }
728 template<
typename Type>
inline Type
Remainder(Type x, Type y) {
return Mod(x, y); }
733 inline float RoundUp(
float x) {
return std::ceil(x); }
735 inline double RoundUp(
double x) {
return std::ceil(x); }
736 inline long double RoundUp(
long double x) {
return std::ceil(x); }
738 template<
typename Type>
744 return remainder ? x-remainder+base : x;
749 inline float RoundDown(
float x) {
return std::floor(x); }
751 inline double RoundDown(
double x) {
return std::floor(x); }
752 inline long double RoundDown(
long double x) {
return std::floor(x); }
754 template<
typename Type>
760 return remainder ? x-remainder : x;
774 template<
typename Type>
780 template<
typename Type>
788 template<
typename Type>
802 inline int Ceil(
float x) {
return int(
RoundUp(x)); }
810 template<
typename Type>
811 inline Type
Chop(Type x, Type delta) {
return (
Abs(x) < delta ? zeroVal<Type>() : x); }
815 template<
typename Type>
819 Type tenth =
Pow(10,digits);
830 inline auto PrintCast(
const T& val) ->
typename std::enable_if<!std::is_same<T, int8_t>::value
831 && !std::is_same<T, uint8_t>::value,
const T&>::type {
return val; }
832 inline int32_t
PrintCast(int8_t val) {
return int32_t(val); }
833 inline uint32_t
PrintCast(uint8_t val) {
return uint32_t(val); }
840 template<
typename Type>
868 template <
typename S,
typename T>
870 using type =
typename boost::numeric::conversion_traits<S, T>::supertype;
881 template<
typename Vec3T>
885 static const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
886 const size_t hashKey =
887 ((v[0] < v[1]) << 2) + ((v[0] < v[2]) << 1) + (v[1] < v[2]);
888 return hashTable[hashKey];
899 template<
typename Vec3T>
903 static const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
904 const size_t hashKey =
905 ((v[0] > v[1]) << 2) + ((v[0] > v[2]) << 1) + (v[1] > v[2]);
906 return hashTable[hashKey];
913 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED bool isNegative< bool >(const bool &)
Definition: Math.h:314
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:188
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:132
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:281
Delta for small floating-point offsets.
Definition: Math.h:97
bool isApproxZero(const Type &x, const Type &tolerance)
Return true if x is equal to zero to within the given tolerance.
Definition: Math.h:302
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:700
static double value()
Definition: Math.h:99
Type RoundUp(Type x, Type base)
Return x rounded up to the nearest multiple of base.
Definition: Math.h:741
bool isRelOrApproxEqual(const bool &a, const bool &b, const bool &, const bool &)
Definition: Math.h:418
bool isInfinite(const Type &x)
Return true if x is an infinity value (either positive infinity or negative infinity).
Definition: Math.h:334
Type Pow4(Type x)
Return x4.
Definition: Math.h:503
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:108
Type FractionalPart(Type x)
Return the fractional part of x.
Definition: Math.h:790
bool isUlpsEqual(const float aLeft, const float aRight, const int32_t aUnitsInLastPlace)
Definition: Math.h:469
Type Inv(Type x)
Return the inverse of x.
Definition: Math.h:842
std::string negative(const std::string &val)
Return the "negation" of the given string.
Definition: Math.h:85
bool isNan(const Type &x)
Return true if x is a NaN (Not-A-Number) value.
Definition: Math.h:344
bool zeroVal< bool >()
Return the bool value that corresponds to zero.
Definition: Math.h:63
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:901
int Ceil(long double x)
Return the ceiling of x.
Definition: Math.h:805
bool SignChange(const Type &a, const Type &b)
Return true if a and b have different signs.
Definition: Math.h:690
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:646
Type Pow2(Type x)
Return x2.
Definition: Math.h:495
RotationOrder
Definition: Math.h:856
Type IntegerPart(Type x)
Return the integer part of x.
Definition: Math.h:782
Axis
Definition: Math.h:849
FloatType operator()()
Return a uniformly distributed random number in the range [0, 1).
Definition: Math.h:135
double ValueType
Definition: Math.h:115
long double Sqrt(long double x)
Return the square root of a floating-point value.
Definition: Math.h:710
int Floor(long double x)
Return the floor of x.
Definition: Math.h:797
long double Round(long double x)
Return x rounded to the nearest integer.
Definition: Math.h:768
Type Exp(const Type &x)
Return ex.
Definition: Math.h:657
T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:59
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
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:238
static double value()
Definition: Math.h:92
long double Cbrt(long double x)
Return the cube root of a floating-point value.
Definition: Math.h:718
IntType operator()()
Return a randomly-generated integer in the current range.
Definition: Math.h:184
Type Clamp01(Type x)
Return x clamped to [0, 1].
Definition: Math.h:213
Type Pow3(Type x)
Return x3.
Definition: Math.h:499
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:175
RandInt(const EngineType &engine, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:155
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:361
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:181
Definition: Exceptions.h:13
Rand01(const EngineType &engine)
Initialize the generator.
Definition: Math.h:119
bool Abs(bool b)
Return the absolute value of the given quantity.
Definition: Math.h:264
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition: Math.h:48
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:311
void setRange(IntType imin, IntType imax)
Change the range over which integers are distributed to [imin, imax].
Definition: Math.h:169
bool ClampTest01(Type &x)
Return true if x is outside [0,1].
Definition: Math.h:219
uint32_t PrintCast(uint8_t val)
Definition: Math.h:833
Type EuclideanRemainder(Type x)
Definition: Math.h:776
double Cos(const double &x)
Return cos x.
Definition: Math.h:674
Library and file format version numbers.
Type Clamp(Type x, Type min, Type max)
Return x clamped to [min, max].
Definition: Math.h:203
Tolerance for floating-point comparison.
Definition: Math.h:90
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition: Math.h:47
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:379
int32_t floatToInt32(const float aFloatValue)
Definition: Math.h:428
bool isFinite(const Type &x)
Return true if x is finite.
Definition: Math.h:324
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:126
int Sign(const Type &x)
Return the sign of the given value as an integer (either -1, 0 or 1).
Definition: Math.h:683
double Pow(double b, double e)
Return be.
Definition: Math.h:529
RandInt(unsigned int seed, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:163
long double Mod(long double x, long double y)
Return the remainder of x / y.
Definition: Math.h:727
Simple random integer generator.
Definition: Math.h:144
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:388
static float value()
Definition: Math.h:98
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:591
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:883
#define OPENVDB_EXACT_IS_APPROX_EQUAL(T)
Definition: Math.h:366
Type Remainder(Type x, Type y)
Return the remainder of x / y.
Definition: Math.h:728
Type RoundDown(Type x, Type base)
Return x rounded down to the nearest multiple of base.
Definition: Math.h:757
double Sin(const double &x)
Return sin x.
Definition: Math.h:665
int64_t doubleToInt64(const double aDoubleValue)
Definition: Math.h:437
static float value()
Definition: Math.h:91
Type Chop(Type x, Type delta)
Return x if it is greater or equal in magnitude than delta. Otherwise, return zero.
Definition: Math.h:811
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:817
std::string operator+(const std::string &s, double)
Needed to support the (zeroVal<ValueType>() + val) idiom when ValueType is std::string.
Definition: Math.h:72
Rand01(unsigned int seed)
Initialize the generator.
Definition: Math.h:123