transform2d.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*************************************************************************/
  2. /* transform2d.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 GODOT_TRANSFORM2D_HPP
  31. #define GODOT_TRANSFORM2D_HPP
  32. #include <godot_cpp/core/error_macros.hpp>
  33. #include <godot_cpp/core/math.hpp>
  34. #include <godot_cpp/variant/packed_vector2_array.hpp>
  35. #include <godot_cpp/variant/rect2.hpp>
  36. #include <godot_cpp/variant/vector2.hpp>
  37. namespace godot {
  38. class Transform2D {
  39. public:
  40. _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
  41. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  42. // M = (elements[0][0] elements[1][0])
  43. // (elements[0][1] elements[1][1])
  44. // 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 elements[i].
  45. // Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to elements[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 #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  49. // and angle is measure from +X to +Y in a clockwise-fashion.
  50. Vector2 elements[3];
  51. inline real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  52. inline real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  53. const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
  54. Vector2 &operator[](int p_idx) { return elements[p_idx]; }
  55. inline Vector2 get_axis(int p_axis) const {
  56. ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
  57. return elements[p_axis];
  58. }
  59. inline void set_axis(int p_axis, const Vector2 &p_vec) {
  60. ERR_FAIL_INDEX(p_axis, 3);
  61. elements[p_axis] = p_vec;
  62. }
  63. void invert();
  64. Transform2D inverse() const;
  65. void affine_invert();
  66. Transform2D affine_inverse() const;
  67. void set_rotation(real_t p_rot);
  68. real_t get_rotation() const;
  69. real_t get_skew() const;
  70. void set_skew(float p_angle);
  71. inline void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
  72. inline void set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, float p_skew);
  73. void rotate(real_t p_phi);
  74. void scale(const Size2 &p_scale);
  75. void scale_basis(const Size2 &p_scale);
  76. void translate(real_t p_tx, real_t p_ty);
  77. void translate(const Vector2 &p_translation);
  78. real_t basis_determinant() const;
  79. Size2 get_scale() const;
  80. void set_scale(const Size2 &p_scale);
  81. inline const Vector2 &get_origin() const { return elements[2]; }
  82. inline void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
  83. Transform2D scaled(const Size2 &p_scale) const;
  84. Transform2D basis_scaled(const Size2 &p_scale) const;
  85. Transform2D translated(const Vector2 &p_offset) const;
  86. Transform2D rotated(real_t p_phi) const;
  87. Transform2D untranslated() const;
  88. void orthonormalize();
  89. Transform2D orthonormalized() const;
  90. bool is_equal_approx(const Transform2D &p_transform) const;
  91. bool operator==(const Transform2D &p_transform) const;
  92. bool operator!=(const Transform2D &p_transform) const;
  93. void operator*=(const Transform2D &p_transform);
  94. Transform2D operator*(const Transform2D &p_transform) const;
  95. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  96. inline Vector2 basis_xform(const Vector2 &p_vec) const;
  97. inline Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  98. inline Vector2 xform(const Vector2 &p_vec) const;
  99. inline Vector2 xform_inv(const Vector2 &p_vec) const;
  100. inline Rect2 xform(const Rect2 &p_rect) const;
  101. inline Rect2 xform_inv(const Rect2 &p_rect) const;
  102. inline PackedVector2Array xform(const PackedVector2Array &p_array) const;
  103. inline PackedVector2Array xform_inv(const PackedVector2Array &p_array) const;
  104. operator String() const;
  105. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
  106. elements[0][0] = xx;
  107. elements[0][1] = xy;
  108. elements[1][0] = yx;
  109. elements[1][1] = yy;
  110. elements[2][0] = ox;
  111. elements[2][1] = oy;
  112. }
  113. Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) {
  114. elements[0] = p_x;
  115. elements[1] = p_y;
  116. elements[2] = p_origin;
  117. }
  118. Transform2D(real_t p_rot, const Vector2 &p_pos);
  119. Transform2D() {
  120. elements[0][0] = 1.0;
  121. elements[1][1] = 1.0;
  122. }
  123. };
  124. Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
  125. return Vector2(
  126. tdotx(p_vec),
  127. tdoty(p_vec));
  128. }
  129. Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
  130. return Vector2(
  131. elements[0].dot(p_vec),
  132. elements[1].dot(p_vec));
  133. }
  134. Vector2 Transform2D::xform(const Vector2 &p_vec) const {
  135. return Vector2(
  136. tdotx(p_vec),
  137. tdoty(p_vec)) +
  138. elements[2];
  139. }
  140. Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
  141. Vector2 v = p_vec - elements[2];
  142. return Vector2(
  143. elements[0].dot(v),
  144. elements[1].dot(v));
  145. }
  146. Rect2 Transform2D::xform(const Rect2 &p_rect) const {
  147. Vector2 x = elements[0] * p_rect.size.x;
  148. Vector2 y = elements[1] * p_rect.size.y;
  149. Vector2 pos = xform(p_rect.position);
  150. Rect2 new_rect;
  151. new_rect.position = pos;
  152. new_rect.expand_to(pos + x);
  153. new_rect.expand_to(pos + y);
  154. new_rect.expand_to(pos + x + y);
  155. return new_rect;
  156. }
  157. void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
  158. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  159. elements[1][1] = Math::cos(p_rot) * p_scale.y;
  160. elements[1][0] = -Math::sin(p_rot) * p_scale.y;
  161. elements[0][1] = Math::sin(p_rot) * p_scale.x;
  162. }
  163. void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, float p_skew) {
  164. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  165. elements[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
  166. elements[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
  167. elements[0][1] = Math::sin(p_rot) * p_scale.x;
  168. }
  169. Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
  170. Vector2 ends[4] = {
  171. xform_inv(p_rect.position),
  172. xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  173. xform_inv(Vector2(p_rect.position.x + p_rect.size.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))
  175. };
  176. Rect2 new_rect;
  177. new_rect.position = ends[0];
  178. new_rect.expand_to(ends[1]);
  179. new_rect.expand_to(ends[2]);
  180. new_rect.expand_to(ends[3]);
  181. return new_rect;
  182. }
  183. PackedVector2Array Transform2D::xform(const PackedVector2Array &p_array) const {
  184. PackedVector2Array array;
  185. array.resize(p_array.size());
  186. for (int i = 0; i < p_array.size(); ++i) {
  187. array[i] = xform(p_array[i]);
  188. }
  189. return array;
  190. }
  191. PackedVector2Array Transform2D::xform_inv(const PackedVector2Array &p_array) const {
  192. PackedVector2Array array;
  193. array.resize(p_array.size());
  194. for (int i = 0; i < p_array.size(); ++i) {
  195. array[i] = xform_inv(p_array[i]);
  196. }
  197. return array;
  198. }
  199. } // namespace godot
  200. #endif // GODOT_TRANSFORM2D_HPP