test_curve_3d.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**************************************************************************/
  2. /* test_curve_3d.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_3D_H
  31. #define TEST_CURVE_3D_H
  32. #include "core/math/math_funcs.h"
  33. #include "scene/resources/curve.h"
  34. #include "tests/test_macros.h"
  35. namespace TestCurve3D {
  36. void add_sample_curve_points(Ref<Curve3D> &curve) {
  37. Vector3 p0 = Vector3(0, 0, 0);
  38. Vector3 p1 = Vector3(50, 0, 0);
  39. Vector3 p2 = Vector3(50, 50, 50);
  40. Vector3 p3 = Vector3(0, 50, 0);
  41. Vector3 control0 = p1 - p0;
  42. Vector3 control1 = p3 - p2;
  43. curve->add_point(p0, Vector3(), control0);
  44. curve->add_point(p3, control1, Vector3());
  45. }
  46. TEST_CASE("[Curve3D] Default curve is empty") {
  47. const Ref<Curve3D> curve = memnew(Curve3D);
  48. CHECK(curve->get_point_count() == 0);
  49. }
  50. TEST_CASE("[Curve3D] Point management") {
  51. Ref<Curve3D> curve = memnew(Curve3D);
  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(Vector3());
  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. Vector3 new_in = Vector3(1, 1, 1);
  64. Vector3 new_out = Vector3(1, 1, 1);
  65. Vector3 new_pos = Vector3(1, 1, 1);
  66. real_t new_tilt = 1;
  67. curve->add_point(Vector3());
  68. CHECK(curve->get_point_in(0) != new_in);
  69. curve->set_point_in(0, new_in);
  70. CHECK(curve->get_point_in(0) == new_in);
  71. CHECK(curve->get_point_out(0) != new_out);
  72. curve->set_point_out(0, new_out);
  73. CHECK(curve->get_point_out(0) == new_out);
  74. CHECK(curve->get_point_position(0) != new_pos);
  75. curve->set_point_position(0, new_pos);
  76. CHECK(curve->get_point_position(0) == new_pos);
  77. CHECK(curve->get_point_tilt(0) != new_tilt);
  78. curve->set_point_tilt(0, new_tilt);
  79. CHECK(curve->get_point_tilt(0) == new_tilt);
  80. }
  81. }
  82. TEST_CASE("[Curve3D] Baked") {
  83. Ref<Curve3D> curve = memnew(Curve3D);
  84. SUBCASE("Single Point") {
  85. curve->add_point(Vector3());
  86. CHECK(curve->get_baked_length() == 0);
  87. CHECK(curve->get_baked_points().size() == 1);
  88. CHECK(curve->get_baked_tilts().size() == 1);
  89. CHECK(curve->get_baked_up_vectors().size() == 1);
  90. }
  91. SUBCASE("Straight line") {
  92. curve->add_point(Vector3());
  93. curve->add_point(Vector3(0, 50, 0));
  94. CHECK(Math::is_equal_approx(curve->get_baked_length(), 50));
  95. CHECK(curve->get_baked_points().size() == 369);
  96. CHECK(curve->get_baked_tilts().size() == 369);
  97. CHECK(curve->get_baked_up_vectors().size() == 369);
  98. }
  99. SUBCASE("Beziér Curve") {
  100. add_sample_curve_points(curve);
  101. real_t len = curve->get_baked_length();
  102. real_t n_points = curve->get_baked_points().size();
  103. // Curve length should be bigger than a straight line between points
  104. CHECK(len > 50);
  105. SUBCASE("Increase bake interval") {
  106. curve->set_bake_interval(10.0);
  107. CHECK(curve->get_bake_interval() == 10.0);
  108. // Lower resolution should imply less points and smaller length
  109. CHECK(curve->get_baked_length() < len);
  110. CHECK(curve->get_baked_points().size() < n_points);
  111. CHECK(curve->get_baked_tilts().size() < n_points);
  112. CHECK(curve->get_baked_up_vectors().size() < n_points);
  113. }
  114. SUBCASE("Disable up vectors") {
  115. curve->set_up_vector_enabled(false);
  116. CHECK(curve->is_up_vector_enabled() == false);
  117. CHECK(curve->get_baked_up_vectors().size() == 0);
  118. }
  119. }
  120. }
  121. TEST_CASE("[Curve3D] Sampling") {
  122. // Sampling over a simple straight line to make assertions simpler
  123. Ref<Curve3D> curve = memnew(Curve3D);
  124. curve->add_point(Vector3());
  125. curve->add_point(Vector3(0, 50, 0));
  126. SUBCASE("sample") {
  127. CHECK(curve->sample(0, 0) == Vector3(0, 0, 0));
  128. CHECK(curve->sample(0, 0.5) == Vector3(0, 25, 0));
  129. CHECK(curve->sample(0, 1) == Vector3(0, 50, 0));
  130. }
  131. SUBCASE("samplef") {
  132. CHECK(curve->samplef(0) == Vector3(0, 0, 0));
  133. CHECK(curve->samplef(0.5) == Vector3(0, 25, 0));
  134. CHECK(curve->samplef(1) == Vector3(0, 50, 0));
  135. }
  136. SUBCASE("sample_baked, cubic = false") {
  137. CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 0, 0))) == Vector3(0, 0, 0));
  138. CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 25, 0))) == Vector3(0, 25, 0));
  139. CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 50, 0))) == Vector3(0, 50, 0));
  140. }
  141. SUBCASE("sample_baked, cubic = true") {
  142. CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 0, 0)), true) == Vector3(0, 0, 0));
  143. CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 25, 0)), true) == Vector3(0, 25, 0));
  144. CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 50, 0)), true) == Vector3(0, 50, 0));
  145. }
  146. SUBCASE("sample_baked_with_rotation, cubic = false, p_apply_tilt = false") {
  147. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 0, 0))) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 0, 0)));
  148. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 25, 0))) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 25, 0)));
  149. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 50, 0))) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 50, 0)));
  150. }
  151. SUBCASE("sample_baked_with_rotation, cubic = false, p_apply_tilt = true") {
  152. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 0, 0)), false, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 0, 0)));
  153. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 25, 0)), false, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 25, 0)));
  154. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 50, 0)), false, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 50, 0)));
  155. }
  156. SUBCASE("sample_baked_with_rotation, cubic = true, p_apply_tilt = false") {
  157. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 0, 0)), true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 0, 0)));
  158. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 25, 0)), true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 25, 0)));
  159. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 50, 0)), true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 50, 0)));
  160. }
  161. SUBCASE("sample_baked_with_rotation, cubic = true, p_apply_tilt = true") {
  162. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 0, 0)), true, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 0, 0)));
  163. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 25, 0)), true, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 25, 0)));
  164. CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 50, 0)), true, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 50, 0)));
  165. }
  166. SUBCASE("sample_baked_tilt") {
  167. CHECK(curve->sample_baked_tilt(curve->get_closest_offset(Vector3(0, 0, 0))) == 0);
  168. CHECK(curve->sample_baked_tilt(curve->get_closest_offset(Vector3(0, 25, 0))) == 0);
  169. CHECK(curve->sample_baked_tilt(curve->get_closest_offset(Vector3(0, 50, 0))) == 0);
  170. }
  171. SUBCASE("sample_baked_up_vector, p_apply_tilt = false") {
  172. CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 0, 0))) == Vector3(1, 0, 0));
  173. CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 25, 0))) == Vector3(1, 0, 0));
  174. CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 50, 0))) == Vector3(1, 0, 0));
  175. }
  176. SUBCASE("sample_baked_up_vector, p_apply_tilt = true") {
  177. CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 0, 0)), true) == Vector3(1, 0, 0));
  178. CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 25, 0)), true) == Vector3(1, 0, 0));
  179. CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 50, 0)), true) == Vector3(1, 0, 0));
  180. }
  181. SUBCASE("get_closest_point") {
  182. CHECK(curve->get_closest_point(Vector3(0, 0, 0)) == Vector3(0, 0, 0));
  183. CHECK(curve->get_closest_point(Vector3(0, 25, 0)) == Vector3(0, 25, 0));
  184. CHECK(curve->get_closest_point(Vector3(50, 25, 0)) == Vector3(0, 25, 0));
  185. CHECK(curve->get_closest_point(Vector3(0, 50, 0)) == Vector3(0, 50, 0));
  186. CHECK(curve->get_closest_point(Vector3(50, 50, 0)) == Vector3(0, 50, 0));
  187. CHECK(curve->get_closest_point(Vector3(0, 100, 0)) == Vector3(0, 50, 0));
  188. }
  189. SUBCASE("sample_baked_up_vector, off-axis") {
  190. // Regression test for issue #81879
  191. Ref<Curve3D> c = memnew(Curve3D);
  192. c->add_point(Vector3());
  193. c->add_point(Vector3(0, .1, 1));
  194. CHECK_LT((c->sample_baked_up_vector(c->get_closest_offset(Vector3(0, 0, .9))) - Vector3(0, 0.995037, -0.099504)).length(), 0.01);
  195. }
  196. }
  197. TEST_CASE("[Curve3D] Tessellation") {
  198. Ref<Curve3D> curve = memnew(Curve3D);
  199. add_sample_curve_points(curve);
  200. const int default_size = curve->tessellate().size();
  201. SUBCASE("Increase to max stages should increase num of points") {
  202. CHECK(curve->tessellate(6).size() > default_size);
  203. }
  204. SUBCASE("Decrease to max stages should decrease num of points") {
  205. CHECK(curve->tessellate(4).size() < default_size);
  206. }
  207. SUBCASE("Increase to tolerance should decrease num of points") {
  208. CHECK(curve->tessellate(5, 5).size() < default_size);
  209. }
  210. SUBCASE("Decrease to tolerance should increase num of points") {
  211. CHECK(curve->tessellate(5, 3).size() > default_size);
  212. }
  213. SUBCASE("Adding a straight segment should only add the last point to tessellate return array") {
  214. curve->add_point(Vector3(0, 100, 0));
  215. PackedVector3Array tes = curve->tessellate();
  216. CHECK(tes.size() == default_size + 1);
  217. CHECK(tes[tes.size() - 1] == Vector3(0, 100, 0));
  218. CHECK(tes[tes.size() - 2] == Vector3(0, 50, 0));
  219. }
  220. }
  221. TEST_CASE("[Curve3D] Even length tessellation") {
  222. Ref<Curve3D> curve = memnew(Curve3D);
  223. add_sample_curve_points(curve);
  224. const int default_size = curve->tessellate_even_length().size();
  225. // Default tessellate_even_length tolerance_length is 20.0, by adding a 100 units
  226. // straight, we expect the total size to be increased by more than 5,
  227. // that is, the algo will pick a length < 20.0 and will divide the straight as
  228. // well as the curve as opposed to tessellate() which only adds the final point.
  229. curve->add_point(Vector3(0, 150, 0));
  230. CHECK(curve->tessellate_even_length().size() > default_size + 5);
  231. }
  232. } // namespace TestCurve3D
  233. #endif // TEST_CURVE_3D_H