SH4ZAM! 0.7.0
Fast math library for the Sega Dreamcast's SH4 CPU
Loading...
Searching...
No Matches
shz_quat.hpp
Go to the documentation of this file.
1/*! \file
2 \brief C++ routines for operating upon quaternions.
3 \ingroup quat
4
5 \todo
6 - overload arithmetic operators
7
8 \author 2025, 2026 Falco Girgis
9 \copyright MIT License
10*/
11
12#ifndef SHZ_QUAT_HPP
13#define SHZ_QUAT_HPP
14
15#include <compare>
16#include <tuple>
17
18#include "shz_quat.h"
19#include "shz_vector.hpp"
20
21namespace shz {
22
23 /*! C++ structure representing a quaternion.
24
25 A quaternion represents a rotation about an arbitrary axis in 3D space.
26
27 \warning
28 The SH4ZAM internal quaternion representation puts the W or angle component
29 first, followed by the X, Y, Z components for the axis!
30
31 \note
32 shz::quat is the C++ extension of shz_quat_t, which adds member functions,
33 convenience operators, and still retains backwards compatibility with the
34 C API.
35
36 \sa shz_quat_t, shz::mat4x4, shz::vec3
37 */
38 struct quat: public shz_quat_t {
39
40 /*! \name Initialization
41 \brief Routines for creating and initializing quaternions.
42 @{
43 */
44
45 //! Default constructor: does nothing.
46 quat() noexcept = default;
47
48 //! Default copy constructor.
49 quat(const quat& other) noexcept = default;
50
51 //! Value constructor: initializes a quaternion with the given values for each component.
52 SHZ_FORCE_INLINE quat(float w, float x, float y, float z) noexcept:
53 shz_quat_t({w, x, y, z}) {}
54
55 //! C Converting constructor: constructs a C++ shz::quat from a C shz_quat_t.
56 SHZ_FORCE_INLINE quat(const shz_quat_t& q) noexcept:
57 shz_quat_t(q) {}
58
59 //! Converting constructor from existing volatile C instance.
60 SHZ_FORCE_INLINE quat(const volatile shz_quat_t& other) noexcept:
61 shz_quat_t(const_cast<const shz_quat_t&>(other)) {}
62
63 //! Returns an identity quaternion.
64 SHZ_FORCE_INLINE static quat identity() noexcept {
66 }
67
68 //! C++ convenience wrapper for shz_quat_from_angles_xyz().
69 SHZ_FORCE_INLINE static quat from_angles_xyz(float x, float y, float z) noexcept {
71 }
72
73 //! Initializes a quaternion which is a rotation in \p angle radians about the given \p axis.
74 SHZ_FORCE_INLINE static quat from_axis_angle(const vec3& axis, float angle) noexcept {
75 return shz_quat_from_axis_angle(axis, angle);
76 }
77
78 //! Creates a quaternion looking in the given direction with the given reference direction.
79 SHZ_FORCE_INLINE static quat from_look_axis(const vec3& forward, const vec3& up) noexcept {
80 return shz_quat_from_look_axis(forward, up);
81 }
82
83 //! Returns the quaternion representing the rotation from the \p start to the \p end axes.
84 SHZ_FORCE_INLINE static quat from_rotated_axis(const vec3& start, const vec3& end) noexcept {
85 return shz_quat_from_rotated_axis(start, end);
86 }
87
88 //! Returns the quaternion that is linearly interpolating \p q to \p p, by a t factor of `0.0f-1.0f`.
89 SHZ_FORCE_INLINE static quat lerp(const quat& q, const quat& p, float t) noexcept {
90 return shz_quat_lerp(q, p, t);
91 }
92
93 //! Equivalent to lerp(), except that the resulting quaternion is normalized.
94 SHZ_FORCE_INLINE static quat nlerp(const quat& q, const quat& p, float t) noexcept {
95 return shz_quat_nlerp(q, p, t);
96 }
97
98 //! Returns the quaternion that is spherically linearly interpolating \p q to \p p, by a \p t factor of `0.0f-1.0f`.
99 SHZ_FORCE_INLINE static quat slerp(const quat& q, const quat& p, float t) noexcept {
100 return shz_quat_slerp(q, p, t);
101 }
102
103 //! Evaluates the smooth cubic spherical interpolation (SQUAD) at parameter \p t.
104 SHZ_FORCE_INLINE static quat squad(const quat& q1, const quat& q2, const quat& s1, const quat& s2, float t) noexcept {
105 return shz_quat_squad(q1, q2, s1, s2, t);
106 }
107
108 //! @}
109
110#ifdef SHZ_CPP23
111
112 /*! \name Component Accessors
113 \brief Routines for iterating and accessing components.
114 @{
115 */
116
117 //! Overloaded subscript operator for indexing into the quaternion like an array.
118 SHZ_FORCE_INLINE auto&& operator[](this auto&& self, size_t index) noexcept {
119 return std::forward<decltype(self)>(self).e[index];
120 }
121
122 //! Returns an iterator to the first element within the quaternion -- For STL support.
123 SHZ_FORCE_INLINE auto begin(this auto&& self) noexcept {
124 return &self[0];
125 }
126
127 //! Returns an iterator to the end of the quaternion -- For STL support.
128 SHZ_FORCE_INLINE auto end(this auto&& self) noexcept {
129 return &self[4];
130 }
131
132 //! @}
133
134 //! Generic overloaded operator for assigning a generic "this" type to volatile reference to base C type.
135 template<typename T>
136 SHZ_FORCE_INLINE auto&& operator=(this T&& self, volatile shz_quat_t other) noexcept {
137 const_cast<std::remove_cvref_t<T>&>(self) = quat(const_cast<shz_quat_t&>(other));
138 return std::forward<T>(self);
139 }
140
141 /*! \name Relational Operators
142 \brief Overloaded comparison operators
143 @{
144 */
145
146 //! Overloaded space-ship operator for auto-generating lexicographical comparison operators.
147 friend auto operator<=>(const quat& lhs, const quat& rhs) noexcept {
149 }
150
151 //! Returns true if \p lhs is lexicographically less than \p rhs.
152 friend auto operator<(const quat& lhs, const quat& rhs) noexcept {
154 rhs.begin(), rhs.end());
155 }
156#endif
157 //! Overloaded comparison operator, checks for quaternion equality.
158 friend bool operator==(const quat& lhs, const quat& rhs) noexcept {
159 return shz_quat_equal(lhs, rhs);
160 }
161
162 //! @}
163
164 /*! \name Properties
165 \brief Routines for accessing or extracting values.
166 @{
167 */
168
169 //! Returns the angle of rotation represented by the given quaternion.
170 SHZ_FORCE_INLINE float angle() const noexcept {
171 return shz_quat_angle(*this);
172 }
173
174 //! Returns the axis of rotation represented by the given quaternion.
175 SHZ_FORCE_INLINE vec3 axis() const noexcept {
176 return shz_quat_axis(*this);
177 }
178
179 //! Returns the angle of rotation about the X axis represented by the given quaternion.
180 SHZ_FORCE_INLINE float angle_x() const noexcept {
181 return shz_quat_angle_x(*this);
182 }
183
184 //! Returns the angle of rotation about the Y axis represented by the given quaternion.
185 SHZ_FORCE_INLINE float angle_y() const noexcept {
186 return shz_quat_angle_y(*this);
187 }
188
189 //! Returns the angle of rotation about the Z axis represented by the given quaternion.
190 SHZ_FORCE_INLINE float angle_z() const noexcept {
191 return shz_quat_angle_z(*this);
192 }
193
194 //! Returns the magnitude of the quaternion squared.
195 SHZ_FORCE_INLINE float magnitude_sqr() const noexcept {
196 return shz_quat_magnitude_sqr(*this);
197 }
198
199 //! Returns the magnitude of the quaternion.
200 SHZ_FORCE_INLINE float magnitude() const noexcept {
201 return shz_quat_magnitude(*this);
202 }
203
204 //! Returns the inverse of the magnitude of the quaternion.
205 SHZ_FORCE_INLINE float magnitude_inv() const noexcept {
206 return shz_quat_magnitude_inv(*this);
207 }
208
209 //! @}
210
211 /*! \name Conversions
212 \brief Routines for converting to other types.
213 @{
214 */
215
216 //! Returns the tait-bryan rotation angles about the X, Y, then Z axes which are represented by the given quaternion.
217 SHZ_FORCE_INLINE vec3 to_angles_xyz() const noexcept {
218 return shz_quat_to_angles_xyz(*this);
219 }
220
221 //! Returns both the axis and angle of rotation through the pointer arguments.
222 SHZ_FORCE_INLINE void to_axis_angle(shz_vec3_t* axis, float* angle) const noexcept {
223 shz_quat_to_axis_angle(*this, axis, angle);
224 }
225
226 //! Returns both the axis and angle of rotation as a std::pair.
227 SHZ_FORCE_INLINE auto to_axis_angle() const noexcept -> std::pair<vec3, float> {
228 std::pair<vec3, float> aa;
229 shz_quat_to_axis_angle(*this, &std::get<0>(aa), &std::get<1>(aa));
230 return aa;
231 }
232
233 //! @}
234
235 /*! \name Modifiers
236 \brief Routines for applying modifiers to an existing quaternion.
237 @{
238 */
239
240 //! Returns the given quaternion as a unit quaternion.
241 SHZ_FORCE_INLINE quat normalized() const noexcept {
242 return shz_quat_normalize(*this);
243 }
244
245 //! Normalizes the given quaternion.
246 SHZ_FORCE_INLINE void normalize() noexcept {
247 *this = normalized();
248 }
249
250 //! Returns a safely normalized quaternion from the given quaternion, protecting against division-by-zero.
251 SHZ_FORCE_INLINE quat normalized_safe() const noexcept {
252 return shz_quat_normalize_safe(*this);
253 }
254
255 //! Safely normalizes the given quaternion by protecting against division-by-zero.
256 SHZ_FORCE_INLINE void normalize_safe() noexcept {
257 *this = normalized_safe();
258 }
259
260 //! Returns a quaternion that is the conjugate of the given quaternion.
261 SHZ_FORCE_INLINE quat conjugated() const noexcept {
262 return shz_quat_conjugate(*this);
263 }
264
265 //! Conjugates the given quaternion.
266 SHZ_FORCE_INLINE void conjugate() noexcept {
267 *this = conjugated();
268 }
269
270 //! Returns the inverse of the given quaternion.
271 SHZ_FORCE_INLINE quat inverse() const noexcept {
272 return shz_quat_inv(*this);
273 }
274
275 //! Inverts the given quaternion.
276 SHZ_FORCE_INLINE void invert() noexcept {
277 *this = inverse();
278 }
279
280 //! Returns a new quaternion whose components are the negated values of the given quaternion.
281 SHZ_FORCE_INLINE quat negated() const noexcept {
282 return shz_quat_neg(*this);
283 }
284
285 //! Negates the components of the given quaternion.
286 SHZ_FORCE_INLINE void negate() noexcept {
287 *this = negated();
288 }
289
290 //! @}
291
292 /*! \name Arithmetic
293 \brief Routines performing calculations with quaternions.
294 @{
295 */
296
297 //! Returns a new quaternion from adding the given quaterion to \p rhs.
298 SHZ_FORCE_INLINE quat add(const quat& rhs) const noexcept {
299 return shz_quat_add(*this, rhs);
300 }
301
302 //! Returns a new quaternion from adding \p rhs from the given quaternion.
303 SHZ_FORCE_INLINE quat sub(const quat& rhs) const noexcept {
304 return shz_quat_sub(*this, rhs);
305 }
306
307 //! Returns a new quaternion from scaling the given quaterion by \p s.
308 SHZ_FORCE_INLINE quat scaled(float s) const noexcept {
309 return shz_quat_scale(*this, s);
310 }
311
312 //! Multiplies each component of the given quaternion by \p s.
313 SHZ_FORCE_INLINE void scale(float s) noexcept {
314 *this = scaled(s);
315 }
316
317 //! Returns the dot product between the given quaternion and another.
318 SHZ_FORCE_INLINE float dot(const quat& other) const noexcept {
319 return shz_quat_dot(*this, other);
320 }
321
322 //! Returns the dot product of the given quaternion against two others.
323 SHZ_FORCE_INLINE vec2 dot(const quat& q1, const quat& q2) const noexcept {
324 return shz_quat_dot2(*this, q1, q2);
325 }
326
327 //! Returns the dot product of the given quaternion against three others.
328 SHZ_FORCE_INLINE vec3 dot(const quat& q1, const quat& q2, const quat& q3) const noexcept {
329 return shz_quat_dot3(*this, q1, q2, q3);
330 }
331
332 //! Returns a new quaterion from multiplying the given quaternion by another.
333 SHZ_FORCE_INLINE quat mult(const quat& rhs) const noexcept {
334 return shz_quat_mult(*this, rhs);
335 }
336
337 //! Returns a new quaterion from dividing the given quaternion by another (or multiplying the given quaternion by the inverse of another).
338 SHZ_FORCE_INLINE quat div(const quat& rhs) const noexcept {
339 return shz_quat_div(*this, rhs);
340 }
341
342 //! @}
343
344 /*! \name Miscellaneous
345 \brief Other types of quaternion operations.
346 @{
347 */
348
349 //! Returns the angle in radians between the rotations represented by quaternions \p q and \p p.
350 SHZ_FORCE_INLINE float angle_between(const quat& p) const noexcept {
351 return shz_quat_angle_between(*this, p);
352 }
353
354 //! Rotates quaternion \p from towards quaternion \p to by at most \p max_angle radians.
355 SHZ_FORCE_INLINE quat rotate_towards(const quat& to, float max_angle) const noexcept {
356 return shz_quat_rotate_towards(*this, to, max_angle);
357 }
358
359 //! @}
360
361 /*! \name Transformations
362 \brief Routines for applying quaternion transforms.
363 @{
364 */
365
366 //! Returns a new shz::vec3 from transforming \p in by the given quaternion.
367 SHZ_FORCE_INLINE vec3 transform(const vec3& in) const noexcept {
368 return shz_quat_transform_vec3(*this, in);
369 }
370
371 //! @}
372
373 //! Overloaded unary negation operator, returns the negation of the given quaternion.
374 SHZ_FORCE_INLINE quat operator-() const noexcept {
375 return negated();
376 }
377
378 //! Adds and accumulates \p rhs onto the given quaternion.
379 SHZ_FORCE_INLINE quat operator+=(const quat& rhs) noexcept {
380 return *this = add(rhs);
381 }
382
383 //! Subtracts \p rhs from the given quaternion.
384 SHZ_FORCE_INLINE quat operator-=(const quat& rhs) noexcept {
385 return *this = sub(rhs);
386 }
387
388 //! Multiplies and accumulates \p rhs into the given quaternion.
389 SHZ_FORCE_INLINE quat operator*=(const quat& rhs) noexcept {
390 return *this = mult(rhs);
391 }
392
393 //! Divides the given quaternion by \p rhs.
394 SHZ_FORCE_INLINE quat operator/=(const quat& rhs) noexcept {
395 return *this = div(rhs);
396 }
397
398 //! Multiplies and accumulates each component of the given quaternion by \p rhs.
399 SHZ_FORCE_INLINE quat operator*=(float rhs) noexcept {
400 scale(rhs);
401 return *this;
402 }
403
404 //! Divides each component of the given quaternion by \p rhs.
405 SHZ_FORCE_INLINE quat operator/=(float rhs) noexcept {
407 return *this;
408 }
409 };
410
411 //! Alternate C++ alias for quat, for those who like POSIX style.
412 using quat_t = quat;
413
414 //! Overloaded operator for adding two quaternions and returning the result.
415 SHZ_FORCE_INLINE quat operator+(const quat& lhs, const quat& rhs) noexcept {
416 return lhs.add(rhs);
417 }
418
419 //! Overloaded operator for subtracting two quaternions and returning the result.
420 SHZ_FORCE_INLINE quat operator-(const quat& lhs, const quat& rhs) noexcept {
421 return lhs.sub(rhs);
422 }
423
424 //! Overloaded operator for multiplying two quaternions and returning the result.
425 SHZ_FORCE_INLINE quat operator*(const quat& lhs, const quat& rhs) noexcept {
426 return lhs.mult(rhs);
427 }
428
429 //! Overloaded operator for dividing \p lhs by \p rhs (or multiplying by the reciprocal of \p rhs) and returning the result.
430 SHZ_FORCE_INLINE quat operator/(const quat& lhs, const quat& rhs) noexcept {
431 return lhs.div(rhs);
432 }
433
434 //! Overloaded operator for scaling each component of \p lhs by \p rhs and returning the result.
435 SHZ_FORCE_INLINE quat operator*(const quat& lhs, float rhs) noexcept {
436 return lhs.scaled(rhs);
437 }
438
439 //! Overloaded operator for scaling each component of \p rhs by \p lhs and returning the result.
440 SHZ_FORCE_INLINE quat operator*(float lhs, const quat& rhs) noexcept {
441 return rhs.scaled(lhs);
442 }
443
444 //! Overloaded operator for dividing each element of \p lhs by \p rhs and returning the result.
445 SHZ_FORCE_INLINE quat operator/(const quat& lhs, float rhs) noexcept {
446 return lhs.scaled(shz_invf(rhs));
447 }
448
449 //! Overloaded operator for dividing each component of \p rhs by \p lhs.
450 SHZ_FORCE_INLINE quat operator/(float lhs, const quat& rhs) noexcept {
451 return rhs.scaled(shz_invf(lhs));
452 }
453
454 //! Overloaded operator for transforming/rotating a vec3, \p rhs, by a quaternion, \p lhs.
455 SHZ_FORCE_INLINE vec3 operator*(const quat& lhs, const vec3& rhs) noexcept {
456 return lhs.transform(rhs);
457 }
458}
459
460#endif
Namespace enclosing the SH4ZAM C++ API.
Definition shz_cdefs.hpp:21
quat operator/(float lhs, const quat &rhs) noexcept
Overloaded operator for dividing each component of rhs by lhs.
Definition shz_quat.hpp:450
vec3 operator*(const quat &lhs, const vec3 &rhs) noexcept
Overloaded operator for transforming/rotating a vec3, rhs, by a quaternion, lhs.
Definition shz_quat.hpp:455
quat operator*(const quat &lhs, float rhs) noexcept
Overloaded operator for scaling each component of lhs by rhs and returning the result.
Definition shz_quat.hpp:435
quat operator*(float lhs, const quat &rhs) noexcept
Overloaded operator for scaling each component of rhs by lhs and returning the result.
Definition shz_quat.hpp:440
quat operator+(const quat &lhs, const quat &rhs) noexcept
Overloaded operator for adding two quaternions and returning the result.
Definition shz_quat.hpp:415
quat operator-(const quat &lhs, const quat &rhs) noexcept
Overloaded operator for subtracting two quaternions and returning the result.
Definition shz_quat.hpp:420
quat operator/(const quat &lhs, const quat &rhs) noexcept
Overloaded operator for dividing lhs by rhs (or multiplying by the reciprocal of rhs) and returning t...
Definition shz_quat.hpp:430
quat operator/(const quat &lhs, float rhs) noexcept
Overloaded operator for dividing each element of lhs by rhs and returning the result.
Definition shz_quat.hpp:445
quat operator*(const quat &lhs, const quat &rhs) noexcept
Overloaded operator for multiplying two quaternions and returning the result.
Definition shz_quat.hpp:425
float shz_quat_magnitude_sqr(shz_quat_t quat) SHZ_NOEXCEPT
Returns the squared magnitude of the given quaternion.
float shz_quat_magnitude(shz_quat_t quat) SHZ_NOEXCEPT
Returns the magnitude of the given quaternion.
shz_vec3_t shz_quat_axis(shz_quat_t q) SHZ_NOEXCEPT
Returns the axis of rotation from the given quaternion.
shz_quat_t shz_quat_inv(shz_quat_t quat) SHZ_NOEXCEPT
Returns the inverse of the given quaternion.
shz_quat_t shz_quat_nlerp(shz_quat_t a, shz_quat_t b, float t) SHZ_NOEXCEPT
Equivalent to shz_quat_lerp(), except that the resulting quaternion is normalized.
shz_quat_t shz_quat_neg(shz_quat_t quat) SHZ_NOEXCEPT
Returns the negation of the given quaternion.
shz_quat_t shz_quat_scale(shz_quat_t q, float f) SHZ_NOEXCEPT
Scales the components of the given quaternion by the given factor.
bool shz_quat_equal(shz_quat_t a, shz_quat_t b) SHZ_NOEXCEPT
Returns true if the two given quaternions are considered equal based on either absolute or relative t...
float shz_quat_angle_between(shz_quat_t q, shz_quat_t p) SHZ_NOEXCEPT
Returns the angle in radians between the rotations represented by quaternions q and p.
shz_quat_t shz_quat_add(shz_quat_t q, shz_quat_t p) SHZ_NOEXCEPT
Returns the quaternion produced from adding each component of the given quaternions.
float shz_quat_magnitude_inv(shz_quat_t quat) SHZ_NOEXCEPT
Returns the inverse magnitude of the given quaternion.
float shz_quat_angle(shz_quat_t q) SHZ_NOEXCEPT
Returns the angle of rotation from the given quaternion.
shz_quat_t shz_quat_slerp(shz_quat_t q, shz_quat_t p, float t) SHZ_NOEXCEPT
Returns the quaternion that is spherically linearly interpolating a to b, by a t factor of 0....
shz_quat_t shz_quat_normalize_safe(shz_quat_t quat) SHZ_NOEXCEPT
SAFELY returns the normalized form of the given quaternion.
shz_quat_t shz_quat_lerp(shz_quat_t a, shz_quat_t b, float t) SHZ_NOEXCEPT
Returns the quaternion that is linearly interpolating a to b, by a t factor of 0.0f-1....
shz_quat_t shz_quat_div(shz_quat_t q, shz_quat_t p) SHZ_NOEXCEPT
Divides quaternion p by quaternion q (multiplying by its inverse), returning the resulting quaternion...
float shz_quat_angle_x(shz_quat_t q) SHZ_NOEXCEPT
Returns the angle of rotation the quaternion represents about the X axis in radians.
shz_vec3_t shz_quat_to_angles_xyz(shz_quat_t q) SHZ_NOEXCEPT
Returns the roll, pitch, and yaw angles of rotation represented by the given quaternion.
shz_quat_t shz_quat_rotate_towards(shz_quat_t from, shz_quat_t to, float max_angle) SHZ_NOEXCEPT
Rotates quaternion from towards quaternion to by at most max_angle radians.
shz_vec2_t shz_quat_dot2(shz_quat_t l, shz_quat_t r1, shz_quat_t r2) SHZ_NOEXCEPT
Returns the two dot products taken between the l quaternion and the r1 and r2 quaternions.
shz_quat_t shz_quat_normalize(shz_quat_t quat) SHZ_NOEXCEPT
Returns the normalized form of the given quaternion.
shz_quat_t shz_quat_squad(shz_quat_t q1, shz_quat_t q2, shz_quat_t s1, shz_quat_t s2, float t) SHZ_NOEXCEPT
Evaluates a smooth cubic spherical interpolation (SQUAD) at parameter t.
float shz_quat_dot(shz_quat_t q1, shz_quat_t q2) SHZ_NOEXCEPT
Returns the dot product of the two quaternions.
shz_quat_t shz_quat_identity(void) SHZ_NOEXCEPT
Initializes and returns an identity quaternion.
void shz_quat_to_axis_angle(shz_quat_t q, shz_vec3_t *vec, float *angle) SHZ_NOEXCEPT
Returns both the axis and angle of rotation simultaneously (faster if both are needed) from the given...
shz_vec3_t shz_quat_dot3(shz_quat_t l, shz_quat_t r1, shz_quat_t r2, shz_quat_t r3) SHZ_NOEXCEPT
Returns the two dot products taken between the l quaternion and the r1, r2, and r3 quaternions.
shz_quat_t shz_quat_from_angles_xyz(float xangle, float yangle, float zangle) SHZ_NOEXCEPT
Initializes and returns a quaternion with the given X-Y-Z rotations in radians.
shz_quat_t shz_quat_conjugate(shz_quat_t quat) SHZ_NOEXCEPT
Returns the conjugate of the given quaternion.
shz_quat_t shz_quat_mult(shz_quat_t q1, shz_quat_t q2) SHZ_NOEXCEPT
Multiplies the two quaternions, returning the result as a new quaternion.
float shz_quat_angle_z(shz_quat_t q) SHZ_NOEXCEPT
Returns the angle of rotation the quaternion represents about the Z axis in radians.
shz_quat_t shz_quat_sub(shz_quat_t q, shz_quat_t p) SHZ_NOEXCEPT
Returns the quaternion produced from subtracting each component of quaternion p from quaterion q.
float shz_quat_angle_y(shz_quat_t q) SHZ_NOEXCEPT
Returns the angle of rotation the quaternion represents about the Y axis in radians.
float shz_invf(float x) SHZ_NOEXCEPT
Takes the inverse of x using a slighty faster approximation than doing a full division,...
C++ structure representing a quaternion.
Definition shz_quat.hpp:38
static quat slerp(const quat &q, const quat &p, float t) noexcept
Returns the quaternion that is spherically linearly interpolating q to p, by a t factor of 0....
Definition shz_quat.hpp:99
float angle_between(const quat &p) const noexcept
Returns the angle in radians between the rotations represented by quaternions q and p.
Definition shz_quat.hpp:350
friend bool operator==(const quat &lhs, const quat &rhs) noexcept
Overloaded comparison operator, checks for quaternion equality.
Definition shz_quat.hpp:158
float angle() const noexcept
Returns the angle of rotation represented by the given quaternion.
Definition shz_quat.hpp:170
void negate() noexcept
Negates the components of the given quaternion.
Definition shz_quat.hpp:286
quat(const shz_quat_t &q) noexcept
C Converting constructor: constructs a C++ shz::quat from a C shz_quat_t.
Definition shz_quat.hpp:56
static quat identity() noexcept
Returns an identity quaternion.
Definition shz_quat.hpp:64
quat normalized_safe() const noexcept
Returns a safely normalized quaternion from the given quaternion, protecting against division-by-zero...
Definition shz_quat.hpp:251
vec3 dot(const quat &q1, const quat &q2, const quat &q3) const noexcept
Returns the dot product of the given quaternion against three others.
Definition shz_quat.hpp:328
quat sub(const quat &rhs) const noexcept
Returns a new quaternion from adding rhs from the given quaternion.
Definition shz_quat.hpp:303
vec3 axis() const noexcept
Returns the axis of rotation represented by the given quaternion.
Definition shz_quat.hpp:175
quat(const volatile shz_quat_t &other) noexcept
Converting constructor from existing volatile C instance.
Definition shz_quat.hpp:60
float magnitude_inv() const noexcept
Returns the inverse of the magnitude of the quaternion.
Definition shz_quat.hpp:205
quat operator-=(const quat &rhs) noexcept
Subtracts rhs from the given quaternion.
Definition shz_quat.hpp:384
quat rotate_towards(const quat &to, float max_angle) const noexcept
Rotates quaternion from towards quaternion to by at most max_angle radians.
Definition shz_quat.hpp:355
quat operator+=(const quat &rhs) noexcept
Adds and accumulates rhs onto the given quaternion.
Definition shz_quat.hpp:379
auto to_axis_angle() const noexcept -> std::pair< vec3, float >
Returns both the axis and angle of rotation as a std::pair.
Definition shz_quat.hpp:227
quat(const quat &other) noexcept=default
Default copy constructor.
void conjugate() noexcept
Conjugates the given quaternion.
Definition shz_quat.hpp:266
quat negated() const noexcept
Returns a new quaternion whose components are the negated values of the given quaternion.
Definition shz_quat.hpp:281
float angle_y() const noexcept
Returns the angle of rotation about the Y axis represented by the given quaternion.
Definition shz_quat.hpp:185
float dot(const quat &other) const noexcept
Returns the dot product between the given quaternion and another.
Definition shz_quat.hpp:318
quat operator/=(const quat &rhs) noexcept
Divides the given quaternion by rhs.
Definition shz_quat.hpp:394
quat scaled(float s) const noexcept
Returns a new quaternion from scaling the given quaterion by s.
Definition shz_quat.hpp:308
static quat lerp(const quat &q, const quat &p, float t) noexcept
Returns the quaternion that is linearly interpolating q to p, by a t factor of 0.0f-1....
Definition shz_quat.hpp:89
quat operator/=(float rhs) noexcept
Divides each component of the given quaternion by rhs.
Definition shz_quat.hpp:405
void invert() noexcept
Inverts the given quaternion.
Definition shz_quat.hpp:276
vec3 to_angles_xyz() const noexcept
Returns the tait-bryan rotation angles about the X, Y, then Z axes which are represented by the given...
Definition shz_quat.hpp:217
vec2 dot(const quat &q1, const quat &q2) const noexcept
Returns the dot product of the given quaternion against two others.
Definition shz_quat.hpp:323
static quat from_axis_angle(const vec3 &axis, float angle) noexcept
Initializes a quaternion which is a rotation in angle radians about the given axis.
Definition shz_quat.hpp:74
void normalize() noexcept
Normalizes the given quaternion.
Definition shz_quat.hpp:246
quat mult(const quat &rhs) const noexcept
Returns a new quaterion from multiplying the given quaternion by another.
Definition shz_quat.hpp:333
quat conjugated() const noexcept
Returns a quaternion that is the conjugate of the given quaternion.
Definition shz_quat.hpp:261
quat operator-() const noexcept
Overloaded unary negation operator, returns the negation of the given quaternion.
Definition shz_quat.hpp:374
static quat from_look_axis(const vec3 &forward, const vec3 &up) noexcept
Creates a quaternion looking in the given direction with the given reference direction.
Definition shz_quat.hpp:79
float magnitude() const noexcept
Returns the magnitude of the quaternion.
Definition shz_quat.hpp:200
quat() noexcept=default
Default constructor: does nothing.
quat normalized() const noexcept
Returns the given quaternion as a unit quaternion.
Definition shz_quat.hpp:241
static quat nlerp(const quat &q, const quat &p, float t) noexcept
Equivalent to lerp(), except that the resulting quaternion is normalized.
Definition shz_quat.hpp:94
static quat from_rotated_axis(const vec3 &start, const vec3 &end) noexcept
Returns the quaternion representing the rotation from the start to the end axes.
Definition shz_quat.hpp:84
float angle_x() const noexcept
Returns the angle of rotation about the X axis represented by the given quaternion.
Definition shz_quat.hpp:180
float magnitude_sqr() const noexcept
Returns the magnitude of the quaternion squared.
Definition shz_quat.hpp:195
quat operator*=(const quat &rhs) noexcept
Multiplies and accumulates rhs into the given quaternion.
Definition shz_quat.hpp:389
quat add(const quat &rhs) const noexcept
Returns a new quaternion from adding the given quaterion to rhs.
Definition shz_quat.hpp:298
static quat from_angles_xyz(float x, float y, float z) noexcept
C++ convenience wrapper for shz_quat_from_angles_xyz().
Definition shz_quat.hpp:69
quat inverse() const noexcept
Returns the inverse of the given quaternion.
Definition shz_quat.hpp:271
quat(float w, float x, float y, float z) noexcept
Value constructor: initializes a quaternion with the given values for each component.
Definition shz_quat.hpp:52
void scale(float s) noexcept
Multiplies each component of the given quaternion by s.
Definition shz_quat.hpp:313
quat div(const quat &rhs) const noexcept
Returns a new quaterion from dividing the given quaternion by another (or multiplying the given quate...
Definition shz_quat.hpp:338
static quat squad(const quat &q1, const quat &q2, const quat &s1, const quat &s2, float t) noexcept
Evaluates the smooth cubic spherical interpolation (SQUAD) at parameter t.
Definition shz_quat.hpp:104
void normalize_safe() noexcept
Safely normalizes the given quaternion by protecting against division-by-zero.
Definition shz_quat.hpp:256
vec3 transform(const vec3 &in) const noexcept
Returns a new shz::vec3 from transforming in by the given quaternion.
Definition shz_quat.hpp:367
void to_axis_angle(shz_vec3_t *axis, float *angle) const noexcept
Returns both the axis and angle of rotation through the pointer arguments.
Definition shz_quat.hpp:222
quat operator*=(float rhs) noexcept
Multiplies and accumulates each component of the given quaternion by rhs.
Definition shz_quat.hpp:399
float angle_z() const noexcept
Returns the angle of rotation about the Z axis represented by the given quaternion.
Definition shz_quat.hpp:190
2D Vector type
3D Vector type