mMathFn.h
Public Variables
F32(*
m_catmullrom )(F32 t, F32 p0, F32 p1, F32 p2, F32 p3)
void(*
m_matF_affineInverse )(F32 *m)
F32(*
m_matF_determinant )(const F32 *m)
void(*
m_matF_identity )(F32 *m)
void(*
m_matF_inverse )(F32 *m)
void(*
m_matF_invert_to )(const F32 *m, F32 *d)
void(*
m_matF_normalize )(F32 *m)
void(*
m_matF_scale )(F32 *m, const F32 *p)
void(*
m_matF_set_euler )(const F32 *e, F32 *result)
void(*
m_matF_set_euler_point )(const F32 *e, const F32 *p, F32 *result)
void(*
m_matF_transpose )(F32 *m)
void(*
m_matF_x_box3F )(const F32 *m, F32 *min, F32 *max)
void(*
m_matF_x_matF )(const F32 *a, const F32 *b, F32 *mresult)
void(*
m_matF_x_matF_aligned )(const F32 *a, const F32 *b, F32 *mresult)
void(*
m_matF_x_point4F )(const F32 *m, const F32 *p, F32 *presult)
void(*
m_matF_x_scale_x_planeF )(const F32 *m, const F32 *s, const F32 *p, F32 *presult)
S32(*
m_mulDivS32 )(S32 a, S32 b, S32 c)
U32(*
m_mulDivU32 )(S32 a, S32 b, U32 c)
void(*
m_point2D_normalize )(F64 *p)
void(*
m_point2D_normalize_f )(F64 *p, F64 len)
void(*
m_point2F_normalize )(F32 *p)
void(*
m_point2F_normalize_f )(F32 *p, F32 len)
void(*
m_point3D_interpolate )(const F64 *from, const F64 *to, F64 factor, F64 *result)
void(*
m_point3D_normalize )(F64 *p)
void(*
m_point3D_normalize_f )(F64 *p, F64 len)
void(*
m_point3F_bulk_dot )(const F32 *refVector, const F32 *dotPoints, const U32 numPoints, const U32 pointStride, F32 *output)
void(*
m_point3F_bulk_dot_indexed )(const F32 *refVector, const F32 *dotPoints, const U32 numPoints, const U32 pointStride, const U32 *pointIndices, F32 *output)
void(*
m_point3F_interpolate )(const F32 *from, const F32 *to, F32 factor, F32 *result)
void(*
m_point3F_normalize )(F32 *p)
void(*
m_point3F_normalize_f )(F32 *p, F32 len)
void(*
m_quatF_set_matF )(F32 x, F32 y, F32 z, F32 w, F32 *m)
U32(*
mSolveCubic )(F32 a, F32 b, F32 c, F32 d, F32 *x)
U32(*
mSolveQuadratic )(F32 a, F32 b, F32 c, F32 *x)
U32(*
mSolveQuartic )(F32 a, F32 b, F32 c, F32 d, F32 e, F32 *x)
Public Functions
Detailed Description
Public Variables
F32(* m_catmullrom )(F32 t, F32 p0, F32 p1, F32 p2, F32 p3)
void(* m_matF_affineInverse )(F32 *m)
F32(* m_matF_determinant )(const F32 *m)
void(* m_matF_identity )(F32 *m)
void(* m_matF_inverse )(F32 *m)
void(* m_matF_invert_to )(const F32 *m, F32 *d)
void(* m_matF_normalize )(F32 *m)
void(* m_matF_scale )(F32 *m, const F32 *p)
void(* m_matF_set_euler )(const F32 *e, F32 *result)
void(* m_matF_set_euler_point )(const F32 *e, const F32 *p, F32 *result)
void(* m_matF_transpose )(F32 *m)
void(* m_matF_x_box3F )(const F32 *m, F32 *min, F32 *max)
void(* m_matF_x_matF )(const F32 *a, const F32 *b, F32 *mresult)
void(* m_matF_x_matF_aligned )(const F32 *a, const F32 *b, F32 *mresult)
void(* m_matF_x_point4F )(const F32 *m, const F32 *p, F32 *presult)
void(* m_matF_x_scale_x_planeF )(const F32 *m, const F32 *s, const F32 *p, F32 *presult)
S32(* m_mulDivS32 )(S32 a, S32 b, S32 c)
U32(* m_mulDivU32 )(S32 a, S32 b, U32 c)
void(* m_point2D_normalize )(F64 *p)
void(* m_point2D_normalize_f )(F64 *p, F64 len)
void(* m_point2F_normalize )(F32 *p)
void(* m_point2F_normalize_f )(F32 *p, F32 len)
void(* m_point3D_interpolate )(const F64 *from, const F64 *to, F64 factor, F64 *result)
void(* m_point3D_normalize )(F64 *p)
void(* m_point3D_normalize_f )(F64 *p, F64 len)
void(* m_point3F_bulk_dot )(const F32 *refVector, const F32 *dotPoints, const U32 numPoints, const U32 pointStride, F32 *output)
void(* m_point3F_bulk_dot_indexed )(const F32 *refVector, const F32 *dotPoints, const U32 numPoints, const U32 pointStride, const U32 *pointIndices, F32 *output)
void(* m_point3F_interpolate )(const F32 *from, const F32 *to, F32 factor, F32 *result)
void(* m_point3F_normalize )(F32 *p)
void(* m_point3F_normalize_f )(F32 *p, F32 len)
void(* m_quatF_set_matF )(F32 x, F32 y, F32 z, F32 w, F32 *m)
void(* m_sincos )(F32 angle, F32 *s, F32 *c)
void(* m_sincosD )(F64 angle, F64 *s, F64 *c)
U32(* mSolveCubic )(F32 a, F32 b, F32 c, F32 d, F32 *x)
U32(* mSolveQuadratic )(F32 a, F32 b, F32 c, F32 *x)
U32(* mSolveQuartic )(F32 a, F32 b, F32 c, F32 d, F32 e, F32 *x)
Public Functions
m_matF_x_point3F(const F32 * m, const F32 * p, F32 * presult)
m_matF_x_vectorF(const F32 * m, const F32 * v, F32 * vresult)
mAbs(const S32 val)
mAcos(const F32 val)
mAcos(const F64 val)
mAlignToMultiple(A val, B mul)
mAsin(const F32 val)
mAsin(const F64 val)
mAtan(const F32 x)
mAtan(const F64 x)
mAtan2(const F32 y, const F32 x)
mAtan2(const F64 x, const F64 y)
MathConsoleInit()
mCatmullrom(F32 t, F32 p0, F32 p1, F32 p2, F32 p3)
mCeil(const F32 val)
mCeilD(const F64 val)
mClamp(S32 val, S32 low, S32 high)
mClampF(F32 val, F32 low, F32 high)
mClampToZero(F32 & input)
mClampU(U32 val, U32 low, U32 high)
mCos(const F32 angle)
mCos(const F64 angle)
mDegToRad(F32 d)
mDegToRad(F64 d)
mExp(const F32 val)
mFabs(const F32 val)
mFabs(const F64 val)
mFabsD(const F64 val)
mFloor(const F32 val)
mFloorD(const F64 val)
mFmod(const F32 val, const F32 mod)
mFmodD(const F64 val, const F64 mod)
mIsEqual(F32 a, F32 b, const F32 epsilon)
mIsInf_F(const F32 x)
mIsNaN_F(const F32 x)
mIsZero(const F32 val, const F32 epsilon)
mLerp(const T & v1, const T & v2, F32 factor)
Template function for doing a linear interpolation between any two types which implement operators for scalar multiply and addition.
mLog(const F32 val)
mLog(const F64 val)
mMax(const F32 x, const F32 y)
mMulDiv(S32 a, S32 b, S32 c)
mMulDiv(S32 a, S32 b, U32 c)
mPow(const F32 x, const F32 y)
mPow(const F64 x, const F64 y)
mRadToDeg(F32 r)
mRadToDeg(F64 r)
mRandF()
mRandF(F32 f1, F32 f2)
mRandI(S32 i1, S32 i2)
mRound(const F32 val)
mRound(const F32 val, const S32 n)
mRoundToNearest(const F32 val)
mSign(const F32 n)
mSin(const F32 angle)
mSin(const F64 angle)
mSinCos(const F32 angle, F32 & s, F32 & c)
mSinCos(const F64 angle, F64 & s, F64 & c)
mSqrt(const F32 val)
mSqrt(const F64 val)
mSqrtD(const F64 val)
mSquared(F32 n)
Returns the input value squared.
mSquared(F64 n)
mTan(const F32 angle)
mTan(const F64 angle)
mTanh(const F32 angle)
mTanh(const F64 angle)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _MMATHFN_H_ 25#define _MMATHFN_H_ 26 27#include <math.h> 28#include <stdlib.h> 29#include <limits> 30 31#ifndef _MCONSTANTS_H_ 32#include "math/mConstants.h" 33#endif 34#ifndef _PLATFORMASSERT_H_ 35#include "platform/platformAssert.h" 36#endif 37 38 39extern void MathConsoleInit(); 40 41//-------------------------------------- 42// Installable Library Prototypes 43extern S32 (*m_mulDivS32)(S32 a, S32 b, S32 c); 44extern U32 (*m_mulDivU32)(S32 a, S32 b, U32 c); 45 46extern F32 (*m_catmullrom)(F32 t, F32 p0, F32 p1, F32 p2, F32 p3); 47 48extern void (*m_sincos)( F32 angle, F32 *s, F32 *c ); 49extern void (*m_sincosD)( F64 angle, F64 *s, F64 *c ); 50 51extern void (*m_point2F_normalize)(F32 *p); 52extern void (*m_point2F_normalize_f)(F32 *p, F32 len); 53extern void (*m_point2D_normalize)(F64 *p); 54extern void (*m_point2D_normalize_f)(F64 *p, F64 len); 55extern void (*m_point3F_normalize)(F32 *p); 56extern void (*m_point3F_normalize_f)(F32 *p, F32 len); 57extern void (*m_point3F_interpolate)(const F32 *from, const F32 *to, F32 factor, F32 *result); 58 59extern void (*m_point3D_normalize)(F64 *p); 60extern void (*m_point3D_normalize_f)(F64 *p, F64 len); 61extern void (*m_point3D_interpolate)(const F64 *from, const F64 *to, F64 factor, F64 *result); 62 63extern void (*m_point3F_bulk_dot)(const F32* refVector, 64 const F32* dotPoints, 65 const U32 numPoints, 66 const U32 pointStride, 67 F32* output); 68extern void (*m_point3F_bulk_dot_indexed)(const F32* refVector, 69 const F32* dotPoints, 70 const U32 numPoints, 71 const U32 pointStride, 72 const U32* pointIndices, 73 F32* output); 74 75extern void (*m_quatF_set_matF)( F32 x, F32 y, F32 z, F32 w, F32* m ); 76 77extern void (*m_matF_set_euler)(const F32 *e, F32 *result); 78extern void (*m_matF_set_euler_point)(const F32 *e, const F32 *p, F32 *result); 79extern void (*m_matF_identity)(F32 *m); 80extern void (*m_matF_inverse)(F32 *m); 81extern void (*m_matF_invert_to)(const F32 *m, F32 *d); 82extern void (*m_matF_affineInverse)(F32 *m); 83extern void (*m_matF_transpose)(F32 *m); 84extern void (*m_matF_scale)(F32 *m,const F32* p); 85extern void (*m_matF_normalize)(F32 *m); 86extern F32 (*m_matF_determinant)(const F32 *m); 87extern void (*m_matF_x_matF)(const F32 *a, const F32 *b, F32 *mresult); 88extern void (*m_matF_x_matF_aligned)(const F32 *a, const F32 *b, F32 *mresult); 89// extern void (*m_matF_x_point3F)(const F32 *m, const F32 *p, F32 *presult); 90// extern void (*m_matF_x_vectorF)(const F32 *m, const F32 *v, F32 *vresult); 91extern void (*m_matF_x_point4F)(const F32 *m, const F32 *p, F32 *presult); 92extern void (*m_matF_x_scale_x_planeF)(const F32 *m, const F32* s, const F32 *p, F32 *presult); 93extern void (*m_matF_x_box3F)(const F32 *m, F32 *min, F32 *max); 94 95// Note that x must point to at least 4 values for quartics, and 3 for cubics 96extern U32 (*mSolveQuadratic)(F32 a, F32 b, F32 c, F32* x); 97extern U32 (*mSolveCubic)(F32 a, F32 b, F32 c, F32 d, F32* x); 98extern U32 (*mSolveQuartic)(F32 a, F32 b, F32 c, F32 d, F32 e, F32* x); 99 100extern S32 mRandI(S32 i1, S32 i2); // random # from i1 to i2 inclusive 101extern F32 mRandF(F32 f1, F32 f2); // random # from f1 to f2 inclusive 102extern F32 mRandF(); // random # from 0.0 to 1.0 inclusive 103 104 105inline void m_matF_x_point3F(const F32 *m, const F32 *p, F32 *presult) 106{ 107 AssertFatal(p != presult, "Error, aliasing matrix mul pointers not allowed here!"); 108 109#ifdef TORQUE_COMPILER_GCC 110 const F32 p0 = p[0], p1 = p[1], p2 = p[2]; 111 const F32 m0 = m[0], m1 = m[1], m2 = m[2]; 112 const F32 m3 = m[3], m4 = m[4], m5 = m[5]; 113 const F32 m6 = m[6], m7 = m[7], m8 = m[8]; 114 const F32 m9 = m[9], m10 = m[10], m11 = m[11]; 115 116 presult[0] = m0*p0 + m1*p1 + m2*p2 + m3; 117 presult[1] = m4*p0 + m5*p1 + m6*p2 + m7; 118 presult[2] = m8*p0 + m9*p1 + m10*p2 + m11; 119#else 120 presult[0] = m[0]*p[0] + m[1]*p[1] + m[2]*p[2] + m[3]; 121 presult[1] = m[4]*p[0] + m[5]*p[1] + m[6]*p[2] + m[7]; 122 presult[2] = m[8]*p[0] + m[9]*p[1] + m[10]*p[2] + m[11]; 123#endif 124} 125 126 127//-------------------------------------- 128inline void m_matF_x_vectorF(const F32 *m, const F32 *v, F32 *vresult) 129{ 130 AssertFatal(v != vresult, "Error, aliasing matrix mul pointers not allowed here!"); 131 132#ifdef TORQUE_COMPILER_GCC 133 const F32 v0 = v[0], v1 = v[1], v2 = v[2]; 134 const F32 m0 = m[0], m1 = m[1], m2 = m[2]; 135 const F32 m4 = m[4], m5 = m[5], m6 = m[6]; 136 const F32 m8 = m[8], m9 = m[9], m10 = m[10]; 137 138 vresult[0] = m0*v0 + m1*v1 + m2*v2; 139 vresult[1] = m4*v0 + m5*v1 + m6*v2; 140 vresult[2] = m8*v0 + m9*v1 + m10*v2; 141#else 142 vresult[0] = m[0]*v[0] + m[1]*v[1] + m[2]*v[2]; 143 vresult[1] = m[4]*v[0] + m[5]*v[1] + m[6]*v[2]; 144 vresult[2] = m[8]*v[0] + m[9]*v[1] + m[10]*v[2]; 145#endif 146} 147 148 149//-------------------------------------- 150// Inlines 151 152inline bool mIsEqual( F32 a, F32 b, const F32 epsilon = __EQUAL_CONST_F ) 153{ 154 F32 diff = a - b; 155 return diff > -epsilon && diff < epsilon; 156} 157 158inline bool mIsZero(const F32 val, const F32 epsilon = __EQUAL_CONST_F ) 159{ 160 return (val > -epsilon) && (val < epsilon); 161} 162 163inline F32 mClampToZero(F32& input) 164{ 165 if (input < __EQUAL_CONST_F && input > -__EQUAL_CONST_F) 166 input = 0.0f; 167 168 return input; 169} 170 171 172inline F32 mMax(const F32 x, const F32 y) 173{ 174 if (x > y) 175 return x; 176 return y; 177} 178 179inline F32 mFloor(const F32 val) 180{ 181 return (F32) floor(val); 182} 183 184inline F32 mCeil(const F32 val) 185{ 186 return (F32) ceil(val); 187} 188 189inline F32 mFabs(const F32 val) 190{ 191 return (F32) fabs(val); 192} 193 194inline F64 mFabs(const F64 val) 195{ 196 return fabs(val); 197} 198 199inline F32 mFmod(const F32 val, const F32 mod) 200{ 201 return fmod(val, mod); 202} 203 204inline S32 mRound(const F32 val) 205{ 206 return (S32)floor(val + 0.5f); 207} 208 209inline F32 mRound(const F32 val, const S32 n) 210{ 211 S32 place = (S32) pow(10.0f, n); 212 213 return mFloor((val*place)+0.5)/place; 214} 215 216inline S32 mAbs(const S32 val) 217{ 218 return abs(val); 219} 220 221inline F32 mRoundToNearest( const F32 val ) 222{ 223 return mFloor( val + .5f ); 224} 225 226inline S32 mClamp(S32 val, S32 low, S32 high) 227{ 228 return getMax(getMin(val, high), low); 229} 230 231inline U32 mClampU(U32 val, U32 low, U32 high) 232{ 233 return getMax(getMin(val, high), low); 234} 235 236inline F32 mClampF(F32 val, F32 low, F32 high) 237{ 238 return (F32) getMax(getMin(val, high), low); 239} 240 241/// Template function for doing a linear interpolation between any two 242/// types which implement operators for scalar multiply and addition. 243template <typename T> 244inline T mLerp( const T &v1, const T &v2, F32 factor ) 245{ 246 return ( v1 * ( 1.0f - factor ) ) + ( v2 * factor ); 247} 248 249inline S32 mMulDiv(S32 a, S32 b, S32 c) 250{ 251 return m_mulDivS32(a, b, c); 252} 253 254inline U32 mMulDiv(S32 a, S32 b, U32 c) 255{ 256 return m_mulDivU32(a, b, c); 257} 258 259inline F32 mSin(const F32 angle) 260{ 261 return (F32) sin(angle); 262} 263 264inline F32 mCos(const F32 angle) 265{ 266 return (F32) cos(angle); 267} 268 269inline F32 mTan(const F32 angle) 270{ 271 return (F32) tan(angle); 272} 273 274inline F32 mAsin(const F32 val) 275{ 276 return (F32) asin(val); 277} 278 279inline F32 mAcos(const F32 val) 280{ 281 return (F32) acos(val); 282} 283 284inline F32 mAtan( const F32 x ) 285{ 286 return (F32) atan( x ); 287} 288 289inline F32 mAtan2(const F32 y, const F32 x) 290{ 291 return (F32)atan2(y, x); 292} 293 294inline void mSinCos(const F32 angle, F32 &s, F32 &c) 295{ 296 m_sincos( angle, &s, &c ); 297} 298 299inline F32 mTanh(const F32 angle) 300{ 301 return (F32) tanh(angle); 302} 303 304inline F32 mSqrt(const F32 val) 305{ 306 return (F32) sqrt(val); 307} 308 309inline F64 mSqrt(const F64 val) 310{ 311 return (F64) sqrt(val); 312} 313 314inline F32 mPow(const F32 x, const F32 y) 315{ 316 return (F32) pow(x, y); 317} 318 319inline F32 mLog(const F32 val) 320{ 321 return (F32) log(val); 322} 323 324inline F32 mExp(const F32 val) 325{ 326 return (F32) exp(val); 327} 328 329inline F64 mSin(const F64 angle) 330{ 331 return (F64) sin(angle); 332} 333 334inline F64 mCos(const F64 angle) 335{ 336 return (F64) cos(angle); 337} 338 339inline F64 mTan(const F64 angle) 340{ 341 return (F64) tan(angle); 342} 343 344inline F64 mAsin(const F64 val) 345{ 346 return (F64) asin(val); 347} 348 349inline F64 mAcos(const F64 val) 350{ 351 return (F64) acos(val); 352} 353 354inline F64 mAtan( const F64 x ) 355{ 356 return (F64) atan( x ); 357} 358 359inline F64 mAtan2(const F64 x, const F64 y) 360{ 361 return (F64) atan2(x, y); 362} 363 364inline void mSinCos(const F64 angle, F64 &s, F64 &c) 365{ 366 m_sincosD( angle, &s, &c ); 367} 368 369inline F64 mTanh(const F64 angle) 370{ 371 return (F64) tanh(angle); 372} 373 374inline F64 mPow(const F64 x, const F64 y) 375{ 376 return (F64) pow(x, y); 377} 378 379inline F64 mLog(const F64 val) 380{ 381 return (F64) log(val); 382} 383 384 385inline F32 mCatmullrom(F32 t, F32 p0, F32 p1, F32 p2, F32 p3) 386{ 387 return m_catmullrom(t, p0, p1, p2, p3); 388} 389 390 391inline F64 mFabsD(const F64 val) 392{ 393 return (F64) fabs(val); 394} 395 396inline F64 mFmodD(const F64 val, const F64 mod) 397{ 398 return (F64) fmod(val, mod); 399} 400 401inline F64 mSqrtD(const F64 val) 402{ 403 return (F64) sqrt(val); 404} 405 406inline F64 mFloorD(const F64 val) 407{ 408 return (F64) floor(val); 409} 410 411inline F64 mCeilD(const F64 val) 412{ 413 return (F64) ceil(val); 414} 415 416/// 417template< typename A, typename B > 418inline A mAlignToMultiple( A val, B mul ) 419{ 420 A rem = val % mul; 421 return ( rem ? val + mul - rem : val ); 422} 423 424//-------------------------------------- 425inline F32 mDegToRad(F32 d) 426{ 427 return((d * M_PI_F) / 180.0f); 428} 429 430inline F32 mRadToDeg(F32 r) 431{ 432 return((r * 180.0f) / M_PI_F); 433} 434 435inline F64 mDegToRad(F64 d) 436{ 437 return (d * M_PI) / 180.0; 438} 439 440inline F64 mRadToDeg(F64 r) 441{ 442 return (r * 180.0) / M_PI; 443} 444 445//------------------------------------------------------------------------------ 446 447inline bool mIsNaN_F( const F32 x ) 448{ 449 // If x is a floating point variable, then (x != x) will be TRUE if x has the value NaN. 450 // This is only going to work if the compiler is IEEE 748 compliant. 451 // 452 // Tested and working on VC2k5 453 return ( x != x ); 454} 455 456inline bool mIsInf_F( const F32 x ) 457{ 458 return ( x == std::numeric_limits< F32 >::infinity() ); 459} 460 461inline F32 mSign( const F32 n ) 462{ 463 if ( n > 0.0f ) 464 return 1.0f; 465 if ( n < 0.0f ) 466 return -1.0f; 467 468 return 0.0f; 469} 470 471/// Returns the input value squared. 472inline F32 mSquared( F32 n ) 473{ 474 return n * n; 475} 476 477/// @copydoc mSquaredF 478inline F64 mSquared( F64 n ) 479{ 480 return n * n; 481} 482 483 484#endif //_MMATHFN_H_ 485
