35 #ifndef OPENVDB_MATH_HAS_BEEN_INCLUDED 36 #define OPENVDB_MATH_HAS_BEEN_INCLUDED 40 #include <boost/numeric/conversion/conversion_traits.hpp> 47 #include <type_traits> 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) 74 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN 75 #define OPENVDB_NO_FP_EQUALITY_WARNING_END 86 template<
typename T>
inline T
zeroVal() {
return T(0); }
88 template<>
inline std::string zeroVal<std::string>() {
return ""; }
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; }
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; }
116 template<
typename T>
struct Tolerance {
static T value() {
return zeroVal<T>(); } };
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; } };
134 template<
typename FloatType =
double,
typename EngineType = std::mt19937>
139 std::uniform_real_distribution<FloatType> mRand;
146 Rand01(
const EngineType& engine): mEngine(engine) {}
150 Rand01(
unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
155 mEngine.seed(static_cast<typename EngineType::result_type>(seed));
159 const EngineType&
engine()
const {
return mEngine; }
170 template<
typename IntType =
int,
typename EngineType = std::mt19937>
174 using Distr = std::uniform_int_distribution<IntType>;
182 RandInt(
const EngineType& engine, IntType imin, IntType imax):
184 mRand(std::
min(imin, imax), std::
max(imin, imax))
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))
204 mEngine.seed(static_cast<typename EngineType::result_type>(seed));
208 const EngineType&
engine()
const {
return mEngine; }
218 return mRand(mEngine,
typename Distr::param_type(lo, hi));
228 template<
typename Type>
238 template<
typename Type>
240 Clamp01(Type x) {
return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0); }
244 template<
typename Type>
248 if (x >= Type(0) && x <= Type(1))
return false;
249 x = x < Type(0) ? Type(0) : Type(1);
254 template<
typename Type>
258 return x > 0 ? x < 1 ? (3-2*x)*x*x : Type(1) : Type(0);
263 template<
typename Type>
276 inline int32_t
Abs(int32_t i) {
return abs(i); }
278 inline int64_t
Abs(int64_t i)
281 return (i < int64_t(0) ? -i : i);
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; }
293 #if defined(__APPLE__) || defined(MACOSX) 294 inline size_t Abs(
size_t i) {
return i; }
306 template<
typename Type>
311 return x == zeroVal<Type>();
318 template<
typename Type>
323 return !(x > tolerance) && !(x < -tolerance);
327 template<
typename Type>
331 return !(x > tolerance) && !(x < -tolerance);
336 template<
typename Type>
346 isFinite(
const float x) {
return std::isfinite(x); }
349 template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
351 isFinite(
const Type& x) {
return std::isfinite(static_cast<double>(x)); }
356 template<
typename Type>
361 return !(
Abs(a - b) > tolerance);
366 template<
typename Type>
370 return !(
Abs(a - b) > tolerance);
373 #define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \ 374 template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \ 375 template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \ 384 template<typename Type>
388 return (b - a < tolerance);
393 template<
typename T0,
typename T1>
403 template<
typename Type>
409 if (!(
Abs(a - b) > absTol))
return true;
416 relError =
Abs((a - b) / b);
418 relError =
Abs((a - b) / a);
420 return (relError <= relTol);
437 union FloatOrInt32 {
float floatValue; int32_t int32Value; };
438 const FloatOrInt32* foi = reinterpret_cast<const FloatOrInt32*>(&aFloatValue);
439 return foi->int32Value;
446 union DoubleOrInt64 {
double doubleValue; int64_t int64Value; };
447 const DoubleOrInt64* dol = reinterpret_cast<const DoubleOrInt64*>(&aDoubleValue);
448 return dol->int64Value;
457 isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
462 longLeft = INT64_C(0x8000000000000000) - longLeft;
468 longRight = INT64_C(0x8000000000000000) - longRight;
471 int64_t difference = labs(longLeft - longRight);
472 return (difference <= aUnitsInLastPlace);
476 isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
481 intLeft = 0x80000000 - intLeft;
487 intRight = 0x80000000 - intRight;
490 int32_t difference = abs(intLeft - intRight);
491 return (difference <= aUnitsInLastPlace);
501 template<
typename Type>
502 inline Type
Pow2(Type x) {
return x*x; }
505 template<
typename Type>
506 inline Type
Pow3(Type x) {
return x*x*x; }
509 template<
typename Type>
513 template<
typename Type>
522 while (n--) ans *= x;
531 assert( b >= 0.0f &&
"Pow(float,float): base is negative" );
538 assert( b >= 0.0 &&
"Pow(double,double): base is negative" );
539 return std::pow(b,e);
547 template<
typename Type>
549 Max(
const Type& a,
const Type& b)
555 template<
typename Type>
557 Max(
const Type& a,
const Type& b,
const Type& c)
563 template<
typename Type>
565 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
571 template<
typename Type>
573 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
579 template<
typename Type>
581 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
587 template<
typename Type>
589 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
590 const Type& e,
const Type& f,
const Type& g)
596 template<
typename Type>
598 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
599 const Type& e,
const Type& f,
const Type& g,
const Type& h)
608 template<
typename Type>
613 template<
typename Type>
618 template<
typename Type>
620 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
626 template<
typename Type>
628 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
634 template<
typename Type>
636 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
642 template<
typename Type>
644 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
645 const Type& e,
const Type& f,
const Type& g)
651 template<
typename Type>
653 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
654 const Type& e,
const Type& f,
const Type& g,
const Type& h)
663 template<
typename Type>
664 inline Type
Exp(
const Type& x) {
return std::exp(x); }
669 inline float Sin(
const float& x) {
return std::sin(x); }
672 inline double Sin(
const double& x) {
return std::sin(x); }
678 inline float Cos(
const float& x) {
return std::cos(x); }
681 inline double Cos(
const double& x) {
return std::cos(x); }
689 template <
typename Type>
690 inline int Sign(
const Type &x) {
return (zeroVal<Type>() < x) - (x < zeroVal<Type>()); }
695 template <
typename Type>
699 return ( (a<zeroVal<Type>()) ^ (b<zeroVal<Type>()) );
705 template <
typename Type>
709 return a * b <= zeroVal<Type>();
714 inline float Sqrt(
float x) {
return std::sqrt(x); }
716 inline double Sqrt(
double x) {
return std::sqrt(x); }
717 inline long double Sqrt(
long double x) {
return std::sqrt(x); }
722 inline float Cbrt(
float x) {
return std::cbrt(x); }
724 inline double Cbrt(
double x) {
return std::cbrt(x); }
725 inline long double Cbrt(
long double x) {
return std::cbrt(x); }
730 inline int Mod(
int x,
int y) {
return (x % y); }
732 inline float Mod(
float x,
float y) {
return std::fmod(x, y); }
733 inline double Mod(
double x,
double y) {
return std::fmod(x, y); }
734 inline long double Mod(
long double x,
long double y) {
return std::fmod(x, y); }
735 template<
typename Type>
inline Type
Remainder(Type x, Type y) {
return Mod(x, y); }
740 inline float RoundUp(
float x) {
return std::ceil(x); }
742 inline double RoundUp(
double x) {
return std::ceil(x); }
743 inline long double RoundUp(
long double x) {
return std::ceil(x); }
745 template<
typename Type>
751 return remainder ? x-remainder+base : x;
756 inline float RoundDown(
float x) {
return std::floor(x); }
758 inline double RoundDown(
double x) {
return std::floor(x); }
759 inline long double RoundDown(
long double x) {
return std::floor(x); }
761 template<
typename Type>
767 return remainder ? x-remainder : x;
781 template<
typename Type>
787 template<
typename Type>
795 template<
typename Type>
809 inline int Ceil(
float x) {
return int(
RoundUp(x)); }
817 template<
typename Type>
818 inline Type
Chop(Type x, Type delta) {
return (
Abs(x) < delta ? zeroVal<Type>() : x); }
822 template<
typename Type>
826 Type tenth =
Pow(10,digits);
837 inline auto PrintCast(
const T& val) ->
typename std::enable_if<!std::is_same<T, int8_t>::value
838 && !std::is_same<T, uint8_t>::value,
const T&>::type {
return val; }
839 inline int32_t
PrintCast(int8_t val) {
return int32_t(val); }
840 inline uint32_t
PrintCast(uint8_t val) {
return uint32_t(val); }
847 template<
typename Type>
875 template <
typename S,
typename T>
877 using type =
typename boost::numeric::conversion_traits<S, T>::supertype;
888 template<
typename Vec3T>
892 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics 895 const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
896 const size_t hashKey =
897 ((v[0] < v[1]) << 2) + ((v[0] < v[2]) << 1) + (v[1] < v[2]);
898 return hashTable[hashKey];
909 template<
typename Vec3T>
913 #ifndef _MSC_VER // Visual C++ doesn't guarantee thread-safe initialization of local statics 916 const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
917 const size_t hashKey =
918 ((v[0] > v[1]) << 2) + ((v[0] > v[2]) << 1) + (v[1] > v[2]);
919 return hashTable[hashKey];
926 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED 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
int Sign(const Type &x)
Return the sign of the given value as an integer (either -1, 0 or 1).
Definition: Math.h:690
Type Inv(Type x)
Return the inverse of x.
Definition: Math.h:849
Type RoundDown(Type x, Type base)
Return x rounded down to the nearest multiple of base.
Definition: Math.h:764
Delta for small floating-point offsets.
Definition: Math.h:124
Type Exp(const Type &x)
Return ex.
Definition: Math.h:664
bool isFinite(const Type &x)
Return true if x is finite.
Definition: Math.h:351
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:707
uint32_t PrintCast(uint8_t val)
Definition: Math.h:840
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:135
bool isUlpsEqual(const float aLeft, const float aRight, const int32_t aUnitsInLastPlace)
Definition: Math.h:476
bool Abs(bool b)
Return the absolute value of the given quantity.
Definition: Math.h:291
static float value()
Definition: Math.h:125
Type Pow3(Type x)
Return x3.
Definition: Math.h:506
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:159
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:395
RotationOrder
Definition: Math.h:863
int Floor(long double x)
Return the floor of x.
Definition: Math.h:804
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:890
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
bool SignChange(const Type &a, const Type &b)
Return true if a and b have different signs.
Definition: Math.h:697
Simple random integer generator.
Definition: Math.h:171
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:386
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
Type Pow4(Type x)
Return x4.
Definition: Math.h:510
T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:86
Type RoundUp(Type x, Type base)
Return x rounded up to the nearest multiple of base.
Definition: Math.h:748
double Sin(const double &x)
Return sin x.
Definition: Math.h:672
Rand01(unsigned int seed)
Initialize the generator.
Definition: Math.h:150
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:653
double Pow(double b, double e)
Return be.
Definition: Math.h:536
int64_t doubleToInt64(const double aDoubleValue)
Definition: Math.h:444
int Ceil(long double x)
Return the ceiling of x.
Definition: Math.h:812
Axis
Definition: Math.h:856
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
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308
Type Remainder(Type x, Type y)
Return the remainder of x / y.
Definition: Math.h:735
FloatType operator()()
Return a uniformly distributed random number in the range [0, 1).
Definition: Math.h:162
static double value()
Definition: Math.h:119
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:153
static double value()
Definition: Math.h:126
Type IntegerPart(Type x)
Return the integer part of x.
Definition: Math.h:789
IntType operator()()
Return a randomly-generated integer in the current range.
Definition: Math.h:211
Type Clamp01(Type x)
Return x clamped to [0, 1].
Definition: Math.h:240
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
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
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:911
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:208
Definition: Exceptions.h:40
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:824
Type FractionalPart(Type x)
Return the fractional part of x.
Definition: Math.h:797
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:368
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition: Math.h:75
void setRange(IntType imin, IntType imax)
Change the range over which integers are distributed to [imin, imax].
Definition: Math.h:196
static float value()
Definition: Math.h:118
Tolerance for floating-point comparison.
Definition: Math.h:117
int32_t floatToInt32(const float aFloatValue)
Definition: Math.h:435
Library and file format version numbers.
long double Cbrt(long double x)
Return the cube root of a floating-point value.
Definition: Math.h:725
Type Chop(Type x, Type delta)
Return x if it is greater or equal in magnitude than delta. Otherwise, return zero.
Definition: Math.h:818
double ValueType
Definition: Math.h:142
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition: Math.h:74
Rand01(const EngineType &engine)
Initialize the generator.
Definition: Math.h:146
Type EuclideanRemainder(Type x)
Definition: Math.h:783
long double Sqrt(long double x)
Return the square root of a floating-point value.
Definition: Math.h:717
Type Clamp(Type x, Type min, Type max)
Return x clamped to [min, max].
Definition: Math.h:230
RandInt(unsigned int seed, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:190
bool isRelOrApproxEqual(const bool &a, const bool &b, const bool &, const bool &)
Definition: Math.h:425
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:598
long double Round(long double x)
Return x rounded to the nearest integer.
Definition: Math.h:775
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
bool ClampTest01(Type &x)
Return true if x is outside [0,1].
Definition: Math.h:246
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:338
double Cos(const double &x)
Return cos x.
Definition: Math.h:681
#define OPENVDB_EXACT_IS_APPROX_EQUAL(T)
Definition: Math.h:373
bool isNegative< bool >(const bool &)
Definition: Math.h:341
long double Mod(long double x, long double y)
Return the remainder of x / y.
Definition: Math.h:734
Type Pow2(Type x)
Return x2.
Definition: Math.h:502