test_curve_2d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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, cubic = false") {
  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, cubic = true") {
  126. CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 0)), true) == Vector2(0, 0));
  127. CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 25)), true) == Vector2(0, 25));
  128. CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 50)), true) == Vector2(0, 50));
  129. }
  130. SUBCASE("sample_baked_with_rotation, cubic = false") {
  131. const real_t pi = 3.14159;
  132. const real_t half_pi = pi * 0.5;
  133. Ref<Curve2D> rot_curve = memnew(Curve2D);
  134. Transform2D t;
  135. rot_curve->clear_points();
  136. rot_curve->add_point(Vector2());
  137. rot_curve->add_point(Vector2(50, 0));
  138. t = rot_curve->sample_baked_with_rotation(25);
  139. CHECK(t.get_origin() == Vector2(25, 0));
  140. CHECK(Math::is_equal_approx(t.get_rotation(), 0));
  141. rot_curve->clear_points();
  142. rot_curve->add_point(Vector2());
  143. rot_curve->add_point(Vector2(0, 50));
  144. t = rot_curve->sample_baked_with_rotation(25);
  145. CHECK(t.get_origin() == Vector2(0, 25));
  146. CHECK(Math::is_equal_approx(t.get_rotation(), half_pi));
  147. rot_curve->clear_points();
  148. rot_curve->add_point(Vector2());
  149. rot_curve->add_point(Vector2(-50, 0));
  150. t = rot_curve->sample_baked_with_rotation(25);
  151. CHECK(t.get_origin() == Vector2(-25, 0));
  152. CHECK(Math::is_equal_approx(t.get_rotation(), pi));
  153. rot_curve->clear_points();
  154. rot_curve->add_point(Vector2());
  155. rot_curve->add_point(Vector2(0, -50));
  156. t = rot_curve->sample_baked_with_rotation(25);
  157. CHECK(t.get_origin() == Vector2(0, -25));
  158. CHECK(Math::is_equal_approx(t.get_rotation(), -half_pi));
  159. }
  160. SUBCASE("sample_baked_with_rotation, cubic = true") {
  161. const real_t pi = 3.14159;
  162. const real_t half_pi = pi * 0.5;
  163. Ref<Curve2D> rot_curve = memnew(Curve2D);
  164. Transform2D t;
  165. rot_curve->clear_points();
  166. rot_curve->add_point(Vector2());
  167. rot_curve->add_point(Vector2(50, 0));
  168. t = rot_curve->sample_baked_with_rotation(25, true);
  169. CHECK(t.get_origin() == Vector2(25, 0));
  170. CHECK(Math::is_equal_approx(t.get_rotation(), 0));
  171. rot_curve->clear_points();
  172. rot_curve->add_point(Vector2());
  173. rot_curve->add_point(Vector2(0, 50));
  174. t = rot_curve->sample_baked_with_rotation(25, true);
  175. CHECK(t.get_origin() == Vector2(0, 25));
  176. CHECK(Math::is_equal_approx(t.get_rotation(), half_pi));
  177. rot_curve->clear_points();
  178. rot_curve->add_point(Vector2());
  179. rot_curve->add_point(Vector2(-50, 0));
  180. t = rot_curve->sample_baked_with_rotation(25, true);
  181. CHECK(t.get_origin() == Vector2(-25, 0));
  182. CHECK(Math::is_equal_approx(t.get_rotation(), pi));
  183. rot_curve->clear_points();
  184. rot_curve->add_point(Vector2());
  185. rot_curve->add_point(Vector2(0, -50));
  186. t = rot_curve->sample_baked_with_rotation(25, true);
  187. CHECK(t.get_origin() == Vector2(0, -25));
  188. CHECK(Math::is_equal_approx(t.get_rotation(), -half_pi));
  189. }
  190. SUBCASE("get_closest_point") {
  191. CHECK(curve->get_closest_point(Vector2(0, 0)) == Vector2(0, 0));
  192. CHECK(curve->get_closest_point(Vector2(0, 25)) == Vector2(0, 25));
  193. CHECK(curve->get_closest_point(Vector2(50, 25)) == Vector2(0, 25));
  194. CHECK(curve->get_closest_point(Vector2(0, 50)) == Vector2(0, 50));
  195. CHECK(curve->get_closest_point(Vector2(50, 50)) == Vector2(0, 50));
  196. CHECK(curve->get_closest_point(Vector2(0, 100)) == Vector2(0, 50));
  197. }
  198. }
  199. TEST_CASE("[Curve2D] Tessellation") {
  200. Ref<Curve2D> curve = memnew(Curve2D);
  201. add_sample_curve_points(curve);
  202. const int default_size = curve->tessellate().size();
  203. SUBCASE("Increase to max stages should increase num of points") {
  204. CHECK(curve->tessellate(6).size() > default_size);
  205. }
  206. SUBCASE("Decrease to max stages should decrease num of points") {
  207. CHECK(curve->tessellate(4).size() < default_size);
  208. }
  209. SUBCASE("Increase to tolerance should decrease num of points") {
  210. CHECK(curve->tessellate(5, 5).size() < default_size);
  211. }
  212. SUBCASE("Decrease to tolerance should increase num of points") {
  213. CHECK(curve->tessellate(5, 3).size() > default_size);
  214. }
  215. SUBCASE("Adding a straight segment should only add the last point to tessellate return array") {
  216. curve->add_point(Vector2(0, 100));
  217. PackedVector2Array tes = curve->tessellate();
  218. CHECK(tes.size() == default_size + 1);
  219. CHECK(tes[tes.size() - 1] == Vector2(0, 100));
  220. CHECK(tes[tes.size() - 2] == Vector2(0, 50));
  221. }
  222. }
  223. TEST_CASE("[Curve2D] Even length tessellation") {
  224. Ref<Curve2D> curve = memnew(Curve2D);
  225. add_sample_curve_points(curve);
  226. const int default_size = curve->tessellate_even_length().size();
  227. // Default tessellate_even_length tolerance_length is 20.0, by adding a 100 units
  228. // straight, we expect the total size to be increased by more than 5,
  229. // that is, the algo will pick a length < 20.0 and will divide the straight as
  230. // well as the curve as opposed to tessellate() which only adds the final point
  231. curve->add_point(Vector2(0, 150));
  232. CHECK(curve->tessellate_even_length().size() > default_size + 5);
  233. }
  234. } // namespace TestCurve2D
  235. #endif // TEST_CURVE_2D_H