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 ""; }
90 inline std::string
operator+(
const std::string& s,
bool) {
return s; }
91 inline std::string
operator+(
const std::string& s,
int) {
return s; }
92 inline std::string
operator+(
const std::string& s,
float) {
return s; }
93 inline std::string
operator+(
const std::string& s,
double) {
return s; }
100 template<
typename T>
inline T
negative(
const T& val) {
return T(-val); }
101 template<>
inline bool negative(
const bool& val) {
return !val; }
103 template<>
inline std::string
negative(
const std::string& val) {
return val; }
119 return (
double)(rand() / (RAND_MAX + 1.0));
129 assert(min<max &&
"RandomInt: invalid arguments");
133 int operator() (
void)
const {
return rand() % my_range + my_min;}
134 int operator() (
int min,
int max)
const {
return rand() % (max-min+1) + min;}
141 template <
typename Type>
144 return x > min ? x < max ? x : max :
min;
148 template <
class Type>
150 return x > Type(0) ? x < Type(1) ? x : Type(1) : Type(0);
153 template <
class Type>
155 if (x>=Type(0) && x<=Type(1))
return false;
156 x = x< Type(0) ? Type(0) : Type(1);
161 template <
class Type>
164 const Type t = (x-
min)/(max-min);
165 return t > 0 ? t < 1 ? (3-2*t)*t*t : Type(1) : Type(0);
169 inline int32_t
Abs(int32_t i)
175 inline int64_t
Abs(int64_t i)
178 return (i < int64_t(0) ? -i : i);
191 inline double Abs(
double x)
197 inline long double Abs(
long double x)
203 inline uint32_t
Abs(uint32_t i)
209 inline uint64_t
Abs(uint64_t i)
216 template<
typename Type>
221 return x == zeroVal<Type>();
226 template<
typename Type>
230 const Type
tolerance = Type(zeroVal<Type>() + 1.0e-8);
231 return !(
Abs(a - b) > tolerance);
234 template<
typename Type>
238 return !(
Abs(a - b) > tolerance);
241 #define OPENVDB_EXACT_IS_APPROX_EQUAL(T) \
242 template<> inline bool isApproxEqual<T>(const T& a, const T& b) { return a == b; } \
243 template<> inline bool isApproxEqual<T>(const T& a, const T& b, const T&) { return a == b; } \
250 template<typename T0, typename T1>
260 template<
typename Type>
266 if (!(
Abs(a - b) > absTol))
return true;
273 relError =
Abs((a - b) / b);
275 relError =
Abs((a - b) / a);
277 return (relError <= relTol);
297 union FloatOrInt32 {
float floatValue; int32_t int32Value; };
298 const FloatOrInt32* foi =
reinterpret_cast<const FloatOrInt32*
>(&aFloatValue);
299 return foi->int32Value;
305 union DoubleOrInt64 {
double doubleValue; int64_t int64Value; };
306 const DoubleOrInt64* dol =
reinterpret_cast<const DoubleOrInt64*
>(&aDoubleValue);
307 return dol->int64Value;
316 isUlpsEqual(
const double aLeft,
const double aRight,
const int64_t aUnitsInLastPlace)
321 longLeft = INT64_C(0x8000000000000000) - longLeft;
327 longRight = INT64_C(0x8000000000000000) - longRight;
330 int64_t difference = labs(longLeft - longRight);
331 return (difference <= aUnitsInLastPlace);
335 isUlpsEqual(
const float aLeft,
const float aRight,
const int32_t aUnitsInLastPlace)
340 intLeft = 0x80000000 - intLeft;
346 intRight = 0x80000000 - intRight;
349 int32_t difference = abs(intLeft - intRight);
350 return (difference <= aUnitsInLastPlace);
356 template<
typename Type>
363 template<
typename Type>
370 template<
typename Type>
377 template <
typename Type>
385 for (
int i = 0; i < n; i++) ans *= x;
390 inline float Pow(
float b,
float e)
392 assert( b >= 0.0f &&
"Pow(float,float): base is negative" );
397 inline double Pow(
double b,
double e)
399 assert( b >= 0.0 &&
"Pow(double,double): base is negative" );
406 template<
typename Type >
407 inline const Type&
Max(
const Type& a,
const Type& b )
413 template<
typename Type >
414 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c )
420 template<
typename Type >
421 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d )
427 template<
typename Type >
428 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
429 const Type& d,
const Type& e )
435 template<
typename Type >
436 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
437 const Type& d,
const Type& e,
const Type& f )
443 template<
typename Type >
444 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
445 const Type& e,
const Type& f,
const Type& g )
451 template<
typename Type >
452 inline const Type&
Max(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
453 const Type& e,
const Type& f,
const Type& g,
const Type& h )
461 template<
typename Type >
462 inline const Type&
Min(
const Type& a,
const Type& b )
468 template<
typename Type >
469 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c )
475 template<
typename Type >
476 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d )
482 template<
typename Type >
483 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
484 const Type& d,
const Type& e )
490 template<
typename Type >
491 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
492 const Type& d,
const Type& e,
const Type& f )
498 template<
typename Type >
499 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
500 const Type& e,
const Type& f,
const Type& g )
506 template<
typename Type >
507 inline const Type&
Min(
const Type& a,
const Type& b,
const Type& c,
const Type& d,
508 const Type& e,
const Type& f,
const Type& g,
const Type& h )
514 template <
typename Type>
515 inline int Sign(
const Type &x) {
return ( (x)<0 ? -1 : (x)==0 ? 0 : 1);}
519 inline float Sqrt(
float x) {
return sqrtf(x);}
520 inline double Sqrt(
double x){
return sqrt(x);}
521 inline long double Sqrt(
long double x) {
return sqrtl(x);}
525 inline int Mod(
int i,
int j) {
return (i%j);};
526 inline float Mod(
float x,
float y) {
return fmodf(x,y);}
527 inline double Mod(
double x,
double y){
return fmod(x,y);}
528 inline long double Mod(
long double x,
long double y) {
return fmodl(x,y);}
531 template <
typename Type>
535 inline float RoundUp(
float x) {
return ceilf(x); }
536 inline double RoundUp(
double x) {
return ceil(x); }
537 inline long double RoundUp(
long double x) {
return ceill(x); }
538 template <
typename Type>
542 return reminder ? x-reminder+base : x;
549 inline long double RoundDown(
long double x) {
return floorl(x); }
550 template <
typename Type>
561 template <
typename Type>
568 template <
typename Type>
582 template <
typename Type>
586 template <
typename Type>
587 inline Type
Chop(Type x, Type delta) {
return (
Abs(x) < delta ? 0 : x); }
590 template <
typename Type>
593 Type tenth=
Pow(10,digits);
598 template <
typename Type>
625 template <
typename S,
typename T>
627 typedef typename boost::numeric::conversion_traits<S, T>::supertype
type;
639 #endif // OPENVDB_MATH_MATH_HAS_BEEN_INCLUDED