Debian Codegraph @

tests/scene/test_curve_2d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_curve_2d.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
31 #pragma once
32
33 #include "core/math/math_funcs.h"
34 #include "scene/resources/curve.h"
35
36 #include "tests/test_macros.h"
37
38 namespace TestCurve2D {
39
40 void add_sample_curve_points(Ref<Curve2D> &curve) {
41 Vector2 p0 = Vector2(0, 0);
42 Vector2 p1 = Vector2(50, 0);
43 Vector2 p2 = Vector2(50, 50);
44 Vector2 p3 = Vector2(0, 50);
45
46 Vector2 control0 = p1 - p0;
47 Vector2 control1 = p3 - p2;
48
49 curve->add_point(p0, Vector2(), control0);
50 curve->add_point(p3, control1, Vector2());
51 }
52
53 TEST_CASE("[Curve2D] Default curve is empty") {
54 const Ref<Curve2D> curve = memnew(Curve2D);
55 CHECK(curve->get_point_count() == 0);
56 }
57
58 TEST_CASE("[Curve2D] Point management") {
59 Ref<Curve2D> curve = memnew(Curve2D);
60
61 SUBCASE("Functions for adding/removing points should behave as expected") {
62 curve->set_point_count(2);
63 CHECK(curve->get_point_count() == 2);
64
65 curve->remove_point(0);
66 CHECK(curve->get_point_count() == 1);
67
68 curve->add_point(Vector2());
69 CHECK(curve->get_point_count() == 2);
70
71 curve->clear_points();
72 CHECK(curve->get_point_count() == 0);
73 }
74
75 SUBCASE("Functions for changing single point properties should behave as expected") {
76 Vector2 new_in = Vector2(1, 1);
77 Vector2 new_out = Vector2(1, 1);
78 Vector2 new_pos = Vector2(1, 1);
79
80 curve->add_point(Vector2());
81
82 CHECK(curve->get_point_in(0) != new_in);
83 curve->set_point_in(0, new_in);
84 CHECK(curve->get_point_in(0) == new_in);
85
86 CHECK(curve->get_point_out(0) != new_out);
87 curve->set_point_out(0, new_out);
88 CHECK(curve->get_point_out(0) == new_out);
89
90 CHECK(curve->get_point_position(0) != new_pos);
91 curve->set_point_position(0, new_pos);
92 CHECK(curve->get_point_position(0) == new_pos);
93 }
94 }
95
96 TEST_CASE("[Curve2D] Baked") {
97 Ref<Curve2D> curve = memnew(Curve2D);
98
99 SUBCASE("Single Point") {
100 curve->add_point(Vector2());
101
102 CHECK(curve->get_baked_length() == 0);
103 CHECK(curve->get_baked_points().size() == 1);
104 }
105
106 SUBCASE("Straight line") {
107 curve->add_point(Vector2());
108 curve->add_point(Vector2(0, 50));
109
110 CHECK(Math::is_equal_approx(curve->get_baked_length(), 50));
111 CHECK(curve->get_baked_points().size() == 15);
112 }
113
114 SUBCASE("BeziƩr Curve") {
115 add_sample_curve_points(curve);
116
117 real_t len = curve->get_baked_length();
118 real_t n_points = curve->get_baked_points().size();
119 // Curve length should be bigger than a straight between points
120 CHECK(len > 50);
121
122 SUBCASE("Increase bake interval") {
123 curve->set_bake_interval(10.0);
124 // Lower resolution should imply less points and smaller length
125 CHECK(curve->get_baked_length() < len);
126 CHECK(curve->get_baked_points().size() < n_points);
127 }
128 }
129 }
130
131 TEST_CASE("[Curve2D] Sampling") {
132 // Sampling over a simple straight line to make assertions simpler
133 Ref<Curve2D> curve = memnew(Curve2D);
134 curve->add_point(Vector2());
135 curve->add_point(Vector2(0, 50));
136
137 SUBCASE("sample") {
138 CHECK(curve->sample(0, 0) == Vector2(0, 0));
139 CHECK(curve->sample(0, 0.5) == Vector2(0, 25));
140 CHECK(curve->sample(0, 1) == Vector2(0, 50));
141 }
142
143 SUBCASE("samplef") {
144 CHECK(curve->samplef(0) == Vector2(0, 0));
145 CHECK(curve->samplef(0.5) == Vector2(0, 25));
146 CHECK(curve->samplef(1) == Vector2(0, 50));
147 }
148
149 SUBCASE("sample_baked, cubic = false") {
150 CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 0))) == Vector2(0, 0));
151 CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 25))) == Vector2(0, 25));
152 CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 50))) == Vector2(0, 50));
153 }
154
155 SUBCASE("sample_baked, cubic = true") {
156 CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 0)), true) == Vector2(0, 0));
157 CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 25)), true) == Vector2(0, 25));
158 CHECK(curve->sample_baked(curve->get_closest_offset(Vector2(0, 50)), true) == Vector2(0, 50));
159 }
160
161 SUBCASE("sample_baked_with_rotation, cubic = false") {
162 const real_t pi = 3.14159;
163 const real_t half_pi = pi * 0.5;
164 Ref<Curve2D> rot_curve = memnew(Curve2D);
165 Transform2D t;
166
167 rot_curve->clear_points();
168 rot_curve->add_point(Vector2());
169 rot_curve->add_point(Vector2(50, 0));
170 t = rot_curve->sample_baked_with_rotation(25);
171 CHECK(t.get_origin() == Vector2(25, 0));
172 CHECK(Math::is_equal_approx(t.get_rotation(), 0));
173
174 rot_curve->clear_points();
175 rot_curve->add_point(Vector2());
176 rot_curve->add_point(Vector2(0, 50));
177 t = rot_curve->sample_baked_with_rotation(25);
178 CHECK(t.get_origin() == Vector2(0, 25));
179 CHECK(Math::is_equal_approx(t.get_rotation(), half_pi));
180
181 rot_curve->clear_points();
182 rot_curve->add_point(Vector2());
183 rot_curve->add_point(Vector2(-50, 0));
184 t = rot_curve->sample_baked_with_rotation(25);
185 CHECK(t.get_origin() == Vector2(-25, 0));
186 CHECK(Math::is_equal_approx(t.get_rotation(), pi));
187
188 rot_curve->clear_points();
189 rot_curve->add_point(Vector2());
190 rot_curve->add_point(Vector2(0, -50));
191 t = rot_curve->sample_baked_with_rotation(25);
192 CHECK(t.get_origin() == Vector2(0, -25));
193 CHECK(Math::is_equal_approx(t.get_rotation(), -half_pi));
194 }
195
196 SUBCASE("sample_baked_with_rotation, cubic = true") {
197 const real_t pi = 3.14159;
198 const real_t half_pi = pi * 0.5;
199 Ref<Curve2D> rot_curve = memnew(Curve2D);
200 Transform2D t;
201
202 rot_curve->clear_points();
203 rot_curve->add_point(Vector2());
204 rot_curve->add_point(Vector2(50, 0));
205 t = rot_curve->sample_baked_with_rotation(25, true);
206 CHECK(t.get_origin() == Vector2(25, 0));
207 CHECK(Math::is_equal_approx(t.get_rotation(), 0));
208
209 rot_curve->clear_points();
210 rot_curve->add_point(Vector2());
211 rot_curve->add_point(Vector2(0, 50));
212 t = rot_curve->sample_baked_with_rotation(25, true);
213 CHECK(t.get_origin() == Vector2(0, 25));
214 CHECK(Math::is_equal_approx(t.get_rotation(), half_pi));
215
216 rot_curve->clear_points();
217 rot_curve->add_point(Vector2());
218 rot_curve->add_point(Vector2(-50, 0));
219 t = rot_curve->sample_baked_with_rotation(25, true);
220 CHECK(t.get_origin() == Vector2(-25, 0));
221 CHECK(Math::is_equal_approx(t.get_rotation(), pi));
222
223 rot_curve->clear_points();
224 rot_curve->add_point(Vector2());
225 rot_curve->add_point(Vector2(0, -50));
226 t = rot_curve->sample_baked_with_rotation(25, true);
227 CHECK(t.get_origin() == Vector2(0, -25));
228 CHECK(Math::is_equal_approx(t.get_rotation(), -half_pi));
229 }
230
231 SUBCASE("get_closest_point") {
232 CHECK(curve->get_closest_point(Vector2(0, 0)) == Vector2(0, 0));
233 CHECK(curve->get_closest_point(Vector2(0, 25)) == Vector2(0, 25));
234 CHECK(curve->get_closest_point(Vector2(50, 25)) == Vector2(0, 25));
235 CHECK(curve->get_closest_point(Vector2(0, 50)) == Vector2(0, 50));
236 CHECK(curve->get_closest_point(Vector2(50, 50)) == Vector2(0, 50));
237 CHECK(curve->get_closest_point(Vector2(0, 100)) == Vector2(0, 50));
238 }
239
240 SUBCASE("sample_baked_with_rotation, linear curve with control1 = end and control2 = begin") {
241 // Regression test for issue #88923
242 // The Vector2s that aren't relevant to the issue have x = 2 or x = -2.
243 // They're just set to make collisions with corner cases less likely
244 // that involve zero-vector control points.
245 Ref<Curve2D> cross_linear_curve = memnew(Curve2D);
246 cross_linear_curve->set_bake_interval(0.5);
247 cross_linear_curve->add_point(Vector2(), Vector2(-2, 0), Vector2(1, 0));
248 cross_linear_curve->add_point(Vector2(1, 0), Vector2(-1, 0), Vector2(2, 0));
249 CHECK(cross_linear_curve->get_baked_points().size() >= 3);
250 CHECK(cross_linear_curve->sample_baked_with_rotation(cross_linear_curve->get_closest_offset(Vector2(0.5, 0))).is_equal_approx(Transform2D(Vector2(1, 0), Vector2(0, 1), Vector2(0.5, 0))));
251 }
252 }
253
254 TEST_CASE("[Curve2D] Tessellation") {
255 Ref<Curve2D> curve = memnew(Curve2D);
256 add_sample_curve_points(curve);
257
258 const int default_size = curve->tessellate().size();
259
260 SUBCASE("Increase to max stages should increase num of points") {
261 CHECK(curve->tessellate(6).size() > default_size);
262 }
263
264 SUBCASE("Decrease to max stages should decrease num of points") {
265 CHECK(curve->tessellate(4).size() < default_size);
266 }
267
268 SUBCASE("Increase to tolerance should decrease num of points") {
269 CHECK(curve->tessellate(5, 5).size() < default_size);
270 }
271
272 SUBCASE("Decrease to tolerance should increase num of points") {
273 CHECK(curve->tessellate(5, 3).size() > default_size);
274 }
275
276 SUBCASE("Adding a straight segment should only add the last point to tessellate return array") {
277 curve->add_point(Vector2(0, 100));
278 PackedVector2Array tes = curve->tessellate();
279 CHECK(tes.size() == default_size + 1);
280 CHECK(tes[tes.size() - 1] == Vector2(0, 100));
281 CHECK(tes[tes.size() - 2] == Vector2(0, 50));
282 }
283 }
284
285 TEST_CASE("[Curve2D] Even length tessellation") {
286 Ref<Curve2D> curve = memnew(Curve2D);
287 add_sample_curve_points(curve);
288
289 const int default_size = curve->tessellate_even_length().size();
290
291 // Default tessellate_even_length tolerance_length is 20.0, by adding a 100 units
292 // straight, we expect the total size to be increased by more than 5,
293 // that is, the algo will pick a length < 20.0 and will divide the straight as
294 // well as the curve as opposed to tessellate() which only adds the final point
295 curve->add_point(Vector2(0, 150));
296 CHECK(curve->tessellate_even_length().size() > default_size + 5);
297 }
298
299 } // namespace TestCurve2D