Skip to main content

vec2.ixx File

2D vector type and utility functions. More...

Included Headers

#include <cassert> #include <cmath> #include <memory> #include <helios.math.traits.FloatingPointType> #include <helios.math.utils> #include <helios.math.concepts>

Namespaces Index

namespacehelios
namespacemath

Mathematical operations and types. More...

Classes Index

structvec2<T>

Represents a 2-dimensional vector of the generic type <T>. More...

Description

2D vector type and utility functions.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file vec2.ixx
3 * @brief 2D vector type and utility functions.
4 */
5module;
6
7#include <cassert>
8#include <cmath>
9#include <memory>
10
11export module helios.math.types:vec2;
12
13
14import helios.math.concepts;
15import helios.math.utils;
16import helios.math.traits.FloatingPointType;
17
18export namespace helios::math {
19
20 template<helios::math::Numeric T>
21 struct vec3;
22
23 /**
24 * @brief Represents a 2-dimensional vector of the generic type <T>.
25 *
26 * The `vec2` struct provides a lightweight and efficient way to handle 2D
27 * vector mathematics for the numeric types float, int, double. For convenient access,
28 * the shorthands `vec2f`, `vec2d` and `vec2i` are available.
29 *
30 * @tparam T The numeric type of the vector components.
31 */
32 template<helios::math::Numeric T>
33 struct vec2 {
34
35 private:
36 /**
37 * @brief Internal array storing the vector components.
38 */
39 T v[2];
40
41 public:
42
43 /**
44 * @brief Creates a new vec2 with its values initialized to (0, 0)
45 */
46 constexpr vec2() noexcept : v{static_cast<T>(0), static_cast<T>(0)} {}
47
48
49 /**
50 * @brief Constructs a new vec2 with the specified x, y components.
51 *
52 * @param x The value for the x component.
53 * @param y The value for the y component.
54 */
55 constexpr vec2(const T x, const T y) noexcept : v{x, y} {}
56
57 /**
58 * @brief Converts the current vec2<T> into a 3D vector representation.
59 *
60 * @return A 3D vector corresponding to the converted representation of the object or input.
61 */
62 constexpr helios::math::vec3<T> toVec3() const {
63 return helios::math::vec3<T>{v[0], v[1], static_cast<T>(0)};
64 }
65
66 /**
67 * @brief Provides read only access to the vector components.
68 * Bounds checking is performed via `assert` in debug builds.
69 *
70 * @param i The index to query
71 *
72 * @return A const ref to the requested component.
73 */
74 constexpr const T& operator[](const size_t i) const noexcept {
75 assert(i <= 1 && "vec2 - Index out of bounds.");
76 return this->v[i];
77 }
78
79 /**
80 * @brief Provides read-write access to the vector components.
81 * Bounds checking is performed via `assert` in debug builds.
82 *
83 * @param i The index to query
84 *
85 * @return A ref to the requested component.
86 */
87 constexpr T& operator[](const size_t i) noexcept {
88 assert(i <= 1 && "vec2 - Index out of bounds.");
89 return this->v[i];
90 }
91
92 /**
93 * @brief Returns the magnitude of this vec2<T>.
94 *
95 * @return The magnitude of this vector as type FloatingPointType<T>.
96 */
97 inline FloatingPointType<T> length() const noexcept {
98 if (v[0] == 0 && v[1] == 0) {
99 return static_cast<FloatingPointType<T>>(0);
100 }
101 return std::hypot(v[0], v[1]);
102 }
103
104
105 /**
106 * @brief Checks if this vector is normalized (unit length).
107 *
108 * @details A vector is considered normalized if its squared length
109 * equals 1 within the tolerance defined by EPSILON_LENGTH.
110 *
111 * @return true if the vector is approximately unit length, false otherwise.
112 */
113 constexpr bool isNormalized() const noexcept {
114 const auto lenSquared =
115 static_cast<FloatingPointType<T>>(v[0]) * static_cast<FloatingPointType<T>>(v[0]) +
116 static_cast<FloatingPointType<T>>(v[1]) * static_cast<FloatingPointType<T>>(v[1]);
117
118 return std::abs(lenSquared - static_cast<FloatingPointType<T>>(1.0)) <= helios::math::EPSILON_LENGTH;
119 }
120
121 /**
122 * @brief Normalizes this vec2<T>.
123 *
124 * @return The normalized FloatingPointType<T> vector.
125 */
126 inline vec2<FloatingPointType<T>> normalize() const noexcept {
127 if (v[0] == 0 && v[1] == 0) {
129 static_cast<FloatingPointType<T>>(0),
130 static_cast<FloatingPointType<T>>(0)
131 );
132 }
134 static_cast<FloatingPointType<T>>(v[0]) / length(),
135 static_cast<FloatingPointType<T>>(v[1]) / length()
136 );
137 }
138
139 /**
140 * @brief Strictly compares the elements of this vector with the elements
141 * of the rgt vector.
142 *
143 * @param rgt The right vector to compare for equal values
144 *
145 * @return True if all elements are equal (==), false otherwise.
146 */
147 constexpr bool operator==(const vec2<T>& rgt) const {
148 return v[0] == rgt[0] && v[1] == rgt[1];
149 }
150
151 /**
152 * @brief Compares this vector's elements with the rgt vector considering
153 * an epsilon value.
154 * Returns true if for all elements the equation |a-b| <= epsilon
155 * holds.
156 *
157 * @param rgt The other vector to compare with this vector for equality.
158 * @param epsilon The epsilon value to use for comparison. If omitted, the default epsilon (0.0001) is used.
159 *
160 * @return True if the elements of the vectors are considered equal,
161 * otherwise false.
162 *
163 * @see https://realtimecollisiondetection.net/blog/?p=89
164 *
165 * @todo account for abs (values close to zero) and rel
166 * (larger values), move epsilon to global constant?
167 */
168 constexpr bool same(const vec2<T>& rgt, T epsilon = 0.0001) const {
169 return std::fabs(v[0] - rgt[0]) <= epsilon &&
170 std::fabs(v[1] - rgt[1]) <= epsilon;
171 }
172
173
174
175 };
176
177 /**
178 * @brief Multiplies a 2D vector by a scalar value.
179 *
180 * @tparam T The numeric type of the vector components.
181 * @param v The vec2<T> vector to be multiplied.
182 * @param n The scalar vector to multiplay the vector by.
183 *
184 * @return a new vec2<T> instance representing the result of the scalar
185 * multiplication.
186 */
187 template<helios::math::Numeric T>
188 constexpr vec2<T> operator*(const vec2<T>& v, const T n) noexcept {
189 return vec2<T>{v[0] * n, v[1] * n};
190 }
191
192
193 /**
194 * @brief Computes the dot product of two 2D vectors.
195 *
196 * @tparam T The numeric type of the vector components.
197 * @param v1 The first vec2<T> vector.
198 * @param v2 The second vec2<T> vector.
199 *
200 * @return The dot product as a value of type T.
201 */
202 template<helios::math::Numeric T>
203 constexpr T dot(const vec2<T>& v1, const vec2<T>& v2) noexcept {
204 return v1[0]*v2[0] + v1[1]*v2[1];
205 }
206
211
212}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.