Functions

gmtl::Math Namespace Reference

Functions

template<class T >
clamp (T number, T lo, T hi)
 clamp "number" to a range between lo and hi
template<class T >
bool quadraticFormula (T &r1, T &r2, const T &a, const T &b, const T &c)
 Uses the quadratic formula to compute the 2 roots of the given 2nd degree polynomial in the form of Ax^2 + Bx + C.
C Math Abstraction

template<typename T >
abs (T iValue)
float abs (float iValue)
double abs (double iValue)
int abs (int iValue)
long abs (long iValue)
template<typename T >
ceil (T fValue)
float ceil (float fValue)
double ceil (double fValue)
template<typename T >
floor (T fValue)
float floor (float fValue)
double floor (double fValue)
template<typename T >
int sign (T iValue)
template<typename T >
zeroClamp (T value, T eps=static_cast< T >(0))
 Clamps the given value down to zero if it is within epsilon of zero.
template<typename T >
aCos (T fValue)
float aCos (float fValue)
double aCos (double fValue)
template<typename T >
aSin (T fValue)
float aSin (float fValue)
double aSin (double fValue)
template<typename T >
aTan (T fValue)
double aTan (double fValue)
float aTan (float fValue)
template<typename T >
aTan2 (T fY, T fX)
float aTan2 (float fY, float fX)
double aTan2 (double fY, double fX)
template<typename T >
cos (T fValue)
float cos (float fValue)
double cos (double fValue)
template<typename T >
exp (T fValue)
float exp (float fValue)
double exp (double fValue)
template<typename T >
log (T fValue)
double log (double fValue)
float log (float fValue)
double pow (double fBase, double fExponent)
float pow (float fBase, float fExponent)
template<typename T >
sin (T fValue)
double sin (double fValue)
float sin (float fValue)
template<typename T >
tan (T fValue)
double tan (double fValue)
float tan (float fValue)
template<typename T >
sqr (T fValue)
template<typename T >
sqrt (T fValue)
double sqrt (double fValue)
float fastInvSqrt (float x)
 Fast inverse square root.
float fastInvSqrt2 (float x)
float fastInvSqrt3 (float x)
void seedRandom (unsigned int seed)
 Seeds the pseudorandom number generator with the given seed.
float unitRandom ()
 get a random number between 0 and 1
float rangeRandom (float x1, float x2)
 return a random number between x1 and x2 RETURNS: random number between x1 and x2
float deg2Rad (float fVal)
double deg2Rad (double fVal)
float rad2Deg (float fVal)
double rad2Deg (double fVal)
template<class T >
bool isEqual (const T &a, const T &b, const T &tolerance)
 Is almost equal? test for equality within some tolerance...
template<class T >
trunc (T val)
 cut off the digits after the decimal place
template<class T >
round (T p)
 round to nearest integer
template<class T >
Min (const T &x, const T &y)
 min returns the minimum of 2 values
template<class T >
Min (const T &x, const T &y, const T &z)
 min returns the minimum of 3 values
template<class T >
Min (const T &w, const T &x, const T &y, const T &z)
 min returns the minimum of 4 values
template<class T >
Max (const T &x, const T &y)
 max returns the maximum of 2 values
template<class T >
Max (const T &x, const T &y, const T &z)
 max returns the maximum of 3 values
template<class T >
Max (const T &w, const T &x, const T &y, const T &z)
 max returns the maximum of 4 values
template<class T >
factorial (T rhs)
 Compute the factorial.
Scalar type interpolation (for doubles, floats, etc...)

template<class T , typename U >
void lerp (T &result, const U &lerp, const T &a, const T &b)
 Linear Interpolation between number [a] and [b].

Variables

Mathematical constants

const float TWO_PI = 6.28318530717958647692f
const float PI = 3.14159265358979323846f
const float PI_OVER_2 = 1.57079632679489661923f
const float PI_OVER_4 = 0.78539816339744830962f

Function Documentation

template<typename T >
T gmtl::Math::abs ( iValue  )  [inline]

Definition at line 54 of file Math.h.

{
    return static_cast<T>( iValue >= static_cast<T>(0) ? iValue : -iValue );
}

float gmtl::Math::abs ( float  iValue  )  [inline]

Definition at line 59 of file Math.h.

{  return fabsf(iValue); }

int gmtl::Math::abs ( int  iValue  )  [inline]

Definition at line 63 of file Math.h.

{  return ::abs(iValue); }

long gmtl::Math::abs ( long  iValue  )  [inline]

Definition at line 65 of file Math.h.

{  return labs(iValue); }

double gmtl::Math::abs ( double  iValue  )  [inline]

Definition at line 61 of file Math.h.

{  return fabs(iValue); }

double gmtl::Math::aCos ( double  fValue  )  [inline]

Definition at line 159 of file Math.h.

{
    if ( -1.0 < fValue )
    {
        if ( fValue < 1.0 )
            return static_cast<double>(::acos(fValue));
        else
            return 0.0;
    }
    else
    {
        return static_cast<double>(gmtl::Math::PI);
    }
}

template<typename T >
T gmtl::Math::aCos ( fValue  )  [inline]
float gmtl::Math::aCos ( float  fValue  )  [inline]

Definition at line 139 of file Math.h.

{
    if ( -1.0f < fValue )
    {
        if ( fValue < 1.0f )
        {
#ifdef NO_ACOSF
            return static_cast<float>(::acos(fValue));
#else
            return static_cast<float>(::acosf(fValue));
#endif
        }
        else
            return 0.0f;
    }
    else
    {
        return static_cast<float>(gmtl::Math::PI);
    }
}

template<typename T >
T gmtl::Math::aSin ( fValue  )  [inline]
float gmtl::Math::aSin ( float  fValue  )  [inline]

Definition at line 176 of file Math.h.

{
    if ( -1.0f < fValue )
    {
        if ( fValue < 1.0f )
        {
#ifdef NO_ASINF
            return static_cast<float>(::asin(fValue));
#else
            return static_cast<float>(::asinf(fValue));
#endif
        }
        else
            return static_cast<float>(-gmtl::Math::PI_OVER_2);
    }
    else
    {
        return static_cast<float>(gmtl::Math::PI_OVER_2);
    }
}

double gmtl::Math::aSin ( double  fValue  )  [inline]

Definition at line 196 of file Math.h.

{
    if ( -1.0 < fValue )
    {
        if ( fValue < 1.0 )
            return static_cast<double>(::asin(fValue));
        else
            return static_cast<double>(-gmtl::Math::PI_OVER_2);
    }
    else
    {
        return static_cast<double>(gmtl::Math::PI_OVER_2);
    }
}

template<typename T >
T gmtl::Math::aTan ( fValue  )  [inline]
double gmtl::Math::aTan ( double  fValue  )  [inline]

Definition at line 213 of file Math.h.

{
    return ::atan( fValue );
}

float gmtl::Math::aTan ( float  fValue  )  [inline]

Definition at line 217 of file Math.h.

{
#ifdef NO_TANF
   return static_cast<float>(::atan(fValue));
#else
   return static_cast<float>(::atanf(fValue));
#endif
}

template<typename T >
T gmtl::Math::aTan2 ( fY,
fX 
) [inline]
float gmtl::Math::aTan2 ( float  fY,
float  fX 
) [inline]

Definition at line 228 of file Math.h.

{
#ifdef NO_ATAN2F
   return static_cast<float>(::atan2(fY, fX));
#else
   return static_cast<float>(::atan2f(fY, fX));
#endif
}

double gmtl::Math::aTan2 ( double  fY,
double  fX 
) [inline]

Definition at line 236 of file Math.h.

{
    return static_cast<double>(::atan2(fY, fX));
}

double gmtl::Math::ceil ( double  fValue  )  [inline]

Definition at line 79 of file Math.h.

{
    return double( ::ceil( fValue ) );
}

template<typename T >
T gmtl::Math::ceil ( fValue  )  [inline]
float gmtl::Math::ceil ( float  fValue  )  [inline]

Definition at line 71 of file Math.h.

{
#ifdef NO_CEILF
   return float(::ceil(fValue));
#else
   return float( ::ceilf( fValue ) );
#endif
}

template<class T >
T gmtl::Math::clamp ( number,
lo,
hi 
) [inline]

clamp "number" to a range between lo and hi

Definition at line 570 of file Math.h.

{
   if (number > hi) number = hi;
   else if (number < lo) number = lo;
   return number;
}

template<typename T >
T gmtl::Math::cos ( fValue  )  [inline]
float gmtl::Math::cos ( float  fValue  )  [inline]

Definition at line 243 of file Math.h.

{
#ifdef NO_COSF
   return static_cast<float>(::cos(fValue));
#else
   return static_cast<float>(::cosf(fValue));
#endif
}

double gmtl::Math::cos ( double  fValue  )  [inline]

Definition at line 251 of file Math.h.

{
    return static_cast<double>(::cos(fValue));
}

float gmtl::Math::deg2Rad ( float  fVal  )  [inline]

Definition at line 464 of file Math.h.

{
   return static_cast<float>(fVal * static_cast<float>(gmtl::Math::PI / 180.0));
}

double gmtl::Math::deg2Rad ( double  fVal  )  [inline]

Definition at line 468 of file Math.h.

{
   return static_cast<double>(fVal * static_cast<double>(gmtl::Math::PI / 180.0));
}

template<typename T >
T gmtl::Math::exp ( fValue  )  [inline]
float gmtl::Math::exp ( float  fValue  )  [inline]

Definition at line 258 of file Math.h.

{
#ifdef NO_EXPF
   return static_cast<float>(::exp(fValue));
#else
   return static_cast<float>(::expf(fValue));
#endif
}

double gmtl::Math::exp ( double  fValue  )  [inline]

Definition at line 266 of file Math.h.

{
    return static_cast<double>(::exp(fValue));
}

template<class T >
T gmtl::Math::factorial ( rhs  )  [inline]

Compute the factorial.

give - an object who's type has operator++, operator=, operator<=, and operator*= defined. it should be a single valued scalar type such as an int, float, double etc.... NOTE: This could be faster with a lookup table, but then wouldn't work templated : kevin

Definition at line 553 of file Math.h.

{
   T lhs = static_cast<T>(1);

   for( T x = static_cast<T>(1); x <= rhs; ++x )
   {
      lhs *= x;
   }

   return lhs;
}

float gmtl::Math::fastInvSqrt ( float  x  )  [inline]

Fast inverse square root.

Definition at line 351 of file Math.h.

{
   GMTL_STATIC_ASSERT(sizeof(float) == sizeof(int),
                      Union_type_sizes_do_not_match);

   // Use an approach to data type reinterpretation that is safe with GCC
   // strict aliasing enabled. This is called type-punning, and it is valid
   // when done with a union where the value read (int_value) is different
   // than the one most recently written to (float_value).
   union
   {
      float float_value;
      int   int_value;
   } data;

   const float xhalf(0.5f*x);
   data.float_value = x;
   // This hides a good amount of math
   data.int_value = 0x5f3759df - (data.int_value >> 1);
   x = data.float_value;
   x = x*(1.5f - xhalf*x*x);   // Repeat for more accuracy
   return x;
}

float gmtl::Math::fastInvSqrt2 ( float  x  )  [inline]

Definition at line 375 of file Math.h.

{
   GMTL_STATIC_ASSERT(sizeof(float) == sizeof(int),
                      Union_type_sizes_do_not_match);

   // Use an approach to data type reinterpretation that is safe with GCC
   // strict aliasing enabled. This is called type-punning, and it is valid
   // when done with a union where the value read (int_value) is different
   // than the one most recently written to (float_value).
   union
   {
      float float_value;
      int   int_value;
   } data;

   const float xhalf(0.5f*x);
   data.float_value = x;
   // This hides a good amount of math
   data.int_value = 0x5f3759df - (data.int_value >> 1);
   x = data.float_value;
   x = x*(1.5f - xhalf*x*x);   // Repeat for more accuracy
   x = x*(1.5f - xhalf*x*x);
   return x;
}

float gmtl::Math::fastInvSqrt3 ( float  x  )  [inline]

Definition at line 400 of file Math.h.

{
   GMTL_STATIC_ASSERT(sizeof(float) == sizeof(int),
                      Union_type_sizes_do_not_match);

   // Use an approach to data type reinterpretation that is safe with GCC
   // strict aliasing enabled. This is called type-punning, and it is valid
   // when done with a union where the value read (int_value) is different
   // than the one most recently written to (float_value).
   union
   {
      float float_value;
      int   int_value;
   } data;

   const float xhalf(0.5f*x);
   data.float_value = x;
   // This hides a good amount of math
   data.int_value = 0x5f3759df - (data.int_value >> 1);
   x = data.float_value;
   x = x*(1.5f - xhalf*x*x);   // Repeat for more accuracy
   x = x*(1.5f - xhalf*x*x);
   x = x*(1.5f - xhalf*x*x);
   return x;
}

template<typename T >
T gmtl::Math::floor ( fValue  )  [inline]
float gmtl::Math::floor ( float  fValue  )  [inline]

Definition at line 86 of file Math.h.

{
#ifdef NO_FLOORF
   return float(::floor(fValue));
#else
   return float( ::floorf( fValue ) );
#endif
}

double gmtl::Math::floor ( double  fValue  )  [inline]

Definition at line 94 of file Math.h.

{
    return double( ::floor( fValue ) );
}

template<class T >
bool gmtl::Math::isEqual ( const T &  a,
const T &  b,
const T &  tolerance 
) [inline]

Is almost equal? test for equality within some tolerance...

: tolerance must be >= 0

Definition at line 488 of file Math.h.

{
   gmtlASSERT(tolerance >= static_cast<T>(0));
   return gmtl::Math::abs( a - b ) <= tolerance;
}

template<class T , typename U >
void gmtl::Math::lerp ( T &  result,
const U &  lerp,
const T &  a,
const T &  b 
) [inline]

Linear Interpolation between number [a] and [b].

lerp=0.0 returns a, lerp=1.0 returns b

Precondition:
use double or float only...

Definition at line 587 of file Math.h.

{
    T size = b - a;
    result = static_cast<U>(a) + (static_cast<U>(size) * lerp);
}

double gmtl::Math::log ( double  fValue  )  [inline]

Definition at line 273 of file Math.h.

{
    return static_cast<double>(::log(fValue));
}

float gmtl::Math::log ( float  fValue  )  [inline]

Definition at line 277 of file Math.h.

{
#ifdef NO_LOGF
   return static_cast<float>(::log(fValue));
#else
   return static_cast<float>(::logf(fValue));
#endif
}

template<typename T >
T gmtl::Math::log ( fValue  )  [inline]
template<class T >
T gmtl::Math::Max ( const T &  w,
const T &  x,
const T &  y,
const T &  z 
) [inline]

max returns the maximum of 4 values

Definition at line 542 of file Math.h.

{
   return gmtl::Math::Max( gmtl::Math::Max( w, x ), gmtl::Math::Max( y, z ) );
}

template<class T >
T gmtl::Math::Max ( const T &  x,
const T &  y,
const T &  z 
) [inline]

max returns the maximum of 3 values

Definition at line 536 of file Math.h.

{
   return Max( gmtl::Math::Max( x, y ), z );
}

template<class T >
T gmtl::Math::Max ( const T &  x,
const T &  y 
) [inline]

max returns the maximum of 2 values

Definition at line 530 of file Math.h.

{
   return ( x > y ) ? x : y;
}

template<class T >
T gmtl::Math::Min ( const T &  x,
const T &  y,
const T &  z 
) [inline]

min returns the minimum of 3 values

Definition at line 517 of file Math.h.

{
   return Min( gmtl::Math::Min( x, y ), z );
}

template<class T >
T gmtl::Math::Min ( const T &  w,
const T &  x,
const T &  y,
const T &  z 
) [inline]

min returns the minimum of 4 values

Definition at line 523 of file Math.h.

{
   return gmtl::Math::Min( gmtl::Math::Min( w, x ), gmtl::Math::Min( y, z ) );
}

template<class T >
T gmtl::Math::Min ( const T &  x,
const T &  y 
) [inline]

min returns the minimum of 2 values

Definition at line 511 of file Math.h.

{
   return ( x > y ) ? y : x;
}

double gmtl::Math::pow ( double  fBase,
double  fExponent 
) [inline]

Definition at line 286 of file Math.h.

{
    return static_cast<double>(::pow(fBase, fExponent));
}

float gmtl::Math::pow ( float  fBase,
float  fExponent 
) [inline]

Definition at line 290 of file Math.h.

{
#ifdef NO_POWF
   return static_cast<float>(::pow(fBase, fExponent));
#else
   return static_cast<float>(::powf(fBase, fExponent));
#endif
}

template<class T >
bool gmtl::Math::quadraticFormula ( T &  r1,
T &  r2,
const T &  a,
const T &  b,
const T &  c 
) [inline]

Uses the quadratic formula to compute the 2 roots of the given 2nd degree polynomial in the form of Ax^2 + Bx + C.

Parameters:
r1 set to the first root
r2 set to the second root
a the coefficient to x^2
b the coefficient to x^1
c the coefficient to x^0
Returns:
true if both r1 and r2 are real; false otherwise

Definition at line 607 of file Math.h.

{
   const T q = b * b - static_cast<T>(4) * a * c;

   // the result has real roots
   if (q >= 0)
   {
      const T sq = gmtl::Math::sqrt(q);
      const T d = static_cast<T>(1) / (static_cast<T>(2) * a);
      r1 = (-b + sq) * d;
      r2 = (-b - sq) * d;
      return true;
   }
   // the result has complex roots
   else
   {
      return false;
   }
}

float gmtl::Math::rad2Deg ( float  fVal  )  [inline]

Definition at line 473 of file Math.h.

{
   return static_cast<float>(fVal * static_cast<float>(180.0 / gmtl::Math::PI));
}

double gmtl::Math::rad2Deg ( double  fVal  )  [inline]

Definition at line 477 of file Math.h.

{
   return static_cast<float>(fVal * static_cast<double>(180.0 / gmtl::Math::PI));
}

float gmtl::Math::rangeRandom ( float  x1,
float  x2 
) [inline]

return a random number between x1 and x2 RETURNS: random number between x1 and x2

Definition at line 449 of file Math.h.

{
   float r = gmtl::Math::unitRandom();
   float size = x2 - x1;
   return static_cast<float>(r * size + x1);
}

template<class T >
T gmtl::Math::round ( p  )  [inline]

round to nearest integer

Definition at line 504 of file Math.h.

{
   return static_cast<T>(gmtl::Math::floor(p + static_cast<T>(0.5)));
}

void gmtl::Math::seedRandom ( unsigned int  seed  )  [inline]

Seeds the pseudorandom number generator with the given seed.

Parameters:
seed the seed for the pseudorandom number generator.

Definition at line 433 of file Math.h.

{
   ::srand(seed);
}

template<typename T >
int gmtl::Math::sign ( iValue  )  [inline]

Definition at line 100 of file Math.h.

{
   if (iValue > static_cast<T>(0))
   {
      return 1;
   }
   else
   {
      if (iValue < static_cast<T>(0))
      {
         return -1;
      }
      else
      {
         return 0;
      }
   }
}

float gmtl::Math::sin ( float  fValue  )  [inline]

Definition at line 305 of file Math.h.

{
#ifdef NO_SINF
   return static_cast<float>(::sin(fValue));
#else
   return static_cast<float>(::sinf(fValue));
#endif
}

double gmtl::Math::sin ( double  fValue  )  [inline]

Definition at line 301 of file Math.h.

{
    return static_cast<double>(::sin(fValue));
}

template<typename T >
T gmtl::Math::sin ( fValue  )  [inline]
template<typename T >
T gmtl::Math::sqr ( fValue  )  [inline]

Definition at line 330 of file Math.h.

{
    return static_cast<T>(fValue * fValue);
}

template<typename T >
T gmtl::Math::sqrt ( fValue  )  [inline]

Definition at line 336 of file Math.h.

{
#ifdef NO_SQRTF
   return static_cast<T>(::sqrt((static_cast<float>(fValue))));
#else
   return static_cast<T>(::sqrtf((static_cast<float>(fValue))));
#endif
}

double gmtl::Math::sqrt ( double  fValue  )  [inline]

Definition at line 344 of file Math.h.

{
    return static_cast<double>(::sqrt(fValue));
}

float gmtl::Math::tan ( float  fValue  )  [inline]

Definition at line 320 of file Math.h.

{
#ifdef NO_TANF
   return static_cast<float>(::tan(fValue));
#else
   return static_cast<float>(::tanf(fValue));
#endif
}

template<typename T >
T gmtl::Math::tan ( fValue  )  [inline]
double gmtl::Math::tan ( double  fValue  )  [inline]

Definition at line 316 of file Math.h.

{
    return static_cast<double>(::tan(fValue));
}

template<class T >
T gmtl::Math::trunc ( val  )  [inline]

cut off the digits after the decimal place

Definition at line 496 of file Math.h.

{
   return static_cast<T>((val < static_cast<T>(0)) ?
                         gmtl::Math::ceil(val)     :
                         gmtl::Math::floor(val));
}

float gmtl::Math::unitRandom (  )  [inline]

get a random number between 0 and 1

Postcondition:
returns number between 0 and 1

Definition at line 441 of file Math.h.

{
   return static_cast<float>(::rand()) / static_cast<float>(RAND_MAX);
}

template<typename T >
T gmtl::Math::zeroClamp ( value,
eps = static_cast<T>(0) 
) [inline]

Clamps the given value down to zero if it is within epsilon of zero.

Parameters:
value the value to clamp
eps the epsilon tolerance or zero by default
Returns:
zero if the value is close to 0, the value otherwise

Definition at line 128 of file Math.h.

{
   return ( (gmtl::Math::abs(value) <= eps) ? static_cast<T>(0) : value );
}


Variable Documentation

const float gmtl::Math::PI = 3.14159265358979323846f

Definition at line 43 of file Math.h.

const float gmtl::Math::PI_OVER_2 = 1.57079632679489661923f

Definition at line 44 of file Math.h.

const float gmtl::Math::PI_OVER_4 = 0.78539816339744830962f

Definition at line 45 of file Math.h.

const float gmtl::Math::TWO_PI = 6.28318530717958647692f

Definition at line 42 of file Math.h.