test_transform_2d.h 10 KB

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