transform_2d.h 9.1 KB

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