STransformation3.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <ionCore.h>
5 #include <glm/gtc/matrix_transform.hpp>
6 #include <glm/gtx/euler_angles.hpp>
7 
8 
9 namespace ion
10 {
11 
15  {
22  };
23 
26  enum class ERotationOrder
27  {
28  ZYX,
29  ZXY,
30  YXZ,
31  YZX,
32  XZY,
33  XYZ
34  };
35 
36 
40  {
41 
42  public:
43 
45  : Translation(1.f), Rotation(1.f), Scale(1.f), Transformation(1.f)
46  {}
47 
48  operator glm::mat4() const
49  {
50  return Transformation;
51  }
52 
53  glm::mat4 const operator() () const
54  {
55  return Translation * Rotation * Scale;
56  }
57 
58  glm::mat4 const GetGLMMat4() const
59  {
60  return Transformation;
61  }
62 
63  glm::mat4 const Get() const
64  {
65  return Transformation;
66  }
67 
68  void Update()
69  {
70  switch (TransformationOrder)
71  {
72  default:
74  Transformation = Translation * Rotation * Scale;
75  break;
76 
78  Transformation = Translation * Scale * Rotation;
79  break;
80 
82  Transformation = Rotation * Scale * Translation;
83  break;
84 
86  Transformation = Rotation * Translation * Scale;
87  break;
88 
90  Transformation = Scale * Translation * Rotation;
91  break;
92 
94  Transformation = Scale * Rotation * Translation;
95  break;
96  }
97  }
98 
99  void SetRotation(glm::mat4 const & rotation)
100  {
101  UseExplicitRotation = true;
102  UseExplicitTransformation = false;
103  Rotation = rotation;
104  Update();
105  }
106 
107  void SetRotation(vec3f const & rotation)
108  {
109  static glm::vec3 const X = glm::vec3(1, 0, 0);
110  static glm::vec3 const Y = glm::vec3(0, 1, 0);
111  static glm::vec3 const Z = glm::vec3(0, 0, 1);
112 
113  UseExplicitRotation = false;
114  UseExplicitTransformation = false;
115 
116  RotationVector = rotation;
117 
118  switch (RotationOrder)
119  {
120  default:
121  case ERotationOrder::ZYX:
122  Rotation = glm::rotate(glm::mat4(1.f), rotation.Z, glm::vec3(0, 0, 1));
123  Rotation = glm::rotate(Rotation, rotation.Y, glm::vec3(0, 1, 0));
124  Rotation = glm::rotate(Rotation, rotation.X, glm::vec3(1, 0, 0));
125  break;
126 
127  case ERotationOrder::ZXY:
128  Rotation = glm::rotate(glm::mat4(1.f), rotation.Z, Z);
129  Rotation = glm::rotate(Rotation, rotation.X, X);
130  Rotation = glm::rotate(Rotation, rotation.Y, Y);
131  break;
132 
133  case ERotationOrder::YXZ:
134  Rotation = glm::rotate(glm::mat4(1.f), rotation.Y, Y);
135  Rotation = glm::rotate(Rotation, rotation.X, X);
136  Rotation = glm::rotate(Rotation, rotation.Z, Z);
137  break;
138 
139  case ERotationOrder::YZX:
140  Rotation = glm::rotate(glm::mat4(1.f), rotation.Y, Y);
141  Rotation = glm::rotate(Rotation, rotation.Z, Z);
142  Rotation = glm::rotate(Rotation, rotation.X, X);
143  break;
144 
145  case ERotationOrder::XZY:
146  Rotation = glm::rotate(glm::mat4(1.f), rotation.X, X);
147  Rotation = glm::rotate(Rotation, rotation.Z, Z);
148  Rotation = glm::rotate(Rotation, rotation.Y, Y);
149  break;
150 
151  case ERotationOrder::XYZ:
152  Rotation = glm::rotate(glm::mat4(1.f), rotation.X, X);
153  Rotation = glm::rotate(Rotation, rotation.Y, Y);
154  Rotation = glm::rotate(Rotation, rotation.Z, Z);
155  break;
156  }
157 
158  Update();
159  }
160 
162  {
163  return RotationVector;
164  }
165 
166  void SetScale(vec3f const & scale)
167  {
168  UseExplicitTransformation = false;
169  Scale = glm::scale(glm::mat4(1.f), (ScaleVector = scale).ToGLM());
170  Update();
171  }
172 
173  vec3f GetScale() const
174  {
175  return ScaleVector;
176  }
177 
178  void SetTranslation(vec3f const & translation)
179  {
180  UseExplicitTransformation = false;
181  Translation = glm::translate(glm::mat4(1.f), (TranslationVector = translation).ToGLM());
182  Update();
183  }
184 
186  {
187  return TranslationVector;
188  }
189 
190  void Set(glm::mat4 const & Transformation)
191  {
192  UseExplicitTransformation = false;
193  this->Transformation = Transformation;
194  }
195 
196  void SetRotationOrder(ERotationOrder const RotationOrder)
197  {
198  this->RotationOrder = RotationOrder;
199 
200  if (! UseExplicitRotation)
201  {
202  SetRotation(RotationVector);
203  }
204  }
205 
207  {
208  return RotationOrder;
209  }
210 
211  void SetOrder(ETransformationOrder const TransformationOrder)
212  {
213  this->TransformationOrder = TransformationOrder;
214 
215  if (! UseExplicitTransformation)
216  {
217  Update();
218  }
219  }
220 
222  {
223  return TransformationOrder;
224  }
225 
226  protected:
227 
228  vec3f TranslationVector, RotationVector, ScaleVector = 1;
229  glm::mat4 Translation, Rotation, Scale;
230 
231  glm::mat4 Transformation;
232 
233  bool UseExplicitRotation = false;
234  bool UseExplicitTransformation = false;
237 
238  };
239 
241  {
242 
243  public:
244 
245  class CStage
246  {
247 
248  public:
249 
251 
252  };
253 
254  CStage * AddStage(ion::string const & Name)
255  {
256  CStage * Stage = new CStage();
257  TransformationOrder.push_back(Stage);
258  TransformationLabels[Name] = Stage;
259 
260  return Stage;
261  }
262 
263  void SetStageTransformationOrder(ion::string const & Name, ETransformationOrder TransformationOrder)
264  {
265  auto it = TransformationLabels.find(Name);
266  if (it != TransformationLabels.end())
267  it->second->Transformation.SetOrder(TransformationOrder);
268  else
269  ion::Log::Error("Failed to find transformation with name %s.", Name);
270  }
271 
272  STransformation3 & GetStageTransformation(ion::string const & Name)
273  {
274  static STransformation3 DummyValue;
275 
276  auto it = TransformationLabels.find(Name);
277  if (it != TransformationLabels.end())
278  return it->second->Transformation;
279  else
280  ion::Log::Error("Failed to find transformation with name %s.", Name);
281 
282  return DummyValue;
283  }
284 
285  glm::mat4 const GetFinalTransformation()
286  {
287  glm::mat4 Result = glm::mat4(1.f);
288 
289  for (auto Stage : TransformationOrder)
290  {
291  Result = Stage->Transformation.Get() * Result;
292  }
293 
294  return Result;
295  }
296 
297  protected:
298 
299  ion::vector<CStage *> TransformationOrder;
300  ion::map<ion::string, CStage *> TransformationLabels;
301 
302  };
303 
304 }
void SetRotation(vec3f const &rotation)
Definition: STransformation3.h:107
STransformation3 Transformation
Definition: STransformation3.h:250
glm::mat4 Transformation
Definition: STransformation3.h:231
ETransformationOrder
Order of transformations used by STransformation3.
Definition: STransformation3.h:14
Definition: STransformation3.h:245
void Update()
Definition: STransformation3.h:68
void SetRotationOrder(ERotationOrder const RotationOrder)
Definition: STransformation3.h:196
Definition: STransformation3.h:240
STransformation3 & GetStageTransformation(ion::string const &Name)
Definition: STransformation3.h:272
vec3f TranslationVector
Definition: STransformation3.h:228
void Set(glm::mat4 const &Transformation)
Definition: STransformation3.h:190
CStage * AddStage(ion::string const &Name)
Definition: STransformation3.h:254
T Z
Definition: vec3.h:20
Describes a 3D transformation as a composed translation, rotation, and scale.
Definition: STransformation3.h:39
T X
Definition: vec3.h:18
glm::mat4 const Get() const
Definition: STransformation3.h:63
vec3f GetRotation() const
Definition: STransformation3.h:161
glm::mat4 Translation
Definition: STransformation3.h:229
ion::vector< CStage * > TransformationOrder
Definition: STransformation3.h:299
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
void SetRotation(glm::mat4 const &rotation)
Definition: STransformation3.h:99
STransformation3()
Definition: STransformation3.h:44
ERotationOrder GetRotationOrder() const
Definition: STransformation3.h:206
vec3f GetTranslation() const
Definition: STransformation3.h:185
void SetScale(vec3f const &scale)
Definition: STransformation3.h:166
void SetTranslation(vec3f const &translation)
Definition: STransformation3.h:178
ERotationOrder
Order of rotations, i.e. Euler angles, used by STransformation3.
Definition: STransformation3.h:26
glm::mat4 const GetGLMMat4() const
Definition: STransformation3.h:58
void SetStageTransformationOrder(ion::string const &Name, ETransformationOrder TransformationOrder)
Definition: STransformation3.h:263
static void Error(char const *const Format, Args const &...args)
Definition: ionLogger.h:79
vec3f GetScale() const
Definition: STransformation3.h:173
glm::mat4 const GetFinalTransformation()
Definition: STransformation3.h:285
void SetOrder(ETransformationOrder const TransformationOrder)
Definition: STransformation3.h:211
ETransformationOrder GetOrder() const
Definition: STransformation3.h:221
ion::map< ion::string, CStage * > TransformationLabels
Definition: STransformation3.h:300
T Y
Definition: vec3.h:19