Blender V5.3
mesh_copy_selection.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 "DNA_object_types.h"
10
12#include "BLI_index_mask.hh"
13#include "BLI_listbase.hh"
14#include "PRF_profile.hh"
15
16#include "BKE_attribute.hh"
17#include "BKE_deform.hh"
18#include "BKE_mesh.hh"
19
21#include "GEO_mesh_selection.hh"
22
23namespace blender::geometry {
24
25static void remap_verts(const OffsetIndices<int> src_faces,
26 const OffsetIndices<int> dst_faces,
27 const int src_verts_num,
28 const IndexMask &vert_mask,
29 const IndexMask &edge_mask,
30 const IndexMask &face_mask,
31 const Span<int2> src_edges,
32 const Span<int> src_corner_verts,
33 MutableSpan<int2> dst_edges,
34 MutableSpan<int> dst_corner_verts)
35{
36 Array<int> map(src_verts_num);
39 vert_mask.size() > 1024,
40 [&]() {
41 face_mask.foreach_index(
42 [&](const int64_t src_i, const int64_t dst_i) {
43 const IndexRange src_face = src_faces[src_i];
44 const IndexRange dst_face = dst_faces[dst_i];
45 for (const int i : src_face.index_range()) {
46 dst_corner_verts[dst_face[i]] = map[src_corner_verts[src_face[i]]];
47 }
48 },
50 },
51 [&]() {
52 edge_mask.foreach_index(
53 [&](const int64_t src_i, const int64_t dst_i) {
54 dst_edges[dst_i][0] = map[src_edges[src_i][0]];
55 dst_edges[dst_i][1] = map[src_edges[src_i][1]];
56 },
58 });
59}
60
61static void remap_edges(const OffsetIndices<int> src_faces,
62 const OffsetIndices<int> dst_faces,
63 const int src_edges_num,
64 const IndexMask &edge_mask,
65 const IndexMask &face_mask,
66 const Span<int> src_corner_edges,
67 MutableSpan<int> dst_corner_edges)
68{
69 Array<int> map(src_edges_num);
71 face_mask.foreach_index(
72 [&](const int64_t src_i, const int64_t dst_i) {
73 const IndexRange src_face = src_faces[src_i];
74 const IndexRange dst_face = dst_faces[dst_i];
75 for (const int i : src_face.index_range()) {
76 dst_corner_edges[dst_face[i]] = map[src_corner_edges[src_face[i]]];
77 }
78 },
80}
81
82static void copy_loose_vert_hint(const Mesh &src, Mesh &dst)
83{
84 const auto &src_cache = src.runtime->loose_verts_cache;
85 if (src_cache.is_cached() && src_cache.data().mask.is_empty()) {
86 dst.tag_loose_verts_none();
87 }
88}
89
90static void copy_loose_edge_hint(const Mesh &src, Mesh &dst)
91{
92 const auto &src_cache = src.runtime->loose_edges_cache;
93 if (src_cache.is_cached() && src_cache.data().mask.is_empty()) {
94 dst.tag_loose_edges_none();
95 }
96}
97
98static void copy_overlapping_hint(const Mesh &src, Mesh &dst)
99{
100 if (src.no_overlapping_topology()) {
101 dst.tag_overlapping_none();
102 }
103}
104
106static void gather_vert_attributes(const Mesh &mesh_src,
107 const bke::AttributeFilter &attribute_filter,
108 const IndexMask &vert_mask,
109 Mesh &mesh_dst)
110{
111 Set<std::string> vertex_group_names;
112 for (bDeformGroup &group : mesh_src.vertex_group_names) {
113 vertex_group_names.add(group.name);
114 }
115
116 const Span<MDeformVert> src = mesh_src.deform_verts();
117 if (!vertex_group_names.is_empty() && !src.is_empty()) {
118 MutableSpan<MDeformVert> dst = mesh_dst.deform_verts_for_write();
119 bke::gather_deform_verts(src, vert_mask, dst);
120 }
121
122 bke::gather_attributes(mesh_src.attributes(),
125 bke::attribute_filter_with_skip_ref(attribute_filter, vertex_group_names),
126 vert_mask,
127 mesh_dst.attributes_for_write());
128}
129
130std::optional<Mesh *> mesh_copy_selection(const Mesh &src_mesh,
131 const VArray<bool> &selection,
132 const bke::AttrDomain selection_domain,
133 const bke::AttributeFilter &attribute_filter)
134{
136 const Span<int2> src_edges = src_mesh.edges();
137 const OffsetIndices src_faces = src_mesh.faces();
138 const Span<int> src_corner_verts = src_mesh.corner_verts();
139 const Span<int> src_corner_edges = src_mesh.corner_edges();
140 const bke::AttributeAccessor src_attributes = src_mesh.attributes();
141
142 if (selection.is_empty()) {
143 return std::nullopt;
144 }
145 if (const std::optional<bool> single = selection.get_if_single()) {
146 return *single ? std::nullopt : std::make_optional<Mesh *>(nullptr);
147 }
148
150 IndexMask vert_mask;
151 IndexMask edge_mask;
152 IndexMask face_mask;
153 switch (selection_domain) {
155 const VArraySpan<bool> span(selection);
157 src_mesh.verts_num > 1024,
158 [&]() { vert_mask = IndexMask::from_bools(span, memory.local()); },
159 [&]() { edge_mask = edge_selection_from_vert(src_edges, span, memory.local()); },
160 [&]() {
161 face_mask = face_selection_from_vert(
162 src_faces, src_corner_verts, span, memory.local());
163 });
164 break;
165 }
167 const VArraySpan<bool> span(selection);
169 src_edges.size() > 1024,
170 [&]() {
171 edge_mask = IndexMask::from_bools(span, memory.local());
172 vert_mask = vert_selection_from_edge(
173 src_edges, edge_mask, src_mesh.verts_num, memory.local());
174 },
175 [&]() {
176 face_mask = face_selection_from_edge(
177 src_faces, src_corner_edges, span, memory.local());
178 });
179 break;
180 }
182 const VArraySpan<bool> span(selection);
183 face_mask = IndexMask::from_bools(span, memory.local());
185 face_mask.size() > 1024,
186 [&]() {
187 vert_mask = vert_selection_from_face(
188 src_faces, face_mask, src_corner_verts, src_mesh.verts_num, memory.local());
189 },
190 [&]() {
191 edge_mask = edge_selection_from_face(
192 src_faces, face_mask, src_corner_edges, src_mesh.edges_num, memory.local());
193 });
194 break;
195 }
196 default:
198 break;
199 }
200
201 if (vert_mask.is_empty()) {
202 return nullptr;
203 }
204 const bool same_verts = vert_mask.size() == src_mesh.verts_num;
205 const bool same_edges = edge_mask.size() == src_mesh.edges_num;
206 const bool same_faces = face_mask.size() == src_mesh.faces_num;
207 if (same_verts && same_edges && same_faces) {
208 return std::nullopt;
209 }
210
212 vert_mask.size(), edge_mask.size(), face_mask.size(), 0);
213 BKE_mesh_copy_parameters_for_eval(dst_mesh, &src_mesh);
214 bke::MutableAttributeAccessor dst_attributes = dst_mesh->attributes_for_write();
215 dst_attributes.add<int2>(".edge_verts", bke::AttrDomain::Edge, bke::AttributeInitConstruct());
216 MutableSpan<int2> dst_edges = dst_mesh->edges_for_write();
217
219 src_faces, face_mask, dst_mesh->face_offsets_for_write());
220 dst_mesh->corners_num = dst_faces.total_size();
221 dst_attributes.add<int>(".corner_vert", bke::AttrDomain::Corner, bke::AttributeInitConstruct());
222 dst_attributes.add<int>(".corner_edge", bke::AttrDomain::Corner, bke::AttributeInitConstruct());
223 MutableSpan<int> dst_corner_verts = dst_mesh->corner_verts_for_write();
224 MutableSpan<int> dst_corner_edges = dst_mesh->corner_edges_for_write();
225
227 vert_mask.size() > 1024,
228 [&]() {
229 remap_verts(src_faces,
230 dst_faces,
231 src_mesh.verts_num,
232 vert_mask,
233 edge_mask,
234 face_mask,
235 src_edges,
236 src_corner_verts,
237 dst_edges,
238 dst_corner_verts);
239 },
240 [&]() {
241 remap_edges(src_faces,
242 dst_faces,
243 src_edges.size(),
244 edge_mask,
245 face_mask,
246 src_corner_edges,
247 dst_corner_edges);
248 },
249 [&]() {
250 gather_vert_attributes(src_mesh, attribute_filter, vert_mask, *dst_mesh);
251 bke::gather_attributes(
252 src_attributes,
253 bke::AttrDomain::Edge,
254 bke::AttrDomain::Edge,
255 bke::attribute_filter_with_skip_ref(attribute_filter, {".edge_verts"}),
256 edge_mask,
257 dst_attributes);
258 bke::gather_attributes(src_attributes,
261 attribute_filter,
262 face_mask,
263 dst_attributes);
265 src_attributes,
269 {".corner_edge", ".corner_vert"}),
270 src_faces,
271 dst_faces,
272 face_mask,
273 dst_attributes);
274 });
275
276 if (selection_domain == bke::AttrDomain::Edge) {
277 copy_loose_vert_hint(src_mesh, *dst_mesh);
278 }
279 else if (selection_domain == bke::AttrDomain::Face) {
280 copy_loose_vert_hint(src_mesh, *dst_mesh);
281 copy_loose_edge_hint(src_mesh, *dst_mesh);
282 }
283 copy_overlapping_hint(src_mesh, *dst_mesh);
284
285 return dst_mesh;
286}
287
288std::optional<Mesh *> mesh_copy_selection_keep_verts(const Mesh &src_mesh,
289 const VArray<bool> &selection,
290 const bke::AttrDomain selection_domain,
291 const bke::AttributeFilter &attribute_filter)
292{
293 const Span<int2> src_edges = src_mesh.edges();
294 const OffsetIndices src_faces = src_mesh.faces();
295 const Span<int> src_corner_verts = src_mesh.corner_verts();
296 const Span<int> src_corner_edges = src_mesh.corner_edges();
297 const bke::AttributeAccessor src_attributes = src_mesh.attributes();
298
299 if (selection.is_empty()) {
300 return std::nullopt;
301 }
302
304 IndexMask edge_mask;
305 IndexMask face_mask;
306 switch (selection_domain) {
308 const VArraySpan<bool> span(selection);
310 src_edges.size() > 1024,
311 [&]() { edge_mask = edge_selection_from_vert(src_edges, span, memory.local()); },
312 [&]() {
313 face_mask = face_selection_from_vert(
314 src_faces, src_corner_verts, span, memory.local());
315 });
316 break;
317 }
319 const VArraySpan<bool> span(selection);
321 src_edges.size() > 1024,
322 [&]() { edge_mask = IndexMask::from_bools(span, memory.local()); },
323 [&]() {
324 face_mask = face_selection_from_edge(
325 src_faces, src_corner_edges, span, memory.local());
326 });
327 break;
328 }
330 const VArraySpan<bool> span(selection);
331 face_mask = IndexMask::from_bools(span, memory.local());
332 edge_mask = edge_selection_from_face(
333 src_faces, face_mask, src_corner_edges, src_edges.size(), memory.local());
334 break;
335 }
336 default:
338 break;
339 }
340
341 const bool same_edges = edge_mask.size() == src_mesh.edges_num;
342 const bool same_faces = face_mask.size() == src_mesh.faces_num;
343 if (same_edges && same_faces) {
344 return std::nullopt;
345 }
346
348 src_mesh.verts_num, edge_mask.size(), face_mask.size(), 0);
349 BKE_mesh_copy_parameters_for_eval(dst_mesh, &src_mesh);
350 bke::MutableAttributeAccessor dst_attributes = dst_mesh->attributes_for_write();
351
353 src_faces, face_mask, dst_mesh->face_offsets_for_write());
354 dst_mesh->corners_num = dst_faces.total_size();
355 dst_attributes.add<int>(".corner_edge", bke::AttrDomain::Corner, bke::AttributeInitConstruct());
356 MutableSpan<int> dst_corner_edges = dst_mesh->corner_edges_for_write();
357
359 [&]() {
360 remap_edges(src_faces,
361 dst_faces,
362 src_edges.size(),
363 edge_mask,
364 face_mask,
365 src_corner_edges,
366 dst_corner_edges);
367 },
368 [&]() {
369 bke::copy_attributes(src_attributes,
372 attribute_filter,
373 dst_attributes);
374 bke::gather_attributes(src_attributes,
377 attribute_filter,
378 edge_mask,
379 dst_attributes);
380 bke::gather_attributes(src_attributes,
383 attribute_filter,
384 face_mask,
385 dst_attributes);
387 src_attributes,
390 bke::attribute_filter_with_skip_ref(attribute_filter, {".corner_edge"}),
391 src_faces,
392 dst_faces,
393 face_mask,
394 dst_attributes);
395 });
396
397 /* Positions are not changed by the operation, so the bounds are the same. */
398 dst_mesh->runtime->bounds_cache = src_mesh.runtime->bounds_cache;
399 if (selection_domain == bke::AttrDomain::Face) {
400 copy_loose_edge_hint(src_mesh, *dst_mesh);
401 }
402 copy_overlapping_hint(src_mesh, *dst_mesh);
403
404 return dst_mesh;
405}
406
407std::optional<Mesh *> mesh_copy_selection_keep_edges(const Mesh &src_mesh,
408 const VArray<bool> &selection,
409 const bke::AttrDomain selection_domain,
410 const bke::AttributeFilter &attribute_filter)
411{
412 const OffsetIndices src_faces = src_mesh.faces();
413 const bke::AttributeAccessor src_attributes = src_mesh.attributes();
414
415 if (selection.is_empty()) {
416 return std::nullopt;
417 }
418
419 IndexMaskMemory memory;
420 IndexMask face_mask;
421 switch (selection_domain) {
423 face_mask = face_selection_from_vert(
424 src_faces, src_mesh.corner_verts(), VArraySpan(selection), memory);
425 break;
427 face_mask = face_selection_from_edge(
428 src_faces, src_mesh.corner_edges(), VArraySpan(selection), memory);
429 break;
431 face_mask = IndexMask::from_bools(selection, memory);
432 break;
433 default:
435 break;
436 }
437
438 const bool same_faces = face_mask.size() == src_mesh.faces_num;
439 if (same_faces) {
440 return std::nullopt;
441 }
442
444 src_mesh.verts_num, src_mesh.edges_num, face_mask.size(), 0);
445 BKE_mesh_copy_parameters_for_eval(dst_mesh, &src_mesh);
446 bke::MutableAttributeAccessor dst_attributes = dst_mesh->attributes_for_write();
447
449 src_faces, face_mask, dst_mesh->face_offsets_for_write());
450 dst_mesh->corners_num = dst_faces.total_size();
451 dst_attributes.add<int>(".corner_vert", bke::AttrDomain::Corner, bke::AttributeInitConstruct());
452 dst_attributes.add<int>(".corner_edge", bke::AttrDomain::Corner, bke::AttributeInitConstruct());
453
454 bke::copy_attributes(src_attributes,
457 attribute_filter,
458 dst_attributes);
459 bke::copy_attributes(src_attributes,
462 attribute_filter,
463 dst_attributes);
464 bke::gather_attributes(src_attributes,
467 attribute_filter,
468 face_mask,
469 dst_attributes);
473 attribute_filter,
474 src_faces,
475 dst_faces,
476 face_mask,
477 dst_attributes);
478
479 /* Positions are not changed by the operation, so the bounds are the same. */
480 dst_mesh->runtime->bounds_cache = src_mesh.runtime->bounds_cache;
481 copy_loose_vert_hint(src_mesh, *dst_mesh);
482 copy_overlapping_hint(src_mesh, *dst_mesh);
483 return dst_mesh;
484}
485
486} // namespace blender::geometry
support for deformation groups and hooks.
Object is a sort of wrapper for general info.
#define PRF_scope(category)
#define BLI_assert_unreachable()
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr IndexRange index_range() const
bool add(const Key &key)
Definition BLI_set.hh:248
bool is_empty() const
Definition BLI_set.hh:614
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr bool is_empty() const
Definition BLI_span.hh:264
std::optional< T > get_if_single() const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
auto attribute_filter_with_skip_ref(const AttributeFilter &filter, const Span< StringRef > skip)
void copy_attributes(const AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, MutableAttributeAccessor dst_attributes)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
Mesh * mesh_new_no_attributes(int verts_num, int edges_num, int faces_num, int corners_num)
void gather_deform_verts(Span< MDeformVert > src, Span< int > indices, MutableSpan< MDeformVert > dst)
Definition deform.cc:1774
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)
constexpr ParallelGrainSize grain_size(int grain_size)
static void copy_loose_edge_hint(const Mesh &src, Mesh &dst)
static void copy_overlapping_hint(const Mesh &src, Mesh &dst)
static void gather_vert_attributes(const Mesh &mesh_src, const bke::AttributeFilter &attribute_filter, const IndexMask &vert_mask, Mesh &mesh_dst)
IndexMask edge_selection_from_face(OffsetIndices< int > faces, const IndexMask &face_mask, Span< int > corner_edges, int edges_num, IndexMaskMemory &memory)
std::optional< Mesh * > mesh_copy_selection_keep_edges(const Mesh &mesh, const VArray< bool > &selection, bke::AttrDomain selection_domain, const bke::AttributeFilter &attribute_filter={})
static void remap_verts(const OffsetIndices< int > src_faces, const OffsetIndices< int > dst_faces, const int src_verts_num, const IndexMask &vert_mask, const IndexMask &edge_mask, const IndexMask &face_mask, const Span< int2 > src_edges, const Span< int > src_corner_verts, MutableSpan< int2 > dst_edges, MutableSpan< int > dst_corner_verts)
static void remap_edges(const OffsetIndices< int > src_faces, const OffsetIndices< int > dst_faces, const int src_edges_num, const IndexMask &edge_mask, const IndexMask &face_mask, const Span< int > src_corner_edges, MutableSpan< int > dst_corner_edges)
std::optional< Mesh * > mesh_copy_selection_keep_verts(const Mesh &src_mesh, const VArray< bool > &selection, bke::AttrDomain selection_domain, const bke::AttributeFilter &attribute_filter={})
IndexMask face_selection_from_edge(OffsetIndices< int > faces, Span< int > corner_edges, Span< bool > edge_mask, IndexMaskMemory &memory)
IndexMask face_selection_from_vert(OffsetIndices< int > faces, Span< int > corner_verts, Span< bool > vert_selection, IndexMaskMemory &memory)
std::optional< Mesh * > mesh_copy_selection(const Mesh &src_mesh, const VArray< bool > &selection, bke::AttrDomain selection_domain, const bke::AttributeFilter &attribute_filter={})
static void copy_loose_vert_hint(const Mesh &src, Mesh &dst)
void build_reverse_map(const IndexMask &mask, MutableSpan< T > r_map)
Definition index_mask.cc:36
template void build_reverse_map< int >(const IndexMask &mask, MutableSpan< int > r_map)
OffsetIndices< int > gather_selected_offsets(OffsetIndices< int > src_offsets, const IndexMask &selection, int start_offset, MutableSpan< int > dst_offsets)
void parallel_invoke(Functions &&...functions)
Definition BLI_task.hh:197
__int64 int64_t
Definition BKE_text.h:90
static void copy_loose_vert_hint(const Mesh &src, Mesh &dst)
void BKE_mesh_copy_parameters_for_eval(Mesh *me_dst, const Mesh *me_src)
VecBase< int32_t, 2 > int2
ListBaseT< bDeformGroup > vertex_group_names
bke::MeshRuntime * runtime
SharedCache< Bounds< float3 > > bounds_cache
SharedCache< LooseGeomCache > loose_edges_cache
SharedCache< LooseGeomCache > loose_verts_cache