transform2d.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**************************************************************************/
  2. /* transform2d.hpp */
  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 <godot_cpp/variant/packed_vector2_array.hpp>
  32. #include <godot_cpp/variant/rect2.hpp>
  33. #include <godot_cpp/variant/vector2.hpp>
  34. namespace godot {
  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. _FORCE_INLINE_ real_t tdotx(const Vector2 &p_v) const { return columns[0][0] * p_v.x + columns[1][0] * p_v.y; }
  51. _FORCE_INLINE_ real_t tdoty(const Vector2 &p_v) const { return columns[0][1] * p_v.x + columns[1][1] * p_v.y; }
  52. const Vector2 &operator[](int p_idx) const { return columns[p_idx]; }
  53. Vector2 &operator[](int p_idx) { return columns[p_idx]; }
  54. void invert();
  55. Transform2D inverse() const;
  56. void affine_invert();
  57. Transform2D affine_inverse() const;
  58. void set_rotation(real_t p_rot);
  59. real_t get_rotation() const;
  60. real_t get_skew() const;
  61. void set_skew(real_t p_angle);
  62. _FORCE_INLINE_ void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
  63. _FORCE_INLINE_ void set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew);
  64. void rotate(real_t p_angle);
  65. void scale(const Size2 &p_scale);
  66. void scale_basis(const Size2 &p_scale);
  67. void translate_local(real_t p_tx, real_t p_ty);
  68. void translate_local(const Vector2 &p_translation);
  69. real_t determinant() const;
  70. Size2 get_scale() const;
  71. void set_scale(const Size2 &p_scale);
  72. _FORCE_INLINE_ const Vector2 &get_origin() const { return columns[2]; }
  73. _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { columns[2] = p_origin; }
  74. Transform2D scaled(const Size2 &p_scale) const;
  75. Transform2D scaled_local(const Size2 &p_scale) const;
  76. Transform2D translated(const Vector2 &p_offset) const;
  77. Transform2D translated_local(const Vector2 &p_offset) const;
  78. Transform2D rotated(real_t p_angle) const;
  79. Transform2D rotated_local(real_t p_angle) const;
  80. Transform2D untranslated() const;
  81. void orthonormalize();
  82. Transform2D orthonormalized() const;
  83. bool is_conformal() const;
  84. bool is_equal_approx(const Transform2D &p_transform) const;
  85. bool is_finite() const;
  86. Transform2D looking_at(const Vector2 &p_target) const;
  87. bool operator==(const Transform2D &p_transform) const;
  88. bool operator!=(const Transform2D &p_transform) const;
  89. void operator*=(const Transform2D &p_transform);
  90. Transform2D operator*(const Transform2D &p_transform) const;
  91. void operator*=(real_t p_val);
  92. Transform2D operator*(real_t p_val) const;
  93. void operator/=(real_t p_val);
  94. Transform2D operator/(real_t p_val) const;
  95. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  96. _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
  97. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  98. _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
  99. _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
  100. _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const;
  101. _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const;
  102. _FORCE_INLINE_ PackedVector2Array xform(const PackedVector2Array &p_array) const;
  103. _FORCE_INLINE_ PackedVector2Array xform_inv(const PackedVector2Array &p_array) const;
  104. operator String() const;
  105. 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) {
  106. columns[0][0] = p_xx;
  107. columns[0][1] = p_xy;
  108. columns[1][0] = p_yx;
  109. columns[1][1] = p_yy;
  110. columns[2][0] = p_ox;
  111. columns[2][1] = p_oy;
  112. }
  113. Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) {
  114. columns[0] = p_x;
  115. columns[1] = p_y;
  116. columns[2] = p_origin;
  117. }
  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() {
  121. columns[0][0] = 1.0;
  122. columns[1][1] = 1.0;
  123. }
  124. };
  125. Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
  126. return Vector2(
  127. tdotx(p_vec),
  128. tdoty(p_vec));
  129. }
  130. Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
  131. return Vector2(
  132. columns[0].dot(p_vec),
  133. columns[1].dot(p_vec));
  134. }
  135. Vector2 Transform2D::xform(const Vector2 &p_vec) const {
  136. return Vector2(
  137. tdotx(p_vec),
  138. tdoty(p_vec)) +
  139. columns[2];
  140. }
  141. Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
  142. Vector2 v = p_vec - columns[2];
  143. return Vector2(
  144. columns[0].dot(v),
  145. columns[1].dot(v));
  146. }
  147. Rect2 Transform2D::xform(const Rect2 &p_rect) const {
  148. Vector2 x = columns[0] * p_rect.size.x;
  149. Vector2 y = columns[1] * p_rect.size.y;
  150. Vector2 pos = xform(p_rect.position);
  151. Rect2 new_rect;
  152. new_rect.position = pos;
  153. new_rect.expand_to(pos + x);
  154. new_rect.expand_to(pos + y);
  155. new_rect.expand_to(pos + x + y);
  156. return new_rect;
  157. }
  158. void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
  159. columns[0][0] = Math::cos(p_rot) * p_scale.x;
  160. columns[1][1] = Math::cos(p_rot) * p_scale.y;
  161. columns[1][0] = -Math::sin(p_rot) * p_scale.y;
  162. columns[0][1] = Math::sin(p_rot) * p_scale.x;
  163. }
  164. void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew) {
  165. columns[0][0] = Math::cos(p_rot) * p_scale.x;
  166. columns[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
  167. columns[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
  168. columns[0][1] = Math::sin(p_rot) * p_scale.x;
  169. }
  170. Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
  171. Vector2 ends[4] = {
  172. xform_inv(p_rect.position),
  173. xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  174. xform_inv(Vector2(p_rect.position.x + p_rect.size.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))
  176. };
  177. Rect2 new_rect;
  178. new_rect.position = ends[0];
  179. new_rect.expand_to(ends[1]);
  180. new_rect.expand_to(ends[2]);
  181. new_rect.expand_to(ends[3]);
  182. return new_rect;
  183. }
  184. PackedVector2Array Transform2D::xform(const PackedVector2Array &p_array) const {
  185. PackedVector2Array array;
  186. array.resize(p_array.size());
  187. const Vector2 *r = p_array.ptr();
  188. Vector2 *w = array.ptrw();
  189. for (int i = 0; i < p_array.size(); ++i) {
  190. w[i] = xform(r[i]);
  191. }
  192. return array;
  193. }
  194. PackedVector2Array Transform2D::xform_inv(const PackedVector2Array &p_array) const {
  195. PackedVector2Array array;
  196. array.resize(p_array.size());
  197. const Vector2 *r = p_array.ptr();
  198. Vector2 *w = array.ptrw();
  199. for (int i = 0; i < p_array.size(); ++i) {
  200. w[i] = xform_inv(r[i]);
  201. }
  202. return array;
  203. }
  204. } // namespace godot