Blender V5.3
obj_nurbs_io_tests.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023-2025 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4#include "BLI_array_utils.hh"
5#include "BLI_string.hh"
6
7#include "BKE_appdir.hh"
8#include "BKE_curves.hh"
9#include "BKE_gtest_base.hh"
10
12#include "obj_export_nurbs.hh"
13#include "obj_exporter.hh"
14#include "obj_importer.hh"
15
16#include "testing/testing.h"
17
18namespace blender::io::obj {
19
20static OBJExportParams default_export_params(const std::string &filepath)
21{
22 OBJExportParams params;
25 STRNCPY(params.filepath, filepath.c_str());
26 return params;
27}
28
29static OBJImportParams default_import_params(const std::string &filepath)
30{
31 OBJImportParams params;
34 STRNCPY(params.filepath, filepath.c_str());
35 return params;
36}
37
39 public:
40 void write_curves(const Span<std::unique_ptr<IOBJCurve>> curves, OBJExportParams params)
41 {
42 export_objects(params, Span<std::unique_ptr<OBJMesh>>(nullptr, 0), curves, params.filepath);
43 }
44
45 void write_curves(const std::unique_ptr<IOBJCurve> &curve, OBJExportParams params)
46 {
47 Span<std::unique_ptr<IOBJCurve>> span(&curve, 1);
48 write_curves(span, params);
49 }
50
52 {
53 float4x4 identity = float4x4::identity();
54 std::unique_ptr<IOBJCurve> curve_wrapper(new OBJCurves(curve, identity, "test"));
55 write_curves(curve_wrapper, params);
56 }
57
59 {
61 importer_geometry(params, geoms);
62 return geoms;
63 }
64
66 {
67 bke::CurvesGeometry curves(points.size(), 1);
68 curves.offsets_for_write()[0] = 0;
69 curves.offsets_for_write()[1] = points.size();
70 curves.cyclic_for_write()[0] = cyclic;
71 curves.positions_for_write().copy_from(points);
72 return curves;
73 }
74
76 Span<float3> points, Span<float> weights, bool cyclic, int8_t order, KnotsMode mode)
77 {
79 curves.nurbs_orders_for_write()[0] = order;
80 curves.nurbs_knots_modes_for_write()[0] = int8_t(mode);
81 curves.nurbs_weights_for_write().copy_from(weights);
82
83 return curves;
84 }
85
87 bool cyclic,
88 int8_t order,
89 KnotsMode mode)
90 {
92 curves.nurbs_orders_for_write()[0] = order;
93 curves.nurbs_knots_modes_for_write()[0] = int8_t(mode);
94 curves.nurbs_weights_for_write().fill(1.0f);
95
96 return curves;
97 }
98
99 void run_nurbs_test(const Span<float3> points,
100 const int8_t order,
101 const KnotsMode mode,
102 const bool cyclic,
103 bke::CurvesGeometry &src_curve,
104 const bke::CurvesGeometry *&result_curve,
105 Span<float3> expected_points = Span<float3>(),
106 const KnotsMode *expected_mode = nullptr,
107 const bool *expected_cyclic = nullptr)
108 {
109 BKE_tempdir_init(nullptr);
110 std::string tempdir = std::string(BKE_tempdir_base());
111 std::string out_file_path = tempdir + BLI_path_basename("io_obj/tmp_6f5273f4.obj");
112
113 /* Write/Read */
114 src_curve = OBJCurvesTest::create_nurbs(points, cyclic, order, mode);
115 ASSERT_TRUE(src_curve.cyclic()[0] == cyclic); /* Validate test function */
116
117 write_curves(src_curve, default_export_params(out_file_path));
118
120
121 ASSERT_TRUE(result.size() == 1);
122 result_curve = &result[0].get_curves()->geometry.wrap();
123
124 /* Validate properties */
125 EXPECT_EQ(result_curve->nurbs_orders()[0], order);
126 EXPECT_EQ(result_curve->cyclic()[0], expected_cyclic ? *expected_cyclic : cyclic);
127 EXPECT_EQ(result_curve->nurbs_knots_modes()[0], int8_t(expected_mode ? *expected_mode : mode));
128
129 const Span<float3> result_points = result_curve->positions();
130 expected_points = expected_points.size() ? expected_points : points;
131 ASSERT_EQ(expected_points.size(), result_points.size());
132 EXPECT_NEAR_ARRAY_ND(
133 expected_points.data(), result_points.data(), expected_points.size(), 3, 1e-4);
134
135 if (result_curve->nurbs_knots_modes()[0] != KnotsMode::NURBS_KNOT_MODE_CUSTOM) {
136 ASSERT_TRUE(result_curve->custom_knots == nullptr);
137 }
138 }
139};
140
141const std::array<float3, 13> position_array{float3{1.0f, -1.0f, 2.0f},
142 float3{2.0f, -2.0f, 4.0f},
143 float3{3.0f, -3.0f, 6.0f},
144 float3{4.0f, -4.0f, 8.0f},
145 float3{5.0f, -5.0f, 10.0f},
146 float3{6.0f, -6.0f, 12.0f},
147 float3{7.0f, -7.0f, 14.0f},
148 float3{1.0f / 4.0f, -2.0f, 3.0f / 6.0f},
149 float3{1.0f / 6.0f, -3.0f, 3.0f / 9.0f},
150 float3{1.0f / 8.0f, -4.0f, 3.0f / 12.0f},
151 float3{1.0f / 5.0f, -5.0f, 3.0f / 11.0f},
152 float3{1.0f / 3.0f, -6.0f, 3.0f / 10.0f},
153 float3{1.0f / 2.0f, -7.0f, 3.0f / 9.0f}};
155
156/* -------------------------------------------------------------------- */
159
160TEST_F(OBJCurvesTest, nurbs_io_uniform_polyline)
161{
162 const int8_t order = 2;
164 const bool cyclic = false;
165 const Span<float3> positions = position_data.slice(0, 5);
166
168
171 run_nurbs_test(positions, order, mode, cyclic, src, result, positions, &expected_mode);
172
173 /* Validate uniform knots, don't do this in general as it only verifies the knot generator
174 * `bke::curves::nurbs::calculate_knots`. */
175 Vector<float> knot_buffer(bke::curves::nurbs::knots_num(positions.size(), order, cyclic));
176 bke::curves::nurbs::calculate_knots(positions.size(), mode, order, cyclic, knot_buffer);
178 knot_buffer);
179
180 std::array<int, 7> expected_mult;
181 std::fill(expected_mult.begin(), expected_mult.end(), 1);
182 EXPECT_EQ_SPAN<int>(multiplicity, expected_mult);
183}
184
185TEST_F(OBJCurvesTest, nurbs_io_uniform_deg5)
186{
187 const int8_t order = 6;
189 const Span<float3> positions = position_data.slice(0, 8);
190
193 run_nurbs_test(positions, order, mode, false, src, result);
194}
195
196TEST_F(OBJCurvesTest, nurbs_io_uniform_clamped_polyline)
197{
198 const int8_t order = 2;
200 const Span<float3> positions = position_data.slice(0, 5);
201
204 run_nurbs_test(positions, order, mode, false, src, result);
205}
206
207TEST_F(OBJCurvesTest, nurbs_io_uniform_endpoint_clamped_deg3)
208{
209 const int8_t order = 3;
211 const Span<float3> positions = position_data.slice(0, 5);
212
215 run_nurbs_test(positions, order, mode, false, src, result);
216}
217
218TEST_F(OBJCurvesTest, nurbs_io_uniform_endpoint_clamped_deg5)
219{
220 const int8_t order = 6;
222 const Span<float3> positions = position_data.slice(0, 8);
223
226 run_nurbs_test(positions, order, mode, false, src, result);
227}
228
229TEST_F(OBJCurvesTest, nurbs_io_uniform_cyclic_polyline)
230{
231 const int8_t order = 2;
233 const Span<float3> positions = position_data.slice(0, 5);
234
236
239 run_nurbs_test(positions, order, mode, true, src, result, positions, &expected_mode);
240}
241
242TEST_F(OBJCurvesTest, nurbs_io_uniform_cyclic_deg4)
243{
244 const int8_t order = 5;
246 const Span<float3> positions = position_data.slice(0, 8);
247
250 run_nurbs_test(positions, order, mode, true, src, result);
251}
252
253TEST_F(OBJCurvesTest, nurbs_io_uniform_cyclic_clamped_deg4)
254{
255 const int8_t order = 5;
257 const Span<float3> positions = position_data.slice(0, 12);
258
261 run_nurbs_test(positions, order, mode, true, src, result);
262}
263
265
266/* -------------------------------------------------------------------- */
269
270TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_single_segment_deg2)
271{
272 const int8_t order = 3;
274 const Span<float3> positions = position_data.slice(0, 3);
275
278 run_nurbs_test(positions, order, mode, false, src, result);
279}
280
281TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_single_segment_deg4)
282{
283 const int8_t order = 5;
285 const Span<float3> positions = position_data.slice(0, 5);
286
289 run_nurbs_test(positions, order, mode, false, src, result);
290}
291
292TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_deg2)
293{
294 const int8_t order = 3;
296 const Span<float3> positions = position_data.slice(0, 7);
297
300 run_nurbs_test(positions, order, mode, false, src, result);
301}
302
303TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_uneven_deg2)
304{
305 const int8_t order = 3;
307 const Span<float3> positions = position_data.slice(0, 8);
308
311 run_nurbs_test(positions, order, mode, false, src, result, positions.slice(0, 7));
312}
313
314TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_deg4)
315{
316 const int8_t order = 5;
318 const Span<float3> positions = position_data.slice(0, 13);
319
320 /* Even (whole Bezier segments). */
321 {
324 run_nurbs_test(positions, order, mode, false, src, result);
325 }
326
327 {
330 run_nurbs_test(positions.slice(0, 9), order, mode, false, src, result);
331 }
332
333 /* Uneven (incomplete segment). */
334
335 {
338 run_nurbs_test(positions.slice(0, 12), order, mode, false, src, result, positions.slice(0, 9));
339 }
340
341 {
344 run_nurbs_test(positions.slice(0, 11), order, mode, false, src, result, positions.slice(0, 9));
345 }
346
347 {
350 run_nurbs_test(positions.slice(0, 10), order, mode, false, src, result, positions.slice(0, 9));
351 }
352}
353
354TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_cyclic_deg4_looped_12)
355{
356 const int8_t order = 5;
358 const Span<float3> positions = position_data.slice(0, 12);
359
362 run_nurbs_test(positions.slice(0, 12), order, mode, true, src, result);
363}
364
365TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_cyclic_deg4_looped_8)
366{
367 const int8_t order = 5;
369 const Span<float3> positions = position_data.slice(0, 8);
370
373 run_nurbs_test(positions, order, mode, true, src, result);
374}
375
376TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_cyclic_deg4_discontinous_13)
377{
378 const int8_t order = 5;
380 const Span<float3> positions = position_data;
381
382 Vector<float3> expected(positions);
383 expected.append(positions[0]);
384 const bool expect_cyclic = false;
386
389
390 run_nurbs_test(
391 positions, order, mode, true, src, result, expected, &expect_mode, &expect_cyclic);
392}
393
394TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_cyclic_deg4_discontinous_11)
395{
396 const int8_t order = 5;
398 const Span<float3> positions = position_data.slice(0, 11);
399
400 Vector<float3> expected(positions);
401 expected.append(positions[0]);
402 const bool expect_cyclic = false;
404
407
408 run_nurbs_test(
409 positions, order, mode, true, src, result, expected, &expect_mode, &expect_cyclic);
410}
411
412TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_cyclic_deg4_discontinous_10)
413{
414 const int8_t order = 5;
416 const Span<float3> positions = position_data.slice(0, 10);
417
418 Vector<float3> expected(positions);
419 expected.append(positions[0]);
420 const bool expect_cyclic = false;
422
425
426 run_nurbs_test(
427 positions, order, mode, true, src, result, expected, &expect_mode, &expect_cyclic);
428}
429
430TEST_F(OBJCurvesTest, nurbs_io_bezier_clamped_cyclic_deg4_discontinous_9)
431{
432 const int8_t order = 5;
434 const Span<float3> positions = position_data.slice(0, 9);
435
436 Vector<float3> expected(positions);
437 expected.append(positions[0]);
438 const bool expect_cyclic = false;
440
443
444 run_nurbs_test(
445 positions, order, mode, true, src, result, expected, &expect_mode, &expect_cyclic);
446}
447
449
450/* -------------------------------------------------------------------- */
453
454TEST_F(OBJCurvesTest, nurbs_io_bezier_cyclic_deg4_looped_12)
455{
456 const int8_t order = 5;
458 const Span<float3> positions = position_data.slice(0, 12);
459
460 Vector<float3> expected(positions.size());
461 array_utils::copy(positions.slice(1, 11), expected.as_mutable_span().slice(0, 11));
462 expected.last() = positions.first();
463
465
468 run_nurbs_test(positions, order, mode, true, src, result, expected, &expect_mode);
469}
470
471TEST_F(OBJCurvesTest, nurbs_io_bezier_cyclic_deg4_looped_discontinous_13)
472{
473 const int8_t order = 5;
475 const Span<float3> positions = position_data.slice(0, 13);
476
477 Vector<float3> expected(positions.size() + 1);
478 array_utils::copy(positions.slice(1, 12), expected.as_mutable_span().slice(0, 12));
479 expected.last(1) = positions.first();
480 expected.last() = positions[1];
481
482 const bool expect_cyclic = false;
484
487 run_nurbs_test(
488 positions, order, mode, true, src, result, expected, &expect_mode, &expect_cyclic);
489}
490
491TEST_F(OBJCurvesTest, nurbs_io_bezier_cyclic_deg4_looped_discontinous_11)
492{
493 const int8_t order = 5;
495 const Span<float3> positions = position_data.slice(0, 11);
496
497 Vector<float3> expected(positions.size() + 1);
498 array_utils::copy(positions.slice(1, 10), expected.as_mutable_span().slice(0, 10));
499 expected.last(1) = positions.first();
500 expected.last() = positions[1];
501
502 const bool expect_cyclic = false;
504
507 run_nurbs_test(
508 positions, order, mode, true, src, result, expected, &expect_mode, &expect_cyclic);
509}
510
511TEST_F(OBJCurvesTest, nurbs_io_bezier_cyclic_deg4_looped_discontinous_10)
512{
513 const int8_t order = 5;
515 const Span<float3> positions = position_data.slice(0, 10);
516
517 Vector<float3> expected(positions.size() + 1);
518 array_utils::copy(positions.slice(1, 9), expected.as_mutable_span().slice(0, 9));
519 expected.last(1) = positions.first();
520 expected.last() = positions[1];
521
522 const bool expect_cyclic = false;
524
527 run_nurbs_test(
528 positions, order, mode, true, src, result, expected, &expect_mode, &expect_cyclic);
529}
530
531TEST_F(OBJCurvesTest, nurbs_io_bezier_cyclic_deg4_looped_discontinous_9)
532{
533 const int8_t order = 5;
535 const Span<float3> positions = position_data.slice(0, 9);
536
537 Vector<float3> expected(positions.size() + 1);
538 array_utils::copy(positions.slice(1, 8), expected.as_mutable_span().slice(0, 8));
539 expected.last(1) = positions.first();
540 expected.last() = positions[1];
541
542 const bool expect_cyclic = false;
544
547 run_nurbs_test(
548 positions, order, mode, true, src, result, expected, &expect_mode, &expect_cyclic);
549}
550
551TEST_F(OBJCurvesTest, nurbs_io_bezier_cyclic_deg4_looped_8)
552{
553 const int8_t order = 5;
555 const Span<float3> positions = position_data.slice(0, 8);
556
557 Vector<float3> expected(positions.size());
558 array_utils::copy(positions.slice(1, 7), expected.as_mutable_span().slice(0, 7));
559 expected.last() = positions.first();
560
562
565 run_nurbs_test(positions, order, mode, true, src, result, expected, &expect_mode);
566}
567
569
570} // namespace blender::io::obj
Low-level operations for curves.
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr Span()=default
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr int64_t size() const
Definition BLI_span.hh:256
void append(const T &value)
const T & last(const int64_t n=0) const
MutableSpan< T > as_mutable_span()
VArray< int8_t > nurbs_orders() const
VArray< bool > cyclic() const
Span< float3 > positions() const
VArray< int8_t > nurbs_knots_modes() const
Vector< bke::GeometrySet > read_curves(OBJImportParams params)
static bke::CurvesGeometry create_rational_nurbs(Span< float3 > points, Span< float > weights, bool cyclic, int8_t order, KnotsMode mode)
static bke::CurvesGeometry create_nurbs(Span< float3 > points, bool cyclic, int8_t order, KnotsMode mode)
void write_curves(const bke::CurvesGeometry &curve, OBJExportParams params)
void write_curves(const std::unique_ptr< IOBJCurve > &curve, OBJExportParams params)
void write_curves(const Span< std::unique_ptr< IOBJCurve > > curves, OBJExportParams params)
static bke::CurvesGeometry create_curves(Span< float3 > points, bool cyclic)
void run_nurbs_test(const Span< float3 > points, const int8_t order, const KnotsMode mode, const bool cyclic, bke::CurvesGeometry &src_curve, const bke::CurvesGeometry *&result_curve, Span< float3 > expected_points=Span< float3 >(), const KnotsMode *expected_mode=nullptr, const bool *expected_cyclic=nullptr)
OBJCurves(const bke::CurvesGeometry &curve, const float4x4 &transform, const std::string &name)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
Vector< int > calculate_multiplicity_sequence(Span< float > knots)
void calculate_knots(int points_num, KnotsMode mode, int8_t order, bool cyclic, MutableSpan< float > knots)
int knots_num(int points_num, int8_t order, bool cyclic)
void export_objects(const OBJExportParams &export_params, const Span< std::unique_ptr< OBJMesh > > meshes, const Span< std::unique_ptr< IOBJCurve > > curves, const char *filepath)
static OBJImportParams default_import_params(const std::string &filepath)
static OBJExportParams default_export_params(const std::string &filepath)
TEST_F(OBJExportTest, filter_objects_curves_as_mesh)
const std::array< float3, 13 > position_array
void importer_geometry(const OBJImportParams &import_params, Vector< bke::GeometrySet > &geometries)
const Span< float3 > position_data
char * STRNCPY(char(&dst)[N], const char *src)
MatBase< float, 4, 4 > float4x4
@ NURBS_KNOT_MODE_ENDPOINT_BEZIER
@ NURBS_KNOT_MODE_BEZIER
@ NURBS_KNOT_MODE_ENDPOINT
@ NURBS_KNOT_MODE_NORMAL
@ NURBS_KNOT_MODE_CUSTOM
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1288
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
VecBase< float, 3 > float3
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1245
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
signed char int8_t
Definition BKE_text.h:76