Blender V5.3
curves_geometry_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_curves.hh"
10#include "BKE_gtest_base.hh"
11
12#include "testing/testing.h"
13
14namespace blender::bke::tests {
15
16constexpr float EPSILON_FLT32 = 1e-6f;
17
18static CurvesGeometry create_basic_curves(const int points_size, const int curves_size)
19{
20 CurvesGeometry curves(points_size, curves_size);
21
22 const int curve_length = points_size / curves_size;
23 for (const int i : curves.curves_range()) {
24 curves.offsets_for_write()[i] = curve_length * i;
25 }
26 curves.offsets_for_write().last() = points_size;
27
28 for (const int i : curves.points_range()) {
29 curves.positions_for_write()[i] = {float(i), float(i % curve_length), 0.0f};
30 }
31
32 return curves;
33}
34
36
38{
39 CurvesGeometry empty(0, 0);
40 empty.cyclic();
41 EXPECT_TRUE(empty.is_empty());
43}
44
46{
48
49 const int *offsets_data = curves.offsets().data();
50 const float3 *positions_data = curves.positions().data();
51
52 CurvesGeometry other = std::move(curves);
53
54 /* The old curves should be empty, and the offsets are expected to be null. */
55 EXPECT_TRUE(curves.is_empty()); /* NOLINT: bugprone-use-after-move */
56 EXPECT_EQ(curves.curve_offsets, nullptr); /* NOLINT: bugprone-use-after-move */
57
58 /* Just a basic check that the new curves work okay. */
60
61 curves = std::move(other);
62
63 CurvesGeometry second_other(std::move(curves));
64
65 /* The data should not have been reallocated ever. */
66 EXPECT_EQ(second_other.positions().data(), positions_data);
67 EXPECT_EQ(second_other.offsets().data(), offsets_data);
68}
69
71{
73 curves.curve_types_for_write().copy_from({
84 });
85 curves.update_curve_types();
86 const std::array<int, CURVE_TYPES_NUM> &counts = curves.curve_type_counts();
88 EXPECT_EQ(counts[CURVE_TYPE_POLY], 3);
89 EXPECT_EQ(counts[CURVE_TYPE_BEZIER], 1);
90 EXPECT_EQ(counts[CURVE_TYPE_NURBS], 3);
91}
92
94{
96 {
97 EXPECT_FALSE(curves.has_cyclic_curve());
98 }
99 {
100 curves.cyclic_for_write().fill(true);
101 curves.tag_topology_changed();
102 EXPECT_TRUE(curves.has_cyclic_curve());
103 }
104 {
105 curves.cyclic_for_write().fill(false);
106 curves.tag_topology_changed();
107 EXPECT_FALSE(curves.has_cyclic_curve());
108 }
109 {
110 curves.attributes_for_write().remove("cyclic");
111 EXPECT_FALSE(curves.has_cyclic_curve());
112 }
113 {
114 curves.cyclic_for_write().copy_from(
115 {false, true, false, true, false, false, false, false, true, false});
116 curves.tag_topology_changed();
117 EXPECT_TRUE(curves.has_cyclic_curve());
118 }
119}
120
121TEST_F(CurvesGeometryTest, InvalidResolution)
122{
124 curves.curve_types_for_write().copy_from({
129 });
130 curves.update_curve_types();
131 curves.resolution_for_write().fill(0);
132
133 static const Array<int> expected_offsets{0, 10, 20, 30, 40};
134
135 OffsetIndices<int> actual_offsets = curves.evaluated_points_by_curve();
136 for (const int i : actual_offsets.index_range()) {
137 EXPECT_EQ(expected_offsets[i], actual_offsets.data()[i]);
138 }
139}
140
141TEST_F(CurvesGeometryTest, AlignedHandleWithCoincidentVectorHandle)
142{
143 const std::array<int8_t, 3> types_left = {
145 const std::array<int8_t, 3> types_right = {
147 const std::array<float3, 3> positions = {
148 float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f)};
149 std::array<float3, 3> handles_left = positions;
150 std::array<float3, 3> handles_right = positions;
151 handles_right[1] = float3(0.0f, 1.0f, 0.0f);
152
154 false, types_left, types_right, positions, handles_left, handles_right);
155
156 EXPECT_V3_NEAR(handles_right[1], float3(0.0f, 1.0f, 0.0f), EPSILON_FLT32);
157}
158
159TEST_F(CurvesGeometryTest, CatmullRomEvaluation)
160{
162 curves.fill_curve_types(CURVE_TYPE_CATMULL_ROM);
163 curves.resolution_for_write().fill(12);
164 curves.offsets_for_write().last() = 4;
165 curves.cyclic_for_write().fill(false);
166
167 MutableSpan<float3> positions = curves.positions_for_write();
168 positions[0] = {1, 1, 0};
169 positions[1] = {0, 1, 0};
170 positions[2] = {0, 0, 0};
171 positions[3] = {-1, 0, 0};
172
173 Span<float3> evaluated_positions = curves.evaluated_positions();
174 static const Array<float3> result_1{{
175 {1, 1, 0},
176 {0.948495, 1.00318, 0},
177 {0.87963, 1.01157, 0},
178 {0.796875, 1.02344, 0},
179 {0.703704, 1.03704, 0},
180 {0.603588, 1.05064, 0},
181 {0.5, 1.0625, 0},
182 {0.396412, 1.07089, 0},
183 {0.296296, 1.07407, 0},
184 {0.203125, 1.07031, 0},
185 {0.12037, 1.05787, 0},
186 {0.0515046, 1.03501, 0},
187 {0, 1, 0},
188 {-0.0318287, 0.948495, 0},
189 {-0.0462963, 0.87963, 0},
190 {-0.046875, 0.796875, 0},
191 {-0.037037, 0.703704, 0},
192 {-0.0202546, 0.603588, 0},
193 {0, 0.5, 0},
194 {0.0202546, 0.396412, 0},
195 {0.037037, 0.296296, 0},
196 {0.046875, 0.203125, 0},
197 {0.0462963, 0.12037, 0},
198 {0.0318287, 0.0515046, 0},
199 {0, 0, 0},
200 {-0.0515046, -0.0350116, 0},
201 {-0.12037, -0.0578704, 0},
202 {-0.203125, -0.0703125, 0},
203 {-0.296296, -0.0740741, 0},
204 {-0.396412, -0.0708912, 0},
205 {-0.5, -0.0625, 0},
206 {-0.603588, -0.0506366, 0},
207 {-0.703704, -0.037037, 0},
208 {-0.796875, -0.0234375, 0},
209 {-0.87963, -0.0115741, 0},
210 {-0.948495, -0.00318287, 0},
211 {-1, 0, 0},
212 }};
213 for (const int i : evaluated_positions.index_range()) {
214 EXPECT_V3_NEAR(evaluated_positions[i], result_1[i], 1e-5f);
215 }
216
217 /* Changing the positions shouldn't cause the evaluated positions array to be reallocated. */
218 curves.tag_positions_changed();
219 curves.evaluated_positions();
220 EXPECT_EQ(curves.evaluated_positions().data(), evaluated_positions.data());
221
222 /* Call recalculation (which shouldn't happen because low-level accessors don't tag caches). */
223 EXPECT_EQ(evaluated_positions[12].x, 0.0f);
224 EXPECT_EQ(evaluated_positions[12].y, 1.0f);
225
226 positions[0] = {1, 0, 0};
227 positions[1] = {1, 1, 0};
228 positions[2] = {0, 1, 0};
229 positions[3] = {0, 0, 0};
230 curves.cyclic_for_write().fill(true);
231
232 /* Tag topology changed because the new cyclic value is different. */
233 curves.tag_topology_changed();
234
235 /* Retrieve the data again since the size should be larger than last time (one more segment). */
236 evaluated_positions = curves.evaluated_positions();
237 static const Array<float3> result_2{{
238 {1, 0, 0},
239 {1.03819, 0.0515046, 0},
240 {1.06944, 0.12037, 0},
241 {1.09375, 0.203125, 0},
242 {1.11111, 0.296296, 0},
243 {1.12153, 0.396412, 0},
244 {1.125, 0.5, 0},
245 {1.12153, 0.603588, 0},
246 {1.11111, 0.703704, 0},
247 {1.09375, 0.796875, 0},
248 {1.06944, 0.87963, 0},
249 {1.03819, 0.948495, 0},
250 {1, 1, 0},
251 {0.948495, 1.03819, 0},
252 {0.87963, 1.06944, 0},
253 {0.796875, 1.09375, 0},
254 {0.703704, 1.11111, 0},
255 {0.603588, 1.12153, 0},
256 {0.5, 1.125, 0},
257 {0.396412, 1.12153, 0},
258 {0.296296, 1.11111, 0},
259 {0.203125, 1.09375, 0},
260 {0.12037, 1.06944, 0},
261 {0.0515046, 1.03819, 0},
262 {0, 1, 0},
263 {-0.0381944, 0.948495, 0},
264 {-0.0694444, 0.87963, 0},
265 {-0.09375, 0.796875, 0},
266 {-0.111111, 0.703704, 0},
267 {-0.121528, 0.603588, 0},
268 {-0.125, 0.5, 0},
269 {-0.121528, 0.396412, 0},
270 {-0.111111, 0.296296, 0},
271 {-0.09375, 0.203125, 0},
272 {-0.0694444, 0.12037, 0},
273 {-0.0381944, 0.0515046, 0},
274 {0, 0, 0},
275 {0.0515046, -0.0381944, 0},
276 {0.12037, -0.0694444, 0},
277 {0.203125, -0.09375, 0},
278 {0.296296, -0.111111, 0},
279 {0.396412, -0.121528, 0},
280 {0.5, -0.125, 0},
281 {0.603588, -0.121528, 0},
282 {0.703704, -0.111111, 0},
283 {0.796875, -0.09375, 0},
284 {0.87963, -0.0694444, 0},
285 {0.948495, -0.0381944, 0},
286 }};
287 for (const int i : evaluated_positions.index_range()) {
288 EXPECT_V3_NEAR(evaluated_positions[i], result_2[i], 1e-5f);
289 }
290}
291
292TEST_F(CurvesGeometryTest, CatmullRomTwoPointCyclic)
293{
295 curves.fill_curve_types(CURVE_TYPE_CATMULL_ROM);
296 curves.resolution_for_write().fill(12);
297 curves.offsets_for_write().last() = 2;
298 curves.cyclic_for_write().fill(true);
299
300 /* The curve should still be cyclic when there are only two control points. */
301 EXPECT_EQ(curves.evaluated_points_num(), 24);
302}
303
304TEST_F(CurvesGeometryTest, BezierPositionEvaluation)
305{
307 curves.fill_curve_types(CURVE_TYPE_BEZIER);
308 curves.resolution_for_write().fill(12);
309 curves.offsets_for_write().last() = 2;
310
311 MutableSpan<float3> handles_left = curves.handle_positions_left_for_write();
312 MutableSpan<float3> handles_right = curves.handle_positions_right_for_write();
313 MutableSpan<float3> positions = curves.positions_for_write();
314 positions.first() = {-1, 0, 0};
315 positions.last() = {1, 0, 0};
316 handles_right.first() = {-0.5f, 0.5f, 0.0f};
317 handles_left.last() = {0, 0, 0};
318
319 /* Dangling handles shouldn't be used in a non-cyclic curve. */
320 handles_left.first() = {100, 100, 100};
321 handles_right.last() = {100, 100, 100};
322
323 Span<float3> evaluated_positions = curves.evaluated_positions();
324 static const Array<float3> result_1{{
325 {-1, 0, 0},
326 {-0.874711, 0.105035, 0},
327 {-0.747685, 0.173611, 0},
328 {-0.617188, 0.210937, 0},
329 {-0.481481, 0.222222, 0},
330 {-0.338831, 0.212674, 0},
331 {-0.1875, 0.1875, 0},
332 {-0.0257524, 0.15191, 0},
333 {0.148148, 0.111111, 0},
334 {0.335937, 0.0703125, 0},
335 {0.539352, 0.0347222, 0},
336 {0.760127, 0.00954859, 0},
337 {1, 0, 0},
338 }};
339 for (const int i : evaluated_positions.index_range()) {
340 EXPECT_V3_NEAR(evaluated_positions[i], result_1[i], 1e-5f);
341 }
342
343 curves.resize(4, 2);
344 curves.fill_curve_types(CURVE_TYPE_BEZIER);
345 curves.resolution_for_write().fill(9);
346 curves.offsets_for_write().last() = 4;
347 handles_left = curves.handle_positions_left_for_write();
348 handles_right = curves.handle_positions_right_for_write();
349 positions = curves.positions_for_write();
350 positions[2] = {-1, 1, 0};
351 positions[3] = {1, 1, 0};
352 handles_right[2] = {-0.5f, 1.5f, 0.0f};
353 handles_left[3] = {0, 1, 0};
354
355 /* Dangling handles shouldn't be used in a non-cyclic curve. */
356 handles_left[2] = {-100, -100, -100};
357 handles_right[3] = {-100, -100, -100};
358
359 evaluated_positions = curves.evaluated_positions();
360 EXPECT_EQ(evaluated_positions.size(), 20);
361 static const Array<float3> result_2{{
362 {-1, 0, 0},
363 {-0.832647, 0.131687, 0},
364 {-0.66118, 0.201646, 0},
365 {-0.481481, 0.222222, 0},
366 {-0.289438, 0.205761, 0},
367 {-0.0809327, 0.164609, 0},
368 {0.148148, 0.111111, 0},
369 {0.40192, 0.0576133, 0},
370 {0.684499, 0.016461, 0},
371 {1, 0, 0},
372 {-1, 1, 0},
373 {-0.832647, 1.13169, 0},
374 {-0.66118, 1.20165, 0},
375 {-0.481481, 1.22222, 0},
376 {-0.289438, 1.20576, 0},
377 {-0.0809327, 1.16461, 0},
378 {0.148148, 1.11111, 0},
379 {0.40192, 1.05761, 0},
380 {0.684499, 1.01646, 0},
381 {1, 1, 0},
382 }};
383 for (const int i : evaluated_positions.index_range()) {
384 EXPECT_V3_NEAR(evaluated_positions[i], result_2[i], 1e-5f);
385 }
386}
387
388TEST_F(CurvesGeometryTest, BezierGenericEvaluation)
389{
391 curves.fill_curve_types(CURVE_TYPE_BEZIER);
392 curves.resolution_for_write().fill(8);
393 curves.offsets_for_write().last() = 3;
394
395 MutableSpan<float3> handles_left = curves.handle_positions_left_for_write();
396 MutableSpan<float3> handles_right = curves.handle_positions_right_for_write();
397 MutableSpan<float3> positions = curves.positions_for_write();
398 positions.first() = {-1, 0, 0};
399 handles_right.first() = {-1, 1, 0};
400 handles_left[1] = {0, 0, 0};
401 positions[1] = {1, 0, 0};
402 handles_right[1] = {2, 0, 0};
403 handles_left.last() = {1, 1, 0};
404 positions.last() = {2, 1, 0};
405
406 /* Dangling handles shouldn't be used in a non-cyclic curve. */
407 handles_left.first() = {100, 100, 100};
408 handles_right.last() = {100, 100, 100};
409
410 Span<float3> evaluated_positions = curves.evaluated_positions();
411 static const Array<float3> result_1{{
412 {-1.0f, 0.0f, 0.0f},
413 {-0.955078f, 0.287109f, 0.0f},
414 {-0.828125f, 0.421875f, 0.0f},
415 {-0.630859f, 0.439453f, 0.0f},
416 {-0.375f, 0.375f, 0.0f},
417 {-0.0722656f, 0.263672f, 0.0f},
418 {0.265625f, 0.140625f, 0.0f},
419 {0.626953f, 0.0410156f, 0.0f},
420 {1.0f, 0.0f, 0.0f},
421 {1.28906f, 0.0429688f, 0.0f},
422 {1.4375f, 0.15625f, 0.0f},
423 {1.49219f, 0.316406f, 0.0f},
424 {1.5f, 0.5f, 0.0f},
425 {1.50781f, 0.683594f, 0.0f},
426 {1.5625f, 0.84375f, 0.0f},
427 {1.71094f, 0.957031f, 0.0f},
428 {2.0f, 1.0f, 0.0f},
429 }};
430 for (const int i : evaluated_positions.index_range()) {
431 EXPECT_V3_NEAR(evaluated_positions[i], result_1[i], 1e-5f);
432 }
433
434 Array<float> radii{{0.0f, 1.0f, 2.0f}};
435 Array<float> evaluated_radii(17);
436 curves.interpolate_to_evaluated(0, radii.as_span(), evaluated_radii.as_mutable_span());
437 static const Array<float> result_2{{
438 0.0f,
439 0.125f,
440 0.25f,
441 0.375f,
442 0.5f,
443 0.625f,
444 0.75f,
445 0.875f,
446 1.0f,
447 1.125f,
448 1.25f,
449 1.375f,
450 1.5f,
451 1.625f,
452 1.75f,
453 1.875f,
454 2.0f,
455 }};
456 for (const int i : evaluated_radii.index_range()) {
457 EXPECT_NEAR(evaluated_radii[i], result_2[i], 1e-6f);
458 }
459}
460
461/* -------------------------------------------------------------------- */
464
465static CurvesGeometry create_single_nurbs(const int num_points)
466{
467 CurvesGeometry curves(num_points, 1);
468 curves.fill_curve_types(CURVE_TYPE_NURBS);
469 curves.resolution_for_write().fill(10);
470 curves.offsets_for_write().last() = num_points;
471 return curves;
472}
473
474TEST_F(CurvesGeometryTest, NURBSEvaluation)
475{
477 MutableSpan<float3> positions = curves.positions_for_write();
478 positions[0] = {1, 1, 0};
479 positions[1] = {0, 1, 0};
480 positions[2] = {0, 0, 0};
481 positions[3] = {-1, 0, 0};
482
483 Span<float3> evaluated_positions = curves.evaluated_positions();
484 static const Array<float3> result_1{{
485 {0.166667, 0.833333, 0},
486 {0.121333, 0.778667, 0},
487 {0.084, 0.716, 0},
488 {0.0526667, 0.647333, 0},
489 {0.0253333, 0.574667, 0},
490 {0, 0.5, 0},
491 {-0.0253333, 0.425333, 0},
492 {-0.0526667, 0.352667, 0},
493 {-0.084, 0.284, 0},
494 {-0.121333, 0.221333, 0},
495 {-0.166667, 0.166667, 0},
496 }};
497 for (const int i : evaluated_positions.index_range()) {
498 EXPECT_V3_NEAR(evaluated_positions[i], result_1[i], 1e-5f);
499 }
500
501 /* Test a cyclic curve. */
502 curves.cyclic_for_write().fill(true);
503 curves.tag_topology_changed();
504 evaluated_positions = curves.evaluated_positions();
505 static const Array<float3> result_2{{
506 {0.166667, 0.833333, 0}, {0.121333, 0.778667, 0},
507 {0.084, 0.716, 0}, {0.0526667, 0.647333, 0},
508 {0.0253333, 0.574667, 0}, {0, 0.5, 0},
509 {-0.0253333, 0.425333, 0}, {-0.0526667, 0.352667, 0},
510 {-0.084, 0.284, 0}, {-0.121333, 0.221333, 0},
511 {-0.166667, 0.166667, 0}, {-0.221, 0.121667, 0},
512 {-0.281333, 0.0866667, 0}, {-0.343667, 0.0616666, 0},
513 {-0.404, 0.0466667, 0}, {-0.458333, 0.0416667, 0},
514 {-0.502667, 0.0466667, 0}, {-0.533, 0.0616666, 0},
515 {-0.545333, 0.0866667, 0}, {-0.535667, 0.121667, 0},
516 {-0.5, 0.166667, 0}, {-0.436, 0.221334, 0},
517 {-0.348, 0.284, 0}, {-0.242, 0.352667, 0},
518 {-0.124, 0.425333, 0}, {0, 0.5, 0},
519 {0.124, 0.574667, 0}, {0.242, 0.647333, 0},
520 {0.348, 0.716, 0}, {0.436, 0.778667, 0},
521 {0.5, 0.833333, 0}, {0.535667, 0.878334, 0},
522 {0.545333, 0.913333, 0}, {0.533, 0.938333, 0},
523 {0.502667, 0.953333, 0}, {0.458333, 0.958333, 0},
524 {0.404, 0.953333, 0}, {0.343667, 0.938333, 0},
525 {0.281333, 0.913333, 0}, {0.221, 0.878333, 0},
526 }};
527 for (const int i : evaluated_positions.index_range()) {
528 EXPECT_V3_NEAR(evaluated_positions[i], result_2[i], 1e-5f);
529 }
530
531 /* Test a circular cyclic curve with weights. */
532 positions[0] = {1, 0, 0};
533 positions[1] = {1, 1, 0};
534 positions[2] = {0, 1, 0};
535 positions[3] = {0, 0, 0};
536 curves.nurbs_weights_for_write().fill(1.0f);
537 curves.nurbs_weights_for_write()[0] = 4.0f;
538 curves.tag_positions_changed();
539 static const Array<float3> result_3{{
540 {0.888889, 0.555556, 0}, {0.837792, 0.643703, 0}, {0.773885, 0.727176, 0},
541 {0.698961, 0.800967, 0}, {0.616125, 0.860409, 0}, {0.529412, 0.901961, 0},
542 {0.443152, 0.923773, 0}, {0.361289, 0.925835, 0}, {0.286853, 0.909695, 0},
543 {0.221722, 0.877894, 0}, {0.166667, 0.833333, 0}, {0.122106, 0.778278, 0},
544 {0.0903055, 0.713148, 0}, {0.0741654, 0.638711, 0}, {0.0762274, 0.556847, 0},
545 {0.0980392, 0.470588, 0}, {0.139591, 0.383875, 0}, {0.199032, 0.301039, 0},
546 {0.272824, 0.226114, 0}, {0.356297, 0.162208, 0}, {0.444444, 0.111111, 0},
547 {0.531911, 0.0731388, 0}, {0.612554, 0.0468976, 0}, {0.683378, 0.0301622, 0},
548 {0.74391, 0.0207962, 0}, {0.794872, 0.017094, 0}, {0.837411, 0.017839, 0},
549 {0.872706, 0.0222583, 0}, {0.901798, 0.0299677, 0}, {0.925515, 0.0409445, 0},
550 {0.944444, 0.0555556, 0}, {0.959056, 0.0744855, 0}, {0.970032, 0.0982019, 0},
551 {0.977742, 0.127294, 0}, {0.982161, 0.162589, 0}, {0.982906, 0.205128, 0},
552 {0.979204, 0.256091, 0}, {0.969838, 0.316622, 0}, {0.953102, 0.387446, 0},
553 {0.926861, 0.468089, 0},
554 }};
555 evaluated_positions = curves.evaluated_positions();
556 for (const int i : evaluated_positions.index_range()) {
557 EXPECT_V3_NEAR(evaluated_positions[i], result_3[i], 1e-5f);
558 }
559}
560
561TEST_F(CurvesGeometryTest, NURBSEvaluateZeroOrderBezierDeg3)
562{
564 curves.nurbs_knots_modes_for_write().fill(NURBS_KNOT_MODE_ENDPOINT_BEZIER);
565
566 MutableSpan<float3> positions = curves.positions_for_write();
567 positions[0] = {2.33f, 1.45f, -0.4f};
568 positions[1] = {0.03f, 0.78f, -0.3f};
569 positions[2] = {0.0f, -0.29f, -0.2f};
570 positions[3] = {-5.12f, 0.0f, -0.1f};
571
572 for (int8_t i = -1; i < 2; i++) {
573 curves.nurbs_orders_for_write().fill(i);
574 curves.tag_topology_changed();
575 Span<float3> evaluated_positions = curves.evaluated_positions();
576 EXPECT_NEAR_SPAN<float>(
577 evaluated_positions.cast<float>(), positions.as_span().cast<float>(), EPSILON_FLT32);
578 }
579}
580
581TEST_F(CurvesGeometryTest, NURBSEvaluateZeroOrderClampedDeg3)
582{
584 curves.nurbs_knots_modes_for_write().fill(NURBS_KNOT_MODE_ENDPOINT);
585
586 MutableSpan<float3> positions = curves.positions_for_write();
587 positions[0] = {2.33f, 1.45f, 0.4f};
588 positions[1] = {0.03f, 0.78f, 0.3f};
589 positions[2] = {0.0f, -0.29f, 0.2f};
590 positions[3] = {-5.12f, 0.0f, 0.1f};
591
592 for (int8_t i = -1; i < 2; i++) {
593 curves.nurbs_orders_for_write().fill(i);
594 curves.tag_topology_changed();
595 Span<float3> evaluated_positions = curves.evaluated_positions();
596 EXPECT_NEAR_SPAN<float>(
597 evaluated_positions.cast<float>(), positions.as_span().cast<float>(), EPSILON_FLT32);
598 }
599}
600
602
603/* -------------------------------------------------------------------- */
606
607TEST_F(CurvesGeometryTest, BasisCacheBezierSegmentDeg2)
608{
609 const int order = 3;
610 const int point_count = 3;
611 const int resolution = 3;
612 const bool is_cyclic = false;
613
614 const std::array<float, 6> knots_data{0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f};
615 const Span<float> knots = Span<float>(knots_data);
616
617 /* Expectation */
618 auto fn_Ni2_span = [](MutableSpan<float> Ni2, const float u) {
619 const float nu = 1.0f - u;
620 Ni2[0] = nu * nu;
621 Ni2[1] = 2.0f * u * nu;
622 Ni2[2] = u * u;
623 };
624
625 std::array<float, 12> expected_data;
626 MutableSpan<float> expectation = MutableSpan<float>(expected_data);
627 fn_Ni2_span(expectation.slice(0, 3), 0.0f);
628 fn_Ni2_span(expectation.slice(3, 3), 1.0f / 3.0f);
629 fn_Ni2_span(expectation.slice(6, 3), 2.0f / 3.0f);
630 fn_Ni2_span(expectation.slice(9, 3), 1.0f);
631
632 /* Test */
633 const int evaluated_num = curves::nurbs::calculate_evaluated_num(
634 point_count, order, is_cyclic, resolution, KnotsMode::NURBS_KNOT_MODE_CUSTOM, knots);
635 EXPECT_EQ(evaluated_num, resolution + 1);
636
639 evaluated_num,
640 order,
641 resolution,
642 is_cyclic,
644 knots,
645 cache);
646 EXPECT_EQ_SPAN<float>(expectation, cache.weights);
647}
648
649TEST_F(CurvesGeometryTest, BasisCacheNonUniformDeg2)
650{
651 const int order = 3;
652 const int point_count = 8;
653 const int resolution = 3;
654 const bool is_cyclic = false;
655
656 const std::array<float, 11> knots_data{
657 0.0f, 0.0f, 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 4.0f, 5.0f, 5.0f, 5.0f};
658 const Span<float> knots = Span<float>(knots_data);
659
660 /* Expectation */
661 auto fn_Ni2_span0 = [](MutableSpan<float> Ni2, const float u) {
662 Ni2[0] = square_f(1.0f - u);
663 Ni2[1] = 2.0f * u - 1.5f * square_f(u);
664 Ni2[2] = square_f(u) / 2.0f;
665 };
666 auto fn_Ni2_span1 = [](MutableSpan<float> Ni2, float u) {
667 Ni2[0] = square_f(2.0f - u) / 2.0f;
668 Ni2[1] = -1.5f + 3 * u - square_f(u);
669 Ni2[2] = square_f(u - 1.0f) / 2.0f;
670 };
671 auto fn_Ni2_span2 = [](MutableSpan<float> Ni2, float u) {
672 Ni2[0] = square_f(3.0f - u) / 2.0f;
673 Ni2[1] = -5.5f + 5.0f * u - square_f(u);
674 Ni2[2] = square_f(u - 2.0f) / 2.0f;
675 };
676 auto fn_Ni2_span3 = [](MutableSpan<float> Ni2, float u) {
677 Ni2[0] = square_f(4.0f - u) / 2.0f;
678 Ni2[1] = -16.0f + 10.0f * u - 1.5f * square_f(u);
679 Ni2[2] = square_f(u - 3.0f);
680 };
681 auto fn_Ni2_span4 = [](MutableSpan<float> Ni2, float u) {
682 Ni2[0] = square_f(5.0f - u);
683 Ni2[1] = 2.0f * (u - 4.0f) * (5.0f - u);
684 Ni2[2] = square_f(u - 4.0f);
685 };
686
687 std::array<float, 48> expected_data;
688 MutableSpan<float> expectation = MutableSpan<float>(expected_data);
689 for (int i = 0; i < 3; i++) {
690 const float du = i / 3.0f;
691 const int step = i * 3;
692 fn_Ni2_span0(expectation.slice(step, 3), du);
693 fn_Ni2_span1(expectation.slice(step + 9, 3), 1.0f + du);
694 fn_Ni2_span2(expectation.slice(step + 18, 3), 2.0f + du);
695 fn_Ni2_span3(expectation.slice(step + 27, 3), 3.0f + du);
696 fn_Ni2_span4(expectation.slice(step + 36, 3), 4.0f + du);
697 }
698 fn_Ni2_span4(expectation.slice(45, 3), 5.0f);
699
700 /* Test */
701 const int evaluated_num = curves::nurbs::calculate_evaluated_num(
702 point_count, order, is_cyclic, resolution, KnotsMode::NURBS_KNOT_MODE_CUSTOM, knots);
703 EXPECT_EQ(evaluated_num, 5 * resolution + 1);
704
707 evaluated_num,
708 order,
709 resolution,
710 is_cyclic,
712 knots,
713 cache);
714 EXPECT_NEAR_SPAN<float>(expectation, cache.weights, EPSILON_FLT32);
715}
716
718
720
721TEST_F(KnotVectorTest, KnotVectorUniform)
722{
723 constexpr int8_t order = 5;
724 constexpr int points_num = 7;
725
726 Vector<float> knots(curves::nurbs::knots_num(points_num, order, false));
728 points_num, KnotsMode::NURBS_KNOT_MODE_NORMAL, order, false, knots);
729
731 EXPECT_EQ_SPAN<int>(Span({1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}), multiplicity);
732}
733
734TEST_F(KnotVectorTest, KnotVectorUniformClamped)
735{
736 constexpr int8_t order = 3;
737 constexpr int points_num = 7;
738
739 Vector<float> knots(curves::nurbs::knots_num(points_num, order, false));
741 points_num, KnotsMode::NURBS_KNOT_MODE_ENDPOINT, order, false, knots);
742
744 EXPECT_EQ_SPAN<int>(Span({3, 1, 1, 1, 1, 3}), multiplicity);
745}
746
747/* -------------------------------------------------------------------- */
750
751TEST_F(KnotVectorTest, KnotVectorBezierClampedSegmentDeg2)
752{
753 constexpr int8_t order = 3;
754 constexpr int points_num = 3;
755
756 Vector<float> knots(curves::nurbs::knots_num(points_num, order, false));
758 points_num, KnotsMode::NURBS_KNOT_MODE_ENDPOINT_BEZIER, order, false, knots);
759
761 EXPECT_EQ_SPAN<int>(Span({3, 3}), multiplicity);
762}
763
764TEST_F(KnotVectorTest, KnotVectorBezierClampedSegmentDeg4)
765{
766 constexpr int8_t order = 5;
767 constexpr int points_num = 5;
768
769 Vector<float> knots(curves::nurbs::knots_num(points_num, order, false));
771 points_num, KnotsMode::NURBS_KNOT_MODE_ENDPOINT_BEZIER, order, false, knots);
772
774 EXPECT_EQ_SPAN<int>(Span({5, 5}), multiplicity);
775}
776
777TEST_F(KnotVectorTest, KnotVectorBezierClampedDeg2)
778{
779 constexpr int8_t order = 3;
780 constexpr int points_num = 9;
781
782 Vector<float> knots(curves::nurbs::knots_num(points_num, order, false));
784 points_num, KnotsMode::NURBS_KNOT_MODE_ENDPOINT_BEZIER, order, false, knots);
785
787 EXPECT_EQ_SPAN<int>(Span({3, 2, 2, 2, 3}), multiplicity);
788}
789
790TEST_F(KnotVectorTest, KnotVectorBezierClampedUnevenDeg2)
791{
792 constexpr int8_t order = 3;
793 constexpr int points_num = 8;
794
795 Vector<float> knots(curves::nurbs::knots_num(points_num, order, false));
797 points_num, KnotsMode::NURBS_KNOT_MODE_ENDPOINT_BEZIER, order, false, knots);
798
800 EXPECT_EQ_SPAN<int>(Span({3, 2, 2, 4}), multiplicity);
801}
802
803TEST_F(KnotVectorTest, KnotVectorBezierClampedDeg4)
804{
805 constexpr int8_t order = 5;
806 constexpr int points_num = 13;
807
808 Vector<float> knots(curves::nurbs::knots_num(points_num, order, false));
810 points_num, KnotsMode::NURBS_KNOT_MODE_ENDPOINT_BEZIER, order, false, knots);
811
813 EXPECT_EQ_SPAN<int>(Span({5, 4, 4, 5}), multiplicity);
814}
815
816TEST_F(KnotVectorTest, KnotVectorBezierClampedUnevenDeg4)
817{
818 constexpr int8_t order = 5;
819 constexpr int points_num[4] = {12, 11, 10, 9};
820 const std::array<std::array<int, 3>, 4> expectation = {std::array<int, 3>{5, 4, 8},
821 std::array<int, 3>{5, 4, 7},
822 std::array<int, 3>{5, 4, 6},
823 std::array<int, 3>{5, 4, 5}};
824
825 for (int i = 0; i < expectation.size(); i++) {
826 Vector<float> knots(curves::nurbs::knots_num(points_num[i], order, false));
828 points_num[i], KnotsMode::NURBS_KNOT_MODE_ENDPOINT_BEZIER, order, false, knots);
829
831 EXPECT_EQ_SPAN<int>(Span(expectation[i]), multiplicity);
832 }
833}
834
835TEST_F(KnotVectorTest, KnotVectorCircleCyclicUnevenDeg2)
836{
837 constexpr int8_t order = 3;
838 constexpr int points_num = 8;
839
840 Vector<float> knots(curves::nurbs::knots_num(points_num, order, true));
842 points_num, KnotsMode::NURBS_KNOT_MODE_ENDPOINT_BEZIER, order, true, knots);
843
845 EXPECT_EQ_SPAN<int>(Span({1, 2, 2, 2, 2, 2, 2}), multiplicity);
846}
847
848TEST_F(KnotVectorTest, KnotVectorBezierClampedCyclicUnevenDeg4)
849{
850 constexpr int8_t order = 5;
851 constexpr int points_num[4] = {12, 11, 10, 9};
852 const std::array<std::array<int, 6>, 4> expectation = {std::array<int, 6>{1, 4, 4, 4, 4, 4},
853 std::array<int, 6>{1, 4, 4, 3, 4, 4},
854 std::array<int, 6>{1, 4, 4, 2, 4, 4},
855 std::array<int, 6>{1, 4, 4, 1, 4, 4}};
856
857 for (int i = 0; i < expectation.size(); i++) {
858 Vector<float> knots(curves::nurbs::knots_num(points_num[i], order, true));
860 points_num[i], KnotsMode::NURBS_KNOT_MODE_ENDPOINT_BEZIER, order, true, knots);
861
863 EXPECT_EQ_SPAN<int>(Span(expectation[i]), multiplicity);
864 }
865}
866
868
869/* -------------------------------------------------------------------- */
872
873TEST_F(KnotVectorTest, KnotVectorBezierSegmentDeg2)
874{
875 constexpr int8_t order = 4;
876 constexpr int points_num = 4;
877
878 Vector<float> knots(curves::nurbs::knots_num(points_num, order, false));
880 points_num, KnotsMode::NURBS_KNOT_MODE_BEZIER, order, false, knots);
881
883 EXPECT_EQ_SPAN<int>(Span({2, 3, 3}), multiplicity);
884}
885
886TEST_F(KnotVectorTest, KnotVectorBezierUnevenDeg2)
887{
888 constexpr int8_t order = 3;
889 constexpr int points_num[4] = {8, 7, 6, 5};
890 const std::array<std::array<int, 6>, 4> expectation = {std::array<int, 6>{2, 2, 2, 2, 2, 1},
891 std::array<int, 6>{2, 2, 2, 2, 2, -1},
892 std::array<int, 6>{2, 2, 2, 2, 1, -1},
893 std::array<int, 6>{2, 2, 2, 2, -1, -1}};
894
895 for (int i = 0; i < expectation.size(); i++) {
896 Vector<float> knots(curves::nurbs::knots_num(points_num[i], order, false));
898 points_num[i], KnotsMode::NURBS_KNOT_MODE_BEZIER, order, false, knots);
899
901 EXPECT_EQ_SPAN<int>(Span(expectation[i].data(), multiplicity.size()), multiplicity);
902 }
903}
904
905TEST_F(KnotVectorTest, KnotVectorBezierUnevenDeg4)
906{
907 constexpr int8_t order = 5;
908 constexpr int points_num[6] = {14, 13, 12, 11, 10, 9};
909 const std::array<std::array<int, 6>, 6> expectation = {std::array<int, 6>{2, 4, 4, 4, 4, 1},
910 std::array<int, 6>{2, 4, 4, 4, 4, -1},
911 std::array<int, 6>{2, 4, 4, 4, 3, -1},
912 std::array<int, 6>{2, 4, 4, 4, 2, -1},
913 std::array<int, 6>{2, 4, 4, 4, 1, -1},
914 std::array<int, 6>{2, 4, 4, 4, -1, -1}};
915
916 for (int i = 0; i < expectation.size(); i++) {
917 Vector<float> knots(curves::nurbs::knots_num(points_num[i], order, false));
919 points_num[i], KnotsMode::NURBS_KNOT_MODE_BEZIER, order, false, knots);
920
922 EXPECT_EQ_SPAN<int>(Span(expectation[i].data(), multiplicity.size()), multiplicity);
923 }
924}
925
926TEST_F(KnotVectorTest, KnotVectorBezierCyclicUnevenDeg4)
927{
928 constexpr int8_t order = 5;
929 constexpr int points_num[4] = {12, 11, 10, 9};
930 const std::array<std::array<int, 6>, 4> expectation = {std::array<int, 6>{2, 4, 4, 4, 4, 3},
931 std::array<int, 6>{2, 4, 4, 3, 4, 3},
932 std::array<int, 6>{2, 4, 4, 2, 4, 3},
933 std::array<int, 6>{2, 4, 5, 4, 3, -1}};
934
935 for (int i = 0; i < expectation.size(); i++) {
936 Vector<float> knots(curves::nurbs::knots_num(points_num[i], order, true));
938 points_num[i], KnotsMode::NURBS_KNOT_MODE_BEZIER, order, true, knots);
939
941 EXPECT_EQ_SPAN<int>(Span(expectation[i].data(), multiplicity.size()), multiplicity);
942 }
943}
944
946
947} // namespace blender::bke::tests
Low-level operations for curves.
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
IndexRange index_range() const
Definition BLI_array.hh:371
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
constexpr T & first() const
Definition BLI_span.hh:685
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
Span< NewT > constexpr cast() const
Definition BLI_span.hh:422
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
int64_t size() const
std::optional< Bounds< float3 > > bounds_min_max(bool use_radius=true) const
VArray< bool > cyclic() const
Span< float3 > positions() const
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
#define EXPECT_NEAR(result, expect, threshold)
void calculate_auto_handles(bool cyclic, Span< int8_t > types_left, Span< int8_t > types_right, Span< float3 > positions, MutableSpan< float3 > positions_left, MutableSpan< float3 > positions_right)
Vector< int > calculate_multiplicity_sequence(Span< float > knots)
int calculate_evaluated_num(int points_num, int8_t order, bool cyclic, int resolution, KnotsMode knots_mode, Span< float > knots)
void calculate_knots(int points_num, KnotsMode mode, int8_t order, bool cyclic, MutableSpan< float > knots)
void calculate_basis_cache(int points_num, int evaluated_num, int8_t order, int resolution, bool cyclic, KnotsMode knots_mode, Span< float > knots, BasisCache &basis_cache)
int knots_num(int points_num, int8_t order, bool cyclic)
static CurvesGeometry create_single_nurbs(const int num_points)
constexpr float EPSILON_FLT32
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
static CurvesGeometry create_basic_curves(const int points_size, const int curves_size)
@ NURBS_KNOT_MODE_ENDPOINT_BEZIER
@ NURBS_KNOT_MODE_BEZIER
@ NURBS_KNOT_MODE_ENDPOINT
@ NURBS_KNOT_MODE_NORMAL
@ NURBS_KNOT_MODE_CUSTOM
BMesh const char void * data
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
@ CURVE_TYPE_CATMULL_ROM
MINLINE float square_f(float a)
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float