source file REDMath.h

Go to the documentation of this file.

#ifndef RED_MATH_H
#define RED_MATH_H

//************************************************************************************************************
// Copyright (c) 2005 Redway3D. All rights reserved. Redway3D is a registered trademark. Any reproduction and /
// or utilization of any material from this document without Redway3D?s express authorization are prohibited.
//************************************************************************************************************

#include <math.h>

#include "RED.h"

namespace RED
{
  class Object;
};

//******************************************************************************
//*** Miscellaneous ************************************************************
//******************************************************************************

template <typename T>
inline const T& REDMin( const T& a, const T& b)
{ 
  return ( a < b ) ? a : b;
}


template <typename T>
inline const T& REDMax( const T& a, const T& b )
{ 
  return ( a < b ) ? b : a;
}


template <typename T>
inline const T& REDClamp( const T& a, const T& min, const T& max )
{ 
  return ( a < min ) ? min : ( ( a > max ) ? max : a );
}


inline double REDLerp( double t, double a, double b )
{ 
  return( ( 1.0 - t ) * a + t * b );
}


//******************************************************************************
//*** Half precision floating point values *************************************
//******************************************************************************

#define RED_HALF_ONE           ( 0x3c00 )
#define RED_HALF_MINUS_ONE     ( 0xbc00 )


extern "C" REDExp float REDHalfToFloat( unsigned short iHalfFloat );


extern "C" REDExp unsigned short REDFloatToHalf( float iFloat );


inline unsigned short REDDoubleToHalf( double iDouble )
{
  float fvalue = (float)iDouble;
  return REDFloatToHalf( fvalue );
}


inline double REDLog2( double iValue )
{
  return log( iValue ) / log( 2.0 );
}


inline double REDFract( double iValue )
{
  return iValue - floor( iValue );
}


inline int REDPositiveMod( int iValue, 
                           int iModulo )
{
  return ( iValue % iModulo + iModulo ) % iModulo;
}


inline int REDFastFloor( double iValue )
{
  return iValue < 0.0 ? (int)iValue - 1 : (int)iValue;
}


inline double REDMISPower( double pdf_a, double pdf_b )
{
  pdf_a *= pdf_a;
  pdf_b *= pdf_b;
  
  if( ( pdf_a + pdf_b ) > 0.0 )
  {
    return pdf_a / ( pdf_a + pdf_b );
  }
  else
  {
    return 0.0;
  }
}


#endif