test_curve.h 11 KB

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