LiteFX 0.4.1.2025
Computer Graphics Engine
Loading...
Searching...
No Matches
vector.hpp
1#pragma once
2
3#include <cassert>
4#include <algorithm>
5#include <array>
6#include <vector>
7#include <ranges>
8
9namespace LiteFX::Math {
10
21 template <typename T, unsigned DIM> requires
22 (DIM > 0) && std::is_standard_layout_v<T> && std::is_trivially_copyable_v<T>
23 struct Vector {
24 public:
28 static constexpr size_t vec_size = DIM;
29
33 using scalar_type = T;
34
39
40 protected:
41 using array_type = std::array<scalar_type, vec_size>;
42 array_type m_elements = { }; // NOLINT
43
44 public:
48 constexpr Vector() noexcept = default;
49
54 constexpr Vector(Vector&& _other) noexcept = default;
55
60 constexpr Vector(const Vector& _other) = default;
61
67 constexpr Vector& operator=(Vector&& _other) noexcept = default;
68
74 constexpr Vector& operator=(const Vector& _other) = default;
75
79 constexpr ~Vector() noexcept = default;
80
85 constexpr Vector(T val) noexcept {
86 std::fill(std::begin(m_elements), std::end(m_elements), val);
87 }
88
94 constexpr Vector(T x, T y) noexcept requires(DIM == 2)
95 {
96 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-constant-array-index)
97 m_elements[0] = x;
98 m_elements[1] = y;
99 // NOLINTEND(cppcoreguidelines-pro-bounds-constant-array-index)
100 }
101
108 constexpr Vector(T x, T y, T z) noexcept requires(DIM == 3)
109 {
110 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-constant-array-index)
111 m_elements[0] = x;
112 m_elements[1] = y;
113 m_elements[2] = z;
114 // NOLINTEND(cppcoreguidelines-pro-bounds-constant-array-index)
115 }
116
124 constexpr Vector(T x, T y, T z, T w) noexcept requires(DIM == 4)
125 {
126 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-constant-array-index)
127 m_elements[0] = x;
128 m_elements[1] = y;
129 m_elements[2] = z;
130 m_elements[3] = w;
131 // NOLINTEND(cppcoreguidelines-pro-bounds-constant-array-index)
132 }
133
138 constexpr explicit Vector(std::ranges::input_range auto&& input) noexcept requires
139 std::is_nothrow_convertible_v<std::ranges::range_value_t<decltype(input)>, T>
140 {
141 std::ranges::copy(input, std::begin(m_elements));
142 }
143
149 constexpr auto& operator=(std::ranges::input_range auto&& input) noexcept requires
150 std::is_nothrow_convertible_v<std::ranges::range_value_t<decltype(input)>, T>
151 {
152 std::ranges::copy(input, std::begin(m_elements));
153 return *this;
154 }
155
156 public:
165 constexpr T operator[](unsigned int i) const noexcept {
166 assert(i < DIM);
167
168 return m_elements[i % DIM]; // NOLINT
169 }
170
179 constexpr T& operator[](unsigned int i) noexcept {
180 assert(i < DIM);
181
182 return m_elements[i % DIM]; // NOLINT
183 }
184
189 constexpr auto begin() noexcept {
190 return m_elements.begin();
191 }
192
197 constexpr auto end() noexcept {
198 return m_elements.end();
199 }
200
205 constexpr auto cbegin() const noexcept {
206 return m_elements.cbegin();
207 }
208
213 constexpr auto cend() const noexcept {
214 return m_elements.cend();
215 }
216
217 public:
222 constexpr const scalar_type* elements() const noexcept {
223 return m_elements.data();
224 }
225
229 constexpr operator std::array<T, DIM>() const noexcept {
230 return m_elements;
231 }
232
236 constexpr operator std::vector<T>() const {
237 return std::vector<T>(std::begin(m_elements), std::end(m_elements));
238 }
239
244 constexpr int size() const noexcept {
245 return vec_size;
246 }
247
252 constexpr scalar_type x() const noexcept requires (DIM > 0) {
253 return m_elements[0]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
254 }
255
260 constexpr scalar_type& x() noexcept requires (DIM > 0) {
261 return m_elements[0]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
262 }
263
268 constexpr scalar_type y() const noexcept requires (DIM > 1) {
269 return m_elements[1]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
270 }
271
276 constexpr scalar_type& y() noexcept requires (DIM > 1) {
277 return m_elements[1]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
278 }
279
284 constexpr scalar_type z() const noexcept requires (DIM > 2) {
285 return m_elements[2]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
286 }
287
292 constexpr scalar_type& z() noexcept requires (DIM > 2) {
293 return m_elements[2]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
294 }
295
300 constexpr scalar_type w() const noexcept requires (DIM > 3) {
301 return m_elements[3]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
302 }
303
308 constexpr scalar_type& w() noexcept requires (DIM > 3) {
309 return m_elements[3]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
310 }
311 };
312
317 template<typename T> using TVector1 = Vector<T, 1>;
318
323 template<typename T> using TVector2 = Vector<T, 2>;
324
329 template<typename T> using TVector3 = Vector<T, 3>;
330
335 template<typename T> using TVector4 = Vector<T, 4>;
336
337}
Definition math.hpp:30
An algebraic vector type.
Definition vector.hpp:23
constexpr Vector(T x, T y, T z) noexcept
Initializes a 3D vector using the values provided by x , y and z .
Definition vector.hpp:108
constexpr auto cend() const noexcept
Returns a constant interator for that addresses the end of the vector elements.
Definition vector.hpp:213
constexpr Vector(std::ranges::input_range auto &&input) noexcept
Initializes the vector from an arbitrary input range.
Definition vector.hpp:138
constexpr T & operator[](unsigned int i) noexcept
Returns a reference to a value from the vector, indexed by the parameter i .
Definition vector.hpp:179
std::array< scalar_type, vec_size > array_type
Definition vector.hpp:41
constexpr int size() const noexcept
Returns the number of dimensions of the vector.
Definition vector.hpp:244
constexpr scalar_type w() const noexcept
Returns the value of the w component of the vector.
Definition vector.hpp:300
constexpr scalar_type x() const noexcept
Returns the value of the x component of the vector.
Definition vector.hpp:252
constexpr auto cbegin() const noexcept
Returns a constant interator for that addresses the begin of the vector elements.
Definition vector.hpp:205
constexpr const scalar_type * elements() const noexcept
Returns a pointer to the elements of the vector.
Definition vector.hpp:222
constexpr auto & operator=(std::ranges::input_range auto &&input) noexcept
Copies the values from an arbitrary input range into the current vector instance.
Definition vector.hpp:149
constexpr scalar_type z() const noexcept
Returns the value of the z component of the vector.
Definition vector.hpp:284
constexpr scalar_type & x() noexcept
Returns a reference of the value of the x component of the vector.
Definition vector.hpp:260
constexpr Vector(T x, T y) noexcept
Initializes a 2D vector using the values provided by x and y .
Definition vector.hpp:94
constexpr auto end() noexcept
Returns an interator for that addresses the end of the vector elements.
Definition vector.hpp:197
constexpr scalar_type & w() noexcept
Returns a reference of the value of the w component of the vector.
Definition vector.hpp:308
T scalar_type
The type of the vector elements.
Definition vector.hpp:33
constexpr auto begin() noexcept
Returns an interator for that addresses the begin of the vector elements.
Definition vector.hpp:189
constexpr scalar_type y() const noexcept
Returns the value of the y component of the vector.
Definition vector.hpp:268
constexpr scalar_type & z() noexcept
Returns a reference of the value of the z component of the vector.
Definition vector.hpp:292
constexpr T operator[](unsigned int i) const noexcept
Returns a value from the vector, indexed by the parameter i .
Definition vector.hpp:165
constexpr Vector() noexcept=default
Initializes an empty vector.
constexpr Vector(T x, T y, T z, T w) noexcept
Initializes a 4D vector using the values provided by x , y , z and w .
Definition vector.hpp:124
constexpr scalar_type & y() noexcept
Returns a reference of the value of the y component of the vector.
Definition vector.hpp:276