00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00043 #ifndef _orxMATH_H_
00044 #define _orxMATH_H_
00045
00046
00047 #include "orxInclude.h"
00048
00051 #include <math.h>
00052
00053
00063 #define orxLERP(A, B, T) ((A) + ((T) * ((B) - (A))))
00064
00065
00071 #define orxMIN(A, B) (((A) > (B)) ? (B) : (A))
00072
00078 #define orxMAX(A, B) (((A) < (B)) ? (B) : (A))
00079
00086 #define orxCLAMP(V, MIN, MAX) orxMAX(orxMIN(V, MAX), MIN)
00087
00094 #define orxCIRCULAR_CLAMP_INC_MIN(V, MIN, MAX) \
00095 do \
00096 { \
00097 while((V) < (MIN)) \
00098 { \
00099 (V) += ((MAX) - (MIN)); \
00100 } \
00101 while((V) >= (MAX)) \
00102 { \
00103 (V) -= ((MAX) - (MIN)); \
00104 } \
00105 } while(orxFALSE)
00106
00113 #define orxCIRCULAR_CLAMP_INC_MAX(V, MIN, MAX) \
00114 do \
00115 { \
00116 while((V) <= (MIN)) \
00117 { \
00118 (V) += ((MAX) - (MIN)); \
00119 } \
00120 while((V) > (MAX)) \
00121 { \
00122 (V) -= ((MAX) - (MIN)); \
00123 } \
00124 } while(orxFALSE)
00125
00130 #define orxF2U(V) ((orxU32) (V))
00131
00136 #define orxF2S(V) ((orxS32) (V))
00137
00142 #define orxU2F(V) ((orxFLOAT)(V))
00143
00148 #define orxS2F(V) ((orxFLOAT)(V))
00149
00150
00151
00152
00156 extern orxDLLAPI void orxFASTCALL orxMath_InitRandom(orxS32 _s32Seed);
00157
00163 extern orxDLLAPI orxFLOAT orxFASTCALL orxMath_GetRandomFloat(orxFLOAT _fMin, orxFLOAT _fMax);
00164
00170 extern orxDLLAPI orxU32 orxFASTCALL orxMath_GetRandomU32(orxU32 _u32Min, orxU32 _u32Max);
00171
00177 extern orxDLLAPI orxS32 orxFASTCALL orxMath_GetRandomS32(orxS32 _s32Min, orxS32 _s32Max);
00178
00179
00180
00181
00186 static orxINLINE orxU32 orxMath_GetBitCount(orxU32 _u32Value)
00187 {
00188 _u32Value -= ((_u32Value >> 1) & 0x55555555);
00189 _u32Value = (((_u32Value >> 2) & 0x33333333) + (_u32Value & 0x33333333));
00190 _u32Value = (((_u32Value >> 4) + _u32Value) & 0x0f0f0f0f);
00191 _u32Value += (_u32Value >> 8);
00192 _u32Value += (_u32Value >> 16);
00193 return(_u32Value & 0x0000003f);
00194 }
00195
00200 static orxINLINE orxU32 orxMath_GetTrailingZeroCount(orxU32 _u32Value)
00201 {
00202
00203 static const orxU32 sau32DeBruijnLUT[32] =
00204 {
00205 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
00206 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
00207 };
00208
00209
00210 return sau32DeBruijnLUT[((orxU32)(((orxS32)_u32Value & -(orxS32)_u32Value) * 0x077CB531U)) >> 27];
00211 }
00212
00217 static orxINLINE orxBOOL orxMath_IsPowerOfTwo(orxU32 _u32Value)
00218 {
00219 orxBOOL bResult;
00220
00221
00222 bResult = ((_u32Value & (_u32Value - 1)) == 0) ? orxTRUE : orxFALSE;
00223
00224
00225 return bResult;
00226 }
00227
00232 static orxINLINE orxU32 orxMath_GetNextPowerOfTwo(orxU32 _u32Value)
00233 {
00234 orxU32 u32Result;
00235
00236
00237 if(_u32Value != 0)
00238 {
00239
00240 u32Result = _u32Value - 1;
00241 u32Result = u32Result | (u32Result >> 1);
00242 u32Result = u32Result | (u32Result >> 2);
00243 u32Result = u32Result | (u32Result >> 4);
00244 u32Result = u32Result | (u32Result >> 8);
00245 u32Result = u32Result | (u32Result >> 16);
00246 u32Result++;
00247 }
00248 else
00249 {
00250
00251 u32Result = 1;
00252 }
00253
00254
00255 return u32Result;
00256 }
00257
00258
00259
00260
00261 #define orxMATH_KF_SQRT_2 orx2F(1.414213562f)
00262 #define orxMATH_KF_EPSILON orx2F(0.0001f)
00263 #define orxMATH_KF_2_PI orx2F(6.283185307f)
00264 #define orxMATH_KF_PI orx2F(3.141592654f)
00265 #define orxMATH_KF_PI_BY_2 orx2F(1.570796327f)
00266 #define orxMATH_KF_PI_BY_4 orx2F(0.785398163f)
00267 #define orxMATH_KF_DEG_TO_RAD orx2F(3.141592654f / 180.0f)
00268 #define orxMATH_KF_RAD_TO_DEG orx2F(180.0f / 3.141592654f)
00271
00272
00277 static orxINLINE orxFLOAT orxMath_Cos(orxFLOAT _fOp)
00278 {
00279 register orxFLOAT fResult;
00280
00281 #ifdef __orxMSVC__
00282
00283
00284 fResult = cosf(_fOp);
00285
00286 #else
00287
00288
00289 fResult = cosf(_fOp);
00290
00291 #endif
00292
00293
00294 return fResult;
00295 }
00296
00301 static orxINLINE orxFLOAT orxMath_Sin(orxFLOAT _fOp)
00302 {
00303 register orxFLOAT fResult;
00304
00305 #ifdef __orxMSVC__
00306
00307
00308 fResult = sinf(_fOp);
00309
00310 #else
00311
00312
00313 fResult = sinf(_fOp);
00314
00315 #endif
00316
00317
00318 return fResult;
00319 }
00320
00325 static orxINLINE orxFLOAT orxMath_Tan(orxFLOAT _fOp)
00326 {
00327 register orxFLOAT fResult;
00328
00329 #ifdef __orxMSVC__
00330
00331
00332 fResult = tanf(_fOp);
00333
00334 #else
00335
00336
00337 fResult = tanf(_fOp);
00338
00339 #endif
00340
00341
00342 return fResult;
00343 }
00344
00349 static orxINLINE orxFLOAT orxMath_ACos(orxFLOAT _fOp)
00350 {
00351 register orxFLOAT fResult;
00352
00353 #ifdef __orxMSVC__
00354
00355
00356 fResult = acosf(_fOp);
00357
00358 #else
00359
00360
00361 fResult = acosf(_fOp);
00362
00363 #endif
00364
00365
00366 return fResult;
00367 }
00368
00373 static orxINLINE orxFLOAT orxMath_ASin(orxFLOAT _fOp)
00374 {
00375 register orxFLOAT fResult;
00376
00377 #ifdef __orxMSVC__
00378
00379
00380 fResult = asinf(_fOp);
00381
00382 #else
00383
00384
00385 fResult = asinf(_fOp);
00386
00387 #endif
00388
00389
00390 return fResult;
00391 }
00392
00398 static orxINLINE orxFLOAT orxMath_ATan(orxFLOAT _fOp1, orxFLOAT _fOp2)
00399 {
00400 register orxFLOAT fResult;
00401
00402 #ifdef __orxMSVC__
00403
00404
00405 fResult = atan2f(_fOp1, _fOp2);
00406
00407 #else
00408
00409
00410 fResult = atan2f(_fOp1, _fOp2);
00411
00412 #endif
00413
00414
00415 return fResult;
00416 }
00417
00418
00419
00420
00425 static orxINLINE orxFLOAT orxMath_Sqrt(orxFLOAT _fOp)
00426 {
00427 register orxFLOAT fResult;
00428
00429
00430 fResult = sqrtf(_fOp);
00431
00432
00433 return fResult;
00434 }
00435
00440 static orxINLINE orxFLOAT orxMath_Floor(orxFLOAT _fOp)
00441 {
00442 register orxFLOAT fResult;
00443
00444 #ifdef __orxMSVC__
00445
00446
00447 fResult = floorf(_fOp);
00448
00449 #else
00450
00451
00452 fResult = floor(_fOp);
00453
00454 #endif
00455
00456
00457 return fResult;
00458 }
00459
00464 static orxINLINE orxFLOAT orxMath_Ceil(orxFLOAT _fOp)
00465 {
00466 register orxFLOAT fResult;
00467
00468
00469 fResult = ceilf(_fOp);
00470
00471
00472 return fResult;
00473 }
00474
00479 static orxINLINE orxFLOAT orxMath_Round(orxFLOAT _fOp)
00480 {
00481 register orxFLOAT fResult;
00482
00483 #ifdef __orxMSVC__
00484
00485
00486 fResult = (fmodf(_fOp, orxFLOAT_1) >= orx2F(0.5f)) ? ceilf(_fOp) : floorf(_fOp);
00487
00488 #else
00489
00490
00491 fResult = rintf(_fOp);
00492
00493 #endif
00494
00495
00496 return fResult;
00497 }
00498
00504 static orxINLINE orxFLOAT orxMath_Mod(orxFLOAT _fOp1, orxFLOAT _fOp2)
00505 {
00506 register orxFLOAT fResult;
00507
00508
00509 fResult = fmodf(_fOp1, _fOp2);
00510
00511
00512 return fResult;
00513 }
00514
00520 static orxINLINE orxFLOAT orxMath_Pow(orxFLOAT _fOp, orxFLOAT _fExp)
00521 {
00522 register orxFLOAT fResult;
00523
00524
00525 fResult = powf(_fOp, _fExp);
00526
00527
00528 return fResult;
00529 }
00530
00535 static orxINLINE orxFLOAT orxMath_Abs(orxFLOAT _fOp)
00536 {
00537
00538 return fabsf(_fOp);
00539 }
00540
00541 #endif
00542