RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
Vector.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cmath>
5#include <cstddef>
6#include <limits>
7#include <ostream>
8#include <type_traits>
9
10namespace RST::Maths {
11
13 template<typename T>
14 struct Vector2D {
15 T x{}, y{};
16
17 constexpr Vector2D() noexcept = default;
18 constexpr Vector2D(T xValue, T yValue) noexcept : x(xValue), y(yValue) {}
19
20 constexpr void set(const T& newX, const T& newY) noexcept { x = newX; y = newY; }
21 [[nodiscard]] constexpr T getX() const noexcept { return x; }
22 [[nodiscard]] constexpr T getY() const noexcept { return y; }
23
24 [[nodiscard]] constexpr T& operator[](std::size_t n) noexcept {
25 assert(n < 2 && "Vector2D index out of range");
26 return n == 0 ? x : y;
27 }
28 [[nodiscard]] constexpr const T& operator[](std::size_t n) const noexcept {
29 assert(n < 2 && "Vector2D index out of range");
30 return n == 0 ? x : y;
31 }
32 [[nodiscard]] constexpr T* data() noexcept { return &x; }
33 [[nodiscard]] constexpr const T* data() const noexcept { return &x; }
34
35 [[nodiscard]] constexpr Vector2D operator+() const noexcept { return *this; }
36 [[nodiscard]] constexpr Vector2D operator-() const noexcept { return Vector2D(-x, -y); }
37
38 constexpr Vector2D& operator+=(const Vector2D& v) noexcept { x += v.x; y += v.y; return *this; }
39 constexpr Vector2D& operator-=(const Vector2D& v) noexcept { x -= v.x; y -= v.y; return *this; }
40 constexpr Vector2D& operator*=(const Vector2D& v) noexcept { x *= v.x; y *= v.y; return *this; }
41 constexpr Vector2D& operator/=(const Vector2D& v) noexcept {
42 if constexpr (std::is_integral_v<T>) {
43 assert(v.x != 0 && v.y != 0 && "Vector2D integer division by zero");
44 }
45 x /= v.x; y /= v.y;
46 return *this;
47 }
48
49 constexpr Vector2D& operator*=(T s) noexcept { x *= s; y *= s; return *this; }
50 constexpr Vector2D& operator/=(T s) noexcept {
51 if constexpr (std::is_integral_v<T>) {
52 assert(s != 0 && "Vector2D integer division by zero");
53 }
54 x /= s; y /= s;
55 return *this;
56 }
57 constexpr Vector2D& operator+=(T s) noexcept { x += s; y += s; return *this; }
58 constexpr Vector2D& operator-=(T s) noexcept { x -= s; y -= s; return *this; }
59
60 [[nodiscard]] constexpr bool operator==(const Vector2D&) const noexcept = default;
61
62 [[nodiscard]] constexpr T dot(const Vector2D& v) const noexcept { return x * v.x + y * v.y; }
64 [[nodiscard]] constexpr T cross(const Vector2D& v) const noexcept { return x * v.y - y * v.x; }
66 [[nodiscard]] constexpr Vector2D perpendicular() const noexcept { return Vector2D(-y, x); }
67
68 [[nodiscard]] constexpr auto lengthSquared() const noexcept { return x * x + y * y; }
69 [[nodiscard]] constexpr auto length() const noexcept { return std::sqrt(lengthSquared()); }
70 [[nodiscard]] constexpr auto distanceSquared(const Vector2D& v) const noexcept { return Vector2D(x - v.x, y - v.y).lengthSquared(); }
71 [[nodiscard]] constexpr auto distance(const Vector2D& v) const noexcept { return Vector2D(x - v.x, y - v.y).length(); }
72
74 constexpr Vector2D& normalize() noexcept {
75 const auto len = length();
76 if (len > std::numeric_limits<T>::epsilon())
77 *this /= static_cast<T>(len);
78 return *this;
79 }
80 [[nodiscard]] constexpr Vector2D normalized() const noexcept { return Vector2D(*this).normalize(); }
81
83 template<typename U>
84 [[nodiscard]] constexpr Vector2D<U> to() const noexcept {
85 return Vector2D<U>(static_cast<U>(x), static_cast<U>(y));
86 }
87
89 [[nodiscard]] constexpr Vector2D min(const Vector2D& v) const noexcept {
90 return Vector2D(v.x < x ? v.x : x, v.y < y ? v.y : y);
91 }
92
94 [[nodiscard]] constexpr Vector2D max(const Vector2D& v) const noexcept {
95 return Vector2D(x < v.x ? v.x : x, y < v.y ? v.y : y);
96 }
97 };
98
101 template<typename T>
102 [[nodiscard]] constexpr Vector2D<T> operator+(Vector2D<T> lhs, const Vector2D<T>& rhs) noexcept { return lhs += rhs; }
103 template<typename T>
104 [[nodiscard]] constexpr Vector2D<T> operator-(Vector2D<T> lhs, const Vector2D<T>& rhs) noexcept { return lhs -= rhs; }
105 template<typename T>
106 [[nodiscard]] constexpr Vector2D<T> operator*(Vector2D<T> lhs, const Vector2D<T>& rhs) noexcept { return lhs *= rhs; }
107 template<typename T>
108 [[nodiscard]] constexpr Vector2D<T> operator/(Vector2D<T> lhs, const Vector2D<T>& rhs) noexcept { return lhs /= rhs; }
109
110 template<typename T>
111 [[nodiscard]] constexpr Vector2D<T> operator*(Vector2D<T> v, std::type_identity_t<T> s) noexcept { return v *= s; }
112 template<typename T>
113 [[nodiscard]] constexpr Vector2D<T> operator*(std::type_identity_t<T> s, Vector2D<T> v) noexcept { return v *= s; }
114 template<typename T>
115 [[nodiscard]] constexpr Vector2D<T> operator/(Vector2D<T> v, std::type_identity_t<T> s) noexcept { return v /= s; }
116 template<typename T>
117 [[nodiscard]] constexpr Vector2D<T> operator+(Vector2D<T> v, std::type_identity_t<T> s) noexcept { return v += s; }
118 template<typename T>
119 [[nodiscard]] constexpr Vector2D<T> operator-(Vector2D<T> v, std::type_identity_t<T> s) noexcept { return v -= s; }
120
121 template<typename T>
122 std::ostream& operator<<(std::ostream& os, const Vector2D<T>& vec) {
123 return os << "(" << vec.getX() << ", " << vec.getY() << ")";
124 }
126
131
132
134 template<typename T>
135 struct Vector3D {
136 T x{}, y{}, z{};
137
138 constexpr Vector3D() noexcept = default;
139 constexpr Vector3D(T xValue, T yValue, T zValue) noexcept : x(xValue), y(yValue), z(zValue) {}
140
141 constexpr void set(const T& newX, const T& newY, const T& newZ) noexcept { x = newX; y = newY; z = newZ; }
142 [[nodiscard]] constexpr T getX() const noexcept { return x; }
143 [[nodiscard]] constexpr T getY() const noexcept { return y; }
144 [[nodiscard]] constexpr T getZ() const noexcept { return z; }
145
146 [[nodiscard]] constexpr T& operator[](std::size_t n) noexcept {
147 assert(n < 3 && "Vector3D index out of range");
148 return n == 0 ? x : (n == 1 ? y : z);
149 }
150 [[nodiscard]] constexpr const T& operator[](std::size_t n) const noexcept {
151 assert(n < 3 && "Vector3D index out of range");
152 return n == 0 ? x : (n == 1 ? y : z);
153 }
154 [[nodiscard]] constexpr T* data() noexcept { return &x; }
155 [[nodiscard]] constexpr const T* data() const noexcept { return &x; }
156
157 [[nodiscard]] constexpr Vector3D operator+() const noexcept { return *this; }
158 [[nodiscard]] constexpr Vector3D operator-() const noexcept { return Vector3D(-x, -y, -z); }
159
160 constexpr Vector3D& operator+=(const Vector3D& v) noexcept { x += v.x; y += v.y; z += v.z; return *this; }
161 constexpr Vector3D& operator-=(const Vector3D& v) noexcept { x -= v.x; y -= v.y; z -= v.z; return *this; }
162 constexpr Vector3D& operator*=(const Vector3D& v) noexcept { x *= v.x; y *= v.y; z *= v.z; return *this; }
163 constexpr Vector3D& operator/=(const Vector3D& v) noexcept {
164 if constexpr (std::is_integral_v<T>) {
165 assert(v.x != 0 && v.y != 0 && v.z != 0 && "Vector3D integer division by zero");
166 }
167 x /= v.x; y /= v.y; z /= v.z;
168 return *this;
169 }
170
171 constexpr Vector3D& operator*=(T s) noexcept { x *= s; y *= s; z *= s; return *this; }
172 constexpr Vector3D& operator/=(T s) noexcept {
173 if constexpr (std::is_integral_v<T>) {
174 assert(s != 0 && "Vector3D integer division by zero");
175 }
176 x /= s; y /= s; z /= s;
177 return *this;
178 }
179 constexpr Vector3D& operator+=(T s) noexcept { x += s; y += s; z += s; return *this; }
180 constexpr Vector3D& operator-=(T s) noexcept { x -= s; y -= s; z -= s; return *this; }
181
182 [[nodiscard]] constexpr bool operator==(const Vector3D&) const noexcept = default;
183
184 [[nodiscard]] constexpr T dot(const Vector3D& v) const noexcept { return x * v.x + y * v.y + z * v.z; }
185 [[nodiscard]] constexpr Vector3D cross(const Vector3D& v) const noexcept {
186 return Vector3D(
187 y * v.z - z * v.y,
188 z * v.x - x * v.z,
189 x * v.y - y * v.x
190 );
191 }
192
193 [[nodiscard]] constexpr auto lengthSquared() const noexcept { return x * x + y * y + z * z; }
194 [[nodiscard]] constexpr auto length() const noexcept { return std::sqrt(lengthSquared()); }
195 [[nodiscard]] constexpr auto distanceSquared(const Vector3D& v) const noexcept { return Vector3D(x - v.x, y - v.y, z - v.z).lengthSquared(); }
196 [[nodiscard]] constexpr auto distance(const Vector3D& v) const noexcept { return Vector3D(x - v.x, y - v.y, z - v.z).length(); }
197
199 constexpr Vector3D& normalize() noexcept {
200 const auto len = length();
201 if (len > std::numeric_limits<T>::epsilon())
202 *this /= static_cast<T>(len);
203 return *this;
204 }
205 [[nodiscard]] constexpr Vector3D normalized() const noexcept { return Vector3D(*this).normalize(); }
206
208 template<typename U>
209 [[nodiscard]] constexpr Vector3D<U> to() const noexcept {
210 return Vector3D<U>(static_cast<U>(x), static_cast<U>(y), static_cast<U>(z));
211 }
212
214 [[nodiscard]] constexpr Vector3D min(const Vector3D& v) const noexcept {
215 return Vector3D(v.x < x ? v.x : x, v.y < y ? v.y : y, v.z < z ? v.z : z);
216 }
217
219 [[nodiscard]] constexpr Vector3D max(const Vector3D& v) const noexcept {
220 return Vector3D(x < v.x ? v.x : x, y < v.y ? v.y : y, z < v.z ? v.z : z);
221 }
222 };
223
226 template<typename T>
227 [[nodiscard]] constexpr Vector3D<T> operator+(Vector3D<T> lhs, const Vector3D<T>& rhs) noexcept { return lhs += rhs; }
228 template<typename T>
229 [[nodiscard]] constexpr Vector3D<T> operator-(Vector3D<T> lhs, const Vector3D<T>& rhs) noexcept { return lhs -= rhs; }
230 template<typename T>
231 [[nodiscard]] constexpr Vector3D<T> operator*(Vector3D<T> lhs, const Vector3D<T>& rhs) noexcept { return lhs *= rhs; }
232 template<typename T>
233 [[nodiscard]] constexpr Vector3D<T> operator/(Vector3D<T> lhs, const Vector3D<T>& rhs) noexcept { return lhs /= rhs; }
234
235 template<typename T>
236 [[nodiscard]] constexpr Vector3D<T> operator*(Vector3D<T> v, std::type_identity_t<T> s) noexcept { return v *= s; }
237 template<typename T>
238 [[nodiscard]] constexpr Vector3D<T> operator*(std::type_identity_t<T> s, Vector3D<T> v) noexcept { return v *= s; }
239 template<typename T>
240 [[nodiscard]] constexpr Vector3D<T> operator/(Vector3D<T> v, std::type_identity_t<T> s) noexcept { return v /= s; }
241 template<typename T>
242 [[nodiscard]] constexpr Vector3D<T> operator+(Vector3D<T> v, std::type_identity_t<T> s) noexcept { return v += s; }
243 template<typename T>
244 [[nodiscard]] constexpr Vector3D<T> operator-(Vector3D<T> v, std::type_identity_t<T> s) noexcept { return v -= s; }
245
246 template<typename T>
247 std::ostream& operator<<(std::ostream& os, const Vector3D<T>& vec) {
248 return os << "(" << vec.getX() << ", " << vec.getY() << ", " << vec.getZ() << ")";
249 }
251
256
257}
Math helpers: constants, interpolation, random numbers, 2D and 3D vectors and matrices.
Definition Constants.hpp:5
constexpr Vector2D< T > operator-(Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
Definition Vector.hpp:104
constexpr Vector2D< T > operator*(Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
Definition Vector.hpp:106
constexpr Vector2D< T > operator/(Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
Definition Vector.hpp:108
constexpr Vector2D< T > operator+(Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
Definition Vector.hpp:102
std::ostream & operator<<(std::ostream &os, const Matrix< T, Rows, Cols > &mat)
Definition Matrix.hpp:380
2D vector; arithmetic operators work component-wise, with a vector or a scalar.
Definition Vector.hpp:14
constexpr bool operator==(const Vector2D &) const noexcept=default
constexpr Vector2D & operator-=(T s) noexcept
Definition Vector.hpp:58
constexpr Vector2D & operator+=(T s) noexcept
Definition Vector.hpp:57
constexpr Vector2D operator+() const noexcept
Definition Vector.hpp:35
constexpr Vector2D min(const Vector2D &v) const noexcept
Component-wise minimum.
Definition Vector.hpp:89
constexpr T & operator[](std::size_t n) noexcept
Definition Vector.hpp:24
constexpr T cross(const Vector2D &v) const noexcept
Z component of the 3D cross product, positive when v is counterclockwise from this vector.
Definition Vector.hpp:64
constexpr auto distanceSquared(const Vector2D &v) const noexcept
Definition Vector.hpp:70
T x
Definition Vector.hpp:15
constexpr Vector2D max(const Vector2D &v) const noexcept
Component-wise maximum.
Definition Vector.hpp:94
constexpr auto lengthSquared() const noexcept
Definition Vector.hpp:68
constexpr Vector2D & normalize() noexcept
Scales this vector to length 1; a zero vector is left unchanged.
Definition Vector.hpp:74
constexpr const T * data() const noexcept
Definition Vector.hpp:33
constexpr T * data() noexcept
Definition Vector.hpp:32
constexpr T dot(const Vector2D &v) const noexcept
Definition Vector.hpp:62
constexpr void set(const T &newX, const T &newY) noexcept
Definition Vector.hpp:20
constexpr Vector2D & operator/=(T s) noexcept
Definition Vector.hpp:50
constexpr Vector2D & operator+=(const Vector2D &v) noexcept
Definition Vector.hpp:38
constexpr Vector2D normalized() const noexcept
Definition Vector.hpp:80
constexpr Vector2D< U > to() const noexcept
Converts the components, such as v.to<int>().
Definition Vector.hpp:84
constexpr T getY() const noexcept
Definition Vector.hpp:22
constexpr Vector2D & operator-=(const Vector2D &v) noexcept
Definition Vector.hpp:39
constexpr Vector2D & operator*=(const Vector2D &v) noexcept
Definition Vector.hpp:40
constexpr Vector2D perpendicular() const noexcept
This vector rotated by 90 degrees counterclockwise.
Definition Vector.hpp:66
constexpr Vector2D() noexcept=default
constexpr auto distance(const Vector2D &v) const noexcept
Definition Vector.hpp:71
constexpr Vector2D operator-() const noexcept
Definition Vector.hpp:36
constexpr const T & operator[](std::size_t n) const noexcept
Definition Vector.hpp:28
constexpr T getX() const noexcept
Definition Vector.hpp:21
T y
Definition Vector.hpp:15
constexpr Vector2D & operator/=(const Vector2D &v) noexcept
Definition Vector.hpp:41
constexpr Vector2D & operator*=(T s) noexcept
Definition Vector.hpp:49
constexpr auto length() const noexcept
Definition Vector.hpp:69
3D vector; arithmetic operators work component-wise, with a vector or a scalar.
Definition Vector.hpp:135
constexpr auto distance(const Vector3D &v) const noexcept
Definition Vector.hpp:196
constexpr T getZ() const noexcept
Definition Vector.hpp:144
constexpr T dot(const Vector3D &v) const noexcept
Definition Vector.hpp:184
constexpr Vector3D & operator*=(const Vector3D &v) noexcept
Definition Vector.hpp:162
constexpr Vector3D & operator-=(const Vector3D &v) noexcept
Definition Vector.hpp:161
constexpr Vector3D< U > to() const noexcept
Converts the components, such as v.to<int>().
Definition Vector.hpp:209
T x
Definition Vector.hpp:136
constexpr void set(const T &newX, const T &newY, const T &newZ) noexcept
Definition Vector.hpp:141
constexpr T getX() const noexcept
Definition Vector.hpp:142
constexpr Vector3D operator+() const noexcept
Definition Vector.hpp:157
constexpr const T & operator[](std::size_t n) const noexcept
Definition Vector.hpp:150
constexpr Vector3D & operator-=(T s) noexcept
Definition Vector.hpp:180
constexpr auto distanceSquared(const Vector3D &v) const noexcept
Definition Vector.hpp:195
constexpr Vector3D & operator*=(T s) noexcept
Definition Vector.hpp:171
constexpr bool operator==(const Vector3D &) const noexcept=default
constexpr Vector3D operator-() const noexcept
Definition Vector.hpp:158
constexpr Vector3D normalized() const noexcept
Definition Vector.hpp:205
constexpr Vector3D & normalize() noexcept
Scales this vector to length 1; a zero vector is left unchanged.
Definition Vector.hpp:199
constexpr T getY() const noexcept
Definition Vector.hpp:143
constexpr Vector3D() noexcept=default
constexpr Vector3D & operator+=(const Vector3D &v) noexcept
Definition Vector.hpp:160
constexpr auto lengthSquared() const noexcept
Definition Vector.hpp:193
constexpr Vector3D cross(const Vector3D &v) const noexcept
Definition Vector.hpp:185
constexpr auto length() const noexcept
Definition Vector.hpp:194
constexpr Vector3D & operator/=(T s) noexcept
Definition Vector.hpp:172
constexpr Vector3D & operator/=(const Vector3D &v) noexcept
Definition Vector.hpp:163
constexpr Vector3D & operator+=(T s) noexcept
Definition Vector.hpp:179
constexpr const T * data() const noexcept
Definition Vector.hpp:155
T z
Definition Vector.hpp:136
constexpr Vector3D min(const Vector3D &v) const noexcept
Component-wise minimum.
Definition Vector.hpp:214
constexpr T & operator[](std::size_t n) noexcept
Definition Vector.hpp:146
constexpr Vector3D max(const Vector3D &v) const noexcept
Component-wise maximum.
Definition Vector.hpp:219
constexpr T * data() noexcept
Definition Vector.hpp:154
T y
Definition Vector.hpp:136