UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Rotation.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Chaos/Real.h"
5#include "Chaos/Vector.h"
6#include "Chaos/Matrix.h"
7
8#if !COMPILE_WITHOUT_UNREAL_SUPPORT
9#include "Math/Quat.h"
10#include "Math/RotationMatrix.h"
11#else
12#include <array>
13#include <cmath>
14
15struct _FQuat
16{
17public:
18 const Chaos::FReal operator[](const int32 i) const
19 {
20 return angles[i];
21 }
22 Chaos::FReal& operator[](const int32 i)
23 {
24 return angles[i];
25 }
26 std::array<Chaos::FReal, 3> angles;
27 static MakeFromEuler(const Vector<Chaos::FReal, 3>& InAngles)
28 {
30 Quat.angles = InAngles;
31 return Quat;
32 }
33};
34using FQuat = _FQuat; // Work around include tool not understanding that this won't be compiled alongside MathFwd.h
35#endif
36
37namespace Chaos
38{
39 template<class T, int d>
41 {
42 private:
43 TRotation() {}
44 ~TRotation() {}
45 };
46
47 template<>
48 class TRotation<FRealSingle, 3> : public UE::Math::TQuat<FRealSingle>
49 {
51 public:
53 : BaseQuat() {}
60 template<typename OtherType>
63
65 {
67 BaseQuat::ToMatrix(R);
68 return R;
69 }
70
83 {
84 OutAngle = GetAngle();
85 return GetRotationAxisSafe(OutAxis, DefaultAxis, EpsilionSq);
86 }
87
99 {
100 // Tolerance must be much larger than error in normalized vector (usually ~1e-4) for the
101 // axis calculation to succeed for small angles. For small angles, W ~= 1, and
102 // X, Y, Z ~= 0. If the values of X, Y, Z are around 1e-4 we are just normalizing error.
103 const FRealSingle LenSq = X * X + Y * Y + Z * Z;
104 if (LenSq > EpsilionSq)
105 {
106 FRealSingle InvLen = FMath::InvSqrt(LenSq);
108 return true;
109 }
110
112 return false;
113 }
114
119 {
120 const FRealSingle x2 = X + X; const FRealSingle y2 = Y + Y; const FRealSingle z2 = Z + Z;
121 const FRealSingle xx = X * x2; const FRealSingle xy = X * y2; const FRealSingle xz = X * z2;
122 const FRealSingle yy = Y * y2; const FRealSingle yz = Y * z2; const FRealSingle zz = Z * z2;
123 const FRealSingle wx = W * x2; const FRealSingle wy = W * y2; const FRealSingle wz = W * z2;
124
125 OutX = TVector<FRealSingle, 3>(1.0f - (yy + zz), xy + wz, xz - wy);
126 OutY = TVector<FRealSingle, 3>(xy - wz, 1.0f - (xx + zz), yz + wx);
127 OutZ = TVector<FRealSingle, 3>(xz + wy, yz - wx, 1.0f - (xx + yy));
128 }
129
135 {
136 OutTwist = (X != 0.0f)? BaseQuat(X, 0, 0, W).GetNormalized() : BaseQuat::Identity;
137 OutSwing = *this * OutTwist.Inverse();
138 }
139
144 {
145 return FMath::Max(FMath::Max(FMath::Abs(X), FMath::Abs(Y)), FMath::Max(FMath::Abs(Z), FMath::Abs(W)));
146 }
147
152 {
153 return (L | R);
154 }
155
159 CHAOSCORE_API static TRotation<FRealSingle, 3> Conjugate(const ::Chaos::TRotation<FRealSingle, 3>& InR);
160
164 CHAOSCORE_API static TRotation<FRealSingle, 3> Negate(const ::Chaos::TRotation<FRealSingle, 3>& InR);
165
170 {
171 return BaseQuat(0, 0, 0, 1);
172 }
173
178 {
181 }
182
186 static inline TRotation<FRealSingle, 3> FromElements(const ::Chaos::TVector<FRealSingle, 3>& V, const FRealSingle W)
187 {
188 return FromElements(V.X, V.Y, V.Z, W);
189 }
190
194 static inline TRotation<FRealSingle, 3> FromAxisAngle(const ::Chaos::TVector<FRealSingle, 3>& Axis, const FRealSingle AngleRad)
195 {
197 }
198
202 CHAOSCORE_API static TRotation<FRealSingle, 3> FromVector(const ::Chaos::TVector<FRealSingle, 3>& V);
203
208 const ::Chaos::TVector<FRealSingle, 3>& InitialVector,
209 const ::Chaos::TVector<FRealSingle, 3>& FinalVector);
210
216 CHAOSCORE_API static TVector<FRealSingle, 3> CalculateAngularVelocity1(const TRotation<FRealSingle, 3>& R0, const TRotation<FRealSingle, 3>& InR1, const FRealSingle InDt);
217
224
231 {
232 return CalculateAngularVelocity1(InR0, InR1, InDt);
233 }
234
241 {
242 return CalculateAngularVelocity(InR0, InR1, 1.0f);
243 }
244
250 CHAOSCORE_API static TRotation<FRealSingle, 3> IntegrateRotationWithAngularVelocity(const TRotation<FRealSingle, 3>& InR0, const TVector<FRealSingle, 3>& InW, const FRealSingle InDt);
251
257 static inline bool IsNearlyEqual(const TRotation<FRealSingle, 3>& A, const TRotation<FRealSingle, 3>& B, const FRealSingle Epsilon)
258 {
259 // Only check imaginary part. This is comparing Epsilon to 2*AngleDelta for small angle deltas
260 return FMath::IsNearlyEqual(A.X, B.X, Epsilon)
261 && FMath::IsNearlyEqual(A.Y, B.Y, Epsilon)
262 && FMath::IsNearlyEqual(A.Z, B.Z, Epsilon);
263 }
264 };
265
266 template<>
267 class TRotation<FRealDouble, 3> : public UE::Math::TQuat<FRealDouble>
268 {
270 public:
272 : BaseQuat() {}
279 template<typename OtherType>
282
284 {
286 BaseQuat::ToMatrix(R);
287 return R;
288 }
289
302 {
303 OutAngle = GetAngle();
304 return GetRotationAxisSafe(OutAxis, DefaultAxis, EpsilionSq);
305 }
306
318 {
319 // Tolerance must be much larger than error in normalized vector (usually ~1e-4) for the
320 // axis calculation to succeed for small angles. For small angles, W ~= 1, and
321 // X, Y, Z ~= 0. If the values of X, Y, Z are around 1e-4 we are just normalizing error.
322 const FRealDouble LenSq = X * X + Y * Y + Z * Z;
323 if (LenSq > EpsilionSq)
324 {
325 FRealDouble InvLen = FMath::InvSqrt(LenSq);
327 return true;
328 }
329
331 return false;
332 }
333
338 {
339 const FRealDouble x2 = X + X; const FRealDouble y2 = Y + Y; const FRealDouble z2 = Z + Z;
340 const FRealDouble xx = X * x2; const FRealDouble xy = X * y2; const FRealDouble xz = X * z2;
341 const FRealDouble yy = Y * y2; const FRealDouble yz = Y * z2; const FRealDouble zz = Z * z2;
342 const FRealDouble wx = W * x2; const FRealDouble wy = W * y2; const FRealDouble wz = W * z2;
343
344 OutX = TVector<FRealDouble, 3>(1.0f - (yy + zz), xy + wz, xz - wy);
345 OutY = TVector<FRealDouble, 3>(xy - wz, 1.0f - (xx + zz), yz + wx);
346 OutZ = TVector<FRealDouble, 3>(xz + wy, yz - wx, 1.0f - (xx + yy));
347 }
348
354 {
355 OutTwist = (X != 0.0f)? BaseQuat(X, 0, 0, W).GetNormalized() : BaseQuat::Identity;
356 OutSwing = *this * OutTwist.Inverse();
357 }
358
363 {
364 return FMath::Max(FMath::Max(FMath::Abs(X), FMath::Abs(Y)), FMath::Max(FMath::Abs(Z), FMath::Abs(W)));
365 }
366
371 {
372 return (L | R);
373 }
374
378 CHAOSCORE_API static TRotation<FRealDouble, 3> Conjugate(const ::Chaos::TRotation<FRealDouble, 3>& InR);
379
383 CHAOSCORE_API static TRotation<FRealDouble, 3> Negate(const ::Chaos::TRotation<FRealDouble, 3>& InR);
384
389 {
390 return BaseQuat(0, 0, 0, 1);
391 }
392
397 {
400 }
401
405 static inline TRotation<FRealDouble, 3> FromElements(const ::Chaos::TVector<FRealDouble, 3>& V, const FRealDouble W)
406 {
407 return FromElements(V.X, V.Y, V.Z, W);
408 }
409
413 static inline TRotation<FRealDouble, 3> FromAxisAngle(const ::Chaos::TVector<FRealDouble, 3>& Axis, const FRealDouble AngleRad)
414 {
416 }
417
421 CHAOSCORE_API static TRotation<FRealDouble, 3> FromVector(const ::Chaos::TVector<FRealDouble, 3>& V);
422
427 const ::Chaos::TVector<FRealDouble, 3>& InitialVector,
428 const ::Chaos::TVector<FRealDouble, 3>& FinalVector);
429
436
443
450 {
451 return CalculateAngularVelocity1(InR0, InR1, InDt);
452 }
453
460 {
461 return CalculateAngularVelocity(InR0, InR1, 1.0f);
462 }
463
470
476 static inline bool IsNearlyEqual(const TRotation<FRealDouble, 3>& A, const TRotation<FRealDouble, 3>& B, const FRealDouble Epsilon)
477 {
478 // Only check imaginary part. This is comparing Epsilon to 2*AngleDelta for small angle deltas
479 return FMath::IsNearlyEqual(A.X, B.X, Epsilon)
480 && FMath::IsNearlyEqual(A.Y, B.Y, Epsilon)
481 && FMath::IsNearlyEqual(A.Z, B.Z, Epsilon);
482 }
483 };
484}
#define FORCEINLINE
Definition AndroidPlatform.h:140
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition Matrix.h:21
void ToMatrixAxes(TVector< FRealDouble, 3 > &OutX, TVector< FRealDouble, 3 > &OutY, TVector< FRealDouble, 3 > &OutZ)
Definition Rotation.h:337
static CHAOSCORE_API TRotation< FRealDouble, 3 > Conjugate(const ::Chaos::TRotation< FRealDouble, 3 > &InR)
static TVector< FRealDouble, 3 > CalculateAngularVelocity(const TRotation< FRealDouble, 3 > &InR0, const TRotation< FRealDouble, 3 > &InR1, const FRealDouble InDt)
Definition Rotation.h:449
static CHAOSCORE_API TRotation< FRealDouble, 3 > IntegrateRotationWithAngularVelocity(const TRotation< FRealDouble, 3 > &InR0, const TVector< FRealDouble, 3 > &InW, const FRealDouble InDt)
static CHAOSCORE_API TVector< FRealDouble, 3 > CalculateAngularVelocity1(const TRotation< FRealDouble, 3 > &R0, const TRotation< FRealDouble, 3 > &InR1, const FRealDouble InDt)
TRotation(const FMatrix44d &Matrix)
Definition Rotation.h:275
TRotation(const BaseQuat &Quat)
Definition Rotation.h:273
static TRotation< FRealDouble, 3 > FromElements(const FRealDouble X, const FRealDouble Y, const FRealDouble Z, const FRealDouble W)
Definition Rotation.h:396
static TRotation< FRealDouble, 3 > FromElements(const ::Chaos::TVector< FRealDouble, 3 > &V, const FRealDouble W)
Definition Rotation.h:405
void ToSwingTwistX(BaseQuat &OutSwing, BaseQuat &OutTwist) const
Definition Rotation.h:353
static TVector< FRealDouble, 3 > CalculateAngularDelta(const TRotation< FRealDouble, 3 > &InR0, const TRotation< FRealDouble, 3 > &InR1)
Definition Rotation.h:459
TRotation(const UE::Math::TQuat< OtherType > &Other)
Definition Rotation.h:280
bool ToAxisAndAngleSafe(TVector< FRealDouble, 3 > &OutAxis, FRealDouble &OutAngle, const TVector< FRealDouble, 3 > &DefaultAxis, FRealDouble EpsilionSq=1e-6f) const
Definition Rotation.h:301
FORCEINLINE FRealDouble GetAbsMax() const
Return the large absolute element value.
Definition Rotation.h:362
static TRotation< FRealDouble, 3 > FromAxisAngle(const ::Chaos::TVector< FRealDouble, 3 > &Axis, const FRealDouble AngleRad)
Definition Rotation.h:413
static CHAOSCORE_API TRotation< FRealDouble, 3 > Negate(const ::Chaos::TRotation< FRealDouble, 3 > &InR)
TRotation()
Definition Rotation.h:271
static CHAOSCORE_API TRotation< FRealDouble, 3 > FromRotatedVector(const ::Chaos::TVector< FRealDouble, 3 > &InitialVector, const ::Chaos::TVector< FRealDouble, 3 > &FinalVector)
bool GetRotationAxisSafe(TVector< FRealDouble, 3 > &OutAxis, const TVector< FRealDouble, 3 > &DefaultAxis, FRealDouble EpsilionSq=1e-6f) const
Definition Rotation.h:317
PMatrix< FRealDouble, 3, 3 > ToMatrix() const
Definition Rotation.h:283
static CHAOSCORE_API TRotation< FRealDouble, 3 > FromVector(const ::Chaos::TVector< FRealDouble, 3 > &V)
static TRotation< FRealDouble, 3 > FromIdentity()
Definition Rotation.h:388
TRotation(const FMatrix44f &Matrix)
Definition Rotation.h:277
static CHAOSCORE_API TVector< FRealDouble, 3 > CalculateAngularVelocity2(const TRotation< FRealDouble, 3 > &R0, const TRotation< FRealDouble, 3 > &InR1, const FRealDouble InDt)
static bool IsNearlyEqual(const TRotation< FRealDouble, 3 > &A, const TRotation< FRealDouble, 3 > &B, const FRealDouble Epsilon)
Definition Rotation.h:476
static FRealDouble DotProduct(const TRotation< FRealDouble, 3 > &L, const TRotation< FRealDouble, 3 > &R)
Return the dot product of two quaternions.
Definition Rotation.h:370
static TRotation< FRealSingle, 3 > FromIdentity()
Definition Rotation.h:169
static TRotation< FRealSingle, 3 > FromAxisAngle(const ::Chaos::TVector< FRealSingle, 3 > &Axis, const FRealSingle AngleRad)
Definition Rotation.h:194
TRotation(const UE::Math::TQuat< OtherType > &Other)
Definition Rotation.h:61
static TVector< FRealSingle, 3 > CalculateAngularVelocity(const TRotation< FRealSingle, 3 > &InR0, const TRotation< FRealSingle, 3 > &InR1, const FRealSingle InDt)
Definition Rotation.h:230
TRotation(const FMatrix44d &Matrix)
Definition Rotation.h:58
FORCEINLINE FRealSingle GetAbsMax() const
Return the large absolute element value.
Definition Rotation.h:143
bool GetRotationAxisSafe(TVector< FRealSingle, 3 > &OutAxis, const TVector< FRealSingle, 3 > &DefaultAxis, FRealSingle EpsilionSq=1e-6f) const
Definition Rotation.h:98
static CHAOSCORE_API TRotation< FRealSingle, 3 > FromRotatedVector(const ::Chaos::TVector< FRealSingle, 3 > &InitialVector, const ::Chaos::TVector< FRealSingle, 3 > &FinalVector)
TRotation(const FMatrix44f &Matrix)
Definition Rotation.h:56
static CHAOSCORE_API TRotation< FRealSingle, 3 > FromVector(const ::Chaos::TVector< FRealSingle, 3 > &V)
static TRotation< FRealSingle, 3 > FromElements(const FRealSingle X, const FRealSingle Y, const FRealSingle Z, const FRealSingle W)
Definition Rotation.h:177
static FRealSingle DotProduct(const TRotation< FRealSingle, 3 > &L, const TRotation< FRealSingle, 3 > &R)
Return the dot product of two quaternions.
Definition Rotation.h:151
void ToSwingTwistX(BaseQuat &OutSwing, BaseQuat &OutTwist) const
Definition Rotation.h:134
PMatrix< FRealSingle, 3, 3 > ToMatrix() const
Definition Rotation.h:64
static bool IsNearlyEqual(const TRotation< FRealSingle, 3 > &A, const TRotation< FRealSingle, 3 > &B, const FRealSingle Epsilon)
Definition Rotation.h:257
static TRotation< FRealSingle, 3 > FromElements(const ::Chaos::TVector< FRealSingle, 3 > &V, const FRealSingle W)
Definition Rotation.h:186
TRotation(const BaseQuat &Quat)
Definition Rotation.h:54
bool ToAxisAndAngleSafe(TVector< FRealSingle, 3 > &OutAxis, FRealSingle &OutAngle, const TVector< FRealSingle, 3 > &DefaultAxis, FRealSingle EpsilionSq=1e-6f) const
Definition Rotation.h:82
void ToMatrixAxes(TVector< FRealSingle, 3 > &OutX, TVector< FRealSingle, 3 > &OutY, TVector< FRealSingle, 3 > &OutZ)
Definition Rotation.h:118
static CHAOSCORE_API TVector< FRealSingle, 3 > CalculateAngularVelocity2(const TRotation< FRealSingle, 3 > &R0, const TRotation< FRealSingle, 3 > &InR1, const FRealSingle InDt)
static CHAOSCORE_API TRotation< FRealSingle, 3 > Negate(const ::Chaos::TRotation< FRealSingle, 3 > &InR)
static TVector< FRealSingle, 3 > CalculateAngularDelta(const TRotation< FRealSingle, 3 > &InR0, const TRotation< FRealSingle, 3 > &InR1)
Definition Rotation.h:240
TRotation()
Definition Rotation.h:52
Definition Rotation.h:41
Definition Vector.h:41
Definition SkeletalMeshComponent.h:307
@ Y
Definition SimulationModuleBase.h:153
@ X
Definition SimulationModuleBase.h:152
FRealDouble FReal
Definition Real.h:22
float FRealSingle
Definition Real.h:14
double FRealDouble
Definition Real.h:13
static UE_FORCEINLINE_HINT bool IsNearlyEqual(float A, float B, float ErrorTolerance=UE_SMALL_NUMBER)
Definition UnrealMathUtility.h:388
TQuat< T > GetNormalized(T Tolerance=UE_SMALL_NUMBER) const
Definition Quat.h:1140
TQuat< T > Inverse() const
Definition Quat.h:1264
FRealSingle FReal
Definition Quat.h:45
Definition Vector.h:51