Plasma Engine  2.0
Loading...
Searching...
No Matches
Mat3.h
1#pragma once
2
3#include <Foundation/Math/Angle.h>
4#include <Foundation/Math/Vec3.h>
5
7template <typename Type>
9{
10public:
11 PL_DECLARE_POD_TYPE();
12
13 using ComponentType = Type;
14
15 // *** Data ***
16public:
17 // The elements are stored in column-major order.
18 // That means first is column 0 (with elements of row 0, row 1, row 2),
19 // then column 1, then column 2
20
23
24 PL_ALWAYS_INLINE Type& Element(plInt32 iColumn, plInt32 iRow) { return m_fElementsCM[iColumn * 3 + iRow]; }
25 PL_ALWAYS_INLINE Type Element(plInt32 iColumn, plInt32 iRow) const { return m_fElementsCM[iColumn * 3 + iRow]; }
26
27 // *** Constructors ***
28public:
30 plMat3Template(); // [tested]
31
33 [[nodiscard]] static plMat3Template<Type> MakeZero();
34
36 [[nodiscard]] static plMat3Template<Type> MakeIdentity();
37
39 [[nodiscard]] static plMat3Template<Type> MakeFromRowMajorArray(const Type* const pData);
40
42 [[nodiscard]] static plMat3Template<Type> MakeFromColumnMajorArray(const Type* const pData);
43
45 [[nodiscard]] static plMat3Template<Type> MakeFromValues(Type c1r1, Type c2r1, Type c3r1, Type c1r2, Type c2r2, Type c3r2, Type c1r3, Type c2r3, Type c3r3);
46
48 [[nodiscard]] static plMat3Template<Type> MakeScaling(const plVec3Template<Type>& vScale);
49
51 [[nodiscard]] static plMat3Template<Type> MakeRotationX(plAngle angle);
52
54 [[nodiscard]] static plMat3Template<Type> MakeRotationY(plAngle angle);
55
57 [[nodiscard]] static plMat3Template<Type> MakeRotationZ(plAngle angle);
58
60 [[nodiscard]] static plMat3Template<Type> MakeAxisRotation(const plVec3Template<Type>& vAxis, plAngle angle);
61
62#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
63 void AssertNotNaN() const
64 {
65 PL_ASSERT_ALWAYS(!IsNaN(), "This object contains NaN values. This can happen when you forgot to initialize it before using it. Please check that "
66 "all code-paths properly initialize this object.");
67 }
68#endif
69
72 void GetAsArray(Type* out_pData, plMatrixLayout::Enum layout) const; // [tested]
73
74 // *** Special matrix constructors ***
75public:
77 void SetZero(); // [tested]
78
80 void SetIdentity(); // [tested]
81
82 // *** Common Matrix Operations ***
83public:
85 void Transpose(); // [tested]
86
88 const plMat3Template<Type> GetTranspose() const; // [tested]
89
91 plResult Invert(Type fEpsilon = plMath::SmallEpsilon<Type>()); // [tested]
92
94 const plMat3Template<Type> GetInverse(Type fEpsilon = plMath::SmallEpsilon<Type>()) const; // [tested]
95
96 // *** Checks ***
97public:
99 bool IsZero(Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
100
102 bool IsIdentity(Type fEpsilon = plMath::DefaultEpsilon<Type>()) const; // [tested]
103
105 bool IsValid() const; // [tested]
106
108 bool IsNaN() const; // [tested]
109
110 // *** Special Accessors ***
111public:
113 plVec3Template<Type> GetRow(plUInt32 uiRow) const; // [tested]
114
116 void SetRow(plUInt32 uiRow, const plVec3Template<Type>& vRow); // [tested]
117
119 plVec3Template<Type> GetColumn(plUInt32 uiColumn) const; // [tested]
120
122 void SetColumn(plUInt32 uiColumn, const plVec3Template<Type>& vColumn); // [tested]
123
125 plVec3Template<Type> GetDiagonal() const; // [tested]
126
128 void SetDiagonal(const plVec3Template<Type>& vDiag); // [tested]
129
131 const plVec3Template<Type> GetScalingFactors() const; // [tested]
132
135 plResult SetScalingFactors(const plVec3Template<Type>& vXYZ, Type fEpsilon = plMath::DefaultEpsilon<Type>()); // [tested]
136
138 Type GetDeterminant() const;
139
140 // *** Operators ***
141public:
143 const plVec3Template<Type> TransformDirection(const plVec3Template<Type>& v) const; // [tested]
144
146 void operator*=(Type f);
147
149 void operator/=(Type f); // [tested]
150
152 bool IsIdentical(const plMat3Template<Type>& rhs) const; // [tested]
153
155 bool IsEqual(const plMat3Template<Type>& rhs, Type fEpsilon) const; // [tested]
156};
157
158// *** free functions ***
159
161template <typename Type>
162const plMat3Template<Type> operator*(const plMat3Template<Type>& m1, const plMat3Template<Type>& m2); // [tested]
163
165template <typename Type>
166const plVec3Template<Type> operator*(const plMat3Template<Type>& m, const plVec3Template<Type>& v); // [tested]
167
169template <typename Type>
170const plMat3Template<Type> operator*(const plMat3Template<Type>& m1, Type f); // [tested]
171
173template <typename Type>
174const plMat3Template<Type> operator*(Type f, const plMat3Template<Type>& m1); // [tested]
175
177template <typename Type>
178const plMat3Template<Type> operator/(const plMat3Template<Type>& m1, Type f); // [tested]
179
181template <typename Type>
182const plMat3Template<Type> operator+(const plMat3Template<Type>& m1, const plMat3Template<Type>& m2); // [tested]
183
185template <typename Type>
186const plMat3Template<Type> operator-(const plMat3Template<Type>& m1, const plMat3Template<Type>& m2); // [tested]
187
189template <typename Type>
190bool operator==(const plMat3Template<Type>& lhs, const plMat3Template<Type>& rhs); // [tested]
191
193template <typename Type>
194bool operator!=(const plMat3Template<Type>& lhs, const plMat3Template<Type>& rhs); // [tested]
195
196#include <Foundation/Math/Implementation/Mat3_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
void SetDiagonal(const plVec3Template< Type > &vDiag)
Sets all 3 components on the diagonal of the matrix.
Definition Mat3_inl.h:236
bool IsZero(Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Checks whether all elements are zero.
Definition Mat3_inl.h:393
void SetZero()
Sets all elements to zero.
Definition Mat3_inl.h:144
plVec3Template< Type > GetRow(plUInt32 uiRow) const
Returns all 3 components of the i-th row.
Definition Mat3_inl.h:180
void SetColumn(plUInt32 uiColumn, const plVec3Template< Type > &vColumn)
Sets all 3 components of the i-th column.
Definition Mat3_inl.h:218
void Transpose()
Transposes this matrix.
Definition Mat3_inl.h:156
static plMat3Template< Type > MakeRotationX(plAngle angle)
Creates a matrix that is a rotation matrix around the X-axis.
Definition Mat3_inl.h:117
static plMat3Template< Type > MakeScaling(const plVec3Template< Type > &vScale)
Creates a matrix with all zero values, except along the diagonal, which is set to x,...
Definition Mat3_inl.h:101
static plMat3Template< Type > MakeAxisRotation(const plVec3Template< Type > &vAxis, plAngle angle)
Creates a matrix that is a rotation matrix around the given axis.
Definition AllClasses_inl.h:189
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 Mat3_inl.h:473
static plMat3Template< Type > MakeFromRowMajorArray(const Type *const pData)
Creates a matrix from 9 values that are in row-major layout.
Definition Mat3_inl.h:64
plResult Invert(Type fEpsilon=plMath::SmallEpsilon< Type >())
Inverts this matrix. Return value indicates whether the matrix could be Inverted.
Definition AllClasses_inl.h:230
bool IsEqual(const plMat3Template< Type > &rhs, Type fEpsilon) const
Equality Check with epsilon.
Definition Mat3_inl.h:364
bool IsIdentical(const plMat3Template< Type > &rhs) const
Equality Check.
Definition Mat3_inl.h:349
static plMat3Template< Type > MakeRotationZ(plAngle angle)
Creates a matrix that is a rotation matrix around the Z-axis.
Definition Mat3_inl.h:135
plVec3Template< Type > GetDiagonal() const
Returns all 3 components on the diagonal of the matrix.
Definition Mat3_inl.h:228
void operator*=(Type f)
Component-wise multiplication (commutative)
Definition Mat3_inl.h:256
static plMat3Template< Type > MakeRotationY(plAngle angle)
Creates a matrix that is a rotation matrix around the Y-axis.
Definition Mat3_inl.h:126
const plMat3Template< Type > GetTranspose() const
Returns the transpose of this matrix.
Definition Mat3_inl.h:164
const plMat3Template< Type > GetInverse(Type fEpsilon=plMath::SmallEpsilon< Type >()) const
Returns the inverse of this matrix.
Definition Mat3_inl.h:170
void operator/=(Type f)
Component-wise division.
Definition Mat3_inl.h:265
plMat3Template()
Default Constructor DOES NOT INITIALIZE the matrix, at all.
Definition Mat3_inl.h:6
void SetIdentity()
Sets all elements to zero, except the diagonal, which is set to one.
Definition Mat3_inl.h:150
static plMat3Template< Type > MakeFromValues(Type c1r1, Type c2r1, Type c3r1, Type c1r2, Type c2r2, Type c3r2, Type c1r3, Type c2r3, Type c3r3)
Creates a matrix from 9 values. Naming is "column-n row-m".
Definition Mat3_inl.h:85
static plMat3Template< Type > MakeZero()
Returns a zero matrix.
Definition Mat3_inl.h:37
const plVec3Template< Type > TransformDirection(const plVec3Template< Type > &v) const
Matrix-vector multiplication, assuming the 4th component of the vector is zero. So,...
Definition Mat3_inl.h:244
static plMat3Template< Type > MakeIdentity()
Returns an identity matrix.
Definition Mat3_inl.h:48
bool IsIdentity(Type fEpsilon=plMath::DefaultEpsilon< Type >()) const
Checks whether this is an identity matrix.
Definition Mat3_inl.h:407
Type GetDeterminant() const
Computes the determinant of the matrix.
Definition Mat3_inl.h:501
void GetAsArray(Type *out_pData, plMatrixLayout::Enum layout) const
Copies the 9 values of this matrix into the given array. 'layout' defines whether the data should end...
Definition Mat3_inl.h:17
bool IsValid() const
Checks whether all components are finite numbers.
Definition Mat3_inl.h:436
Type m_fElementsCM[9]
The matrix as a 9-element Type array (column-major)
Definition Mat3.h:22
bool IsNaN() const
Checks whether any component is NaN.
Definition Mat3_inl.h:448
void SetRow(plUInt32 uiRow, const plVec3Template< Type > &vRow)
Sets all 3 components of the i-th row.
Definition Mat3_inl.h:194
const plVec3Template< Type > GetScalingFactors() const
Returns the 3 scaling factors that are encoded in the matrix.
Definition Mat3_inl.h:460
static plMat3Template< Type > MakeFromColumnMajorArray(const Type *const pData)
Creates a matrix from 9 values that are in column-major layout.
Definition Mat3_inl.h:77
plVec3Template< Type > GetColumn(plUInt32 uiColumn) const
Returns all 3 components of the i-th column.
Definition Mat3_inl.h:204
A 3-component vector class.
Definition Vec3.h:9
Enum
Definition Declarations.h:65
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54