test_curve.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 unit 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 non-unit curve with free tangents") {
  126. Ref<Curve> curve = memnew(Curve);
  127. curve->set_min_domain(-100.0);
  128. curve->set_max_domain(100.0);
  129. // "Sawtooth" curve with an open ending towards the 100 offset.
  130. curve->add_point(Vector2(-100, 0));
  131. curve->add_point(Vector2(-50, 1));
  132. curve->add_point(Vector2(0, 0));
  133. curve->add_point(Vector2(50, 1));
  134. curve->set_bake_resolution(11);
  135. CHECK_MESSAGE(
  136. Math::is_zero_approx(curve->get_point_left_tangent(0)),
  137. "get_point_left_tangent() should return the expected value for point index 0.");
  138. CHECK_MESSAGE(
  139. Math::is_zero_approx(curve->get_point_right_tangent(0)),
  140. "get_point_right_tangent() should return the expected value for point index 0.");
  141. CHECK_MESSAGE(
  142. curve->get_point_left_mode(0) == Curve::TangentMode::TANGENT_FREE,
  143. "get_point_left_mode() should return the expected value for point index 0.");
  144. CHECK_MESSAGE(
  145. curve->get_point_right_mode(0) == Curve::TangentMode::TANGENT_FREE,
  146. "get_point_right_mode() should return the expected value for point index 0.");
  147. CHECK_MESSAGE(
  148. curve->get_point_count() == 4,
  149. "Custom free curve should contain the expected number of points.");
  150. CHECK_MESSAGE(
  151. Math::is_zero_approx(curve->sample(-200)),
  152. "Custom free curve should return the expected value at offset -200.");
  153. CHECK_MESSAGE(
  154. curve->sample(0.1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.352),
  155. "Custom free curve should return the expected value at offset equivalent to a unit curve's 0.1.");
  156. CHECK_MESSAGE(
  157. curve->sample(0.4 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.352),
  158. "Custom free curve should return the expected value at offset equivalent to a unit curve's 0.4.");
  159. CHECK_MESSAGE(
  160. curve->sample(0.7 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.896),
  161. "Custom free curve should return the expected value at offset equivalent to a unit curve's 0.7.");
  162. CHECK_MESSAGE(
  163. curve->sample(1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(1),
  164. "Custom free curve should return the expected value at offset equivalent to a unit curve's 1.");
  165. CHECK_MESSAGE(
  166. curve->sample(2 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(1),
  167. "Custom free curve should return the expected value at offset equivalent to a unit curve's 2.");
  168. CHECK_MESSAGE(
  169. Math::is_zero_approx(curve->sample_baked(-200)),
  170. "Custom free curve should return the expected baked value at offset equivalent to a unit curve's -0.1.");
  171. CHECK_MESSAGE(
  172. curve->sample_baked(0.1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.352),
  173. "Custom free curve should return the expected baked value at offset equivalent to a unit curve's 0.1.");
  174. CHECK_MESSAGE(
  175. curve->sample_baked(0.4 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.352),
  176. "Custom free curve should return the expected baked value at offset equivalent to a unit curve's 0.4.");
  177. CHECK_MESSAGE(
  178. curve->sample_baked(0.7 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.896),
  179. "Custom free curve should return the expected baked value at offset equivalent to a unit curve's 0.7.");
  180. CHECK_MESSAGE(
  181. curve->sample_baked(1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(1),
  182. "Custom free curve should return the expected baked value at offset equivalent to a unit curve's 1.");
  183. CHECK_MESSAGE(
  184. curve->sample_baked(2 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(1),
  185. "Custom free curve should return the expected baked value at offset equivalent to a unit curve's 2.");
  186. curve->remove_point(1);
  187. CHECK_MESSAGE(
  188. curve->sample(0.1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(0),
  189. "Custom free curve should return the expected value at offset equivalent to a unit curve's 0.1 after removing point at index 1.");
  190. CHECK_MESSAGE(
  191. curve->sample_baked(0.1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(0),
  192. "Custom free curve should return the expected baked value at offset equivalent to a unit curve's 0.1 after removing point at index 1.");
  193. curve->clear_points();
  194. CHECK_MESSAGE(
  195. curve->sample(0.6 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(0),
  196. "Custom free curve should return the expected value at offset 0.6 after clearing all points.");
  197. CHECK_MESSAGE(
  198. curve->sample_baked(0.6 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(0),
  199. "Custom free curve should return the expected baked value at offset 0.6 after clearing all points.");
  200. }
  201. TEST_CASE("[Curve] Custom unit curve with linear tangents") {
  202. Ref<Curve> curve = memnew(Curve);
  203. // "Sawtooth" curve with an open ending towards the 1.0 offset.
  204. curve->add_point(Vector2(0, 0), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  205. curve->add_point(Vector2(0.25, 1), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  206. curve->add_point(Vector2(0.5, 0), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  207. curve->add_point(Vector2(0.75, 1), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  208. CHECK_MESSAGE(
  209. curve->get_point_left_tangent(3) == doctest::Approx(4),
  210. "get_point_left_tangent() should return the expected value for point index 3.");
  211. CHECK_MESSAGE(
  212. Math::is_zero_approx(curve->get_point_right_tangent(3)),
  213. "get_point_right_tangent() should return the expected value for point index 3.");
  214. CHECK_MESSAGE(
  215. curve->get_point_left_mode(3) == Curve::TangentMode::TANGENT_LINEAR,
  216. "get_point_left_mode() should return the expected value for point index 3.");
  217. CHECK_MESSAGE(
  218. curve->get_point_right_mode(3) == Curve::TangentMode::TANGENT_LINEAR,
  219. "get_point_right_mode() should return the expected value for point index 3.");
  220. ERR_PRINT_OFF;
  221. CHECK_MESSAGE(
  222. Math::is_zero_approx(curve->get_point_right_tangent(300)),
  223. "get_point_right_tangent() should return the expected value for invalid point index 300.");
  224. CHECK_MESSAGE(
  225. curve->get_point_left_mode(-12345) == Curve::TangentMode::TANGENT_FREE,
  226. "get_point_left_mode() should return the expected value for invalid point index -12345.");
  227. ERR_PRINT_ON;
  228. CHECK_MESSAGE(
  229. curve->get_point_count() == 4,
  230. "Custom linear curve should contain the expected number of points.");
  231. CHECK_MESSAGE(
  232. Math::is_zero_approx(curve->sample(-0.1)),
  233. "Custom linear curve should return the expected value at offset -0.1.");
  234. CHECK_MESSAGE(
  235. curve->sample(0.1) == doctest::Approx((real_t)0.4),
  236. "Custom linear curve should return the expected value at offset 0.1.");
  237. CHECK_MESSAGE(
  238. curve->sample(0.4) == doctest::Approx((real_t)0.4),
  239. "Custom linear curve should return the expected value at offset 0.4.");
  240. CHECK_MESSAGE(
  241. curve->sample(0.7) == doctest::Approx((real_t)0.8),
  242. "Custom linear curve should return the expected value at offset 0.7.");
  243. CHECK_MESSAGE(
  244. curve->sample(1) == doctest::Approx(1),
  245. "Custom linear curve should return the expected value at offset 1.0.");
  246. CHECK_MESSAGE(
  247. curve->sample(2) == doctest::Approx(1),
  248. "Custom linear curve should return the expected value at offset 2.0.");
  249. CHECK_MESSAGE(
  250. Math::is_zero_approx(curve->sample_baked(-0.1)),
  251. "Custom linear curve should return the expected baked value at offset -0.1.");
  252. CHECK_MESSAGE(
  253. curve->sample_baked(0.1) == doctest::Approx((real_t)0.4),
  254. "Custom linear curve should return the expected baked value at offset 0.1.");
  255. CHECK_MESSAGE(
  256. curve->sample_baked(0.4) == doctest::Approx((real_t)0.4),
  257. "Custom linear curve should return the expected baked value at offset 0.4.");
  258. CHECK_MESSAGE(
  259. curve->sample_baked(0.7) == doctest::Approx((real_t)0.8),
  260. "Custom linear curve should return the expected baked value at offset 0.7.");
  261. CHECK_MESSAGE(
  262. curve->sample_baked(1) == doctest::Approx(1),
  263. "Custom linear curve should return the expected baked value at offset 1.0.");
  264. CHECK_MESSAGE(
  265. curve->sample_baked(2) == doctest::Approx(1),
  266. "Custom linear curve should return the expected baked value at offset 2.0.");
  267. ERR_PRINT_OFF;
  268. curve->remove_point(10);
  269. ERR_PRINT_ON;
  270. CHECK_MESSAGE(
  271. curve->sample(0.7) == doctest::Approx((real_t)0.8),
  272. "Custom free curve should return the expected value at offset 0.7 after removing point at invalid index 10.");
  273. CHECK_MESSAGE(
  274. curve->sample_baked(0.7) == doctest::Approx((real_t)0.8),
  275. "Custom free curve should return the expected baked value at offset 0.7 after removing point at invalid index 10.");
  276. }
  277. TEST_CASE("[Curve] Custom non-unit curve with linear tangents") {
  278. Ref<Curve> curve = memnew(Curve);
  279. curve->set_min_domain(-100.0);
  280. curve->set_max_domain(100.0);
  281. // "Sawtooth" curve with an open ending towards the 100 offset.
  282. curve->add_point(Vector2(-100, 0), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  283. curve->add_point(Vector2(-50, 1), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  284. curve->add_point(Vector2(0, 0), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  285. curve->add_point(Vector2(50, 1), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  286. CHECK_MESSAGE(
  287. curve->get_point_left_tangent(3) == doctest::Approx(1.f / 50),
  288. "get_point_left_tangent() should return the expected value for point index 3.");
  289. CHECK_MESSAGE(
  290. Math::is_zero_approx(curve->get_point_right_tangent(3)),
  291. "get_point_right_tangent() should return the expected value for point index 3.");
  292. CHECK_MESSAGE(
  293. curve->get_point_left_mode(3) == Curve::TangentMode::TANGENT_LINEAR,
  294. "get_point_left_mode() should return the expected value for point index 3.");
  295. CHECK_MESSAGE(
  296. curve->get_point_right_mode(3) == Curve::TangentMode::TANGENT_LINEAR,
  297. "get_point_right_mode() should return the expected value for point index 3.");
  298. ERR_PRINT_OFF;
  299. CHECK_MESSAGE(
  300. Math::is_zero_approx(curve->get_point_right_tangent(300)),
  301. "get_point_right_tangent() should return the expected value for invalid point index 300.");
  302. CHECK_MESSAGE(
  303. curve->get_point_left_mode(-12345) == Curve::TangentMode::TANGENT_FREE,
  304. "get_point_left_mode() should return the expected value for invalid point index -12345.");
  305. ERR_PRINT_ON;
  306. CHECK_MESSAGE(
  307. curve->get_point_count() == 4,
  308. "Custom linear unit curve should contain the expected number of points.");
  309. CHECK_MESSAGE(
  310. Math::is_zero_approx(curve->sample(-0.1 * curve->get_domain_range() + curve->get_min_domain())),
  311. "Custom linear curve should return the expected value at offset equivalent to a unit curve's -0.1.");
  312. CHECK_MESSAGE(
  313. curve->sample(0.1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.4),
  314. "Custom linear curve should return the expected value at offset equivalent to a unit curve's 0.1.");
  315. CHECK_MESSAGE(
  316. curve->sample(0.4 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.4),
  317. "Custom linear curve should return the expected value at offset equivalent to a unit curve's 0.4.");
  318. CHECK_MESSAGE(
  319. curve->sample(0.7 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.8),
  320. "Custom linear curve should return the expected value at offset equivalent to a unit curve's 0.7.");
  321. CHECK_MESSAGE(
  322. curve->sample(1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(1),
  323. "Custom linear curve should return the expected value at offset equivalent to a unit curve's 1.0.");
  324. CHECK_MESSAGE(
  325. curve->sample(2 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(1),
  326. "Custom linear curve should return the expected value at offset equivalent to a unit curve's 2.0.");
  327. CHECK_MESSAGE(
  328. Math::is_zero_approx(curve->sample_baked(-0.1 * curve->get_domain_range() + curve->get_min_domain())),
  329. "Custom linear curve should return the expected baked value at offset equivalent to a unit curve's -0.1.");
  330. CHECK_MESSAGE(
  331. curve->sample_baked(0.1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.4),
  332. "Custom linear curve should return the expected baked value at offset equivalent to a unit curve's 0.1.");
  333. CHECK_MESSAGE(
  334. curve->sample_baked(0.4 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.4),
  335. "Custom linear curve should return the expected baked value at offset equivalent to a unit curve's 0.4.");
  336. CHECK_MESSAGE(
  337. curve->sample_baked(0.7 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.8),
  338. "Custom linear curve should return the expected baked value at offset equivalent to a unit curve's 0.7.");
  339. CHECK_MESSAGE(
  340. curve->sample_baked(1 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(1),
  341. "Custom linear curve should return the expected baked value at offset equivalent to a unit curve's 1.0.");
  342. CHECK_MESSAGE(
  343. curve->sample_baked(2 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx(1),
  344. "Custom linear curve should return the expected baked value at offset equivalent to a unit curve's 2.0.");
  345. ERR_PRINT_OFF;
  346. curve->remove_point(10);
  347. ERR_PRINT_ON;
  348. CHECK_MESSAGE(
  349. curve->sample(0.7 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.8),
  350. "Custom free curve should return the expected value at offset equivalent to a unit curve's 0.7 after removing point at invalid index 10.");
  351. CHECK_MESSAGE(
  352. curve->sample_baked(0.7 * curve->get_domain_range() + curve->get_min_domain()) == doctest::Approx((real_t)0.8),
  353. "Custom free curve should return the expected baked value at offset equivalent to a unit curve's 0.7 after removing point at invalid index 10.");
  354. }
  355. TEST_CASE("[Curve] Straight line offset test") {
  356. Ref<Curve> curve = memnew(Curve);
  357. curve->add_point(Vector2(0, 0));
  358. curve->add_point(Vector2(1, 1));
  359. CHECK_MESSAGE(
  360. curve->sample_baked(1.0 - (0.5 / curve->get_bake_resolution())) != curve->sample_baked(1),
  361. "Straight line curve should return different baked values at offset 1 vs offset (1 - 0.5 / bake resolution) .");
  362. }
  363. TEST_CASE("[Curve2D] Linear sampling should return exact value") {
  364. Ref<Curve2D> curve = memnew(Curve2D);
  365. real_t len = 2048.0;
  366. curve->add_point(Vector2(0, 0));
  367. curve->add_point(Vector2(len, 0));
  368. real_t baked_length = curve->get_baked_length();
  369. CHECK(len == baked_length);
  370. for (int i = 0; i < len; i++) {
  371. Vector2 pos = curve->sample_baked(i);
  372. CHECK_MESSAGE(Math::is_equal_approx(pos.x, i), "sample_baked should return exact value");
  373. }
  374. }
  375. TEST_CASE("[Curve3D] Linear sampling should return exact value") {
  376. Ref<Curve3D> curve = memnew(Curve3D);
  377. real_t len = 2048.0;
  378. curve->add_point(Vector3(0, 0, 0));
  379. curve->add_point(Vector3(len, 0, 0));
  380. ERR_PRINT_OFF
  381. real_t baked_length = curve->get_baked_length();
  382. ERR_PRINT_ON
  383. CHECK(len == baked_length);
  384. for (int i = 0; i < len; i++) {
  385. Vector3 pos = curve->sample_baked(i);
  386. CHECK_MESSAGE(Math::is_equal_approx(pos.x, i), "sample_baked should return exact value");
  387. }
  388. }
  389. } // namespace TestCurve
  390. #endif // TEST_CURVE_H