nmath.h
Go to the documentation of this file.00001 #ifndef N_MATH_H
00002 #define N_MATH_H
00003
00012 #include <math.h>
00013 #include <stdlib.h>
00014
00015 #include "kernel/ntypes.h"
00016
00017 #ifdef _MSC_VER
00018 #define isnan _isnan
00019 #define isinf _isinf
00020 #endif
00021
00022 #ifndef PI
00023 #define PI (3.1415926535897932384626433832795028841971693993751f)
00024 #endif
00025
00026 #define N_PI PI
00027
00028 #ifndef TINY
00029 #define TINY (0.0000001f)
00030 #endif
00031 #define N_TINY TINY
00032
00033 #define n_max(a,b) (((a) > (b)) ? (a) : (b))
00034 #define n_min(a,b) (((a) < (b)) ? (a) : (b))
00035 #define n_abs(a) (((a)<0.0f) ? (-(a)) : (a))
00036 #define n_sgn(a) (((a)<0.0f) ? (-1) : (1))
00037 #define n_deg2rad(d) (((d)*PI)/180.0f)
00038 #define n_rad2deg(r) (((r)*180.0f)/PI)
00039 #define n_sin(x) (float(sin(x)))
00040 #define n_cos(x) (float(cos(x)))
00041 #define n_tan(x) (float(tan(x)))
00042 #define n_atan(x) (float(atan(x)))
00043 #define n_atan2(x,y) (float(atan2(x,y)))
00044 #define n_exp(x) (float(exp(x)))
00045 #define n_floor(x) (float(floor(x)))
00046 #define n_ceil(x) (float(ceil(x)))
00047 #define n_pow(x,y) (float(pow(x,y)))
00048
00049
00053 const float LN_2 = 0.693147180559945f;
00054 inline float n_log2(float f)
00055 {
00056 return logf(f) / LN_2;
00057 }
00058
00059
00063 inline int n_iclamp(int val, int minVal, int maxVal)
00064 {
00065 if (val < minVal) return minVal;
00066 else if (val > maxVal) return maxVal;
00067 else return val;
00068 }
00069
00070
00074 inline float n_acos(float x)
00075 {
00076 if (x > 1.0f) x = 1.0f;
00077 if (x < -1.0f) x = -1.0f;
00078 return (float)acos(x);
00079 }
00080
00081
00085 inline float n_asin(float x)
00086 {
00087 if (x > 1.0f) x = 1.0f;
00088 if (x < -1.0f) x = -1.0f;
00089 return (float)asin(x);
00090 }
00091
00092
00096 inline float n_sqrt(float x)
00097 {
00098 if (x < 0.0f) x = (float) 0.0f;
00099 return (float) sqrt(x);
00100 }
00101
00102
00106 inline bool n_fequal(float f0, float f1, float tol) {
00107 float f = f0-f1;
00108 if ((f>(-tol)) && (f<tol)) return true;
00109 else return false;
00110 }
00111
00112
00116 inline bool n_fless(float f0, float f1, float tol) {
00117 if ((f0-f1)<tol) return true;
00118 else return false;
00119 }
00120
00121
00125 inline bool n_fgreater(float f0, float f1, float tol) {
00126 if ((f0-f1)>tol) return true;
00127 else return false;
00128 }
00129
00130
00136 inline long n_ftol(float val)
00137 {
00138 double v = double(val) + (68719476736.0*1.5);
00139 return ((long*)&v)[0] >> 16;
00140 }
00141
00142
00146 inline float n_smooth(float newVal, float curVal, float maxChange)
00147 {
00148 float diff = newVal - curVal;
00149 if (fabs(diff) > maxChange)
00150 {
00151 if (diff > 0.0f)
00152 {
00153 curVal += maxChange;
00154 if (curVal > newVal)
00155 {
00156 curVal = newVal;
00157 }
00158 }
00159 else if (diff < 0.0f)
00160 {
00161 curVal -= maxChange;
00162 if (curVal < newVal)
00163 {
00164 curVal = newVal;
00165 }
00166 }
00167 }
00168 else
00169 {
00170 curVal = newVal;
00171 }
00172 return curVal;
00173 }
00174
00175
00179 inline float n_clamp(float val, float lower, float upper)
00180 {
00181 if (val < lower) return lower;
00182 else if (val > upper) return upper;
00183 else return val;
00184 }
00185
00186
00190 inline float n_saturate(float val)
00191 {
00192 if (val < 0.0f) return 0.0f;
00193 else if (val > 1.0f) return 1.0f;
00194 else return val;
00195 }
00196
00197
00201 inline float n_rand()
00202 {
00203 return float(rand()) / float(RAND_MAX);
00204 }
00205
00206
00210 inline float n_rand(float min, float max)
00211 {
00212 float unit = float(rand()) / RAND_MAX;
00213 float diff = max - min;
00214
00215 return min + unit * diff;
00216 }
00217
00218
00222 inline int n_fchop(float f)
00223 {
00224
00225 return int(f);
00226 }
00227
00228
00232 inline int n_frnd(float f)
00233 {
00234 return n_fchop(floorf(f + 0.5f));
00235 }
00236
00237
00241 inline float n_lerp(float x, float y, float l)
00242 {
00243 return x + l * (y - x);
00244 }
00245
00246
00249 inline
00250 float
00251 n_fmod(float x, float y)
00252 {
00253 return fmodf(x, y);
00254 }
00255
00256
00261 template<class TYPE>
00262 inline
00263 void
00264 lerp(TYPE & result, const TYPE & val0, const TYPE & val1, float lerpVal)
00265 {
00266 n_error("Unimplemented lerp function!");
00267 }
00268
00269
00272 template<>
00273 inline
00274 void
00275 lerp<int>(int & result, const int & val0, const int & val1, float lerpVal)
00276 {
00277 result = n_frnd((float)val0 + (((float)val1 - (float)val0) * lerpVal));
00278 }
00279
00280
00283 template<>
00284 inline
00285 void
00286 lerp<float>(float & result, const float & val0, const float & val1, float lerpVal)
00287 {
00288 result = val0 + ((val1 - val0) * lerpVal);
00289 }
00290
00291
00295 inline float n_normangle(float a)
00296 {
00297 while (a < 0.0f)
00298 {
00299 a += n_deg2rad(360.0f);
00300 }
00301 if (a >= n_deg2rad(360.0f))
00302 {
00303 a = n_fmod(a, n_deg2rad(360.0f));
00304 }
00305 return a;
00306 }
00307
00308
00312 inline
00313 float
00314 n_angulardistance(float from, float to)
00315 {
00316 float normFrom = n_normangle(from);
00317 float normTo = n_normangle(to);
00318 float dist = normTo - normFrom;
00319 if (dist < n_deg2rad(-180.0f))
00320 {
00321 dist += n_deg2rad(360.0f);
00322 }
00323 else if (dist > n_deg2rad(180.0f))
00324 {
00325 dist -= n_deg2rad(360.0f);
00326 }
00327 return dist;
00328 }
00329
00330 #endif
00331