transform_2d.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*************************************************************************/
  2. /* transform_2d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "core/pool_vector.h"
  34. struct Transform2D {
  35. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  36. // M = (elements[0][0] elements[1][0])
  37. // (elements[0][1] elements[1][1])
  38. // 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].
  39. // 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.
  40. // This requires additional care when working with explicit indices.
  41. // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
  42. // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  43. // and angle is measure from +X to +Y in a clockwise-fashion.
  44. Vector2 elements[3];
  45. _FORCE_INLINE_ real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  46. _FORCE_INLINE_ real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  47. const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
  48. Vector2 &operator[](int p_idx) { return elements[p_idx]; }
  49. _FORCE_INLINE_ Vector2 get_axis(int p_axis) const {
  50. ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
  51. return elements[p_axis];
  52. }
  53. _FORCE_INLINE_ void set_axis(int p_axis, const Vector2 &p_vec) {
  54. ERR_FAIL_INDEX(p_axis, 3);
  55. elements[p_axis] = p_vec;
  56. }
  57. void invert();
  58. Transform2D inverse() const;
  59. void affine_invert();
  60. Transform2D affine_inverse() const;
  61. void set_rotation(real_t p_rot);
  62. real_t get_rotation() const;
  63. _FORCE_INLINE_ void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
  64. void rotate(real_t p_phi);
  65. void scale(const Size2 &p_scale);
  66. void scale_basis(const Size2 &p_scale);
  67. void translate(real_t p_tx, real_t p_ty);
  68. void translate(const Vector2 &p_translation);
  69. real_t basis_determinant() const;
  70. Size2 get_scale() const;
  71. void set_scale(const Size2 &p_scale);
  72. _FORCE_INLINE_ const Vector2 &get_origin() const { return elements[2]; }
  73. _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
  74. Transform2D scaled(const Size2 &p_scale) const;
  75. Transform2D basis_scaled(const Size2 &p_scale) const;
  76. Transform2D translated(const Vector2 &p_offset) const;
  77. Transform2D rotated(real_t p_phi) const;
  78. Transform2D untranslated() const;
  79. void orthonormalize();
  80. Transform2D orthonormalized() const;
  81. bool is_equal_approx(const Transform2D &p_transform) const;
  82. bool operator==(const Transform2D &p_transform) const;
  83. bool operator!=(const Transform2D &p_transform) const;
  84. void operator*=(const Transform2D &p_transform);
  85. Transform2D operator*(const Transform2D &p_transform) const;
  86. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  87. _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
  88. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  89. _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
  90. _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
  91. _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const;
  92. _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const;
  93. _FORCE_INLINE_ PoolVector<Vector2> xform(const PoolVector<Vector2> &p_array) const;
  94. _FORCE_INLINE_ PoolVector<Vector2> xform_inv(const PoolVector<Vector2> &p_array) const;
  95. operator String() const;
  96. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
  97. elements[0][0] = xx;
  98. elements[0][1] = xy;
  99. elements[1][0] = yx;
  100. elements[1][1] = yy;
  101. elements[2][0] = ox;
  102. elements[2][1] = oy;
  103. }
  104. Transform2D(real_t p_rot, const Vector2 &p_pos);
  105. Transform2D() {
  106. elements[0][0] = 1.0;
  107. elements[1][1] = 1.0;
  108. }
  109. };
  110. Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
  111. return Vector2(
  112. tdotx(p_vec),
  113. tdoty(p_vec));
  114. }
  115. Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
  116. return Vector2(
  117. elements[0].dot(p_vec),
  118. elements[1].dot(p_vec));
  119. }
  120. Vector2 Transform2D::xform(const Vector2 &p_vec) const {
  121. return Vector2(
  122. tdotx(p_vec),
  123. tdoty(p_vec)) +
  124. elements[2];
  125. }
  126. Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
  127. Vector2 v = p_vec - elements[2];
  128. return Vector2(
  129. elements[0].dot(v),
  130. elements[1].dot(v));
  131. }
  132. Rect2 Transform2D::xform(const Rect2 &p_rect) const {
  133. Vector2 x = elements[0] * p_rect.size.x;
  134. Vector2 y = elements[1] * p_rect.size.y;
  135. Vector2 pos = xform(p_rect.position);
  136. Rect2 new_rect;
  137. new_rect.position = pos;
  138. new_rect.expand_to(pos + x);
  139. new_rect.expand_to(pos + y);
  140. new_rect.expand_to(pos + x + y);
  141. return new_rect;
  142. }
  143. void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
  144. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  145. elements[1][1] = Math::cos(p_rot) * p_scale.y;
  146. elements[1][0] = -Math::sin(p_rot) * p_scale.y;
  147. elements[0][1] = Math::sin(p_rot) * p_scale.x;
  148. }
  149. Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
  150. Vector2 ends[4] = {
  151. xform_inv(p_rect.position),
  152. xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  153. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  154. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
  155. };
  156. Rect2 new_rect;
  157. new_rect.position = ends[0];
  158. new_rect.expand_to(ends[1]);
  159. new_rect.expand_to(ends[2]);
  160. new_rect.expand_to(ends[3]);
  161. return new_rect;
  162. }
  163. PoolVector<Vector2> Transform2D::xform(const PoolVector<Vector2> &p_array) const {
  164. PoolVector<Vector2> array;
  165. array.resize(p_array.size());
  166. PoolVector<Vector2>::Read r = p_array.read();
  167. PoolVector<Vector2>::Write w = array.write();
  168. for (int i = 0; i < p_array.size(); ++i) {
  169. w[i] = xform(r[i]);
  170. }
  171. return array;
  172. }
  173. PoolVector<Vector2> Transform2D::xform_inv(const PoolVector<Vector2> &p_array) const {
  174. PoolVector<Vector2> array;
  175. array.resize(p_array.size());
  176. PoolVector<Vector2>::Read r = p_array.read();
  177. PoolVector<Vector2>::Write w = array.write();
  178. for (int i = 0; i < p_array.size(); ++i) {
  179. w[i] = xform_inv(r[i]);
  180. }
  181. return array;
  182. }
  183. #endif // TRANSFORM_2D_H