test_curve_2d.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* test_curve_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_CURVE_2D_H
  31. #define TEST_CURVE_2D_H
  32. #include "core/math/math_funcs.h"
  33. #include "scene/resources/curve.h"
  34. #include "tests/test_macros.h"
  35. namespace TestCurve2D {
  36. void add_sample_curve_points(Ref<Curve2D> &curve) {
  37. Vector2 p0 = Vector2(0, 0);
  38. Vector2 p1 = Vector2(50, 0);
  39. Vector2 p2 = Vector2(50, 50);
  40. Vector2 p3 = Vector2(0, 50);
  41. Vector2 control0 = p1 - p0;
  42. Vector2 control1 = p3 - p2;
  43. curve->add_point(p0, Vector2(), control0);
  44. curve->add_point(p3, control1, Vector2());
  45. }
  46. TEST_CASE("[Curve2D] Default curve is empty") {
  47. const Ref<Curve2D> curve = memnew(Curve2D);
  48. CHECK(curve->get_point_count() == 0);
  49. }
  50. TEST_CASE("[Curve2D] Point management") {
  51. Ref<Curve2D> curve = memnew(Curve2D);
  52. SUBCASE("Functions for adding/removing points should behave as expected") {
  53. curve->set_point_count(2);
  54. CHECK(curve->get_point_count() == 2);
  55. curve->remove_point(0);
  56. CHECK(curve->get_point_count() == 1);
  57. curve->add_point(Vector2());
  58. CHECK(curve->get_point_count() == 2);
  59. curve->clear_points();
  60. CHECK(curve->get_point_count() == 0);
  61. }
  62. SUBCASE("Functions for changing single point properties should behave as expected") {
  63. Vector2 new_in = Vector2(1, 1);
  64. Vector2 new_out = Vector2(1, 1);
  65. Vector2 new_pos = Vector2(1, 1);
  66. curve->add_point(Vector2());
  67. CHECK(curve->get_point_in(0) != new_in);
  68. curve->set_point_in(0, new_in);
  69. CHECK(curve->get_point_in(0) == new_in);
  70. CHECK(curve->get_point_out(0) != new_out);
  71. curve->set_point_out(0, new_out);
  72. CHECK(curve->get_point_out(0) == new_out);
  73. CHECK(curve->get_point_position(0) != new_pos);
  74. curve->set_point_position(0, new_pos);
  75. CHECK(curve->get_point_position(0) == new_pos);
  76. }
  77. }
  78. TEST_CASE("[Curve2D] Baked") {
  79. Ref<Curve2D> curve = memnew(Curve2D);
  80. SUBCASE("Single Point") {
  81. curve->add_point(Vector2());
  82. CHECK(curve->get_baked_length() == 0);
  83. CHECK(curve->get_baked_points().size() == 1);
  84. }
  85. SUBCASE("Straight line") {
  86. curve->add_point(Vector2());
  87. curve->add_point(Vector2(0, 50));
  88. CHECK(Math::is_equal_approx(curve->get_baked_length(), 50));
  89. CHECK(curve->get_baked_points().size() == 15);
  90. }
  91. SUBCASE("Beziér Curve") {
  92. add_sample_curve_points(curve);
  93. real_t len = curve->get_baked_length();
  94. real_t n_points = curve->get_baked_points().size();
  95. // Curve length should be bigger than a straight between points
  96. CHECK(len > 50);
  97. SUBCASE("Increase bake interval") {
  98. curve->set_bake_interval(10.0);
  99. // Lower resolution should imply less points and smaller length
  100. CHECK(curve->get_baked_length() < len);
  101. CHECK(curve->get_baked_points().size() < n_points);
  102. }
  103. }
  104. }
  105. TEST_CASE("[Curve2D] Sampling") {
  106. // Sampling over a simple straight line to make assertions simpler
  107. Ref<Curve2D> curve = memnew(Curve2D);
  108. curve->add_point(Vector2());
  109. curve->add_point(Vector2(0, 50));
  110. SUBCASE("sample") {
  111. CHECK(curve->sample(0, 0) == Vector2(0, 0));
  112. CHECK(curve->sample(0, 0.5) == Vector2(0, 25));
  113. CHECK(curve->sample(0, 1) == Vector2(0, 50));
  114. }
  115. SUBCASE("samplef") {
  116. CHECK(curve->samplef(0) == Vector2(0, 0));
  117. CHECK(curve->samplef(0.5) == Vector2(0, 25));
  118. CHECK(curve->samplef(1) == Vector2(0, 50));
  119. }
  120. SUBCASE("sample_baked") {
  121. CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 0))) == Vector2(0, 0));
  122. CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 25))) == Vector2(0, 25));
  123. CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 50))) == Vector2(0, 50));
  124. }
  125. SUBCASE("sample_baked_with_rotation") {
  126. const real_t pi = 3.14159;
  127. Transform2D t = curve->sample_baked_with_rotation(curve->get_closest_offset(Vector2(0, 0)));
  128. CHECK(t.get_origin() == Vector2(0, 0));
  129. CHECK(Math::is_equal_approx(t.get_rotation(), pi));
  130. t = curve->sample_baked_with_rotation(curve->get_closest_offset(Vector2(0, 25)));
  131. CHECK(t.get_origin() == Vector2(0, 25));
  132. CHECK(Math::is_equal_approx(t.get_rotation(), pi));
  133. t = curve->sample_baked_with_rotation(curve->get_closest_offset(Vector2(0, 50)));
  134. CHECK(t.get_origin() == Vector2(0, 50));
  135. CHECK(Math::is_equal_approx(t.get_rotation(), pi));
  136. }
  137. SUBCASE("get_closest_point") {
  138. CHECK(curve->get_closest_point(Vector2(0, 0)) == Vector2(0, 0));
  139. CHECK(curve->get_closest_point(Vector2(0, 25)) == Vector2(0, 25));
  140. CHECK(curve->get_closest_point(Vector2(50, 25)) == Vector2(0, 25));
  141. CHECK(curve->get_closest_point(Vector2(0, 50)) == Vector2(0, 50));
  142. CHECK(curve->get_closest_point(Vector2(50, 50)) == Vector2(0, 50));
  143. CHECK(curve->get_closest_point(Vector2(0, 100)) == Vector2(0, 50));
  144. }
  145. }
  146. TEST_CASE("[Curve2D] Tessellation") {
  147. Ref<Curve2D> curve = memnew(Curve2D);
  148. add_sample_curve_points(curve);
  149. const int default_size = curve->tessellate().size();
  150. SUBCASE("Increase to max stages should increase num of points") {
  151. CHECK(curve->tessellate(6).size() > default_size);
  152. }
  153. SUBCASE("Decrease to max stages should decrease num of points") {
  154. CHECK(curve->tessellate(4).size() < default_size);
  155. }
  156. SUBCASE("Increase to tolerance should decrease num of points") {
  157. CHECK(curve->tessellate(5, 5).size() < default_size);
  158. }
  159. SUBCASE("Decrease to tolerance should increase num of points") {
  160. CHECK(curve->tessellate(5, 3).size() > default_size);
  161. }
  162. SUBCASE("Adding a straight segment should only add the last point to tessellate return array") {
  163. curve->add_point(Vector2(0, 100));
  164. PackedVector2Array tes = curve->tessellate();
  165. CHECK(tes.size() == default_size + 1);
  166. CHECK(tes[tes.size() - 1] == Vector2(0, 100));
  167. CHECK(tes[tes.size() - 2] == Vector2(0, 50));
  168. }
  169. }
  170. TEST_CASE("[Curve2D] Even length tessellation") {
  171. Ref<Curve2D> curve = memnew(Curve2D);
  172. add_sample_curve_points(curve);
  173. const int default_size = curve->tessellate_even_length().size();
  174. // Default tessellate_even_length tolerance_length is 20.0, by adding a 100 units
  175. // straight, we expect the total size to be increased by more than 5,
  176. // that is, the algo will pick a length < 20.0 and will divide the straight as
  177. // well as the curve as opposed to tessellate() which only adds the final point
  178. curve->add_point(Vector2(0, 150));
  179. CHECK(curve->tessellate_even_length().size() > default_size + 5);
  180. }
  181. } // namespace TestCurve2D
  182. #endif // TEST_CURVE_2D_H