Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Mat44.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
10
12class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) Mat44
13{
14public:
16
17 // Underlying column type
19
20 // Argument type
22
24 Mat44() = default;
25 JPH_INLINE Mat44(Vec4Arg inC1, Vec4Arg inC2, Vec4Arg inC3, Vec4Arg inC4);
26 JPH_INLINE Mat44(Vec4Arg inC1, Vec4Arg inC2, Vec4Arg inC3, Vec3Arg inC4);
27 Mat44(const Mat44 &inM2) = default;
28 Mat44 & operator = (const Mat44 &inM2) = default;
29 JPH_INLINE Mat44(Type inC1, Type inC2, Type inC3, Type inC4);
30
32 static JPH_INLINE Mat44 sZero();
33
35 static JPH_INLINE Mat44 sIdentity();
36
38 static JPH_INLINE Mat44 sNaN();
39
41 static JPH_INLINE Mat44 sLoadFloat4x4(const Float4 *inV);
42
44 static JPH_INLINE Mat44 sLoadFloat4x4Aligned(const Float4 *inV);
45
47 static JPH_INLINE Mat44 sRotationX(float inX);
48 static JPH_INLINE Mat44 sRotationY(float inY);
49 static JPH_INLINE Mat44 sRotationZ(float inZ);
50
52 static JPH_INLINE Mat44 sRotation(Vec3Arg inAxis, float inAngle);
53
55 static JPH_INLINE Mat44 sRotation(QuatArg inQuat);
56
58 static JPH_INLINE Mat44 sTranslation(Vec3Arg inV);
59
61 static JPH_INLINE Mat44 sRotationTranslation(QuatArg inR, Vec3Arg inT);
62
64 static JPH_INLINE Mat44 sInverseRotationTranslation(QuatArg inR, Vec3Arg inT);
65
67 static JPH_INLINE Mat44 sScale(float inScale);
68
70 static JPH_INLINE Mat44 sScale(Vec3Arg inV);
71
73 static JPH_INLINE Mat44 sOuterProduct(Vec3Arg inV1, Vec3Arg inV2);
74
76 static JPH_INLINE Mat44 sCrossProduct(Vec3Arg inV);
77
79 static JPH_INLINE Mat44 sQuatLeftMultiply(QuatArg inQ);
80
82 static JPH_INLINE Mat44 sQuatRightMultiply(QuatArg inQ);
83
88 static JPH_INLINE Mat44 sLookAt(Vec3Arg inPos, Vec3Arg inTarget, Vec3Arg inUp);
89
91 static JPH_INLINE Mat44 sPerspective(float inFovY, float inAspect, float inNear, float inFar);
92
94 JPH_INLINE float operator () (uint inRow, uint inColumn) const { JPH_ASSERT(inRow < 4); JPH_ASSERT(inColumn < 4); return mCol[inColumn].mF32[inRow]; }
95 JPH_INLINE float & operator () (uint inRow, uint inColumn) { JPH_ASSERT(inRow < 4); JPH_ASSERT(inColumn < 4); return mCol[inColumn].mF32[inRow]; }
96
98 JPH_INLINE bool operator == (Mat44Arg inM2) const;
99 JPH_INLINE bool operator != (Mat44Arg inM2) const { return !(*this == inM2); }
100
102 JPH_INLINE bool IsClose(Mat44Arg inM2, float inMaxDistSq = 1.0e-12f) const;
103
105 JPH_INLINE Mat44 operator * (Mat44Arg inM) const;
106
108 JPH_INLINE Vec3 operator * (Vec3Arg inV) const;
109 JPH_INLINE Vec4 operator * (Vec4Arg inV) const;
110
112 JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const;
113
115 JPH_INLINE Vec3 Multiply3x3Transposed(Vec3Arg inV) const;
116
118 JPH_INLINE Mat44 Multiply3x3(Mat44Arg inM) const;
119
121 JPH_INLINE Mat44 Multiply3x3LeftTransposed(Mat44Arg inM) const;
122
124 JPH_INLINE Mat44 Multiply3x3RightTransposed(Mat44Arg inM) const;
125
127 JPH_INLINE Mat44 operator * (float inV) const;
128 friend JPH_INLINE Mat44 operator * (float inV, Mat44Arg inM) { return inM * inV; }
129
131 JPH_INLINE Mat44 & operator *= (float inV);
132
134 JPH_INLINE Mat44 operator + (Mat44Arg inM) const;
135
137 JPH_INLINE Mat44 operator - () const;
138
140 JPH_INLINE Mat44 operator - (Mat44Arg inM) const;
141
143 JPH_INLINE Mat44 & operator += (Mat44Arg inM);
144
146 JPH_INLINE Vec3 GetAxisX() const { return Vec3(mCol[0]); }
147 JPH_INLINE void SetAxisX(Vec3Arg inV) { mCol[0] = Vec4(inV, 0.0f); }
148 JPH_INLINE Vec3 GetAxisY() const { return Vec3(mCol[1]); }
149 JPH_INLINE void SetAxisY(Vec3Arg inV) { mCol[1] = Vec4(inV, 0.0f); }
150 JPH_INLINE Vec3 GetAxisZ() const { return Vec3(mCol[2]); }
151 JPH_INLINE void SetAxisZ(Vec3Arg inV) { mCol[2] = Vec4(inV, 0.0f); }
152 JPH_INLINE Vec3 GetTranslation() const { return Vec3(mCol[3]); }
153 JPH_INLINE void SetTranslation(Vec3Arg inV) { mCol[3] = Vec4(inV, 1.0f); }
154 JPH_INLINE Vec3 GetDiagonal3() const { return Vec3(mCol[0][0], mCol[1][1], mCol[2][2]); }
155 JPH_INLINE void SetDiagonal3(Vec3Arg inV) { mCol[0][0] = inV.GetX(); mCol[1][1] = inV.GetY(); mCol[2][2] = inV.GetZ(); }
156 JPH_INLINE Vec4 GetDiagonal4() const { return Vec4(mCol[0][0], mCol[1][1], mCol[2][2], mCol[3][3]); }
157 JPH_INLINE void SetDiagonal4(Vec4Arg inV) { mCol[0][0] = inV.GetX(); mCol[1][1] = inV.GetY(); mCol[2][2] = inV.GetZ(); mCol[3][3] = inV.GetW(); }
158 JPH_INLINE Vec3 GetColumn3(uint inCol) const { JPH_ASSERT(inCol < 4); return Vec3(mCol[inCol]); }
159 JPH_INLINE void SetColumn3(uint inCol, Vec3Arg inV) { JPH_ASSERT(inCol < 4); mCol[inCol] = Vec4(inV, inCol == 3? 1.0f : 0.0f); }
160 JPH_INLINE Vec4 GetColumn4(uint inCol) const { JPH_ASSERT(inCol < 4); return mCol[inCol]; }
161 JPH_INLINE void SetColumn4(uint inCol, Vec4Arg inV) { JPH_ASSERT(inCol < 4); mCol[inCol] = inV; }
162
164 JPH_INLINE void StoreFloat4x4(Float4 *outV) const;
165
167 JPH_INLINE Mat44 Transposed() const;
168
170 JPH_INLINE Mat44 Transposed3x3() const;
171
173 JPH_INLINE Mat44 Inversed() const;
174
176 JPH_INLINE Mat44 InversedRotationTranslation() const;
177
179 JPH_INLINE float GetDeterminant3x3() const;
180
182 JPH_INLINE Mat44 Adjointed3x3() const;
183
185 JPH_INLINE Mat44 Inversed3x3() const;
186
188 JPH_INLINE bool SetInversed3x3(Mat44Arg inM);
189
191 JPH_INLINE Mat44 GetRotation() const;
192
194 JPH_INLINE Mat44 GetRotationSafe() const;
195
197 JPH_INLINE void SetRotation(Mat44Arg inRotation);
198
200 JPH_INLINE Quat GetQuaternion() const;
201
203 JPH_INLINE Mat44 GetDirectionPreservingMatrix() const { return GetRotation().Inversed3x3().Transposed3x3(); }
204
206 JPH_INLINE Mat44 PreTranslated(Vec3Arg inTranslation) const;
207
209 JPH_INLINE Mat44 PostTranslated(Vec3Arg inTranslation) const;
210
212 JPH_INLINE Mat44 PreScaled(Vec3Arg inScale) const;
213
215 JPH_INLINE Mat44 PostScaled(Vec3Arg inScale) const;
216
221 JPH_INLINE Mat44 Decompose(Vec3 &outScale) const;
222
223#ifndef JPH_DOUBLE_PRECISION
225 JPH_INLINE Mat44 ToMat44() const { return *this; }
226#endif // !JPH_DOUBLE_PRECISION
227
229 friend ostream & operator << (ostream &inStream, Mat44Arg inM)
230 {
231 inStream << inM.mCol[0] << ", " << inM.mCol[1] << ", " << inM.mCol[2] << ", " << inM.mCol[3];
232 return inStream;
233 }
234
235private:
236 Vec4 mCol[4];
237};
238
239static_assert(is_trivial<Mat44>(), "Is supposed to be a trivial type!");
240
242
243#include "Mat44.inl"
unsigned int uint
Definition: Core.h:452
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition: DVec3.inl:451
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
const Mat44 & Mat44Arg
Definition: MathTypes.h:31
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:31
Class that holds 4 float values. Convert to Vec4 to perform calculations.
Definition: Float4.h:11
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
JPH_INLINE Vec3 GetAxisY() const
Definition: Mat44.h:148
JPH_INLINE void SetAxisX(Vec3Arg inV)
Definition: Mat44.h:147
JPH_INLINE void SetColumn3(uint inCol, Vec3Arg inV)
Definition: Mat44.h:159
JPH_INLINE Vec3 GetAxisZ() const
Definition: Mat44.h:150
JPH_INLINE Mat44 Transposed3x3() const
Transpose 3x3 subpart of matrix.
Definition: Mat44.inl:502
JPH_INLINE Mat44 GetDirectionPreservingMatrix() const
Get matrix that transforms a direction with the same transform as this matrix (length is not preserve...
Definition: Mat44.h:203
JPH_INLINE void SetDiagonal4(Vec4Arg inV)
Definition: Mat44.h:157
JPH_INLINE Mat44 ToMat44() const
In single precision mode just return the matrix itself.
Definition: Mat44.h:225
Vec4::Type Type
Definition: Mat44.h:18
JPH_INLINE void SetAxisZ(Vec3Arg inV)
Definition: Mat44.h:151
JPH_INLINE Vec3 GetColumn3(uint inCol) const
Definition: Mat44.h:158
JPH_INLINE Vec3 GetDiagonal3() const
Definition: Mat44.h:154
JPH_INLINE Vec4 GetColumn4(uint inCol) const
Definition: Mat44.h:160
JPH_INLINE Vec3 GetAxisX() const
Access to the columns.
Definition: Mat44.h:146
JPH_INLINE void SetDiagonal3(Vec3Arg inV)
Definition: Mat44.h:155
Mat44(const Mat44 &inM2)=default
JPH_INLINE Vec3 GetTranslation() const
Definition: Mat44.h:152
JPH_INLINE void SetColumn4(uint inCol, Vec4Arg inV)
Definition: Mat44.h:161
Mat44()=default
Constructor.
JPH_INLINE Mat44 Inversed3x3() const
Inverse 3x3 matrix.
Definition: Mat44.inl:753
JPH_INLINE void SetAxisY(Vec3Arg inV)
Definition: Mat44.h:149
JPH_INLINE void SetTranslation(Vec3Arg inV)
Definition: Mat44.h:153
JPH_INLINE Vec4 GetDiagonal4() const
Definition: Mat44.h:156
Definition: Quat.h:33
Definition: Vec3.h:17
JPH_INLINE float GetX() const
Get individual components.
Definition: Vec3.h:124
JPH_INLINE float GetY() const
Definition: Vec3.h:125
JPH_INLINE float GetZ() const
Definition: Vec3.h:126
Definition: Vec4.h:14
JPH_INLINE float GetW() const
Definition: Vec4.h:116
JPH_INLINE float GetX() const
Get individual components.
Definition: Vec4.h:113
{ float mData[4] Type
Definition: Vec4.h:24
JPH_INLINE float GetZ() const
Definition: Vec4.h:115
JPH_INLINE float GetY() const
Definition: Vec4.h:114