home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / math / Math.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  8KB  |  274 lines

  1.  
  2. #include "../precompiled.h"
  3. #pragma hdrstop
  4.  
  5.  
  6. const float    idMath::PI                = 3.14159265358979323846f;
  7. const float    idMath::TWO_PI            = 2.0f * PI;
  8. const float    idMath::HALF_PI            = 0.5f * PI;
  9. const float    idMath::ONEFOURTH_PI    = 0.25f * PI;
  10. const float idMath::E                = 2.71828182845904523536f;
  11. const float idMath::SQRT_TWO        = 1.41421356237309504880f;
  12. const float idMath::SQRT_THREE        = 1.73205080756887729352f;
  13. // RAVEN BEGIN
  14. const float    idMath::THREEFOURTHS_PI    = 0.75f * PI;
  15. // RAVEN END
  16. const float    idMath::SQRT_1OVER2        = 0.70710678118654752440f;
  17. const float    idMath::SQRT_1OVER3        = 0.57735026918962576450f;
  18. const float    idMath::M_DEG2RAD        = PI / 180.0f;
  19. const float    idMath::M_RAD2DEG        = 180.0f / PI;
  20. const float    idMath::M_SEC2MS        = 1000.0f;
  21. const float    idMath::M_MS2SEC        = 0.001f;
  22. const float    idMath::INFINITY        = 1e30f;
  23. // RAVEN BEGIN
  24. // jscott: renamed to prevent name clash
  25. const float idMath::FLOAT_EPSILON    = 1.192092896e-07f;
  26. // ddynerman: added, from limits.h
  27. const int idMath::INT_MIN            = (-2147483647 - 1);
  28. const int idMath::INT_MAX            = 2147483647;
  29. // RAVEN END
  30.  
  31. bool        idMath::initialized        = false;
  32. #ifdef _FAST_MATH
  33. dword        idMath::iSqrt[SQRT_TABLE_SIZE];        // inverse square root lookup table
  34. #endif
  35.  
  36. #ifdef ID_WIN_X86_SSE
  37. const float idMath::SSE_FLOAT_ZERO    = 0.0f;
  38. const float idMath::SSE_FLOAT_255    = 255.0f;
  39. #endif
  40.  
  41. /*
  42. ===============
  43. idMath::Init
  44. ===============
  45. */
  46. void idMath::Init( void ) {
  47. #ifdef _FAST_MATH
  48.     union _flint fi, fo;
  49.  
  50.     for ( int i = 0; i < SQRT_TABLE_SIZE; i++ ) {
  51.         fi.i     = ((EXP_BIAS-1) << EXP_POS) | (i << LOOKUP_POS);
  52.         fo.f     = (float)( 1.0 / sqrt( fi.f ) );
  53.         iSqrt[i] = ((dword)(((fo.i + (1<<(SEED_POS-2))) >> SEED_POS) & 0xFF))<<SEED_POS;
  54.     }
  55.     
  56.     iSqrt[SQRT_TABLE_SIZE / 2] = ((dword)(0xFF))<<(SEED_POS); 
  57. #endif
  58.  
  59.     initialized = true;
  60. }
  61.  
  62. /*
  63. ================
  64. idMath::FloatToBits
  65. ================
  66. */
  67. int idMath::FloatToBits( float f, int exponentBits, int mantissaBits ) {
  68.     int i, sign, exponent, mantissa, value;
  69.  
  70.     assert( exponentBits >= 2 && exponentBits <= 8 );
  71.     assert( mantissaBits >= 2 && mantissaBits <= 23 );
  72.  
  73.     int maxBits = ( ( ( 1 << ( exponentBits - 1 ) ) - 1 ) << mantissaBits ) | ( ( 1 << mantissaBits ) - 1 );
  74.     int minBits = ( ( ( 1 <<   exponentBits       ) - 2 ) << mantissaBits ) | 1;
  75.  
  76.     float max = BitsToFloat( maxBits, exponentBits, mantissaBits );
  77.     float min = BitsToFloat( minBits, exponentBits, mantissaBits );
  78.  
  79.     if ( f >= 0.0f ) {
  80.         if ( f >= max ) {
  81.             return maxBits;
  82.         } else if ( f <= min ) {
  83.             return minBits;
  84.         }
  85.     } else {
  86.         if ( f <= -max ) {
  87.             return ( maxBits | ( 1 << ( exponentBits + mantissaBits ) ) );
  88.         } else if ( f >= -min ) {
  89.             return ( minBits | ( 1 << ( exponentBits + mantissaBits ) ) );
  90.         }
  91.     }
  92.  
  93.     exponentBits--;
  94.     i = *reinterpret_cast<int *>(&f);
  95.     sign = ( i >> IEEE_FLT_SIGN_BIT ) & 1;
  96.     exponent = ( ( i >> IEEE_FLT_MANTISSA_BITS ) & ( ( 1 << IEEE_FLT_EXPONENT_BITS ) - 1 ) ) - IEEE_FLT_EXPONENT_BIAS;
  97.     mantissa = i & ( ( 1 << IEEE_FLT_MANTISSA_BITS ) - 1 );
  98.     value = sign << ( 1 + exponentBits + mantissaBits );
  99.     value |= ( ( INTSIGNBITSET( exponent ) << exponentBits ) | ( abs( exponent ) & ( ( 1 << exponentBits ) - 1 ) ) ) << mantissaBits;
  100.     value |= mantissa >> ( IEEE_FLT_MANTISSA_BITS - mantissaBits );
  101.     return value;
  102. }
  103.  
  104. /*
  105. ================
  106. idMath::BitsToFloat
  107. ================
  108. */
  109. float idMath::BitsToFloat( int i, int exponentBits, int mantissaBits ) {
  110.     static int exponentSign[2] = { 1, -1 };
  111.     int sign, exponent, mantissa, value;
  112.     assert( exponentBits >= 2 && exponentBits <= 8 );
  113.     assert( mantissaBits >= 2 && mantissaBits <= 23 );
  114.  
  115.     exponentBits--;
  116.     sign = i >> ( 1 + exponentBits + mantissaBits );
  117.     exponent = ( ( i >> mantissaBits ) & ( ( 1 << exponentBits ) - 1 ) ) * exponentSign[( i >> ( exponentBits + mantissaBits ) ) & 1];
  118.     mantissa = ( i & ( ( 1 << mantissaBits ) - 1 ) ) << ( IEEE_FLT_MANTISSA_BITS - mantissaBits );
  119.     value = sign << IEEE_FLT_SIGN_BIT | ( exponent + IEEE_FLT_EXPONENT_BIAS ) << IEEE_FLT_MANTISSA_BITS | mantissa;
  120.     return *reinterpret_cast<float *>(&value);
  121. }
  122.  
  123. // RAVEN BEGIN
  124. // bdube: added block
  125.  
  126. void idMath::ArtesianFromPolar( idVec3 &result, idVec3 view )
  127. {
  128.     float    s1, c1, s2, c2;
  129.  
  130.     idMath::SinCos( view[1], s1, c1 );
  131.     idMath::SinCos( view[2], s2, c2 );
  132.  
  133.     result[0] = c1 * s2 * view[0];
  134.     result[1] = s1 * s2 * view[0];
  135.     result[2] = c2 * view[0];
  136. }
  137.  
  138. void idMath::PolarFromArtesian( idVec3 &view, idVec3 artesian )
  139. {
  140.     float    length;
  141.     view[0] = artesian.Length();
  142.     view[1] = idMath::ATan( artesian[1], artesian[0] );
  143.     length = sqrtf( ( artesian[0] * artesian[0] ) + ( artesian[1] * artesian[1] ) );
  144.     view[2] = idMath::ATan( length, artesian[2] );
  145. }
  146.  
  147. // ================================================================================================
  148. // jscott: fast and reliable random routines
  149. // ================================================================================================
  150.  
  151. unsigned long rvRandom::mSeed;
  152.  
  153. float rvRandom::flrand( float min, float max )
  154. {
  155.     float    result;
  156.  
  157.     mSeed = ( mSeed * 214013L ) + 2531011;
  158.     // Note: the shift and divide cannot be combined as this breaks the routine
  159.     result = ( float )( mSeed >> 17 );                        // 0 - 32767 range
  160.     result = ( ( result * ( max - min ) ) * ( 1.0f / 32768.0f ) ) + min;
  161.     return( result );
  162. }
  163.  
  164. float rvRandom::flrand() {
  165.     return flrand( 0.0f, 1.0f );
  166. }
  167.  
  168. float rvRandom::flrand( const idVec2& v ) {
  169.     return flrand( v[0], v[1] );
  170. }
  171.  
  172. int rvRandom::irand( int min, int max )
  173. {
  174.     int        result;
  175.  
  176.     max++;
  177.     mSeed = ( mSeed * 214013L ) + 2531011;
  178.     result = mSeed >> 17;
  179.     result = ( ( result * ( max - min ) ) >> 15 ) + min;
  180.     return( result );
  181. }
  182.  
  183. // Try to get a seed independent of the random number system
  184.  
  185. int rvRandom::Init( void )
  186. {
  187.     mSeed *= ( unsigned long )sys->Milliseconds();
  188.  
  189.     return( mSeed );
  190. }
  191.  
  192. // ================================================================================================
  193. // Barycentric texture coordinate functions
  194. // Get the *SIGNED* area of a triangle required for barycentric
  195. // ================================================================================================
  196. float idMath::BarycentricTriangleArea( const idVec3 &normal, const idVec3 &a, const idVec3 &b, const idVec3 &c ) 
  197. {
  198.     idVec3    v1, v2;
  199.     idVec3    cross;
  200.     float    area;
  201.  
  202.     v1 = b - a;
  203.     v2 = c - a;
  204.     cross = v1.Cross( v2 );
  205.     area = 0.5f * DotProduct( cross, normal );
  206.  
  207.     return( area );
  208. }
  209.  
  210. void idMath::BarycentricEvaluate( idVec2 &result, const idVec3 &point, const idVec3 &normal, const float area, const idVec3 t[3], const idVec2 tc[3] )
  211. {
  212.     float    b1, b2, b3;
  213.  
  214.     b1 = idMath::BarycentricTriangleArea( normal, point, t[1], t[2] ) / area;
  215.     b2 = idMath::BarycentricTriangleArea( normal, t[0], point, t[2] ) / area;
  216.     b3 = idMath::BarycentricTriangleArea( normal, t[0], t[1], point ) / area;
  217.  
  218.     result[0] = ( b1 * tc[0][0] ) + ( b2 * tc[1][0] ) + ( b3 * tc[2][0] );
  219.     result[1] = ( b1 * tc[0][1] ) + ( b2 * tc[1][1] ) + ( b3 * tc[2][1] );
  220. }
  221.  
  222. // abahr:
  223. float idMath::Lerp( const idVec2& range, float frac ) {
  224.     return Lerp( range[0], range[1], frac );
  225. }
  226.  
  227. // abahr:
  228. float idMath::Lerp( float start, float end, float frac ) {
  229.     if( frac >= 1.0f ) {
  230.         return end;
  231.     }
  232.  
  233.     if( frac <= 0.0f ) {
  234.         return start;
  235.     }
  236.  
  237.     return start + (end - start) * frac;
  238. }
  239.  
  240. // abahr:
  241. float idMath::MidPointLerp( float start, float mid, float end, float frac ) {
  242.     if( frac < 0.5f ) {
  243.         return Lerp( start, mid, 2.0f * frac );
  244.     }
  245.  
  246.     return Lerp( mid, end, 2.0f * (frac - 0.5f) );
  247. }
  248.  
  249. float idMath::dBToScale( float db ) {
  250.  
  251.     if( db < -60.0f ) {
  252.  
  253.         return( 0.0f );
  254.  
  255.     } else {
  256.  
  257.         return( powf( 2.0f, db * ( 1.0f / 6.0f ) ) );
  258.     }
  259. }
  260.  
  261. float idMath::ScaleToDb( float scale ) {
  262.  
  263.     if( scale <= 0.0f ) {
  264.  
  265.         return( -60.0f );
  266.  
  267.     } else {
  268.  
  269.         return( 6.0f * idMath::Log( scale ) / idMath::Log( 2 ) );
  270.     }
  271. }
  272.  
  273. // RAVEN END
  274.