A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Vector.hh
Go to the documentation of this file.
1#pragma once
2
3#include "egg/math/Math.hh"
4
5#include "egg/util/Stream.hh"
6
7#include <format>
8
9namespace Kinoko::EGG {
10
12struct Vector2f {
13#ifdef BUILD_DEBUG
14 constexpr Vector2f()
15 : x(std::numeric_limits<f32>::signaling_NaN()),
16 y(std::numeric_limits<f32>::signaling_NaN()) {}
17#else
18 constexpr Vector2f() = default;
19#endif
20 constexpr Vector2f(f32 x_, f32 y_) : x(x_), y(y_) {}
21
22 constexpr ~Vector2f() = default;
23
24 constexpr inline void set(f32 val) {
25 x = y = val;
26 }
27
28 [[nodiscard]] constexpr Vector2f operator-() const {
29 return Vector2f(-x, -y);
30 }
31
32 [[nodiscard]] constexpr Vector2f operator-(const Vector2f &rhs) const {
33 return Vector2f(x - rhs.x, y - rhs.y);
34 }
35
36 [[nodiscard]] constexpr Vector2f operator+(const Vector2f &rhs) const {
37 return Vector2f(x + rhs.x, y + rhs.y);
38 }
39
40 constexpr Vector2f &operator+=(const Vector2f &rhs) {
41 return *this = *this + rhs;
42 }
43
44 [[nodiscard]] constexpr Vector2f operator*(const f32 scalar) const {
45 return Vector2f(x * scalar, y * scalar);
46 }
47
48 constexpr Vector2f &operator*=(const f32 scalar) {
49 return *this = *this * scalar;
50 }
51
52 friend constexpr Vector2f operator*(f32 scalar, const Vector2f &rhs) {
53 return Vector2f(scalar * rhs.x, scalar * rhs.y);
54 }
55
56 [[nodiscard]] constexpr f32 cross(const Vector2f &rhs) const {
57 return x * rhs.y - y * rhs.x;
58 }
59
60 [[nodiscard]] constexpr f32 dot(const Vector2f &rhs) const {
61 return x * rhs.x + y * rhs.y;
62 }
63
64 [[nodiscard]] constexpr f32 dot() const {
65 return x * x + y * y;
66 }
67
68 [[nodiscard]] constexpr f32 length() const {
69 return dot() > std::numeric_limits<f32>::epsilon() ? Mathf::sqrt(dot()) : 0.0f;
70 }
71
73 constexpr f32 normalise() {
74 f32 len = length();
75 if (len != 0.0f) {
76 *this = *this * (1.0f / len);
77 }
78
79 return len;
80 }
81
83 constexpr void normalise2() {
84 f32 sqLen = dot();
85 if (sqLen > std::numeric_limits<f32>::epsilon()) {
86 *this *= EGG::Mathf::frsqrt(sqLen);
87 }
88 }
89
91 void read(Stream &stream) {
92 x = stream.read_f32();
93 y = stream.read_f32();
94 }
95
98
99 static const Vector2f zero;
100 static const Vector2f ex, ey;
101};
102
103inline constexpr Vector2f Vector2f::zero = Vector2f(0.0f, 0.0f);
104inline constexpr Vector2f Vector2f::ex = Vector2f(1.0f, 0.0f);
105inline constexpr Vector2f Vector2f::ey = Vector2f(0.0f, 1.0f);
106
108struct Vector3f {
109#ifdef BUILD_DEBUG
110 constexpr Vector3f()
111 : x(std::numeric_limits<f32>::signaling_NaN()),
112 y(std::numeric_limits<f32>::signaling_NaN()),
113 z(std::numeric_limits<f32>::signaling_NaN()) {}
114#else
115 constexpr Vector3f() = default;
116#endif
117 constexpr Vector3f(f32 x_, f32 y_, f32 z_) : x(x_), y(y_), z(z_) {}
118
119 // NOTE: Defining the destructor in the header ensures the struct is trivially destructible
120 constexpr ~Vector3f() = default;
121
122 constexpr inline void setZero() {
123 set(0.0f);
124 }
125
126 constexpr inline void set(f32 val) {
127 x = y = z = val;
128 }
129
130 [[nodiscard]] constexpr Vector3f operator-() const {
131 return Vector3f(-x, -y, -z);
132 }
133
134 [[nodiscard]] constexpr Vector3f operator-(const Vector3f &rhs) const {
135 return Vector3f(x - rhs.x, y - rhs.y, z - rhs.z);
136 }
137
138 constexpr Vector3f &operator-=(const Vector3f &rhs) {
139 return *this = *this - rhs;
140 }
141
142 [[nodiscard]] constexpr Vector3f operator+(const Vector3f &rhs) const {
143 return Vector3f(x + rhs.x, y + rhs.y, z + rhs.z);
144 }
145
146 constexpr Vector3f &operator+=(const Vector3f &rhs) {
147 return *this = *this + rhs;
148 }
149
150 [[nodiscard]] constexpr Vector3f operator+(f32 val) const {
151 return Vector3f(x + val, y + val, z + val);
152 }
153
154 constexpr Vector3f &operator+=(f32 val) {
155 return *this = *this + val;
156 }
157
158 [[nodiscard]] constexpr Vector3f operator*(const Vector3f &rhs) const {
159 return Vector3f(x * rhs.x, y * rhs.y, z * rhs.z);
160 }
161
162 [[nodiscard]] constexpr Vector3f operator*(f32 scalar) const {
163 return Vector3f(x * scalar, y * scalar, z * scalar);
164 }
165
166 [[nodiscard]] friend constexpr Vector3f operator*(f32 scalar, const Vector3f &rhs) {
167 return rhs * scalar;
168 }
169
170 constexpr Vector3f &operator*=(f32 scalar) {
171 return *this = *this * scalar;
172 }
173
174 [[nodiscard]] constexpr Vector3f operator/(f32 scalar) const {
175 return Vector3f(x / scalar, y / scalar, z / scalar);
176 }
177
178 constexpr Vector3f &operator/=(f32 scalar) {
179 return *this = *this / scalar;
180 }
181
182 [[nodiscard]] constexpr bool operator==(const Vector3f &rhs) const {
183 return x == rhs.x && y == rhs.y && z == rhs.z;
184 }
185
186 [[nodiscard]] constexpr bool operator!=(const Vector3f &rhs) const {
187 return !(*this == rhs);
188 }
189
191 [[nodiscard]] explicit operator std::string() const {
192 return std::format("[0x{:08X}, 0x{:08X}, 0x{:08X}] | [{}, {}, {}]", f2u(x), f2u(y), f2u(z),
193 x, y, z);
194 }
195
197 [[nodiscard]] constexpr Vector3f cross(const Vector3f &rhs) const {
198 return Vector3f(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
199 }
200
202 [[nodiscard]] constexpr f32 squaredLength() const {
203 return x * x + y * y + z * z;
204 }
205
207 [[nodiscard]] constexpr f32 dot(const Vector3f &rhs) const {
208 return x * rhs.x + y * rhs.y + z * rhs.z;
209 }
210
212 [[nodiscard]] constexpr f32 length() const {
213 return Mathf::sqrt(squaredLength());
214 }
215
217 [[nodiscard]] constexpr f32 ps_length() const {
218 f32 dot = Mathf::fma(z, z, x * x + y * y);
219 return dot == 0.0f ? 0.0f : Mathf::sqrt(dot);
220 }
221
224 [[nodiscard]] constexpr Vector3f proj(const Vector3f &rhs) const {
225 return rhs * rhs.dot(*this);
226 }
227
230 [[nodiscard]] constexpr Vector3f rej(const Vector3f &rhs) const {
231 return *this - proj(rhs);
232 }
233
235 [[nodiscard]] constexpr std::pair<Vector3f, Vector3f> projAndRej(const Vector3f &rhs) const {
236 return std::pair(proj(rhs), rej(rhs));
237 }
238
240 [[nodiscard]] constexpr Vector3f abs() const {
241 return Vector3f(Mathf::abs(x), Mathf::abs(y), Mathf::abs(z));
242 }
243
245 [[nodiscard]] constexpr f32 sqDistance(const Vector3f &rhs) const {
246 const EGG::Vector3f diff = *this - rhs;
247 return diff.squaredLength();
248 }
249
252 [[nodiscard]] constexpr EGG::Vector3f multInv(f32 val) const {
253 return *this * (1.0f / val);
254 }
255
257 [[nodiscard]] constexpr f32 ps_dot() const {
258 return ps_dot(*this);
259 }
260
263 [[nodiscard]] constexpr f32 ps_dot(const Vector3f &rhs) const {
264 f32 y_ = y * rhs.y;
265 f32 xy = Mathf::fma(x, rhs.x, y_);
266 return xy + z * rhs.z;
267 }
268
270 [[nodiscard]] constexpr f32 ps_squareMag() const {
271 f32 x_ = x * x;
272 f32 zx = Mathf::fma(z, z, x_);
273 return zx + y * y;
274 }
275
279 constexpr f32 normalise() {
280 f32 len = 0.0f;
281
282 if (squaredLength() > std::numeric_limits<f32>::epsilon()) {
283 len = length();
284 *this *= (1.0f / len);
285 }
286
287 return len;
288 }
289
291 [[nodiscard]] constexpr EGG::Vector3f ps_normalize() const {
292 f32 dot = Mathf::fma(z, z, x * x) + y * y;
293 return *this * EGG::Mathf::frsqrt(dot);
294 }
295
297 [[nodiscard]] constexpr std::pair<f32, EGG::Vector3f> ps_normalized() {
298 f32 mag = ps_length();
299 if (mag <= 0.0f) {
300 return std::make_pair(mag, EGG::Vector3f::zero);
301 }
302
303 return std::make_pair(mag, *this * (1.0f / mag));
304 }
305
307 constexpr void normalise2() {
308 f32 sqLen = squaredLength();
309 if (sqLen > std::numeric_limits<f32>::epsilon()) {
310 *this *= Mathf::frsqrt(sqLen);
311 }
312 }
313
316 [[nodiscard]] constexpr Vector3f maximize(const Vector3f &rhs) const {
317 Vector3f out;
318
319 out.x = x > rhs.x ? x : rhs.x;
320 out.y = y > rhs.y ? y : rhs.y;
321 out.z = z > rhs.z ? z : rhs.z;
322
323 return out;
324 }
325
328 [[nodiscard]] constexpr Vector3f minimize(const Vector3f &rhs) const {
329 Vector3f out;
330
331 out.x = x < rhs.x ? x : rhs.x;
332 out.y = y < rhs.y ? y : rhs.y;
333 out.z = z < rhs.z ? z : rhs.z;
334
335 return out;
336 }
337
340 [[nodiscard]] constexpr f32 ps_sqDistance(const Vector3f &rhs) const {
341 const EGG::Vector3f diff = *this - rhs;
342 return diff.ps_dot();
343 }
344
347 [[nodiscard]] constexpr Vector3f perpInPlane(const EGG::Vector3f &rhs, bool normalise) const {
348 if (Mathf::abs(dot(rhs)) == 1.0f) {
349 return EGG::Vector3f::zero;
350 }
351
352 f32 _x = (rhs.z * x - rhs.x * z) * rhs.z - (rhs.x * y - rhs.y * x) * rhs.y;
353 f32 _y = (rhs.x * y - rhs.y * x) * rhs.x - (rhs.y * z - rhs.z * y) * rhs.z;
354 f32 _z = (rhs.y * z - rhs.z * y) * rhs.y - (rhs.z * x - rhs.x * z) * rhs.x;
355
356 EGG::Vector3f ret(_x, _y, _z);
357
358 if (normalise) {
359 ret.normalise();
360 }
361
362 return ret;
363 }
364
371 [[nodiscard]] constexpr EGG::Vector3f interpolate(f32 t, const EGG::Vector3f &v) const {
372 return *this + (v - *this) * t;
373 }
374
376 void read(Stream &stream) {
377 x = stream.read_f32();
378 y = stream.read_f32();
379 z = stream.read_f32();
380 }
381
385
386 static const Vector3f zero;
387 static const Vector3f unit;
388 static const Vector3f ex, ey, ez;
389 static const Vector3f inf;
390};
391
392inline constexpr Vector3f Vector3f::zero = Vector3f(0.0f, 0.0f, 0.0f);
393inline constexpr Vector3f Vector3f::unit = Vector3f(1.0f, 1.0f, 1.0f);
394inline constexpr Vector3f Vector3f::ex = Vector3f(1.0f, 0.0f, 0.0f);
395inline constexpr Vector3f Vector3f::ey = Vector3f(0.0f, 1.0f, 0.0f);
396inline constexpr Vector3f Vector3f::ez = Vector3f(0.0f, 0.0f, 1.0f);
397
399inline constexpr Vector3f Vector3f::inf = Vector3f(std::numeric_limits<f32>::infinity(),
400 std::numeric_limits<f32>::infinity(), std::numeric_limits<f32>::infinity());
401
402} // namespace Kinoko::EGG
A stream of data, abstracted to allow for continuous seeking.
Definition Stream.hh:8
EGG core library.
Definition Allocator.hh:5
float f32
Definition Memory.hh:21
A 2D float vector.
Definition Vector.hh:12
static const Vector2f zero
Definition Vector.hh:99
constexpr void normalise2()
Definition Vector.hh:83
constexpr Vector2f & operator*=(const f32 scalar)
Definition Vector.hh:48
void read(Stream &stream)
Initializes a Vector2f by reading 8 bytes from the stream.
Definition Vector.hh:91
constexpr Vector2f operator-(const Vector2f &rhs) const
Definition Vector.hh:32
constexpr Vector2f operator+(const Vector2f &rhs) const
Definition Vector.hh:36
constexpr void set(f32 val)
Definition Vector.hh:24
constexpr f32 length() const
Definition Vector.hh:68
constexpr f32 dot() const
Definition Vector.hh:64
friend constexpr Vector2f operator*(f32 scalar, const Vector2f &rhs)
Definition Vector.hh:52
constexpr f32 cross(const Vector2f &rhs) const
Definition Vector.hh:56
constexpr f32 normalise()
Definition Vector.hh:73
static const Vector2f ey
Definition Vector.hh:100
static const Vector2f ex
Definition Vector.hh:100
constexpr Vector2f(f32 x_, f32 y_)
Definition Vector.hh:20
constexpr Vector2f()=default
constexpr Vector2f operator-() const
Definition Vector.hh:28
constexpr ~Vector2f()=default
constexpr Vector2f operator*(const f32 scalar) const
Definition Vector.hh:44
constexpr Vector2f & operator+=(const Vector2f &rhs)
Definition Vector.hh:40
constexpr f32 dot(const Vector2f &rhs) const
Definition Vector.hh:60
A 3D float vector.
Definition Vector.hh:108
constexpr std::pair< f32, EGG::Vector3f > ps_normalized()
Definition Vector.hh:297
constexpr EGG::Vector3f multInv(f32 val) const
Multiplies a vector by the inverse of val.
Definition Vector.hh:252
constexpr void normalise2()
Definition Vector.hh:307
constexpr f32 squaredLength() const
The dot product between the vector and itself.
Definition Vector.hh:202
constexpr f32 ps_dot() const
Paired-singles dot product implementation.
Definition Vector.hh:257
constexpr Vector3f & operator-=(const Vector3f &rhs)
Definition Vector.hh:138
constexpr Vector3f & operator+=(const Vector3f &rhs)
Definition Vector.hh:146
constexpr f32 ps_length() const
Definition Vector.hh:217
constexpr Vector3f()=default
void read(Stream &stream)
Initializes a Vector3f by reading 12 bytes from the stream.
Definition Vector.hh:376
constexpr Vector3f abs() const
Returns the absolute value of each element of the vector.
Definition Vector.hh:240
static const Vector3f zero
Definition Vector.hh:386
constexpr Vector3f & operator+=(f32 val)
Definition Vector.hh:154
static const Vector3f ey
Definition Vector.hh:388
constexpr Vector3f operator-(const Vector3f &rhs) const
Definition Vector.hh:134
constexpr Vector3f operator-() const
Definition Vector.hh:130
static const Vector3f ez
Definition Vector.hh:388
static const Vector3f inf
Definition Vector.hh:389
constexpr Vector3f operator+(f32 val) const
Definition Vector.hh:150
constexpr Vector3f operator+(const Vector3f &rhs) const
Definition Vector.hh:142
constexpr f32 ps_sqDistance(const Vector3f &rhs) const
Paired-singles impl. of sqDistance.
Definition Vector.hh:340
constexpr void set(f32 val)
Definition Vector.hh:126
constexpr f32 sqDistance(const Vector3f &rhs) const
The square of the distance between two vectors.
Definition Vector.hh:245
constexpr f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
Definition Vector.hh:207
constexpr f32 length() const
The square root of the vector's dot product.
Definition Vector.hh:212
constexpr Vector3f proj(const Vector3f &rhs) const
The projection of this vector onto rhs.
Definition Vector.hh:224
constexpr f32 ps_dot(const Vector3f &rhs) const
Paired-singles dot product implementation.
Definition Vector.hh:263
constexpr Vector3f operator*(const Vector3f &rhs) const
Definition Vector.hh:158
friend constexpr Vector3f operator*(f32 scalar, const Vector3f &rhs)
Definition Vector.hh:166
constexpr f32 normalise()
Normalizes the vector and returns the original length.
Definition Vector.hh:279
constexpr void setZero()
Definition Vector.hh:122
constexpr Vector3f cross(const Vector3f &rhs) const
Definition Vector.hh:197
constexpr Vector3f(f32 x_, f32 y_, f32 z_)
Definition Vector.hh:117
constexpr Vector3f minimize(const Vector3f &rhs) const
Returns a vector whose elements are the min of the elements of both vectors.
Definition Vector.hh:328
constexpr EGG::Vector3f ps_normalize() const
Definition Vector.hh:291
constexpr Vector3f & operator*=(f32 scalar)
Definition Vector.hh:170
static const Vector3f ex
Definition Vector.hh:388
constexpr Vector3f & operator/=(f32 scalar)
Definition Vector.hh:178
constexpr EGG::Vector3f interpolate(f32 t, const EGG::Vector3f &v) const
Linearly interpolates this vector towards the provided vector at the provided rate.
Definition Vector.hh:371
constexpr bool operator==(const Vector3f &rhs) const
Definition Vector.hh:182
constexpr Vector3f rej(const Vector3f &rhs) const
The rejection of this vector onto rhs.
Definition Vector.hh:230
static const Vector3f unit
Definition Vector.hh:387
constexpr Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
Definition Vector.hh:316
constexpr ~Vector3f()=default
constexpr Vector3f perpInPlane(const EGG::Vector3f &rhs, bool normalise) const
Calculates the orthogonal vector, based on the plane defined by this vector and rhs.
Definition Vector.hh:347
constexpr Vector3f operator*(f32 scalar) const
Definition Vector.hh:162
constexpr std::pair< Vector3f, Vector3f > projAndRej(const Vector3f &rhs) const
Definition Vector.hh:235
constexpr bool operator!=(const Vector3f &rhs) const
Definition Vector.hh:186
constexpr f32 ps_squareMag() const
Differs from ps_dot due to variation in which operands are fused.
Definition Vector.hh:270
constexpr Vector3f operator/(f32 scalar) const
Definition Vector.hh:174