glmath.h

00001 /*
00002 Copyright (c) 2000-2007 Lee Thomason (www.grinninglizard.com)
00003 Grinning Lizard Utilities.
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any 
00010 purpose, including commercial applications, and to alter it and 
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must 
00014 not claim that you wrote the original software. If you use this 
00015 software in a product, an acknowledgment in the product documentation 
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and 
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source 
00022 distribution.
00023 */
00024 
00025 
00026 #ifndef GRINLIZ_MATH_INCLUDED
00027 #define GRINLIZ_MATH_INCLUDED
00028 
00029 #include "glutil.h"
00030 
00031 namespace grinliz
00032 {
00033 const float  PI = 3.1415926535897932384626433832795f;
00034 const float  TWO_PI = 2.0f*3.1415926535897932384626433832795f;
00035 const double PI_D = 3.1415926535897932384626433832795;
00036 const double TWO_PI_D = 2.0*3.1415926535897932384626433832795;
00037 
00038 const float RAD_TO_DEG = (float)( 360.0 / ( 2.0 * PI ) );
00039 const float DEG_TO_RAD = (float)( ( 2.0 * PI ) / 360.0 );
00040 const float SQRT2 = 1.4142135623730950488016887242097f;
00041 const float SQRT2OVER2 = float( 1.4142135623730950488016887242097 / 2.0 );
00042 
00043 inline float ToDegree( float radian ) { return radian * RAD_TO_DEG; }
00044 inline float ToRadian( float degree ) { return degree * DEG_TO_RAD; }
00045 
00046 void SinCosDegree( float degreeTheta, float* sinTheta, float* cosTheta );
00047 
00048 const float EPSILON = 0.000001f;
00049 
00050 inline float NormalizeAngleDegrees( float alpha ) {
00051         while ( alpha < 0.0f )
00052                 alpha += 360.0f;
00053         while ( alpha >= 360.0f )
00054                 alpha -= 360.0f;
00055         return alpha;
00056 }
00057 
00059 inline bool Equal( float x, float y, float epsilon )
00060 {
00061         return fabsf( x - y ) <= epsilon;
00062 }
00063 
00064 inline bool Equal( double x, double y, double epsilon )
00065 {
00066         return fabs( x - y ) <= epsilon;
00067 }
00068 
00069 inline bool EqualInt( float a, float epsilon = EPSILON )
00070 {
00071         return Equal( (float)LRintf(a), a, epsilon );
00072 }
00073 
00080 void DegDelta( float angle0, float angle1, float* distance, float* bias );
00081 
00083 inline float Length( float x, float y )
00084 {
00085         // It's worth some time here:
00086         //              http://www.azillionmonkeys.com/qed/sqroot.html
00087         // to make this (perhaps) better. 
00088 
00089         // Use the much faster "f" version.
00090         return sqrtf( x*x + y*y );
00091 }
00092 
00093 inline double Length( double x, double y ) 
00094 { 
00095         // The more accurate double version.
00096         return sqrt( x*x + y*y ); 
00097 }
00098 
00100 inline float Length( float x, float y, float z )
00101 {
00102         return sqrtf( x*x + y*y + z*z );
00103 }
00104 
00105 inline double Length( double x, double y, double z ) 
00106 { 
00107         return sqrt( x*x + y*y + z*z ); 
00108 }
00109 
00111 inline float Length( float x, float y, float z, float w )
00112 {
00113         return sqrtf( x*x + y*y + z*z + w*w );
00114 }
00115 
00116 
00118 template <class T> inline T FastLength( T x, T y, T z )
00119 {
00120         float a = (float) fabs( x );
00121         float b = (float) fabs( y );
00122         float c = (float) fabs( z );
00123 
00124         if ( b > a ) Swap( &b, &a );
00125         if ( c > a ) Swap( &c, &a );
00126 
00127         // a, b, and c are now meaningless, but
00128         // a is the max.
00129 
00130         float ret = a + ( b + c ) / 4;
00131         return ret;
00132 }
00133 
00134 
00136 template <class T> inline T FastLength( T x, T y )
00137 {
00138         float a = (float) fabs( x );
00139         float b = (float) fabs( y );
00140 
00141         if ( b > a ) grinliz::Swap( &b, &a );
00142 
00143         // a and b are now meaningless, but
00144         // a is the max.
00145 
00146         float ret = a + b / 4;
00147         return ret;
00148 }
00149 
00151 template <class T> inline T Average( T y0, T y1 )
00152 {
00153         return ( y0 + y1 ) / T( 2 );
00154 }
00155 
00156 
00157 
00158 };      // namespace grinliz
00159 
00160 #endif

Generated on Fri Mar 23 19:36:21 2007 for Lilith3D by  doxygen 1.5.1-p1