RTFX  0.3
Real time special effects collaborative visualization and production library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
RTFXMath.h
1 #ifndef __RTFX_MATH__
2 #define __RTFX_MATH__
3 
4 /*
5  Lesley Northam
6  RTFXMath
7 
8  October 22, 2011.
9 */
10 #pragma warning(disable: 4996)
11 
12 #include <iostream>
13 #include <limits>
14 
15 #include "algebra.hpp"
16 #include "RTFXEnums.h"
17 #include "RTFXDebug.h"
18 #include "RTFXError.h"
19 
20 namespace RTFX
21 {
22  const AXISFRAME RTFX_NATIVE_FRAME = XFORWARD_YRIGHT_ZUP;
23  const double RTFXPI = 3.14159265358979;
24 
25  const Matrix4x4 I(1, 0, 0, 0, 1, 0, 0, 0, 1);
26 
27  const Matrix4x4 frameMatrices[] = {
28 
29  Matrix4x4( 0, 0, 1, 1, 0, 0, 0, 1, 0 ), // Unity Frame: XRIGHT_YUP_ZFORWARD VERIFIED CORRECT
30  Matrix4x4( 0, 0, -1, 1, 0, 0, 0, 1, 0 ), // Houdini Frame: XRIGHT_YUP_ZBACKWARD VERIFIED CORRECT
31  Matrix4x4( 0, 0, -1, -1, 0, 0, 0, -1, 0 ),
32  Matrix4x4( 0, 0, -1, -1, 0, 0, 0, 1, 0 ),
33  Matrix4x4( 0, 0, -1, 0, 1, 0, -1, 0, 0 ),
34  Matrix4x4( 0, 0, -1, 0, 1, 0, 1, 0, 0 ),
35  Matrix4x4( 0, 0, -1, 0, -1, 0, -1, 0, 0 ),
36  Matrix4x4( 0, 0, -1, 0, -1, 0, 1, 0, 0 ),
37 
38  Matrix4x4( 0, 0, 1, 1, 0, 0, 0, -1, 0 ),
39  Matrix4x4( 0, 0, 1, 1, 0, 0, 0, 1, 0 ),
40  Matrix4x4( 0, 0, 1, -1, 0, 0, 0, -1, 0 ),
41  Matrix4x4( 0, 0, 1, -1, 0, 0, 0, 1, 0 ),
42  Matrix4x4( 0, 0, 1, 0, 1, 0, -1, 0, 0 ),
43  Matrix4x4( 0, 0, 1, 0, 1, 0, 1, 0, 0 ),
44  Matrix4x4( 0, 0, 1, 0, -1, 0, -1, 0, 0 ),
45  Matrix4x4( 0, 0, 1, 0, -1, 0, 1, 0, 0 ),
46 
47  Matrix4x4( 1, 0, 0, 0, 0, 1, 0, 1, 0 ),
48  Matrix4x4( 1, 0, 0, 0, 0, -1, 0, 1, 0 ),
49  Matrix4x4( 1, 0, 0, 0, 0, 1, 0, -1, 0 ),
50  Matrix4x4( 1, 0, 0, 0, 0, -1, 0, -1, 0 ),
51  Matrix4x4( 1, 0, 0, 0, 1, 0, 0, 0, 1 ), // Default Frame: XFORWARD_YRIGHT_ZUP VERIFIED CORRECT
52  Matrix4x4( 1, 0, 0, 0, 1, 0, 0, 0, -1 ),
53  Matrix4x4( 1, 0, 0, 0, -1, 0, 0, 0, 1 ),
54  Matrix4x4( 1, 0, 0, 0, -1, 0, 0, 0, -1 ),
55 
56  Matrix4x4( -1, 0, 0, 0, 0, 1, 0, 1, 0 ),
57  Matrix4x4( -1, 0, 0, 0, 0, -1, 0, 1, 0 ),
58  Matrix4x4( -1, 0, 0, 0, 0, 1, 0, -1, 0 ),
59  Matrix4x4( -1, 0, 0, 0, 0, -1, 0, -1, 0 ),
60  Matrix4x4( -1, 0, 0, 0, 1, 0, 0, 0, 1 ),
61  Matrix4x4( -1, 0, 0, 0, 1, 0, 0, 0, -1 ),
62  Matrix4x4( -1, 0, 0, 0, -1, 0, 0, 0, 1 ),
63  Matrix4x4( -1, 0, 0, 0, -1, 0, 0, 0, -1 ),
64 
65  Matrix4x4( 0, 1, 0, 0, 0, 1, 1, 0, 0 ),
66  Matrix4x4( 0, 1, 0, 0, 0, 1, -1, 0, 0 ),
67  Matrix4x4( 0, 1, 0, 0, 0, -1, 1, 0, 0 ),
68  Matrix4x4( 0, 1, 0, 0, 0, -1, -1, 0, 0 ),
69  Matrix4x4( 0, 1, 0, 1, 0, 0, 0, 0, 1 ),
70  Matrix4x4( 0, 1, 0, 1, 0, 0, 0, 0, -1 ),
71  Matrix4x4( 0, 1, 0, -1, 0, 0, 0, 0, 1 ),
72  Matrix4x4( 0, 1, 0, -1, 0, 0, 0, 0, -1 ),
73 
74  Matrix4x4( 0, -1, 0, 0, 0, 1, 1, 0, 0 ),
75  Matrix4x4( 0, -1, 0, 0, 0, 1, -1, 0, 0 ),
76  Matrix4x4( 0, -1, 0, 0, 0, -1, 1, 0, 0 ),
77  Matrix4x4( 0, -1, 0, 0, 0, -1, -1, 0, 0 ),
78  Matrix4x4( 0, -1, 0, 1, 0, 0, 0, 0, 1 ),
79  Matrix4x4( 0, -1, 0, 1, 0, 0, 0, 0, -1 ),
80  Matrix4x4( 0, -1, 0, -1, 0, 0, 0, 0, 1 ),
81  Matrix4x4( 0, -1, 0, -1, 0, 0, 0, 0, -1 ),
82  };
83 
86  class RTFXAPI RTFXMath
87  {
88  public:
90  static Matrix4x4 GetFrameTransformMatrix( AXISFRAME _frameA, AXISFRAME _frameB )
91  {
92  // if the two frames are equal, do nothing
93  if ( _frameA == _frameB ) return I;
94 
95  Matrix4x4 t_ = I;
96 
97  // if the second frame is RTFXNative
98  if ( _frameB == RTFX_NATIVE_FRAME )
99  t_ = frameMatrices[ _frameA ];
100 
101  else if ( _frameA == RTFX_NATIVE_FRAME )
102  {
103  t_ = frameMatrices[ _frameB ];
104  t_ = t_.invert();
105  }
106  else
107  {
108  // convert from frameA to RTFX_NATIVE_FRAME
109  Matrix4x4 A_ = frameMatrices[ _frameA ];
110 
111  // convert from RTFX_NATIVE_FRAME to frameB
112  Matrix4x4 B_ = frameMatrices[ _frameB ];
113  B_ = B_.invert();
114 
115  t_ = B_ * A_;
116  }
117 
118  return t_;
119  }
120 
121 
122  static void ExtractEulerRotationsFromMatrix( Matrix4x4 _m, float &_x, float &_y, float &_z );
123  static Point3D ConvertPointToNative( AXISFRAME _orig, Point3D _point );
124  static Point3D ConvertPointFromNativeToOther( AXISFRAME _other, Point3D _point );
125  static float ToRadians( float _angle );
126  static float ToDegrees( float _angle );
127  static Matrix4x4 RotateX( float _angle );
128  static Matrix4x4 RotateY( float _angle );
129  static Matrix4x4 RotateZ( float _angle );
130  };
131 
132 }
133 
134 #endif