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
45 void makeZero() {
46 *this = Matrix34f::zero;
47 }
48
49 void makeOrthonormalBasis(const Vector3f &v0, const Vector3f &v1);
50 void setAxisRotation(f32 angle, const Vector3f &axis);
51 void mulRow33(size_t rowIdx, const Vector3f &row);
52 void setBase(size_t col, const Vector3f &base);
53
54 [[nodiscard]] Matrix34f multiplyTo(const Matrix34f &rhs) const;
55 [[nodiscard]] Vector3f multVector(const Vector3f &vec) const;
56 [[nodiscard]] Vector3f ps_multVector(const Vector3f &vec) const;
57 [[nodiscard]] Vector3f multVector33(const Vector3f &vec) const;
58 [[nodiscard]] Vector3f ps_multVector33(const Vector3f &vec) const;
59 void inverseTo33(Matrix34f &out) const;
60 bool ps_inverse(Matrix34f &out) const;
61 [[nodiscard]] Matrix34f transpose() const;
62
63 [[nodiscard]] Vector3f translation() const {
64 return Vector3f(mtx[0][3], mtx[1][3], mtx[2][3]);
65 }
66
69 [[nodiscard]] Vector3f base(size_t col) const {
70 return Vector3f(mtx[0][col], mtx[1][col], mtx[2][col]);
71 }
72
73 static const Matrix34f ident;
74 static const Matrix34f zero;
75
76private:
77 union {
78 std::array<std::array<f32, 4>, 3> mtx;
79 std::array<f32, 12> a;
80 };
81};
82
84inline constexpr Matrix34f Matrix34f::ident(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
85 0.0f, 1.0f, 0.0f);
86
87inline constexpr Matrix34f Matrix34f::zero(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
88 0.0f, 0.0f, 0.0f);
89
90} // 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:138
Matrix34f multiplyTo(const Matrix34f &rhs) const
Multiplies two matrices.
Definition Matrix.cc:173
void setBase(size_t col, const Vector3f &base)
Sets one column of a matrix.
Definition Matrix.cc:165
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:69
void makeZero()
Zeroes every element of the matrix.
Definition Matrix.hh:45
void setAxisRotation(f32 angle, const Vector3f &axis)
Rotates the matrix about an axis.
Definition Matrix.cc:151
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:219
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:245
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:196
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:304
Vector3f ps_multVector(const Vector3f &vec) const
Paired-singles impl. of multVector.
Definition Matrix.cc:208
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:158
bool ps_inverse(Matrix34f &out) const
Definition Matrix.cc:274
Vector3f ps_multVector33(const Vector3f &vec) const
Paired-singles impl. of multVector33.
Definition Matrix.cc:231
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