Debian Codegraph @

3rdparty/xspublic/xstypes/xsquaternion.c

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #include "xsquaternion.h"
34 #include "xseuler.h"
35 #include "xsmatrix.h"
36 #include "xsvector.h"
37 #include <math.h>
38 #include "xsfloatmath.h"
39
40
41 /*! \class XsQuaternion
42 \brief A class that implements a quaternion
43 */
44
45 /*! \addtogroup cinterface C Interface
46 @{
47 */
48
49 /*! \relates XsQuaternion
50 \brief Sets the contents to 0, which is an invalid XsQuaternion
51 \details The normal destructor does not call this function, it is intended for explicit signalling of errors
52 */
53 void XsQuaternion_destruct(XsQuaternion* thisPtr)
54 {
55 thisPtr->m_w = XsMath_zero;
56 thisPtr->m_x = XsMath_zero;
57 thisPtr->m_y = XsMath_zero;
58 thisPtr->m_z = XsMath_zero;
59 }
60
61 /*! \relates XsQuaternion \brief Test if this is a null object. */
62 int XsQuaternion_empty(const XsQuaternion* thisPtr)
63 {
64 return thisPtr->m_w == XsMath_zero && thisPtr->m_x == XsMath_zero && thisPtr->m_y == XsMath_zero && thisPtr->m_z == XsMath_zero;
65 }
66
67 /*! \relates XsQuaternion
68 \brief Invert this quaternion
69 \details Where \a q = a + bi + cj + dk, this function will replace it with a - bi - cj - dk.
70 */
71 void XsQuaternion_invert(XsQuaternion* thisPtr)
72 {
73 XsQuaternion_inverse(thisPtr, thisPtr);
74 }
75
76 /*! \relates XsQuaternion
77 \brief Compute the inverse/conjugate of this quaternion
78 \details Where \a q = a + bi + cj + dk, this function will return a - bi - cj - dk in \a dest.
79 \param dest The object to write to
80 */
81 void XsQuaternion_inverse(const XsQuaternion* thisPtr, XsQuaternion* dest)
82 {
83 dest->m_w = thisPtr->m_w;
84 dest->m_x = -thisPtr->m_x;
85 dest->m_y = -thisPtr->m_y;
86 dest->m_z = -thisPtr->m_z;
87 }
88
89 /*! \relates XsQuaternion \brief Create a normalized version of this quaternion
90 */
91 XsReal XsQuaternion_normalized(const XsQuaternion* thisPtr, XsQuaternion* dest)
92 {
93 XsReal divisor, length = sqrt(thisPtr->m_w*thisPtr->m_w + thisPtr->m_x*thisPtr->m_x + thisPtr->m_y*thisPtr->m_y + thisPtr->m_z*thisPtr->m_z);
94 divisor = XsMath_one/length;
95 if (thisPtr->m_w < 0)
96 divisor = -divisor;
97
98 dest->m_w = thisPtr->m_w * divisor;
99 dest->m_x = thisPtr->m_x * divisor;
100 dest->m_y = thisPtr->m_y * divisor;
101 dest->m_z = thisPtr->m_z * divisor;
102
103 return length;
104 }
105
106 /*! \relates XsQuaternion \brief Normalize this quaternion
107 */
108 XsReal XsQuaternion_normalize(XsQuaternion* thisPtr)
109 {
110 return XsQuaternion_normalized(thisPtr, thisPtr);
111 }
112
113 /*! \relates XsQuaternion \brief Create a quaternion representation from \a euler angles */
114 void XsQuaternion_fromEulerAngles(XsQuaternion* thisPtr, const XsEuler* src)
115 {
116 XsReal cosX, sinX, cosY, sinY, cosZ, sinZ;
117
118 if (XsEuler_empty(src))
119 {
120 *thisPtr = *XsQuaternion_identity();
121 return;
122 }
123
124 cosX = cos(XsMath_pt5 * XsMath_deg2rad(src->m_x));
125 sinX = sin(XsMath_pt5 * XsMath_deg2rad(src->m_x));
126 cosY = cos(XsMath_pt5 * XsMath_deg2rad(src->m_y));
127 sinY = sin(XsMath_pt5 * XsMath_deg2rad(src->m_y));
128 cosZ = cos(XsMath_pt5 * XsMath_deg2rad(src->m_z));
129 sinZ = sin(XsMath_pt5 * XsMath_deg2rad(src->m_z));
130
131 thisPtr->m_w = cosX * cosY * cosZ + sinX * sinY * sinZ;
132 thisPtr->m_x = sinX * cosY * cosZ - cosX * sinY * sinZ;
133 thisPtr->m_y = cosX * sinY * cosZ + sinX * cosY * sinZ;
134 thisPtr->m_z = cosX * cosY * sinZ - sinX * sinY * cosZ;
135 }
136
137 /*! \relates XsQuaternion
138 \brief Create a quaternion representation of orientation matrix \a ori
139 \details The matrix \a ori is interpreted as an orthonormal orientation matrix, which is
140 translated into a quaternion representation. If \a ori is not a 3x3 matrix, a null-quaternion is
141 returned.
142 \param ori The source orientation matrix
143 */
144 void XsQuaternion_fromRotationMatrix(XsQuaternion* thisPtr, const XsMatrix* ori)
145 {
146 XsReal trace; // Trace of matrix
147 XsReal s;
148
149 if (!XsMatrix_dimensionsMatch(ori, 3,3))
150 {
151 XsQuaternion_destruct(thisPtr);
152 return;
153 }
154
155 // XsQuaternion result;
156
157 // Calculate trace of matrix
158 // T = 4 - 4x^2 - 4y^2 - 4z^2
159 // = 4 ( 1 - x^2 - y^2 - z^2)
160 // = 1 + fRgs[0][0] + fRgs[1][1] + fRgs[2][2] (= 4q0^2)
161
162 trace = XsMatrix_value(ori, 0, 0) + XsMatrix_value(ori, 1, 1) + XsMatrix_value(ori, 2, 2) + XsMath_one;
163
164 // If the trace of the matrix is greater than zero,
165 // then perform an "instant" calculation.
166 // Important note wrt. rounding errors:
167 // Test if (T > 0.0000001) to avoid large distortions!
168 // If the trace of the matrix is equal to zero, then identify
169 // which major diagonal element has the greatest value
170 if (trace*trace >= XsMath_tinyValue)
171 {
172 s = XsMath_two * sqrt(trace);
173 thisPtr->m_w = XsMath_pt25 * s;
174
175 s = XsMath_one/s;
176 thisPtr->m_x = (XsMatrix_value(ori, 1, 2) - XsMatrix_value(ori, 2, 1)) * s;
177 thisPtr->m_y = (XsMatrix_value(ori, 2, 0) - XsMatrix_value(ori, 0, 2)) * s;
178 thisPtr->m_z = (XsMatrix_value(ori, 0, 1) - XsMatrix_value(ori, 1, 0)) * s;
179 }
180 else if ((XsMatrix_value(ori, 0, 0) > XsMatrix_value(ori, 1, 1)) && (XsMatrix_value(ori, 0, 0) > XsMatrix_value(ori, 2, 2)))
181 {
182 trace = XsMath_one + XsMatrix_value(ori, 0, 0) - XsMatrix_value(ori, 1, 1) - XsMatrix_value(ori, 2, 2);
183 s = XsMath_two * sqrt(trace);
184 thisPtr->m_x = XsMath_pt25 * s;
185
186 s = XsMath_one/s;
187 thisPtr->m_w = (XsMatrix_value(ori, 1, 2) - XsMatrix_value(ori, 2, 1)) * s;
188 thisPtr->m_y = (XsMatrix_value(ori, 0, 1) + XsMatrix_value(ori, 1, 0)) * s;
189 thisPtr->m_z = (XsMatrix_value(ori, 2, 0) + XsMatrix_value(ori, 0, 2)) * s;
190 }
191 else if (XsMatrix_value(ori, 1, 1) > XsMatrix_value(ori, 2, 2))
192 {
193 trace = XsMath_one + XsMatrix_value(ori, 1, 1) - XsMatrix_value(ori, 0, 0) - XsMatrix_value(ori, 2, 2);
194 s = XsMath_two * sqrt(trace);
195 thisPtr->m_y = XsMath_pt25 * s;
196
197 s = XsMath_one/s;
198 thisPtr->m_w = (XsMatrix_value(ori, 2, 0) - XsMatrix_value(ori, 0, 2)) * s;
199 thisPtr->m_x = (XsMatrix_value(ori, 0, 1) + XsMatrix_value(ori, 1, 0)) * s;
200 thisPtr->m_z = (XsMatrix_value(ori, 1, 2) + XsMatrix_value(ori, 2, 1)) * s;
201 }
202 else
203 {
204 trace = XsMath_one + XsMatrix_value(ori, 2, 2) - XsMatrix_value(ori, 0, 0) - XsMatrix_value(ori, 1, 1);
205 s = XsMath_two * sqrt(trace);
206 thisPtr->m_z = XsMath_pt25 * s;
207
208 s = XsMath_one/s;
209 thisPtr->m_w = (XsMatrix_value(ori, 0, 1) - XsMatrix_value(ori, 1, 0)) * s;
210 thisPtr->m_x = (XsMatrix_value(ori, 2, 0) + XsMatrix_value(ori, 0, 2)) * s;
211 thisPtr->m_y = (XsMatrix_value(ori, 1, 2) + XsMatrix_value(ori, 2, 1)) * s;
212 }
213
214 XsQuaternion_inverse(thisPtr, thisPtr);
215 }
216
217 /*! \relates XsQuaternion \brief Returns an XsQuaternion that represents the identity quaternion */
218 const XsQuaternion* XsQuaternion_identity(void)
219 {
220 static const XsQuaternion sIdentity = { { { 1, 0, 0, 0 } } };
221 return &sIdentity;
222 }
223
224 /*! \relates XsQuaternion \brief Multiply \a left quaternion with \a right quaternion and put the result in \a dest. The parameters may point to the same XsQuaternion(s). */
225 void XsQuaternion_multiply(const XsQuaternion* left, const XsQuaternion* right, XsQuaternion* dest)
226 {
227 XsReal qa0 = left->m_w;
228 XsReal qa1 = left->m_x;
229 XsReal qa2 = left->m_y;
230 XsReal qa3 = left->m_z;
231
232 XsReal qb0 = right->m_w;
233 XsReal qb1 = right->m_x;
234 XsReal qb2 = right->m_y;
235 XsReal qb3 = right->m_z;
236
237 dest->m_w = qa0 * qb0 - qa1 * qb1 - qa2 * qb2 - qa3 * qb3;
238 dest->m_x = qa1 * qb0 + qa0 * qb1 - qa3 * qb2 + qa2 * qb3;
239 dest->m_y = qa2 * qb0 + qa3 * qb1 + qa0 * qb2 - qa1 * qb3;
240 dest->m_z = qa3 * qb0 - qa2 * qb1 + qa1 * qb2 + qa0 * qb3;
241 }
242
243 /*! \relates XsQuaternion \brief Swap the contents of \a a and \a b
244 */
245 void XsQuaternion_swap(XsQuaternion* a, XsQuaternion* b)
246 {
247 XsReal t;
248 int i;
249 for (i = 0; i < 4; ++i)
250 {
251 t = a->m_data[i];
252 a->m_data[i] = b->m_data[i];
253 b->m_data[i] = t;
254 }
255 }
256
257 /*! \relates XsQuaternion \brief Copies the contents of \a thisPtr into \a copy
258 */
259 void XsQuaternion_copy(XsQuaternion* copy, XsQuaternion const* src)
260 {
261 copy->m_w = src->m_w;
262 copy->m_x = src->m_x;
263 copy->m_y = src->m_y;
264 copy->m_z = src->m_z;
265 }
266
267 /*! \relates XsQuaternion \brief returns non-zero if \a a and \a b are numerically equal
268 */
269 int XsQuaternion_equal(XsQuaternion const* a, XsQuaternion const* b)
270 {
271 return (a->m_w == b->m_w &&
272 a->m_x == b->m_x &&
273 a->m_y == b->m_y &&
274 a->m_z == b->m_z);
275 }
276
277 /*! \brief Checks whether \a a and \a b are equal with tolerance \a tolerance
278 \param[in] a double a
279 \param[in] b double b
280 \param[in] tolerance The tolerance
281 \returns a positive value when \a a and \a b are considered equal
282 */
283 static int fuzzyIsEqual(double a, double b, double tolerance)
284 {
285 return fabs(a - b) <= tolerance;
286 }
287
288 /*! \relates XsQuaternion
289 \brief Returns non-zero if the values at \a thisPtr and \a other are within \a tolerance of each other
290 */
291 int XsQuaternion_compare(XsQuaternion const* thisPtr, XsQuaternion const* other, XsReal tolerance)
292 {
293 if (thisPtr == other)
294 return 1;
295
296 if (fuzzyIsEqual(thisPtr->m_data[0], other->m_data[0], tolerance) &&
297 fuzzyIsEqual(thisPtr->m_data[1], other->m_data[1], tolerance) &&
298 fuzzyIsEqual(thisPtr->m_data[2], other->m_data[2], tolerance) &&
299 fuzzyIsEqual(thisPtr->m_data[3], other->m_data[3], tolerance))
300 return 1;
301 // add extra check for q == -q (negative-definite vs positive-definite comparison)
302 return (fuzzyIsEqual(thisPtr->m_data[0], -other->m_data[0], tolerance) &&
303 fuzzyIsEqual(thisPtr->m_data[1], -other->m_data[1], tolerance) &&
304 fuzzyIsEqual(thisPtr->m_data[2], -other->m_data[2], tolerance) &&
305 fuzzyIsEqual(thisPtr->m_data[3], -other->m_data[3], tolerance));
306 }
307
308 /*! \relates XsQuaternion
309 \brief Returns the dot product of the \a thisPtr with \a other
310 */
311 XsReal XsQuaternion_dotProduct(XsQuaternion const* thisPtr, XsQuaternion const* other)
312 {
313 return (thisPtr->m_w * other->m_w) +
314 (thisPtr->m_x * other->m_x) +
315 (thisPtr->m_y * other->m_y) +
316 (thisPtr->m_z * other->m_z);
317 }
318
319 /*! @} */