8#ifndef OPENVDB_MATH_HAS_BEEN_INCLUDED
9#define OPENVDB_MATH_HAS_BEEN_INCLUDED
12#include <openvdb/version.h>
29#if defined(__INTEL_COMPILER)
30 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
31 _Pragma("warning (push)") \
32 _Pragma("warning (disable:1572)")
33 #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
34 _Pragma("warning (pop)")
35#elif defined(__clang__)
36 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
37 PRAGMA(clang diagnostic push) \
38 PRAGMA(clang diagnostic ignored "-Wfloat-equal")
39 #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
40 PRAGMA(clang diagnostic pop)
49 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
50 #define OPENVDB_NO_FP_EQUALITY_WARNING_END
57#define OPENVDB_IS_POD(Type) \
58static_assert(std::is_standard_layout<Type>::value, \
59 #Type" must be a POD type (satisfy StandardLayoutType.)"); \
60static_assert(std::is_trivial<Type>::value, \
61 #Type" must be a POD type (satisfy TrivialType.)");
71template<
typename T>
inline constexpr T
zeroVal() {
return T(0); }
93inline std::string
operator+(
const std::string& s,
bool) {
return s; }
94inline std::string
operator+(
const std::string& s,
int) {
return s; }
95inline std::string
operator+(
const std::string& s,
float) {
return s; }
96inline std::string
operator+(
const std::string& s,
double) {
return s; }
100template<
typename Type1,
typename Type2>
109template<
typename Type1,
typename Type2>
118template<
typename Type1,
typename Type2>
130template <
typename T>
inline constexpr T
pi() {
return 3.141592653589793238462643383279502884e+00; }
131template <>
inline constexpr float pi() {
return 3.141592653589793238462643383279502884e+00F; }
132template <>
inline constexpr double pi() {
return 3.141592653589793238462643383279502884e+00; }
133template <>
inline constexpr long double pi() {
return 3.141592653589793238462643383279502884e+00L; }
139template<
typename T>
inline T
negative(
const T& val)
144#pragma warning(disable:4146)
152template<>
inline bool negative(
const bool& val) {
return !val; }
154template<>
inline std::string
negative(
const std::string& val) {
return val; }
169template<>
struct Delta<float> {
static float value() {
return 1e-5f; } };
170template<>
struct Delta<double> {
static double value() {
return 1e-9; } };
178template<
typename FloatType =
double,
typename EngineType = std::mt19937>
183 std::uniform_real_distribution<FloatType> mRand;
194 Rand01(
unsigned int seed): mEngine(static_cast<typename EngineType::result_type>(seed)) {}
199 mEngine.seed(
static_cast<typename EngineType::result_type
>(seed));
203 const EngineType&
engine()
const {
return mEngine; }
214template<
typename IntType =
int,
typename EngineType = std::mt19937>
218 using Distr = std::uniform_int_distribution<IntType>;
228 mRand(
std::min(imin, imax),
std::max(imin, imax))
234 RandInt(
unsigned int seed, IntType imin, IntType imax):
235 mEngine(static_cast<typename EngineType::result_type>(seed)),
236 mRand(
std::min(imin, imax),
std::max(imin, imax))
242 mRand = Distr(std::min(imin, imax), std::max(imin, imax));
248 mEngine.seed(
static_cast<typename EngineType::result_type
>(seed));
252 const EngineType&
engine()
const {
return mEngine; }
261 const IntType lo = std::min(imin, imax), hi = std::max(imin, imax);
262 return mRand(mEngine,
typename Distr::param_type(lo, hi));
272template<
typename Type>
277 return x > min ? x < max ? x : max : min;
282template<
typename Type>
284Clamp01(Type x) {
return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0); }
288template<
typename Type>
292 if (x >= Type(0) && x <= Type(1))
return false;
293 x = x < Type(0) ? Type(0) : Type(1);
298template<
typename Type>
302 return x > 0 ? x < 1 ? (3-2*x)*x*x : Type(1) : Type(0);
307template<
typename Type>
321inline int32_t
Abs(int32_t i) {
return std::abs(i); }
322inline int64_t
Abs(int64_t i)
324 static_assert(
sizeof(
decltype(std::abs(i))) ==
sizeof(int64_t),
325 "std::abs(int64) broken");
328inline float Abs(
float x) {
return std::fabs(x); }
329inline double Abs(
double x) {
return std::fabs(x); }
330inline long double Abs(
long double x) {
return std::fabs(x); }
331inline uint32_t
Abs(uint32_t i) {
return i; }
332inline uint64_t
Abs(uint64_t i) {
return i; }
333inline bool Abs(
bool b) {
return b; }
336inline typename std::enable_if<std::is_same<T, size_t>::value, T>::type
348template<
typename Type>
360template<
typename Type>
365 return !(x > tolerance) && !(x < -tolerance);
369template<
typename Type>
373 return !(x > tolerance) && !(x < -tolerance);
378template<
typename Type>
388isFinite(
const float x) {
return std::isfinite(x); }
395template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
397isFinite(
const Type& x) {
return std::isfinite(
static_cast<double>(x)); }
409template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
411isInfinite(
const Type& x) {
return std::isinf(
static_cast<double>(x)); }
416isNan(
const float x) {
return std::isnan(x); }
423template<typename Type, typename std::enable_if<std::is_arithmetic<Type>::value,
int>::type = 0>
425isNan(
const Type& x) {
return std::isnan(
static_cast<double>(x)); }
429template<
typename Type>
438template<
typename Type>
446#define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \
447 template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \
448 template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \
457template<typename Type>
461 return (b - a < tolerance);
466template<
typename T0,
typename T1>
476template<
typename Type>
482 if (!(
Abs(a - b) > absTol))
return true;
489 relError =
Abs((a - b) / b);
491 relError =
Abs((a - b) / a);
493 return (relError <= relTol);
507 static_assert(
sizeof(int32_t) ==
sizeof f,
"`float` has an unexpected size.");
509 std::memcpy(&ret, &f,
sizeof(int32_t));
517 static_assert(
sizeof(int64_t) ==
sizeof d,
"`double` has an unexpected size.");
519 std::memcpy(&ret, &d,
sizeof(int64_t));
528isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
533 longLeft = INT64_C(0x8000000000000000) - longLeft;
539 longRight = INT64_C(0x8000000000000000) - longRight;
542 int64_t difference =
Abs(longLeft - longRight);
543 return (difference <= aUnitsInLastPlace);
547isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
552 intLeft = 0x80000000 - intLeft;
558 intRight = 0x80000000 - intRight;
561 int32_t difference =
Abs(intLeft - intRight);
562 return (difference <= aUnitsInLastPlace);
572template<
typename Type>
573inline Type
Pow2(Type x) {
return x*x; }
576template<
typename Type>
577inline Type
Pow3(Type x) {
return x*x*x; }
580template<
typename Type>
584template<
typename Type>
593 while (n--) ans *= x;
602 OPENVDB_ASSERT( b >= 0.0f &&
"Pow(half,half): base is negative" );
611 OPENVDB_ASSERT( b >= 0.0f &&
"Pow(float,float): base is negative" );
618 OPENVDB_ASSERT( b >= 0.0 &&
"Pow(double,double): base is negative" );
619 return std::pow(b,e);
631 return a < b ? b : a;
634template<
typename Type>
636max_impl(
const Type& a,
const Type& b)
638 return std::max(a,b);
644template<
typename Type>
646Max(
const Type& a,
const Type& b)
648 return internal::max_impl(a,b);
652template<
typename Type>
654Max(
const Type& a,
const Type& b,
const Type& c)
656 return internal::max_impl(internal::max_impl(a,b), c);
660template<
typename Type>
662Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
664 return internal::max_impl(internal::max_impl(a,b), internal::max_impl(c,d));
668template<
typename Type>
670Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
672 return internal::max_impl(internal::max_impl(a,b),
Max(c,d,e));
676template<
typename Type>
678Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
680 return internal::max_impl(
Max(a,b,c),
Max(d,e,f));
684template<
typename Type>
686Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
687 const Type& e,
const Type& f,
const Type& g)
689 return internal::max_impl(
Max(a,b,c,d),
Max(e,f,g));
693template<
typename Type>
695Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
696 const Type& e,
const Type& f,
const Type& g,
const Type& h)
698 return internal::max_impl(
Max(a,b,c,d),
Max(e,f,g,h));
709 return b < a ? b : a;
712template<
typename Type>
714min_impl(
const Type& a,
const Type& b)
716 return std::min(a,b);
722template<
typename Type>
724Min(
const Type& a,
const Type& b) {
return internal::min_impl(a, b); }
727template<
typename Type>
729Min(
const Type& a,
const Type& b,
const Type& c)
731 return internal::min_impl(internal::min_impl(a, b), c);
735template<
typename Type>
737Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d)
739 return internal::min_impl(internal::min_impl(a, b), internal::min_impl(c, d));
743template<
typename Type>
745Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e)
747 return internal::min_impl(internal::min_impl(a,b),
Min(c,d,e));
751template<
typename Type>
753Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
const Type& e,
const Type& f)
755 return internal::min_impl(
Min(a,b,c),
Min(d,e,f));
759template<
typename Type>
761Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
762 const Type& e,
const Type& f,
const Type& g)
764 return internal::min_impl(
Min(a,b,c,d),
Min(e,f,g));
768template<
typename Type>
770Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
771 const Type& e,
const Type& f,
const Type& g,
const Type& h)
773 return internal::min_impl(
Min(a,b,c,d),
Min(e,f,g,h));
780template<
typename Type>
781inline Type
Exp(
const Type& x) {
return std::exp(x); }
787inline float Sin(
const float& x) {
return std::sin(x); }
789inline double Sin(
const double& x) {
return std::sin(x); }
796inline float Cos(
const float& x) {
return std::cos(x); }
798inline double Cos(
const double& x) {
return std::cos(x); }
806template <
typename Type>
812template <
typename Type>
822template <
typename Type>
826 return a * b <= zeroVal<Type>();
832inline float Sqrt(
float x) {
return std::sqrt(x); }
833inline double Sqrt(
double x) {
return std::sqrt(x); }
834inline long double Sqrt(
long double x) {
return std::sqrt(x); }
840inline float Cbrt(
float x) {
return std::cbrt(x); }
841inline double Cbrt(
double x) {
return std::cbrt(x); }
842inline long double Cbrt(
long double x) {
return std::cbrt(x); }
848inline int Mod(
int x,
int y) {
return (x % y); }
849inline float Mod(
float x,
float y) {
return std::fmod(x, y); }
850inline double Mod(
double x,
double y) {
return std::fmod(x, y); }
851inline long double Mod(
long double x,
long double y) {
return std::fmod(x, y); }
852template<
typename Type>
inline Type
Remainder(Type x, Type y) {
return Mod(x, y); }
858inline float RoundUp(
float x) {
return std::ceil(x); }
859inline double RoundUp(
double x) {
return std::ceil(x); }
860inline long double RoundUp(
long double x) {
return std::ceil(x); }
863template<
typename Type>
868 return remainder ? x-remainder+base : x;
874inline float RoundDown(
float x) {
return std::floor(x); }
875inline double RoundDown(
double x) {
return std::floor(x); }
876inline long double RoundDown(
long double x) {
return std::floor(x); }
879template<
typename Type>
884 return remainder ? x-remainder : x;
898template<
typename Type>
904template<
typename Type>
912template<
typename Type>
934template<
typename Type>
939template<
typename Type>
943 Type tenth =
static_cast<Type
>(
Pow(
size_t(10), digits));
953inline auto PrintCast(
const T& val) ->
typename std::enable_if<!std::is_same<T, int8_t>::value
954 && !std::is_same<T, uint8_t>::value,
const T&>::type {
return val; }
955inline int32_t
PrintCast(int8_t val) {
return int32_t(val); }
956inline uint32_t
PrintCast(uint8_t val) {
return uint32_t(val); }
963template<
typename Type>
990template <
typename S,
typename T,
typename = std::enable_if_t<openvdb::is_arithmetic_v<S>&& openvdb::is_arithmetic_v<T>>>
992 using type =
typename std::common_type_t<S,T>;
1000template<
typename Vec3T>
1005 for (
size_t i = 1; i < 3; ++i) {
1007 if (v[i] <= v[r]) r = i;
1016template<
typename Vec3T>
1021 for (
size_t i = 1; i < 3; ++i) {
1023 if (v[i] >= v[r]) r = i;
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
#define OPENVDB_EXACT_IS_APPROX_EQUAL(T)
Definition Math.h:446
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition Math.h:50
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition Math.h:49
Simple generator of random numbers over the range [0, 1)
Definition Math.h:180
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition Math.h:197
Rand01(const EngineType &engine)
Initialize the generator.
Definition Math.h:190
const std::mt19937 & engine() const
Definition Math.h:203
FloatType operator()()
Return a uniformly distributed random number in the range [0, 1).
Definition Math.h:206
Rand01(unsigned int seed)
Initialize the generator.
Definition Math.h:194
FloatType ValueType
Definition Math.h:186
Simple random integer generator.
Definition Math.h:216
void setSeed(unsigned int seed)
Set the seed value for the random number generator.
Definition Math.h:246
RandInt(unsigned int seed, IntType imin, IntType imax)
Initialize the generator.
Definition Math.h:234
void setRange(IntType imin, IntType imax)
Change the range over which integers are distributed to [imin, imax].
Definition Math.h:240
IntType operator()()
Return a randomly-generated integer in the current range.
Definition Math.h:255
const std::mt19937 & engine() const
Definition Math.h:252
RandInt(const EngineType &engine, IntType imin, IntType imax)
Initialize the generator.
Definition Math.h:226
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:259
int Sign(const Type &x)
Return the sign of the given value as an integer (either -1, 0 or 1).
Definition Math.h:807
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:459
size_t MaxIndex(const Vec3T &v)
Return the index [0,1,2] of the largest value in a 3D vector.
Definition Math.h:1018
float RoundUp(float x)
Return x rounded up to the nearest integer.
Definition Math.h:858
Type SmoothUnitStep(Type x)
Return 0 if x < 0, 1 if x > 1 or else (3 − 2 x) x².
Definition Math.h:300
int Ceil(float x)
Return the ceiling of x.
Definition Math.h:927
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance.
Definition Math.h:362
bool isInfinite(const float x)
Return true if x is an infinity value (either positive infinity or negative infinity).
Definition Math.h:402
Type IntegerPart(Type x)
Return the integer part of x.
Definition Math.h:906
bool cwiseLessThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
Definition Mat.h:1015
constexpr T pi()
Pi constant taken from Boost to match old behaviour.
Definition Math.h:130
Type Pow(Type x, int n)
Return xn.
Definition Math.h:586
float Sqrt(float x)
Return the square root of a floating-point value.
Definition Math.h:832
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:431
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition Math.h:478
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition Math.h:646
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition Math.h:1002
bool isFinite(const float x)
Return true if x is finite.
Definition Math.h:388
auto PrintCast(const T &val) -> typename std::enable_if<!std::is_same< T, int8_t >::value &&!std::is_same< T, uint8_t >::value, const T & >::type
8-bit integer values print to std::ostreams as characters. Cast them so that they print as integers i...
Definition Math.h:953
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:528
Type Pow4(Type x)
Return x4.
Definition Math.h:581
Type EuclideanRemainder(Type x)
Definition Math.h:900
Type Remainder(Type x, Type y)
Definition Math.h:852
Type Inv(Type x)
Return the inverse of x.
Definition Math.h:965
Type Exp(const Type &x)
Return ex.
Definition Math.h:781
bool isNegative(const Type &x)
Return true if x is less than zero.
Definition Math.h:380
bool isNan(const float x)
Return true if x is a NaN (Not-A-Number) value.
Definition Math.h:416
int64_t doubleToInt64(const double d)
Definition Math.h:514
Type Clamp01(Type x)
Return x clamped to [0, 1].
Definition Math.h:284
float Cos(const float &x)
Return cos x.
Definition Math.h:796
bool isUlpsEqual(const double aLeft, const double aRight, const int64_t aUnitsInLastPlace)
Definition Math.h:528
RandInt< int, std::mt19937 > RandomInt
Definition Math.h:266
float RoundDown(float x)
Return x rounded down to the nearest integer.
Definition Math.h:874
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition Math.h:724
Coord Abs(const Coord &xyz)
Definition Coord.h:518
int Mod(int x, int y)
Return the remainder of x / y.
Definition Math.h:848
Type Truncate(Type x, unsigned int digits)
Return x truncated to the given number of decimal digits.
Definition Math.h:941
T negative(const T &val)
Return the unary negation of the given value.
Definition Math.h:139
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition Math.h:468
float Round(float x)
Return x rounded to the nearest integer.
Definition Math.h:890
Type Pow3(Type x)
Return x3.
Definition Math.h:577
Type Clamp(Type x, Type min, Type max)
Return x clamped to [min, max].
Definition Math.h:274
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition Math.h:350
Type Chop(Type x, Type delta)
Return x if it is greater or equal in magnitude than delta. Otherwise, return zero.
Definition Math.h:935
Type FractionalPart(Type x)
Return the fractional part of x.
Definition Math.h:914
auto cwiseAdd(const Vec3H &v, const float s)
Definition Types.h:765
internal::half half
Definition HalfDecl.h:25
Axis
Definition Math.h:972
@ Z_AXIS
Definition Math.h:975
@ X_AXIS
Definition Math.h:973
@ Y_AXIS
Definition Math.h:974
bool cwiseGreaterThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
Definition Mat.h:1029
bool ClampTest01(Type &x)
Return true if x is outside [0,1].
Definition Math.h:290
float Sin(const float &x)
Return sin x.
Definition Math.h:787
int32_t floatToInt32(const float f)
Definition Math.h:504
Type Pow2(Type x)
Return x2.
Definition Math.h:573
RotationOrder
Definition Math.h:979
@ YXZ_ROTATION
Definition Math.h:982
@ ZXY_ROTATION
Definition Math.h:984
@ YZX_ROTATION
Definition Math.h:983
@ ZXZ_ROTATION
Definition Math.h:987
@ XZX_ROTATION
Definition Math.h:986
@ XYZ_ROTATION
Definition Math.h:980
@ ZYX_ROTATION
Definition Math.h:985
@ XZY_ROTATION
Definition Math.h:981
float Cbrt(float x)
Return the cube root of a floating-point value.
Definition Math.h:840
int Floor(float x)
Return the floor of x.
Definition Math.h:919
bool SignChange(const Type &a, const Type &b)
Return true if a and b have different signs.
Definition Math.h:814
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:824
Rand01< double, std::mt19937 > Random01
Definition Math.h:209
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition Math.h:71
constexpr bool is_arithmetic_v
Definition Math.h:85
Definition Exceptions.h:13
static double value()
Definition Math.h:170
static float value()
Definition Math.h:169
static math::half value()
Definition Math.h:168
Delta for small floating-point offsets.
Definition Math.h:167
static T value()
Definition Math.h:167
static double value()
Definition Math.h:162
static float value()
Definition Math.h:161
static math::half value()
Definition Math.h:160
Tolerance for floating-point comparison.
Definition Math.h:159
static T value()
Definition Math.h:159
#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:218