transform_2d.cpp 9.2 KB

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