test_transform_2d.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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] translation") {
  42. Vector2 offset = Vector2(1, 2);
  43. // Both versions should give the same result applied to identity.
  44. CHECK(identity().translated(offset) == identity().translated_local(offset));
  45. // Check both versions against left and right multiplications.
  46. Transform2D orig = create_dummy_transform();
  47. Transform2D T = identity().translated(offset);
  48. CHECK(orig.translated(offset) == T * orig);
  49. CHECK(orig.translated_local(offset) == orig * T);
  50. }
  51. TEST_CASE("[Transform2D] scaling") {
  52. Vector2 scaling = Vector2(1, 2);
  53. // Both versions should give the same result applied to identity.
  54. CHECK(identity().scaled(scaling) == identity().scaled_local(scaling));
  55. // Check both versions against left and right multiplications.
  56. Transform2D orig = create_dummy_transform();
  57. Transform2D S = identity().scaled(scaling);
  58. CHECK(orig.scaled(scaling) == S * orig);
  59. CHECK(orig.scaled_local(scaling) == orig * S);
  60. }
  61. TEST_CASE("[Transform2D] rotation") {
  62. real_t phi = 1.0;
  63. // Both versions should give the same result applied to identity.
  64. CHECK(identity().rotated(phi) == identity().rotated_local(phi));
  65. // Check both versions against left and right multiplications.
  66. Transform2D orig = create_dummy_transform();
  67. Transform2D R = identity().rotated(phi);
  68. CHECK(orig.rotated(phi) == R * orig);
  69. CHECK(orig.rotated_local(phi) == orig * R);
  70. }
  71. TEST_CASE("[Transform2D] Interpolation") {
  72. 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));
  73. 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));
  74. Transform2D interpolated = Transform2D().interpolate_with(rotate_scale_skew_pos, 0.5);
  75. CHECK(interpolated.get_origin().is_equal_approx(rotate_scale_skew_pos_halfway.get_origin()));
  76. CHECK(interpolated.get_rotation() == doctest::Approx(rotate_scale_skew_pos_halfway.get_rotation()));
  77. CHECK(interpolated.get_scale().is_equal_approx(rotate_scale_skew_pos_halfway.get_scale()));
  78. CHECK(interpolated.get_skew() == doctest::Approx(rotate_scale_skew_pos_halfway.get_skew()));
  79. CHECK(interpolated.is_equal_approx(rotate_scale_skew_pos_halfway));
  80. interpolated = rotate_scale_skew_pos.interpolate_with(Transform2D(), 0.5);
  81. CHECK(interpolated.is_equal_approx(rotate_scale_skew_pos_halfway));
  82. }
  83. TEST_CASE("[Transform2D] Finite number checks") {
  84. const Vector2 x(0, 1);
  85. const Vector2 infinite(NAN, NAN);
  86. CHECK_MESSAGE(
  87. Transform2D(x, x, x).is_finite(),
  88. "Transform2D with all components finite should be finite");
  89. CHECK_FALSE_MESSAGE(
  90. Transform2D(infinite, x, x).is_finite(),
  91. "Transform2D with one component infinite should not be finite.");
  92. CHECK_FALSE_MESSAGE(
  93. Transform2D(x, infinite, x).is_finite(),
  94. "Transform2D with one component infinite should not be finite.");
  95. CHECK_FALSE_MESSAGE(
  96. Transform2D(x, x, infinite).is_finite(),
  97. "Transform2D with one component infinite should not be finite.");
  98. CHECK_FALSE_MESSAGE(
  99. Transform2D(infinite, infinite, x).is_finite(),
  100. "Transform2D with two components infinite should not be finite.");
  101. CHECK_FALSE_MESSAGE(
  102. Transform2D(infinite, x, infinite).is_finite(),
  103. "Transform2D with two components infinite should not be finite.");
  104. CHECK_FALSE_MESSAGE(
  105. Transform2D(x, infinite, infinite).is_finite(),
  106. "Transform2D with two components infinite should not be finite.");
  107. CHECK_FALSE_MESSAGE(
  108. Transform2D(infinite, infinite, infinite).is_finite(),
  109. "Transform2D with three components infinite should not be finite.");
  110. }
  111. } // namespace TestTransform2D
  112. #endif // TEST_TRANSFORM_2D_H