Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_curve_3d.h
10277 views
1
/**************************************************************************/
2
/* test_curve_3d.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 TestCurve3D {
39
40
void add_sample_curve_points(Ref<Curve3D> &curve) {
41
Vector3 p0 = Vector3(0, 0, 0);
42
Vector3 p1 = Vector3(50, 0, 0);
43
Vector3 p2 = Vector3(50, 50, 50);
44
Vector3 p3 = Vector3(0, 50, 0);
45
46
Vector3 control0 = p1 - p0;
47
Vector3 control1 = p3 - p2;
48
49
curve->add_point(p0, Vector3(), control0);
50
curve->add_point(p3, control1, Vector3());
51
}
52
53
TEST_CASE("[Curve3D] Default curve is empty") {
54
const Ref<Curve3D> curve = memnew(Curve3D);
55
CHECK(curve->get_point_count() == 0);
56
}
57
58
TEST_CASE("[Curve3D] Point management") {
59
Ref<Curve3D> curve = memnew(Curve3D);
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(Vector3());
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
Vector3 new_in = Vector3(1, 1, 1);
77
Vector3 new_out = Vector3(1, 1, 1);
78
Vector3 new_pos = Vector3(1, 1, 1);
79
real_t new_tilt = 1;
80
81
curve->add_point(Vector3());
82
83
CHECK(curve->get_point_in(0) != new_in);
84
curve->set_point_in(0, new_in);
85
CHECK(curve->get_point_in(0) == new_in);
86
87
CHECK(curve->get_point_out(0) != new_out);
88
curve->set_point_out(0, new_out);
89
CHECK(curve->get_point_out(0) == new_out);
90
91
CHECK(curve->get_point_position(0) != new_pos);
92
curve->set_point_position(0, new_pos);
93
CHECK(curve->get_point_position(0) == new_pos);
94
95
CHECK(curve->get_point_tilt(0) != new_tilt);
96
curve->set_point_tilt(0, new_tilt);
97
CHECK(curve->get_point_tilt(0) == new_tilt);
98
}
99
}
100
101
TEST_CASE("[Curve3D] Baked") {
102
Ref<Curve3D> curve = memnew(Curve3D);
103
104
SUBCASE("Single Point") {
105
curve->add_point(Vector3());
106
107
CHECK(curve->get_baked_length() == 0);
108
CHECK(curve->get_baked_points().size() == 1);
109
CHECK(curve->get_baked_tilts().size() == 1);
110
CHECK(curve->get_baked_up_vectors().size() == 1);
111
}
112
113
SUBCASE("Straight line") {
114
curve->add_point(Vector3());
115
curve->add_point(Vector3(0, 50, 0));
116
117
CHECK(Math::is_equal_approx(curve->get_baked_length(), 50));
118
CHECK(curve->get_baked_points().size() == 369);
119
CHECK(curve->get_baked_tilts().size() == 369);
120
CHECK(curve->get_baked_up_vectors().size() == 369);
121
}
122
123
SUBCASE("BeziƩr Curve") {
124
add_sample_curve_points(curve);
125
126
real_t len = curve->get_baked_length();
127
real_t n_points = curve->get_baked_points().size();
128
// Curve length should be bigger than a straight line between points
129
CHECK(len > 50);
130
131
SUBCASE("Increase bake interval") {
132
curve->set_bake_interval(10.0);
133
CHECK(curve->get_bake_interval() == 10.0);
134
// Lower resolution should imply less points and smaller length
135
CHECK(curve->get_baked_length() < len);
136
CHECK(curve->get_baked_points().size() < n_points);
137
CHECK(curve->get_baked_tilts().size() < n_points);
138
CHECK(curve->get_baked_up_vectors().size() < n_points);
139
}
140
141
SUBCASE("Disable up vectors") {
142
curve->set_up_vector_enabled(false);
143
CHECK(curve->is_up_vector_enabled() == false);
144
CHECK(curve->get_baked_up_vectors().size() == 0);
145
}
146
}
147
}
148
149
TEST_CASE("[Curve3D] Sampling") {
150
// Sampling over a simple straight line to make assertions simpler
151
Ref<Curve3D> curve = memnew(Curve3D);
152
curve->add_point(Vector3());
153
curve->add_point(Vector3(0, 50, 0));
154
155
SUBCASE("sample") {
156
CHECK(curve->sample(0, 0) == Vector3(0, 0, 0));
157
CHECK(curve->sample(0, 0.5) == Vector3(0, 25, 0));
158
CHECK(curve->sample(0, 1) == Vector3(0, 50, 0));
159
}
160
161
SUBCASE("samplef") {
162
CHECK(curve->samplef(0) == Vector3(0, 0, 0));
163
CHECK(curve->samplef(0.5) == Vector3(0, 25, 0));
164
CHECK(curve->samplef(1) == Vector3(0, 50, 0));
165
}
166
167
SUBCASE("sample_baked, cubic = false") {
168
CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 0, 0))) == Vector3(0, 0, 0));
169
CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 25, 0))) == Vector3(0, 25, 0));
170
CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 50, 0))) == Vector3(0, 50, 0));
171
}
172
173
SUBCASE("sample_baked, cubic = true") {
174
CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 0, 0)), true) == Vector3(0, 0, 0));
175
CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 25, 0)), true) == Vector3(0, 25, 0));
176
CHECK(curve->sample_baked(curve->get_closest_offset(Vector3(0, 50, 0)), true) == Vector3(0, 50, 0));
177
}
178
179
SUBCASE("sample_baked_with_rotation, cubic = false, p_apply_tilt = false") {
180
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 0, 0))) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 0, 0)));
181
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 25, 0))) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 25, 0)));
182
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 50, 0))) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 50, 0)));
183
}
184
185
SUBCASE("sample_baked_with_rotation, cubic = false, p_apply_tilt = true") {
186
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 0, 0)), false, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 0, 0)));
187
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 25, 0)), false, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 25, 0)));
188
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 50, 0)), false, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 50, 0)));
189
}
190
191
SUBCASE("sample_baked_with_rotation, cubic = true, p_apply_tilt = false") {
192
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 0, 0)), true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 0, 0)));
193
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 25, 0)), true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 25, 0)));
194
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 50, 0)), true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 50, 0)));
195
}
196
197
SUBCASE("sample_baked_with_rotation, cubic = true, p_apply_tilt = true") {
198
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 0, 0)), true, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 0, 0)));
199
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 25, 0)), true, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 25, 0)));
200
CHECK(curve->sample_baked_with_rotation(curve->get_closest_offset(Vector3(0, 50, 0)), true, true) == Transform3D(Basis(Vector3(0, 0, -1), Vector3(1, 0, 0), Vector3(0, -1, 0)), Vector3(0, 50, 0)));
201
}
202
203
SUBCASE("sample_baked_tilt") {
204
CHECK(curve->sample_baked_tilt(curve->get_closest_offset(Vector3(0, 0, 0))) == 0);
205
CHECK(curve->sample_baked_tilt(curve->get_closest_offset(Vector3(0, 25, 0))) == 0);
206
CHECK(curve->sample_baked_tilt(curve->get_closest_offset(Vector3(0, 50, 0))) == 0);
207
}
208
209
SUBCASE("sample_baked_up_vector, p_apply_tilt = false") {
210
CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 0, 0))) == Vector3(1, 0, 0));
211
CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 25, 0))) == Vector3(1, 0, 0));
212
CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 50, 0))) == Vector3(1, 0, 0));
213
}
214
215
SUBCASE("sample_baked_up_vector, p_apply_tilt = true") {
216
CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 0, 0)), true) == Vector3(1, 0, 0));
217
CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 25, 0)), true) == Vector3(1, 0, 0));
218
CHECK(curve->sample_baked_up_vector(curve->get_closest_offset(Vector3(0, 50, 0)), true) == Vector3(1, 0, 0));
219
}
220
221
SUBCASE("get_closest_point") {
222
CHECK(curve->get_closest_point(Vector3(0, 0, 0)) == Vector3(0, 0, 0));
223
CHECK(curve->get_closest_point(Vector3(0, 25, 0)) == Vector3(0, 25, 0));
224
CHECK(curve->get_closest_point(Vector3(50, 25, 0)) == Vector3(0, 25, 0));
225
CHECK(curve->get_closest_point(Vector3(0, 50, 0)) == Vector3(0, 50, 0));
226
CHECK(curve->get_closest_point(Vector3(50, 50, 0)) == Vector3(0, 50, 0));
227
CHECK(curve->get_closest_point(Vector3(0, 100, 0)) == Vector3(0, 50, 0));
228
}
229
230
SUBCASE("sample_baked_up_vector, off-axis") {
231
// Regression test for issue #81879
232
Ref<Curve3D> c = memnew(Curve3D);
233
c->add_point(Vector3());
234
c->add_point(Vector3(0, .1, 1));
235
CHECK_LT((c->sample_baked_up_vector(c->get_closest_offset(Vector3(0, 0, .9))) - Vector3(0, 0.995037, -0.099504)).length(), 0.01);
236
}
237
238
SUBCASE("sample_baked_with_rotation, linear curve with control1 = end and control2 = begin") {
239
// Regression test for issue #88923
240
// The Vector3s that aren't relevant to the issue have z = 2.
241
// They're just set to make collisions with corner cases less likely
242
// that involve zero-vector control points.
243
Ref<Curve3D> cross_linear_curve = memnew(Curve3D);
244
cross_linear_curve->add_point(Vector3(), Vector3(-1, 0, 2), Vector3(1, 0, 0));
245
cross_linear_curve->add_point(Vector3(1, 0, 0), Vector3(-1, 0, 0), Vector3(1, 0, 2));
246
CHECK(cross_linear_curve->get_baked_points().size() >= 3);
247
CHECK(cross_linear_curve->sample_baked_with_rotation(cross_linear_curve->get_closest_offset(Vector3(0.5, 0, 0))).is_equal_approx(Transform3D(Basis(Vector3(0, 0, 1), Vector3(0, 1, 0), Vector3(-1, 0, 0)), Vector3(0.5, 0, 0))));
248
}
249
}
250
251
TEST_CASE("[Curve3D] Tessellation") {
252
Ref<Curve3D> curve = memnew(Curve3D);
253
add_sample_curve_points(curve);
254
255
const int default_size = curve->tessellate().size();
256
257
SUBCASE("Increase to max stages should increase num of points") {
258
CHECK(curve->tessellate(6).size() > default_size);
259
}
260
261
SUBCASE("Decrease to max stages should decrease num of points") {
262
CHECK(curve->tessellate(4).size() < default_size);
263
}
264
265
SUBCASE("Increase to tolerance should decrease num of points") {
266
CHECK(curve->tessellate(5, 5).size() < default_size);
267
}
268
269
SUBCASE("Decrease to tolerance should increase num of points") {
270
CHECK(curve->tessellate(5, 3).size() > default_size);
271
}
272
273
SUBCASE("Adding a straight segment should only add the last point to tessellate return array") {
274
curve->add_point(Vector3(0, 100, 0));
275
PackedVector3Array tes = curve->tessellate();
276
CHECK(tes.size() == default_size + 1);
277
CHECK(tes[tes.size() - 1] == Vector3(0, 100, 0));
278
CHECK(tes[tes.size() - 2] == Vector3(0, 50, 0));
279
}
280
}
281
282
TEST_CASE("[Curve3D] Even length tessellation") {
283
Ref<Curve3D> curve = memnew(Curve3D);
284
add_sample_curve_points(curve);
285
286
const int default_size = curve->tessellate_even_length().size();
287
288
// Default tessellate_even_length tolerance_length is 20.0, by adding a 100 units
289
// straight, we expect the total size to be increased by more than 5,
290
// that is, the algo will pick a length < 20.0 and will divide the straight as
291
// well as the curve as opposed to tessellate() which only adds the final point.
292
curve->add_point(Vector3(0, 150, 0));
293
CHECK(curve->tessellate_even_length().size() > default_size + 5);
294
}
295
296
} // namespace TestCurve3D
297
298