transform_2d.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*************************************************************************/
  2. /* transform_2d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TRANSFORM_2D_H
  31. #define TRANSFORM_2D_H
  32. #include "core/math/rect2.h" // also includes vector2, math_funcs, and ustring
  33. struct Transform2D {
  34. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  35. // M = (elements[0][0] elements[1][0])
  36. // (elements[0][1] elements[1][1])
  37. // This is such that the columns, which can be interpreted as basis vectors of the coordinate system "painted" on the object, can be accessed as elements[i].
  38. // Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to elements[1][0] here.
  39. // This requires additional care when working with explicit indices.
  40. // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
  41. // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  42. // and angle is measure from +X to +Y in a clockwise-fashion.
  43. Vector2 elements[3];
  44. _FORCE_INLINE_ real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  45. _FORCE_INLINE_ real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  46. const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
  47. Vector2 &operator[](int p_idx) { return elements[p_idx]; }
  48. _FORCE_INLINE_ Vector2 get_axis(int p_axis) const {
  49. ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
  50. return elements[p_axis];
  51. }
  52. _FORCE_INLINE_ void set_axis(int p_axis, const Vector2 &p_vec) {
  53. ERR_FAIL_INDEX(p_axis, 3);
  54. elements[p_axis] = p_vec;
  55. }
  56. void invert();
  57. Transform2D inverse() const;
  58. void affine_invert();
  59. Transform2D affine_inverse() const;
  60. void set_rotation(real_t p_rot);
  61. real_t get_rotation() const;
  62. _FORCE_INLINE_ void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
  63. void rotate(real_t p_phi);
  64. void scale(const Size2 &p_scale);
  65. void scale_basis(const Size2 &p_scale);
  66. void translate(real_t p_tx, real_t p_ty);
  67. void translate(const Vector2 &p_translation);
  68. real_t basis_determinant() const;
  69. Size2 get_scale() const;
  70. _FORCE_INLINE_ const Vector2 &get_origin() const { return elements[2]; }
  71. _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
  72. Transform2D scaled(const Size2 &p_scale) const;
  73. Transform2D basis_scaled(const Size2 &p_scale) const;
  74. Transform2D translated(const Vector2 &p_offset) const;
  75. Transform2D rotated(real_t p_phi) const;
  76. Transform2D untranslated() const;
  77. void orthonormalize();
  78. Transform2D orthonormalized() const;
  79. bool operator==(const Transform2D &p_transform) const;
  80. bool operator!=(const Transform2D &p_transform) const;
  81. void operator*=(const Transform2D &p_transform);
  82. Transform2D operator*(const Transform2D &p_transform) const;
  83. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  84. _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
  85. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  86. _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
  87. _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
  88. _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const;
  89. _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const;
  90. operator String() const;
  91. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
  92. elements[0][0] = xx;
  93. elements[0][1] = xy;
  94. elements[1][0] = yx;
  95. elements[1][1] = yy;
  96. elements[2][0] = ox;
  97. elements[2][1] = oy;
  98. }
  99. Transform2D(real_t p_rot, const Vector2 &p_pos);
  100. Transform2D() {
  101. elements[0][0] = 1.0;
  102. elements[1][1] = 1.0;
  103. }
  104. };
  105. Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
  106. return Vector2(
  107. tdotx(p_vec),
  108. tdoty(p_vec));
  109. }
  110. Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
  111. return Vector2(
  112. elements[0].dot(p_vec),
  113. elements[1].dot(p_vec));
  114. }
  115. Vector2 Transform2D::xform(const Vector2 &p_vec) const {
  116. return Vector2(
  117. tdotx(p_vec),
  118. tdoty(p_vec)) +
  119. elements[2];
  120. }
  121. Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
  122. Vector2 v = p_vec - elements[2];
  123. return Vector2(
  124. elements[0].dot(v),
  125. elements[1].dot(v));
  126. }
  127. Rect2 Transform2D::xform(const Rect2 &p_rect) const {
  128. Vector2 x = elements[0] * p_rect.size.x;
  129. Vector2 y = elements[1] * p_rect.size.y;
  130. Vector2 pos = xform(p_rect.position);
  131. Rect2 new_rect;
  132. new_rect.position = pos;
  133. new_rect.expand_to(pos + x);
  134. new_rect.expand_to(pos + y);
  135. new_rect.expand_to(pos + x + y);
  136. return new_rect;
  137. }
  138. void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
  139. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  140. elements[1][1] = Math::cos(p_rot) * p_scale.y;
  141. elements[1][0] = -Math::sin(p_rot) * p_scale.y;
  142. elements[0][1] = Math::sin(p_rot) * p_scale.x;
  143. }
  144. Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
  145. Vector2 ends[4] = {
  146. xform_inv(p_rect.position),
  147. xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  148. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  149. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
  150. };
  151. Rect2 new_rect;
  152. new_rect.position = ends[0];
  153. new_rect.expand_to(ends[1]);
  154. new_rect.expand_to(ends[2]);
  155. new_rect.expand_to(ends[3]);
  156. return new_rect;
  157. }
  158. #endif // TRANSFORM_2D_H