test_curve_3d.h 13 KB

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