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 JPH_INLINE float operator () (uint inRow, uint inColumn) const { JPH_ASSERT(inRow < 4); JPH_ASSERT(inColumn < 4); return mCol[inColumn].mF32[inRow]; }
92 JPH_INLINE float & operator () (uint inRow, uint inColumn) { JPH_ASSERT(inRow < 4); JPH_ASSERT(inColumn < 4); return mCol[inColumn].mF32[inRow]; }
93
95 JPH_INLINE bool operator == (Mat44Arg inM2) const;
96 JPH_INLINE bool operator != (Mat44Arg inM2) const { return !(*this == inM2); }
97
99 JPH_INLINE bool IsClose(Mat44Arg inM2, float inMaxDistSq = 1.0e-12f) const;
100
102 JPH_INLINE Mat44 operator * (Mat44Arg inM) const;
103
105 JPH_INLINE Vec3 operator * (Vec3Arg inV) const;
106 JPH_INLINE Vec4 operator * (Vec4Arg inV) const;
107
109 JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const;
110
112 JPH_INLINE Vec3 Multiply3x3Transposed(Vec3Arg inV) const;
113
115 JPH_INLINE Mat44 Multiply3x3(Mat44Arg inM) const;
116
118 JPH_INLINE Mat44 Multiply3x3LeftTransposed(Mat44Arg inM) const;
119
121 JPH_INLINE Mat44 Multiply3x3RightTransposed(Mat44Arg inM) const;
122
124 JPH_INLINE Mat44 operator * (float inV) const;
125 friend JPH_INLINE Mat44 operator * (float inV, Mat44Arg inM) { return inM * inV; }
126
128 JPH_INLINE Mat44 & operator *= (float inV);
129
131 JPH_INLINE Mat44 operator + (Mat44Arg inM) const;
132
134 JPH_INLINE Mat44 operator - () const;
135
137 JPH_INLINE Mat44 operator - (Mat44Arg inM) const;
138
140 JPH_INLINE Mat44 & operator += (Mat44Arg inM);
141
143 JPH_INLINE Vec3 GetAxisX() const { return Vec3(mCol[0]); }
144 JPH_INLINE void SetAxisX(Vec3Arg inV) { mCol[0] = Vec4(inV, 0.0f); }
145 JPH_INLINE Vec3 GetAxisY() const { return Vec3(mCol[1]); }
146 JPH_INLINE void SetAxisY(Vec3Arg inV) { mCol[1] = Vec4(inV, 0.0f); }
147 JPH_INLINE Vec3 GetAxisZ() const { return Vec3(mCol[2]); }
148 JPH_INLINE void SetAxisZ(Vec3Arg inV) { mCol[2] = Vec4(inV, 0.0f); }
149 JPH_INLINE Vec3 GetTranslation() const { return Vec3(mCol[3]); }
150 JPH_INLINE void SetTranslation(Vec3Arg inV) { mCol[3] = Vec4(inV, 1.0f); }
151 JPH_INLINE Vec3 GetDiagonal3() const { return Vec3(mCol[0][0], mCol[1][1], mCol[2][2]); }
152 JPH_INLINE void SetDiagonal3(Vec3Arg inV) { mCol[0][0] = inV.GetX(); mCol[1][1] = inV.GetY(); mCol[2][2] = inV.GetZ(); }
153 JPH_INLINE Vec4 GetDiagonal4() const { return Vec4(mCol[0][0], mCol[1][1], mCol[2][2], mCol[3][3]); }
154 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(); }
155 JPH_INLINE Vec3 GetColumn3(uint inCol) const { JPH_ASSERT(inCol < 4); return Vec3(mCol[inCol]); }
156 JPH_INLINE void SetColumn3(uint inCol, Vec3Arg inV) { JPH_ASSERT(inCol < 4); mCol[inCol] = Vec4(inV, inCol == 3? 1.0f : 0.0f); }
157 JPH_INLINE Vec4 GetColumn4(uint inCol) const { JPH_ASSERT(inCol < 4); return mCol[inCol]; }
158 JPH_INLINE void SetColumn4(uint inCol, Vec4Arg inV) { JPH_ASSERT(inCol < 4); mCol[inCol] = inV; }
159
161 JPH_INLINE void StoreFloat4x4(Float4 *outV) const;
162
164 JPH_INLINE Mat44 Transposed() const;
165
167 JPH_INLINE Mat44 Transposed3x3() const;
168
170 JPH_INLINE Mat44 Inversed() const;
171
173 JPH_INLINE Mat44 InversedRotationTranslation() const;
174
176 JPH_INLINE float GetDeterminant3x3() const;
177
179 JPH_INLINE Mat44 Adjointed3x3() const;
180
182 JPH_INLINE Mat44 Inversed3x3() const;
183
185 JPH_INLINE bool SetInversed3x3(Mat44Arg inM);
186
188 JPH_INLINE Mat44 GetRotation() const;
189
191 JPH_INLINE Mat44 GetRotationSafe() const;
192
194 JPH_INLINE void SetRotation(Mat44Arg inRotation);
195
197 JPH_INLINE Quat GetQuaternion() const;
198
200 JPH_INLINE Mat44 GetDirectionPreservingMatrix() const { return GetRotation().Inversed3x3().Transposed3x3(); }
201
203 JPH_INLINE Mat44 PreTranslated(Vec3Arg inTranslation) const;
204
206 JPH_INLINE Mat44 PostTranslated(Vec3Arg inTranslation) const;
207
209 JPH_INLINE Mat44 PreScaled(Vec3Arg inScale) const;
210
212 JPH_INLINE Mat44 PostScaled(Vec3Arg inScale) const;
213
218 JPH_INLINE Mat44 Decompose(Vec3 &outScale) const;
219
220#ifndef JPH_DOUBLE_PRECISION
222 JPH_INLINE Mat44 ToMat44() const { return *this; }
223#endif // !JPH_DOUBLE_PRECISION
224
226 friend ostream & operator << (ostream &inStream, Mat44Arg inM)
227 {
228 inStream << inM.mCol[0] << ", " << inM.mCol[1] << ", " << inM.mCol[2] << ", " << inM.mCol[3];
229 return inStream;
230 }
231
232private:
233 Vec4 mCol[4];
234};
235
236static_assert(is_trivial<Mat44>(), "Is supposed to be a trivial type!");
237
239
240#include "Mat44.inl"
unsigned int uint
Definition: Core.h:426
#define JPH_NAMESPACE_END
Definition: Core.h:354
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:348
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition: DVec3.inl:447
#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:29
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:145
JPH_INLINE void SetAxisX(Vec3Arg inV)
Definition: Mat44.h:144
JPH_INLINE void SetColumn3(uint inCol, Vec3Arg inV)
Definition: Mat44.h:156
JPH_INLINE Vec3 GetAxisZ() const
Definition: Mat44.h:147
JPH_INLINE Mat44 Transposed3x3() const
Transpose 3x3 subpart of matrix.
Definition: Mat44.inl:493
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:200
JPH_INLINE void SetDiagonal4(Vec4Arg inV)
Definition: Mat44.h:154
JPH_INLINE Mat44 ToMat44() const
In single precision mode just return the matrix itself.
Definition: Mat44.h:222
Vec4::Type Type
Definition: Mat44.h:18
JPH_INLINE void SetAxisZ(Vec3Arg inV)
Definition: Mat44.h:148
JPH_INLINE Vec3 GetColumn3(uint inCol) const
Definition: Mat44.h:155
JPH_INLINE Vec3 GetDiagonal3() const
Definition: Mat44.h:151
JPH_INLINE Vec4 GetColumn4(uint inCol) const
Definition: Mat44.h:157
JPH_INLINE Vec3 GetAxisX() const
Access to the columns.
Definition: Mat44.h:143
JPH_INLINE void SetDiagonal3(Vec3Arg inV)
Definition: Mat44.h:152
Mat44(const Mat44 &inM2)=default
JPH_INLINE Vec3 GetTranslation() const
Definition: Mat44.h:149
JPH_INLINE void SetColumn4(uint inCol, Vec4Arg inV)
Definition: Mat44.h:158
Mat44()=default
Constructor.
JPH_INLINE Mat44 Inversed3x3() const
Inverse 3x3 matrix.
Definition: Mat44.inl:744
JPH_INLINE void SetAxisY(Vec3Arg inV)
Definition: Mat44.h:146
JPH_INLINE void SetTranslation(Vec3Arg inV)
Definition: Mat44.h:150
JPH_INLINE Vec4 GetDiagonal4() const
Definition: Mat44.h:153
Definition: Quat.h:33
Definition: Vec3.h:16
JPH_INLINE float GetX() const
Get individual components.
Definition: Vec3.h:123
JPH_INLINE float GetY() const
Definition: Vec3.h:124
JPH_INLINE float GetZ() const
Definition: Vec3.h:125
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