fbxmath.h

Go to the documentation of this file.
00001 /****************************************************************************************
00002  
00003    Copyright (C) 2012 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 
00124 inline float FbxFloor(const float& x)
00125 {
00126     return float(floor(x));
00127 }
00128 
00129 inline double FbxFloor(const double& x)
00130 {
00131     return floor(x);
00132 }
00133 
00134 inline float FbxCeil(const float& x)
00135 {
00136     return float(ceil(x));
00137 }
00138 
00139 inline double FbxCeil(const double& x)
00140 {
00141     return ceil(x);
00142 }
00143 
00144 template<class T> inline T FbxSign(const T& x)
00145 {
00146     return (x < 0) ? T(-1) : T(1);
00147 }
00148 
00149 template<class T> inline T FbxRound(const T& x)
00150 {
00151     T y = FbxFloor(x);
00152     return (x - y < T(0.5)) ? y : y + T(1);
00153 }
00154 
00155 inline const FbxUChar FbxAbs(const FbxUChar& x)
00156 {
00157     return x;
00158 }
00159 
00160 inline const FbxUShort FbxAbs(const FbxUShort& x)
00161 {
00162     return x;
00163 }
00164 
00165 inline const FbxUInt FbxAbs(const FbxUInt& x)
00166 {
00167     return x;
00168 }
00169 
00170 #ifndef FBXSDK_SYSTEM_IS_LP64
00171     inline const FbxULong FbxAbs(const FbxULong& x)
00172     {
00173         return x;
00174     }
00175 #endif
00176 
00177 inline const FbxULongLong FbxAbs(const FbxULongLong x)
00178 {
00179     return x;
00180 }
00181 
00182 inline const FbxFloat FbxAbs(const FbxFloat& x)
00183 {
00184     return fabs(x);
00185 }
00186 
00187 inline const FbxDouble FbxAbs(const FbxDouble& x)
00188 {
00189     return fabs(x);
00190 }
00191 
00192 template<class T> inline const T FbxAbs(const T& x)
00193 {
00194     return (x >= 0) ? x : ((x > FbxMin(x)) ? -x : FbxMax(x));
00195 }
00196 
00197 template<class T> inline T FbxAbs(const T& x, const T& y)
00198 {
00199     T ax, ay;
00200     if( x )
00201     {
00202         if( ax = FbxAbs(x), y )
00203         {
00204             return ( ax > (ay = FbxAbs(y)) ) ? ay * FbxSqrt(T(1) + kSq(x / y)) : ax * FbxSqrt(T(1) + kSq(y / x));
00205         }
00206         else
00207         {
00208             return ax;
00209         }
00210     }
00211     else
00212     {
00213         return FbxAbs(y);
00214     }
00215 }
00216 
00217 template<class T> inline const T FbxClamp(const T& value, const T& min, const T& max)
00218 {
00219     return (value < min) ? min : ((value > max) ? max : value);
00220 }
00221 
00222 template<class T> inline bool FbxEqual(const T& x, const T& y, const T& e=FBXSDK_TOLERANCE)
00223 {
00224     return FbxAbs(x - y) <= e;
00225 }
00226 
00227 inline bool FbxEqual(const FbxDouble2& x, const FbxDouble2& y, const double& e=FBXSDK_TOLERANCE)
00228 {
00229     return ( FbxEqual(x.mData[0], y.mData[0], e) && FbxEqual(x.mData[1], y.mData[1], e) );
00230 }
00231 
00232 inline bool FbxEqual(const FbxDouble3& x, const FbxDouble3& y, const double& e=FBXSDK_TOLERANCE)
00233 {
00234     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) );
00235 }
00236 
00237 inline bool FbxEqual(const FbxDouble4& x, const FbxDouble4& y, const double& e=FBXSDK_TOLERANCE)
00238 {
00239     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) );
00240 }
00241 
00242 inline bool FbxEqual(const FbxDouble4x4& x, const FbxDouble4x4& y, const double& e=FBXSDK_TOLERANCE)
00243 {
00244     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) );
00245 }
00246 
00247 inline bool FbxEqual(const FbxVector2& x, const FbxVector2& y, const double& e=FBXSDK_TOLERANCE)
00248 {
00249     return ( FbxEqual(x.mData[0], y.mData[0], e) && FbxEqual(x.mData[1], y.mData[1], e) );
00250 }
00251 
00252 inline bool FbxEqual(const FbxVector4& x, const FbxVector4& y, const double& e=FBXSDK_TOLERANCE)
00253 {
00254     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) );
00255 }
00256 
00257 inline bool FbxEqual(const FbxMatrix& x, const FbxMatrix& y, const double& e=FBXSDK_TOLERANCE)
00258 {
00259     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) );
00260 }
00261 
00262 inline bool FbxEqual(const FbxAMatrix& x, const FbxAMatrix& y, const double& e=FBXSDK_TOLERANCE)
00263 {
00264     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) );
00265 }
00266 
00267 template<class T> inline T FbxReciprocal(const T& x)
00268 {
00269     return T(1) / x;
00270 }
00271 
00272 inline double FbxSqrt(const double& x)
00273 {
00274     return sqrt(x);
00275 }
00276 
00277 inline float FbxSqrt(const float& x)
00278 {
00279     return sqrtf(x);
00280 }
00281 
00282 template<class T> inline T FbxSqrt(const T& x)
00283 {
00284     if( x > 1 )
00285     {
00286         T z, y = x >> 1; 
00287         do 
00288         { 
00289             z = y; 
00290             y = (y + (x / y)) >> 1; 
00291         }
00292         while(y < z); 
00293 
00294         return z;
00295     }
00296     else
00297     {
00298         return x;
00299     }
00300 }
00301 
00302 inline float FbxExp(const float& x)
00303 {
00304     return expf(x);
00305 }
00306 
00307 inline double FbxExp(const double& x)
00308 {
00309     return exp(x);
00310 }
00311 
00312 template<class T> inline T FbxPow(const T& x, const T& y)
00313 {
00314     return FbxExp(y * FbxLog(x));
00315 }
00316 
00317 inline float FbxLog(const float& x)
00318 {
00319     return float(log(x));
00320 }
00321 
00322 inline double FbxLog(const double& x)
00323 {
00324     return log(x);
00325 }
00326 
00327 template<class T> inline T FbxLog2(const T& x)
00328 {
00329     return FbxLog(x) * T(FBXSDK_1_DIV_LN2);
00330 }
00331 
00332 inline float FbxSin(const float& x)
00333 {
00334     return sinf(x);
00335 }
00336 
00337 inline double FbxSin(const double& x)
00338 {
00339     return sin(x);
00340 }
00341 
00342 inline float FbxCos(const float& x)
00343 {
00344     return cosf(x);
00345 }
00346 
00347 inline double FbxCos(const double& x)
00348 {
00349     return cos(x);
00350 }
00351 
00352 template<class T> inline T FbxCos(const T& x);
00353 
00354 // *y = cos(x), sin(x)
00355 template<class T> inline T FbxSinCos(const T& x, T* y)
00356 {
00357     return *y = FbxCos(x), FbxSin(x);
00358 }
00359 
00360 // *y = cos(x * pi/180), sin(x * pi/180)
00361 template<class T> inline T FbxSinCosd(const T& x, T* y)
00362 {
00363     return FbxSinCos(T(x * FBXSDK_PI_DIV_180), y);
00364 }
00365 
00366 inline double FbxASin(const double& x)
00367 {
00368     return asin(x);
00369 }
00370 
00371 template<class T> inline T FbxASind(const T& x)
00372 {
00373     return FbxASin(x) * FBXSDK_180_DIV_PI;
00374 }
00375 
00376 template<class T> inline T FbxACosd(const T& x)
00377 {
00378     return FbxACos(x) * FBXSDK_180_DIV_PI;
00379 }
00380 
00381 inline float FbxATan(const float& x)
00382 {
00383     return atanf(x);
00384 }
00385 
00386 inline double FbxATan(const double& x)
00387 {
00388     return atan(x);
00389 }
00390 
00391 template<class T> inline T FbxATand(const T& x)
00392 {
00393     return FbxATan(x) * FBXSDK_180_DIV_PI;
00394 }
00395 
00396 inline float FbxATan(const float& y, const float& x)
00397 {
00398     return atan2f(y, x);
00399 }
00400 
00401 inline double FbxATan(const double& y, const double& x)
00402 {
00403     return atan2(y, x);
00404 }
00405 
00406 template<class T> inline T FbxATand(const T& y, const T& x)
00407 {
00408     return FbxATan(y, x) * FBXSDK_180_DIV_PI;
00409 }
00410 
00411 template<class T> inline T FbxNorm(const T& x, const T& y)
00412 {
00413     return FbxSqrt(x * x + y * y);
00414 }
00415 
00416 template<class T> inline T FbxNorm(const T& x, const T& y, const T& z)
00417 {
00418     return FbxSqrt(x * x + y * y + z * z);
00419 }
00420 
00421 template<class T> inline T FbxNorm(const T& w, const T& x, const T& y, const T& z)
00422 {
00423     return FbxSqrt(w * w + x * x + y * y + z * z);
00424 }
00425 
00426 template<class T> inline T FbxHypot(const T& x, const T& y)
00427 {
00428     return FbxSqrt(x * x + y * y);
00429 }
00430 
00431 template<class T> inline T FbxHypot(const T& x, const T& y, const T& z)
00432 {
00433     return FbxSqrt(x * x + y * y + z * z);
00434 }
00435 
00436 template<class T> inline T FbxHypot(const T& w, const T& x, const T& y, const T& z)
00437 {
00438     return FbxSqrt(w * w + x * x + y * y + z * z);
00439 }
00440 
00441 inline float FbxASin(const float& x)
00442 {
00443     return asinf(x);
00444 }
00445 
00446 inline float FbxACos(const float& x)
00447 {
00448     return acosf(x);
00449 }
00450 
00451 inline double FbxACos(const double& x)
00452 {
00453     return acos(x);
00454 }
00455 
00456 template<class T> inline const int FbxBitCount(const T& x)
00457 {
00458     int n = 0;
00459     T c = x;
00460     while( c )
00461     {
00462         n += int(c & 1);
00463         c = (c >> 1);
00464     }
00465     return n;
00466 }
00467 
00468 template<class T> inline void FbxFixInfinite(T& x)
00469 {
00470     if( x != x || x > FbxMax(x) || x < -FbxMax(x) )
00471     {
00472         x = T(0);
00473     }
00474 }
00475 
00476 template<class T> inline T FbxExp(const T& x);
00477 template<class T> inline T FbxLog(const T& x);
00478 template<class T> inline T FbxSin(const T& x);
00479 template<class T> inline T FbxASin(const T& x);
00480 template<class T> inline T FbxACos(const T& x);
00481 template<class T> inline T FbxATan(const T& x);
00482 template<class T> inline T FbxATan(const T& y, const T& x);
00483 
00484 #include <fbxsdk/fbxsdk_nsend.h>
00485 
00486 #endif /* _FBXSDK_CORE_MATH_H_ */