transform_2d.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**************************************************************************/
  2. /* transform_2d.cpp */
  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. #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 / 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_angle) {
  59. *this = Transform2D(p_angle, Vector2()) * (*this);
  60. }
  61. real_t Transform2D::get_rotation() const {
  62. return Math::atan2(elements[0].y, elements[0].x);
  63. }
  64. void Transform2D::set_rotation(real_t p_rot) {
  65. Size2 scale = get_scale();
  66. real_t cr = Math::cos(p_rot);
  67. real_t sr = Math::sin(p_rot);
  68. elements[0][0] = cr;
  69. elements[0][1] = sr;
  70. elements[1][0] = -sr;
  71. elements[1][1] = cr;
  72. set_scale(scale);
  73. }
  74. Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) {
  75. real_t cr = Math::cos(p_rot);
  76. real_t sr = Math::sin(p_rot);
  77. elements[0][0] = cr;
  78. elements[0][1] = sr;
  79. elements[1][0] = -sr;
  80. elements[1][1] = cr;
  81. elements[2] = p_pos;
  82. }
  83. Size2 Transform2D::get_scale() const {
  84. real_t det_sign = SGN(basis_determinant());
  85. return Size2(elements[0].length(), det_sign * elements[1].length());
  86. }
  87. void Transform2D::set_scale(const Size2 &p_scale) {
  88. elements[0].normalize();
  89. elements[1].normalize();
  90. elements[0] *= p_scale.x;
  91. elements[1] *= p_scale.y;
  92. }
  93. void Transform2D::scale(const Size2 &p_scale) {
  94. scale_basis(p_scale);
  95. elements[2] *= p_scale;
  96. }
  97. void Transform2D::scale_basis(const Size2 &p_scale) {
  98. elements[0][0] *= p_scale.x;
  99. elements[0][1] *= p_scale.y;
  100. elements[1][0] *= p_scale.x;
  101. elements[1][1] *= p_scale.y;
  102. }
  103. void Transform2D::translate(real_t p_tx, real_t p_ty) {
  104. translate(Vector2(p_tx, p_ty));
  105. }
  106. void Transform2D::translate(const Vector2 &p_translation) {
  107. elements[2] += basis_xform(p_translation);
  108. }
  109. void Transform2D::orthonormalize() {
  110. // Gram-Schmidt Process
  111. Vector2 x = elements[0];
  112. Vector2 y = elements[1];
  113. x.normalize();
  114. y = (y - x * (x.dot(y)));
  115. y.normalize();
  116. elements[0] = x;
  117. elements[1] = y;
  118. }
  119. Transform2D Transform2D::orthonormalized() const {
  120. Transform2D on = *this;
  121. on.orthonormalize();
  122. return on;
  123. }
  124. bool Transform2D::is_equal_approx(const Transform2D &p_transform) const {
  125. 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]);
  126. }
  127. bool Transform2D::operator==(const Transform2D &p_transform) const {
  128. for (int i = 0; i < 3; i++) {
  129. if (elements[i] != p_transform.elements[i]) {
  130. return false;
  131. }
  132. }
  133. return true;
  134. }
  135. bool Transform2D::operator!=(const Transform2D &p_transform) const {
  136. for (int i = 0; i < 3; i++) {
  137. if (elements[i] != p_transform.elements[i]) {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. void Transform2D::operator*=(const Transform2D &p_transform) {
  144. elements[2] = xform(p_transform.elements[2]);
  145. real_t x0, x1, y0, y1;
  146. x0 = tdotx(p_transform.elements[0]);
  147. x1 = tdoty(p_transform.elements[0]);
  148. y0 = tdotx(p_transform.elements[1]);
  149. y1 = tdoty(p_transform.elements[1]);
  150. elements[0][0] = x0;
  151. elements[0][1] = x1;
  152. elements[1][0] = y0;
  153. elements[1][1] = y1;
  154. }
  155. Transform2D Transform2D::operator*(const Transform2D &p_transform) const {
  156. Transform2D t = *this;
  157. t *= p_transform;
  158. return t;
  159. }
  160. Transform2D Transform2D::scaled(const Size2 &p_scale) const {
  161. Transform2D copy = *this;
  162. copy.scale(p_scale);
  163. return copy;
  164. }
  165. Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const {
  166. Transform2D copy = *this;
  167. copy.scale_basis(p_scale);
  168. return copy;
  169. }
  170. Transform2D Transform2D::untranslated() const {
  171. Transform2D copy = *this;
  172. copy.elements[2] = Vector2();
  173. return copy;
  174. }
  175. Transform2D Transform2D::translated(const Vector2 &p_offset) const {
  176. Transform2D copy = *this;
  177. copy.translate(p_offset);
  178. return copy;
  179. }
  180. Transform2D Transform2D::rotated(real_t p_angle) const {
  181. Transform2D copy = *this;
  182. copy.rotate(p_angle);
  183. return copy;
  184. }
  185. real_t Transform2D::basis_determinant() const {
  186. return elements[0].x * elements[1].y - elements[0].y * elements[1].x;
  187. }
  188. Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t p_c) const {
  189. //extract parameters
  190. Vector2 p1 = get_origin();
  191. Vector2 p2 = p_transform.get_origin();
  192. real_t r1 = get_rotation();
  193. real_t r2 = p_transform.get_rotation();
  194. Size2 s1 = get_scale();
  195. Size2 s2 = p_transform.get_scale();
  196. //slerp rotation
  197. Vector2 v1(Math::cos(r1), Math::sin(r1));
  198. Vector2 v2(Math::cos(r2), Math::sin(r2));
  199. real_t dot = v1.dot(v2);
  200. dot = CLAMP(dot, -1, 1);
  201. Vector2 v;
  202. if (dot > 0.9995f) {
  203. v = Vector2::linear_interpolate(v1, v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
  204. } else {
  205. real_t angle = p_c * Math::acos(dot);
  206. Vector2 v3 = (v2 - v1 * dot).normalized();
  207. v = v1 * Math::cos(angle) + v3 * Math::sin(angle);
  208. }
  209. //construct matrix
  210. Transform2D res(Math::atan2(v.y, v.x), Vector2::linear_interpolate(p1, p2, p_c));
  211. res.scale_basis(Vector2::linear_interpolate(s1, s2, p_c));
  212. return res;
  213. }
  214. Transform2D::operator String() const {
  215. return String(String() + elements[0] + ", " + elements[1] + ", " + elements[2]);
  216. }