test_curve.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**************************************************************************/
  2. /* test_curve.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_H
  31. #define TEST_CURVE_H
  32. #include "core/math/math_funcs.h"
  33. #include "scene/resources/curve.h"
  34. #include "tests/test_macros.h"
  35. namespace TestCurve {
  36. TEST_CASE("[Curve] Default curve") {
  37. const Ref<Curve> curve = memnew(Curve);
  38. CHECK_MESSAGE(
  39. curve->get_point_count() == 0,
  40. "Default curve should contain the expected number of points.");
  41. CHECK_MESSAGE(
  42. Math::is_zero_approx(curve->sample(0)),
  43. "Default curve should return the expected value at offset 0.0.");
  44. CHECK_MESSAGE(
  45. Math::is_zero_approx(curve->sample(0.5)),
  46. "Default curve should return the expected value at offset 0.5.");
  47. CHECK_MESSAGE(
  48. Math::is_zero_approx(curve->sample(1)),
  49. "Default curve should return the expected value at offset 1.0.");
  50. }
  51. TEST_CASE("[Curve] Custom curve with free tangents") {
  52. Ref<Curve> curve = memnew(Curve);
  53. // "Sawtooth" curve with an open ending towards the 1.0 offset.
  54. curve->add_point(Vector2(0, 0));
  55. curve->add_point(Vector2(0.25, 1));
  56. curve->add_point(Vector2(0.5, 0));
  57. curve->add_point(Vector2(0.75, 1));
  58. curve->set_bake_resolution(11);
  59. CHECK_MESSAGE(
  60. Math::is_zero_approx(curve->get_point_left_tangent(0)),
  61. "get_point_left_tangent() should return the expected value for point index 0.");
  62. CHECK_MESSAGE(
  63. Math::is_zero_approx(curve->get_point_right_tangent(0)),
  64. "get_point_right_tangent() should return the expected value for point index 0.");
  65. CHECK_MESSAGE(
  66. curve->get_point_left_mode(0) == Curve::TangentMode::TANGENT_FREE,
  67. "get_point_left_mode() should return the expected value for point index 0.");
  68. CHECK_MESSAGE(
  69. curve->get_point_right_mode(0) == Curve::TangentMode::TANGENT_FREE,
  70. "get_point_right_mode() should return the expected value for point index 0.");
  71. CHECK_MESSAGE(
  72. curve->get_point_count() == 4,
  73. "Custom free curve should contain the expected number of points.");
  74. CHECK_MESSAGE(
  75. Math::is_zero_approx(curve->sample(-0.1)),
  76. "Custom free curve should return the expected value at offset -0.1.");
  77. CHECK_MESSAGE(
  78. curve->sample(0.1) == doctest::Approx((real_t)0.352),
  79. "Custom free curve should return the expected value at offset 0.1.");
  80. CHECK_MESSAGE(
  81. curve->sample(0.4) == doctest::Approx((real_t)0.352),
  82. "Custom free curve should return the expected value at offset 0.4.");
  83. CHECK_MESSAGE(
  84. curve->sample(0.7) == doctest::Approx((real_t)0.896),
  85. "Custom free curve should return the expected value at offset 0.7.");
  86. CHECK_MESSAGE(
  87. curve->sample(1) == doctest::Approx(1),
  88. "Custom free curve should return the expected value at offset 1.");
  89. CHECK_MESSAGE(
  90. curve->sample(2) == doctest::Approx(1),
  91. "Custom free curve should return the expected value at offset 2.");
  92. CHECK_MESSAGE(
  93. Math::is_zero_approx(curve->sample_baked(-0.1)),
  94. "Custom free curve should return the expected baked value at offset -0.1.");
  95. CHECK_MESSAGE(
  96. curve->sample_baked(0.1) == doctest::Approx((real_t)0.352),
  97. "Custom free curve should return the expected baked value at offset 0.1.");
  98. CHECK_MESSAGE(
  99. curve->sample_baked(0.4) == doctest::Approx((real_t)0.352),
  100. "Custom free curve should return the expected baked value at offset 0.4.");
  101. CHECK_MESSAGE(
  102. curve->sample_baked(0.7) == doctest::Approx((real_t)0.896),
  103. "Custom free curve should return the expected baked value at offset 0.7.");
  104. CHECK_MESSAGE(
  105. curve->sample_baked(1) == doctest::Approx(1),
  106. "Custom free curve should return the expected baked value at offset 1.");
  107. CHECK_MESSAGE(
  108. curve->sample_baked(2) == doctest::Approx(1),
  109. "Custom free curve should return the expected baked value at offset 2.");
  110. curve->remove_point(1);
  111. CHECK_MESSAGE(
  112. curve->sample(0.1) == doctest::Approx(0),
  113. "Custom free curve should return the expected value at offset 0.1 after removing point at index 1.");
  114. CHECK_MESSAGE(
  115. curve->sample_baked(0.1) == doctest::Approx(0),
  116. "Custom free curve should return the expected baked value at offset 0.1 after removing point at index 1.");
  117. curve->clear_points();
  118. CHECK_MESSAGE(
  119. curve->sample(0.6) == doctest::Approx(0),
  120. "Custom free curve should return the expected value at offset 0.6 after clearing all points.");
  121. CHECK_MESSAGE(
  122. curve->sample_baked(0.6) == doctest::Approx(0),
  123. "Custom free curve should return the expected baked value at offset 0.6 after clearing all points.");
  124. }
  125. TEST_CASE("[Curve] Custom curve with linear tangents") {
  126. Ref<Curve> curve = memnew(Curve);
  127. // "Sawtooth" curve with an open ending towards the 1.0 offset.
  128. curve->add_point(Vector2(0, 0), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  129. curve->add_point(Vector2(0.25, 1), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  130. curve->add_point(Vector2(0.5, 0), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  131. curve->add_point(Vector2(0.75, 1), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  132. CHECK_MESSAGE(
  133. curve->get_point_left_tangent(3) == doctest::Approx(4),
  134. "get_point_left_tangent() should return the expected value for point index 3.");
  135. CHECK_MESSAGE(
  136. Math::is_zero_approx(curve->get_point_right_tangent(3)),
  137. "get_point_right_tangent() should return the expected value for point index 3.");
  138. CHECK_MESSAGE(
  139. curve->get_point_left_mode(3) == Curve::TangentMode::TANGENT_LINEAR,
  140. "get_point_left_mode() should return the expected value for point index 3.");
  141. CHECK_MESSAGE(
  142. curve->get_point_right_mode(3) == Curve::TangentMode::TANGENT_LINEAR,
  143. "get_point_right_mode() should return the expected value for point index 3.");
  144. ERR_PRINT_OFF;
  145. CHECK_MESSAGE(
  146. Math::is_zero_approx(curve->get_point_right_tangent(300)),
  147. "get_point_right_tangent() should return the expected value for invalid point index 300.");
  148. CHECK_MESSAGE(
  149. curve->get_point_left_mode(-12345) == Curve::TangentMode::TANGENT_FREE,
  150. "get_point_left_mode() should return the expected value for invalid point index -12345.");
  151. ERR_PRINT_ON;
  152. CHECK_MESSAGE(
  153. curve->get_point_count() == 4,
  154. "Custom linear curve should contain the expected number of points.");
  155. CHECK_MESSAGE(
  156. Math::is_zero_approx(curve->sample(-0.1)),
  157. "Custom linear curve should return the expected value at offset -0.1.");
  158. CHECK_MESSAGE(
  159. curve->sample(0.1) == doctest::Approx((real_t)0.4),
  160. "Custom linear curve should return the expected value at offset 0.1.");
  161. CHECK_MESSAGE(
  162. curve->sample(0.4) == doctest::Approx((real_t)0.4),
  163. "Custom linear curve should return the expected value at offset 0.4.");
  164. CHECK_MESSAGE(
  165. curve->sample(0.7) == doctest::Approx((real_t)0.8),
  166. "Custom linear curve should return the expected value at offset 0.7.");
  167. CHECK_MESSAGE(
  168. curve->sample(1) == doctest::Approx(1),
  169. "Custom linear curve should return the expected value at offset 1.0.");
  170. CHECK_MESSAGE(
  171. curve->sample(2) == doctest::Approx(1),
  172. "Custom linear curve should return the expected value at offset 2.0.");
  173. CHECK_MESSAGE(
  174. Math::is_zero_approx(curve->sample_baked(-0.1)),
  175. "Custom linear curve should return the expected baked value at offset -0.1.");
  176. CHECK_MESSAGE(
  177. curve->sample_baked(0.1) == doctest::Approx((real_t)0.4),
  178. "Custom linear curve should return the expected baked value at offset 0.1.");
  179. CHECK_MESSAGE(
  180. curve->sample_baked(0.4) == doctest::Approx((real_t)0.4),
  181. "Custom linear curve should return the expected baked value at offset 0.4.");
  182. CHECK_MESSAGE(
  183. curve->sample_baked(0.7) == doctest::Approx((real_t)0.8),
  184. "Custom linear curve should return the expected baked value at offset 0.7.");
  185. CHECK_MESSAGE(
  186. curve->sample_baked(1) == doctest::Approx(1),
  187. "Custom linear curve should return the expected baked value at offset 1.0.");
  188. CHECK_MESSAGE(
  189. curve->sample_baked(2) == doctest::Approx(1),
  190. "Custom linear curve should return the expected baked value at offset 2.0.");
  191. ERR_PRINT_OFF;
  192. curve->remove_point(10);
  193. ERR_PRINT_ON;
  194. CHECK_MESSAGE(
  195. curve->sample(0.7) == doctest::Approx((real_t)0.8),
  196. "Custom free curve should return the expected value at offset 0.7 after removing point at invalid index 10.");
  197. CHECK_MESSAGE(
  198. curve->sample_baked(0.7) == doctest::Approx((real_t)0.8),
  199. "Custom free curve should return the expected baked value at offset 0.7 after removing point at invalid index 10.");
  200. }
  201. TEST_CASE("[Curve] Straight line offset test") {
  202. Ref<Curve> curve = memnew(Curve);
  203. curve->add_point(Vector2(0, 0));
  204. curve->add_point(Vector2(1, 1));
  205. CHECK_MESSAGE(
  206. curve->sample_baked(1.0 - (0.5 / curve->get_bake_resolution())) != curve->sample_baked(1),
  207. "Straight line curve should return different baked values at offset 1 vs offset (1 - 0.5 / bake resolution) .");
  208. }
  209. TEST_CASE("[Curve2D] Linear sampling should return exact value") {
  210. Ref<Curve2D> curve = memnew(Curve2D);
  211. real_t len = 2048.0;
  212. curve->add_point(Vector2(0, 0));
  213. curve->add_point(Vector2(len, 0));
  214. real_t baked_length = curve->get_baked_length();
  215. CHECK(len == baked_length);
  216. for (int i = 0; i < len; i++) {
  217. Vector2 pos = curve->sample_baked(i);
  218. CHECK_MESSAGE(Math::is_equal_approx(pos.x, i), "sample_baked should return exact value");
  219. }
  220. }
  221. TEST_CASE("[Curve3D] Linear sampling should return exact value") {
  222. Ref<Curve3D> curve = memnew(Curve3D);
  223. real_t len = 2048.0;
  224. curve->add_point(Vector3(0, 0, 0));
  225. curve->add_point(Vector3(len, 0, 0));
  226. ERR_PRINT_OFF
  227. real_t baked_length = curve->get_baked_length();
  228. ERR_PRINT_ON
  229. CHECK(len == baked_length);
  230. for (int i = 0; i < len; i++) {
  231. Vector3 pos = curve->sample_baked(i);
  232. CHECK_MESSAGE(Math::is_equal_approx(pos.x, i), "sample_baked should return exact value");
  233. }
  234. }
  235. } // namespace TestCurve
  236. #endif // TEST_CURVE_H