1#ifndef SB_MATH_HELPER_H
2#define SB_MATH_HELPER_H
28#include <Inventor/SbBase.h>
31#include <Inventor/STL/limits>
32#include <Inventor/STL/complex>
52 template <
typename T>
inline T
Max(T a, T b) {
return (a>b)?a:b; }
53 template <
typename T>
inline T
Min(T a, T b) {
return (a<b)?a:b; }
61 template <
typename T> T
shiftValue(T v,
int offset) {
return v + offset; }
68 template <
typename T>
inline T
Clamp(T a, T minV, T maxV) {
return Min(
Max(minV, a), maxV); }
75 if ( n & ((T(1)<<shift)-1) )
76 n = (n + (T(1)<<shift)) & ~((T(1)<<shift)-1);
94 template <
int N,
typename T>
100 return v + (N - (v%N));
106 template <
int N,
typename T>
122 return (a*
float(M_PI))/180.f;
130 return (a*180.f)/float(M_PI);
146 return floor(x + 0.5);
155 return _isnan(a) != 0;
157 return std::isnan(a);
169#if defined(_MSC_VER) && (_MSC_VER < 1800)
170 return value == value &&
171 value != std::numeric_limits<double>::infinity() &&
172 value != -std::numeric_limits<double>::infinity();
174 return std::isfinite( value );
180#if defined(_MSC_VER) && (_MSC_VER < 1800)
181 return value == value &&
182 value != std::numeric_limits<float>::infinity() &&
183 value != -std::numeric_limits<float>::infinity();
185 return std::isfinite( value );
206 template<
typename T >
209 return ( fabs( x - y ) <= tol );
213 template<
typename T>
inline T
abs(
const T& v ) { return ::abs(v); }
214 template<>
inline long int abs(
const long int& v ) { return ::labs(v); }
215 template<>
inline float abs(
const float& v ) {
return std::abs(v); }
216 template<>
inline double abs(
const double& v ) {
return std::abs(v); }
224 return ( x < y ) && !
isCoinc( x, y, tol );
233 return ( x > y ) && !
isCoinc( x, y, tol );
242 return ( x < y ) ||
isCoinc( x, y, tol );
251 return ( x > y ) ||
isCoinc( x, y, tol );
277 template <
typename T>
280 long double ABfabs(std::fabs((
long double)(A - B)));
281 if ( ABfabs < maxAbsoluteError )
283 long double Afabs( std::fabs((
long double) A) );
284 long double Bfabs( std::fabs((
long double) B) );
285 T relativeError = T( ABfabs / ( ( Bfabs > Afabs ) ? Bfabs : Afabs ) );
286 return ( relativeError <= maxRelativeError ) ;
293 template <
typename T>
296 return (T(0) < val) - (val < T(0));
300 template <
typename T>
inline T
rangeMax() {
return std::numeric_limits<T>::max(); }
304 template <
typename T>
inline T
rangeMin() {
return std::numeric_limits<T>::min(); }
310 template <
typename T>
inline T
fract(
const T& )
315 template <>
inline float fract(
const float& value)
318 return modff(value, &intpart);
320 template <>
inline double fract(
const double& value)
323 return modf(value, &intpart);
325 template <>
inline long double fract(
const long double& value)
328 return modfl(value, &intpart);
SbBool floatIsEqual(float A, float B, unsigned int numFloat)
Return true if A and B are equal.
static const int OIV_RAND_MAX
Maximum value returned by SbMathHelper::rand()
T nearestUpperMultipleOf(T v)
Return the nearest upper multiple of some value.
int sgn(const T &val)
Implements signum return value in { -1, 0, +1 }.
bool isGreaterThan(const T &x, const T &y, T tol=(T) OIV_DEF_MATH_HELPER_EPS)
Greater than test (x > y) using given tolerance.
T Clamp(T a, T minV, T maxV)
Clamps value to given range [minV, maxV].
bool isNaN(double a)
Returns true if the specified value is NaN.
bool isGreaterOrEqualThan(const T &x, const T &y, T tol=(T) OIV_DEF_MATH_HELPER_EPS)
Greater or equal than test (x >= y) using given tolerance.
bool checkRangeI(const T &x, const T &min, T max, T tol=(T) OIV_DEF_MATH_HELPER_EPS)
Inside closed interval (including endpoints) using given tolerance test.
bool isCoincRelativeOrAbsolute(const T &A, const T &B, T maxRelativeError, T maxAbsoluteError)
Relative or absolute error without sign comparison.
float rad2Deg(float a)
Convert radians to degrees.
void srand(unsigned seed)
Set seed for a new sequence of pseudo-random integers to be returned by rand()
static const float OIV_DEF_MATH_HELPER_EPS
Default epsilon value for coincidence and interval checking.
T alignToNextPowerOf2(T n, size_t shift)
Return an integer multiple of 2^shift greater or equals to n.
T rangeMax()
Returns the maximum finite value representable by the numeric type T.
int rand()
Returns a pseudo-random integer between 0 and OIV_RAND_MAX.
bool isFinite(double value)
Returns true if the value is a finite value (i.e.
T abs(const T &v)
Return absolute value of v.
bool isLessOrEqualThan(const T &x, const T &y, T tol=(T) OIV_DEF_MATH_HELPER_EPS)
Less or equal than test (x <= y) using given tolerance.
double rangeMin< double >()
int getNextLog2(int num)
Return the next log of 2 greater or equal to a.
double roundToNearestInt(double x)
Round the double value to the nearest integer value.
T nearestLowerMultipleOf(T v)
Return the nearest lower multiple of some value.
T fract(const T &)
Return fractional part of given value.
bool isLessThan(const T &x, const T &y, T tol=(T) OIV_DEF_MATH_HELPER_EPS)
Less than test (x < y) using given tolerance.
float rangeMin< float >()
T rangeMin()
Returns the lowest finite value representable by the numeric type T, that is, a finite value x such t...
T getNextPow2(T a)
Return the next power of 2 greater or equal to a.
bool isCoinc(const T &x, const T &y, T tol=(T) OIV_DEF_MATH_HELPER_EPS)
Coincidence test using given tolerance.
float deg2Rad(float a)
Convert degrees to radians.
bool checkRangeE(const T &x, const T &min, T max, T tol=(T) OIV_DEF_MATH_HELPER_EPS)
Inside open interval (excluding endpoints) using given tolerance test.
T shiftValue(T v, int offset)
shift the value of offset representable values considering value type.