forked from PlusToolkit/ndicapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndicapi_math.cxx
295 lines (256 loc) · 9.79 KB
/
ndicapi_math.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*=======================================================================
Copyright (c) 2000-2005 Atamai, Inc.
Use, modification and redistribution of the software, in source or
binary forms, are permitted provided that the following terms and
conditions are met:
1) Redistribution of the source code, in verbatim or modified
form, must retain the above copyright notice, this license,
the following disclaimer, and any notices that refer to this
license and/or the following disclaimer.
2) Redistribution in binary form must include the above copyright
notice, a copy of this license and the following disclaimer
in the documentation or with other materials provided with the
distribution.
3) Modified copies of the source code must be clearly marked as such,
and must not be misrepresented as verbatim copies of the source code.
THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS"
WITHOUT EXPRESSED OR IMPLIED WARRANTY INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. IN NO EVENT SHALL ANY COPYRIGHT HOLDER OR OTHER PARTY WHO MAY
MODIFY AND/OR REDISTRIBUTE THE SOFTWARE UNDER THE TERMS OF THIS LICENSE
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR DATA BECOMING INACCURATE
OR LOSS OF PROFIT OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF
THE USE OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
=======================================================================*/
#include <math.h>
#include "ndicapi_math.h"
//----------------------------------------------------------------------------
// Divide the transform 'trans' by the transform 'ref':
// trans = trans * ref^(-1)
ndicapiExport void ndiRelativeTransform(const double a[8], const double b[8], double c[8])
{
double f, x, y, z, w1, x1, y1, z1, w2, x2, y2, z2;
w1 = b[0];
x1 = b[1];
y1 = b[2];
z1 = b[3];
w2 = a[0];
x2 = a[1];
y2 = a[2];
z2 = a[3];
/* for rotation part of transformation:
q = q1\q2 (divide on the right to get new orientation) */
c[0] = w1 * w2 + x1 * x2 + y1 * y2 + z1 * z2;
c[1] = w1 * x2 - x1 * w2 - y1 * z2 + z1 * y2;
c[2] = w1 * y2 + x1 * z2 - y1 * w2 - z1 * x2;
c[3] = w1 * z2 - x1 * y2 + y1 * x2 - z1 * w2;
/* several steps required to calculate new translation: */
/* distance between tools */
x = a[4] - b[4];
y = a[5] - b[5];
z = a[6] - b[6];
/* q = q1\q*q1 (apply inverse of reference tranformation to distance) */
/* first: qtmp = q1\q */
w2 = x1 * x + y1 * y + z1 * z;
x2 = w1 * x - y1 * z + z1 * y;
y2 = w1 * y + x1 * z - z1 * x;
z2 = w1 * z - x1 * y + y1 * x;
/* next: q = qtmp*q1 */
x = w2 * x1 + x2 * w1 + y2 * z1 - z2 * y1;
y = w2 * y1 - x2 * z1 + y2 * w1 + z2 * x1;
z = w2 * z1 + x2 * y1 - y2 * x1 + z2 * w1;
/* find the normalization factor for q1 */
f = 1.0f / (w1 * w1 + x1 * x1 + y1 * y1 + z1 * z1);
c[4] = x * f;
c[5] = y * f;
c[6] = z * f;
c[7] = 0.0;
}
//----------------------------------------------------------------------------
// Converts the quaternion rotation + translation transform returned
// by the NDICAPI into a 4x4 base-zero row-major matrix, following
// the right-multiplication convention:
//
// / m_11 m_12 m_11 m_12 \
// [ x' y' z' 1 ] = [ x y z 1 ] | m_21 m_22 m_21 m_22 |
// | m_31 m_32 m_31 m_32 |
// \ m_41 m_42 m_41 m_42 /
//
// where m_11 == m[0][0], m_12 = m[0][1], etc. This is the
// convention used by QuickDraw3D on the Macintosh and by
// DirectDraw3D on the PC.
//
// This matrix can also be used in OpenGL without modification
// even though OpenGL follows the left-multiplication convention,
// because OpenGL uses a column-major storage model so references
// to the matrix are automagically transposed.
ndicapiExport void ndiTransformToMatrixf(const float trans[8], float matrix[16])
{
float ww, xx, yy, zz, wx, wy, wz, xy, xz, yz, ss, rr, f;
/* Determine some calculations done more than once. */
ww = trans[0] * trans[0];
xx = trans[1] * trans[1];
yy = trans[2] * trans[2];
zz = trans[3] * trans[3];
wx = trans[0] * trans[1];
wy = trans[0] * trans[2];
wz = trans[0] * trans[3];
xy = trans[1] * trans[2];
xz = trans[1] * trans[3];
yz = trans[2] * trans[3];
rr = xx + yy + zz;
ss = (ww - rr) * 0.5f;
/* Normalization factor */
f = 2.0f / (ww + rr);
/* Fill in the matrix. */
matrix[0] = (ss + xx) * f;
matrix[1] = (wz + xy) * f;
matrix[2] = (-wy + xz) * f;
matrix[3] = 0;
matrix[4] = (-wz + xy) * f;
matrix[5] = (ss + yy) * f;
matrix[6] = (wx + yz) * f;
matrix[7] = 0;
matrix[8] = (wy + xz) * f;
matrix[9] = (-wx + yz) * f;
matrix[10] = (ss + zz) * f;
matrix[11] = 0;
matrix[12] = trans[4];
matrix[13] = trans[5];
matrix[14] = trans[6];
matrix[15] = 1;
}
//----------------------------------------------------------------------------
ndicapiExport void ndiTransformToMatrixfd(const float trans[8], double matrix[16])
{
double ww, xx, yy, zz, wx, wy, wz, xy, xz, yz, ss, rr, f;
/* Determine some calculations done more than once. */
ww = trans[0] * trans[0];
xx = trans[1] * trans[1];
yy = trans[2] * trans[2];
zz = trans[3] * trans[3];
wx = trans[0] * trans[1];
wy = trans[0] * trans[2];
wz = trans[0] * trans[3];
xy = trans[1] * trans[2];
xz = trans[1] * trans[3];
yz = trans[2] * trans[3];
rr = xx + yy + zz;
ss = (ww - rr) * 0.5;
/* Normalization factor */
f = 2.0 / (ww + rr);
/* Fill in the matrix. */
matrix[0] = (ss + xx) * f;
matrix[1] = (wz + xy) * f;
matrix[2] = (-wy + xz) * f;
matrix[3] = 0;
matrix[4] = (-wz + xy) * f;
matrix[5] = (ss + yy) * f;
matrix[6] = (wx + yz) * f;
matrix[7] = 0;
matrix[8] = (wy + xz) * f;
matrix[9] = (-wx + yz) * f;
matrix[10] = (ss + zz) * f;
matrix[11] = 0;
matrix[12] = trans[4];
matrix[13] = trans[5];
matrix[14] = trans[6];
matrix[15] = 1;
}
//----------------------------------------------------------------------------
ndicapiExport void ndiTransformToMatrixd(const double trans[8], double matrix[16])
{
double ww, xx, yy, zz, wx, wy, wz, xy, xz, yz, ss, rr, f;
/* Determine some calculations done more than once. */
ww = trans[0] * trans[0];
xx = trans[1] * trans[1];
yy = trans[2] * trans[2];
zz = trans[3] * trans[3];
wx = trans[0] * trans[1];
wy = trans[0] * trans[2];
wz = trans[0] * trans[3];
xy = trans[1] * trans[2];
xz = trans[1] * trans[3];
yz = trans[2] * trans[3];
rr = xx + yy + zz;
ss = (ww - rr) * 0.5;
/* Normalization factor */
f = 2.0 / (ww + rr);
/* Fill in the matrix. */
matrix[0] = (ss + xx) * f;
matrix[1] = (wz + xy) * f;
matrix[2] = (-wy + xz) * f;
matrix[3] = 0;
matrix[4] = (-wz + xy) * f;
matrix[5] = (ss + yy) * f;
matrix[6] = (wx + yz) * f;
matrix[7] = 0;
matrix[8] = (wy + xz) * f;
matrix[9] = (-wx + yz) * f;
matrix[10] = (ss + zz) * f;
matrix[11] = 0;
matrix[12] = trans[4];
matrix[13] = trans[5];
matrix[14] = trans[6];
matrix[15] = 1;
}
//----------------------------------------------------------------------------
// Extracts the roll,pitch,yaw rotation angles (in radians) from the
// 4x4 transform matrix (note that these are _not_ the same as Euler
// angles, which follow a different convention).
//
// The rotations are described by the following three matrices, the
// product of which is the full rotation matrix. Please note that
// the matrices follow the right-multiplication convention (see notes
// for ndiTransformToMatrix() ).
//
// roll around x axis pitch around y axix yaw around z axis
//
// / 1 0 0 \ / cos(pch) 0 -sin(pch) \ / cos(rol) sin(rol) 0 \
// | 0 cos(yaw) sin(yaw) |*| 0 1 0 |*| -sin(rol) cos(rol) 0 |
// \ 0 -sin(yaw) cos(yaw) / \ sin(pch) 0 cos(pch) / \ 0 0 1 /
//
// Pay careful attention to the above: according to the right-multiplication
// convention, the order in which the rotations are applied are 1) roll
// around x 2) pitch around y 3) yaw around z.
//
// This function was nabbed from the NDI ndicapi docs and heavily modified.
ndicapiExport void ndiAnglesFromMatrixf(float outRotationAngles[3], const float rotationMatrix[16])
{
float yaw, cosYaw, sinYaw;
yaw = atan2f(rotationMatrix[1], rotationMatrix[0]);
cosYaw = cosf(yaw);
sinYaw = sinf(yaw);
outRotationAngles[2] = (float)yaw;
outRotationAngles[1] = (float)atan2f(-rotationMatrix[2], (cosYaw * rotationMatrix[0]) + (sinYaw * rotationMatrix[1]));
outRotationAngles[0] = (float)atan2f((sinYaw * rotationMatrix[8]) - (cosYaw * rotationMatrix[9]), (-sinYaw * rotationMatrix[4]) + (cosYaw * rotationMatrix[5]));
}
//----------------------------------------------------------------------------
ndicapiExport void ndiAnglesFromMatrixd(double outRotationAngles[3], const double rotationMatrix[16])
{
double yaw, cosYaw, sinYaw;
yaw = atan2(rotationMatrix[1], rotationMatrix[0]);
cosYaw = cos(yaw);
sinYaw = sin(yaw);
outRotationAngles[2] = yaw;
outRotationAngles[1] = atan2(-rotationMatrix[2], (cosYaw * rotationMatrix[0]) + (sinYaw * rotationMatrix[1]));
outRotationAngles[0] = atan2((sinYaw * rotationMatrix[8]) - (cosYaw * rotationMatrix[9]), (-sinYaw * rotationMatrix[4]) + (cosYaw * rotationMatrix[5]));
}
//----------------------------------------------------------------------------
// A very simple function to extract the translation portion of a
// transformation matrix.
ndicapiExport void ndiCoordsFromMatrixf(float coords[3], const float matrix[16])
{
coords[0] = matrix[12];
coords[1] = matrix[13];
coords[2] = matrix[14];
}
//----------------------------------------------------------------------------
ndicapiExport void ndiCoordsFromMatrixd(double coords[3], const double matrix[16])
{
coords[0] = matrix[12];
coords[1] = matrix[13];
coords[2] = matrix[14];
}