Plasma Engine  2.0
Loading...
Searching...
No Matches
Mat4.h
1#pragma once
2
3#include <Foundation/Math/Angle.h>
4#include <Foundation/Math/Math.h>
5#include <Foundation/Math/Vec3.h>
6#include <Foundation/Math/Vec4.h>
7
9template <typename Type>
11{
12public:
13 PL_DECLARE_POD_TYPE();
14
15 using ComponentType = Type;
16
17 // *** Data ***
18public:
19 // The elements are stored in column-major order.
20 // That means first is column 0 (with elements of row 0, row 1, row 2, row 3),
21 // then column 1, then column 2 and finally column 3
22
24 Type m_fElementsCM[16];
25
26 PL_ALWAYS_INLINE Type& Element(plInt32 iColumn, plInt32 iRow) { return m_fElementsCM[iColumn * 4 + iRow]; }
27 PL_ALWAYS_INLINE Type Element(plInt32 iColumn, plInt32 iRow) const { return m_fElementsCM[iColumn * 4 + iRow]; }
28
29 // *** Constructors ***
30public:
32 plMat4Template(); // [tested]
33
41 plMat4Template(const Type* const pData, plMatrixLayout::Enum layout); // [tested]
42
44 plMat4Template(Type c1r1, Type c2r1, Type c3r1, Type c4r1, Type c1r2, Type c2r2, Type c3r2, Type c4r2, Type c1r3, Type c2r3, Type c3r3, Type c4r3,
45 Type c1r4, Type c2r4, Type c3r4, Type c4r4); // [tested]
46
48 plMat4Template(const plMat3Template<Type>& mRotation, const plVec3Template<Type>& vTranslation); // [tested]
49
50#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
51 void AssertNotNaN() const
52 {
53 PL_ASSERT_ALWAYS(!IsNaN(), "This object contains NaN values. This can happen when you forgot to initialize it before using it. Please "
54 "check that all code-paths properly initialize this object.");
55 }
56#endif
57
59 [[nodiscard]] static plMat4Template<Type> MakeZero();
60
62 [[nodiscard]] static plMat4Template<Type> MakeIdentity();
63
65 [[nodiscard]] static plMat4Template<Type> MakeFromRowMajorArray(const Type* const pData);
66
68 [[nodiscard]] static plMat4Template<Type> MakeFromColumnMajorArray(const Type* const pData);
69
71 [[nodiscard]] static plMat4Template<Type> MakeFromValues(Type c1r1, Type c2r1, Type c3r1, Type c4r1, Type c1r2, Type c2r2, Type c3r2, Type c4r2, Type c1r3, Type c2r3, Type c3r3, Type c4r3, Type c1r4, Type c2r4, Type c3r4, Type c4r4);
72
74 [[nodiscard]] static plMat4Template<Type> MakeTranslation(const plVec3Template<Type>& vTranslation);
75
77 [[nodiscard]] static plMat4Template<Type> MakeTransformation(const plMat3Template<Type>& mRotation, const plVec3Template<Type>& vTranslation);
78
80 [[nodiscard]] static plMat4Template<Type> MakeScaling(const plVec3Template<Type>& vScale);
81
83 [[nodiscard]] static plMat4Template<Type> MakeRotationX(plAngle angle);
84
86 [[nodiscard]] static plMat4Template<Type> MakeRotationY(plAngle angle);
87
89 [[nodiscard]] static plMat4Template<Type> MakeRotationZ(plAngle angle);
90
92 [[nodiscard]] static plMat4Template<Type> MakeAxisRotation(const plVec3Template<Type>& vAxis, plAngle angle);
93
96 void GetAsArray(Type* out_pData, plMatrixLayout::Enum layout) const; // [tested]
97
99 void SetTransformationMatrix(const plMat3Template<Type>& mRotation, const plVec3Template<Type>& vTranslation); // [tested]
100
101 // *** Special matrix constructors ***
102public:
104 void SetZero(); // [tested]
105
107 void SetIdentity(); // [tested]
108
109 // *** Common Matrix Operations ***
110public:
112 void Transpose(); // [tested]
113
115 const plMat4Template<Type> GetTranspose() const; // [tested]
116
118 plResult Invert(Type fEpsilon = plMath::SmallEpsilon<Type>()); // [tested]
119
121 const plMat4Template<Type> GetInverse(Type fEpsilon = plMath::SmallEpsilon<Type>()) const; // [tested]
122
123 // *** Checks ***
124public:
126 bool IsZero(Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
127
129 bool IsIdentity(Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
130
132 bool IsValid() const; // [tested]
133
135 bool IsNaN() const; // [tested]
136
137 // *** Special Accessors ***
138public:
140 plVec4Template<Type> GetRow(plUInt32 uiRow) const; // [tested]
141
143 void SetRow(plUInt32 uiRow, const plVec4Template<Type>& vRow); // [tested]
144
146 plVec4Template<Type> GetColumn(plUInt32 uiColumn) const; // [tested]
147
149 void SetColumn(plUInt32 uiColumn, const plVec4Template<Type>& vColumn); // [tested]
150
152 plVec4Template<Type> GetDiagonal() const; // [tested]
153
155 void SetDiagonal(const plVec4Template<Type>& vDiag); // [tested]
156
158 const plVec3Template<Type> GetTranslationVector() const; // [tested]
159
161 void SetTranslationVector(const plVec3Template<Type>& v); // [tested]
162
164 void SetRotationalPart(const plMat3Template<Type>& mRotation); // [tested]
165
167 const plMat3Template<Type> GetRotationalPart() const; // [tested]
168
170 const plVec3Template<Type> GetScalingFactors() const; // [tested]
171
174 plResult SetScalingFactors(const plVec3Template<Type>& vXYZ, Type fEpsilon = plMath::DefaultEpsilon<Type>()); // [tested]
175
176 // *** Operators ***
177public:
179 const plVec3Template<Type> TransformPosition(const plVec3Template<Type>& v) const; // [tested]
180
182 void TransformPosition(plVec3Template<Type>* pV, plUInt32 uiNumVectors, plUInt32 uiStride = sizeof(plVec3Template<Type>)) const; // [tested]
183
186 const plVec3Template<Type> TransformDirection(const plVec3Template<Type>& v) const; // [tested]
187
190 void TransformDirection(plVec3Template<Type>* pV, plUInt32 uiNumVectors, plUInt32 uiStride = sizeof(plVec3Template<Type>)) const; // [tested]
191
193 const plVec4Template<Type> Transform(const plVec4Template<Type>& v) const; // [tested]
194
196 void Transform(plVec4Template<Type>* pV, plUInt32 uiNumVectors, plUInt32 uiStride = sizeof(plVec4Template<Type>)) const; // [tested]
197
199 void operator*=(Type f); // [tested]
200
202 void operator/=(Type f); // [tested]
203
205 bool IsIdentical(const plMat4Template<Type>& rhs) const; // [tested]
206
208 bool IsEqual(const plMat4Template<Type>& rhs, Type fEpsilon) const; // [tested]
209};
210
211// *** free functions ***
212
214template <typename Type>
215const plMat4Template<Type> operator*(const plMat4Template<Type>& m1, const plMat4Template<Type>& m2); // [tested]
216
218template <typename Type>
219const plVec3Template<Type> operator*(const plMat4Template<Type>& m, const plVec3Template<Type>& v); // [tested]
220
222template <typename Type>
223const plVec4Template<Type> operator*(const plMat4Template<Type>& m, const plVec4Template<Type>& v); // [tested]
224
226template <typename Type>
227const plMat4Template<Type> operator*(const plMat4Template<Type>& m1, Type f); // [tested]
228
230template <typename Type>
231const plMat4Template<Type> operator*(Type f, const plMat4Template<Type>& m1); // [tested]
232
234template <typename Type>
235const plMat4Template<Type> operator/(const plMat4Template<Type>& m1, Type f); // [tested]
236
238template <typename Type>
239const plMat4Template<Type> operator+(const plMat4Template<Type>& m1, const plMat4Template<Type>& m2); // [tested]
240
242template <typename Type>
243const plMat4Template<Type> operator-(const plMat4Template<Type>& m1, const plMat4Template<Type>& m2); // [tested]
244
246template <typename Type>
247bool operator==(const plMat4Template<Type>& lhs, const plMat4Template<Type>& rhs); // [tested]
248
250template <typename Type>
251bool operator!=(const plMat4Template<Type>& lhs, const plMat4Template<Type>& rhs); // [tested]
252
253#include <Foundation/Math/Implementation/Mat4_inl.h>
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
A 3x3 component matrix class.
Definition Mat3.h:9
A 4x4 component matrix class.
Definition Mat4.h:11
plMat4Template()
Default Constructor DOES NOT INITIALIZE the matrix, at all.
Definition Mat4_inl.h:6
static plMat4Template< Type > MakeAxisRotation(const plVec3Template< Type > &vAxis, plAngle angle)
Creates a matrix that is a rotation matrix around the given axis.
Definition AllClasses_inl.h:260
void SetIdentity()
Sets all elements to zero, except the diagonal, which is set to one.
Definition Mat4_inl.h:243
const plVec3Template< Type > GetTranslationVector() const
Returns the first 3 components of the last column.
Definition Mat4_inl.h:430
static plMat4Template< Type > MakeIdentity()
Returns an identity matrix.
Definition Mat4_inl.h:75
static plMat4Template< Type > MakeScaling(const plVec3Template< Type > &vScale)
Creates a matrix with all zero values, except along the diagonal, which is set to x,...
Definition Mat4_inl.h:158
plVec4Template< Type > GetColumn(plUInt32 uiColumn) const
Returns all 4 components of the i-th column.
Definition Mat4_inl.h:304
static plMat4Template< Type > MakeTransformation(const plMat3Template< Type > &mRotation, const plVec3Template< Type > &vTranslation)
Creates a transformation matrix from a rotation and a translation.
Definition Mat4_inl.h:150
const plMat3Template< Type > GetRotationalPart() const
Returns the 3x3 rotational and scaling part of the matrix.
Definition Mat4_inl.h:458
static plMat4Template< Type > MakeFromValues(Type c1r1, Type c2r1, Type c3r1, Type c4r1, Type c1r2, Type c2r2, Type c3r2, Type c4r2, Type c1r3, Type c2r3, Type c3r3, Type c4r3, Type c1r4, Type c2r4, Type c3r4, Type c4r4)
Creates a matrix from 16 values. Naming is "column-n row-m".
Definition Mat4_inl.h:120
static plMat4Template< Type > MakeFromRowMajorArray(const Type *const pData)
Creates a matrix from 16 values that are in row-major layout.
Definition Mat4_inl.h:98
bool IsValid() const
Checks whether all components are finite numbers.
Definition Mat4_inl.h:713
static plMat4Template< Type > MakeRotationY(plAngle angle)
Creates a matrix that is a rotation matrix around the Y-axis.
Definition Mat4_inl.h:190
static plMat4Template< Type > MakeRotationX(plAngle angle)
Creates a matrix that is a rotation matrix around the X-axis.
Definition Mat4_inl.h:181
void SetTranslationVector(const plVec3Template< Type > &v)
Sets the first 3 components of the last column.
Definition Mat4_inl.h:438
Type m_fElementsCM[16]
The matrix as a 16-element Type array (column-major)
Definition Mat4.h:24
plVec4Template< Type > GetDiagonal() const
Returns all 4 components on the diagonal of the matrix.
Definition Mat4_inl.h:330
const plVec3Template< Type > TransformPosition(const plVec3Template< Type > &v) const
Matrix-vector multiplication, assuming the 4th component of the vector is one (default behavior).
Definition Mat4_inl.h:347
const plMat4Template< Type > GetTranspose() const
Returns the transpose of this matrix.
Definition Mat4_inl.h:260
const plVec4Template< Type > Transform(const plVec4Template< Type > &v) const
Matrix-vector multiplication.
Definition Mat4_inl.h:402
plResult SetScalingFactors(const plVec3Template< Type > &vXYZ, Type fEpsilon=plMath::DefaultEpsilon< Type >())
Tries to set the three scaling factors in the matrix. Returns PL_FAILURE if the matrix columns cannot...
Definition Mat4_inl.h:750
static plMat4Template< Type > MakeRotationZ(plAngle angle)
Creates a matrix that is a rotation matrix around the Z-axis.
Definition Mat4_inl.h:199
void SetDiagonal(const plVec4Template< Type > &vDiag)
Sets all 4 components on the diagonal of the matrix.
Definition Mat4_inl.h:338
void Transpose()
Transposes this matrix.
Definition Mat4_inl.h:249
static plMat4Template< Type > MakeTranslation(const plVec3Template< Type > &vTranslation)
Creates a matrix with all zero values, except the last column, which is set to x, y,...
Definition Mat4_inl.h:143
static plMat4Template< Type > MakeZero()
Returns a zero matrix.
Definition Mat4_inl.h:64
const plVec3Template< Type > TransformDirection(const plVec3Template< Type > &v) const
Matrix-vector multiplication, assuming the 4th component of the vector is zero. So,...
Definition Mat4_inl.h:374
bool IsZero(Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Checks whether all elements are zero.
Definition Mat4_inl.h:655
void SetTransformationMatrix(const plMat3Template< Type > &mRotation, const plVec3Template< Type > &vTranslation)
Sets a transformation matrix from a rotation and a translation.
Definition Mat4_inl.h:208
bool IsIdentity(Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Checks whether this is an identity matrix.
Definition Mat4_inl.h:669
void SetRotationalPart(const plMat3Template< Type > &mRotation)
Sets the 3x3 rotational part of the matrix.
Definition Mat4_inl.h:446
bool IsIdentical(const plMat4Template< Type > &rhs) const
Equality Check.
Definition Mat4_inl.h:611
bool IsNaN() const
Checks whether any component is NaN.
Definition Mat4_inl.h:725
void SetColumn(plUInt32 uiColumn, const plVec4Template< Type > &vColumn)
Sets all 4 components of the i-th column.
Definition Mat4_inl.h:319
plVec4Template< Type > GetRow(plUInt32 uiRow) const
Returns all 4 components of the i-th row.
Definition Mat4_inl.h:278
const plVec3Template< Type > GetScalingFactors() const
Returns the 3 scaling factors that are encoded in the matrix.
Definition Mat4_inl.h:737
void GetAsArray(Type *out_pData, plMatrixLayout::Enum layout) const
Copies the 16 values of this matrix into the given array. 'layout' defines whether the data should en...
Definition Mat4_inl.h:216
const plMat4Template< Type > GetInverse(Type fEpsilon=plMath::SmallEpsilon< Type >()) const
Returns the inverse of this matrix.
Definition Mat4_inl.h:268
void operator*=(Type f)
Component-wise multiplication (commutative)
Definition Mat4_inl.h:475
void operator/=(Type f)
Component-wise division.
Definition Mat4_inl.h:484
bool IsEqual(const plMat4Template< Type > &rhs, Type fEpsilon) const
Equality Check with epsilon.
Definition Mat4_inl.h:626
plResult Invert(Type fEpsilon=plMath::SmallEpsilon< Type >())
Inverts this matrix. Return value indicates whether the matrix could be inverted.
Definition AllClasses_inl.h:310
void SetRow(plUInt32 uiRow, const plVec4Template< Type > &vRow)
Sets all 4 components of the i-th row.
Definition Mat4_inl.h:293
static plMat4Template< Type > MakeFromColumnMajorArray(const Type *const pData)
Creates a matrix from 16 values that are in column-major layout.
Definition Mat4_inl.h:112
void SetZero()
Sets all elements to zero.
Definition Mat4_inl.h:237
A 3-component vector class.
Definition Vec3.h:9
A 4-component vector class.
Definition Vec4.h:9
Enum
Definition Declarations.h:65
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54