35 #ifndef OPENVDB_MATH_HAS_BEEN_INCLUDED
36 #define OPENVDB_MATH_HAS_BEEN_INCLUDED
45 #include <boost/numeric/conversion/conversion_traits.hpp>
46 #include <openvdb/Platform.h>
47 #include <openvdb/version.h>
53 #if defined(__INTEL_COMPILER)
54 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN \
55 _Pragma("warning (push)") \
56 _Pragma("warning (disable:1572)")
57 #define OPENVDB_NO_FP_EQUALITY_WARNING_END \
58 _Pragma("warning (pop)")
67 #define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
68 #define OPENVDB_NO_FP_EQUALITY_WARNING_END
79 template<
typename T>
inline T
zeroVal() {
return T(0); }
81 template<>
inline std::string zeroVal<std::string>() {
return ""; }
93 inline std::string
operator+(
const std::string& s,
bool) {
return s; }
94 inline std::string
operator+(
const std::string& s,
int) {
return s; }
95 inline std::string
operator+(
const std::string& s,
float) {
return s; }
96 inline std::string
operator+(
const std::string& s,
double) {
return s; }
103 template<
typename T>
inline T
negative(
const T& val) {
return T(-val); }
104 template<>
inline bool negative(
const bool& val) {
return !val; }
106 template<>
inline std::string
negative(
const std::string& val) {
return val; }
122 return (
double)(rand() / (RAND_MAX + 1.0));
132 assert(min<max &&
"RandomInt: invalid arguments");
136 int operator() (
void)
const {
return rand() % my_range + my_min;}
137 int operator() (
int min,
int max)
const {
return rand() % (max-min+1) + min;}
144 template <
typename Type>
147 return x > min ? x < max ? x : max :
min;
151 template <
class Type>
153 return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0);
156 template <
class Type>
158 if (x>=Type(0) && x<=Type(1))
return false;
159 x = x< Type(0) ? Type(0) : Type(1);
164 template <
class Type>
167 const Type t = (x-
min)/(max-min);
168 return t > 0 ? t < 1 ? (3-2*t)*t*t : Type(1) : Type(0);
172 inline int32_t
Abs(int32_t i)
178 inline int64_t
Abs(int64_t i)
181 return (i < int64_t(0) ? -i : i);
194 inline double Abs(
double x)
200 inline long double Abs(
long double x)
206 inline uint32_t
Abs(uint32_t i)
212 inline uint64_t
Abs(uint64_t i)
219 template<
typename Type>
224 return x == zeroVal<Type>();
228 template<
typename Type>
232 return x < zeroVal<Type>();
235 template<
typename Type>
239 const Type
tolerance = Type(zeroVal<Type>() + toleranceValue<Type>());
240 return !(
Abs(a - b) > tolerance);
243 template<
typename Type>
247 return !(
Abs(a - b) > tolerance);
250 #define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \
251 template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \
252 template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \
259 template<typename T0, typename T1>
269 template<
typename Type>
275 if (!(
Abs(a - b) > absTol))
return true;
282 relError =
Abs((a - b) / b);
284 relError =
Abs((a - b) / a);
286 return (relError <= relTol);
306 union FloatOrInt32 {
float floatValue; int32_t int32Value; };
307 const FloatOrInt32* foi =
reinterpret_cast<const FloatOrInt32*
>(&aFloatValue);
308 return foi->int32Value;
314 union DoubleOrInt64 {
double doubleValue; int64_t int64Value; };
315 const DoubleOrInt64* dol =
reinterpret_cast<const DoubleOrInt64*
>(&aDoubleValue);
316 return dol->int64Value;
325 isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
330 longLeft = INT64_C(0x8000000000000000) - longLeft;
336 longRight = INT64_C(0x8000000000000000) - longRight;
339 int64_t difference = labs(longLeft - longRight);
340 return (difference <= aUnitsInLastPlace);
344 isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
349 intLeft = 0x80000000 - intLeft;
355 intRight = 0x80000000 - intRight;
358 int32_t difference = abs(intLeft - intRight);
359 return (difference <= aUnitsInLastPlace);
365 template<
typename Type>
372 template<
typename Type>
379 template<
typename Type>
386 template <
typename Type>
394 for (
int i = 0; i < n; i++) ans *= x;
399 inline float Pow(
float b,
float e)
401 assert( b >= 0.0f &&
"Pow(float,float): base is negative" );
406 inline double Pow(
double b,
double e)
408 assert( b >= 0.0 &&
"Pow(double,double): base is negative" );
415 template<
typename Type >
416 inline const Type&
Max(
const Type& a,
const Type& b )
422 template<
typename Type >
423 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c )
429 template<
typename Type >
430 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d )
436 template<
typename Type >
437 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
438 const Type& d,
const Type& e )
444 template<
typename Type >
445 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
446 const Type& d,
const Type& e,
const Type& f )
452 template<
typename Type >
453 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
454 const Type& e,
const Type& f,
const Type& g )
460 template<
typename Type >
461 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
462 const Type& e,
const Type& f,
const Type& g,
const Type& h )
470 template<
typename Type >
471 inline const Type&
Min(
const Type& a,
const Type& b )
477 template<
typename Type >
478 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c )
484 template<
typename Type >
485 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d )
491 template<
typename Type >
492 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
493 const Type& d,
const Type& e )
499 template<
typename Type >
500 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
501 const Type& d,
const Type& e,
const Type& f )
507 template<
typename Type >
508 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
509 const Type& e,
const Type& f,
const Type& g )
515 template<
typename Type >
516 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
517 const Type& e,
const Type& f,
const Type& g,
const Type& h )
523 template <
typename Type>
524 inline int Sign(
const Type &x) {
return ( (x)<0 ? -1 : (x)==0 ? 0 : 1);}
528 inline float Sqrt(
float x) {
return sqrtf(x);}
529 inline double Sqrt(
double x){
return sqrt(x);}
530 inline long double Sqrt(
long double x) {
return sqrtl(x);}
534 inline int Mod(
int i,
int j) {
return (i%j);};
535 inline float Mod(
float x,
float y) {
return fmodf(x,y);}
536 inline double Mod(
double x,
double y){
return fmod(x,y);}
537 inline long double Mod(
long double x,
long double y) {
return fmodl(x,y);}
540 template <
typename Type>
544 inline float RoundUp(
float x) {
return ceilf(x); }
545 inline double RoundUp(
double x) {
return ceil(x); }
546 inline long double RoundUp(
long double x) {
return ceill(x); }
547 template <
typename Type>
551 return reminder ? x-reminder+base : x;
558 inline long double RoundDown(
long double x) {
return floorl(x); }
559 template <
typename Type>
570 template <
typename Type>
577 template <
typename Type>
591 template <
typename Type>
595 template <
typename Type>
596 inline Type
Chop(Type x, Type delta) {
return (
Abs(x) < delta ? 0 : x); }
599 template <
typename Type>
602 Type tenth=
Pow(10,digits);
607 template <
typename Type>
634 template <
typename S,
typename T>
636 typedef typename boost::numeric::conversion_traits<S, T>::supertype
type;
648 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED