fbxmath.h

Go to the documentation of this file.
00001 /****************************************************************************************
00002  
00003    Copyright (C) 2013 Autodesk, Inc.
00004    All rights reserved.
00005  
00006    Use of this software is subject to the terms of the Autodesk license agreement
00007    provided at the time of installation or download, or which otherwise accompanies
00008    this software in either electronic or hard copy form.
00009  
00010 ****************************************************************************************/
00011 
00013 #ifndef _FBXSDK_CORE_MATH_H_
00014 #define _FBXSDK_CORE_MATH_H_
00015 
00016 #include <fbxsdk/fbxsdk_def.h>
00017 
00018 #include <fbxsdk/core/math/fbxvector2.h>
00019 #include <fbxsdk/core/math/fbxvector4.h>
00020 #include <fbxsdk/core/math/fbxmatrix.h>
00021 #include <fbxsdk/core/math/fbxaffinematrix.h>
00022 
00023 //On Mac OS, cmath will include math.h and undef "isnan"
00024 #if defined(FBXSDK_ENV_MAC)
00025     #include <cmath>
00026     extern "C" int isnan (double);
00027 #endif
00028 
00029 #include <fbxsdk/fbxsdk_nsbegin.h>
00030 
00031 #if defined(FBXSDK_ENV_WIN)
00032     #ifndef isnan
00033         #define isnan   _isnan
00034     #endif
00035     #ifndef finite
00036         #define finite  _finite
00037     #endif
00038 #endif
00039 
00040 //---------------------------------------------------------------------------------------
00041 //Common Constants
00042 #define FBXSDK_PI           3.1415926535897932384626433832795028841971693993751
00043 #define FBXSDK_PI_DIV_2     1.5707963267948966192313216916397514420985846996875
00044 #define FBXSDK_PI_DIV_180   0.017453292519943295769236907684886127134428718885417
00045 #define FBXSDK_180_DIV_PI   57.295779513082320876798154814105170332405472466565
00046 #define FBXSDK_1_DIV_LN2    1.4426950408889634073599246810018921374266459541530
00047 
00048 //---------------------------------------------------------------------------------------
00049 //Unit Convertion Ratio
00050 #define FBXSDK_DEG_TO_RAD   FBXSDK_PI_DIV_180   //!< Degree to Radian
00051 #define FBXSDK_RAD_TO_DEG   FBXSDK_180_DIV_PI   //!< Radian to Degree
00052 #define FBXSDK_IN_TO_CM     2.54                //!< Inch to Centimeter
00053 #define FBXSDK_CM_TO_IN     0.393700787         //!< Centimeter to Inch
00054 #define FBXSDK_IN_TO_MM     25.4                //!< Inch to Millimeter
00055 #define FBXSDK_MM_TO_IN     0.0393700787        //!< Millimeter to Inch
00056 #define FBXSDK_FT_TO_M      0.3048              //!< Feet to Meter
00057 #define FBXSDK_M_TO_FT      3.2808399           //!< Meter to Feet
00058 #define FBXSDK_YD_TO_FT     3                   //!< Yard to Feet
00059 #define FBXSDK_FT_TO_YD     0.333333333         //!< Feet to Yard
00060 #define FBXSDK_KM_TO_MILE   0.621371192         //!< Kilometer to Mile
00061 #define FBXSDK_MILE_TO_KM   1.609344            //!< Mile to Kilometer
00062 #define FBXSDK_YD_TO_M      0.9144              //!< Yard to Meter
00063 #define FBXSDK_M_TO_YD      1.0936133           //!< Meter to Yard
00064 
00065 //---------------------------------------------------------------------------------------
00066 //Euler Definition
00067 class FBXSDK_DLL FbxEuler
00068 {
00069 public:
00070     enum ERepeat {eRepeatNo=0, eRepeatYes=1};
00071     enum EParity {eParityEven=0, eParityOdd=2};
00072     enum EAxis {eAxisX=0, eAxisY=1, eAxisZ=2};
00073 
00074     #define EulerOrder(EAxis, EParity, ERepeat) (((EAxis) << 2) | (EParity) | (ERepeat))
00075 
00076     enum EOrder
00077     {
00078         eOrderXYZ = EulerOrder(eAxisX, eParityEven, eRepeatNo),
00079         eOrderXYX = EulerOrder(eAxisX, eParityEven, eRepeatYes),    //deprecated
00080         eOrderXZY = EulerOrder(eAxisX, eParityOdd, eRepeatNo),
00081         eOrderXZX = EulerOrder(eAxisX, eParityOdd, eRepeatYes),     //deprecated
00082         eOrderYZX = EulerOrder(eAxisY, eParityEven, eRepeatNo),
00083         eOrderYZY = EulerOrder(eAxisY, eParityEven, eRepeatYes),    //deprecated
00084         eOrderYXZ = EulerOrder(eAxisY, eParityOdd, eRepeatNo),
00085         eOrderYXY = EulerOrder(eAxisY, eParityOdd, eRepeatYes),     //deprecated
00086         eOrderZXY = EulerOrder(eAxisZ, eParityEven, eRepeatNo),
00087         eOrderZXZ = EulerOrder(eAxisZ, eParityEven, eRepeatYes),    //deprecated
00088         eOrderZYX = EulerOrder(eAxisZ, eParityOdd, eRepeatNo),
00089         eOrderZYZ = EulerOrder(eAxisZ, eParityOdd, eRepeatYes)      //deprecated
00090     };
00091 
00092     static const int AxisTableSize;
00093     static const int AxisTable[][3];
00094 };
00095 
00100 enum EFbxRotationOrder
00101 { 
00102     eEulerXYZ,
00103     eEulerXZY,
00104     eEulerYZX,
00105     eEulerYXZ,
00106     eEulerZXY,
00107     eEulerZYX,
00108     eSphericXYZ
00109 };
00110 
00112 enum EFbxQuatInterpMode
00113 {
00114     eQuatInterpOff,                 
00115     eQuatInterpClassic,             
00116     eQuatInterpSlerp,               
00117     eQuatInterpCubic,               
00118     eQuatInterpTangentDependent,    
00119     eQuatInterpCount                
00120 };
00121 
00122 extern FBXSDK_DLL const double FbxIdentityMatrix[4][4];
00123 extern FBXSDK_DLL const FbxVector4 FbxZeroVector4;
00124 
00125 inline float FbxFloor(const float x)
00126 {
00127     return float(floor(x));
00128 }
00129 
00130 inline double FbxFloor(const double x)
00131 {
00132     return floor(x);
00133 }
00134 
00135 inline float FbxCeil(const float x)
00136 {
00137     return float(ceil(x));
00138 }
00139 
00140 inline double FbxCeil(const double x)
00141 {
00142     return ceil(x);
00143 }
00144 
00145 template<class T> inline T FbxSign(const T x)
00146 {
00147     return (x < 0) ? T(-1) : T(1);
00148 }
00149 
00150 template<class T> inline T FbxRound(const T x)
00151 {
00152     T y = FbxFloor(x);
00153     return (x - y < T(0.5)) ? y : y + T(1);
00154 }
00155 
00156 inline FbxUChar FbxAbs(const FbxUChar x)
00157 {
00158     return x;
00159 }
00160 
00161 inline FbxUShort FbxAbs(const FbxUShort x)
00162 {
00163     return x;
00164 }
00165 
00166 inline FbxUInt FbxAbs(const FbxUInt x)
00167 {
00168     return x;
00169 }
00170 
00171 #ifndef FBXSDK_SYSTEM_IS_LP64
00172     inline FbxULong FbxAbs(const FbxULong x)
00173     {
00174         return x;
00175     }
00176 #endif
00177 
00178 inline FbxULongLong FbxAbs(const FbxULongLong x)
00179 {
00180     return x;
00181 }
00182 
00183 inline FbxFloat FbxAbs(const FbxFloat x)
00184 {
00185     return fabs(x);
00186 }
00187 
00188 inline FbxDouble FbxAbs(const FbxDouble x)
00189 {
00190     return fabs(x);
00191 }
00192 
00193 template<class T> inline T FbxAbs(const T x)
00194 {
00195     return (x >= 0) ? x : ((x > FbxMin(x)) ? -x : FbxMax(x));
00196 }
00197 
00198 template<class T> inline T FbxAbs(const T x, const T y)
00199 {
00200     T ax, ay;
00201     if( x )
00202     {
00203         if( ax = FbxAbs(x), y )
00204         {
00205             return ( ax > (ay = FbxAbs(y)) ) ? ay * FbxSqrt(T(1) + kSq(x / y)) : ax * FbxSqrt(T(1) + kSq(y / x));
00206         }
00207         else
00208         {
00209             return ax;
00210         }
00211     }
00212     else
00213     {
00214         return FbxAbs(y);
00215     }
00216 }
00217 
00218 template<class T> inline T FbxClamp(const T value, const T min, const T max)
00219 {
00220     return (value < min) ? min : ((value > max) ? max : value);
00221 }
00222 
00223 template<class T> inline bool FbxEqual(const T x, const T y, const T e=FBXSDK_TOLERANCE)
00224 {
00225     return FbxAbs(x - y) <= e;
00226 }
00227 
00228 inline bool FbxEqual(const FbxDouble2& x, const FbxDouble2& y, const double e=FBXSDK_TOLERANCE)
00229 {
00230     return ( FbxEqual(x.mData[0], y.mData[0], e) && FbxEqual(x.mData[1], y.mData[1], e) );
00231 }
00232 
00233 inline bool FbxEqual(const FbxDouble3& x, const FbxDouble3& y, const double e=FBXSDK_TOLERANCE)
00234 {
00235     return ( FbxEqual(x.mData[0], y.mData[0], e) && FbxEqual(x.mData[1], y.mData[1], e) && FbxEqual(x.mData[2], y.mData[2], e) );
00236 }
00237 
00238 inline bool FbxEqual(const FbxDouble4& x, const FbxDouble4& y, const double e=FBXSDK_TOLERANCE)
00239 {
00240     return ( FbxEqual(x.mData[0], y.mData[0], e) && FbxEqual(x.mData[1], y.mData[1], e) && FbxEqual(x.mData[2], y.mData[2], e) && FbxEqual(x.mData[3], y.mData[3], e) );
00241 }
00242 
00243 inline bool FbxEqual(const FbxDouble4x4& x, const FbxDouble4x4& y, const double e=FBXSDK_TOLERANCE)
00244 {
00245     return ( FbxEqual(x[0], y[0], e) && FbxEqual(x[1], y[1], e) && FbxEqual(x[2], y[2], e) && FbxEqual(x[3], y[3], e) );
00246 }
00247 
00248 inline bool FbxEqual(const FbxVector2& x, const FbxVector2& y, const double e=FBXSDK_TOLERANCE)
00249 {
00250     return ( FbxEqual(x.mData[0], y.mData[0], e) && FbxEqual(x.mData[1], y.mData[1], e) );
00251 }
00252 
00253 inline bool FbxEqual(const FbxVector4& x, const FbxVector4& y, const double e=FBXSDK_TOLERANCE)
00254 {
00255     return ( FbxEqual(x.mData[0], y.mData[0], e) && FbxEqual(x.mData[1], y.mData[1], e) && FbxEqual(x.mData[2], y.mData[2], e) && FbxEqual(x.mData[3], y.mData[3], e) );
00256 }
00257 
00258 inline bool FbxEqual(const FbxMatrix& x, const FbxMatrix& y, const double e=FBXSDK_TOLERANCE)
00259 {
00260     return ( FbxEqual(x[0], y[0], e) && FbxEqual(x[1], y[1], e) && FbxEqual(x[2], y[2], e) && FbxEqual(x[3], y[3], e) );
00261 }
00262 
00263 inline bool FbxEqual(const FbxAMatrix& x, const FbxAMatrix& y, const double e=FBXSDK_TOLERANCE)
00264 {
00265     return ( FbxEqual(x[0], y[0], e) && FbxEqual(x[1], y[1], e) && FbxEqual(x[2], y[2], e) && FbxEqual(x[3], y[3], e) );
00266 }
00267 
00268 template<class T> inline T FbxReciprocal(const T x)
00269 {
00270     return T(1) / x;
00271 }
00272 
00273 inline double FbxSqrt(const double x)
00274 {
00275     return sqrt(x);
00276 }
00277 
00278 inline float FbxSqrt(const float x)
00279 {
00280     return sqrtf(x);
00281 }
00282 
00283 template<class T> inline T FbxSqrt(const T x)
00284 {
00285     if( x > 1 )
00286     {
00287         T z, y = x >> 1; 
00288         do 
00289         { 
00290             z = y; 
00291             y = (y + (x / y)) >> 1; 
00292         }
00293         while(y < z); 
00294 
00295         return z;
00296     }
00297     else
00298     {
00299         return x;
00300     }
00301 }
00302 
00303 inline float FbxExp(const float x)
00304 {
00305     return expf(x);
00306 }
00307 
00308 inline double FbxExp(const double x)
00309 {
00310     return exp(x);
00311 }
00312 
00313 template<class T> inline T FbxPow(const T x, const T y)
00314 {
00315     return FbxExp(y * FbxLog(x));
00316 }
00317 
00318 inline float FbxLog(const float x)
00319 {
00320     return float(log(x));
00321 }
00322 
00323 inline double FbxLog(const double x)
00324 {
00325     return log(x);
00326 }
00327 
00328 template<class T> inline T FbxLog2(const T x)
00329 {
00330     return FbxLog(x) * T(FBXSDK_1_DIV_LN2);
00331 }
00332 
00333 inline float FbxSin(const float x)
00334 {
00335     return sinf(x);
00336 }
00337 
00338 inline double FbxSin(const double x)
00339 {
00340     return sin(x);
00341 }
00342 
00343 inline float FbxCos(const float x)
00344 {
00345     return cosf(x);
00346 }
00347 
00348 inline double FbxCos(const double x)
00349 {
00350     return cos(x);
00351 }
00352 
00353 inline float FbxTan(const float x)
00354 {
00355     return tanf(x);
00356 }
00357 
00358 inline double FbxTan(const double x)
00359 {
00360     return tan(x);
00361 }
00362 
00363 template<class T> inline T FbxCos(const T x);
00364 
00365 // *y = cos(x), sin(x)
00366 template<class T> inline T FbxSinCos(const T x, T* y)
00367 {
00368     return *y = FbxCos(x), FbxSin(x);
00369 }
00370 
00371 // *y = cos(x * pi/180), sin(x * pi/180)
00372 template<class T> inline T FbxSinCosd(const T x, T* y)
00373 {
00374     return FbxSinCos(T(x * FBXSDK_PI_DIV_180), y);
00375 }
00376 
00377 inline double FbxASin(const double x)
00378 {
00379     return asin(x);
00380 }
00381 
00382 template<class T> inline T FbxASind(const T x)
00383 {
00384     return FbxASin(x) * FBXSDK_180_DIV_PI;
00385 }
00386 
00387 template<class T> inline T FbxACosd(const T x)
00388 {
00389     return FbxACos(x) * FBXSDK_180_DIV_PI;
00390 }
00391 
00392 inline float FbxATan(const float x)
00393 {
00394     return atanf(x);
00395 }
00396 
00397 inline double FbxATan(const double x)
00398 {
00399     return atan(x);
00400 }
00401 
00402 template<class T> inline T FbxATand(const T x)
00403 {
00404     return FbxATan(x) * FBXSDK_180_DIV_PI;
00405 }
00406 
00407 inline float FbxATan(const float y, const float x)
00408 {
00409     return atan2f(y, x);
00410 }
00411 
00412 inline double FbxATan(const double y, const double x)
00413 {
00414     return atan2(y, x);
00415 }
00416 
00417 template<class T> inline T FbxATand(const T y, const T x)
00418 {
00419     return FbxATan(y, x) * FBXSDK_180_DIV_PI;
00420 }
00421 
00422 template<class T> inline T FbxNorm(const T x, const T y)
00423 {
00424     return FbxSqrt(x * x + y * y);
00425 }
00426 
00427 template<class T> inline T FbxNorm(const T x, const T y, const T z)
00428 {
00429     return FbxSqrt(x * x + y * y + z * z);
00430 }
00431 
00432 template<class T> inline T FbxNorm(const T w, const T x, const T y, const T z)
00433 {
00434     return FbxSqrt(w * w + x * x + y * y + z * z);
00435 }
00436 
00437 template<class T> inline T FbxHypot(const T x, const T y)
00438 {
00439     return FbxSqrt(x * x + y * y);
00440 }
00441 
00442 template<class T> inline T FbxHypot(const T x, const T y, const T z)
00443 {
00444     return FbxSqrt(x * x + y * y + z * z);
00445 }
00446 
00447 template<class T> inline T FbxHypot(const T w, const T x, const T y, const T z)
00448 {
00449     return FbxSqrt(w * w + x * x + y * y + z * z);
00450 }
00451 
00452 inline float FbxASin(const float x)
00453 {
00454     return asinf(x);
00455 }
00456 
00457 inline float FbxACos(const float x)
00458 {
00459     return acosf(x);
00460 }
00461 
00462 inline double FbxACos(const double x)
00463 {
00464     return acos(x);
00465 }
00466 
00467 template<class T> inline int FbxBitCount(const T x)
00468 {
00469     int n = 0;
00470     T c = x;
00471     while( c )
00472     {
00473         n += int(c & 1);
00474         c = (c >> 1);
00475     }
00476     return n;
00477 }
00478 
00479 template<class T> inline void FbxFixInfinite(T& x)
00480 {
00481     if( x != x || x > FbxMax(x) || x < -FbxMax(x) )
00482     {
00483         x = T(0);
00484     }
00485 }
00486 
00487 template<class T> inline T FbxExp(const T x);
00488 template<class T> inline T FbxLog(const T x);
00489 template<class T> inline T FbxSin(const T x);
00490 template<class T> inline T FbxASin(const T x);
00491 template<class T> inline T FbxACos(const T x);
00492 template<class T> inline T FbxATan(const T x);
00493 template<class T> inline T FbxATan(const T y, const T x);
00494 
00495 #include <fbxsdk/fbxsdk_nsend.h>
00496 
00497 #endif /* _FBXSDK_CORE_MATH_H_ */