transform_2d.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*************************************************************************/
  2. /* transform_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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(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(float 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. real_t det = basis_determinant();
  71. Transform2D m = orthonormalized();
  72. if (det < 0) {
  73. m.scale_basis(Size2(1, -1)); // convention to separate rotation and reflection for 2D is to absorb a flip along y into scaling.
  74. }
  75. return Math::atan2(m[0].y, m[0].x);
  76. }
  77. void Transform2D::set_rotation(real_t p_rot) {
  78. Size2 scale = get_scale();
  79. real_t cr = Math::cos(p_rot);
  80. real_t sr = Math::sin(p_rot);
  81. elements[0][0] = cr;
  82. elements[0][1] = sr;
  83. elements[1][0] = -sr;
  84. elements[1][1] = cr;
  85. set_scale(scale);
  86. }
  87. Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) {
  88. real_t cr = Math::cos(p_rot);
  89. real_t sr = Math::sin(p_rot);
  90. elements[0][0] = cr;
  91. elements[0][1] = sr;
  92. elements[1][0] = -sr;
  93. elements[1][1] = cr;
  94. elements[2] = p_pos;
  95. }
  96. Size2 Transform2D::get_scale() const {
  97. real_t det_sign = SGN(basis_determinant());
  98. return Size2(elements[0].length(), det_sign * elements[1].length());
  99. }
  100. void Transform2D::set_scale(const Size2 &p_scale) {
  101. elements[0].normalize();
  102. elements[1].normalize();
  103. elements[0] *= p_scale.x;
  104. elements[1] *= p_scale.y;
  105. }
  106. void Transform2D::scale(const Size2 &p_scale) {
  107. scale_basis(p_scale);
  108. elements[2] *= p_scale;
  109. }
  110. void Transform2D::scale_basis(const Size2 &p_scale) {
  111. elements[0][0] *= p_scale.x;
  112. elements[0][1] *= p_scale.y;
  113. elements[1][0] *= p_scale.x;
  114. elements[1][1] *= p_scale.y;
  115. }
  116. void Transform2D::translate(real_t p_tx, real_t p_ty) {
  117. translate(Vector2(p_tx, p_ty));
  118. }
  119. void Transform2D::translate(const Vector2 &p_translation) {
  120. elements[2] += basis_xform(p_translation);
  121. }
  122. void Transform2D::orthonormalize() {
  123. // Gram-Schmidt Process
  124. Vector2 x = elements[0];
  125. Vector2 y = elements[1];
  126. x.normalize();
  127. y = (y - x * (x.dot(y)));
  128. y.normalize();
  129. elements[0] = x;
  130. elements[1] = y;
  131. }
  132. Transform2D Transform2D::orthonormalized() const {
  133. Transform2D on = *this;
  134. on.orthonormalize();
  135. return on;
  136. }
  137. bool Transform2D::is_equal_approx(const Transform2D &p_transform) const {
  138. 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]);
  139. }
  140. bool Transform2D::operator==(const Transform2D &p_transform) const {
  141. for (int i = 0; i < 3; i++) {
  142. if (elements[i] != p_transform.elements[i])
  143. return false;
  144. }
  145. return true;
  146. }
  147. bool Transform2D::operator!=(const Transform2D &p_transform) const {
  148. for (int i = 0; i < 3; i++) {
  149. if (elements[i] != p_transform.elements[i])
  150. return true;
  151. }
  152. return false;
  153. }
  154. void Transform2D::operator*=(const Transform2D &p_transform) {
  155. elements[2] = xform(p_transform.elements[2]);
  156. real_t x0, x1, y0, y1;
  157. x0 = tdotx(p_transform.elements[0]);
  158. x1 = tdoty(p_transform.elements[0]);
  159. y0 = tdotx(p_transform.elements[1]);
  160. y1 = tdoty(p_transform.elements[1]);
  161. elements[0][0] = x0;
  162. elements[0][1] = x1;
  163. elements[1][0] = y0;
  164. elements[1][1] = y1;
  165. }
  166. Transform2D Transform2D::operator*(const Transform2D &p_transform) const {
  167. Transform2D t = *this;
  168. t *= p_transform;
  169. return t;
  170. }
  171. Transform2D Transform2D::scaled(const Size2 &p_scale) const {
  172. Transform2D copy = *this;
  173. copy.scale(p_scale);
  174. return copy;
  175. }
  176. Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const {
  177. Transform2D copy = *this;
  178. copy.scale_basis(p_scale);
  179. return copy;
  180. }
  181. Transform2D Transform2D::untranslated() const {
  182. Transform2D copy = *this;
  183. copy.elements[2] = Vector2();
  184. return copy;
  185. }
  186. Transform2D Transform2D::translated(const Vector2 &p_offset) const {
  187. Transform2D copy = *this;
  188. copy.translate(p_offset);
  189. return copy;
  190. }
  191. Transform2D Transform2D::rotated(real_t p_phi) const {
  192. Transform2D copy = *this;
  193. copy.rotate(p_phi);
  194. return copy;
  195. }
  196. real_t Transform2D::basis_determinant() const {
  197. return elements[0].x * elements[1].y - elements[0].y * elements[1].x;
  198. }
  199. Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t p_c) const {
  200. //extract parameters
  201. Vector2 p1 = get_origin();
  202. Vector2 p2 = p_transform.get_origin();
  203. real_t r1 = get_rotation();
  204. real_t r2 = p_transform.get_rotation();
  205. Size2 s1 = get_scale();
  206. Size2 s2 = p_transform.get_scale();
  207. //slerp rotation
  208. Vector2 v1(Math::cos(r1), Math::sin(r1));
  209. Vector2 v2(Math::cos(r2), Math::sin(r2));
  210. real_t dot = v1.dot(v2);
  211. dot = (dot < -1.0) ? -1.0 : ((dot > 1.0) ? 1.0 : dot); //clamp dot to [-1,1]
  212. Vector2 v;
  213. if (dot > 0.9995) {
  214. v = v1.lerp(v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
  215. } else {
  216. real_t angle = p_c * Math::acos(dot);
  217. Vector2 v3 = (v2 - v1 * dot).normalized();
  218. v = v1 * Math::cos(angle) + v3 * Math::sin(angle);
  219. }
  220. //construct matrix
  221. Transform2D res(Math::atan2(v.y, v.x), p1.lerp(p2, p_c));
  222. res.scale_basis(s1.lerp(s2, p_c));
  223. return res;
  224. }
  225. Transform2D::operator String() const {
  226. return String(String() + elements[0] + ", " + elements[1] + ", " + elements[2]);
  227. }