Plasma Engine  2.0
Loading...
Searching...
No Matches
Transform_inl.h
1#pragma once
2
3#include <Foundation/Math/Transform.h>
4
5template <typename Type>
7 const plQuatTemplate<Type>& qRotation, const plVec3Template<Type>& vScale)
8 : m_vPosition(vPosition)
9 , m_qRotation(qRotation)
10 , m_vScale(vScale)
11{
12}
13
14template <typename Type>
15inline plTransformTemplate<Type> plTransformTemplate<Type>::Make(const plVec3Template<Type>& vPosition, const plQuatTemplate<Type>& qRotation /*= plQuatTemplate<Type>::IdentityQuaternion()*/, const plVec3Template<Type>& vScale /*= plVec3Template<Type>(1)*/)
16{
18 res.m_vPosition = vPosition;
19 res.m_qRotation = qRotation;
20 res.m_vScale = vScale;
21 return res;
22}
23
24template <typename Type>
26{
28 res.m_vPosition.SetZero();
29 res.m_qRotation = plQuatTemplate<Type>::MakeIdentity();
30 res.m_vScale.Set(1.0f);
31 return res;
32}
33
34template <typename Type>
36{
38
40 res.m_vPosition = mMat.GetTranslationVector();
41 res.m_vScale = mRot.GetScalingFactors();
43 res.m_qRotation = plQuat::MakeFromMat3(mRot);
44 return res;
45}
46
47template <typename Type>
49{
50 const auto invRot = globalTransformParent.m_qRotation.GetInverse();
51 const auto invScale = plVec3Template<Type>(1).CompDiv(globalTransformParent.m_vScale);
54 res.m_vPosition = (invRot * (globalTransformChild.m_vPosition - globalTransformParent.m_vPosition)).CompMul(invScale);
55 res.m_qRotation = invRot * globalTransformChild.m_qRotation;
56 res.m_vScale = invScale.CompMul(globalTransformChild.m_vScale);
57 return res;
58}
59
60template <typename Type>
61PL_ALWAYS_INLINE plTransformTemplate<Type> plTransformTemplate<Type>::MakeGlobalTransform(const plTransformTemplate& globalTransformParent, const plTransformTemplate& localTransformChild)
63 return globalTransformParent * localTransformChild;
64}
66template <typename Type>
69 *this = MakeIdentity();
70}
72template <typename Type>
73PL_ALWAYS_INLINE Type plTransformTemplate<Type>::GetMaxScale() const
75 auto absScale = m_vScale.Abs();
76 return plMath::Max(absScale.x, plMath::Max(absScale.y, absScale.z));
78
79template <typename Type>
81{
82 return (m_vScale.x * m_vScale.y * m_vScale.z) < 0.0f;
84
85template <typename Type>
87{
88 const Type fEpsilon = plMath::DefaultEpsilon<Type>();
89 return plMath::IsEqual(m_vScale.x, m_vScale.y, fEpsilon) && plMath::IsEqual(m_vScale.x, m_vScale.z, fEpsilon);
90}
92template <typename Type>
94{
95 return m_vPosition.IsIdentical(rhs.m_vPosition) && (m_qRotation == rhs.m_qRotation) && m_vScale.IsIdentical(rhs.m_vScale);
97
98template <typename Type>
99inline bool plTransformTemplate<Type>::IsEqual(const plTransformTemplate<Type>& rhs, Type fEpsilon) const
100{
101 return m_vPosition.IsEqual(rhs.m_vPosition, fEpsilon) && m_qRotation.IsEqualRotation(rhs.m_qRotation, fEpsilon) && m_vScale.IsEqual(rhs.m_vScale, fEpsilon);
102}
103
104template <typename Type>
106{
107 return m_vPosition.IsValid() && m_qRotation.IsValid(0.005f) && m_vScale.IsValid();
108}
109
110template <typename Type>
112{
113 auto result = m_qRotation.GetAsMat4();
114
115 result.m_fElementsCM[0] *= m_vScale.x;
116 result.m_fElementsCM[1] *= m_vScale.x;
117 result.m_fElementsCM[2] *= m_vScale.x;
118
119 result.m_fElementsCM[4] *= m_vScale.y;
120 result.m_fElementsCM[5] *= m_vScale.y;
121 result.m_fElementsCM[6] *= m_vScale.y;
122
123 result.m_fElementsCM[8] *= m_vScale.z;
124 result.m_fElementsCM[9] *= m_vScale.z;
125 result.m_fElementsCM[10] *= m_vScale.z;
126
127 result.m_fElementsCM[12] = m_vPosition.x;
128 result.m_fElementsCM[13] = m_vPosition.y;
129 result.m_fElementsCM[14] = m_vPosition.z;
130
131 return result;
132}
133
134
135template <typename Type>
137{
138 m_vPosition += v;
139}
140
141template <typename Type>
143{
144 m_vPosition -= v;
145}
146
147template <typename Type>
149{
150 const auto scaled = m_vScale.CompMul(v);
151 const auto rotated = m_qRotation * scaled;
152 return m_vPosition + rotated;
153}
154
155template <typename Type>
157{
158 const auto scaled = m_vScale.CompMul(v);
159 const auto rotated = m_qRotation * scaled;
160 return rotated;
161}
162
163template <typename Type>
164PL_ALWAYS_INLINE const plTransformTemplate<Type> operator*(const plQuatTemplate<Type>& q, const plTransformTemplate<Type>& t)
165{
166 plTransform r;
167
168 r.m_vPosition = t.m_vPosition;
169 r.m_qRotation = q * t.m_qRotation;
170 r.m_vScale = t.m_vScale;
171
172 return r;
173}
174
175template <typename Type>
176PL_ALWAYS_INLINE const plTransformTemplate<Type> operator*(const plTransformTemplate<Type>& t, const plQuatTemplate<Type>& q)
177{
178 plTransform r;
179
180 r.m_vPosition = t.m_vPosition;
181 r.m_qRotation = t.m_qRotation * q;
182 r.m_vScale = t.m_vScale;
183
184 return r;
185}
186
187template <typename Type>
188PL_ALWAYS_INLINE const plTransformTemplate<Type> operator+(const plTransformTemplate<Type>& t, const plVec3Template<Type>& v)
189{
190 return plTransformTemplate<Type>(t.m_vPosition + v, t.m_qRotation, t.m_vScale);
191}
192
193template <typename Type>
194PL_ALWAYS_INLINE const plTransformTemplate<Type> operator-(const plTransformTemplate<Type>& t, const plVec3Template<Type>& v)
195{
196 return plTransformTemplate<Type>(t.m_vPosition - v, t.m_qRotation, t.m_vScale);
197}
198
199template <typename Type>
200PL_ALWAYS_INLINE const plVec3Template<Type> operator*(const plTransformTemplate<Type>& t, const plVec3Template<Type>& v)
201{
202 return t.TransformPosition(v);
203}
204
205template <typename Type>
206inline const plTransformTemplate<Type> operator*(const plTransformTemplate<Type>& t1, const plTransformTemplate<Type>& t2)
207{
209
210 t.m_vPosition = (t1.m_qRotation * t2.m_vPosition.CompMul(t1.m_vScale)) + t1.m_vPosition;
211 t.m_qRotation = t1.m_qRotation * t2.m_qRotation;
212 t.m_vScale = t1.m_vScale.CompMul(t2.m_vScale);
213
214 return t;
215}
216
217template <typename Type>
218PL_ALWAYS_INLINE bool operator==(const plTransformTemplate<Type>& t1, const plTransformTemplate<Type>& t2)
219{
220 return t1.IsIdentical(t2);
221}
222
223template <typename Type>
224PL_ALWAYS_INLINE bool operator!=(const plTransformTemplate<Type>& t1, const plTransformTemplate<Type>& t2)
225{
226 return !t1.IsIdentical(t2);
227}
228
229template <typename Type>
231{
232 (*this) = GetInverse();
233}
234
235template <typename Type>
237{
238 const auto invRot = m_qRotation.GetInverse();
239 const auto invScale = plVec3Template<Type>(1).CompDiv(m_vScale);
240 const auto invPos = invRot * (invScale.CompMul(-m_vPosition));
241
242 return plTransformTemplate<Type>(invPos, invRot, invScale);
243}
A 3x3 component matrix class.
Definition Mat3.h:9
plResult SetScalingFactors(const plVec3Template< Type > &vXYZ, Type fEpsilon=plMath::DefaultEpsilon< Type >())
Tries to set the three scaling factors in the matrix. Returns PL_FAILURE if the matrix columns cannot...
Definition Mat3_inl.h:473
const plVec3Template< Type > GetScalingFactors() const
Returns the 3 scaling factors that are encoded in the matrix.
Definition Mat3_inl.h:460
A 4x4 component matrix class.
Definition Mat4.h:11
const plVec3Template< Type > GetTranslationVector() const
Returns the first 3 components of the last column.
Definition Mat4_inl.h:430
const plMat3Template< Type > GetRotationalPart() const
Returns the 3x3 rotational and scaling part of the matrix.
Definition Mat4_inl.h:458
Type m_fElementsCM[16]
The matrix as a 16-element Type array (column-major)
Definition Mat4.h:24
Quaternions can be used to represent rotations in 3D space.
Definition Quat.h:19
static plQuatTemplate< float > MakeFromMat3(const plMat3Template< float > &m)
Definition Quat_inl.h:275
static const plQuatTemplate< Type > MakeIdentity()
Static function that returns a quaternion that represents the identity rotation (none).
Definition Quat_inl.h:29
A class that represents position, rotation and scaling via a position vector, a quaternion and a scal...
Definition Transform.h:31
void Invert()
Inverts this transform.
Definition Transform_inl.h:230
static plTransformTemplate< Type > MakeIdentity()
Creates an identity transform.
Definition Transform_inl.h:25
static plTransformTemplate< Type > MakeGlobalTransform(const plTransformTemplate &globalTransformParent, const plTransformTemplate &localTransformChild)
Creates a transform that is the global transform, that is reached by applying the child's local trans...
Definition Transform_inl.h:61
static plTransformTemplate< Type > MakeLocalTransform(const plTransformTemplate &globalTransformParent, const plTransformTemplate &globalTransformChild)
Creates a transform that is the local transformation needed to get from the parent's transform to the...
Definition Transform_inl.h:48
bool HasMirrorScaling() const
Returns whether this transform contains negative scaling aka mirroring.
Definition Transform_inl.h:80
const plMat4Template< Type > GetAsMat4() const
Returns the transformation as a matrix.
Definition Transform_inl.h:111
bool IsValid() const
Checks that all components are valid (no NaN, only finite numbers).
Definition Transform_inl.h:105
const plTransformTemplate GetInverse() const
Returns the inverse of this transform.
Definition Transform_inl.h:236
bool IsEqual(const plTransformTemplate &rhs, Type fEpsilon) const
Equality Check with epsilon.
Definition Transform_inl.h:99
Type GetMaxScale() const
Returns the scale component with maximum magnitude.
Definition Transform_inl.h:73
void SetIdentity()
Sets the position to be zero and the rotation to identity.
Definition Transform_inl.h:67
bool IsIdentical(const plTransformTemplate &rhs) const
Equality Check (bitwise)
Definition Transform_inl.h:93
bool ContainsUniformScale() const
Returns whether this transform contains uniform scaling.
Definition Transform_inl.h:86
plTransformTemplate()=default
Default constructor: Does not do any initialization.
static plTransformTemplate< Type > Make(const plVec3Template< Type > &vPosition, const plQuatTemplate< Type > &qRotation=plQuatTemplate< Type >::MakeIdentity(), const plVec3Template< Type > &vScale=plVec3Template< Type >(1))
Creates a transform from the given position, rotation and scale.
Definition Transform_inl.h:15
static plTransformTemplate< Type > MakeFromMat4(const plMat4Template< Type > &mMat)
Creates a transform from the given matrix.
Definition Transform_inl.h:35
A 3-component vector class.
Definition Vec3.h:9
const plVec3Template< Type > CompDiv(const plVec3Template< Type > &rhs) const
Returns the component-wise division of *this and rhs.
Definition Vec3_inl.h:354
constexpr bool IsEqual(Type lhs, Type rhs, Type fEpsilon)
Checks, whether fValue is in the range [fDesired - fMaxImprecision; fDesired + fMaxImprecision].
Definition Math_inl.h:276
constexpr PL_ALWAYS_INLINE T Max(T f1, T f2)
Returns the greater value, f1 or f2.
Definition Math_inl.h:39
PL_ALWAYS_INLINE void IgnoreResult()
Used to silence compiler warnings, when success or failure doesn't matter.
Definition Types.h:69