Blender V5.3
curves_edit_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "ED_curves.hh"
10
11#include "BKE_gtest_base.hh"
12
13#include "testing/testing.h"
14
16
18 const int order,
19 const Set<int> &is_cyclic)
20{
21 Array<int> offsets(all_positions.size() + 1, 0);
22 for (const int curve : all_positions.index_range()) {
23 const Vector<float3> &curve_positions = all_positions[curve];
24 offsets[curve + 1] = offsets[curve] + curve_positions.size();
25 }
26
27 bke::CurvesGeometry curves(offsets.last(), all_positions.size());
28
29 curves.offsets_for_write().copy_from(offsets);
30 const OffsetIndices points_by_curve = curves.points_by_curve();
31 MutableSpan<float3> positions = curves.positions_for_write();
32 MutableSpan<bool> cyclic = curves.cyclic_for_write();
33 MutableSpan<int8_t> orders = curves.nurbs_orders_for_write();
34
35 for (const int curve : all_positions.index_range()) {
36 positions.slice(points_by_curve[curve]).copy_from(all_positions[curve]);
37 cyclic[curve] = is_cyclic.contains(curve);
38 orders[curve] = order;
39 }
40
41 curves.tag_topology_changed();
42 return curves;
43}
44
46 const int order,
47 const Set<int> &is_cyclic)
48{
49 return create_curves(Span<Vector<float3>>(&positions, 1), order, is_cyclic);
50}
51
52static void validate_positions(const Span<Vector<float3>> expected_positions,
53 const OffsetIndices<int> points_by_curve,
54 const Span<float3> positions)
55{
56 for (const int curve : expected_positions.index_range()) {
57 const Span<float3> expected_curve_positions = expected_positions[curve];
58 const IndexRange points = points_by_curve[curve];
59 for (const int point : expected_curve_positions.index_range()) {
60 EXPECT_EQ(positions[points[point]], expected_curve_positions[point]);
61 }
62 }
63}
64
66
67TEST_F(CurvesEditorsTest, DuplicatePointsTwoSingle)
68{
69 /* Two points from single curve. */
70 const Vector<float3> expected_positions = {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}};
71
72 bke::CurvesGeometry curves = create_curves(expected_positions, 4, {});
73 IndexMaskMemory memory;
75
77
78 EXPECT_TRUE(curves.curves_num() == 2);
79
80 const Span<float3> positions = curves.positions();
81
82 for (const int point : expected_positions.index_range()) {
83 EXPECT_TRUE(positions[point] == expected_positions[point]);
84 }
85
86 EXPECT_TRUE(positions[4] == expected_positions[1]);
87 EXPECT_TRUE(positions[5] == expected_positions[2]);
88}
89
90TEST_F(CurvesEditorsTest, DuplicatePointsFourThree)
91{
92 /* Four points from three curves. One curve has one point. */
93 const Vector<Vector<float3>> expected_positions = {
94 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}},
95 {{0, 0, 0}},
96 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}, {1, -1, 0}}};
97
98 bke::CurvesGeometry curves = create_curves(expected_positions, 4, {});
99 IndexMaskMemory memory;
100 const IndexMask mask = IndexMask::from_indices(Array<int>{0, 1, 4, 9}.as_span(), memory);
101
103
104 EXPECT_TRUE(curves.curves_num() == expected_positions.size() + 3);
105
106 const Span<float3> positions = curves.positions();
107 const OffsetIndices points_by_curve = curves.points_by_curve();
108
109 for (const int curve : expected_positions.index_range()) {
110 const Span<float3> expected_curve_positions = expected_positions[curve];
111 const IndexRange points = points_by_curve[curve];
112 for (const int point : expected_curve_positions.index_range()) {
113 EXPECT_TRUE(positions[points[point]] == expected_curve_positions[point]);
114 }
115 }
116
117 EXPECT_TRUE(positions[10] == expected_positions[0][0]);
118 EXPECT_TRUE(positions[11] == expected_positions[0][1]);
119 EXPECT_TRUE(positions[12] == expected_positions[1][0]);
120 EXPECT_TRUE(positions[13] == expected_positions[2][4]);
121}
122
123TEST_F(CurvesEditorsTest, DuplicatePointsTwoCyclic)
124{
125 /* Two points from cyclic curve. Points are on cycle. */
126 const Vector<Vector<float3>> expected_positions = {
127 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}},
128 {{0, 0, 0}},
129 {{1, 1, 0}, {1, -1, 0}, {-1, -1, 0}, {-1, 1, 0}},
130 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}, {1, -1, 0}}};
131
132 bke::CurvesGeometry curves = create_curves(expected_positions, 4, {2});
133 IndexMaskMemory memory;
135
137
138 EXPECT_TRUE(curves.curves_num() == expected_positions.size() + 1);
139
140 const Span<float3> positions = curves.positions();
141 const OffsetIndices points_by_curve = curves.points_by_curve();
142
143 for (const int curve : expected_positions.index_range()) {
144 const Span<float3> expected_curve_positions = expected_positions[curve];
145 const IndexRange points = points_by_curve[curve];
146 for (const int point : expected_curve_positions.index_range()) {
147 EXPECT_TRUE(positions[points[point]] == expected_curve_positions[point]);
148 }
149 }
150
151 EXPECT_TRUE(positions[14] == expected_positions[2][3]);
152 EXPECT_TRUE(positions[15] == expected_positions[2][0]);
153}
154
155TEST_F(CurvesEditorsTest, SplitPointsTwoSingle)
156{
157 /* Split two points from single curve. */
158 const Vector<float3> positions = {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}};
159
160 bke::CurvesGeometry curves = create_curves(positions, 4, {});
161 IndexMaskMemory memory;
162 const IndexMask mask = IndexMask::from_indices<int>({1, 2}, memory);
163
165
166 const Vector<Vector<float3>> expected_positions = {
167 {{-1, 1, 0}, {1, 1, 0}}, {{-1.5, 0, 0}, {-1, 1, 0}}, {{1, 1, 0}, {1.5, 0, 0}}};
168
169 GTEST_ASSERT_EQ(new_curves.curves_num(), expected_positions.size());
170 validate_positions(expected_positions, new_curves.points_by_curve(), new_curves.positions());
171}
172
173TEST_F(CurvesEditorsTest, SplitPointsFourThree)
174{
175 /* Four points from three curves. One curve has one point. */
176 const Vector<Vector<float3>> positions = {
177 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}},
178 {{0, 0, 0}},
179 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}, {1, -1, 0}}};
180
181 bke::CurvesGeometry curves = create_curves(positions, 4, {});
182 IndexMaskMemory memory;
183 const IndexMask mask = IndexMask::from_indices(Array<int>{0, 1, 4, 9}.as_span(), memory);
184
186
187 const Vector<Vector<float3>> expected_positions = {
188 {{-1.5, 0, 0}, {-1, 1, 0}},
189 {{-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}},
190 {{0, 0, 0}},
191 {{1, -1, 0}},
192 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}, {1, -1, 0}}};
193
194 GTEST_ASSERT_EQ(new_curves.curves_num(), expected_positions.size());
195 validate_positions(expected_positions, new_curves.points_by_curve(), new_curves.positions());
196}
197
198TEST_F(CurvesEditorsTest, SplitPointsTwoCyclic)
199{
200 /* Two points from cyclic curve. Points are on cycle. */
201 const Vector<Vector<float3>> positions = {
202 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}},
203 {{0, 0, 0}},
204 {{1, 1, 0}, {1, -1, 0}, {-1, -1, 0}, {-1, 1, 0}},
205 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}, {1, -1, 0}}};
206
207 bke::CurvesGeometry curves = create_curves(positions, 4, {2});
208 IndexMaskMemory memory;
210
212
213 const Vector<Vector<float3>> expected_positions = {
214 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}},
215 {{0, 0, 0}},
216 {{-1, 1, 0}, {1, 1, 0}},
217 {{1, 1, 0}, {1, -1, 0}, {-1, -1, 0}, {-1, 1, 0}},
218 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}, {1, -1, 0}}};
219
220 GTEST_ASSERT_EQ(new_curves.curves_num(), expected_positions.size());
221 validate_positions(expected_positions, new_curves.points_by_curve(), new_curves.positions());
222 Array<bool> expected_cyclic = {false, false, false, false, false};
223 VArray<bool> cyclic = new_curves.cyclic();
224 for (const int i : expected_cyclic.index_range()) {
225 EXPECT_EQ(expected_cyclic[i], cyclic[i]);
226 }
227}
228
229TEST_F(CurvesEditorsTest, SplitPointsTwoTouchCyclic)
230{
231 /* Two points from cyclic curve. Points are touching cycle. */
232 const Vector<Vector<float3>> positions = {
233 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}},
234 {{0, 0, 0}},
235 {{1, 1, 0}, {1, -1, 0}, {-1, -1, 0}, {-1, 1, 0}},
236 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}, {1, -1, 0}}};
237
238 bke::CurvesGeometry curves = create_curves(positions, 4, {2});
239 IndexMaskMemory memory;
241
243
244 const Vector<Vector<float3>> expected_positions = {
245 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}},
246 {{0, 0, 0}},
247 {{1, 1, 0}, {1, -1, 0}},
248 {{1, -1, 0}, {-1, -1, 0}, {-1, 1, 0}, {1, 1, 0}},
249 {{-1.5, 0, 0}, {-1, 1, 0}, {1, 1, 0}, {1.5, 0, 0}, {1, -1, 0}}};
250
251 GTEST_ASSERT_EQ(new_curves.curves_num(), expected_positions.size());
252 validate_positions(expected_positions, new_curves.points_by_curve(), new_curves.positions());
253}
254
255TEST_F(CurvesEditorsTest, SplitEverySecondCyclic)
256{
257 /* Split every second point in cyclic curve. Expected result all selected points
258 * as separate curves and original curve. */
259 const Vector<Vector<float3>> positions = {{{0, -1, 0},
260 {-1, -1, 0},
261 {-1, 0, 0},
262 {-1, 1, 0},
263 {0, 1, 0},
264 {1, 1, 0},
265 {1, 0, 0},
266 {1, -1, 0}}};
267
268 bke::CurvesGeometry curves = create_curves(positions, 4, {0});
269 IndexMaskMemory memory;
270 const IndexMask mask = IndexMask::from_indices(Array<int>{0, 2, 4, 6}.as_span(), memory);
271
273
274 const Vector<Vector<float3>> expected_positions = {{{0, -1, 0}},
275 {{-1, 0, 0}},
276 {{0, 1, 0}},
277 {{1, 0, 0}},
278 {{0, -1, 0},
279 {-1, -1, 0},
280 {-1, 0, 0},
281 {-1, 1, 0},
282 {0, 1, 0},
283 {1, 1, 0},
284 {1, 0, 0},
285 {1, -1, 0}}};
286
287 GTEST_ASSERT_EQ(new_curves.curves_num(), expected_positions.size());
288 validate_positions(expected_positions, new_curves.points_by_curve(), new_curves.positions());
289}
290
291TEST_F(CurvesEditorsTest, SplitAllSelectedButFirstCyclic)
292{
293 /* Split all except first points in cyclic curve. Expected result two curves. One from selected
294 * points another from first, second and last. Both not cyclic. */
295 const Vector<Vector<float3>> positions = {{{0, -1, 0},
296 {-1, -1, 0},
297 {-1, 0, 0},
298 {-1, 1, 0},
299 {0, 1, 0},
300 {1, 1, 0},
301 {1, 0, 0},
302 {1, -1, 0}}};
303
304 bke::CurvesGeometry curves = create_curves(positions, 4, {0});
305 IndexMaskMemory memory;
306 const IndexMask mask = IndexMask::from_indices(Array<int>{1, 2, 3, 4, 5, 6, 7}.as_span(),
307 memory);
308
310
311 const Vector<Vector<float3>> expected_positions = {
312 {{-1, -1, 0}, {-1, 0, 0}, {-1, 1, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, {1, -1, 0}},
313 {{1, -1, 0}, {0, -1, 0}, {-1, -1, 0}},
314 };
315
316 GTEST_ASSERT_EQ(new_curves.curves_num(), expected_positions.size());
317 validate_positions(expected_positions, new_curves.points_by_curve(), new_curves.positions());
318 EXPECT_EQ(new_curves.curves_num(), expected_positions.size());
319 EXPECT_FALSE(new_curves.cyclic()[0]);
320 EXPECT_FALSE(new_curves.cyclic()[1]);
321}
322
323TEST_F(CurvesEditorsTest, SplitTwoOnSeamAndExtraCyclic)
324{
325 /* Split first, last and pair in the middle. Expected result four non cyclic curves. */
326 const Vector<Vector<float3>> positions = {{{0, -1, 0},
327 {-1, -1, 0},
328 {-1, 0, 0},
329 {-1, 1, 0},
330 {0, 1, 0},
331 {1, 1, 0},
332 {1, 0, 0},
333 {1, -1, 0}}};
334
335 bke::CurvesGeometry curves = create_curves(positions, 4, {0});
336 IndexMaskMemory memory;
337 const IndexMask mask = IndexMask::from_indices(Array<int>{0, 3, 4, 7}.as_span(), memory);
338
340
341 const Vector<Vector<float3>> expected_positions = {
342 {{-1, 1, 0}, {0, 1, 0}},
343 {{1, -1, 0}, {0, -1, 0}},
344 {{0, -1, 0}, {-1, -1, 0}, {-1, 0, 0}, {-1, 1, 0}},
345 {{0, 1, 0}, {1, 1, 0}, {1, 0, 0}, {1, -1, 0}}};
346
347 GTEST_ASSERT_EQ(new_curves.curves_num(), expected_positions.size());
348 validate_positions(expected_positions, new_curves.points_by_curve(), new_curves.positions());
349 EXPECT_FALSE(new_curves.cyclic()[0]);
350 EXPECT_FALSE(new_curves.cyclic()[1]);
351 EXPECT_FALSE(new_curves.cyclic()[2]);
352 EXPECT_FALSE(new_curves.cyclic()[3]);
353}
354
355} // namespace blender::ed::curves::tests
Span< T > as_span() const
Definition BLI_array.hh:254
const T & last(const int64_t n=0) const
Definition BLI_array.hh:307
IndexRange index_range() const
Definition BLI_array.hh:371
static IndexMask from_indices(Span< T > indices, LinearAllocator<> &memory)
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
bool contains(const Key &key) const
Definition BLI_set.hh:310
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
int64_t size() const
IndexRange index_range() const
VArray< bool > cyclic() const
Span< float3 > positions() const
OffsetIndices< int > points_by_curve() const
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
TEST_F(CurvesEditorsTest, DuplicatePointsTwoSingle)
static void validate_positions(const Span< Vector< float3 > > expected_positions, const OffsetIndices< int > points_by_curve, const Span< float3 > positions)
static bke::CurvesGeometry create_curves(const Span< Vector< float3 > > all_positions, const int order, const Set< int > &is_cyclic)
void duplicate_points(bke::CurvesGeometry &curves, const IndexMask &mask)
bke::CurvesGeometry split_points(const bke::CurvesGeometry &curves, const IndexMask &points_to_split)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)