transform_2d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. static const Transform2D FLIP_X;
  50. static const Transform2D FLIP_Y;
  51. Vector2 columns[3] = {
  52. { 1, 0 },
  53. { 0, 1 },
  54. { 0, 0 },
  55. };
  56. _FORCE_INLINE_ real_t tdotx(const Vector2 &p_v) const { return columns[0][0] * p_v.x + columns[1][0] * p_v.y; }
  57. _FORCE_INLINE_ real_t tdoty(const Vector2 &p_v) const { return columns[0][1] * p_v.x + columns[1][1] * p_v.y; }
  58. constexpr const Vector2 &operator[](int p_idx) const { return columns[p_idx]; }
  59. constexpr Vector2 &operator[](int p_idx) { return columns[p_idx]; }
  60. void invert();
  61. Transform2D inverse() const;
  62. void affine_invert();
  63. Transform2D affine_inverse() const;
  64. void set_rotation(real_t p_rot);
  65. real_t get_rotation() const;
  66. real_t get_skew() const;
  67. void set_skew(real_t p_angle);
  68. _FORCE_INLINE_ void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
  69. _FORCE_INLINE_ void set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew);
  70. void rotate(real_t p_angle);
  71. void scale(const Size2 &p_scale);
  72. void scale_basis(const Size2 &p_scale);
  73. void translate_local(real_t p_tx, real_t p_ty);
  74. void translate_local(const Vector2 &p_translation);
  75. real_t determinant() const;
  76. Size2 get_scale() const;
  77. void set_scale(const Size2 &p_scale);
  78. _FORCE_INLINE_ const Vector2 &get_origin() const { return columns[2]; }
  79. _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { columns[2] = p_origin; }
  80. Transform2D scaled(const Size2 &p_scale) const;
  81. Transform2D scaled_local(const Size2 &p_scale) const;
  82. Transform2D translated(const Vector2 &p_offset) const;
  83. Transform2D translated_local(const Vector2 &p_offset) const;
  84. Transform2D rotated(real_t p_angle) const;
  85. Transform2D rotated_local(real_t p_angle) const;
  86. Transform2D untranslated() const;
  87. void orthonormalize();
  88. Transform2D orthonormalized() const;
  89. bool is_conformal() const;
  90. bool is_equal_approx(const Transform2D &p_transform) const;
  91. bool is_same(const Transform2D &p_transform) const;
  92. bool is_finite() const;
  93. Transform2D looking_at(const Vector2 &p_target) const;
  94. constexpr bool operator==(const Transform2D &p_transform) const;
  95. constexpr bool operator!=(const Transform2D &p_transform) const;
  96. void operator*=(const Transform2D &p_transform);
  97. Transform2D operator*(const Transform2D &p_transform) const;
  98. constexpr void operator*=(real_t p_val);
  99. constexpr Transform2D operator*(real_t p_val) const;
  100. constexpr void operator/=(real_t p_val);
  101. constexpr Transform2D operator/(real_t p_val) const;
  102. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  103. _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
  104. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  105. _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
  106. _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
  107. _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const;
  108. _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const;
  109. _FORCE_INLINE_ Vector<Vector2> xform(const Vector<Vector2> &p_array) const;
  110. _FORCE_INLINE_ Vector<Vector2> xform_inv(const Vector<Vector2> &p_array) const;
  111. explicit operator String() const;
  112. 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) :
  113. columns{
  114. { p_xx, p_xy },
  115. { p_yx, p_yy },
  116. { p_ox, p_oy },
  117. } {}
  118. constexpr Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) :
  119. columns{ p_x, p_y, p_origin } {}
  120. Transform2D(real_t p_rot, const Vector2 &p_pos);
  121. Transform2D(real_t p_rot, const Size2 &p_scale, real_t p_skew, const Vector2 &p_pos);
  122. Transform2D() = default;
  123. };
  124. inline constexpr Transform2D Transform2D::FLIP_X = { { -1, 0 }, { 0, 1 }, { 0, 0 } };
  125. inline constexpr Transform2D Transform2D::FLIP_Y = { { 1, 0 }, { 0, -1 }, { 0, 0 } };
  126. constexpr bool Transform2D::operator==(const Transform2D &p_transform) const {
  127. for (int i = 0; i < 3; i++) {
  128. if (columns[i] != p_transform.columns[i]) {
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. constexpr bool Transform2D::operator!=(const Transform2D &p_transform) const {
  135. for (int i = 0; i < 3; i++) {
  136. if (columns[i] != p_transform.columns[i]) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. constexpr void Transform2D::operator*=(real_t p_val) {
  143. columns[0] *= p_val;
  144. columns[1] *= p_val;
  145. columns[2] *= p_val;
  146. }
  147. constexpr Transform2D Transform2D::operator*(real_t p_val) const {
  148. Transform2D ret(*this);
  149. ret *= p_val;
  150. return ret;
  151. }
  152. constexpr void Transform2D::operator/=(real_t p_val) {
  153. columns[0] /= p_val;
  154. columns[1] /= p_val;
  155. columns[2] /= p_val;
  156. }
  157. constexpr Transform2D Transform2D::operator/(real_t p_val) const {
  158. Transform2D ret(*this);
  159. ret /= p_val;
  160. return ret;
  161. }
  162. Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
  163. return Vector2(
  164. tdotx(p_vec),
  165. tdoty(p_vec));
  166. }
  167. Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
  168. return Vector2(
  169. columns[0].dot(p_vec),
  170. columns[1].dot(p_vec));
  171. }
  172. Vector2 Transform2D::xform(const Vector2 &p_vec) const {
  173. return Vector2(
  174. tdotx(p_vec),
  175. tdoty(p_vec)) +
  176. columns[2];
  177. }
  178. Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
  179. Vector2 v = p_vec - columns[2];
  180. return Vector2(
  181. columns[0].dot(v),
  182. columns[1].dot(v));
  183. }
  184. Rect2 Transform2D::xform(const Rect2 &p_rect) const {
  185. Vector2 x = columns[0] * p_rect.size.x;
  186. Vector2 y = columns[1] * p_rect.size.y;
  187. Vector2 pos = xform(p_rect.position);
  188. Rect2 new_rect;
  189. new_rect.position = pos;
  190. new_rect.expand_to(pos + x);
  191. new_rect.expand_to(pos + y);
  192. new_rect.expand_to(pos + x + y);
  193. return new_rect;
  194. }
  195. void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
  196. columns[0][0] = Math::cos(p_rot) * p_scale.x;
  197. columns[1][1] = Math::cos(p_rot) * p_scale.y;
  198. columns[1][0] = -Math::sin(p_rot) * p_scale.y;
  199. columns[0][1] = Math::sin(p_rot) * p_scale.x;
  200. }
  201. void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew) {
  202. columns[0][0] = Math::cos(p_rot) * p_scale.x;
  203. columns[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
  204. columns[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
  205. columns[0][1] = Math::sin(p_rot) * p_scale.x;
  206. }
  207. Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
  208. Vector2 ends[4] = {
  209. xform_inv(p_rect.position),
  210. xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  211. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  212. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
  213. };
  214. Rect2 new_rect;
  215. new_rect.position = ends[0];
  216. new_rect.expand_to(ends[1]);
  217. new_rect.expand_to(ends[2]);
  218. new_rect.expand_to(ends[3]);
  219. return new_rect;
  220. }
  221. Vector<Vector2> Transform2D::xform(const Vector<Vector2> &p_array) const {
  222. Vector<Vector2> array;
  223. array.resize(p_array.size());
  224. const Vector2 *r = p_array.ptr();
  225. Vector2 *w = array.ptrw();
  226. for (int i = 0; i < p_array.size(); ++i) {
  227. w[i] = xform(r[i]);
  228. }
  229. return array;
  230. }
  231. Vector<Vector2> Transform2D::xform_inv(const Vector<Vector2> &p_array) const {
  232. Vector<Vector2> array;
  233. array.resize(p_array.size());
  234. const Vector2 *r = p_array.ptr();
  235. Vector2 *w = array.ptrw();
  236. for (int i = 0; i < p_array.size(); ++i) {
  237. w[i] = xform_inv(r[i]);
  238. }
  239. return array;
  240. }