fbxsdk/core/math/fbxmath.h Source File

fbxmath.h
Go to the documentation of this file.
1 /****************************************************************************************
2 
3  Copyright (C) 2014 Autodesk, Inc.
4  All rights reserved.
5 
6  Use of this software is subject to the terms of the Autodesk license agreement
7  provided at the time of installation or download, or which otherwise accompanies
8  this software in either electronic or hard copy form.
9 
10 ****************************************************************************************/
11 
13 #ifndef _FBXSDK_CORE_MATH_H_
14 #define _FBXSDK_CORE_MATH_H_
15 
16 #include <fbxsdk/fbxsdk_def.h>
17 
22 
23 //On Mac OS, cmath will include math.h and undef "isnan"
24 #if defined(FBXSDK_ENV_MAC)
25  #include <cmath>
26  extern "C" int isnan (double);
27 #endif
28 
29 #include <fbxsdk/fbxsdk_nsbegin.h>
30 
31 #if defined(FBXSDK_ENV_WIN)
32  #ifndef isnan
33  #define isnan _isnan
34  #endif
35  #ifndef finite
36  #define finite _finite
37  #endif
38 #endif
39 
40 //---------------------------------------------------------------------------------------
41 //Common Constants
42 #define FBXSDK_PI 3.1415926535897932384626433832795028841971693993751
43 #define FBXSDK_PI_DIV_2 1.5707963267948966192313216916397514420985846996875
44 #define FBXSDK_PI_DIV_180 0.017453292519943295769236907684886127134428718885417
45 #define FBXSDK_180_DIV_PI 57.295779513082320876798154814105170332405472466565
46 #define FBXSDK_1_DIV_LN2 1.4426950408889634073599246810018921374266459541530
47 
48 //---------------------------------------------------------------------------------------
49 //Unit Convertion Ratio
50 #define FBXSDK_DEG_TO_RAD FBXSDK_PI_DIV_180
51 #define FBXSDK_RAD_TO_DEG FBXSDK_180_DIV_PI
52 #define FBXSDK_IN_TO_CM 2.54
53 #define FBXSDK_MM_TO_CM 0.1
54 #define FBXSDK_CM_TO_IN 0.393700787
55 #define FBXSDK_IN_TO_MM 25.4
56 #define FBXSDK_MM_TO_IN 0.0393700787
57 #define FBXSDK_FT_TO_M 0.3048
58 #define FBXSDK_M_TO_FT 3.2808399
59 #define FBXSDK_YD_TO_FT 3
60 #define FBXSDK_FT_TO_YD 0.333333333
61 #define FBXSDK_KM_TO_MILE 0.621371192
62 #define FBXSDK_MILE_TO_KM 1.609344
63 #define FBXSDK_YD_TO_M 0.9144
64 #define FBXSDK_M_TO_YD 1.0936133
65 
66 //---------------------------------------------------------------------------------------
67 //Euler Definition
68 #define FBXSDK_EULER_DEGENERATE (16.0*FBXSDK_FLOAT_EPSILON)
69 
71 {
72 public:
73  enum EAxis {eAxisX=0, eAxisY=1, eAxisZ=2};
74 
75  enum EOrder
76  {
83  eOrderSphericXYZ
84  };
85 
86  static bool IsParityOdd(EOrder pOrder);
87  static bool IsRepeat(EOrder pOrder);
88 
89  static const int AxisTableSize;
90  static const int AxisTable[][3];
91 };
92 
98 #define EFbxRotationOrder FbxEuler::EOrder
99 #define eEulerXYZ FbxEuler::eOrderXYZ
100 #define eEulerXZY FbxEuler::eOrderXZY
101 #define eEulerYZX FbxEuler::eOrderYZX
102 #define eEulerYXZ FbxEuler::eOrderYXZ
103 #define eEulerZXY FbxEuler::eOrderZXY
104 #define eEulerZYX FbxEuler::eOrderZYX
105 #define eSphericXYZ FbxEuler::eOrderSphericXYZ
106 
107 
108 
111 {
118 };
119 
120 extern FBXSDK_DLL const FbxDouble FbxIdentityMatrix[4][4];
122 
123 inline float FbxFloor(const float x)
124 {
125  return float(floor(x));
126 }
127 
128 inline double FbxFloor(const double x)
129 {
130  return floor(x);
131 }
132 
133 inline float FbxCeil(const float x)
134 {
135  return float(ceil(x));
136 }
137 
138 inline double FbxCeil(const double x)
139 {
140  return ceil(x);
141 }
142 
143 template<class T> inline T FbxSign(const T x)
144 {
145  return (x < 0) ? T(-1) : T(1);
146 }
147 
148 template<class T> inline T FbxRound(const T x)
149 {
150  T y = FbxFloor(x);
151  return (x - y < T(0.5)) ? y : y + T(1);
152 }
153 
154 inline FbxUChar FbxAbs(const FbxUChar x)
155 {
156  return x;
157 }
158 
159 inline FbxUShort FbxAbs(const FbxUShort x)
160 {
161  return x;
162 }
163 
164 inline FbxUInt FbxAbs(const FbxUInt x)
165 {
166  return x;
167 }
168 
169 #ifndef FBXSDK_SYSTEM_IS_LP64
170  inline FbxULong FbxAbs(const FbxULong x)
171  {
172  return x;
173  }
174 #endif
175 
177 {
178  return x;
179 }
180 
181 inline FbxFloat FbxAbs(const FbxFloat x)
182 {
183  return fabs(x);
184 }
185 
186 inline FbxDouble FbxAbs(const FbxDouble x)
187 {
188  return fabs(x);
189 }
190 
191 template<class T> inline T FbxAbs(const T x)
192 {
193  return (x >= 0) ? x : ((x > FbxMin(x)) ? -x : FbxMax(x));
194 }
195 
196 template<class T> inline T FbxClamp(const T value, const T min, const T max)
197 {
198  return (value < min) ? min : ((value > max) ? max : value);
199 }
200 
201 template<class T> inline bool FbxEqual(const T x, const T y, const T e=(T)FBXSDK_TOLERANCE)
202 {
203  return FbxAbs(x - y) <= e;
204 }
205 
206 inline bool FbxEqual(const FbxDouble2& x, const FbxDouble2& y, const double e=FBXSDK_TOLERANCE)
207 {
208  return ( FbxEqual(x.mData[0], y.mData[0], e) && FbxEqual(x.mData[1], y.mData[1], e) );
209 }
210 
211 inline bool FbxEqual(const FbxDouble3& x, const FbxDouble3& y, const double e=FBXSDK_TOLERANCE)
212 {
213  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) );
214 }
215 
216 inline bool FbxEqual(const FbxDouble4& x, const FbxDouble4& y, const double e=FBXSDK_TOLERANCE)
217 {
218  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) );
219 }
220 
221 inline bool FbxEqual(const FbxDouble4x4& x, const FbxDouble4x4& y, const double e=FBXSDK_TOLERANCE)
222 {
223  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) );
224 }
225 
226 inline bool FbxEqual(const FbxVector2& x, const FbxVector2& y, const double e=FBXSDK_TOLERANCE)
227 {
228  return ( FbxEqual(x.mData[0], y.mData[0], e) && FbxEqual(x.mData[1], y.mData[1], e) );
229 }
230 
231 inline bool FbxEqual(const FbxVector4& x, const FbxVector4& y, const double e=FBXSDK_TOLERANCE)
232 {
233  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) );
234 }
235 
236 inline bool FbxEqual(const FbxMatrix& x, const FbxMatrix& y, const double e=FBXSDK_TOLERANCE)
237 {
238  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) );
239 }
240 
241 inline bool FbxEqual(const FbxAMatrix& x, const FbxAMatrix& y, const double e=FBXSDK_TOLERANCE)
242 {
243  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) );
244 }
245 
246 inline FbxDouble FbxMod(const FbxFloat x, FbxFloat& i)
247 {
248  return modff(x, &i);
249 }
250 
251 inline FbxDouble FbxMod(const FbxDouble x, FbxDouble& i)
252 {
253  return modf(x, &i);
254 }
255 
256 inline FbxDouble FbxMod(const FbxFloat x)
257 {
258  FbxFloat i;
259  return modff(x, &i);
260 }
261 
262 inline FbxDouble FbxMod(const FbxDouble x)
263 {
264  FbxDouble i;
265  return modf(x, &i);
266 }
267 
268 template<class T> inline T FbxReciprocal(const T x)
269 {
270  return T(1) / x;
271 }
272 
273 inline double FbxSqrt(const double x)
274 {
275  return sqrt(x);
276 }
277 
278 inline float FbxSqrt(const float x)
279 {
280  return sqrtf(x);
281 }
282 
283 template<class T> inline T FbxSqrt(const T x)
284 {
285  if( x > 1 )
286  {
287  T z, y = x >> 1;
288  do
289  {
290  z = y;
291  y = (y + (x / y)) >> 1;
292  }
293  while(y < z);
294 
295  return z;
296  }
297  else
298  {
299  return x;
300  }
301 }
302 
303 inline float FbxExp(const float x)
304 {
305  return expf(x);
306 }
307 
308 inline double FbxExp(const double x)
309 {
310  return exp(x);
311 }
312 
313 inline float FbxLog(const float x)
314 {
315  return float(log(x));
316 }
317 
318 inline double FbxLog(const double x)
319 {
320  return log(x);
321 }
322 
323 template<class T> inline T FbxPow(const T x, const T y)
324 {
325  return (T)FbxExp(y * FbxLog((double)x));
326 }
327 
328 template<class T> inline T FbxLog2(const T x)
329 {
330  return (T)(FbxLog(x) * FBXSDK_1_DIV_LN2);
331 }
332 
333 inline float FbxSin(const float x)
334 {
335  return sinf(x);
336 }
337 
338 inline double FbxSin(const double x)
339 {
340  return sin(x);
341 }
342 
343 inline float FbxCos(const float x)
344 {
345  return cosf(x);
346 }
347 
348 inline double FbxCos(const double x)
349 {
350  return cos(x);
351 }
352 
353 inline float FbxTan(const float x)
354 {
355  return tanf(x);
356 }
357 
358 inline double FbxTan(const double x)
359 {
360  return tan(x);
361 }
362 
363 // *y = cos(x), sin(x)
364 template<class T> inline T FbxSinCos(const T x, T* y)
365 {
366  return *y = FbxCos(x), FbxSin(x);
367 }
368 
369 // *y = cos(x * pi/180), sin(x * pi/180)
370 template<class T> inline T FbxSinCosd(const T x, T* y)
371 {
372  return FbxSinCos(T(x * FBXSDK_PI_DIV_180), y);
373 }
374 
375 inline float FbxASin(const float x)
376 {
377  return asinf(x);
378 }
379 
380 inline double FbxASin(const double x)
381 {
382  return asin(x);
383 }
384 
385 template<class T> inline T FbxASind(const T x)
386 {
387  return (T)(FbxASin((double)x) * FBXSDK_180_DIV_PI);
388 }
389 
390 inline float FbxACos(const float x)
391 {
392  return acosf(x);
393 }
394 
395 inline double FbxACos(const double x)
396 {
397  return acos(x);
398 }
399 
400 template<class T> inline T FbxACosd(const T x)
401 {
402  return (T)(FbxACos(x) * FBXSDK_180_DIV_PI);
403 }
404 
405 inline float FbxATan(const float x)
406 {
407  return atanf(x);
408 }
409 
410 inline double FbxATan(const double x)
411 {
412  return atan(x);
413 }
414 
415 template<class T> inline T FbxATand(const T x)
416 {
417  return (T)(FbxATan(x) * FBXSDK_180_DIV_PI);
418 }
419 
420 inline float FbxATan(const float y, const float x)
421 {
422  return atan2f(y, x);
423 }
424 
425 inline double FbxATan(const double y, const double x)
426 {
427  return atan2(y, x);
428 }
429 
430 template<class T> inline T FbxATand(const T y, const T x)
431 {
432  return (T)(FbxATan(y, x) * FBXSDK_180_DIV_PI);
433 }
434 
435 template<class T> inline T FbxNorm(const T x, const T y)
436 {
437  return FbxSqrt(x * x + y * y);
438 }
439 
440 template<class T> inline T FbxNorm(const T x, const T y, const T z)
441 {
442  return FbxSqrt(x * x + y * y + z * z);
443 }
444 
445 template<class T> inline T FbxNorm(const T w, const T x, const T y, const T z)
446 {
447  return FbxSqrt(w * w + x * x + y * y + z * z);
448 }
449 
450 template<class T> inline T FbxHypot(const T x, const T y)
451 {
452  return FbxSqrt(x * x + y * y);
453 }
454 
455 template<class T> inline T FbxHypot(const T x, const T y, const T z)
456 {
457  return FbxSqrt(x * x + y * y + z * z);
458 }
459 
460 template<class T> inline T FbxHypot(const T w, const T x, const T y, const T z)
461 {
462  return FbxSqrt(w * w + x * x + y * y + z * z);
463 }
464 
465 inline FbxVector4 FbxRejection(const FbxVector4& a, const FbxVector4& b)
466 {
467  return a - b * (a.DotProduct(b) / b.DotProduct(b));
468 }
469 
470 template<class T> inline int FbxBitCount(const T x)
471 {
472  int n = 0;
473  T c = x;
474  while( c )
475  {
476  n += int(c & 1);
477  c = (c >> 1);
478  }
479  return n;
480 }
481 
482 template<class T> inline void FbxFixInfinite(T& x)
483 {
484  if( x != x || x > FbxMax(x) || x < -FbxMax(x) )
485  {
486  x = T(0);
487  }
488 }
489 
490 template<class T> inline T FbxExp(const T x);
491 template<class T> inline T FbxLog(const T x);
492 template<class T> inline T FbxSin(const T x);
493 template<class T> inline T FbxCos(const T x);
494 template<class T> inline T FbxASin(const T x);
495 template<class T> inline T FbxACos(const T x);
496 template<class T> inline T FbxATan(const T x);
497 template<class T> inline T FbxATan(const T y, const T x);
498 
499 #include <fbxsdk/fbxsdk_nsend.h>
500 
501 #endif /* _FBXSDK_CORE_MATH_H_ */
FbxUInt64 FbxULongLong
Definition: fbxtypes.h:93
unsigned int FbxUInt
Definition: fbxtypes.h:40
float FbxCos(const float x)
Definition: fbxmath.h:343
bool FbxEqual(const T x, const T y, const T e=(T) FBXSDK_TOLERANCE)
Definition: fbxmath.h:201
#define FBXSDK_1_DIV_LN2
1 divided by LogN2
Definition: fbxmath.h:46
FBX SDK environment definition.
float FbxLog(const float x)
Definition: fbxmath.h:313
#define FBXSDK_TOLERANCE
Definition: fbxtypes.h:128
Mix between Slerp and cubic interpolation, depending on the specified tangents for each key...
Definition: fbxmath.h:116
FbxDouble FbxMod(const FbxFloat x, FbxFloat &i)
Definition: fbxmath.h:246
int FbxBitCount(const T x)
Definition: fbxmath.h:470
#define FBXSDK_180_DIV_PI
180 divided by PI
Definition: fbxmath.h:45
FBXSDK_DLL const FbxDouble FbxIdentityMatrix[4][4]
EFbxQuatInterpMode
Quaternion interpolation modes.
Definition: fbxmath.h:110
T FbxClamp(const T value, const T min, const T max)
Definition: fbxmath.h:196
float FbxExp(const float x)
Definition: fbxmath.h:303
FbxVector4 FbxRejection(const FbxVector4 &a, const FbxVector4 &b)
Definition: fbxmath.h:465
void FbxFixInfinite(T &x)
Definition: fbxmath.h:482
Spherical linear interpolation.
Definition: fbxmath.h:114
double DotProduct(const FbxVector4 &pVector) const
Calculate the dot product of two vectors.
T FbxRound(const T x)
Definition: fbxmath.h:148
unsigned short FbxUShort
Definition: fbxtypes.h:38
Do not evaluate using quaternion interpolation.
Definition: fbxmath.h:112
unsigned char FbxUChar
Definition: fbxtypes.h:36
T FbxReciprocal(const T x)
Definition: fbxmath.h:268
T FbxPow(const T x, const T y)
Definition: fbxmath.h:323
float FbxFloor(const float x)
Definition: fbxmath.h:123
double FbxDouble
Definition: fbxtypes.h:42
FbxUChar FbxAbs(const FbxUChar x)
Definition: fbxmath.h:154
T FbxSinCos(const T x, T *y)
Definition: fbxmath.h:364
double FbxSqrt(const double x)
Definition: fbxmath.h:273
float FbxATan(const float x)
Definition: fbxmath.h:405
Cubic interpolation.
Definition: fbxmath.h:115
T FbxSign(const T x)
Definition: fbxmath.h:143
T FbxACosd(const T x)
Definition: fbxmath.h:400
Legacy quaternion interpolation mode.
Definition: fbxmath.h:113
const FbxChar FbxMin(const FbxChar)
Definition: fbxtypes.h:154
float FbxFloat
Definition: fbxtypes.h:41
float FbxACos(const float x)
Definition: fbxmath.h:390
T FbxSinCosd(const T x, T *y)
Definition: fbxmath.h:370
T FbxHypot(const T x, const T y)
Definition: fbxmath.h:450
T FbxASind(const T x)
Definition: fbxmath.h:385
float FbxCeil(const float x)
Definition: fbxmath.h:133
Number of quaternion interpolation modes.
Definition: fbxmath.h:117
float FbxTan(const float x)
Definition: fbxmath.h:353
FBXSDK_DLL const FbxVector4 FbxZeroVector4
A four double mathematic vector class.
Definition: fbxvector4.h:25
FBX SDK affine matrix class.
const FbxChar FbxMax(const FbxChar)
Definition: fbxtypes.h:165
T FbxNorm(const T x, const T y)
Definition: fbxmath.h:435
#define FBXSDK_DLL
Definition: fbxarch.h:170
float FbxSin(const float x)
Definition: fbxmath.h:333
A two double mathematic vector class.
Definition: fbxvector2.h:23
T FbxLog2(const T x)
Definition: fbxmath.h:328
static const int AxisTableSize
Definition: fbxmath.h:89
FBX SDK basic 4x4 double matrix class.
Definition: fbxmatrix.h:27
float FbxASin(const float x)
Definition: fbxmath.h:375
#define FBXSDK_PI_DIV_180
PI divived by 180.
Definition: fbxmath.h:44
unsigned long FbxULong
Definition: fbxtypes.h:90
T FbxATand(const T x)
Definition: fbxmath.h:415