Transform.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #ifndef ANKI_MATH_TRANSFORM_H
  2. #define ANKI_MATH_TRANSFORM_H
  3. #include "anki/math/Vec3.h"
  4. #include "anki/math/Mat3.h"
  5. #include "anki/math/CommonIncludes.h"
  6. namespace anki {
  7. /// @addtogroup Math
  8. /// @{
  9. /// Transformation
  10. template<typename T>
  11. class TTransform
  12. {
  13. public:
  14. /// @name Constructors
  15. /// @{
  16. explicit TTransform()
  17. {}
  18. TTransform(const TTransform& b)
  19. : origin(b.origin), rotation(b.rotation), scale(b.scale)
  20. {}
  21. explicit TTransform(const Mat4& m4)
  22. {
  23. rotation = m4.getRotationPart();
  24. origin = m4.getTranslationPart();
  25. scale = 1.0;
  26. }
  27. explicit TTransform(const TVec3<T>& origin_, const TMat3<T>& rotation_,
  28. const T scale_)
  29. : origin(origin_), rotation(rotation_), scale(scale_)
  30. {}
  31. /// @}
  32. /// @name Accessors
  33. /// @{
  34. const TVec3<T>& getOrigin() const
  35. {
  36. return origin;
  37. }
  38. TVec3<T>& getOrigin()
  39. {
  40. return origin;
  41. }
  42. void setOrigin(const TVec3<T> o)
  43. {
  44. origin = o;
  45. }
  46. const TMat3<T>& getRotation() const
  47. {
  48. return rotation;
  49. }
  50. TMat3<T>& getRotation()
  51. {
  52. return rotation;
  53. }
  54. void setRotation(const TMat3<T>& r)
  55. {
  56. rotation = r;
  57. }
  58. T getScale() const
  59. {
  60. return scale;
  61. }
  62. T& getScale()
  63. {
  64. return scale;
  65. }
  66. void setScale(const T s)
  67. {
  68. scale = s;
  69. }
  70. /// @}
  71. /// @name Operators with same type
  72. /// @{
  73. TTransform& operator=(const TTransform& b)
  74. {
  75. origin = b.origin;
  76. rotation = b.rotation;
  77. scale = b.scale;
  78. return *this;
  79. }
  80. Bool operator==(const TTransform& b) const
  81. {
  82. return origin == b.origin && rotation == b.rotation && scale == b.scale;
  83. }
  84. Bool operator!=(const TTransform& b) const
  85. {
  86. return !operator==(b);
  87. }
  88. /// @}
  89. /// @name Other
  90. /// @{
  91. void setIdentity()
  92. {
  93. (*this) = getIdentity();
  94. }
  95. static const TTransform& getIdentity()
  96. {
  97. static const TTransform ident(
  98. TVec3<T>(0.0), TMat3<T>::getIdentity(), 1.0);
  99. return ident;
  100. }
  101. static void combineTransformations(
  102. const TVec3<T>& t0, const TMat3<T>& r0, const T s0,
  103. const TVec3<T>& t1, const TMat3<T>& r1, const T s1,
  104. TVec3<T>& tf, TMat3<T>& rf, T& sf)
  105. {
  106. tf = t1.getTransformed(t0, r0, s0);
  107. rf = r0 * r1;
  108. sf = s0 * s1;
  109. }
  110. static void combineTransformations(
  111. const TVec3<T>& t0, const TMat3<T>& r0,
  112. const TVec3<T>& t1, const TMat3<T>& r1,
  113. TVec3<T>& tf, TMat3<T>& rf)
  114. {
  115. tf = t1.getTransformed(t0, r0);
  116. rf = r0 * r1;
  117. }
  118. /// @copybrief combineTTransformations
  119. static TTransform combineTransformations(const TTransform& a,
  120. const TTransform& b)
  121. {
  122. TTransform out;
  123. out.origin = b.origin.getTransformed(a.origin, a.rotation, a.scale);
  124. out.rotation = a.rotation * b.rotation;
  125. out.scale = a.scale * b.scale;
  126. return out;
  127. }
  128. /// Get the inverse transformation. Its faster that inverting a Mat4
  129. TTransform getInverse() const
  130. {
  131. TTransform o;
  132. o.rotation = rotation.getTransposed(); // Rotation
  133. o.scale = 1.0 / scale; // Apply scale
  134. o.origin = -((o.rotation * o.scale) * origin); // Translation
  135. return o;
  136. }
  137. void invert()
  138. {
  139. *this = getInverse();
  140. }
  141. void transform(const TTransform& b)
  142. {
  143. origin = b.origin.getTransformed(origin, rotation, scale);
  144. rotation = rotation * b.rotation;
  145. scale *= b.scale;
  146. }
  147. std::string toString() const
  148. {
  149. return origin.toString() + " " + rotation.toString() + " " +
  150. std::to_string(scale);
  151. }
  152. /// @}
  153. private:
  154. /// @name Data
  155. /// @{
  156. TVec3<T> origin; ///< The rotation
  157. TMat3<T> rotation; ///< The translation
  158. T scale; ///< The uniform scaling
  159. /// @}
  160. };
  161. /// F32 transformation
  162. typedef TTransform<F32> Transform;
  163. /// @}
  164. } // end namespace anki
  165. #endif