Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TDecomposedTransform.h
Go to the documentation of this file.
1#pragma once
2
3#include "Math/TVector3.h"
4#include "Math/TQuaternion.h"
5#include "Math/TMatrix4.h"
6#include "Math/math.h"
7
8#include <Common/assertion.h>
9
10#include <cstdlib>
11
12namespace ph::math
13{
14
22template<typename T>
24{
25public:
30
35 const TVector3<T>& pos,
36 const TQuaternion<T>& rot,
37 const TVector3<T>& scale);
38
40 {
41 return translate(amount.x(), amount.y(), amount.z());
42 }
43
44 inline TDecomposedTransform& translate(const T x, const T y, const T z)
45 {
46 m_pos.addLocal({x, y, z});
47
48 return *this;
49 }
50
52 {
53 TQuaternion<T> addtionalRot(rot);
54 addtionalRot.normalizeLocal();
55
56 const TQuaternion<T> totalRot = addtionalRot.mul(m_rot).normalizeLocal();
57 setRot(totalRot);
58
59 return *this;
60 }
61
62 inline TDecomposedTransform& rotate(const TVector3<T>& axis, const T degrees)
63 {
64 const TVector3<T> normalizedAxis(axis.normalize());
65 rotate(TQuaternion<T>(normalizedAxis, math::to_radians(degrees)));
66
67 return *this;
68 }
69
70 inline TDecomposedTransform& scale(const TVector3<T>& amount)
71 {
72 return scale(amount.x(), amount.y(), amount.z());
73 }
74
75 inline TDecomposedTransform& scale(const T x, const T y, const T z)
76 {
77 m_scale.mulLocal({x, y, z});
78
79 return *this;
80 }
81
83 {
84 m_pos = pos;
85
86 return *this;
87 }
88
90 {
91 m_rot = rot;
92
93 return *this;
94 }
95
97 {
99 }
100
102 {
103 m_scale = scale;
104
105 return *this;
106 }
107
108 TVector3<T> getPos() const;
109 TQuaternion<T> getRot() const;
110 TVector3<T> getScale() const;
111
112 void genTransformMatrix(TMatrix4<T>* out_result) const;
113 void genInverseTransformMatrix(TMatrix4<T>* out_result) const;
114
115 // Inverts the transformation components. The effect of inverted and
116 // un-inverted transforms will cancel each other out.
118
119 bool hasScaleEffect(T margin = 0) const;
120 bool isScaleUniform(T margin = 0) const;
121 bool isIdentity() const;
122
123 bool operator == (const TDecomposedTransform& rhs) const;
124 bool operator != (const TDecomposedTransform& rhs) const;
125
126private:
127 TVector3<T> m_pos;
128 TQuaternion<T> m_rot;
129 TVector3<T> m_scale;
130};
131
132// In-header Implementations:
133
134template<typename T>
141
142template<typename T>
144 const TVector3<T>& pos,
145 const TQuaternion<T>& rot,
146 const TVector3<T>& scale)
147
148 : m_pos(pos)
149 , m_rot(rot)
150 , m_scale(scale)
151{}
152
153template<typename T>
155{
156 PH_ASSERT(out_result);
157
158 TMatrix4<T> translationMatrix;
159 TMatrix4<T> rotationMatrix;
160 TMatrix4<T> scaleMatrix;
161 translationMatrix.initTranslation(m_pos);
162 rotationMatrix.initRotation(m_rot);
163 scaleMatrix.initScale(m_scale);
164
165 *out_result = translationMatrix.mul(rotationMatrix).mul(scaleMatrix);
166}
167
168template<typename T>
170{
171 PH_ASSERT(out_result);
172
173 TDecomposedTransform inverted = this->invert();
174
175 TMatrix4<T> inverseTranslationMatrix;
176 TMatrix4<T> inverseRotationMatrix;
177 TMatrix4<T> inverseScaleMatrix;
178 inverseTranslationMatrix.initTranslation(inverted.m_pos);
179 inverseRotationMatrix.initRotation(inverted.m_rot);
180 inverseScaleMatrix.initScale(inverted.m_scale);
181
182 *out_result = inverseScaleMatrix.mul(inverseRotationMatrix).mul(inverseTranslationMatrix);
183}
184
185template<typename T>
187{
189 result.m_pos = m_pos.mul(-1);
190 result.m_rot = m_rot.conjugate();
191 result.m_scale = m_scale.rcp();
192 return result;
193}
194
195template<typename T>
196inline bool TDecomposedTransform<T>::hasScaleEffect(const T margin) const
197{
198 return std::abs(m_scale.x() - 1) > margin ||
199 std::abs(m_scale.y() - 1) > margin ||
200 std::abs(m_scale.z() - 1) > margin;
201}
202
203template<typename T>
204inline bool TDecomposedTransform<T>::isScaleUniform(const T margin) const
205{
206 const T dSxSy = std::abs(m_scale.x() - m_scale.y());
207 const T dSySz = std::abs(m_scale.y() - m_scale.z());
208 const T dSzSx = std::abs(m_scale.z() - m_scale.x());
209
210 return dSxSy < margin && dSySz < margin && dSzSx < margin;
211}
212
213template<typename T>
215{
216 return *this == TDecomposedTransform();
217}
218
219template<typename T>
221{
222 return m_pos == rhs.m_pos &&
223 m_rot == rhs.m_rot &&
224 m_scale == rhs.m_scale;
225}
226
227template<typename T>
229{
230 return !(*this == rhs);
231}
232
233template<typename T>
235{
236 return m_pos;
237}
238
239template<typename T>
241{
242 return m_rot;
243}
244
245template<typename T>
247{
248 return m_scale;
249}
250
251}// end namespace ph::math
Perform affine transformations in decomposed form.
Definition TDecomposedTransform.h:24
TDecomposedTransform rotate(const TQuaternion< T > &rot)
Definition TDecomposedTransform.h:51
TDecomposedTransform & rotate(const TVector3< T > &axis, const T degrees)
Definition TDecomposedTransform.h:62
TDecomposedTransform & scale(const T x, const T y, const T z)
Definition TDecomposedTransform.h:75
TQuaternion< T > getRot() const
Definition TDecomposedTransform.h:240
TDecomposedTransform invert() const
Definition TDecomposedTransform.h:186
bool operator!=(const TDecomposedTransform &rhs) const
Definition TDecomposedTransform.h:228
bool isIdentity() const
Definition TDecomposedTransform.h:214
bool isScaleUniform(T margin=0) const
Definition TDecomposedTransform.h:204
TDecomposedTransform & translate(const T x, const T y, const T z)
Definition TDecomposedTransform.h:44
void genTransformMatrix(TMatrix4< T > *out_result) const
Definition TDecomposedTransform.h:154
TDecomposedTransform & setRot(const TQuaternion< T > &rot)
Definition TDecomposedTransform.h:89
TDecomposedTransform & scale(const TVector3< T > &amount)
Definition TDecomposedTransform.h:70
void genInverseTransformMatrix(TMatrix4< T > *out_result) const
Definition TDecomposedTransform.h:169
bool operator==(const TDecomposedTransform &rhs) const
Definition TDecomposedTransform.h:220
TDecomposedTransform()
Creates a transformation that will not have any effect. Defaults to identity transform.
Definition TDecomposedTransform.h:135
TDecomposedTransform & setScale(const TVector3< T > &scale)
Definition TDecomposedTransform.h:101
TDecomposedTransform & setPos(const TVector3< T > &pos)
Definition TDecomposedTransform.h:82
bool hasScaleEffect(T margin=0) const
Definition TDecomposedTransform.h:196
TVector3< T > getScale() const
Definition TDecomposedTransform.h:246
TVector3< T > getPos() const
Definition TDecomposedTransform.h:234
TDecomposedTransform & translate(const TVector3< T > &amount)
Definition TDecomposedTransform.h:39
TDecomposedTransform & setScale(const T scale)
Definition TDecomposedTransform.h:96
Represents a 4x4 matrix.
Definition TMatrix4.h:17
TMatrix4 mul(const TMatrix4 &rhs) const
Definition TMatrix4.ipp:150
TMatrix4 & initRotation(const TQuaternion< T > &rot)
Definition TMatrix4.ipp:79
TMatrix4 & initScale(T x, T y, T z)
Definition TMatrix4.ipp:116
TMatrix4 & initTranslation(T x, T y, T z)
Definition TMatrix4.ipp:62
Represents a quaternion.
Definition TQuaternion.h:17
TQuaternion & normalizeLocal()
Definition TQuaternion.ipp:159
static TQuaternion makeNoRotation()
Definition TQuaternion.ipp:14
TQuaternion mul(const TVector3< T > &xyz) const
Quaternion multiplication (treating the input's w component as 0).
Definition TQuaternion.ipp:143
Represents a 3-D vector.
Definition TVector3.h:17
T & y()
Definition TVector3.ipp:189
T & z()
Definition TVector3.ipp:195
T & x()
Definition TVector3.ipp:183
Derived normalize() const
Normalize the vector. Notice that normalizing a integer typed vector will result in 0-vector most of ...
Definition TVectorNBase.ipp:50
Miscellaneous math utilities.
Math functions and utilities.
Definition TransformInfo.h:10
T to_radians(const T degrees)
Convert degrees to radians.
Definition math.h:140