CATCH
 All Classes Functions
CatUtil.h
1 #pragma once
2 
3 #include <cmath>
4 #include <string>
5 #include <iostream>
6 
7 #pragma region Constants
8 
9 #define EXECUTION_FREQ__HZ 120
10 #define EXECUTION_TIME__SEC (1.0/EXECUTION_FREQ__HZ)
11 //#define USE_SPACE_MOUSE
12 
13 #pragma endregion
14 
15 #define print_err(libName, message) std::cerr << "[ERROR] " << libName << "(" << __LINE__ << "): " << message << std::endl;
16 #define print_msg(libName, message) std::cerr << "[INFO] " << libName << ": " << message << std::endl;
17 
18 typedef
19 enum CAT_RESULT
20  {
21  CAT_RESULT_OK = 0,
22  CAT_RESULT_ERROR = ( CAT_RESULT_OK + 1 ),
23  CAT_RESULT_FILE_NOT_FOUND = ( CAT_RESULT_OK + 2 ),
24  CAT_RESULT_ABORT = ( CAT_RESULT_OK + 3 )
25  } CAT_RESULT;
26 
44 inline void invertTransform(double inM[16], double outM[16])
45 {
46  //translation from in matrix
47  double x = inM[12];
48  double y = inM[13];
49  double z = inM[14];
50 
51 
52  //transpose rotation matrix and insert
53 
54  outM[0] = inM[0];
55  outM[4] = inM[1];
56  outM[8] = inM[2];
57  //0
58  outM[1] = inM[4];
59  outM[5] = inM[5];
60  outM[9] = inM[6];
61  //0
62  outM[2] = inM[8];
63  outM[6] = inM[9];
64  outM[10] = inM[10];
65  //0
66 
67  //calculate inv rotation * translation as translation component
68  outM[12] = -(outM[0] * x + outM[1] * y + outM[2] * z);
69  outM[13] = -(outM[4] * x + outM[5] * y + outM[6] * z);
70  outM[14] = -(outM[8] * x + outM[9] * y + outM[10] * z);
71 
72  //set homogenous values
73  outM[3] = 0;
74  outM[7] = 0;
75  outM[11] = 0;
76  outM[15] = 1;
77 }
78 
79 
97 inline void multiplyTransforms(double l[16], double r[16], double outM[16])
98 {
99  outM[0] = l[0]*r[0] + l[1]*r[4] + l[2]*r[8] + l[12]*r[3];
100  outM[1] = l[0]*r[1] + l[1]*r[5] + l[2]*r[9] + l[12]*r[7];
101  outM[2] = l[0]*r[2] + l[1]*r[6] + l[2]*r[10] + l[12]*r[11];
102  outM[12] = l[0]*r[12] + l[1]*r[13] + l[2]*r[14] + l[12]*r[15];
103  outM[4] = l[4]*r[0] + l[5]*r[4] + l[6]*r[8] + l[13]*r[3];
104  outM[5] = l[4]*r[1] + l[5]*r[5] + l[6]*r[9] + l[13]*r[7];
105  outM[6] = l[4]*r[2] + l[5]*r[6] + l[6]*r[10] + l[13]*r[11];
106  outM[13] = l[4]*r[12] + l[5]*r[13] + l[6]*r[14] + l[13]*r[15];
107  outM[8] = l[8]*r[0] + l[9]*r[4] + l[10]*r[8] + l[14]*r[3];
108  outM[9] = l[8]*r[1] + l[9]*r[5] + l[10]*r[9] + l[14]*r[7];
109  outM[10] = l[8]*r[2] + l[9]*r[6] + l[10]*r[10] + l[14]*r[11];
110  outM[14] = l[8]*r[12] + l[9]*r[13] + l[10]*r[14] + l[14]*r[15];
111  outM[3] = l[3]*r[0] + l[7]*r[4] + l[11]*r[8] + l[15]*r[3];
112  outM[7] = l[3]*r[1] + l[7]*r[5] + l[11]*r[9] + l[15]*r[7];
113  outM[11] = l[3]*r[2] + l[7]*r[6] + l[11]*r[10] + l[15]*r[11];
114  outM[15] = l[3]*r[12] + l[7]*r[13] + l[11]*r[14] + l[15]*r[15];
115 }
116 
117 
118 // Makes a 3 X 4 into a homogenous 4x4
119 //
120 // if the input matrix is:
121 //
122 // 0 1 2| 9
123 // 3 4 5| 10 indices 9, 10, 11 are translation
124 // 5 6 7| 11 0 - 7 is row major rotation matrix
125 //
126 // the output matrix becomes:
127 //
128 // 0 1 2| 12
129 // 4 5 6| 13 indices 3, 7, 11 are set to 0
130 // 8 9 10| 14 indices 12, 13, 14 are translation
131 // 3 7 11 15
132 inline void extendTransform(double inM[12], double outM[16])
133 {
134  int col =-1;
135  for(int i = 0; i < 12; i++)
136  {
137  if(i % 3 == 0)
138  {
139  col+=1;
140  }
141  outM[i+col] = inM[i];
142  }
143 
144  //set homogenous values
145  outM[3] = 0;
146  outM[7] = 0;
147  outM[11] = 0;
148  outM[15] = 1;
149 }
150 
151 inline void transposeRotation(double inM[12], double outM[12])
152 {
153  outM[0] = inM[0];
154  outM[1] = inM[3];
155  outM[2] = inM[6];
156  outM[3] = inM[1];
157  outM[4] = inM[4];
158  outM[5] = inM[7];
159  outM[6] = inM[2];
160  outM[7] = inM[5];
161  outM[8] = inM[8];
162  outM[9] = inM[9];
163  outM[10] = inM[10];
164  outM[11] = inM[11];
165 }
166 
167 inline void transposeRotation4x4(double inM[16], double outM[16])
168 {
169  outM[0] = inM[0];
170  outM[1] = inM[4];
171  outM[2] = inM[8];
172 
173  outM[4] = inM[1];
174  outM[5] = inM[5];
175  outM[6] = inM[9];
176 
177  outM[8] = inM[2];
178  outM[9] = inM[6];
179  outM[10] = inM[10];
180 
181  //this part of the matrix should be zero
182  outM[3] = inM[11];
183  outM[7] = inM[7];
184  outM[11] = inM[3];
185 
186  //keep translation the same
187  outM[12] = inM[12];
188  outM[13] = inM[13];
189  outM[14] = inM[14];
190  outM[15] = inM[15];
191 }
192 
193 //makes a 4x4 homogenous transform into a 3x4 row major matrix
194 //
195 // if the input matrix is:
196 //
197 // 0 1 2| 12
198 // 4 5 6| 13 where 3 7 11 are 0
199 // 8 9 10| 14 0 - 10 is row major rotation matrix
200 // 3 7 11 15 and 12, 13, 14 are translation
201 
202 // the output matrix becomes
203 // 0 1 2| 9
204 // 3 4 5| 10 ( 9, 10, 11 are the translation)
205 // 5 6 7| 11 0 - 7 is row major rotation matrix
206 //
207 //
208 inline void shrinkTransform(double inM[16], double outM[12])
209 {
210  int col = -1;
211  for(int i = 0; i < 12; i++)
212  {
213  if(i % 3 == 0)
214  {
215  col+=1;
216  }
217  outM[i] = inM[i+col];
218  }
219 }
220 
221 //Creates a quaternion and a position from a 3x4 column-major transformation matrix
222 //Output format: pos[3]+quat[4]
223 // the quaternion is in the order of [u v w a]
224 // In IPSI, quaternions are stored with scalar last.
225 //So, the quaternion Q = a + bi + cj + dk
226 //will be stored [b, c, d, a] (or, if you prefer, [x, y, z, w])
227 inline void rotationMatrixToQuaternion(double inM[12], double outM[7])
228 {
229  outM[0] = inM[9];
230  outM[1] = inM[10];
231  outM[2] = inM[11];
232 
233  outM[6] = std::sqrt(1 + inM[0] + (inM[4]) + inM[8])/2;
234  outM[3] = (inM[5]-inM[7])/(4*outM[6]);
235  outM[4] = (inM[6]-inM[2])/(4*outM[6]);
236  outM[5] = (inM[1]-inM[3])/(4*outM[6]);
237 }