test_curve.h 21 KB

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