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
orientation.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 <type_traits>
27
28#include "utility/math/matrix.hpp"
29#include "utility/math/quaternion.hpp"
30#include "utility/math/vector.hpp"
31
32namespace utility::graphic
33{
34
39 template<typename Type>
40 concept CanBeOrientationComponent = std::is_floating_point_v<Type>;
41
52 template<CanBeOrientationComponent OrientationComponentType>
54 public utility::math::Quaternion<OrientationComponentType>
55 {
56 public:
63 : utility::math::Quaternion<OrientationComponentType>(
64 OrientationComponentType(0), OrientationComponentType(0),
65 OrientationComponentType(0), OrientationComponentType(1))
66 {
67 }
68
75 explicit Orientation(OrientationComponentType value)
76 : utility::math::Quaternion<OrientationComponentType>(value, value,
77 value, value)
78 {
79 }
80
85 explicit Orientation(
87 &quaternion)
88 : utility::math::Quaternion<OrientationComponentType>(quaternion)
89 {
90 }
91
99 Orientation(OrientationComponentType x, OrientationComponentType y,
100 OrientationComponentType z, OrientationComponentType w)
101 : utility::math::Quaternion<OrientationComponentType>(x, y, z, w)
102 {
103 }
104
111 Orientation(std::initializer_list<OrientationComponentType> values)
112 : utility::math::Quaternion<OrientationComponentType>(
113 OrientationComponentType(0), OrientationComponentType(0),
114 OrientationComponentType(0), OrientationComponentType(1))
115 {
116 if (values.size() != 4) {
117 throw std::invalid_argument(
118 "Orientation requires exactly four components");
119 }
120 const auto it = values.begin();
121 this->x = *it;
122 this->y = *(it + 1);
123 this->z = *(it + 2);
124 this->w = *(it + 3);
125 }
126
133 const std::initializer_list<OrientationComponentType> &value)
134 : utility::math::Quaternion<OrientationComponentType>(value)
135 {
136 }
137
142 Orientation(const Orientation &other) = default;
143
148 Orientation(Orientation &&other) noexcept = default;
149
155 Orientation &operator=(const Orientation &other) = default;
156
162 Orientation &operator=(Orientation &&other) noexcept = default;
163
167 ~Orientation(void) = default;
168
174 getForward(void) const noexcept
175 {
178 this->x, this->y, this->z, this->w)
180 OrientationComponentType(0), OrientationComponentType(0),
181 OrientationComponentType(-1) });
182 }
183
189 getUp(void) const noexcept
190 {
193 this->x, this->y, this->z, this->w)
195 OrientationComponentType(0), OrientationComponentType(1),
196 OrientationComponentType(0) });
197 }
198
204 getRight(void) const noexcept
205 {
208 this->x, this->y, this->z, this->w)
210 OrientationComponentType(1), OrientationComponentType(0),
211 OrientationComponentType(0) });
212 }
213
228
240
246 Orientation combined(const Orientation &parent) const noexcept
247 {
248 return Orientation(
250 parent.x, parent.y, parent.z, parent.w)
252 this->x, this->y, this->z, this->w));
253 }
254
265 {
266 return math::Matrix4x4F(math::toMat4(
268 this->x, this->y, this->z, this->w)));
269 }
270
275 Orientation normalized(void) const noexcept
276 {
277 return Orientation(
279 utility::math::normalize(
280 static_cast<const utility::math::Quaternion<
281 OrientationComponentType> &>(*this))));
282 }
283
288 Orientation inversed(void) const noexcept
289 {
290 return Orientation(
292 utility::math::inverse(
293 static_cast<const utility::math::Quaternion<
294 OrientationComponentType> &>(*this))));
295 }
296
302 bool isIdentity(OrientationComponentType epsilon =
303 OrientationComponentType { 1e-6 }) const noexcept
304 {
305 return std::abs(this->x) <= epsilon && std::abs(this->y) <= epsilon
306 && std::abs(this->z) <= epsilon
307 && std::abs(this->w - OrientationComponentType { 1 })
308 <= epsilon;
309 }
310
316 bool operator==(const Orientation &other) const noexcept
317 {
318 if (this->x != other.x) {
319 return false;
320 }
321 if (this->y != other.y) {
322 return false;
323 }
324 if (this->z != other.z) {
325 return false;
326 }
327 if (this->w != other.w) {
328 return false;
329 }
330 return true;
331 }
332
338 bool operator!=(const Orientation &other) const noexcept
339 {
340 return !(*this == other);
341 }
342
349 bool operator<(const Orientation &other) const noexcept
350 {
351 if (this->x != other.x) {
352 return this->x < other.x;
353 }
354 if (this->y != other.y) {
355 return this->y < other.y;
356 }
357 if (this->z != other.z) {
358 return this->z < other.z;
359 }
360 if (this->w != other.w) {
361 return this->w < other.w;
362 }
363 return false;
364 }
365 };
366
370 using OrientationF = Orientation<float>;
371
375 using OrientationD = Orientation<double>;
376
383 std::ostream &operator<<(std::ostream &stream,
384 const OrientationF &orientation);
385
392 std::ostream &operator<<(std::ostream &stream,
393 const OrientationD &orientation);
394
395} // namespace utility::graphic
Represents the current 3D orientation (pose) of an object or view.
Orientation(Orientation &&other) noexcept=default
Move constructor.
Orientation inversed(void) const noexcept
Return inverse orientation.
Orientation(const std::initializer_list< OrientationComponentType > &value)
Construct from a const initializer list of three PositionComponentType values.
Orientation(OrientationComponentType value)
Construct by filling all components with the same OrientationComponentType value.
bool operator==(const Orientation &other) const noexcept
Equality operator for Orientation.
Orientation(OrientationComponentType x, OrientationComponentType y, OrientationComponentType z, OrientationComponentType w)
Construct from explicit quaternion components (xyzw order).
Orientation & operator=(Orientation &&other) noexcept=default
Move assignment operator.
Orientation(void)
Default constructor (identity orientation).
bool operator<(const Orientation &other) const noexcept
Less-than operator for Orientation.
Orientation(std::initializer_list< OrientationComponentType > values)
Construct from initializer list of three PositionComponentType values.
utility::math::Vector< OrientationComponentType, 3 > getForward(void) const noexcept
Get the forward (Z-) axis in world space.
bool operator!=(const Orientation &other) const noexcept
Inequality operator for Orientation.
~Orientation(void)=default
Default destructor for Orientation.
Orientation(const Orientation &other)=default
Copy constructor.
utility::math::Vector< OrientationComponentType, 3 > getRight(void) const noexcept
Get the right (X+) axis in world space.
math::Matrix4x4F toRotationMatrix(void) const noexcept
Convert the orientation to a 4x4 rotation matrix.
utility::math::Vector< OrientationComponentType, 3 > rotateVector(const utility::math::Vector< OrientationComponentType, 3 > &direction) const noexcept
Rotate a direction vector by this orientation.
Orientation normalized(void) const noexcept
Return a normalized orientation.
Orientation(const utility::math::Quaternion< OrientationComponentType > &quaternion)
Construct from a quaternion.
utility::math::Vector< OrientationComponentType, 3 > getUp(void) const noexcept
Get the up (Y+) axis in world space.
Orientation & operator=(const Orientation &other)=default
Copy assignment operator.
bool isIdentity(OrientationComponentType epsilon=OrientationComponentType { 1e-6 }) const noexcept
Check whether this is approximately the identity orientation.
Orientation combined(const Orientation &parent) const noexcept
Combine with another orientation (parent * this).
utility::math::Vector< OrientationComponentType, 3 > transformPoint(const utility::math::Vector< OrientationComponentType, 3 > &local) const noexcept
Transform a point from local to world space.
M x N matrix class inheriting from glm::mat<Cols, Rows, Type>.
Definition matrix.hpp:92
Quaternion class inheriting from glm::qua.
Quaternion()
Default constructor (identity quaternion).
3D vector class inheriting from glm::vec3.
Definition vector.hpp:83
Concept to constrain orientation component type.
The utility namespace contains classes and functions for the utility project.
Definition cache.hpp:34