transform_2d.h 10 KB

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