TDME2  1.9.200
Transform.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <string>
5 
6 #include <tdme/tdme.h>
7 #include <tdme/engine/fwd-tdme.h>
9 #include <tdme/engine/Rotation.h>
10 #include <tdme/math/fwd-tdme.h>
11 #include <tdme/math/Matrix4x4.h>
12 #include <tdme/math/Quaternion.h>
13 #include <tdme/math/Vector3.h>
14 
15 using std::vector;
16 using std::string;
17 using std::to_string;
18 
24 
25 /**
26  * Transform which contain scale, rotations and translation
27  * @author Andreas Drewke
28  */
30 private:
34  vector<Rotation> rotations;
36 
37 public:
38  /**
39  * Public constructor
40  */
41  inline Transform() {
43  scale.set(1.0f, 1.0f, 1.0f);
45  }
46 
47  /**
48  * Destructor
49  */
50  virtual inline ~Transform() {
51  }
52 
53  /**
54  * @return object translation
55  */
56  inline const Vector3& getTranslation() const {
57  return translation;
58  }
59 
60  /**
61  * Set translation
62  * @param translation translation
63  */
64  inline void setTranslation(const Vector3& translation) {
65  this->translation.set(translation);
66  }
67 
68  /**
69  * @return object scale
70  */
71  inline const Vector3& getScale() const {
72  return scale;
73  }
74 
75  /**
76  * Set scale
77  * @param scale scale
78  */
79  inline void setScale(const Vector3& scale) {
80  this->scale.set(scale);
81  }
82 
83  /**
84  * @return rotation count
85  */
86  inline const int getRotationCount() const {
87  return rotations.size();
88  }
89 
90  /**
91  * Get rotation at given index
92  * @param idx rotation index
93  * @return rotation
94  */
95  inline Rotation& getRotation(const int idx) {
96  return rotations[idx];
97  }
98 
99  /**
100  * Get rotation at given index
101  * @param idx rotation index
102  * @return rotation
103  */
104  inline const Rotation& getRotation(const int idx) const {
105  return rotations[idx];
106  }
107 
108  /**
109  * Add rotation
110  * @param axis axis
111  * @param angle angle
112  */
113  inline void addRotation(const Vector3& axis, const float angle) {
114  rotations.emplace_back(axis, angle);
115  }
116 
117  /**
118  * Remove rotation
119  * @param idx index
120  */
121  inline void removeRotation(const int idx) {
122  rotations.erase(rotations.begin() + idx);
123  }
124 
125  /**
126  * @param idx rotation index
127  * @return rotation axis for rotation with given index
128  */
129  inline const Vector3& getRotationAxis(const int idx) const {
130  return rotations[idx].getAxis();
131  }
132 
133  /**
134  * Set rotation axis
135  * @param idx rotation index
136  * @param axis rotation axis
137  */
138  inline void setRotationAxis(const int idx, const Vector3& axis) {
139  return rotations[idx].setAxis(axis);
140  }
141 
142  /**
143  * @param idx rotation index
144  * @return rotation angle for rotation with given index
145  */
146  inline const float getRotationAngle(const int idx) const {
147  return rotations[idx].getAngle();
148  }
149 
150  /**
151  * @param idx rotation index
152  * @param angle rotation angle
153  * @return rotation angle for rotation with given index
154  */
155  inline void setRotationAngle(const int idx, const float angle) {
156  rotations[idx].setAngle(angle);
157  }
158 
159  /**
160  * @return rotations quaternion
161  */
162  inline const Quaternion& getRotationsQuaternion() const {
163  return rotationsQuaternion;
164  }
165 
166  /**
167  * @return this transform matrix
168  */
169  inline const Matrix4x4& getTransformMatrix() const {
170  return transformMatrix;
171  }
172 
173  /**
174  * Set transform
175  * @param transform transform
176  */
177  inline virtual void setTransform(const Transform& transform) {
178  if (this == &transform) return;
179  *this = transform;
180  }
181 
182  /**
183  * Set up this transform from given matrix and rotation order
184  * @param matrix matrix
185  * @param rotationOrder rotation order
186  */
187  virtual void fromMatrix(const Matrix4x4& matrix, RotationOrder* rotationOrder);
188 
189  /**
190  * Computes transform matrix
191  * @param parentTransform parent transform
192  */
193  virtual void update();
194 
195  /**
196  * Invert this transform
197  */
198  virtual Transform& invert();
199 
200  /**
201  * Clones the transform
202  * @return new cloned transform
203  */
204  inline Transform clone() const {
205  Transform clonedTransform;
206  clonedTransform.translation = translation;
207  clonedTransform.scale = scale;
208  clonedTransform.rotationsQuaternion = rotationsQuaternion;
209  clonedTransform.rotations = rotations;
210  clonedTransform.transformMatrix = transformMatrix;
211  return clonedTransform;
212 
213  }
214 
215  /**
216  * Multiplies this transform with another transform
217  * @param t transform
218  * @return this transform
219  */
220  inline Transform& multiply(const Transform& t) {
221  update();
223  scale*= t.scale;
225  auto eulerAngles = rotationsQuaternion.computeEulerAngles();
226  rotations.clear();
227  rotations.emplace_back(Vector3(0.0f, 0.0f, 1.0f), eulerAngles[2]);
228  rotations.emplace_back(Vector3(0.0f, 1.0f, 0.0f), eulerAngles[1]);
229  rotations.emplace_back(Vector3(1.0f, 0.0f, 0.0f), eulerAngles[0]);
230  update();
231  return *this;
232  }
233 
234  /**
235  * Operator *
236  * @param v vec3 to multiply by
237  * @return new vec3 (this * v)
238  */
239  inline Vector3 operator *(const Vector3& v) const {
240  return transformMatrix * v;
241  }
242 
243  /*
244  * Operator *=
245  * @param t transform to multiply by
246  * @return this transform multiplied by t
247  */
248  inline Transform& operator *=(const Transform& t) {
249  return this->multiply(t);
250  }
251 
252  /**
253  * Operator *
254  * @param t transform to multiply by
255  * @return new transform (this * t)
256  */
257  inline Transform operator *(const Transform& t) const {
258  auto r = this->clone().multiply(t);
259  return r;
260  }
261 
262  /**
263  * Equality comparison operator
264  * @param t transform to compare to
265  * @return equality
266  */
267 
268  inline bool operator ==(const Transform& t) const {
270  }
271 
272  /**
273  * Non equality comparison operator
274  * @param t transform to compare to
275  * @return non equality
276  */
277 
278  inline bool operator !=(const Transform& t) const {
280  }
281 
282 };
Rotation representation.
Definition: Rotation.h:18
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
virtual ~Transform()
Destructor.
Definition: Transform.h:50
virtual void setTransform(const Transform &transform)
Set transform.
Definition: Transform.h:177
virtual void fromMatrix(const Matrix4x4 &matrix, RotationOrder *rotationOrder)
Set up this transform from given matrix and rotation order.
Definition: Transform.cpp:22
const Rotation & getRotation(const int idx) const
Get rotation at given index.
Definition: Transform.h:104
void setRotationAngle(const int idx, const float angle)
Definition: Transform.h:155
void setRotationAxis(const int idx, const Vector3 &axis)
Set rotation axis.
Definition: Transform.h:138
void setTranslation(const Vector3 &translation)
Set translation.
Definition: Transform.h:64
Vector3 operator*(const Vector3 &v) const
Operator *.
Definition: Transform.h:239
void removeRotation(const int idx)
Remove rotation.
Definition: Transform.h:121
Transform clone() const
Clones the transform.
Definition: Transform.h:204
const Vector3 & getScale() const
Definition: Transform.h:71
Rotation & getRotation(const int idx)
Get rotation at given index.
Definition: Transform.h:95
Transform & operator*=(const Transform &t)
Definition: Transform.h:248
Quaternion rotationsQuaternion
Definition: Transform.h:33
virtual Transform & invert()
Invert this transform.
Definition: Transform.cpp:58
void setScale(const Vector3 &scale)
Set scale.
Definition: Transform.h:79
vector< Rotation > rotations
Definition: Transform.h:34
bool operator!=(const Transform &t) const
Non equality comparison operator.
Definition: Transform.h:278
const int getRotationCount() const
Definition: Transform.h:86
Transform()
Public constructor.
Definition: Transform.h:41
const Vector3 & getRotationAxis(const int idx) const
Definition: Transform.h:129
Matrix4x4 transformMatrix
Definition: Transform.h:35
const Quaternion & getRotationsQuaternion() const
Definition: Transform.h:162
Transform & multiply(const Transform &t)
Multiplies this transform with another transform.
Definition: Transform.h:220
virtual void update()
Computes transform matrix.
Definition: Transform.cpp:33
const Vector3 & getTranslation() const
Definition: Transform.h:56
bool operator==(const Transform &t) const
Equality comparison operator.
Definition: Transform.h:268
void addRotation(const Vector3 &axis, const float angle)
Add rotation.
Definition: Transform.h:113
const float getRotationAngle(const int idx) const
Definition: Transform.h:146
const Matrix4x4 & getTransformMatrix() const
Definition: Transform.h:169
Represents rotation orders of a model.
Definition: RotationOrder.h:23
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Matrix4x4 & identity()
Creates identity matrix.
Definition: Matrix4x4.h:158
bool equals(const Matrix4x4 &matrix) const
Compares this matrix with given matrix.
Definition: Matrix4x4.h:292
Quaternion class representing quaternion mathematical structure and operations with x,...
Definition: Quaternion.h:24
Quaternion & identity()
Creates identity quaternion.
Definition: Quaternion.h:226
Vector3 computeEulerAngles() const
Compute Euler angles.
Definition: Quaternion.h:404
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70