test_curve.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*************************************************************************/
  2. /* test_curve.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "scene/resources/curve.h"
  33. #include "thirdparty/doctest/doctest.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->interpolate(0)),
  42. "Default curve should return the expected value at offset 0.0.");
  43. CHECK_MESSAGE(
  44. Math::is_zero_approx(curve->interpolate(0.5)),
  45. "Default curve should return the expected value at offset 0.5.");
  46. CHECK_MESSAGE(
  47. Math::is_zero_approx(curve->interpolate(1)),
  48. "Default curve should return the expected value at offset 1.0.");
  49. }
  50. TEST_CASE("[Curve] Custom 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. CHECK_MESSAGE(
  58. Math::is_zero_approx(curve->get_point_left_tangent(0)),
  59. "get_point_left_tangent() should return the expected value for point index 0.");
  60. CHECK_MESSAGE(
  61. Math::is_zero_approx(curve->get_point_right_tangent(0)),
  62. "get_point_right_tangent() should return the expected value for point index 0.");
  63. CHECK_MESSAGE(
  64. curve->get_point_left_mode(0) == Curve::TangentMode::TANGENT_FREE,
  65. "get_point_left_mode() should return the expected value for point index 0.");
  66. CHECK_MESSAGE(
  67. curve->get_point_right_mode(0) == Curve::TangentMode::TANGENT_FREE,
  68. "get_point_right_mode() should return the expected value for point index 0.");
  69. CHECK_MESSAGE(
  70. curve->get_point_count() == 4,
  71. "Custom free curve should contain the expected number of points.");
  72. CHECK_MESSAGE(
  73. Math::is_equal_approx(curve->interpolate(-0.1), 0),
  74. "Custom free curve should return the expected value at offset 0.1.");
  75. CHECK_MESSAGE(
  76. Math::is_equal_approx(curve->interpolate(0.1), 0.352),
  77. "Custom free curve should return the expected value at offset 0.1.");
  78. CHECK_MESSAGE(
  79. Math::is_equal_approx(curve->interpolate(0.4), 0.352),
  80. "Custom free curve should return the expected value at offset 0.1.");
  81. CHECK_MESSAGE(
  82. Math::is_equal_approx(curve->interpolate(0.7), 0.896),
  83. "Custom free curve should return the expected value at offset 0.1.");
  84. CHECK_MESSAGE(
  85. Math::is_equal_approx(curve->interpolate(1), 1),
  86. "Custom free curve should return the expected value at offset 0.1.");
  87. CHECK_MESSAGE(
  88. Math::is_equal_approx(curve->interpolate(2), 1),
  89. "Custom free curve should return the expected value at offset 0.1.");
  90. CHECK_MESSAGE(
  91. Math::is_equal_approx(curve->interpolate_baked(-0.1), 0),
  92. "Custom free curve should return the expected baked value at offset 0.1.");
  93. CHECK_MESSAGE(
  94. Math::is_equal_approx(curve->interpolate_baked(0.1), 0.352),
  95. "Custom free curve should return the expected baked value at offset 0.1.");
  96. CHECK_MESSAGE(
  97. Math::is_equal_approx(curve->interpolate_baked(0.4), 0.352),
  98. "Custom free curve should return the expected baked value at offset 0.1.");
  99. CHECK_MESSAGE(
  100. Math::is_equal_approx(curve->interpolate_baked(0.7), 0.896),
  101. "Custom free curve should return the expected baked value at offset 0.1.");
  102. CHECK_MESSAGE(
  103. Math::is_equal_approx(curve->interpolate_baked(1), 1),
  104. "Custom free curve should return the expected baked value at offset 0.1.");
  105. CHECK_MESSAGE(
  106. Math::is_equal_approx(curve->interpolate_baked(2), 1),
  107. "Custom free curve should return the expected baked value at offset 0.1.");
  108. curve->remove_point(1);
  109. CHECK_MESSAGE(
  110. Math::is_equal_approx(curve->interpolate(0.1), 0),
  111. "Custom free curve should return the expected value at offset 0.1 after removing point at index 1.");
  112. CHECK_MESSAGE(
  113. Math::is_equal_approx(curve->interpolate_baked(0.1), 0),
  114. "Custom free curve should return the expected baked value at offset 0.1 after removing point at index 1.");
  115. curve->clear_points();
  116. CHECK_MESSAGE(
  117. Math::is_equal_approx(curve->interpolate(0.6), 0),
  118. "Custom free curve should return the expected value at offset 0.6 after clearing all points.");
  119. CHECK_MESSAGE(
  120. Math::is_equal_approx(curve->interpolate_baked(0.6), 0),
  121. "Custom free curve should return the expected baked value at offset 0.6 after clearing all points.");
  122. }
  123. TEST_CASE("[Curve] Custom curve with linear tangents") {
  124. Ref<Curve> curve = memnew(Curve);
  125. // "Sawtooth" curve with an open ending towards the 1.0 offset.
  126. curve->add_point(Vector2(0, 0), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  127. curve->add_point(Vector2(0.25, 1), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  128. curve->add_point(Vector2(0.5, 0), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  129. curve->add_point(Vector2(0.75, 1), 0, 0, Curve::TangentMode::TANGENT_LINEAR, Curve::TangentMode::TANGENT_LINEAR);
  130. CHECK_MESSAGE(
  131. Math::is_equal_approx(curve->get_point_left_tangent(3), 4),
  132. "get_point_left_tangent() should return the expected value for point index 3.");
  133. CHECK_MESSAGE(
  134. Math::is_zero_approx(curve->get_point_right_tangent(3)),
  135. "get_point_right_tangent() should return the expected value for point index 3.");
  136. CHECK_MESSAGE(
  137. curve->get_point_left_mode(3) == Curve::TangentMode::TANGENT_LINEAR,
  138. "get_point_left_mode() should return the expected value for point index 3.");
  139. CHECK_MESSAGE(
  140. curve->get_point_right_mode(3) == Curve::TangentMode::TANGENT_LINEAR,
  141. "get_point_right_mode() should return the expected value for point index 3.");
  142. ERR_PRINT_OFF;
  143. CHECK_MESSAGE(
  144. Math::is_zero_approx(curve->get_point_right_tangent(300)),
  145. "get_point_right_tangent() should return the expected value for invalid point index 300.");
  146. CHECK_MESSAGE(
  147. curve->get_point_left_mode(-12345) == Curve::TangentMode::TANGENT_FREE,
  148. "get_point_left_mode() should return the expected value for invalid point index -12345.");
  149. ERR_PRINT_ON;
  150. CHECK_MESSAGE(
  151. curve->get_point_count() == 4,
  152. "Custom linear curve should contain the expected number of points.");
  153. CHECK_MESSAGE(
  154. Math::is_equal_approx(curve->interpolate(-0.1), 0),
  155. "Custom linear curve should return the expected value at offset -0.1.");
  156. CHECK_MESSAGE(
  157. Math::is_equal_approx(curve->interpolate(0.1), 0.4),
  158. "Custom linear curve should return the expected value at offset 0.1.");
  159. CHECK_MESSAGE(
  160. Math::is_equal_approx(curve->interpolate(0.4), 0.4),
  161. "Custom linear curve should return the expected value at offset 0.4.");
  162. CHECK_MESSAGE(
  163. Math::is_equal_approx(curve->interpolate(0.7), 0.8),
  164. "Custom linear curve should return the expected value at offset 0.7.");
  165. CHECK_MESSAGE(
  166. Math::is_equal_approx(curve->interpolate(1), 1),
  167. "Custom linear curve should return the expected value at offset 1.0.");
  168. CHECK_MESSAGE(
  169. Math::is_equal_approx(curve->interpolate(2), 1),
  170. "Custom linear curve should return the expected value at offset 2.0.");
  171. CHECK_MESSAGE(
  172. Math::is_equal_approx(curve->interpolate_baked(-0.1), 0),
  173. "Custom linear curve should return the expected baked value at offset -0.1.");
  174. CHECK_MESSAGE(
  175. Math::is_equal_approx(curve->interpolate_baked(0.1), 0.4),
  176. "Custom linear curve should return the expected baked value at offset 0.1.");
  177. CHECK_MESSAGE(
  178. Math::is_equal_approx(curve->interpolate_baked(0.4), 0.4),
  179. "Custom linear curve should return the expected baked value at offset 0.4.");
  180. CHECK_MESSAGE(
  181. Math::is_equal_approx(curve->interpolate_baked(0.7), 0.8),
  182. "Custom linear curve should return the expected baked value at offset 0.7.");
  183. CHECK_MESSAGE(
  184. Math::is_equal_approx(curve->interpolate_baked(1), 1),
  185. "Custom linear curve should return the expected baked value at offset 1.0.");
  186. CHECK_MESSAGE(
  187. Math::is_equal_approx(curve->interpolate_baked(2), 1),
  188. "Custom linear curve should return the expected baked value at offset 2.0.");
  189. ERR_PRINT_OFF;
  190. curve->remove_point(10);
  191. ERR_PRINT_ON;
  192. CHECK_MESSAGE(
  193. Math::is_equal_approx(curve->interpolate(0.7), 0.8),
  194. "Custom free curve should return the expected value at offset 0.7 after removing point at invalid index 10.");
  195. CHECK_MESSAGE(
  196. Math::is_equal_approx(curve->interpolate_baked(0.7), 0.8),
  197. "Custom free curve should return the expected baked value at offset 0.7 after removing point at invalid index 10.");
  198. }
  199. } // namespace TestCurve
  200. #endif // TEST_CURVE_H