Go to the documentation of this file.
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 ""; }
69 inline std::string
operator+(
const std::string& s,
bool) {
return s; }
72 inline std::string
operator+(
const std::string& s,
int) {
return s; }
73 inline std::string
operator+(
const std::string& s,
float) {
return s; }
74 inline std::string
operator+(
const std::string& s,
double) {
return s; }
78 template<
typename Type1,
typename Type2>
79 inline auto cwiseAdd(
const Type1& v,
const Type2 s)
87 template<
typename Type1,
typename Type2>
96 template<
typename Type1,
typename Type2>
108 template <
typename T>
inline constexpr T
pi() {
return 3.141592653589793238462643383279502884e+00; }
109 template <>
inline constexpr
float pi() {
return 3.141592653589793238462643383279502884e+00F; }
110 template <>
inline constexpr
double pi() {
return 3.141592653589793238462643383279502884e+00; }
111 template <>
inline constexpr
long double pi() {
return 3.141592653589793238462643383279502884e+00L; }
117 template<
typename T>
inline T
negative(
const T& val)
120 #if defined(_MSC_VER)
121 #pragma warning(push)
122 #pragma warning(disable:4146)
125 #if defined(_MSC_VER)
130 template<>
inline bool negative(
const bool& val) {
return !val; }
132 template<>
inline std::string
negative(
const std::string& val) {
return val; }
136 template<
typename T>
struct Tolerance {
static T value() {
return zeroVal<T>(); } };
143 template<
typename T>
struct Delta {
static T value() {
return zeroVal<T>(); } };
145 template<>
struct Delta<float> {
static float value() {
return 1e-5f; } };
146 template<>
struct Delta<double> {
static double value() {
return 1e-9; } };
154 template<
typename FloatType =
double,
typename EngineType = std::mt19937>
159 std::uniform_real_distribution<FloatType> mRand;
166 Rand01(
const EngineType& engine): mEngine(engine) {}
170 Rand01(
unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
175 mEngine.seed(
static_cast<typename EngineType::result_type
>(seed));
179 const EngineType&
engine()
const {
return mEngine; }
190 template<
typename IntType =
int,
typename EngineType = std::mt19937>
194 using Distr = std::uniform_int_distribution<IntType>;
202 RandInt(
const EngineType& engine, IntType imin, IntType imax):
210 RandInt(
unsigned int seed, IntType imin, IntType imax):
211 mEngine(static_cast<typename EngineType::result_type>(seed)),
224 mEngine.seed(
static_cast<typename EngineType::result_type
>(seed));
228 const EngineType&
engine()
const {
return mEngine; }
238 return mRand(mEngine,
typename Distr::param_type(lo, hi));
248 template<
typename Type>
258 template<
typename Type>
260 Clamp01(Type x) {
return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0); }
264 template<
typename Type>
268 if (x >= Type(0) && x <= Type(1))
return false;
269 x = x < Type(0) ? Type(0) : Type(1);
274 template<
typename Type>
278 return x > 0 ? x < 1 ? (3-2*x)*x*x : Type(1) : Type(0);
283 template<
typename Type>
296 inline int32_t
Abs(int32_t i) {
return abs(i); }
298 inline int64_t
Abs(int64_t i)
301 return (i < int64_t(0) ? -i : i);
306 inline float Abs(
float x) {
return std::fabs(x); }
307 inline double Abs(
double x) {
return std::fabs(x); }
308 inline long double Abs(
long double x) {
return std::fabs(x); }
309 inline uint32_t
Abs(uint32_t i) {
return i; }
310 inline uint64_t
Abs(uint64_t i) {
return i; }
311 inline bool Abs(
bool b) {
return b; }
313 #if defined(__APPLE__) || defined(MACOSX)
314 inline size_t Abs(
size_t i) {
return i; }
326 template<
typename Type>
331 return x == zeroVal<Type>();
338 template<
typename Type>
343 return !(x > tolerance) && !(x < -tolerance);
347 template<
typename Type>
351 return !(x > tolerance) && !(x < -tolerance);
356 template<
typename Type>
366 isFinite(
const float x) {
return std::isfinite(x); }
369 template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
371 isFinite(
const Type& x) {
return std::isfinite(
static_cast<double>(x)); }
379 template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
381 isInfinite(
const Type& x) {
return std::isinf(
static_cast<double>(x)); }
386 isNan(
const float x) {
return std::isnan(x); }
389 template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
391 isNan(
const Type& x) {
return std::isnan(
static_cast<double>(x)); }
395 template<
typename Type>
404 template<
typename Type>
412 #define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \
413 template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \
414 template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \
423 template<typename Type>
427 return (b - a < tolerance);
432 template<
typename T0,
typename T1>
442 template<
typename Type>
448 if (!(
Abs(a - b) > absTol))
return true;
455 relError =
Abs((a - b) / b);
457 relError =
Abs((a - b) / a);
459 return (relError <= relTol);
476 union FloatOrInt32 {
float floatValue; int32_t int32Value; };
477 const FloatOrInt32* foi =
reinterpret_cast<const FloatOrInt32*
>(&aFloatValue);
478 return foi->int32Value;
485 union DoubleOrInt64 {
double doubleValue; int64_t int64Value; };
486 const DoubleOrInt64* dol =
reinterpret_cast<const DoubleOrInt64*
>(&aDoubleValue);
487 return dol->int64Value;
496 isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
501 longLeft = INT64_C(0x8000000000000000) - longLeft;
507 longRight = INT64_C(0x8000000000000000) - longRight;
510 int64_t difference = labs(longLeft - longRight);
511 return (difference <= aUnitsInLastPlace);
515 isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
520 intLeft = 0x80000000 - intLeft;
526 intRight = 0x80000000 - intRight;
529 int32_t difference = abs(intLeft - intRight);
530 return (difference <= aUnitsInLastPlace);
540 template<
typename Type>
541 inline Type
Pow2(Type x) {
return x*x; }
544 template<
typename Type>
545 inline Type
Pow3(Type x) {
return x*x*x; }
548 template<
typename Type>
552 template<
typename Type>
561 while (n--) ans *= x;
570 assert( b >= 0.0f &&
"Pow(float,float): base is negative" );
577 assert( b >= 0.0 &&
"Pow(double,double): base is negative" );
578 return std::pow(b,e);
586 template<
typename Type>
588 Max(
const Type& a,
const Type& b)
594 template<
typename Type>
596 Max(
const Type& a,
const Type& b,
const Type& c)
602 template<
typename Type>
604 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
610 template<
typename Type>
612 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
618 template<
typename Type>
620 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
626 template<
typename Type>
628 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
629 const Type& e,
const Type& f,
const Type& g)
635 template<
typename Type>
637 Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
638 const Type& e,
const Type& f,
const Type& g,
const Type& h)
647 template<
typename Type>
652 template<
typename Type>
657 template<
typename Type>
659 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
665 template<
typename Type>
667 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
673 template<
typename Type>
675 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
681 template<
typename Type>
683 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
684 const Type& e,
const Type& f,
const Type& g)
690 template<
typename Type>
692 Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
693 const Type& e,
const Type& f,
const Type& g,
const Type& h)
702 template<
typename Type>
703 inline Type
Exp(
const Type& x) {
return std::exp(x); }
708 inline float Sin(
const float& x) {
return std::sin(x); }
711 inline double Sin(
const double& x) {
return std::sin(x); }
717 inline float Cos(
const float& x) {
return std::cos(x); }
720 inline double Cos(
const double& x) {
return std::cos(x); }
728 template <
typename Type>
729 inline int Sign(
const Type &x) {
return (zeroVal<Type>() < x) - (x < zeroVal<Type>()); }
734 template <
typename Type>
738 return ( (a<zeroVal<Type>()) ^ (b<zeroVal<Type>()) );
744 template <
typename Type>
748 return a * b <= zeroVal<Type>();
753 inline float Sqrt(
float x) {
return std::sqrt(x); }
755 inline double Sqrt(
double x) {
return std::sqrt(x); }
756 inline long double Sqrt(
long double x) {
return std::sqrt(x); }
761 inline float Cbrt(
float x) {
return std::cbrt(x); }
763 inline double Cbrt(
double x) {
return std::cbrt(x); }
764 inline long double Cbrt(
long double x) {
return std::cbrt(x); }
769 inline int Mod(
int x,
int y) {
return (x % y); }
771 inline float Mod(
float x,
float y) {
return std::fmod(x, y); }
772 inline double Mod(
double x,
double y) {
return std::fmod(x, y); }
773 inline long double Mod(
long double x,
long double y) {
return std::fmod(x, y); }
774 template<
typename Type>
inline Type
Remainder(Type x, Type y) {
return Mod(x, y); }
779 inline float RoundUp(
float x) {
return std::ceil(x); }
781 inline double RoundUp(
double x) {
return std::ceil(x); }
782 inline long double RoundUp(
long double x) {
return std::ceil(x); }
784 template<
typename Type>
790 return remainder ? x-remainder+base : x;
795 inline float RoundDown(
float x) {
return std::floor(x); }
797 inline double RoundDown(
double x) {
return std::floor(x); }
798 inline long double RoundDown(
long double x) {
return std::floor(x); }
800 template<
typename Type>
806 return remainder ? x-remainder : x;
820 template<
typename Type>
826 template<
typename Type>
834 template<
typename Type>
848 inline int Ceil(
float x) {
return int(
RoundUp(x)); }
856 template<
typename Type>
857 inline Type
Chop(Type x, Type delta) {
return (
Abs(x) < delta ? zeroVal<Type>() : x); }
861 template<
typename Type>
865 Type tenth =
Pow(10,digits);
875 inline auto PrintCast(
const T& val) ->
typename std::enable_if<!std::is_same<T, int8_t>::value
876 && !std::is_same<T, uint8_t>::value,
const T&>::type {
return val; }
877 inline int32_t
PrintCast(int8_t val) {
return int32_t(val); }
878 inline uint32_t
PrintCast(uint8_t val) {
return uint32_t(val); }
885 template<
typename Type>
913 template <
typename S,
typename T>
915 using type =
typename boost::numeric::conversion_traits<S, T>::supertype;
926 template<
typename Vec3T>
930 static const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
931 const size_t hashKey =
932 ((v[0] < v[1]) << 2) + ((v[0] < v[2]) << 1) + (v[1] < v[2]);
933 return hashTable[hashKey];
944 template<
typename Vec3T>
948 static const size_t hashTable[8] = { 2, 1, 9, 1, 2, 9, 0, 0 };
949 const size_t hashKey =
950 ((v[0] > v[1]) << 2) + ((v[0] > v[2]) << 1) + (v[1] > v[2]);
951 return hashTable[hashKey];
958 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED
Type Pow4(Type x)
Return x4.
Definition: Math.h:549
@ ZXY_ROTATION
Definition: Math.h:906
@ YZX_ROTATION
Definition: Math.h:905
Type Chop(Type x, Type delta)
Return x if it is greater or equal in magnitude than delta. Otherwise, return zero.
Definition: Math.h:857
std::string operator+(const std::string &s, double)
Definition: Math.h:74
RandInt(const EngineType &engine, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:202
RandInt(unsigned int seed, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:210
double Cos(const double &x)
Definition: Math.h:720
Type Remainder(Type x, Type y)
Definition: Math.h:774
static float value()
Definition: Math.h:138
double ValueType
Definition: Math.h:162
bool isNegative< bool >(const bool &)
Definition: Math.h:361
bool zeroVal< bool >()
Return the bool value that corresponds to zero.
Definition: Math.h:63
Rand01(const EngineType &engine)
Initialize the generator.
Definition: Math.h:166
bool isNan(const Type &x)
Return true if x is a NaN (Not-A-Number) value.
Definition: Math.h:391
std::string negative(const std::string &val)
Return the "negation" of the given string.
Definition: Math.h:132
@ ZXZ_ROTATION
Definition: Math.h:909
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:637
int Floor(long double x)
Definition: Math.h:843
Delta for small floating-point offsets.
Definition: Math.h:144
bool isApproxZero(const Type &x, const Type &tolerance)
Return true if x is equal to zero to within the given tolerance.
Definition: Math.h:349
int32_t floatToInt32(const float aFloatValue)
Definition: Math.h:474
RotationOrder
Definition: Math.h:901
Type Clamp01(Type x)
Return x clamped to [0, 1].
Definition: Math.h:260
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:328
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:222
static float value()
Definition: Math.h:145
Library and file format version numbers.
Type Inv(Type x)
Return the inverse of x.
Definition: Math.h:887
static double value()
Definition: Math.h:139
bool Abs(bool b)
Definition: Math.h:311
bool isRelOrApproxEqual(const bool &a, const bool &b, const bool &, const bool &)
Definition: Math.h:464
@ XZY_ROTATION
Definition: Math.h:903
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:179
@ XYZ_ROTATION
Definition: Math.h:902
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:746
constexpr float pi()
Definition: Math.h:109
uint32_t PrintCast(uint8_t val)
Definition: Math.h:878
Tolerance for floating-point comparison.
Definition: Math.h:137
Type RoundDown(Type x, Type base)
Return x rounded down to the nearest multiple of base.
Definition: Math.h:803
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:406
@ Z_AXIS
Definition: Math.h:897
void setRange(IntType imin, IntType imax)
Change the range over which integers are distributed to [imin, imax].
Definition: Math.h:216
Type Pow2(Type x)
Return x2.
Definition: Math.h:541
Axis
Definition: Math.h:894
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:156
int Ceil(long double x)
Definition: Math.h:851
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:863
Type RoundUp(Type x, Type base)
Return x rounded up to the nearest multiple of base.
Definition: Math.h:787
FloatType operator()()
Return a uniformly distributed random number in the range [0, 1).
Definition: Math.h:182
Type FractionalPart(Type x)
Return the fractional part of x.
Definition: Math.h:836
auto cwiseAdd(const Type1 &v, const Type2 s)
Componentwise adder for POD types.
Definition: Math.h:79
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition: Math.h:47
Type Pow3(Type x)
Return x3.
Definition: Math.h:545
bool isFinite(const Type &x)
Return true if x is finite.
Definition: Math.h:371
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:173
bool cwiseLessThan(const Type1 &a, const Type2 &b)
Componentwise less than for POD types.
Definition: Math.h:88
double Sin(const double &x)
Definition: Math.h:711
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:946
bool isInfinite(const Type &x)
Return true if x is an infinity value (either positive infinity or negative infinity).
Definition: Math.h:381
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:358
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:425
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:928
int64_t doubleToInt64(const double aDoubleValue)
Definition: Math.h:483
Type EuclideanRemainder(Type x)
Definition: Math.h:822
@ YXZ_ROTATION
Definition: Math.h:904
@ X_AXIS
Definition: Math.h:895
#define OPENVDB_EXACT_IS_APPROX_EQUAL(T)
Definition: Math.h:412
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:147
T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:59
Type IntegerPart(Type x)
Return the integer part of x.
Definition: Math.h:828
static double value()
Definition: Math.h:146
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:692
@ XZX_ROTATION
Definition: Math.h:908
int Sign(const Type &x)
Return the sign of the given value as an integer (either -1, 0 or 1).
Definition: Math.h:729
long double Round(long double x)
Definition: Math.h:814
IntType operator()()
Return a randomly-generated integer in the current range.
Definition: Math.h:231
bool SignChange(const Type &a, const Type &b)
Return true if a and b have different signs.
Definition: Math.h:736
@ Y_AXIS
Definition: Math.h:896
Simple random integer generator.
Definition: Math.h:192
Type Exp(const Type &x)
Return ex.
Definition: Math.h:703
Rand01(unsigned int seed)
Initialize the generator.
Definition: Math.h:170
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:228
long double Cbrt(long double x)
Definition: Math.h:764
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:95
long double Sqrt(long double x)
Definition: Math.h:756
double Pow(double b, double e)
Definition: Math.h:575
bool ClampTest01(Type &x)
Return true if x is outside [0,1].
Definition: Math.h:266
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:434
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:285
Definition: openvdb/Exceptions.h:13
bool isUlpsEqual(const float aLeft, const float aRight, const int32_t aUnitsInLastPlace)
Definition: Math.h:515
@ ZYX_ROTATION
Definition: Math.h:907
long double Mod(long double x, long double y)
Definition: Math.h:773
bool cwiseGreaterThan(const Type1 &a, const Type2 &b)
Componentwise greater than for POD types.
Definition: Math.h:97
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:235
Type Clamp(Type x, Type min, Type max)
Return x clamped to [min, max].
Definition: Math.h:250
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition: Math.h:48