SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
Vector.h
1#pragma once
2
3#include <array>
4#include <cmath>
5#include <functional>
6#include <numeric>
7
8namespace spark::math::details
9{
10 template <template <typename> typename Impl, typename T, std::size_t N>
11 class Vector
12 {
13 public:
15 explicit constexpr Vector(std::array<std::reference_wrapper<T>, N> values)
16 : m_values(values) {}
17
23 constexpr bool operator==(const Vector& other) const noexcept
24 {
25 for (std::size_t i = 0; i < m_values.size(); i++)
26 if (m_values[i].get() != other.m_values[i].get())
27 return false;
28 return true;
29 }
30
36 constexpr bool operator!=(const Vector& other) const noexcept
37 {
38 return !operator==(other);
39 }
40
46 constexpr Impl<T> operator+(const Impl<T>& other) const noexcept
47 {
48 Impl<T> result;
49 for (std::size_t i = 0; i < N; ++i)
50 result.m_values[i].get() = m_values[i] + other.m_values[i];
51 return result;
52 }
53
59 constexpr Impl<T> operator-(const Impl<T>& other) const noexcept
60 {
61 Impl<T> result;
62 for (std::size_t i = 0; i < N; ++i)
63 result.m_values[i].get() = m_values[i] - other.m_values[i];
64 return result;
65 }
66
72 constexpr Impl<T> operator*(const Impl<T>& other) const noexcept
73 {
74 Impl<T> result;
75 for (std::size_t i = 0; i < N; ++i)
76 result.m_values[i].get() = m_values[i] * other.m_values[i];
77 return result;
78 }
79
85 constexpr Impl<T> operator/(const Impl<T>& other) const noexcept
86 {
87 Impl<T> result;
88 for (std::size_t i = 0; i < N; ++i)
89 result.m_values[i].get() = m_values[i] / other.m_values[i];
90 return result;
91 }
92
98 constexpr Impl<T> operator*(const T& value) const noexcept
99 {
100 Impl<T> result;
101 for (std::size_t i = 0; i < N; ++i)
102 result.m_values[i].get() = m_values[i] * value;
103 return result;
104 }
105
111 constexpr Impl<T> operator/(const T& value) const noexcept
112 {
113 Impl<T> result;
114 for (std::size_t i = 0; i < N; ++i)
115 result.m_values[i].get() = m_values[i] / value;
116 return result;
117 }
118
124 constexpr Impl<T>& operator+=(const Vector& other) noexcept
125 {
126 for (std::size_t i = 0; i < N; ++i)
127 m_values[i] += other.m_values[i];
128 return static_cast<Impl<T>&>(*this);
129 }
130
136 constexpr Impl<T>& operator-=(const Vector& other) noexcept
137 {
138 for (std::size_t i = 0; i < N; ++i)
139 m_values[i] -= other.m_values[i];
140 return static_cast<Impl<T>&>(*this);
141 }
142
148 constexpr Impl<T>& operator*=(const Vector& other) noexcept
149 {
150 for (std::size_t i = 0; i < N; ++i)
151 m_values[i] *= other.m_values[i];
152 return static_cast<Impl<T>&>(*this);
153 }
154
160 constexpr Impl<T>& operator/=(const T& value) noexcept
161 {
162 for (std::size_t i = 0; i < N; ++i)
163 m_values[i] /= value;
164 return static_cast<Impl<T>&>(*this);
165 }
166
171 constexpr Impl<T> operator-() const noexcept
172 {
173 Impl<T> result;
174 for (std::size_t i = 0; i < N; ++i)
175 result.m_values[i].get() = -m_values[i];
176 return result;
177 }
178
183 [[nodiscard]] constexpr T norm() const noexcept
184 {
185 static_assert(std::is_floating_point_v<T>, "The norm can only be computed for floating point types.");
186
187 T value {};
188 for (std::size_t i = 0; i < N; ++i)
189 value += m_values[i].get() * m_values[i].get();
190 return std::sqrt(value);
191 }
192
197 [[nodiscard]] constexpr Impl<T> normalized() const noexcept
198 {
199 return *this / norm();
200 }
201
207 [[nodiscard]] constexpr T dot(const Impl<T>& other) const noexcept
208 {
209 T value {};
210 for (std::size_t i = 0; i < N; ++i)
211 value += m_values[i].get() * other.m_values[i].get();
212 return value;
213 }
214
220 friend constexpr void swap(Vector& lhs, Vector& rhs) noexcept
221 {
222 for (std::size_t i = 0; i < N; ++i)
223 {
224 T temp = lhs.m_values[i];
225 lhs.m_values[i].get() = rhs.m_values[i].get();
226 rhs.m_values[i].get() = temp;
227 }
228 }
229
230 private:
231 std::array<std::reference_wrapper<T>, N> m_values;
232 };
233}
Definition Vector.h:12
constexpr Vector(std::array< std::reference_wrapper< T >, N > values)
Initializes a new Vector.
Definition Vector.h:15
constexpr Impl< T > operator*(const T &value) const noexcept
Multiplies a Vector by a value of type T.
Definition Vector.h:98
constexpr Impl< T > operator-() const noexcept
Negates all components of the current Vector.
Definition Vector.h:171
constexpr Impl< T > operator*(const Impl< T > &other) const noexcept
Multiplies two Vector.
Definition Vector.h:72
constexpr Impl< T > normalized() const noexcept
Gets the normalized Vector. (v / ||v||)
Definition Vector.h:197
constexpr Impl< T > operator/(const T &value) const noexcept
Divides a Vector by a value of type T.
Definition Vector.h:111
constexpr Impl< T > & operator+=(const Vector &other) noexcept
Adds the value of other to the current vector.
Definition Vector.h:124
constexpr Impl< T > operator-(const Impl< T > &other) const noexcept
Subtracts two Vector.
Definition Vector.h:59
friend constexpr void swap(Vector &lhs, Vector &rhs) noexcept
Swaps two Vector.
Definition Vector.h:220
constexpr T norm() const noexcept
Computes the norm of the Vector. (||v|| = sqrt(v1^2 + v2^2 + ... + vn^2))
Definition Vector.h:183
constexpr Impl< T > operator/(const Impl< T > &other) const noexcept
Divides two Vector.
Definition Vector.h:85
constexpr bool operator==(const Vector &other) const noexcept
Compares two Vector for equality.
Definition Vector.h:23
constexpr T dot(const Impl< T > &other) const noexcept
Computes the dot product between the current Vector and other.
Definition Vector.h:207
constexpr Impl< T > & operator/=(const T &value) noexcept
Divides the value of other from the current vector.
Definition Vector.h:160
constexpr Impl< T > operator+(const Impl< T > &other) const noexcept
Adds two Vector.
Definition Vector.h:46
constexpr Impl< T > & operator*=(const Vector &other) noexcept
Multiplies the value of other to the current vector.
Definition Vector.h:148
constexpr bool operator!=(const Vector &other) const noexcept
Compares two Vector for inequality.
Definition Vector.h:36
constexpr Impl< T > & operator-=(const Vector &other) noexcept
Subtracts the value of other to the current vector.
Definition Vector.h:136