COGS
Computer Graphics Structures library
Transform.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef COGS_TRANSFORM_H
7 #define COGS_TRANSFORM_H
8 #include <functional>
9 
10 #include <glm/glm.hpp>
11 #include <glm/gtx/quaternion.hpp>
12 
13 #include <COGS/API.h>
14 
15 
16 namespace cogs
17 {
18 
26  class COGS_API Transform
27  {
28  public:
29 
31  Transform() = default;
32 
34  Transform(const glm::mat4 &matrix);
35 
37  Transform(const std::function<void(void)> &notifier_func);
38 
39  virtual ~Transform() = default;
40 
41  Transform(const Transform &) = default;
42  Transform &operator=(const Transform &) = default;
43  Transform(Transform &&) noexcept = default;
44  Transform &operator=(Transform &&) noexcept = default;
45 
47  void Translate(const glm::vec3 &move_vector);
48 
50  void SetPosition(const glm::vec3 &position);
51 
53  const glm::vec3 GetPosition() const;
54 
56  void Rotate(const glm::quat &rotation);
57 
59  void SetRotation(const glm::quat &rotation);
60 
62  const glm::quat GetRotation() const;
63 
65  void Scale(const glm::vec3 &scale);
66 
68  void SetScaling(const glm::vec3 &scale);
69 
71  const glm::vec3 GetScaling() const;
72 
74  void Set(const glm::mat4 &trans_mat);
75 
77  virtual const glm::mat4 &GetMat4() const;
78 
80  void AddTransform(const Transform &transform);
81 
96  void InterpolateWith(
97  const Transform &target,
98  const float target_weight,
99  Transform *out_transform
100  ) const;
101 
103  virtual void SaveToStream(std::ostream &ostr) const;
104 
106  virtual void LoadFromStream(std::istream &istr);
107 
109  Transform operator*(const Transform &t) const;
110 
111  operator glm::mat4() const { return GetMat4(); };
112 
113  protected:
114 
116  virtual void OnInvariantChange();
117 
118  private:
119  glm::vec3 position = glm::vec3(0.0f);
120  glm::quat rotation = glm::quat();
121  glm::vec3 scaling = glm::vec3(1.0f);
122  mutable bool is_matrix_invalid = false; // whether matrix should be re-calculated
123  mutable glm::mat4 matrix = glm::mat4(1.0f); // cached transformation matrix, re-calculated only when required
124  std::function<void(void)> on_change_notifier = nullptr;
125  };
126 
127 }
128 #endif /* !COGS_TRANSFORM_H */
Namespace of "Computer Graphic Structures" library, which implement many core classes and structures...
Definition: AabbHierarchy.h:8
Class used for effective management of transformation matrix by handling translation, rotation and scaling separately.
Definition: Transform.h:26