Debian Codegraph @

3rdparty/xspublic/xstypes/xsquaternion.h

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 #ifndef XSQUATERNION_H
34 #define XSQUATERNION_H
35
36 #include "xsmath.h"
37
38 struct XsEuler;
39 struct XsMatrix;
40 struct XsVector;
41 struct XsQuaternion;
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 #ifndef __cplusplus
47 #define XSQUATERNION_INITIALIZER { { { XsMath_zero, XsMath_zero, XsMath_zero, XsMath_zero } } }
48 typedef struct XsQuaternion XsQuaternion;
49 #endif
50
51 XSTYPES_DLL_API void XsQuaternion_destruct(XsQuaternion* thisPtr);
52 XSTYPES_DLL_API int XsQuaternion_empty(const XsQuaternion* thisPtr);
53 XSTYPES_DLL_API void XsQuaternion_inverse(const XsQuaternion* thisPtr, XsQuaternion* dest);
54 XSTYPES_DLL_API XsReal XsQuaternion_normalized(const XsQuaternion* thisPtr, XsQuaternion* dest);
55 XSTYPES_DLL_API XsReal XsQuaternion_normalize(XsQuaternion* thisPtr);
56 XSTYPES_DLL_API void XsQuaternion_fromEulerAngles(XsQuaternion* thisPtr, const struct XsEuler* src);
57 XSTYPES_DLL_API void XsQuaternion_fromRotationMatrix(XsQuaternion* thisPtr, const struct XsMatrix* ori);
58 XSTYPES_DLL_API const XsQuaternion* XsQuaternion_identity(void);
59 XSTYPES_DLL_API void XsQuaternion_multiply(const XsQuaternion* left, const XsQuaternion* right, XsQuaternion* dest);
60 XSTYPES_DLL_API void XsQuaternion_swap(XsQuaternion* a, XsQuaternion* b);
61 XSTYPES_DLL_API void XsQuaternion_copy(XsQuaternion* copy, XsQuaternion const* src);
62 XSTYPES_DLL_API int XsQuaternion_equal(XsQuaternion const* a, XsQuaternion const* b);
63 XSTYPES_DLL_API int XsQuaternion_compare(XsQuaternion const* thisPtr, XsQuaternion const* other, XsReal tolerance);
64 XSTYPES_DLL_API XsReal XsQuaternion_dotProduct(XsQuaternion const* thisPtr, XsQuaternion const* other);
65
66 #ifdef __cplusplus
67 } // extern "C"
68 #endif
69
70 struct XsQuaternion {
71 XSCPPPROTECTED
72 union {
73 struct {
74 XsReal m_w; //!< Stores the w component of the quaternion
75 XsReal m_x; //!< Stores the x component of the quaternion
76 XsReal m_y; //!< Stores the y component of the quaternion
77 XsReal m_z; //!< Stores the z component of the quaternion
78 };
79 XsReal m_data[4]; //!< Stores the quaternion in an array of four elements
80 };
81 #ifdef __cplusplus
82 public:
83 //! \brief Construct a quaternion with the supplied values, or zero's by default
84 inline explicit XsQuaternion(XsReal w_ = XsMath_zero, XsReal x_ = XsMath_zero, XsReal y_ = XsMath_zero, XsReal z_ = XsMath_zero)
85 : m_w(w_)
86 , m_x(x_)
87 , m_y(y_)
88 , m_z(z_)
89 {
90 }
91
92 //! \brief Construct a quaternion with the supplied values, or zero's by default and optionally normalize
93 inline explicit XsQuaternion(XsReal w_, XsReal x_, XsReal y_, XsReal z_, bool normalize_)
94 : m_w(w_)
95 , m_x(x_)
96 , m_y(y_)
97 , m_z(z_)
98 {
99 if (normalize_)
100 normalize();
101 }
102
103 //! \brief Construct a quaternion with the supplied values from the \a other Quaternion
104 inline XsQuaternion(const XsQuaternion& other)
105 : m_w(other.m_w)
106 , m_x(other.m_x)
107 , m_y(other.m_y)
108 , m_z(other.m_z)
109 {}
110
111 //! \brief Construct a quaternion by converting from an XsEuler object
112 inline explicit XsQuaternion(const XsEuler& euler)
113 {
114 XsQuaternion_fromEulerAngles(this, &euler);
115 }
116
117 //! \brief Construct a quaternion by converting from an XsMatrix rotation matrix object
118 inline explicit XsQuaternion(const XsMatrix& ori)
119 {
120 XsQuaternion_fromRotationMatrix(this, &ori);
121 }
122
123 //! \brief Destructor, intentionally empty
124 inline ~XsQuaternion()
125 {
126 }
127
128 //! \brief Assigns the \a other quaternion to this quaternion
129 inline XsQuaternion& operator=(const XsQuaternion& other)
130 {
131 if (this != &other)
132 {
133 m_w = other.m_w;
134 m_x = other.m_x;
135 m_y = other.m_y;
136 m_z = other.m_z;
137 }
138 return *this;
139 }
140
141 /*! \brief Set the Quaternion to these specific values
142 */
143 inline void assign(XsReal w_, XsReal x_, XsReal y_, XsReal z_)
144 {
145 m_w = w_;
146 m_x = x_;
147 m_y = y_;
148 m_z = z_;
149 }
150
151 /*! \brief Set the Quaternion to the specific values in the supplied array.
152 \param values An array that contains at least 4 items. The first four will be interpreted as w,x,y,z
153 */
154 inline void assign(const XsReal* values)
155 {
156 for (int i = 0; i < 4; ++i)
157 m_data[i] = values[i];
158 }
159
160 //! \brief Returns a reference to the \a index'th component of the quaternion
161 inline XsReal& operator[](XsSize index)
162 {
163 assert(index < 4);
164 return m_data[index];
165 }
166
167 //! \brief Returns the \a index'th component of the quaternion
168 inline XsReal operator[](XsSize index) const
169 {
170 assert(index < 4);
171 return m_data[index];
172 }
173
174 //! \brief Return a const pointer to the internal data
175 inline const XsReal* data() const
176 {
177 return m_data;
178 }
179
180 //! \brief \copybrief XsQuaternion_inverse \returns The inverse/conjugate of the quaternion
181 inline XsQuaternion inverse() const
182 {
183 XsQuaternion tmp;
184 XsQuaternion_inverse(this, &tmp);
185 return tmp;
186 }
187
188 //! \brief \copybrief XsQuaternion_inverse \returns The inverse/conjugate of the quaternion
189 inline XsQuaternion conjugate() const
190 {
191 XsQuaternion tmp;
192 XsQuaternion_inverse(this, &tmp);
193 return tmp;
194 }
195
196 //! \brief Return a normalized version of the quaternion \sa XsQuaternion_normalized \returns The normalized quaternion
197 XsQuaternion normalized() const
198 {
199 XsQuaternion tmp;
200 XsQuaternion_normalized(this, &tmp);
201 return tmp;
202 }
203
204 //! \brief Normalize the quaternion \sa XsQuaternion_normalized \returns The cartesian length of the quaternion before normalization
205 inline XsReal normalize()
206 {
207 return XsQuaternion_normalized(this, this);
208 }
209
210 //! \brief \copybrief XsQuaternion_empty
211 inline bool empty() const
212 {
213 return 0 != XsQuaternion_empty(this);
214 }
215
216 //! \brief \copybrief XsQuaternion_fromEulerAngles
217 inline XsQuaternion& fromEulerAngles(const XsEuler& src)
218 {
219 XsQuaternion_fromEulerAngles(this, &src);
220 return *this;
221 }
222
223 //! \brief \copybrief XsQuaternion_fromRotationMatrix
224 inline XsQuaternion& fromRotationMatrix(const XsMatrix& ori)
225 {
226 XsQuaternion_fromRotationMatrix(this, &ori);
227 return *this;
228 }
229
230 //! \brief \copybrief XsQuaternion_identity
231 inline static const XsQuaternion& identity()
232 {
233 return *XsQuaternion_identity();
234 }
235
236 /*! \brief In-place multiplication of this quaternion with \a other quaternion
237 \param other The other quaternion to multiply with (right side of multiplication)
238 \sa XsQuaternion_multiply()
239 */
240 inline void operator*=(const XsQuaternion& other)
241 {
242 XsQuaternion_multiply(this, &other, this);
243 }
244
245 //! \brief Return the w component of the quaternion
246 inline XsReal w() const { return m_w; }
247 //! \brief Return the x component of the quaternion
248 inline XsReal x() const { return m_x; }
249 //! \brief Return the y component of the quaternion
250 inline XsReal y() const { return m_y; }
251 //! \brief Return the z component of the quaternion
252 inline XsReal z() const { return m_z; }
253 //! \brief Return a reference to the w component of the quaternion
254 inline XsReal& w() { return m_w; }
255 //! \brief Return a reference to the x component of the quaternion
256 inline XsReal& x() { return m_x; }
257 //! \brief Return a reference to the y component of the quaternion
258 inline XsReal& y() { return m_y; }
259 //! \brief Return a reference to the z component of the quaternion
260 inline XsReal& z() { return m_z; }
261
262 //! \brief Swap the contents with \a other
263 inline void swap(XsQuaternion& other)
264 {
265 XsQuaternion_swap(this, &other);
266 }
267
268 //! \brief Returns true if \a other is numerically identical to this
269 inline bool operator ==(const XsQuaternion& other) const
270 {
271 return m_w == other.m_w &&
272 m_x == other.m_x &&
273 m_y == other.m_y &&
274 m_z == other.m_z;
275 }
276
277 //! \brief Returns true if the fields of this and \a other are within \a tolerance of each otehr
278 inline bool isEqual(const XsQuaternion& other, XsReal tolerance) const
279 {
280 return !!XsQuaternion_compare(this, &other, tolerance);
281 }
282
283 //! \brief Returns the dot product of this with \a other
284 inline XsReal dotProduct(const XsQuaternion& other) const
285 {
286 return XsQuaternion_dotProduct(this, &other);
287 }
288 #endif
289 };
290
291 #ifdef __cplusplus
292 //! \brief Return the negated version of the Quaternion \a q (w,-x,-y,-z)
293 inline XsQuaternion operator-(const XsQuaternion &q)
294 {
295 return XsQuaternion(q.w(), -q.x(), -q.y(), -q.z());
296 }
297
298 //! \brief Multiply \a lhs by \a rhs and return the result
299 inline XsQuaternion operator *(const XsQuaternion& lhs, const XsQuaternion &rhs)
300 {
301 XsQuaternion tmp(lhs);
302 tmp *= rhs;
303 return tmp;
304 }
305 #endif
306
307 #endif