utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
quaternion.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21*/
22
23#pragma once
24
25#include <cmath>
26#include <stdexcept>
27#include <string>
28#include <type_traits>
29#include <utility>
30
31#include <glm/ext/quaternion_geometric.hpp>
32#include <glm/gtx/quaternion.hpp>
33
34namespace utility::math
35{
36
37 using glm::toMat4;
38
39 using glm::conjugate;
40 using glm::inverse;
41 using glm::isinf;
42 using glm::isnan;
43 using glm::lerp;
44 using glm::mix;
45 using glm::slerp;
46
47 using glm::cross;
48 using glm::dot;
49 using glm::length;
50 using glm::normalize;
51
56 template<typename Type>
57 concept CanBeQuaternionComponent = std::is_floating_point_v<Type>;
58
65 template<CanBeQuaternionComponent QuaternionComponentType> class Quaternion:
66 public glm::qua<QuaternionComponentType>
67 {
68 public:
73 : glm::qua<QuaternionComponentType>(
74 QuaternionComponentType(1), QuaternionComponentType(0),
75 QuaternionComponentType(0), QuaternionComponentType(0))
76 {
77 }
78
92 Quaternion(QuaternionComponentType x, QuaternionComponentType y,
93 QuaternionComponentType z, QuaternionComponentType w)
94 : glm::qua<QuaternionComponentType>(w, x, y, z)
95 {
96 }
97
102 explicit Quaternion(const glm::qua<QuaternionComponentType> &q)
103 : glm::qua<QuaternionComponentType>(q)
104 {
105 }
106
111 Quaternion(const Quaternion &other) = default;
112
117 Quaternion(Quaternion &&other) noexcept = default;
118
122 ~Quaternion() = default;
123
129 Quaternion &operator=(const Quaternion &other) = default;
130
136 Quaternion &operator=(Quaternion &&other) noexcept = default;
137
143 Quaternion operator+(const Quaternion &rhs) const noexcept
144 {
145 return Quaternion(
146 static_cast<const glm::qua<QuaternionComponentType> &>(*this)
147 + static_cast<const glm::qua<QuaternionComponentType> &>(rhs));
148 }
149
155 Quaternion &operator+=(const Quaternion &rhs) noexcept
156 {
157 *static_cast<glm::qua<QuaternionComponentType> *>(this) +=
158 static_cast<const glm::qua<QuaternionComponentType> &>(rhs);
159 return *this;
160 }
161
167 Quaternion operator-(const Quaternion &rhs) const noexcept
168 {
169 return Quaternion(
170 static_cast<const glm::qua<QuaternionComponentType> &>(*this)
171 - static_cast<const glm::qua<QuaternionComponentType> &>(rhs));
172 }
173
179 Quaternion &operator-=(const Quaternion &rhs) noexcept
180 {
181 *static_cast<glm::qua<QuaternionComponentType> *>(this) -=
182 static_cast<const glm::qua<QuaternionComponentType> &>(rhs);
183 return *this;
184 }
185
191 Quaternion operator*(const Quaternion &rhs) const noexcept
192 {
193 return Quaternion(
194 static_cast<const glm::qua<QuaternionComponentType> &>(*this)
195 * static_cast<const glm::qua<QuaternionComponentType> &>(rhs));
196 }
197
203 Quaternion &operator*=(const Quaternion &rhs) noexcept
204 {
205 *static_cast<glm::qua<QuaternionComponentType> *>(this) =
206 static_cast<const glm::qua<QuaternionComponentType> &>(*this)
207 * static_cast<const glm::qua<QuaternionComponentType> &>(rhs);
208 return *this;
209 }
210
216 Quaternion operator*(QuaternionComponentType scalar) const noexcept
217 {
218 return Quaternion(
219 static_cast<const glm::qua<QuaternionComponentType> &>(*this)
220 * scalar);
221 }
222
228 Quaternion &operator*=(QuaternionComponentType scalar) noexcept
229 {
230 *static_cast<glm::qua<QuaternionComponentType> *>(this) *= scalar;
231 return *this;
232 }
233
242 bool operator==(const Quaternion &rhs) const noexcept
243 {
244 return static_cast<const glm::qua<QuaternionComponentType> &>(*this)
245 == static_cast<const glm::qua<QuaternionComponentType> &>(rhs);
246 }
247
257 bool equalsEpsilon(const Quaternion &rhs,
258 QuaternionComponentType epsilon =
259 QuaternionComponentType { 1e-5 }) const
260 {
261 return std::abs(this->x - rhs.x) <= epsilon
262 && std::abs(this->y - rhs.y) <= epsilon
263 && std::abs(this->z - rhs.z) <= epsilon
264 && std::abs(this->w - rhs.w) <= epsilon;
265 }
266
272 bool operator!=(const Quaternion &rhs) const noexcept
273 {
274 return !(*this == rhs);
275 }
276
281 Quaternion operator-(void) const noexcept
282 {
283 return Quaternion(
284 -static_cast<const glm::qua<QuaternionComponentType> &>(*this));
285 }
286
294 friend Quaternion operator*(QuaternionComponentType scalar,
295 const Quaternion &q)
296 {
297 return q * scalar;
298 }
299 };
300
304 using QuaternionF = Quaternion<float>;
305
309 using QuaternionD = Quaternion<double>;
310
311} // namespace utility::math
Quaternion class inheriting from glm::qua.
Quaternion operator*(QuaternionComponentType scalar) const noexcept
Scalar multiplication.
Quaternion operator+(const Quaternion &rhs) const noexcept
Quaternion addition.
bool operator==(const Quaternion &rhs) const noexcept
Equality comparison.
Quaternion & operator-=(const Quaternion &rhs) noexcept
Quaternion subtraction assignment.
Quaternion & operator*=(QuaternionComponentType scalar) noexcept
Scalar multiplication assignment.
Quaternion(QuaternionComponentType x, QuaternionComponentType y, QuaternionComponentType z, QuaternionComponentType w)
Construct from explicit components in (x, y, z, w) order.
~Quaternion()=default
Default destructor for Quaternion.
Quaternion()
Default constructor (identity quaternion).
Quaternion(const glm::qua< QuaternionComponentType > &q)
Construct from a GLM quaternion.
Quaternion & operator*=(const Quaternion &rhs) noexcept
Quaternion multiplication assignment (Hamilton product).
Quaternion & operator=(const Quaternion &other)=default
Copy assignment operator.
Quaternion operator-(void) const noexcept
Unary negation.
friend Quaternion operator*(QuaternionComponentType scalar, const Quaternion &q)
Friend function for scalar multiplication with scalar on the left.
Quaternion & operator+=(const Quaternion &rhs) noexcept
Quaternion addition assignment.
Quaternion & operator=(Quaternion &&other) noexcept=default
Move assignment operator.
Quaternion(const Quaternion &other)=default
Copy constructor.
bool operator!=(const Quaternion &rhs) const noexcept
Inequality comparison.
Quaternion operator-(const Quaternion &rhs) const noexcept
Quaternion subtraction.
Quaternion operator*(const Quaternion &rhs) const noexcept
Quaternion multiplication (Hamilton product).
Quaternion(Quaternion &&other) noexcept=default
Move constructor.
bool equalsEpsilon(const Quaternion &rhs, QuaternionComponentType epsilon=QuaternionComponentType { 1e-5 }) const
Approximate equality using an epsilon threshold.
Concept to constrain quaternion component type.