transform_2d.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*************************************************************************/
  2. /* transform_2d.cpp */
  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. #include "transform_2d.h"
  31. void Transform2D::invert() {
  32. // FIXME: this function assumes the basis is a rotation matrix, with no scaling.
  33. // Transform2D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
  34. SWAP(elements[0][1], elements[1][0]);
  35. elements[2] = basis_xform(-elements[2]);
  36. }
  37. Transform2D Transform2D::inverse() const {
  38. Transform2D inv = *this;
  39. inv.invert();
  40. return inv;
  41. }
  42. void Transform2D::affine_invert() {
  43. real_t det = basis_determinant();
  44. #ifdef MATH_CHECKS
  45. ERR_FAIL_COND(det == 0);
  46. #endif
  47. real_t idet = 1.0 / det;
  48. SWAP(elements[0][0], elements[1][1]);
  49. elements[0] *= Vector2(idet, -idet);
  50. elements[1] *= Vector2(-idet, idet);
  51. elements[2] = basis_xform(-elements[2]);
  52. }
  53. Transform2D Transform2D::affine_inverse() const {
  54. Transform2D inv = *this;
  55. inv.affine_invert();
  56. return inv;
  57. }
  58. void Transform2D::rotate(const real_t p_phi) {
  59. *this = Transform2D(p_phi, Vector2()) * (*this);
  60. }
  61. real_t Transform2D::get_skew() const {
  62. real_t det = basis_determinant();
  63. return Math::acos(elements[0].normalized().dot(SGN(det) * elements[1].normalized())) - Math_PI * 0.5;
  64. }
  65. void Transform2D::set_skew(const real_t p_angle) {
  66. real_t det = basis_determinant();
  67. elements[1] = SGN(det) * elements[0].rotated((Math_PI * 0.5 + p_angle)).normalized() * elements[1].length();
  68. }
  69. real_t Transform2D::get_rotation() const {
  70. return Math::atan2(elements[0].y, elements[0].x);
  71. }
  72. void Transform2D::set_rotation(const real_t p_rot) {
  73. Size2 scale = get_scale();
  74. real_t cr = Math::cos(p_rot);
  75. real_t sr = Math::sin(p_rot);
  76. elements[0][0] = cr;
  77. elements[0][1] = sr;
  78. elements[1][0] = -sr;
  79. elements[1][1] = cr;
  80. set_scale(scale);
  81. }
  82. Transform2D::Transform2D(const real_t p_rot, const Vector2 &p_pos) {
  83. real_t cr = Math::cos(p_rot);
  84. real_t sr = Math::sin(p_rot);
  85. elements[0][0] = cr;
  86. elements[0][1] = sr;
  87. elements[1][0] = -sr;
  88. elements[1][1] = cr;
  89. elements[2] = p_pos;
  90. }
  91. Transform2D::Transform2D(const real_t p_rot, const Size2 &p_scale, const real_t p_skew, const Vector2 &p_pos) {
  92. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  93. elements[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
  94. elements[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
  95. elements[0][1] = Math::sin(p_rot) * p_scale.x;
  96. elements[2] = p_pos;
  97. }
  98. Size2 Transform2D::get_scale() const {
  99. real_t det_sign = SGN(basis_determinant());
  100. return Size2(elements[0].length(), det_sign * elements[1].length());
  101. }
  102. void Transform2D::set_scale(const Size2 &p_scale) {
  103. elements[0].normalize();
  104. elements[1].normalize();
  105. elements[0] *= p_scale.x;
  106. elements[1] *= p_scale.y;
  107. }
  108. void Transform2D::scale(const Size2 &p_scale) {
  109. scale_basis(p_scale);
  110. elements[2] *= p_scale;
  111. }
  112. void Transform2D::scale_basis(const Size2 &p_scale) {
  113. elements[0][0] *= p_scale.x;
  114. elements[0][1] *= p_scale.y;
  115. elements[1][0] *= p_scale.x;
  116. elements[1][1] *= p_scale.y;
  117. }
  118. void Transform2D::translate(const real_t p_tx, const real_t p_ty) {
  119. translate(Vector2(p_tx, p_ty));
  120. }
  121. void Transform2D::translate(const Vector2 &p_translation) {
  122. elements[2] += basis_xform(p_translation);
  123. }
  124. void Transform2D::orthonormalize() {
  125. // Gram-Schmidt Process
  126. Vector2 x = elements[0];
  127. Vector2 y = elements[1];
  128. x.normalize();
  129. y = (y - x * (x.dot(y)));
  130. y.normalize();
  131. elements[0] = x;
  132. elements[1] = y;
  133. }
  134. Transform2D Transform2D::orthonormalized() const {
  135. Transform2D on = *this;
  136. on.orthonormalize();
  137. return on;
  138. }
  139. bool Transform2D::is_equal_approx(const Transform2D &p_transform) const {
  140. return elements[0].is_equal_approx(p_transform.elements[0]) && elements[1].is_equal_approx(p_transform.elements[1]) && elements[2].is_equal_approx(p_transform.elements[2]);
  141. }
  142. Transform2D Transform2D::looking_at(const Vector2 &p_target) const {
  143. Transform2D return_trans = Transform2D(get_rotation(), get_origin());
  144. Vector2 target_position = affine_inverse().xform(p_target);
  145. return_trans.set_rotation(return_trans.get_rotation() + (target_position * get_scale()).angle());
  146. return return_trans;
  147. }
  148. bool Transform2D::operator==(const Transform2D &p_transform) const {
  149. for (int i = 0; i < 3; i++) {
  150. if (elements[i] != p_transform.elements[i]) {
  151. return false;
  152. }
  153. }
  154. return true;
  155. }
  156. bool Transform2D::operator!=(const Transform2D &p_transform) const {
  157. for (int i = 0; i < 3; i++) {
  158. if (elements[i] != p_transform.elements[i]) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. void Transform2D::operator*=(const Transform2D &p_transform) {
  165. elements[2] = xform(p_transform.elements[2]);
  166. real_t x0, x1, y0, y1;
  167. x0 = tdotx(p_transform.elements[0]);
  168. x1 = tdoty(p_transform.elements[0]);
  169. y0 = tdotx(p_transform.elements[1]);
  170. y1 = tdoty(p_transform.elements[1]);
  171. elements[0][0] = x0;
  172. elements[0][1] = x1;
  173. elements[1][0] = y0;
  174. elements[1][1] = y1;
  175. }
  176. Transform2D Transform2D::operator*(const Transform2D &p_transform) const {
  177. Transform2D t = *this;
  178. t *= p_transform;
  179. return t;
  180. }
  181. Transform2D Transform2D::scaled(const Size2 &p_scale) const {
  182. Transform2D copy = *this;
  183. copy.scale(p_scale);
  184. return copy;
  185. }
  186. Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const {
  187. Transform2D copy = *this;
  188. copy.scale_basis(p_scale);
  189. return copy;
  190. }
  191. Transform2D Transform2D::untranslated() const {
  192. Transform2D copy = *this;
  193. copy.elements[2] = Vector2();
  194. return copy;
  195. }
  196. Transform2D Transform2D::translated(const Vector2 &p_offset) const {
  197. Transform2D copy = *this;
  198. copy.translate(p_offset);
  199. return copy;
  200. }
  201. Transform2D Transform2D::rotated(const real_t p_phi) const {
  202. Transform2D copy = *this;
  203. copy.rotate(p_phi);
  204. return copy;
  205. }
  206. real_t Transform2D::basis_determinant() const {
  207. return elements[0].x * elements[1].y - elements[0].y * elements[1].x;
  208. }
  209. Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, const real_t p_c) const {
  210. //extract parameters
  211. Vector2 p1 = get_origin();
  212. Vector2 p2 = p_transform.get_origin();
  213. real_t r1 = get_rotation();
  214. real_t r2 = p_transform.get_rotation();
  215. Size2 s1 = get_scale();
  216. Size2 s2 = p_transform.get_scale();
  217. //slerp rotation
  218. Vector2 v1(Math::cos(r1), Math::sin(r1));
  219. Vector2 v2(Math::cos(r2), Math::sin(r2));
  220. real_t dot = v1.dot(v2);
  221. dot = CLAMP(dot, -1.0, 1.0);
  222. Vector2 v;
  223. if (dot > 0.9995) {
  224. v = v1.lerp(v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
  225. } else {
  226. real_t angle = p_c * Math::acos(dot);
  227. Vector2 v3 = (v2 - v1 * dot).normalized();
  228. v = v1 * Math::cos(angle) + v3 * Math::sin(angle);
  229. }
  230. //construct matrix
  231. Transform2D res(v.angle(), p1.lerp(p2, p_c));
  232. res.scale_basis(s1.lerp(s2, p_c));
  233. return res;
  234. }
  235. void Transform2D::operator*=(const real_t p_val) {
  236. elements[0] *= p_val;
  237. elements[1] *= p_val;
  238. elements[2] *= p_val;
  239. }
  240. Transform2D Transform2D::operator*(const real_t p_val) const {
  241. Transform2D ret(*this);
  242. ret *= p_val;
  243. return ret;
  244. }
  245. Transform2D::operator String() const {
  246. return "[X: " + elements[0].operator String() +
  247. ", Y: " + elements[1].operator String() +
  248. ", O: " + elements[2].operator String() + "]";
  249. }