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
position.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#define _USE_MATH_DEFINES
26#include <cmath>
27#include <math.h>
28
29#include <stdexcept>
30#include <type_traits>
31#include <utility>
32
33#include "utility/math/vector.hpp"
34
35namespace utility::graphic
36{
37
42 template<typename Type>
44
51 template<CanBePositionComponent PositionComponentType> class Position:
52 public math::Vector<PositionComponentType, 3>
53 {
54 public:
59 : math::Vector<PositionComponentType, 3>(
60 PositionComponentType { 0 })
61 {
62 }
63
70 explicit Position(PositionComponentType value)
71 : math::Vector<PositionComponentType, 3>({ value, value, value })
72 {
73 }
74
79 explicit Position(
81 : utility::math::Vector<PositionComponentType, 3>(vector)
82 {
83 }
84
91 Position(PositionComponentType x, PositionComponentType y,
92 PositionComponentType z)
93 : math::Vector<PositionComponentType, 3>({ x, y, z })
94 {
95 }
96
103 Position(std::initializer_list<PositionComponentType> values)
104 : math::Vector<PositionComponentType, 3>(
105 { PositionComponentType { 0 }, PositionComponentType { 0 },
106 PositionComponentType { 0 } })
107 {
108 if (values.size() != 3) {
109 throw std::invalid_argument(
110 "Position requires exactly three components");
111 }
112 const auto it = values.begin();
113 this->x = *it;
114 this->y = *(it + 1);
115 this->z = *(it + 2);
116 }
117
122 Position(const std::initializer_list<PositionComponentType> &value)
123 : math::Vector<PositionComponentType, 3>(value)
124 {
125 }
126
131 Position(const Position &other) = default;
132
137 Position(Position &&other) noexcept = default;
138
144 Position &operator=(const Position &other) = default;
145
151 Position &operator=(Position &&other) noexcept = default;
152
156 ~Position(void) = default;
157
163 Position &setX(const PositionComponentType value) noexcept
164 {
165 this->x = value;
166 return *this;
167 }
168
173 PositionComponentType getX(void) const noexcept
174 {
175 return this->x;
176 }
177
183 Position &setY(const PositionComponentType value) noexcept
184 {
185 this->y = value;
186 return *this;
187 }
188
193 PositionComponentType getY(void) const noexcept
194 {
195 return this->y;
196 }
197
203 Position &setZ(const PositionComponentType value) noexcept
204 {
205 this->z = value;
206 return *this;
207 }
208
213 PositionComponentType getZ(void) const noexcept
214 {
215 return this->z;
216 }
217
223 Position &translate(const Position &offset) noexcept
224 {
225 *this += offset;
226 return *this;
227 }
228
234 Position translated(const Position &offset) const noexcept
235 {
236 Position result(*this);
237 result += offset;
238 return result;
239 }
240
246 PositionComponentType distanceSquaredTo(const Position &other) const
247 {
248 const PositionComponentType dx = this->x - other.x;
249 const PositionComponentType dy = this->y - other.y;
250 const PositionComponentType dz = this->z - other.z;
251 return dx * dx + dy * dy + dz * dz;
252 }
253
259 PositionComponentType distanceTo(const Position &other) const
260 {
261 return std::sqrt(distanceSquaredTo(other));
262 }
263
269 bool isOrigin(PositionComponentType epsilon = PositionComponentType {
270 1e-6 }) const noexcept
271 {
272 return std::abs(this->x) <= epsilon && std::abs(this->y) <= epsilon
273 && std::abs(this->z) <= epsilon;
274 }
275
281 Position midpoint(const Position &other) const noexcept
282 {
283 return Position((this->x + other.x) / PositionComponentType { 2 },
284 (this->y + other.y) / PositionComponentType { 2 },
285 (this->z + other.z) / PositionComponentType { 2 });
286 }
287
293 bool operator==(const Position &other) const noexcept
294 {
295 if (this->x != other.x) {
296 return false;
297 }
298 if (this->y != other.y) {
299 return false;
300 }
301 if (this->z != other.z) {
302 return false;
303 }
304 return true;
305 }
306
312 bool operator!=(const Position &other) const noexcept
313 {
314 return !(*this == other);
315 }
316
322 bool operator<(const Position &other) const noexcept
323 {
324 if (this->x != other.x) {
325 return this->x < other.x;
326 }
327 if (this->y != other.y) {
328 return this->y < other.y;
329 }
330 if (this->z != other.z) {
331 return this->z < other.z;
332 }
333 return false;
334 }
335 };
336
340 using PositionF = Position<float>;
341
345 using PositionD = Position<double>;
346
353 std::ostream &operator<<(std::ostream &stream, const PositionF &position);
354
361 std::ostream &operator<<(std::ostream &stream, const PositionD &position);
362
363} // namespace utility::graphic
Position in 3D space represented as a vector of three PositionComponentType components (x,...
Definition position.hpp:53
bool isOrigin(PositionComponentType epsilon=PositionComponentType { 1e-6 }) const noexcept
Check whether this position is at (or near) the origin.
Definition position.hpp:269
Position(PositionComponentType x, PositionComponentType y, PositionComponentType z)
Construct from three PositionComponentType values (x, y, z).
Definition position.hpp:91
~Position(void)=default
Default destructor for Position.
bool operator!=(const Position &other) const noexcept
Inequality comparison.
Definition position.hpp:312
Position(const std::initializer_list< PositionComponentType > &value)
Construct from a GLM vector.
Definition position.hpp:122
Position(PositionComponentType value)
Construct by filling all components with the same PositionComponentType value.
Definition position.hpp:70
Position translated(const Position &offset) const noexcept
Return a translated copy of this position.
Definition position.hpp:234
PositionComponentType getZ(void) const noexcept
Get the Z component of the position.
Definition position.hpp:213
Position(Position &&other) noexcept=default
Move constructor.
Position(const Position &other)=default
Copy constructor.
PositionComponentType distanceSquaredTo(const Position &other) const
Compute squared Euclidean distance to another position.
Definition position.hpp:246
Position(const utility::math::Vector< PositionComponentType, 3 > &vector)
Construct from a vector.
Definition position.hpp:79
PositionComponentType getX(void) const noexcept
Get the X component of the position.
Definition position.hpp:173
Position(std::initializer_list< PositionComponentType > values)
Construct from initializer list of three PositionComponentType values.
Definition position.hpp:103
bool operator<(const Position &other) const noexcept
Less-than comparison for ordering.
Definition position.hpp:322
PositionComponentType distanceTo(const Position &other) const
Compute Euclidean distance to another position.
Definition position.hpp:259
bool operator==(const Position &other) const noexcept
Equality comparison.
Definition position.hpp:293
PositionComponentType getY(void) const noexcept
Get the Y component of the position.
Definition position.hpp:193
Position & setX(const PositionComponentType value) noexcept
Set the X component of the position.
Definition position.hpp:163
Position & setZ(const PositionComponentType value) noexcept
Set the Z component of the position.
Definition position.hpp:203
Position & translate(const Position &offset) noexcept
Translate this position by an offset.
Definition position.hpp:223
Position midpoint(const Position &other) const noexcept
Compute midpoint between this position and another.
Definition position.hpp:281
Position(void)
Default constructor initializing position to (0, 0, 0).
Definition position.hpp:58
Position & operator=(Position &&other) noexcept=default
Move assignment operator.
Position & operator=(const Position &other)=default
Copy assignment operator.
Position & setY(const PositionComponentType value) noexcept
Set the Y component of the position.
Definition position.hpp:183
3D vector class inheriting from glm::vec3.
Definition vector.hpp:83
Vector(void)
Default constructor initializing vector values to zero.
Definition vector.hpp:88
Concept to constrain position component type.
Definition position.hpp:43
Concept to constrain vector component type.
Definition vector.hpp:72
The utility namespace contains classes and functions for the utility project.
Definition cache.hpp:34