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