Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
RotationMatrix.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Math/Vector.h"
7#include "Math/Rotator.h"
8#include "Math/Matrix.h"
9#include "Math/RotationTranslationMatrix.h"
10#include "Math/QuatRotationTranslationMatrix.h"
11
12namespace UE {
13namespace Math {
14
15/** Rotation matrix no translation */
16template<typename T>
19{
20public:
22
23 /**
24 * Constructor.
25 *
26 * @param Rot rotation
27 */
28 TRotationMatrix(const TRotator<T>& Rot)
30 { }
31
32 /** Matrix factory. Return an TMatrix<T> so we don't have type conversion issues in expressions. */
33 static TMatrix<T> Make(TRotator<T> const& Rot)
34 {
35 return TRotationMatrix(Rot);
36 }
37
38 /** Matrix factory. Return an TMatrix<T> so we don't have type conversion issues in expressions. */
39 static TMatrix<T> Make(TQuat<T> const& Rot);
40
41 /** Builds a rotation matrix given only a XAxis. Y and Z are unspecified but will be orthonormal. XAxis need not be normalized. */
42 static TMatrix<T> MakeFromX(TVector<T> const& XAxis);
43
44 /** Builds a rotation matrix given only a YAxis. X and Z are unspecified but will be orthonormal. YAxis need not be normalized. */
45 static TMatrix<T> MakeFromY(TVector<T> const& YAxis);
46
47 /** Builds a rotation matrix given only a ZAxis. X and Y are unspecified but will be orthonormal. ZAxis need not be normalized. */
48 static TMatrix<T> MakeFromZ(TVector<T> const& ZAxis);
49
50 /** Builds a matrix with given X and Y axes. X will remain fixed, Y may be changed minimally to enforce orthogonality. Z will be computed. Inputs need not be normalized. */
51 static TMatrix<T> MakeFromXY(TVector<T> const& XAxis, TVector<T> const& YAxis);
52
53 /** Builds a matrix with given X and Z axes. X will remain fixed, Z may be changed minimally to enforce orthogonality. Y will be computed. Inputs need not be normalized. */
54 static TMatrix<T> MakeFromXZ(TVector<T> const& XAxis, TVector<T> const& ZAxis);
55
56 /** Builds a matrix with given Y and X axes. Y will remain fixed, X may be changed minimally to enforce orthogonality. Z will be computed. Inputs need not be normalized. */
57 static TMatrix<T> MakeFromYX(TVector<T> const& YAxis, TVector<T> const& XAxis);
58
59 /** Builds a matrix with given Y and Z axes. Y will remain fixed, Z may be changed minimally to enforce orthogonality. X will be computed. Inputs need not be normalized. */
60 static TMatrix<T> MakeFromYZ(TVector<T> const& YAxis, TVector<T> const& ZAxis);
61
62 /** Builds a matrix with given Z and X axes. Z will remain fixed, X may be changed minimally to enforce orthogonality. Y will be computed. Inputs need not be normalized. */
63 static TMatrix<T> MakeFromZX(TVector<T> const& ZAxis, TVector<T> const& XAxis);
64
65 /** Builds a matrix with given Z and Y axes. Z will remain fixed, Y may be changed minimally to enforce orthogonality. X will be computed. Inputs need not be normalized. */
66 static TMatrix<T> MakeFromZY(TVector<T> const& ZAxis, TVector<T> const& YAxis);
67};
68
69template<typename T>
70TMatrix<T> TRotationMatrix<T>::Make(TQuat<T> const& Rot)
71{
73}
74
75template<typename T>
76TMatrix<T> TRotationMatrix<T>::MakeFromX(TVector<T> const& XAxis)
77{
78 TVector<T> const NewX = XAxis.GetSafeNormal();
79
80 // try to use up if possible
81 TVector<T> const UpVector = (FMath::Abs(NewX.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
82
83 const TVector<T> NewY = (UpVector ^ NewX).GetSafeNormal();
84 const TVector<T> NewZ = NewX ^ NewY;
85
86 return TMatrix<T>(NewX, NewY, NewZ, TVector<T>::ZeroVector);
87}
88
89template<typename T>
90TMatrix<T> TRotationMatrix<T>::MakeFromY(TVector<T> const& YAxis)
91{
92 TVector<T> const NewY = YAxis.GetSafeNormal();
93
94 // try to use up if possible
95 TVector<T> const UpVector = (FMath::Abs(NewY.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
96
97 const TVector<T> NewZ = (UpVector ^ NewY).GetSafeNormal();
98 const TVector<T> NewX = NewY ^ NewZ;
99
100 return TMatrix<T>(NewX, NewY, NewZ, TVector<T>::ZeroVector);
101}
102
103template<typename T>
104TMatrix<T> TRotationMatrix<T>::MakeFromZ(TVector<T> const& ZAxis)
105{
106 TVector<T> const NewZ = ZAxis.GetSafeNormal();
107
108 // try to use up if possible
109 TVector<T> const UpVector = (FMath::Abs(NewZ.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
110
111 const TVector<T> NewX = (UpVector ^ NewZ).GetSafeNormal();
112 const TVector<T> NewY = NewZ ^ NewX;
113
114 return TMatrix<T>(NewX, NewY, NewZ, TVector<T>::ZeroVector);
115}
116
117template<typename T>
118TMatrix<T> TRotationMatrix<T>::MakeFromXY(TVector<T> const& XAxis, TVector<T> const& YAxis)
119{
122
123 // if they're almost same, we need to find arbitrary vector
124 if (FMath::IsNearlyEqual(FMath::Abs(NewX | Norm), T(1.f)))
125 {
126 // make sure we don't ever pick the same as NewX
127 Norm = (FMath::Abs(NewX.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
128 }
129
130 const TVector<T> NewZ = (NewX ^ Norm).GetSafeNormal();
131 const TVector<T> NewY = NewZ ^ NewX;
132
133 return TMatrix<T>(NewX, NewY, NewZ, TVector<T>::ZeroVector);
134}
135
136template<typename T>
137TMatrix<T> TRotationMatrix<T>::MakeFromXZ(TVector<T> const& XAxis, TVector<T> const& ZAxis)
138{
139 TVector<T> const NewX = XAxis.GetSafeNormal();
141
142 // if they're almost same, we need to find arbitrary vector
143 if (FMath::IsNearlyEqual(FMath::Abs(NewX | Norm), T(1.f)))
144 {
145 // make sure we don't ever pick the same as NewX
146 Norm = (FMath::Abs(NewX.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
147 }
148
149 const TVector<T> NewY = (Norm ^ NewX).GetSafeNormal();
150 const TVector<T> NewZ = NewX ^ NewY;
151
152 return TMatrix<T>(NewX, NewY, NewZ, TVector<T>::ZeroVector);
153}
154
155template<typename T>
156TMatrix<T> TRotationMatrix<T>::MakeFromYX(TVector<T> const& YAxis, TVector<T> const& XAxis)
157{
158 TVector<T> const NewY = YAxis.GetSafeNormal();
160
161 // if they're almost same, we need to find arbitrary vector
162 if (FMath::IsNearlyEqual(FMath::Abs(NewY | Norm), T(1.f)))
163 {
164 // make sure we don't ever pick the same as NewX
165 Norm = (FMath::Abs(NewY.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
166 }
167
168 const TVector<T> NewZ = (Norm ^ NewY).GetSafeNormal();
169 const TVector<T> NewX = NewY ^ NewZ;
170
171 return TMatrix<T>(NewX, NewY, NewZ, TVector<T>::ZeroVector);
172}
173
174template<typename T>
175TMatrix<T> TRotationMatrix<T>::MakeFromYZ(TVector<T> const& YAxis, TVector<T> const& ZAxis)
176{
177 TVector<T> const NewY = YAxis.GetSafeNormal();
179
180 // if they're almost same, we need to find arbitrary vector
181 if (FMath::IsNearlyEqual(FMath::Abs(NewY | Norm), T(1.f)))
182 {
183 // make sure we don't ever pick the same as NewX
184 Norm = (FMath::Abs(NewY.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
185 }
186
187 const TVector<T> NewX = (NewY ^ Norm).GetSafeNormal();
188 const TVector<T> NewZ = NewX ^ NewY;
189
190 return TMatrix<T>(NewX, NewY, NewZ, TVector<T>::ZeroVector);
191}
192
193template<typename T>
194TMatrix<T> TRotationMatrix<T>::MakeFromZX(TVector<T> const& ZAxis, TVector<T> const& XAxis)
195{
196 TVector<T> const NewZ = ZAxis.GetSafeNormal();
198
199 // if they're almost same, we need to find arbitrary vector
200 if (FMath::IsNearlyEqual(FMath::Abs(NewZ | Norm), T(1.f)))
201 {
202 // make sure we don't ever pick the same as NewX
203 Norm = (FMath::Abs(NewZ.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
204 }
205
206 const TVector<T> NewY = (NewZ ^ Norm).GetSafeNormal();
207 const TVector<T> NewX = NewY ^ NewZ;
208
209 return TMatrix<T>(NewX, NewY, NewZ, TVector<T>::ZeroVector);
210}
211
212template<typename T>
213TMatrix<T> TRotationMatrix<T>::MakeFromZY(TVector<T> const& ZAxis, TVector<T> const& YAxis)
214{
215 TVector<T> const NewZ = ZAxis.GetSafeNormal();
217
218 // if they're almost same, we need to find arbitrary vector
219 if (FMath::IsNearlyEqual(FMath::Abs(NewZ | Norm), T(1.f)))
220 {
221 // make sure we don't ever pick the same as NewX
222 Norm = (FMath::Abs(NewZ.Z) < (1.f - UE_KINDA_SMALL_NUMBER)) ? TVector<T>(0, 0, 1.f) : TVector<T>(1.f, 0, 0);
223 }
224
225 const TVector<T> NewX = (Norm ^ NewZ).GetSafeNormal();
226 const TVector<T> NewY = NewZ ^ NewX;
227
228 return TMatrix<T>(NewX, NewY, NewZ, TVector<T>::ZeroVector);
229}
230
231} // namespace Math
232} // namespace UE
233
234UE_DECLARE_LWC_TYPE(RotationMatrix, 44);
235
236template<> struct TIsUECoreVariant<FRotationMatrix44f> { enum { Value = true }; };
237template<> struct TIsUECoreVariant<FRotationMatrix44d> { enum { Value = true }; };
#define UE_DECLARE_LWC_TYPE(...)
#define UE_KINDA_SMALL_NUMBER
Definition Vector.h:40
static TMatrix< T > MakeFromZ(TVector< T > const &ZAxis)
static TMatrix< T > MakeFromYX(TVector< T > const &YAxis, TVector< T > const &XAxis)
static TMatrix< T > Make(TQuat< T > const &Rot)
TRotationMatrix(const TRotator< T > &Rot)
static TMatrix< T > MakeFromYZ(TVector< T > const &YAxis, TVector< T > const &ZAxis)
static TMatrix< T > MakeFromZY(TVector< T > const &ZAxis, TVector< T > const &YAxis)
static TMatrix< T > MakeFromX(TVector< T > const &XAxis)
static TMatrix< T > MakeFromXY(TVector< T > const &XAxis, TVector< T > const &YAxis)
static TMatrix< T > MakeFromY(TVector< T > const &YAxis)
static TMatrix< T > MakeFromXZ(TVector< T > const &XAxis, TVector< T > const &ZAxis)
static TMatrix< T > Make(TRotator< T > const &Rot)
static TMatrix< T > MakeFromZX(TVector< T > const &ZAxis, TVector< T > const &XAxis)