transform_2d.cpp 7.5 KB

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