test_transform_2d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**************************************************************************/
  2. /* test_transform_2d.h */
  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. #ifndef TEST_TRANSFORM_2D_H
  31. #define TEST_TRANSFORM_2D_H
  32. #include "core/math/transform_2d.h"
  33. #include "tests/test_macros.h"
  34. namespace TestTransform2D {
  35. Transform2D create_dummy_transform() {
  36. return Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(5, 6));
  37. }
  38. Transform2D identity() {
  39. return Transform2D();
  40. }
  41. TEST_CASE("[Transform2D] Default constructor") {
  42. Transform2D default_constructor = Transform2D();
  43. CHECK(default_constructor == Transform2D(Vector2(1, 0), Vector2(0, 1), Vector2(0, 0)));
  44. }
  45. TEST_CASE("[Transform2D] Copy constructor") {
  46. Transform2D T = create_dummy_transform();
  47. Transform2D copy_constructor = Transform2D(T);
  48. CHECK(T == copy_constructor);
  49. }
  50. TEST_CASE("[Transform2D] Constructor from angle and position") {
  51. constexpr float ROTATION = Math_PI / 4;
  52. const Vector2 TRANSLATION = Vector2(20, -20);
  53. const Transform2D test = Transform2D(ROTATION, TRANSLATION);
  54. const Transform2D expected = Transform2D().rotated(ROTATION).translated(TRANSLATION);
  55. CHECK(test == expected);
  56. }
  57. TEST_CASE("[Transform2D] Constructor from angle, scale, skew and position") {
  58. constexpr float ROTATION = Math_PI / 2;
  59. const Vector2 SCALE = Vector2(2, 0.5);
  60. constexpr float SKEW = Math_PI / 4;
  61. const Vector2 TRANSLATION = Vector2(30, 0);
  62. const Transform2D test = Transform2D(ROTATION, SCALE, SKEW, TRANSLATION);
  63. Transform2D expected = Transform2D().scaled(SCALE).rotated(ROTATION).translated(TRANSLATION);
  64. expected.set_skew(SKEW);
  65. CHECK(test.is_equal_approx(expected));
  66. }
  67. TEST_CASE("[Transform2D] Constructor from raw values") {
  68. const Transform2D test = Transform2D(1, 2, 3, 4, 5, 6);
  69. const Transform2D expected = Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(5, 6));
  70. CHECK(test == expected);
  71. }
  72. TEST_CASE("[Transform2D] xform") {
  73. const Vector2 v = Vector2(2, 3);
  74. const Transform2D T = Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(5, 6));
  75. const Vector2 expected = Vector2(1 * 2 + 3 * 3 + 5 * 1, 2 * 2 + 4 * 3 + 6 * 1);
  76. CHECK(T.xform(v) == expected);
  77. }
  78. TEST_CASE("[Transform2D] Basis xform") {
  79. const Vector2 v = Vector2(2, 2);
  80. const Transform2D T1 = Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(0, 0));
  81. // Both versions should be the same when the origin is (0,0).
  82. CHECK(T1.basis_xform(v) == T1.xform(v));
  83. const Transform2D T2 = Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(5, 6));
  84. // Each version should be different when the origin is not (0,0).
  85. CHECK_FALSE(T2.basis_xform(v) == T2.xform(v));
  86. }
  87. TEST_CASE("[Transform2D] Affine inverse") {
  88. const Transform2D orig = create_dummy_transform();
  89. const Transform2D affine_inverted = orig.affine_inverse();
  90. const Transform2D affine_inverted_again = affine_inverted.affine_inverse();
  91. CHECK(affine_inverted_again == orig);
  92. }
  93. TEST_CASE("[Transform2D] Orthonormalized") {
  94. const Transform2D T = create_dummy_transform();
  95. const Transform2D orthonormalized_T = T.orthonormalized();
  96. // Check each basis has length 1.
  97. CHECK(Math::is_equal_approx(orthonormalized_T[0].length_squared(), 1));
  98. CHECK(Math::is_equal_approx(orthonormalized_T[1].length_squared(), 1));
  99. const Vector2 vx = Vector2(orthonormalized_T[0].x, orthonormalized_T[1].x);
  100. const Vector2 vy = Vector2(orthonormalized_T[0].y, orthonormalized_T[1].y);
  101. // Check the basis are orthogonal.
  102. CHECK(Math::is_equal_approx(orthonormalized_T.tdotx(vx), 1));
  103. CHECK(Math::is_equal_approx(orthonormalized_T.tdotx(vy), 0));
  104. CHECK(Math::is_equal_approx(orthonormalized_T.tdoty(vx), 0));
  105. CHECK(Math::is_equal_approx(orthonormalized_T.tdoty(vy), 1));
  106. }
  107. TEST_CASE("[Transform2D] translation") {
  108. const Vector2 offset = Vector2(1, 2);
  109. // Both versions should give the same result applied to identity.
  110. CHECK(identity().translated(offset) == identity().translated_local(offset));
  111. // Check both versions against left and right multiplications.
  112. const Transform2D orig = create_dummy_transform();
  113. const Transform2D T = identity().translated(offset);
  114. CHECK(orig.translated(offset) == T * orig);
  115. CHECK(orig.translated_local(offset) == orig * T);
  116. }
  117. TEST_CASE("[Transform2D] scaling") {
  118. const Vector2 scaling = Vector2(1, 2);
  119. // Both versions should give the same result applied to identity.
  120. CHECK(identity().scaled(scaling) == identity().scaled_local(scaling));
  121. // Check both versions against left and right multiplications.
  122. const Transform2D orig = create_dummy_transform();
  123. const Transform2D S = identity().scaled(scaling);
  124. CHECK(orig.scaled(scaling) == S * orig);
  125. CHECK(orig.scaled_local(scaling) == orig * S);
  126. }
  127. TEST_CASE("[Transform2D] rotation") {
  128. constexpr real_t phi = 1.0;
  129. // Both versions should give the same result applied to identity.
  130. CHECK(identity().rotated(phi) == identity().rotated_local(phi));
  131. // Check both versions against left and right multiplications.
  132. const Transform2D orig = create_dummy_transform();
  133. const Transform2D R = identity().rotated(phi);
  134. CHECK(orig.rotated(phi) == R * orig);
  135. CHECK(orig.rotated_local(phi) == orig * R);
  136. }
  137. TEST_CASE("[Transform2D] Interpolation") {
  138. const Transform2D rotate_scale_skew_pos = Transform2D(Math::deg_to_rad(170.0), Vector2(3.6, 8.0), Math::deg_to_rad(20.0), Vector2(2.4, 6.8));
  139. const Transform2D rotate_scale_skew_pos_halfway = Transform2D(Math::deg_to_rad(85.0), Vector2(2.3, 4.5), Math::deg_to_rad(10.0), Vector2(1.2, 3.4));
  140. Transform2D interpolated = Transform2D().interpolate_with(rotate_scale_skew_pos, 0.5);
  141. CHECK(interpolated.get_origin().is_equal_approx(rotate_scale_skew_pos_halfway.get_origin()));
  142. CHECK(interpolated.get_rotation() == doctest::Approx(rotate_scale_skew_pos_halfway.get_rotation()));
  143. CHECK(interpolated.get_scale().is_equal_approx(rotate_scale_skew_pos_halfway.get_scale()));
  144. CHECK(interpolated.get_skew() == doctest::Approx(rotate_scale_skew_pos_halfway.get_skew()));
  145. CHECK(interpolated.is_equal_approx(rotate_scale_skew_pos_halfway));
  146. interpolated = rotate_scale_skew_pos.interpolate_with(Transform2D(), 0.5);
  147. CHECK(interpolated.is_equal_approx(rotate_scale_skew_pos_halfway));
  148. }
  149. TEST_CASE("[Transform2D] Finite number checks") {
  150. const Vector2 x = Vector2(0, 1);
  151. const Vector2 infinite = Vector2(NAN, NAN);
  152. CHECK_MESSAGE(
  153. Transform2D(x, x, x).is_finite(),
  154. "Transform2D with all components finite should be finite");
  155. CHECK_FALSE_MESSAGE(
  156. Transform2D(infinite, x, x).is_finite(),
  157. "Transform2D with one component infinite should not be finite.");
  158. CHECK_FALSE_MESSAGE(
  159. Transform2D(x, infinite, x).is_finite(),
  160. "Transform2D with one component infinite should not be finite.");
  161. CHECK_FALSE_MESSAGE(
  162. Transform2D(x, x, infinite).is_finite(),
  163. "Transform2D with one component infinite should not be finite.");
  164. CHECK_FALSE_MESSAGE(
  165. Transform2D(infinite, infinite, x).is_finite(),
  166. "Transform2D with two components infinite should not be finite.");
  167. CHECK_FALSE_MESSAGE(
  168. Transform2D(infinite, x, infinite).is_finite(),
  169. "Transform2D with two components infinite should not be finite.");
  170. CHECK_FALSE_MESSAGE(
  171. Transform2D(x, infinite, infinite).is_finite(),
  172. "Transform2D with two components infinite should not be finite.");
  173. CHECK_FALSE_MESSAGE(
  174. Transform2D(infinite, infinite, infinite).is_finite(),
  175. "Transform2D with three components infinite should not be finite.");
  176. }
  177. TEST_CASE("[Transform2D] Is conformal checks") {
  178. CHECK_MESSAGE(
  179. Transform2D().is_conformal(),
  180. "Identity Transform2D should be conformal.");
  181. CHECK_MESSAGE(
  182. Transform2D(1.2, Vector2()).is_conformal(),
  183. "Transform2D with only rotation should be conformal.");
  184. CHECK_MESSAGE(
  185. Transform2D(Vector2(1, 0), Vector2(0, -1), Vector2()).is_conformal(),
  186. "Transform2D with only a flip should be conformal.");
  187. CHECK_MESSAGE(
  188. Transform2D(Vector2(1.2, 0), Vector2(0, 1.2), Vector2()).is_conformal(),
  189. "Transform2D with only uniform scale should be conformal.");
  190. CHECK_MESSAGE(
  191. Transform2D(Vector2(1.2, 3.4), Vector2(3.4, -1.2), Vector2()).is_conformal(),
  192. "Transform2D with a flip, rotation, and uniform scale should be conformal.");
  193. CHECK_FALSE_MESSAGE(
  194. Transform2D(Vector2(1.2, 0), Vector2(0, 3.4), Vector2()).is_conformal(),
  195. "Transform2D with non-uniform scale should not be conformal.");
  196. CHECK_FALSE_MESSAGE(
  197. Transform2D(Vector2(Math_SQRT12, Math_SQRT12), Vector2(0, 1), Vector2()).is_conformal(),
  198. "Transform2D with the X axis skewed 45 degrees should not be conformal.");
  199. }
  200. } // namespace TestTransform2D
  201. #endif // TEST_TRANSFORM_2D_H