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