Blender
V5.0
source
blender
geometry
tests
GEO_merge_curves_test.cc
Go to the documentation of this file.
1
/* SPDX-FileCopyrightText: 2024 Blender Authors
2
*
3
* SPDX-License-Identifier: Apache-2.0 */
4
5
#include "
BKE_attribute.hh
"
6
#include "
BKE_curves.hh
"
7
8
#include "
BLI_array_utils.hh
"
9
#include "
GEO_merge_curves.hh
"
10
11
#include "testing/testing.h"
12
13
using namespace
blender::bke
;
14
15
namespace
blender::geometry::tests
{
16
17
static
bke::CurvesGeometry
create_test_curves
(
Span<int>
offsets,
Span<bool>
cyclic)
18
{
19
BLI_assert
(!offsets.
is_empty
());
20
const
int
curves_num = offsets.
size
() - 1;
21
BLI_assert
(cyclic.
size
() == curves_num);
22
const
int
points_num = offsets.
last
();
23
24
bke::CurvesGeometry
curves
(points_num, curves_num);
25
curves
.offsets_for_write().copy_from(offsets);
26
curves
.cyclic_for_write().copy_from(cyclic);
27
28
/* Attribute storing original indices to test point remapping. */
29
SpanAttributeWriter<int>
test_indices_writer =
30
curves
.attributes_for_write().lookup_or_add_for_write_span<
int
>(
31
"test_index"
,
bke::AttrDomain::Point
,
bke::AttributeInitConstruct
());
32
array_utils::fill_index_range
(test_indices_writer.
span
);
33
test_indices_writer.
finish
();
34
35
return
curves
;
36
}
37
38
TEST
(merge_curves, NoConnections)
39
{
40
bke::CurvesGeometry
src_curves =
create_test_curves
({0, 3, 6, 9, 12},
41
{
false
,
true
,
true
,
false
});
42
43
Array<int>
connect_to_curve(4, -1);
44
Array<bool>
flip_direction(4,
false
);
45
46
bke::CurvesGeometry
dst_curves =
geometry::curves_merge_endpoints
(
47
src_curves, connect_to_curve, flip_direction, {});
48
const
VArraySpan<bool>
cyclic = dst_curves.
cyclic
();
49
50
EXPECT_EQ
(dst_curves.
points_num
(), 12);
51
EXPECT_EQ
(dst_curves.
curves_num
(), 4);
52
EXPECT_EQ_SPAN(
Span
({0, 3, 6, 9, 12}), dst_curves.
offsets
());
53
EXPECT_EQ_SPAN(
Span
({
false
,
true
,
true
,
false
}), cyclic);
54
}
55
56
TEST
(merge_curves, ConnectSingleCurve)
57
{
58
bke::CurvesGeometry
src_curves =
create_test_curves
({0, 3, 6, 9, 12},
59
{
false
,
true
,
true
,
false
});
60
61
Array<int>
connect_to_curve = {-1, -1, -1, 1};
62
Array<bool>
flip_direction(4,
false
);
63
64
bke::CurvesGeometry
dst_curves =
geometry::curves_merge_endpoints
(
65
src_curves, connect_to_curve, flip_direction, {});
66
const
VArraySpan<bool>
cyclic = dst_curves.
cyclic
();
67
const
VArraySpan<int>
dst_indices = *dst_curves.
attributes
().
lookup
<
int
>(
"test_index"
);
68
69
EXPECT_EQ
(dst_curves.
points_num
(), 12);
70
EXPECT_EQ
(dst_curves.
curves_num
(), 3);
71
EXPECT_EQ_SPAN(
Span
({0, 3, 6, 12}), dst_curves.
offsets
());
72
EXPECT_EQ_SPAN(
Span
({
false
,
true
,
false
}), cyclic);
73
EXPECT_EQ_SPAN(
Span
({0, 1, 2, 6, 7, 8, 9, 10, 11, 3, 4, 5}), dst_indices);
74
}
75
76
TEST
(merge_curves, ReverseCurves)
77
{
78
bke::CurvesGeometry
src_curves =
create_test_curves
({0, 3, 6, 9, 12},
79
{
false
,
true
,
true
,
false
});
80
81
Array<int>
connect_to_curve = {-1, -1, -1, -1};
82
Array<bool>
flip_direction = {
false
,
true
,
false
,
true
};
83
84
bke::CurvesGeometry
dst_curves =
geometry::curves_merge_endpoints
(
85
src_curves, connect_to_curve, flip_direction, {});
86
const
VArraySpan<bool>
cyclic = dst_curves.
cyclic
();
87
const
VArraySpan<int>
dst_indices = *dst_curves.
attributes
().
lookup
<
int
>(
"test_index"
);
88
89
EXPECT_EQ
(dst_curves.
points_num
(), 12);
90
EXPECT_EQ
(dst_curves.
curves_num
(), 4);
91
EXPECT_EQ_SPAN(
Span
({0, 3, 6, 9, 12}), dst_curves.
offsets
());
92
EXPECT_EQ_SPAN(
Span
({
false
,
true
,
true
,
false
}), cyclic);
93
EXPECT_EQ_SPAN(
Span
({0, 1, 2, 5, 4, 3, 6, 7, 8, 11, 10, 9}), dst_indices);
94
}
95
96
TEST
(merge_curves, ConnectAndReverseCurves)
97
{
98
bke::CurvesGeometry
src_curves =
create_test_curves
({0, 3, 6, 9, 12},
99
{
false
,
true
,
true
,
false
});
100
101
Array<int>
connect_to_curve = {3, 0, -1, -1};
102
Array<bool>
flip_direction = {
true
,
false
,
true
,
false
};
103
104
bke::CurvesGeometry
dst_curves =
geometry::curves_merge_endpoints
(
105
src_curves, connect_to_curve, flip_direction, {});
106
const
VArraySpan<bool>
cyclic = dst_curves.
cyclic
();
107
const
VArraySpan<int>
dst_indices = *dst_curves.
attributes
().
lookup
<
int
>(
"test_index"
);
108
109
EXPECT_EQ
(dst_curves.
points_num
(), 12);
110
EXPECT_EQ
(dst_curves.
curves_num
(), 2);
111
EXPECT_EQ_SPAN(
Span
({0, 9, 12}), dst_curves.
offsets
());
112
EXPECT_EQ_SPAN(
Span
({
false
,
true
}), cyclic);
113
EXPECT_EQ_SPAN(
Span
({3, 4, 5, 2, 1, 0, 9, 10, 11, 8, 7, 6}), dst_indices);
114
}
115
116
TEST
(merge_curves, CyclicConnection)
117
{
118
bke::CurvesGeometry
src_curves =
create_test_curves
({0, 3, 6, 9, 12},
119
{
false
,
true
,
true
,
false
});
120
121
Array<int>
connect_to_curve = {-1, 3, -1, 1};
122
Array<bool>
flip_direction(4,
false
);
123
124
bke::CurvesGeometry
dst_curves =
geometry::curves_merge_endpoints
(
125
src_curves, connect_to_curve, flip_direction, {});
126
const
VArraySpan<bool>
cyclic = dst_curves.
cyclic
();
127
const
VArraySpan<int>
dst_indices = *dst_curves.
attributes
().
lookup
<
int
>(
"test_index"
);
128
129
EXPECT_EQ
(dst_curves.
points_num
(), 12);
130
EXPECT_EQ
(dst_curves.
curves_num
(), 3);
131
EXPECT_EQ_SPAN(
Span
({0, 3, 9, 12}), dst_curves.
offsets
());
132
EXPECT_EQ_SPAN(
Span
({
false
,
true
,
true
}), cyclic);
133
EXPECT_EQ_SPAN(
Span
({0, 1, 2, 3, 4, 5, 9, 10, 11, 6, 7, 8}), dst_indices);
134
}
135
136
TEST
(merge_curves, SelfConnectCurve)
137
{
138
bke::CurvesGeometry
src_curves =
create_test_curves
({0, 3, 6, 9, 12},
139
{
false
,
false
,
false
,
false
});
140
141
Array<int>
connect_to_curve = {-1, 1, 2, -1};
142
Array<bool>
flip_direction(4,
false
);
143
144
bke::CurvesGeometry
dst_curves =
geometry::curves_merge_endpoints
(
145
src_curves, connect_to_curve, flip_direction, {});
146
const
VArraySpan<bool>
cyclic = dst_curves.
cyclic
();
147
const
VArraySpan<int>
dst_indices = *dst_curves.
attributes
().
lookup
<
int
>(
"test_index"
);
148
149
EXPECT_EQ
(dst_curves.
points_num
(), 12);
150
EXPECT_EQ
(dst_curves.
curves_num
(), 4);
151
EXPECT_EQ_SPAN(
Span
({0, 3, 6, 9, 12}), dst_curves.
offsets
());
152
EXPECT_EQ_SPAN(
Span
({
false
,
true
,
true
,
false
}), cyclic);
153
EXPECT_EQ_SPAN(
Span
({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}), dst_indices);
154
}
155
156
TEST
(merge_curves, MergeAll)
157
{
158
bke::CurvesGeometry
src_curves =
create_test_curves
({0, 3, 6, 9, 12},
159
{
false
,
true
,
true
,
false
});
160
161
Array<int>
connect_to_curve = {2, 0, 3, 1};
162
Array<bool>
flip_direction(4,
false
);
163
164
bke::CurvesGeometry
dst_curves =
geometry::curves_merge_endpoints
(
165
src_curves, connect_to_curve, flip_direction, {});
166
const
VArraySpan<bool>
cyclic = dst_curves.
cyclic
();
167
const
VArraySpan<int>
dst_indices = *dst_curves.
attributes
().
lookup
<
int
>(
"test_index"
);
168
169
EXPECT_EQ
(dst_curves.
points_num
(), 12);
170
EXPECT_EQ
(dst_curves.
curves_num
(), 1);
171
EXPECT_EQ_SPAN(
Span
({0, 12}), dst_curves.
offsets
());
172
EXPECT_EQ_SPAN(
Span
({
true
}), cyclic);
173
EXPECT_EQ_SPAN(
Span
({0, 1, 2, 6, 7, 8, 9, 10, 11, 3, 4, 5}), dst_indices);
174
}
175
176
TEST
(merge_curves, Branching)
177
{
178
bke::CurvesGeometry
src_curves =
create_test_curves
({0, 3, 6, 9, 12},
179
{
false
,
true
,
true
,
false
});
180
181
/* Multiple curves connect to curve 2, one connection is ignored. */
182
Array<int>
connect_to_curve = {2, 2, -1, -1};
183
Array<bool>
flip_direction(4,
false
);
184
185
bke::CurvesGeometry
dst_curves =
geometry::curves_merge_endpoints
(
186
src_curves, connect_to_curve, flip_direction, {});
187
const
VArraySpan<bool>
cyclic = dst_curves.
cyclic
();
188
const
VArraySpan<int>
dst_indices = *dst_curves.
attributes
().
lookup
<
int
>(
"test_index"
);
189
190
EXPECT_EQ
(dst_curves.
points_num
(), 12);
191
EXPECT_EQ
(dst_curves.
curves_num
(), 3);
192
EXPECT_EQ_SPAN(
Span
({0, 6, 9, 12}), dst_curves.
offsets
());
193
EXPECT_EQ_SPAN(
Span
({
false
,
false
,
false
}), cyclic);
194
EXPECT_EQ_SPAN(
Span
({0, 1, 2, 6, 7, 8, 3, 4, 5, 9, 10, 11}), dst_indices);
195
}
196
197
}
// namespace blender::geometry::tests
BKE_attribute.hh
BKE_curves.hh
Low-level operations for curves.
BLI_array_utils.hh
BLI_assert
#define BLI_assert(a)
Definition
BLI_assert.h:46
EXPECT_EQ
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
GEO_merge_curves.hh
blender::Array
Definition
BLI_array.hh:50
blender::Span
Definition
BLI_span.hh:74
blender::Span::size
constexpr int64_t size() const
Definition
BLI_span.hh:252
blender::Span::last
constexpr const T & last(const int64_t n=0) const
Definition
BLI_span.hh:325
blender::Span::is_empty
constexpr bool is_empty() const
Definition
BLI_span.hh:260
blender::VArraySpan
Definition
BLI_virtual_array.hh:1108
blender::bke::AttributeAccessor::lookup
GAttributeReader lookup(const StringRef attribute_id) const
Definition
BKE_attribute.hh:610
blender::bke::CurvesGeometry
Definition
BKE_curves.hh:155
blender::bke::CurvesGeometry::points_num
int points_num() const
Definition
BKE_curves.hh:982
blender::bke::CurvesGeometry::offsets
Span< int > offsets() const
Definition
curves_geometry.cc:336
blender::bke::CurvesGeometry::attributes
AttributeAccessor attributes() const
Definition
curves_geometry.cc:1865
blender::bke::CurvesGeometry::curves_num
int curves_num() const
Definition
BKE_curves.hh:986
blender::bke::CurvesGeometry::cyclic
VArray< bool > cyclic() const
Definition
curves_geometry.cc:353
blender::array_utils::fill_index_range
void fill_index_range(MutableSpan< T > span, const T start=0)
Definition
BLI_array_utils.hh:324
blender::bke::curves
Definition
BKE_curves.hh:47
blender::bke
Definition
AS_asset_library.hh:27
blender::bke::AttrDomain::Point
@ Point
Definition
BKE_attribute.hh:67
blender::geometry::tests
Definition
GEO_merge_curves_test.cc:15
blender::geometry::tests::TEST
TEST(merge_curves, NoConnections)
Definition
GEO_merge_curves_test.cc:38
blender::geometry::tests::create_test_curves
static bke::CurvesGeometry create_test_curves(Span< int > offsets, Span< bool > cyclic)
Definition
GEO_merge_curves_test.cc:17
blender::geometry::curves_merge_endpoints
bke::CurvesGeometry curves_merge_endpoints(const bke::CurvesGeometry &src_curves, Span< int > connect_to_curve, Span< bool > flip_direction, const bke::AttributeFilter &attribute_filter)
Definition
merge_curves.cc:287
blender::bke::AttributeInitConstruct
Definition
BKE_attribute.hh:128
blender::bke::SpanAttributeWriter
Definition
BKE_attribute.hh:285
blender::bke::SpanAttributeWriter::finish
void finish()
Definition
BKE_attribute.hh:318
blender::bke::SpanAttributeWriter::span
MutableVArraySpan< T > span
Definition
BKE_attribute.hh:289
Generated on
for Blender by
doxygen
1.16.1