25#define _USE_MATH_DEFINES
33#include "utility/math/vector.hpp"
35namespace utility::graphic
42 template<
typename Type>
51 template<CanBePositionComponent PositionComponentType>
class Position:
59 : math::
Vector<PositionComponentType, 3>(
60 PositionComponentType { 0 })
71 : math::
Vector<PositionComponentType, 3>({ value, value, value })
91 Position(PositionComponentType x, PositionComponentType y,
92 PositionComponentType z)
93 : math::
Vector<PositionComponentType, 3>({ x, y, z })
103 Position(std::initializer_list<PositionComponentType> values)
104 : math::
Vector<PositionComponentType, 3>(
105 { PositionComponentType { 0 }, PositionComponentType { 0 },
106 PositionComponentType { 0 } })
108 if (values.size() != 3) {
109 throw std::invalid_argument(
110 "Position requires exactly three components");
112 const auto it = values.begin();
122 Position(
const std::initializer_list<PositionComponentType> &value)
123 : math::
Vector<PositionComponentType, 3>(value)
173 PositionComponentType
getX(
void)
const noexcept
193 PositionComponentType
getY(
void)
const noexcept
213 PositionComponentType
getZ(
void)
const noexcept
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;
269 bool isOrigin(PositionComponentType epsilon = PositionComponentType {
270 1e-6 })
const noexcept
272 return std::abs(this->x) <= epsilon && std::abs(this->y) <= epsilon
273 && std::abs(this->z) <= epsilon;
283 return Position((this->x + other.x) / PositionComponentType { 2 },
284 (this->y + other.y) / PositionComponentType { 2 },
285 (this->z + other.z) / PositionComponentType { 2 });
295 if (this->x != other.x) {
298 if (this->y != other.y) {
301 if (this->z != other.z) {
314 return !(*
this == other);
324 if (this->x != other.x) {
325 return this->x < other.x;
327 if (this->y != other.y) {
328 return this->y < other.y;
330 if (this->z != other.z) {
331 return this->z < other.z;
340 using PositionF = Position<float>;
345 using PositionD = Position<double>;
353 std::ostream &operator<<(std::ostream &stream,
const PositionF &position);
361 std::ostream &operator<<(std::ostream &stream,
const PositionD &position);
Position in 3D space represented as a vector of three PositionComponentType components (x,...
bool isOrigin(PositionComponentType epsilon=PositionComponentType { 1e-6 }) const noexcept
Check whether this position is at (or near) the origin.
Position(PositionComponentType x, PositionComponentType y, PositionComponentType z)
Construct from three PositionComponentType values (x, y, z).
~Position(void)=default
Default destructor for Position.
bool operator!=(const Position &other) const noexcept
Inequality comparison.
Position(const std::initializer_list< PositionComponentType > &value)
Construct from a GLM vector.
Position(PositionComponentType value)
Construct by filling all components with the same PositionComponentType value.
Position translated(const Position &offset) const noexcept
Return a translated copy of this position.
PositionComponentType getZ(void) const noexcept
Get the Z component of the position.
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.
Position(const utility::math::Vector< PositionComponentType, 3 > &vector)
Construct from a vector.
PositionComponentType getX(void) const noexcept
Get the X component of the position.
Position(std::initializer_list< PositionComponentType > values)
Construct from initializer list of three PositionComponentType values.
bool operator<(const Position &other) const noexcept
Less-than comparison for ordering.
PositionComponentType distanceTo(const Position &other) const
Compute Euclidean distance to another position.
bool operator==(const Position &other) const noexcept
Equality comparison.
PositionComponentType getY(void) const noexcept
Get the Y component of the position.
Position & setX(const PositionComponentType value) noexcept
Set the X component of the position.
Position & setZ(const PositionComponentType value) noexcept
Set the Z component of the position.
Position & translate(const Position &offset) noexcept
Translate this position by an offset.
Position midpoint(const Position &other) const noexcept
Compute midpoint between this position and another.
Position(void)
Default constructor initializing position to (0, 0, 0).
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.
3D vector class inheriting from glm::vec3.
Vector(void)
Default constructor initializing vector values to zero.
Concept to constrain position component type.
Concept to constrain vector component type.
The utility namespace contains classes and functions for the utility project.