00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 double PI_D = 3.1415926535897932384626433832795;
00035
00036 const float RAD_TO_DEG = (float)( 360.0 / ( 2.0 * PI ) );
00037 const float DEG_TO_RAD = (float)( ( 2.0 * PI ) / 360.0 );
00038 const float SQRT2 = 1.4142135623730950488016887242097f;
00039 const float SQRT2OVER2 = float( 1.4142135623730950488016887242097 / 2.0 );
00040
00041 inline float ToDegree( float radian ) { return radian * RAD_TO_DEG; }
00042 inline float ToRadian( float degree ) { return degree * DEG_TO_RAD; }
00043
00044 void SinCosDegree( float degreeTheta, float* sinTheta, float* cosTheta );
00045
00046 const float EPSILON = 0.000001f;
00047
00048 inline float NormalizeAngleDegrees( float alpha ) {
00049 while ( alpha < 0.0f )
00050 alpha += 360.0f;
00051 while ( alpha >= 360.0f )
00052 alpha -= 360.0f;
00053 return alpha;
00054 }
00055
00057 inline bool Equal( float x, float y, float epsilon )
00058 {
00059
00060 return fabsf( x - y ) <= epsilon;
00061 }
00062 inline bool Equal( double x, double y, double epsilon )
00063 {
00064 return fabs( x - y ) <= epsilon;
00065 }
00066
00073 void DegDelta( float angle0, float angle1, float* distance, float* bias );
00074
00076 inline float Length( float x, float y )
00077 {
00078
00079
00080
00081
00082
00083 return sqrtf( x*x + y*y );
00084 }
00085
00086 inline double Length( double x, double y )
00087 {
00088
00089 return sqrt( x*x + y*y );
00090 }
00091
00093 inline float Length( float x, float y, float z )
00094 {
00095 return sqrtf( x*x + y*y + z*z );
00096 }
00097
00098 inline double Length( double x, double y, double z )
00099 {
00100 return sqrt( x*x + y*y + z*z );
00101 }
00102
00104 inline float Length( float x, float y, float z, float w )
00105 {
00106 return sqrtf( x*x + y*y + z*z + w*w );
00107 }
00108
00109
00111 template <class T> inline T FastLength( T x, T y, T z )
00112 {
00113 float a = (float) fabs( x );
00114 float b = (float) fabs( y );
00115 float c = (float) fabs( z );
00116
00117 if ( b > a ) Swap( &b, &a );
00118 if ( c > a ) Swap( &c, &a );
00119
00120
00121
00122
00123 float ret = a + ( b + c ) / 4;
00124 return ret;
00125 }
00126
00127
00129 template <class T> inline T FastLength( T x, T y )
00130 {
00131 float a = (float) fabs( x );
00132 float b = (float) fabs( y );
00133
00134 if ( b > a ) grinliz::Swap( &b, &a );
00135
00136
00137
00138
00139 float ret = a + b / 4;
00140 return ret;
00141 }
00142
00144 template <class T> inline T Average( T y0, T y1 )
00145 {
00146 return ( y0 + y1 ) / T( 2 );
00147 }
00148
00149
00150
00151 };
00152
00153 #endif