A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Matrix.hh
1#pragma once
2
3#include "egg/math/Quat.hh"
4
5namespace EGG {
6
8class Matrix34f {
9public:
10#ifdef BUILD_DEBUG
11 constexpr Matrix34f() {
12 a.fill(std::numeric_limits<f32>::signaling_NaN());
13 }
14#else
15 constexpr Matrix34f() = default;
16#endif
17
18 constexpr Matrix34f(f32 _e00, f32 _e01, f32 _e02, f32 _e03, f32 _e10, f32 _e11, f32 _e12,
19 f32 _e13, f32 _e20, f32 _e21, f32 _e22, f32 _e23)
20 : mtx{{_e00, _e01, _e02, _e03, _e10, _e11, _e12, _e13, _e20, _e21, _e22, _e23}} {}
21
22 ~Matrix34f() = default;
23
24 bool operator==(const Matrix34f &rhs) const {
25 return mtx == rhs.mtx;
26 }
27
29 [[nodiscard]] f32 &operator[](size_t row, size_t col) {
30 return mtx[row][col];
31 }
32
34 [[nodiscard]] f32 operator[](size_t row, size_t col) const {
35 return mtx[row][col];
36 }
37
38 void makeQT(const Quatf &q, const Vector3f &t);
39 void makeQ(const Quatf &q);
40 void makeRT(const Vector3f &r, const Vector3f &t);
41 void makeR(const Vector3f &r);
42 void makeS(const Vector3f &s);
43 void makeT(const Vector3f &t);
44
46 void makeZero() {
47 *this = Matrix34f::zero;
48 }
49
50 void makeOrthonormalBasis(const Vector3f &v0, const Vector3f &v1);
51 void setAxisRotation(f32 angle, const Vector3f &axis);
52 void mulRow33(size_t rowIdx, const Vector3f &row);
53 void setBase(size_t col, const Vector3f &base);
54
55 [[nodiscard]] Matrix34f multiplyTo(const Matrix34f &rhs) const;
56 [[nodiscard]] Vector3f multVector(const Vector3f &vec) const;
57 [[nodiscard]] Vector3f ps_multVector(const Vector3f &vec) const;
58 [[nodiscard]] Vector3f multVector33(const Vector3f &vec) const;
59 [[nodiscard]] Vector3f ps_multVector33(const Vector3f &vec) const;
60 void inverseTo33(Matrix34f &out) const;
61 bool ps_inverse(Matrix34f &out) const;
62 [[nodiscard]] Matrix34f transpose() const;
63
64 [[nodiscard]] Vector3f translation() const {
65 return Vector3f(mtx[0][3], mtx[1][3], mtx[2][3]);
66 }
67
70 [[nodiscard]] Vector3f base(size_t col) const {
71 return Vector3f(mtx[0][col], mtx[1][col], mtx[2][col]);
72 }
73
74 static const Matrix34f ident;
75 static const Matrix34f zero;
76
77private:
78 union {
79 std::array<std::array<f32, 4>, 3> mtx;
80 std::array<f32, 12> a;
81 };
82};
83
85inline constexpr Matrix34f Matrix34f::ident(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
86 0.0f, 1.0f, 0.0f);
87
88inline constexpr Matrix34f Matrix34f::zero(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
89 0.0f, 0.0f, 0.0f);
90
91} // namespace EGG
A 3 x 4 matrix.
Definition Matrix.hh:8
void makeOrthonormalBasis(const Vector3f &v0, const Vector3f &v1)
Sets a 3x3 orthonormal basis for a local coordinate system.
Definition Matrix.cc:154
Matrix34f multiplyTo(const Matrix34f &rhs) const
Multiplies two matrices.
Definition Matrix.cc:189
void setBase(size_t col, const Vector3f &base)
Sets one column of a matrix.
Definition Matrix.cc:181
f32 operator[](size_t row, size_t col) const
Accesses the matrix element at the specified row and column.
Definition Matrix.hh:34
Vector3f base(size_t col) const
Get a particular column from a matrix.
Definition Matrix.hh:70
void makeZero()
Zeroes every element of the matrix.
Definition Matrix.hh:46
void setAxisRotation(f32 angle, const Vector3f &axis)
Rotates the matrix about an axis.
Definition Matrix.cc:167
void makeQ(const Quatf &q)
Sets rotation matrix from quaternion.
Definition Matrix.cc:41
Vector3f multVector33(const Vector3f &vec) const
Multiplies a 3x3 matrix by a vector.
Definition Matrix.cc:236
f32 & operator[](size_t row, size_t col)
Accesses the matrix element at the specified row and column.
Definition Matrix.hh:29
void inverseTo33(Matrix34f &out) const
Inverts the 3x3 portion of the 3x4 matrix.
Definition Matrix.cc:262
void makeR(const Vector3f &r)
Sets 3x3 rotation matrix from a vector of Euler angles.
Definition Matrix.cc:98
Vector3f multVector(const Vector3f &vec) const
Multiplies a vector by a matrix.
Definition Matrix.cc:212
void makeRT(const Vector3f &r, const Vector3f &t)
Sets rotation-translation matrix.
Definition Matrix.cc:71
Matrix34f transpose() const
Transposes the 3x3 portion of the matrix.
Definition Matrix.cc:321
Vector3f ps_multVector(const Vector3f &vec) const
Paired-singles impl. of multVector.
Definition Matrix.cc:224
void makeQT(const Quatf &q, const Vector3f &t)
Sets matrix from rotation and position.
Definition Matrix.cc:11
void mulRow33(size_t rowIdx, const Vector3f &row)
Multiplies one row of a 3x3 matrix by a vector.
Definition Matrix.cc:174
bool ps_inverse(Matrix34f &out) const
Definition Matrix.cc:291
Vector3f ps_multVector33(const Vector3f &vec) const
Paired-singles impl. of multVector33.
Definition Matrix.cc:248
EGG core library.
Definition Archive.cc:6
A quaternion, used to represent 3D rotation.
Definition Quat.hh:12
A 3D float vector.
Definition Vector.hh:87