transform_2d.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_conformal() const;
  82. bool is_equal_approx(const Transform2D &p_transform) const;
  83. bool is_finite() const;
  84. Transform2D looking_at(const Vector2 &p_target) const;
  85. bool operator==(const Transform2D &p_transform) const;
  86. bool operator!=(const Transform2D &p_transform) const;
  87. void operator*=(const Transform2D &p_transform);
  88. Transform2D operator*(const Transform2D &p_transform) const;
  89. void operator*=(const real_t p_val);
  90. Transform2D operator*(const real_t p_val) const;
  91. Transform2D interpolate_with(const Transform2D &p_transform, const real_t p_c) const;
  92. _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
  93. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  94. _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
  95. _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
  96. _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const;
  97. _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const;
  98. _FORCE_INLINE_ Vector<Vector2> xform(const Vector<Vector2> &p_array) const;
  99. _FORCE_INLINE_ Vector<Vector2> xform_inv(const Vector<Vector2> &p_array) const;
  100. operator String() const;
  101. 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) {
  102. columns[0][0] = xx;
  103. columns[0][1] = xy;
  104. columns[1][0] = yx;
  105. columns[1][1] = yy;
  106. columns[2][0] = ox;
  107. columns[2][1] = oy;
  108. }
  109. Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) {
  110. columns[0] = p_x;
  111. columns[1] = p_y;
  112. columns[2] = p_origin;
  113. }
  114. Transform2D(const real_t p_rot, const Vector2 &p_pos);
  115. Transform2D(const real_t p_rot, const Size2 &p_scale, const real_t p_skew, const Vector2 &p_pos);
  116. Transform2D() {
  117. columns[0][0] = 1.0;
  118. columns[1][1] = 1.0;
  119. }
  120. };
  121. Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
  122. return Vector2(
  123. tdotx(p_vec),
  124. tdoty(p_vec));
  125. }
  126. Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
  127. return Vector2(
  128. columns[0].dot(p_vec),
  129. columns[1].dot(p_vec));
  130. }
  131. Vector2 Transform2D::xform(const Vector2 &p_vec) const {
  132. return Vector2(
  133. tdotx(p_vec),
  134. tdoty(p_vec)) +
  135. columns[2];
  136. }
  137. Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
  138. Vector2 v = p_vec - columns[2];
  139. return Vector2(
  140. columns[0].dot(v),
  141. columns[1].dot(v));
  142. }
  143. Rect2 Transform2D::xform(const Rect2 &p_rect) const {
  144. Vector2 x = columns[0] * p_rect.size.x;
  145. Vector2 y = columns[1] * p_rect.size.y;
  146. Vector2 pos = xform(p_rect.position);
  147. Rect2 new_rect;
  148. new_rect.position = pos;
  149. new_rect.expand_to(pos + x);
  150. new_rect.expand_to(pos + y);
  151. new_rect.expand_to(pos + x + y);
  152. return new_rect;
  153. }
  154. void Transform2D::set_rotation_and_scale(const real_t p_rot, const Size2 &p_scale) {
  155. columns[0][0] = Math::cos(p_rot) * p_scale.x;
  156. columns[1][1] = Math::cos(p_rot) * p_scale.y;
  157. columns[1][0] = -Math::sin(p_rot) * p_scale.y;
  158. columns[0][1] = Math::sin(p_rot) * p_scale.x;
  159. }
  160. void Transform2D::set_rotation_scale_and_skew(const real_t p_rot, const Size2 &p_scale, const real_t p_skew) {
  161. columns[0][0] = Math::cos(p_rot) * p_scale.x;
  162. columns[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
  163. columns[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
  164. columns[0][1] = Math::sin(p_rot) * p_scale.x;
  165. }
  166. Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
  167. Vector2 ends[4] = {
  168. xform_inv(p_rect.position),
  169. xform_inv(Vector2(p_rect.position.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 + p_rect.size.y)),
  171. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
  172. };
  173. Rect2 new_rect;
  174. new_rect.position = ends[0];
  175. new_rect.expand_to(ends[1]);
  176. new_rect.expand_to(ends[2]);
  177. new_rect.expand_to(ends[3]);
  178. return new_rect;
  179. }
  180. Vector<Vector2> Transform2D::xform(const Vector<Vector2> &p_array) const {
  181. Vector<Vector2> array;
  182. array.resize(p_array.size());
  183. const Vector2 *r = p_array.ptr();
  184. Vector2 *w = array.ptrw();
  185. for (int i = 0; i < p_array.size(); ++i) {
  186. w[i] = xform(r[i]);
  187. }
  188. return array;
  189. }
  190. Vector<Vector2> Transform2D::xform_inv(const Vector<Vector2> &p_array) const {
  191. Vector<Vector2> array;
  192. array.resize(p_array.size());
  193. const Vector2 *r = p_array.ptr();
  194. Vector2 *w = array.ptrw();
  195. for (int i = 0; i < p_array.size(); ++i) {
  196. w[i] = xform_inv(r[i]);
  197. }
  198. return array;
  199. }
  200. #endif // TRANSFORM_2D_H