AI_Math.h

Go to the documentation of this file.
00001 #ifndef AI_MATH_H
00002 #define AI_MATH_H
00003 //------------------------------------------------------------------------------
00009 #include <math.h>
00010 #include <stdlib.h>
00011 
00012 #ifdef _MSC_VER
00013     #ifndef isnan
00014     #define isnan _isnan
00015     #endif
00016     
00017     #ifndef isinf
00018     #define isinf _isinf
00019     #endif
00020 #endif
00021 
00022 #ifndef PI
00023 #define PI (3.1415926535897932384626433832795028841971693993751f)
00024 #endif
00025 
00026 #define AI_PI PI
00027 #define AI_2PI 6.2831853f
00028 #define AI_HALFPI 1.570796326f
00029 #define AI_QUARTERPI 0.785398163f
00030 #define AI_TINY 0.00001f //0.0000001
00031 #define AI_INFINITY 3500000.0f
00032 #define AI_SQRT2 1.414213562373f
00033 
00034 #define ai_max(a,b)      (((a) > (b)) ? (a) : (b))
00035 #define ai_min(a,b)      (((a) < (b)) ? (a) : (b))
00036 #define ai_abs(a)        (((a)<0.0f) ? (-(a)) : (a))
00037 #define ai_sgn(a)        (((a)<0.0f) ? (-1) : (1))
00038 #define ai_deg2rad(d)    (((d)*AI_PI)/180.0f)
00039 #define ai_rad2deg(r)    (((r)*180.0f)/AI_PI)
00040 #define ai_sin(x)        (float(sin(x)))
00041 #define ai_cos(x)        (float(cos(x)))
00042 #define ai_tan(x)        (float(tan(x)))
00043 #define ai_atan(x)       (float(atan(x)))
00044 
00045 //------------------------------------------------------------------------------
00049 const float AI_LN_2 = 0.693147180559945f;
00050 static inline float ai_log2(float f)
00051 {
00052     return logf(f) / AI_LN_2;
00053 }
00054 
00055 //------------------------------------------------------------------------------
00059 static inline int ai_iclamp(int val, int minVal, int maxVal)
00060 {
00061     if (val < minVal)      return minVal;
00062     else if (val > maxVal) return maxVal;
00063     else return val;
00064 }
00065 
00066 //------------------------------------------------------------------------------
00070 static inline float ai_acos(float x)
00071 {
00072     if(x >  1.0f) x =  1.0f;
00073     if(x < -1.0f) x = -1.0f;
00074     return (float)acos(x);
00075 }
00076 
00077 //------------------------------------------------------------------------------
00081 static inline float ai_asin(float x)
00082 {
00083     if(x >  1.0f) x =  1.0f;
00084     if(x < -1.0f) x = -1.0f;
00085     return (float)asin(x);
00086 }
00087 
00088 //------------------------------------------------------------------------------
00092 static inline float ai_sqrt(float x)
00093 {
00094     if (x < 0.0f) x = (float) 0.0f;
00095     return (float) sqrt(x);
00096 }
00097 
00098 //------------------------------------------------------------------------------
00102 static inline bool ai_fequal(float f0, float f1, float tol) {
00103     float f = f0-f1;
00104     if ((f>(-tol)) && (f<tol)) return true;
00105     else                       return false;
00106 }
00107 
00108 //------------------------------------------------------------------------------
00112 static inline bool ai_fless(float f0, float f1, float tol) {
00113     if ((f0-f1)<tol) return true;
00114     else             return false;
00115 }
00116 
00117 //------------------------------------------------------------------------------
00121 static inline bool ai_fgreater(float f0, float f1, float tol) {
00122     if ((f0-f1)>tol) return true;
00123     else             return false;
00124 }
00125 
00126 //------------------------------------------------------------------------------
00132 static inline long ai_ftol(float val)
00133 {
00134     double v = double(val) + (68719476736.0*1.5);
00135     return ((long*)&v)[0] >> 16;
00136 }
00137 
00138 //------------------------------------------------------------------------------
00142 static inline float ai_smooth(float newVal, float curVal, float maxChange)
00143 {
00144     float diff = newVal - curVal;
00145     if (fabs(diff) > maxChange)
00146     {
00147         if (diff > 0.0f)
00148         {
00149             curVal += maxChange;
00150             if (curVal > newVal)
00151             {
00152                 curVal = newVal;
00153             }
00154         }
00155         else if (diff < 0.0f)
00156         {
00157             curVal -= maxChange;
00158             if (curVal < newVal)
00159             {
00160                 curVal = newVal;
00161             }
00162         }
00163     }
00164     else
00165     {
00166         curVal = newVal;
00167     }
00168     return curVal;
00169 }
00170 
00171 //------------------------------------------------------------------------------
00175 static inline float ai_clamp(float val, float lower, float upper)
00176 {
00177     if (val < lower)      return lower;
00178     else if (val > upper) return upper;
00179     else                  return val;
00180 }
00181 
00182 //------------------------------------------------------------------------------
00186 static inline float ai_saturate(float val)
00187 {
00188     if (val < 0.0f)      return 0.0f;
00189     else if (val > 1.0f) return 1.0f;
00190     else return val;
00191 }
00192 
00193 //------------------------------------------------------------------------------
00197 static inline float ai_rand()
00198 {
00199     return float(rand()) / float(RAND_MAX);
00200 }
00201 
00202 //------------------------------------------------------------------------------
00206 static inline int ai_fchop(float f)
00207 {
00208     // FIXME!
00209     return int(f);
00210 }
00211 
00212 //------------------------------------------------------------------------------
00216 static inline int ai_frnd(float f)
00217 {
00218     return ai_fchop(f + 0.5f);
00219 }
00220 
00221 //------------------------------------------------------------------------------
00225 static inline float ai_lerp(float x, float y, float l)
00226 {
00227     return x + l * (y - x);
00228 }
00229 
00230 
00231 static inline bool ai_isEqualFloat(const float f0, const float f1, const float tol) 
00232 {
00233     float f = f0 - f1;
00234     return ((f>(-tol)) && (f<tol));
00235 }
00236 
00237 
00238 static inline int ai_random(const int i_sides)
00239 {
00240     return (rand() % i_sides) + 1;
00241 }
00242 
00243 
00244 //------------------------------------------------------------------------------
00249 template<class TYPE>
00250 static inline
00251 void
00252 ai_lerp(TYPE & result, const TYPE & val0, const TYPE & val1, float lerpVal)
00253 {
00254     n_error("Unimplemented lerp function!");
00255 }
00256 
00257 //------------------------------------------------------------------------------
00260 template<>
00261 static inline
00262 void
00263 ai_lerp<int>(int & result, const int & val0, const int & val1, float lerpVal)
00264 {
00265     result = ai_fchop((float)val0 + (((float)val1 - (float)val0) * lerpVal));
00266 }
00267 
00268 //------------------------------------------------------------------------------
00271 template<>
00272 static inline
00273 void
00274 ai_lerp<float>(float & result, const float & val0, const float & val1, float lerpVal)
00275 {
00276     result = val0 + ((val1 - val0) * lerpVal);
00277 }
00278 
00279 //------------------------------------------------------------------------------
00280 #endif
00281