8#ifndef OPENVDB_MATH_HAS_BEEN_INCLUDED
9#define OPENVDB_MATH_HAS_BEEN_INCLUDED
12#include <openvdb/version.h>
13#include <boost/numeric/conversion/conversion_traits.hpp>
28#if defined(__INTEL_COMPILER)
29 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
30 _Pragma("warning (push)") \
31 _Pragma("warning (disable:1572)")
32 #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
33 _Pragma("warning (pop)")
34#elif defined(__clang__)
35 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
36 PRAGMA(clang diagnostic push) \
37 PRAGMA(clang diagnostic ignored "-Wfloat-equal")
38 #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
39 PRAGMA(clang diagnostic pop)
48 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
49 #define OPENVDB_NO_FP_EQUALITY_WARNING_END
56#define OPENVDB_IS_POD(Type) \
57static_assert(std::is_standard_layout<Type>::value, \
58 #Type" must be a POD type (satisfy StandardLayoutType.)"); \
59static_assert(std::is_trivial<Type>::value, \
60 #Type" must be a POD type (satisfy TrivialType.)");
70template<
typename T>
inline T
zeroVal() {
return T(0); }
72template<>
inline std::string zeroVal<std::string>() {
return ""; }
82inline std::string
operator+(
const std::string& s,
bool) {
return s; }
83inline std::string
operator+(
const std::string& s,
int) {
return s; }
84inline std::string
operator+(
const std::string& s,
float) {
return s; }
85inline std::string
operator+(
const std::string& s,
double) {
return s; }
89template<
typename Type1,
typename Type2>
90inline auto cwiseAdd(
const Type1& v,
const Type2 s)
98template<
typename Type1,
typename Type2>
107template<
typename Type1,
typename Type2>
119template <
typename T>
inline constexpr T
pi() {
return 3.141592653589793238462643383279502884e+00; }
120template <>
inline constexpr float pi() {
return 3.141592653589793238462643383279502884e+00F; }
121template <>
inline constexpr double pi() {
return 3.141592653589793238462643383279502884e+00; }
122template <>
inline constexpr long double pi() {
return 3.141592653589793238462643383279502884e+00L; }
128template<
typename T>
inline T
negative(
const T& val)
133#pragma warning(disable:4146)
141template<>
inline bool negative(
const bool& val) {
return !val; }
143template<>
inline std::string
negative(
const std::string& val) {
return val; }
148template<
typename T>
struct Tolerance {
static T
value() {
return zeroVal<T>(); } };
155template<
typename T>
struct Delta {
static T
value() {
return zeroVal<T>(); } };
156template<>
struct Delta<float> {
static float value() {
return 1e-5f; } };
157template<>
struct Delta<double> {
static double value() {
return 1e-9; } };
165template<
typename FloatType =
double,
typename EngineType = std::mt19937>
170 std::uniform_real_distribution<FloatType> mRand;
177 Rand01(
const EngineType& engine): mEngine(engine) {}
181 Rand01(
unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
186 mEngine.seed(
static_cast<typename EngineType::result_type
>(seed));
190 const EngineType&
engine()
const {
return mEngine; }
201template<
typename IntType =
int,
typename EngineType = std::mt19937>
205 using Distr = std::uniform_int_distribution<IntType>;
213 RandInt(
const EngineType& engine, IntType imin, IntType imax):
221 RandInt(
unsigned int seed, IntType imin, IntType imax):
222 mEngine(static_cast<typename EngineType::result_type>(seed)),
235 mEngine.seed(
static_cast<typename EngineType::result_type
>(seed));
239 const EngineType&
engine()
const {
return mEngine; }
249 return mRand(mEngine,
typename Distr::param_type(lo, hi));
259template<
typename Type>
269template<
typename Type>
271Clamp01(Type x) {
return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0); }
275template<
typename Type>
279 if (x >= Type(0) && x <= Type(1))
return false;
280 x = x < Type(0) ? Type(0) : Type(1);
285template<
typename Type>
289 return x > 0 ? x < 1 ? (3-2*x)*x*x : Type(1) : Type(0);
294template<
typename Type>
308inline int32_t
Abs(int32_t i) {
return std::abs(i); }
309inline int64_t
Abs(int64_t i)
311 static_assert(
sizeof(
decltype(std::abs(i))) ==
sizeof(int64_t),
312 "std::abs(int64) broken");
315inline float Abs(
float x) {
return std::fabs(x); }
316inline double Abs(
double x) {
return std::fabs(x); }
317inline long double Abs(
long double x) {
return std::fabs(x); }
318inline uint32_t
Abs(uint32_t i) {
return i; }
319inline uint64_t
Abs(uint64_t i) {
return i; }
320inline bool Abs(
bool b) {
return b; }
335template<
typename Type>
340 return x == zeroVal<Type>();
347template<
typename Type>
352 return !(x > tolerance) && !(x < -tolerance);
356template<
typename Type>
360 return !(x > tolerance) && !(x < -tolerance);
365template<
typename Type>
375isFinite(
const float x) {
return std::isfinite(x); }
380isFinite(
const Type& x) {
return std::isfinite(
static_cast<double>(x)); }
390isInfinite(
const Type& x) {
return std::isinf(
static_cast<double>(x)); }
395isNan(
const float x) {
return std::isnan(x); }
400isNan(
const Type& x) {
return std::isnan(
static_cast<double>(x)); }
404template<
typename Type>
413template<
typename Type>
421#define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \
422 template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \
423 template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \
432template<typename Type>
436 return (b - a < tolerance);
441template<
typename T0,
typename T1>
451template<
typename Type>
457 if (!(
Abs(a - b) > absTol))
return true;
464 relError =
Abs((a - b) / b);
466 relError =
Abs((a - b) / a);
468 return (relError <= relTol);
482 static_assert(
sizeof(int32_t) ==
sizeof f,
"`float` has an unexpected size.");
484 std::memcpy(&ret, &f,
sizeof(int32_t));
492 static_assert(
sizeof(int64_t) ==
sizeof d,
"`double` has an unexpected size.");
494 std::memcpy(&ret, &d,
sizeof(int64_t));
503isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
508 longLeft = INT64_C(0x8000000000000000) - longLeft;
514 longRight = INT64_C(0x8000000000000000) - longRight;
517 int64_t difference =
Abs(longLeft - longRight);
518 return (difference <= aUnitsInLastPlace);
522isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
527 intLeft = 0x80000000 - intLeft;
533 intRight = 0x80000000 - intRight;
536 int32_t difference =
Abs(intLeft - intRight);
537 return (difference <= aUnitsInLastPlace);
547template<
typename Type>
548inline Type
Pow2(Type x) {
return x*x; }
551template<
typename Type>
552inline Type
Pow3(Type x) {
return x*x*x; }
555template<
typename Type>
559template<
typename Type>
568 while (n--) ans *= x;
577 assert( b >= 0.0f &&
"Pow(float,float): base is negative" );
584 assert( b >= 0.0 &&
"Pow(double,double): base is negative" );
585 return std::pow(b,e);
593template<
typename Type>
595Max(
const Type& a,
const Type& b)
601template<
typename Type>
603Max(
const Type& a,
const Type& b,
const Type& c)
609template<
typename Type>
611Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
617template<
typename Type>
619Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
625template<
typename Type>
627Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
633template<
typename Type>
635Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
636 const Type& e,
const Type& f,
const Type& g)
642template<
typename Type>
644Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
645 const Type& e,
const Type& f,
const Type& g,
const Type& h)
654template<
typename Type>
659template<
typename Type>
664template<
typename Type>
666Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
672template<
typename Type>
674Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
680template<
typename Type>
682Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
688template<
typename Type>
690Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
691 const Type& e,
const Type& f,
const Type& g)
697template<
typename Type>
699Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
700 const Type& e,
const Type& f,
const Type& g,
const Type& h)
709template<
typename Type>
710inline Type
Exp(
const Type& x) {
return std::exp(x); }
716inline float Sin(
const float& x) {
return std::sin(x); }
718inline double Sin(
const double& x) {
return std::sin(x); }
725inline float Cos(
const float& x) {
return std::cos(x); }
727inline double Cos(
const double& x) {
return std::cos(x); }
735template <
typename Type>
736inline int Sign(
const Type &x) {
return (zeroVal<Type>() < x) - (x < zeroVal<Type>()); }
741template <
typename Type>
745 return ( (a<zeroVal<Type>()) ^ (b<zeroVal<Type>()) );
751template <
typename Type>
755 return a * b <= zeroVal<Type>();
761inline float Sqrt(
float x) {
return std::sqrt(x); }
762inline double Sqrt(
double x) {
return std::sqrt(x); }
763inline long double Sqrt(
long double x) {
return std::sqrt(x); }
769inline float Cbrt(
float x) {
return std::cbrt(x); }
770inline double Cbrt(
double x) {
return std::cbrt(x); }
771inline long double Cbrt(
long double x) {
return std::cbrt(x); }
777inline int Mod(
int x,
int y) {
return (x % y); }
778inline float Mod(
float x,
float y) {
return std::fmod(x, y); }
779inline double Mod(
double x,
double y) {
return std::fmod(x, y); }
780inline long double Mod(
long double x,
long double y) {
return std::fmod(x, y); }
781template<
typename Type>
inline Type
Remainder(Type x, Type y) {
return Mod(x, y); }
787inline float RoundUp(
float x) {
return std::ceil(x); }
788inline double RoundUp(
double x) {
return std::ceil(x); }
789inline long double RoundUp(
long double x) {
return std::ceil(x); }
792template<
typename Type>
797 return remainder ? x-remainder+base : x;
803inline float RoundDown(
float x) {
return std::floor(x); }
804inline double RoundDown(
double x) {
return std::floor(x); }
805inline long double RoundDown(
long double x) {
return std::floor(x); }
808template<
typename Type>
813 return remainder ? x-remainder : x;
827template<
typename Type>
833template<
typename Type>
841template<
typename Type>
863template<
typename Type>
864inline Type
Chop(Type x, Type delta) {
return (
Abs(x) < delta ? zeroVal<Type>() : x); }
868template<
typename Type>
872 Type tenth =
static_cast<Type
>(
Pow(
size_t(10), digits));
884inline int32_t
PrintCast(int8_t val) {
return int32_t(val); }
885inline uint32_t
PrintCast(uint8_t val) {
return uint32_t(val); }
892template<
typename Type>
920template <
typename S,
typename T>
922 using type =
typename boost::numeric::conversion_traits<S, T>::supertype;
929template<
typename Vec3T>
934 for (
size_t i = 1; i < 3; ++i) {
936 if (v[i] <= v[r]) r = i;
945template<
typename Vec3T>
950 for (
size_t i = 1; i < 3; ++i) {
952 if (v[i] >= v[r]) r = i;
ValueT value
Definition: GridBuilder.h:1290
#define OPENVDB_EXACT_IS_APPROX_EQUAL(T)
Definition: Math.h:421
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition: Math.h:49
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition: Math.h:48
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:167
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:184
Rand01(const EngineType &engine)
Initialize the generator.
Definition: Math.h:177
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:190
FloatType operator()()
Return a uniformly distributed random number in the range [0, 1).
Definition: Math.h:193
Rand01(unsigned int seed)
Initialize the generator.
Definition: Math.h:181
FloatType ValueType
Definition: Math.h:173
Simple random integer generator.
Definition: Math.h:203
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition: Math.h:233
RandInt(unsigned int seed, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:221
void setRange(IntType imin, IntType imax)
Change the range over which integers are distributed to [imin, imax].
Definition: Math.h:227
IntType operator()()
Return a randomly-generated integer in the current range.
Definition: Math.h:242
const EngineType & engine() const
Return a const reference to the random number generator.
Definition: Math.h:239
RandInt(const EngineType &engine, IntType imin, IntType imax)
Initialize the generator.
Definition: Math.h:213
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:246
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:753
bool isRelOrApproxEqual(const bool &a, const bool &b, const bool &, const bool &)
Definition: Math.h:473
double Pow(double b, double e)
Definition: Math.h:582
bool isApproxZero(const Type &x, const Type &tolerance)
Return true if x is equal to zero to within the given tolerance.
Definition: Math.h:358
int Sign(const Type &x)
Return the sign of the given value as an integer (either -1, 0 or 1).
Definition: Math.h:736
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition: Math.h:367
bool isFinite(const Type &x)
Return true if x is finite.
Definition: Math.h:380
long double Round(long double x)
Definition: Math.h:821
Type Pow4(Type x)
Return x4.
Definition: Math.h:556
long double Mod(long double x, long double y)
Definition: Math.h:780
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:699
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:443
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:644
double Sin(const double &x)
Definition: Math.h:718
bool isNegative< bool >(const bool &)
Definition: Math.h:370
Type Inv(Type x)
Return the inverse of x.
Definition: Math.h:894
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:931
bool isUlpsEqual(const float aLeft, const float aRight, const int32_t aUnitsInLastPlace)
Definition: Math.h:522
Type IntegerPart(Type x)
Return the integer part of x.
Definition: Math.h:835
std::string negative(const std::string &val)
Return the "negation" of the given string.
Definition: Math.h:143
auto cwiseAdd(const Type1 &v, const Type2 s)
Componentwise adder for POD types.
Definition: Math.h:90
bool isNan(const Type &x)
Return true if x is a NaN (Not-A-Number) value.
Definition: Math.h:400
uint32_t PrintCast(uint8_t val)
Definition: Math.h:885
Type Exp(const Type &x)
Return ex.
Definition: Math.h:710
bool SignChange(const Type &a, const Type &b)
Return true if a and b have different signs.
Definition: Math.h:743
std::string operator+(const std::string &s, double)
Definition: Math.h:85
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition: Math.h:947
double Cos(const double &x)
Definition: Math.h:727
Type Clamp(Type x, Type min, Type max)
Return x clamped to [min, max].
Definition: Math.h:261
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:415
Type Pow2(Type x)
Return x2.
Definition: Math.h:548
int64_t doubleToInt64(const double d)
Definition: Math.h:489
Type FractionalPart(Type x)
Return the fractional part of x.
Definition: Math.h:843
long double Cbrt(long double x)
Definition: Math.h:771
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:296
Type EuclideanRemainder(Type x)
Definition: Math.h:829
Type Pow3(Type x)
Return x3.
Definition: Math.h:552
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition: Math.h:870
constexpr float pi()
Definition: Math.h:120
int32_t floatToInt32(const float f)
Definition: Math.h:479
bool isInfinite(const Type &x)
Return true if x is an infinity value (either positive infinity or negative infinity).
Definition: Math.h:390
bool cwiseGreaterThan(const Type1 &a, const Type2 &b)
Componentwise greater than for POD types.
Definition: Math.h:108
std::enable_if< std::is_same< T, size_t >::value, T >::type Abs(T i)
Definition: Math.h:324
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:434
Type RoundUp(Type x, Type base)
Return x rounded up to the nearest multiple of base.
Definition: Math.h:794
long double Sqrt(long double x)
Definition: Math.h:763
Type Remainder(Type x, Type y)
Definition: Math.h:781
int Floor(long double x)
Definition: Math.h:850
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:337
bool ClampTest01(Type &x)
Return true if x is outside [0,1].
Definition: Math.h:277
Type RoundDown(Type x, Type base)
Return x rounded down to the nearest multiple of base.
Definition: Math.h:810
Type Clamp01(Type x)
Return x clamped to [0, 1].
Definition: Math.h:271
int Ceil(long double x)
Definition: Math.h:858
Axis
Definition: Math.h:901
@ Z_AXIS
Definition: Math.h:904
@ X_AXIS
Definition: Math.h:902
@ Y_AXIS
Definition: Math.h:903
Type Chop(Type x, Type delta)
Return x if it is greater or equal in magnitude than delta. Otherwise, return zero.
Definition: Math.h:864
bool cwiseLessThan(const Type1 &a, const Type2 &b)
Componentwise less than for POD types.
Definition: Math.h:99
RotationOrder
Definition: Math.h:908
@ YXZ_ROTATION
Definition: Math.h:911
@ ZXY_ROTATION
Definition: Math.h:913
@ YZX_ROTATION
Definition: Math.h:912
@ ZXZ_ROTATION
Definition: Math.h:916
@ XZX_ROTATION
Definition: Math.h:915
@ XYZ_ROTATION
Definition: Math.h:909
@ ZYX_ROTATION
Definition: Math.h:914
@ XZY_ROTATION
Definition: Math.h:910
T zeroVal()
Return the value of type T that corresponds to zero.
Definition: Math.h:70
bool zeroVal< bool >()
Return the bool value that corresponds to zero.
Definition: Math.h:74
Definition: Exceptions.h:13
static double value()
Definition: Math.h:157
static float value()
Definition: Math.h:156
Delta for small floating-point offsets.
Definition: Math.h:155
static T value()
Definition: Math.h:155
static double value()
Definition: Math.h:150
static float value()
Definition: Math.h:149
Tolerance for floating-point comparison.
Definition: Math.h:148
static T value()
Definition: Math.h:148
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212