transform_2d.h 9.1 KB

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