Blender V5.3
mesh_to_curve_convert.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_deform.hh"
10#include "BLI_array.hh"
11#include "BLI_array_utils.hh"
12#include "BLI_set.hh"
13
14#include "BKE_attribute.hh"
15#include "BKE_attribute_math.hh"
16#include "BKE_curves.hh"
17#include "BKE_mesh.hh"
18
19#include "GEO_mesh_to_curve.hh"
20#include "GEO_randomize.hh"
21
22namespace blender::geometry {
23
24/* Don't copy attributes that are built-in on meshes but not on curves. */
25static auto filter_builtin_attributes(const bke::AttributeAccessor &mesh_attributes,
26 const bke::AttributeAccessor &curves_attributes,
28 const bke::AttributeFilter &attribute_filter)
29{
30 for (const StringRef name : mesh_attributes.all_names()) {
31 if (mesh_attributes.is_builtin(name) && !curves_attributes.is_builtin(name)) {
32 storage.add(name);
33 }
34 }
35 return bke::attribute_filter_with_skip_ref(attribute_filter, storage);
36}
37
39 const bke::AttributeAccessor &mesh_attributes,
40 const Span<int> vert_indices,
41 const Span<int> curve_offsets,
42 const IndexRange cyclic_curves,
43 const bke::AttributeFilter &attribute_filter)
44{
46 bke::CurvesGeometry curves(vert_indices.size(), curve_offsets.size());
47 curves.offsets_for_write().drop_back(1).copy_from(curve_offsets);
48 curves.offsets_for_write().last() = vert_indices.size();
49 curves.fill_curve_types(CURVE_TYPE_POLY);
50
51 bke::MutableAttributeAccessor curves_attributes = curves.attributes_for_write();
52
53 if (!cyclic_curves.is_empty()) {
54 curves.cyclic_for_write().slice(cyclic_curves).fill(true);
55 }
56
57 Set<StringRef> skip_storage;
58 const auto attribute_filter_with_skip = filter_builtin_attributes(
59 mesh_attributes, curves_attributes, skip_storage, attribute_filter);
60
61 bke::gather_attributes(mesh_attributes,
64 attribute_filter_with_skip,
65 vert_indices,
66 curves_attributes);
67
68 /* Transfer attributes from edge, face, and corner domains to curve points. */
69 mesh_attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
70 if (iter.domain == bke::AttrDomain::Point) {
71 return;
72 }
73 if (iter.data_type == bke::AttrType::String) {
74 return;
75 }
76 if (attribute_filter_with_skip.allow_skip(iter.name)) {
77 return;
78 }
79
81 /* Some attributes might not exist if they were builtin on domains that don't have
82 * any elements, i.e. a face attribute on the output of the line primitive node. */
83 if (!src) {
84 return;
85 }
86
87 const CommonVArrayInfo info = src.varray.common_info();
90 if (curves_attributes.add(iter.name, bke::AttrDomain::Point, iter.data_type, init)) {
91 return;
92 }
93 }
94
97 if (!dst) {
98 return;
99 }
100 bke::attribute_math::gather(*src, vert_indices, dst.span);
101 dst.finish();
102 });
103
105
106 return curves;
107}
108
117
119 const Span<int2> edges)
120{
122 /* Compute the number of edges connecting to each vertex. */
123 Array<int> neighbor_offsets_data(verts_num + 1, 0);
125 edges.cast<int>(), neighbor_offsets_data);
126
127 /* Use as an index into the "neighbor group" for each vertex. */
128 Array<int> used_slots(verts_num, 0);
129 /* Calculate the indices of each vertex's neighboring edges. */
130 Array<int> neighbors(edges.size() * 2);
131 for (const int i : edges.index_range()) {
132 const int v1 = edges[i][0];
133 const int v2 = edges[i][1];
134 neighbors[neighbor_offsets[v1].start() + used_slots[v1]] = v2;
135 neighbors[neighbor_offsets[v2].start() + used_slots[v2]] = v1;
136 used_slots[v1]++;
137 used_slots[v2]++;
138 }
139
140 Vector<int> vert_indices;
141 vert_indices.reserve(edges.size());
142 Vector<int> curve_offsets;
143
144 /* Now use the neighbor group offsets calculated above to count used edges at each vertex. */
145 Array<int> unused_edges = std::move(used_slots);
146
147 for (const int start_vert : IndexRange(verts_num)) {
148 /* Don't start at vertices with two neighbors, which may become part of cyclic curves. */
149 if (neighbor_offsets[start_vert].size() == 2) {
150 continue;
151 }
152
153 /* The vertex has no connected edges, or they were already used. */
154 if (unused_edges[start_vert] == 0) {
155 continue;
156 }
157
158 for (const int neighbor : neighbors.as_span().slice(neighbor_offsets[start_vert])) {
159 int current_vert = start_vert;
160 int next_vert = neighbor;
161
162 if (unused_edges[next_vert] == 0) {
163 continue;
164 }
165
166 /* Start a new curve in the output. */
167 curve_offsets.append(vert_indices.size());
168 vert_indices.append(current_vert);
169
170 /* Follow connected edges until we read a vertex with more than two connected edges. */
171 while (true) {
172 int last_vert = current_vert;
173 current_vert = next_vert;
174
175 vert_indices.append(current_vert);
176 unused_edges[current_vert]--;
177 unused_edges[last_vert]--;
178
179 if (neighbor_offsets[current_vert].size() != 2) {
180 break;
181 }
182
183 const int offset = neighbor_offsets[current_vert].start();
184 const int next_a = neighbors[offset];
185 const int next_b = neighbors[offset + 1];
186 next_vert = (last_vert == next_a) ? next_b : next_a;
187 }
188 }
189 }
190
191 /* All curves added after this are cyclic. */
192 const int cyclic_start = curve_offsets.size();
193
194 /* All remaining edges are part of cyclic curves because
195 * we skipped starting at vertices with two edges before. */
196 for (const int start_vert : IndexRange(verts_num)) {
197 if (unused_edges[start_vert] != 2) {
198 continue;
199 }
200
201 int current_vert = start_vert;
202 int next_vert = neighbors[neighbor_offsets[current_vert].start()];
203
204 curve_offsets.append(vert_indices.size());
205 vert_indices.append(current_vert);
206
207 /* Follow connected edges until we loop back to the start vertex. */
208 while (next_vert != start_vert) {
209 const int last_vert = current_vert;
210 current_vert = next_vert;
211
212 vert_indices.append(current_vert);
213 unused_edges[current_vert]--;
214 unused_edges[last_vert]--;
215
216 const int offset = neighbor_offsets[current_vert].start();
217 const int next_a = neighbors[offset];
218 const int next_b = neighbors[offset + 1];
219 next_vert = (last_vert == next_a) ? next_b : next_a;
220 }
221 }
222
223 const IndexRange cyclic_curves = curve_offsets.index_range().drop_front(cyclic_start);
224
225 return {std::move(vert_indices), std::move(curve_offsets), cyclic_curves};
226}
227
229 const Mesh &mesh, const Span<int2> edges, const bke::AttributeFilter &attribute_filter)
230{
232 return create_curve_from_vert_indices(mesh.attributes(),
233 output.vert_indices,
234 output.curve_offsets,
235 output.cyclic_curves,
236 attribute_filter);
237}
238
240 const IndexMask &selection,
241 const bke::AttributeFilter &attribute_filter)
242{
244 const Span<int2> edges = mesh.edges();
245 if (selection.size() == edges.size()) {
246 return edges_to_curves_convert(mesh, edges, attribute_filter);
247 }
248 Array<int2> selected_edges(selection.size());
249 array_utils::gather(edges, selection, selected_edges.as_mutable_span());
250 return edges_to_curves_convert(mesh, selected_edges, attribute_filter);
251}
252
254 const OffsetIndices<int> faces,
255 const IndexMask &selection)
256{
258 if (selection.size() == faces.size()) {
260 mesh.runtime->face_offsets_sharing_info,
261 &curves.curve_offsets,
262 &curves.runtime->curve_offsets_sharing_info);
263 curves.curve_num = faces.size();
264 curves.resize(mesh.corners_num, faces.size());
265 }
266 else {
267 curves.resize(0, selection.size());
268 offset_indices::gather_selected_offsets(faces, selection, curves.offsets_for_write());
269 curves.resize(curves.offsets().last(), curves.curves_num());
270 }
271
272 BKE_defgroup_copy_list(&curves.vertex_group_names, &mesh.vertex_group_names);
273 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
274 attributes.add<bool>("cyclic", bke::AttrDomain::Curve, bke::AttributeInitValue(true));
275 curves.fill_curve_types(CURVE_TYPE_POLY);
276 return curves;
277}
278
280 const OffsetIndices<int> faces,
281 const OffsetIndices<int> points_by_curve,
282 const IndexMask &selection,
283 Array<int> &map_data)
284{
285 if (selection.size() == faces.size()) {
286 return mesh.corner_verts();
287 }
288 map_data.reinitialize(points_by_curve.total_size());
290 faces, points_by_curve, selection, mesh.corner_verts(), map_data.as_mutable_span());
291 return map_data;
292}
293
295 const IndexMask &selection,
296 const bke::AttributeFilter &attribute_filter)
297{
299 const OffsetIndices faces = mesh.faces();
300 const bke::AttributeAccessor src_attributes = mesh.attributes();
301
303 const OffsetIndices points_by_curve = curves.points_by_curve();
304 bke::MutableAttributeAccessor dst_attributes = curves.attributes_for_write();
305
306 Array<int> point_to_vert_data;
307 const Span<int> point_to_vert_map = create_point_to_vert_map(
308 mesh, faces, points_by_curve, selection, point_to_vert_data);
309
310 Set<StringRef> skip_storage;
311 const auto attribute_filter_with_skip = filter_builtin_attributes(
312 src_attributes, dst_attributes, skip_storage, attribute_filter);
313
314 bke::gather_attributes(src_attributes,
317 attribute_filter_with_skip,
318 point_to_vert_map,
319 dst_attributes);
320
321 src_attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
322 if (iter.domain != bke::AttrDomain::Edge) {
323 return;
324 }
325 if (iter.data_type == bke::AttrType::String) {
326 return;
327 }
328 if (attribute_filter_with_skip.allow_skip(iter.name)) {
329 return;
330 }
331 const GVArray src = *iter.get(bke::AttrDomain::Point);
334 if (!dst) {
335 return;
336 }
337 bke::attribute_math::gather(src, point_to_vert_map, dst.span);
338 dst.finish();
339 });
340
341 bke::gather_attributes(src_attributes,
344 attribute_filter_with_skip,
345 selection,
346 dst_attributes);
347
351 attribute_filter_with_skip,
352 faces,
353 points_by_curve,
354 selection,
355 dst_attributes);
356
357 return curves;
358}
359
360} // namespace blender::geometry
Low-level operations for curves.
support for deformation groups and hooks.
#define PRF_scope(category)
#define BLI_NOINLINE
#define output
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
GPointer()=default
CommonVArrayInfo common_info() const
constexpr bool is_empty() const
constexpr IndexRange()=default
constexpr IndexRange drop_front(int64_t n) const
Span< NewT > constexpr cast() const
Definition BLI_span.hh:422
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
int64_t size() const
void append(const T &value)
IndexRange index_range() const
void reserve(const int64_t min_capacity)
Set< StringRefNull > all_names() const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool is_builtin(const StringRef name) const
GAttributeReader get() const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
#define storage(slot, qualifiers)
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void gather_group_to_group(const OffsetIndices< int > src_offsets, const OffsetIndices< int > dst_offsets, const IndexMask &selection, const Span< T > src, MutableSpan< T > dst)
void gather(GSpan src, Span< int > map, GMutableSpan dst)
auto attribute_filter_with_skip_ref(const AttributeFilter &filter, const Span< StringRef > skip)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
void gather_attributes_group_to_group(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
static Span< int > create_point_to_vert_map(const Mesh &mesh, const OffsetIndices< int > faces, const OffsetIndices< int > points_by_curve, const IndexMask &selection, Array< int > &map_data)
static auto filter_builtin_attributes(const bke::AttributeAccessor &mesh_attributes, const bke::AttributeAccessor &curves_attributes, Set< StringRef > &storage, const bke::AttributeFilter &attribute_filter)
static bke::CurvesGeometry create_curves_for_faces(const Mesh &mesh, const OffsetIndices< int > faces, const IndexMask &selection)
static BLI_NOINLINE CurveFromEdgesOutput edges_to_curve_point_indices(const int verts_num, const Span< int2 > edges)
bke::CurvesGeometry mesh_edges_to_curves_convert(const Mesh &mesh, const IndexMask &selection, const bke::AttributeFilter &attribute_filter)
bke::CurvesGeometry create_curve_from_vert_indices(const bke::AttributeAccessor &mesh_attributes, Span< int > vert_indices, Span< int > curve_offsets, IndexRange cyclic_curves, const bke::AttributeFilter &attribute_filter)
void debug_randomize_curve_order(bke::CurvesGeometry *curves)
Definition randomize.cc:274
static BLI_NOINLINE bke::CurvesGeometry edges_to_curves_convert(const Mesh &mesh, const Span< int2 > edges, const bke::AttributeFilter &attribute_filter)
bke::CurvesGeometry mesh_faces_to_curves_convert(const Mesh &mesh, const IndexMask &selection, const bke::AttributeFilter &attribute_filter)
void copy_shared_pointer(T *src_ptr, const ImplicitSharingInfo *src_sharing_info, T **r_dst_ptr, const ImplicitSharingInfo **r_dst_sharing_info)
OffsetIndices< int > build_reverse_offsets(Span< int > indices, MutableSpan< int > offsets)
OffsetIndices< int > gather_selected_offsets(OffsetIndices< int > src_offsets, const IndexMask &selection, int start_offset, MutableSpan< int > dst_offsets)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void BKE_defgroup_copy_list(ListBaseT< bDeformGroup > *outbase, const ListBaseT< bDeformGroup > *inbase)
Definition deform.cc:75