UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MatrixTypes.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "VectorTypes.h"
6#include "VectorUtil.h"
7
8namespace UE
9{
10namespace Geometry
11{
12
13using namespace UE::Math;
14
15template <typename RealType>
17{
21
23 {
24 }
25
26 TMatrix3(RealType ConstantValue)
27 {
28 Row0 = TVector<RealType>(ConstantValue, ConstantValue, ConstantValue);
29 Row1 = Row0;
30 Row2 = Row0;
31 }
32
33 TMatrix3(RealType Diag0, RealType Diag1, RealType Diag2)
34 {
38 }
39
45 : Row0(U.X * V.X, U.X * V.Y, U.X * V.Z),
46 Row1(U.Y * V.X, U.Y * V.Y, U.Y * V.Z),
47 Row2(U.Z * V.X, U.Z * V.Y, U.Z * V.Z)
48 {
49 }
50
51 TMatrix3(RealType M00, RealType M01, RealType M02, RealType M10, RealType M11, RealType M12, RealType M20, RealType M21, RealType M22)
52 : Row0(M00, M01, M02),
53 Row1(M10, M11, M12),
54 Row2(M20, M21, M22)
55 {
56 }
57
59 {
60 if (bRows)
61 {
62 Row0 = V1;
63 Row1 = V2;
64 Row2 = V3;
65 }
66 else
67 {
68 Row0 = TVector<RealType>(V1.X, V2.X, V3.X);
69 Row1 = TVector<RealType>(V1.Y, V2.Y, V3.Y);
70 Row2 = TVector<RealType>(V1.Z, V2.Z, V3.Z);
71 }
72 }
73
74 template<typename RealType2>
75 explicit constexpr TMatrix3(const TMatrix3<RealType2>& Mat) :
76 Row0(TVector<RealType>{Mat.Row0}),
79 {
80 }
81
83 {
84 return TMatrix3<RealType>(0);
85 }
87 {
88 return TMatrix3<RealType>(1, 1, 1);
89 }
90
91 RealType operator()(int Row, int Col) const
92 {
93 check(Row >= 0 && Row < 3 && Col >= 0 && Col < 3);
94 if (Row == 0)
95 {
96 return Row0[Col];
97 }
98 else if (Row == 1)
99 {
100 return Row1[Col];
101 }
102 else
103 {
104 return Row2[Col];
105 }
106 }
107
109 {
110 return TMatrix3<RealType>(
111 Row0.X * Scale, Row0.Y * Scale, Row0.Z * Scale,
112 Row1.X * Scale, Row1.Y * Scale, Row1.Z * Scale,
113 Row2.X * Scale, Row2.Y * Scale, Row2.Z * Scale);
114 }
115
117 {
118 return TVector<RealType>(
119 Row0.X * V.X + Row0.Y * V.Y + Row0.Z * V.Z,
120 Row1.X * V.X + Row1.Y * V.Y + Row1.Z * V.Z,
121 Row2.X * V.X + Row2.Y * V.Y + Row2.Z * V.Z);
122 }
123
125 {
126 RealType M00 = Row0.X * Mat2.Row0.X + Row0.Y * Mat2.Row1.X + Row0.Z * Mat2.Row2.X;
127 RealType M01 = Row0.X * Mat2.Row0.Y + Row0.Y * Mat2.Row1.Y + Row0.Z * Mat2.Row2.Y;
128 RealType M02 = Row0.X * Mat2.Row0.Z + Row0.Y * Mat2.Row1.Z + Row0.Z * Mat2.Row2.Z;
129
130 RealType M10 = Row1.X * Mat2.Row0.X + Row1.Y * Mat2.Row1.X + Row1.Z * Mat2.Row2.X;
131 RealType M11 = Row1.X * Mat2.Row0.Y + Row1.Y * Mat2.Row1.Y + Row1.Z * Mat2.Row2.Y;
132 RealType M12 = Row1.X * Mat2.Row0.Z + Row1.Y * Mat2.Row1.Z + Row1.Z * Mat2.Row2.Z;
133
134 RealType M20 = Row2.X * Mat2.Row0.X + Row2.Y * Mat2.Row1.X + Row2.Z * Mat2.Row2.X;
135 RealType M21 = Row2.X * Mat2.Row0.Y + Row2.Y * Mat2.Row1.Y + Row2.Z * Mat2.Row2.Y;
136 RealType M22 = Row2.X * Mat2.Row0.Z + Row2.Y * Mat2.Row1.Z + Row2.Z * Mat2.Row2.Z;
137
138 return TMatrix3<RealType>(M00, M01, M02, M10, M11, M12, M20, M21, M22);
139 }
140
142 {
143 return TMatrix3<RealType>(Row0 + Mat2.Row0, Row1 + Mat2.Row1, Row2 + Mat2.Row2, true);
144 }
145
147 {
148 return TMatrix3<RealType>(Row0 - Mat2.Row0, Row1 - Mat2.Row1, Row2 - Mat2.Row2, true);
149 }
150
151 inline TMatrix3<RealType>& operator*=(const RealType& Scalar)
152 {
153 Row0 *= Scalar;
154 Row1 *= Scalar;
155 Row2 *= Scalar;
156 return *this;
157 }
158
160 {
161 Row0 += Mat2.Row0;
162 Row1 += Mat2.Row1;
163 Row2 += Mat2.Row2;
164 return *this;
165 }
166
167 RealType InnerProduct(const TMatrix3<RealType>& Mat2) const
168 {
169 return Row0.Dot(Mat2.Row0) + Row1.Dot(Mat2.Row1) + Row2.Dot(Mat2.Row2);
170 }
171
172 RealType Trace() const
173 {
174 return Row0.X + Row1.Y + Row2.Z;
175 }
176
177 RealType Determinant() const
178 {
179 RealType a11 = Row0.X, a12 = Row0.Y, a13 = Row0.Z, a21 = Row1.X, a22 = Row1.Y, a23 = Row1.Z, a31 = Row2.X, a32 = Row2.Y, a33 = Row2.Z;
180 RealType i00 = a33 * a22 - a32 * a23;
181 RealType i01 = -(a33 * a12 - a32 * a13);
182 RealType i02 = a23 * a12 - a22 * a13;
183 return a11 * i00 + a21 * i01 + a31 * i02;
184 }
185
187 {
188 RealType a11 = Row0.X, a12 = Row0.Y, a13 = Row0.Z, a21 = Row1.X, a22 = Row1.Y, a23 = Row1.Z, a31 = Row2.X, a32 = Row2.Y, a33 = Row2.Z;
189 RealType i00 = a33 * a22 - a32 * a23;
190 RealType i01 = -(a33 * a12 - a32 * a13);
191 RealType i02 = a23 * a12 - a22 * a13;
192
193 RealType i10 = -(a33 * a21 - a31 * a23);
194 RealType i11 = a33 * a11 - a31 * a13;
195 RealType i12 = -(a23 * a11 - a21 * a13);
196
197 RealType i20 = a32 * a21 - a31 * a22;
198 RealType i21 = -(a32 * a11 - a31 * a12);
199 RealType i22 = a22 * a11 - a21 * a12;
200
201 RealType det = a11 * i00 + a21 * i01 + a31 * i02;
203 det = 1.0 / det;
204 return TMatrix3<RealType>(i00 * det, i01 * det, i02 * det, i10 * det, i11 * det, i12 * det, i20 * det, i21 * det, i22 * det);
205 }
206
208 {
209 return TMatrix3<RealType>(
210 Row0.X, Row1.X, Row2.X,
211 Row0.Y, Row1.Y, Row2.Y,
212 Row0.Z, Row1.Z, Row2.Z);
213 }
214
216 {
218 Row0.X * V.X + Row1.X * V.Y + Row2.X * V.Z,
219 Row0.Y * V.X + Row1.Y * V.Y + Row2.Y * V.Z,
220 Row0.Z * V.X + Row1.Z * V.Y + Row2.Z * V.Z);
221 }
222
223 // Computes |A|(A^-1)^T.
224 // This value is sometimes useful for computing rotated surface normals from a continuous deformation.
225 // Since computing the 3x3 inverse using Cramer's rule divides by the 3x3 determinant in the final step, this
226 // function avoids computing the determinant at all, since it would just cancel out.
228 {
229 RealType a11 = Row0.X, a12 = Row0.Y, a13 = Row0.Z, a21 = Row1.X, a22 = Row1.Y, a23 = Row1.Z, a31 = Row2.X, a32 = Row2.Y, a33 = Row2.Z;
230
231 RealType i00 = a33 * a22 - a32 * a23;
232 RealType i01 = -(a33 * a12 - a32 * a13);
233 RealType i02 = a23 * a12 - a22 * a13;
234
235 RealType i10 = -(a33 * a21 - a31 * a23);
236 RealType i11 = a33 * a11 - a31 * a13;
237 RealType i12 = -(a23 * a11 - a21 * a13);
238
239 RealType i20 = a32 * a21 - a31 * a22;
240 RealType i21 = -(a32 * a11 - a31 * a12);
241 RealType i22 = a22 * a11 - a21 * a12;
242
243 return TMatrix3<RealType>(i00, i10, i20, i01, i11, i21, i02, i12, i22);
244 }
245
246
247 bool EpsilonEqual(const TMatrix3<RealType>& Mat2, RealType Epsilon) const
248 {
249 return VectorUtil::EpsilonEqual(Row0, Mat2.Row0, Epsilon) &&
250 VectorUtil::EpsilonEqual(Row1, Mat2.Row1, Epsilon) &&
251 VectorUtil::EpsilonEqual(Row2, Mat2.Row2, Epsilon);
252 }
253
255 {
256 RealType cs = TMathUtil<RealType>::Cos(AngleRad);
257 RealType sn = TMathUtil<RealType>::Sin(AngleRad);
258 RealType oneMinusCos = 1.0 - cs;
259 RealType x2 = Axis[0] * Axis[0];
260 RealType y2 = Axis[1] * Axis[1];
261 RealType z2 = Axis[2] * Axis[2];
262 RealType xym = Axis[0] * Axis[1] * oneMinusCos;
263 RealType xzm = Axis[0] * Axis[2] * oneMinusCos;
264 RealType yzm = Axis[1] * Axis[2] * oneMinusCos;
265 RealType xSin = Axis[0] * sn;
266 RealType ySin = Axis[1] * sn;
267 RealType zSin = Axis[2] * sn;
268 return TMatrix3<RealType>(
269 x2 * oneMinusCos + cs, xym - zSin, xzm + ySin,
270 xym + zSin, y2 * oneMinusCos + cs, yzm - xSin,
271 xzm - ySin, yzm + xSin, z2 * oneMinusCos + cs);
272 }
273
278};
279
280
281template <typename RealType>
283{
286
288 {
289 }
290
291 TMatrix2(RealType ConstantValue)
292 {
293 Row0 = Row1 = TVector2<RealType>(ConstantValue, ConstantValue);
294 }
295
296 TMatrix2(RealType Diag0, RealType Diag1)
297 {
300 }
301
307 : Row0(U.X * V.X, U.X * V.Y),
308 Row1(U.Y * V.X, U.Y * V.Y)
309 {
310 }
311
312 TMatrix2(RealType M00, RealType M01, RealType M10, RealType M11)
313 : Row0(M00, M01),
314 Row1(M10, M11)
315 {
316 }
317
319 {
320 if (bRows)
321 {
322 Row0 = V1;
323 Row1 = V2;
324 }
325 else
326 {
327 Row0 = TVector2<RealType>(V1.X, V2.X);
328 Row1 = TVector2<RealType>(V1.Y, V2.Y);
329 }
330 }
331
333 {
334 return TMatrix2<RealType>(0);
335 }
337 {
338 return TMatrix2<RealType>(1, 1);
339 }
340
341 RealType operator()(int Row, int Col) const
342 {
343 check(Row >= 0 && Row < 2 && Col >= 0 && Col < 2);
344 if (Row == 0)
345 {
346 return Row0[Col];
347 }
348 else
349 {
350 return Row1[Col];
351 }
352 }
353
355 {
356 return TMatrix2<RealType>(
357 Row0.X * Scale, Row0.Y * Scale,
358 Row1.X * Scale, Row1.Y * Scale);
359 }
360
362 {
363 return TVector2<RealType>(
364 Row0.X * V.X + Row0.Y * V.Y,
365 Row1.X * V.X + Row1.Y * V.Y);
366 }
367
369 {
370 RealType M00 = Row0.X * Mat2.Row0.X + Row0.Y * Mat2.Row1.X;
371 RealType M01 = Row0.X * Mat2.Row0.Y + Row0.Y * Mat2.Row1.Y;
372
373 RealType M10 = Row1.X * Mat2.Row0.X + Row1.Y * Mat2.Row1.X;
374 RealType M11 = Row1.X * Mat2.Row0.Y + Row1.Y * Mat2.Row1.Y;
375
376 return TMatrix2<RealType>(M00, M01, M10, M11);
377 }
378
380 {
381 return TMatrix2<RealType>(Row0 + Mat2.Row0, Row1 + Mat2.Row1, true);
382 }
383
385 {
386 return TMatrix2<RealType>(Row0 - Mat2.Row0, Row1 - Mat2.Row1, true);
387 }
388
389 inline TMatrix2<RealType>& operator*=(const RealType& Scalar)
390 {
391 Row0 *= Scalar;
392 Row1 *= Scalar;
393 return *this;
394 }
395
397 {
398 Row0 += Mat2.Row0;
399 Row1 += Mat2.Row1;
400 return *this;
401 }
402
403 RealType InnerProduct(const TMatrix2<RealType>& Mat2) const
404 {
405 return Row0.Dot(Mat2.Row0) + Row1.Dot(Mat2.Row1);
406 }
407
408 RealType Trace() const
409 {
410 return Row0.X + Row1.Y;
411 }
412
413 RealType Determinant() const
414 {
415 return Row0.X * Row1.Y - Row0.Y * Row1.X;
416 }
417
419 {
420 RealType Det = Determinant();
422 RealType DetInv = 1.0 / Det;
424 }
425
427 {
428 return TMatrix2<RealType>(
429 Row0.X, Row1.X,
430 Row0.Y, Row1.Y);
431 }
432
433 bool EpsilonEqual(const TMatrix2<RealType>& Mat2, RealType Epsilon) const
434 {
435 return VectorUtil::EpsilonEqual(Row0, Mat2.Row0, Epsilon) &&
436 VectorUtil::EpsilonEqual(Row1, Mat2.Row1, Epsilon);
437 }
438
440 {
441 RealType cs = TMathUtil<RealType>::Cos(AngleRad);
442 RealType sn = TMathUtil<RealType>::Sin(AngleRad);
443 return TMatrix2<RealType>(cs, -sn, sn, cs);
444 }
445
450
451 // Create a matrix that scales along the specified axis
452 // @param Axis Axis to scale along
453 // @param Scale Amount to scale
454 // @param bNormalizeAxis Whether to normalize Axis. If false, assumes Axis was already normalized.
456 {
457 if (bNormalizeAxis)
458 {
459 Axis.Normalize();
460 }
461 RealType X2 = Axis.X * Axis.X;
462 RealType Y2 = Axis.Y * Axis.Y;
463 RealType XY = Axis.X * Axis.Y;
464 return TMatrix2<RealType>(
465 Scale * X2 + Y2, Scale * XY - XY,
466 Scale * XY - XY, Scale * Y2 + X2);
467 }
468
472 RealType GetAngleRad()
473 {
475 }
476
477};
478
479template <typename RealType>
481{
482 return TMatrix3<RealType>(
483 Mat.Row0.X * Scale, Mat.Row0.Y * Scale, Mat.Row0.Z * Scale,
484 Mat.Row1.X * Scale, Mat.Row1.Y * Scale, Mat.Row1.Z * Scale,
485 Mat.Row2.X * Scale, Mat.Row2.Y * Scale, Mat.Row2.Z * Scale);
486}
487
488template <typename RealType>
490{
491 return TMatrix2<RealType>(
492 Mat.Row0.X * Scale, Mat.Row0.Y * Scale,
493 Mat.Row1.X * Scale, Mat.Row1.Y * Scale);
494}
495
496// Skew-Symmetric matrix such that A X B = CrossProductMatrix(A) * B;
497template <typename RealType>
498inline static TMatrix3<RealType> CrossProductMatrix(const UE::Math::TVector<RealType>& A)
499{
500 RealType Zero(0);
501
502 return TMatrix3<RealType>( Zero, -A[2], A[1],
503 A[2], Zero, -A[0],
504 -A[1], A[0], Zero);
505}
506
507
508
513
514} // end namespace UE::Geometry
515} // end namespace UE
#define check(expr)
Definition AssertionMacros.h:314
#define ensure( InExpression)
Definition AssertionMacros.h:464
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define X(Name, Desc)
Definition FormatStringSan.h:47
Definition MathUtil.h:150
static RealType Cos(const RealType Value)
Definition MathUtil.h:372
static RealType Atan2(const RealType ValueY, const RealType ValueX)
Definition MathUtil.h:360
static RealType Sin(const RealType Value)
Definition MathUtil.h:366
bool EpsilonEqual(RealType A, RealType B, RealType Epsilon)
Definition VectorUtil.h:151
TMatrix3< double > FMatrix3d
Definition MatrixTypes.h:510
TMatrix2< float > FMatrix2f
Definition MatrixTypes.h:511
TMatrix2< double > FMatrix2d
Definition MatrixTypes.h:512
TMatrix3< float > FMatrix3f
Definition MatrixTypes.h:509
Definition Sphere.cpp:10
UE_FORCEINLINE_HINT TQuat< T > operator*(const float Scale, const TQuat< T > &Q)
Definition Quat.h:1055
Definition AdvancedWidgetsModule.cpp:13
Definition MatrixTypes.h:283
static TMatrix2< RealType > RotationDeg(RealType AngleDeg)
Definition MatrixTypes.h:446
TMatrix2(RealType M00, RealType M01, RealType M10, RealType M11)
Definition MatrixTypes.h:312
TVector2< RealType > Row0
Definition MatrixTypes.h:284
TMatrix2(RealType ConstantValue)
Definition MatrixTypes.h:291
TMatrix2< RealType > & operator+=(const TMatrix2< RealType > &Mat2)
Definition MatrixTypes.h:396
TVector2< RealType > operator*(const TVector2< RealType > &V) const
Definition MatrixTypes.h:361
TMatrix2< RealType > & operator*=(const RealType &Scalar)
Definition MatrixTypes.h:389
RealType InnerProduct(const TMatrix2< RealType > &Mat2) const
Definition MatrixTypes.h:403
TMatrix2()
Definition MatrixTypes.h:287
TMatrix2(const TVector2< RealType > &V1, const TVector2< RealType > &V2, bool bRows)
Definition MatrixTypes.h:318
RealType Trace() const
Definition MatrixTypes.h:408
TMatrix2(RealType Diag0, RealType Diag1)
Definition MatrixTypes.h:296
bool EpsilonEqual(const TMatrix2< RealType > &Mat2, RealType Epsilon) const
Definition MatrixTypes.h:433
static TMatrix2< RealType > AxisScale(TVector2< RealType > Axis, RealType Scale, bool bNormalizeAxis=true)
Definition MatrixTypes.h:455
TMatrix2< RealType > Inverse() const
Definition MatrixTypes.h:418
TMatrix2< RealType > operator-(const TMatrix2< RealType > &Mat2)
Definition MatrixTypes.h:384
TVector2< RealType > Row1
Definition MatrixTypes.h:285
TMatrix2< RealType > operator*(const TMatrix2< RealType > &Mat2) const
Definition MatrixTypes.h:368
RealType Determinant() const
Definition MatrixTypes.h:413
TMatrix2< RealType > operator+(const TMatrix2< RealType > &Mat2)
Definition MatrixTypes.h:379
static TMatrix2< RealType > Identity()
Definition MatrixTypes.h:336
static TMatrix2< RealType > Zero()
Definition MatrixTypes.h:332
RealType operator()(int Row, int Col) const
Definition MatrixTypes.h:341
static TMatrix2< RealType > RotationRad(RealType AngleRad)
Definition MatrixTypes.h:439
TMatrix2< RealType > operator*(RealType Scale) const
Definition MatrixTypes.h:354
TMatrix2< RealType > Transpose() const
Definition MatrixTypes.h:426
RealType GetAngleRad()
Definition MatrixTypes.h:472
Definition MatrixTypes.h:17
TMatrix3(RealType Diag0, RealType Diag1, RealType Diag2)
Definition MatrixTypes.h:33
TMatrix3()
Definition MatrixTypes.h:22
TMatrix3< RealType > operator*(const TMatrix3< RealType > &Mat2) const
Definition MatrixTypes.h:124
TVector< RealType > Row0
Definition MatrixTypes.h:18
RealType Determinant() const
Definition MatrixTypes.h:177
RealType Trace() const
Definition MatrixTypes.h:172
TMatrix3(const UE::Math::TVector< RealType > &V1, const UE::Math::TVector< RealType > &V2, const UE::Math::TVector< RealType > &V3, bool bRows)
Definition MatrixTypes.h:58
TMatrix3< RealType > operator*(RealType Scale) const
Definition MatrixTypes.h:108
TMatrix3< RealType > & operator*=(const RealType &Scalar)
Definition MatrixTypes.h:151
TVector< RealType > operator*(const UE::Math::TVector< RealType > &V) const
Definition MatrixTypes.h:116
TMatrix3< RealType > DeterminantTimesInverseTranspose() const
Definition MatrixTypes.h:227
TMatrix3< RealType > Inverse() const
Definition MatrixTypes.h:186
TMatrix3(RealType ConstantValue)
Definition MatrixTypes.h:26
static TMatrix3< RealType > AxisAngleR(const UE::Math::TVector< RealType > &Axis, RealType AngleRad)
Definition MatrixTypes.h:254
TMatrix3< RealType > operator-(const TMatrix3< RealType > &Mat2) const
Definition MatrixTypes.h:146
TVector< RealType > Row2
Definition MatrixTypes.h:20
static TMatrix3< RealType > AxisAngleD(const UE::Math::TVector< RealType > &Axis, RealType AngleDeg)
Definition MatrixTypes.h:274
TMatrix3(RealType M00, RealType M01, RealType M02, RealType M10, RealType M11, RealType M12, RealType M20, RealType M21, RealType M22)
Definition MatrixTypes.h:51
TMatrix3< RealType > & operator+=(const TMatrix3< RealType > &Mat2)
Definition MatrixTypes.h:159
static TMatrix3< RealType > Identity()
Definition MatrixTypes.h:86
constexpr TMatrix3(const TMatrix3< RealType2 > &Mat)
Definition MatrixTypes.h:75
UE::Math::TVector< RealType > TransformByTranspose(const UE::Math::TVector< RealType > &V) const
Definition MatrixTypes.h:215
static TMatrix3< RealType > Zero()
Definition MatrixTypes.h:82
RealType InnerProduct(const TMatrix3< RealType > &Mat2) const
Definition MatrixTypes.h:167
TMatrix3< RealType > operator+(const TMatrix3< RealType > &Mat2) const
Definition MatrixTypes.h:141
TMatrix3< RealType > Transpose() const
Definition MatrixTypes.h:207
bool EpsilonEqual(const TMatrix3< RealType > &Mat2, RealType Epsilon) const
Definition MatrixTypes.h:247
RealType operator()(int Row, int Col) const
Definition MatrixTypes.h:91
TVector< RealType > Row1
Definition MatrixTypes.h:19
Definition Vector2D.h:38
T Y
Definition Vector2D.h:52
T Dot(const TVector2< T > &V2) const
Definition Vector2D.h:1123
T X
Definition Vector2D.h:49
Definition Vector.h:51
T Z
Definition Vector.h:68
T Y
Definition Vector.h:65
T X
Definition Vector.h:62
UE_FORCEINLINE_HINT T Dot(const TVector< T > &V) const
Definition Vector.h:1553