Blender V5.3
sculpt_face_set.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include "sculpt_face_set.hh"
9
10#include <cmath>
11#include <cstdlib>
12#include <queue>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_array.hh"
17#include "BLI_array_utils.hh"
18#include "BLI_bit_vector.hh"
20#include "BLI_function_ref.hh"
21#include "BLI_hash_c.hh"
22#include "BLI_math_matrix_c.hh"
23#include "BLI_math_vector.hh"
24#include "BLI_math_vector_c.hh"
26#include "BLI_span.hh"
27#include "BLI_task.hh"
28#include "BLI_vector.hh"
29
30#include "BLT_translation.hh"
31
33#include "DNA_object_types.h"
34#include "DNA_scene_types.h"
35
36#include "BKE_attribute.hh"
37#include "BKE_ccg.hh"
38#include "BKE_context.hh"
39#include "BKE_customdata.hh"
40#include "BKE_layer.hh"
41#include "BKE_mesh.hh"
42#include "BKE_mesh_fair.hh"
43#include "BKE_mesh_mapping.hh"
44#include "BKE_object.hh"
45#include "BKE_object_types.hh"
46#include "BKE_paint.hh"
47#include "BKE_paint_bvh.hh"
48#include "BKE_paint_types.hh"
49#include "BKE_subdiv_ccg.hh"
50
51#include "DEG_depsgraph.hh"
52
53#include "WM_api.hh"
54#include "WM_types.hh"
55
56#include "ED_sculpt.hh"
57
58#include "mesh_brush_common.hh"
59#include "paint_hide.hh"
60#include "sculpt_boundary.hh"
61#include "sculpt_gesture.hh"
62#include "sculpt_intern.hh"
63#include "sculpt_islands.hh"
64#include "sculpt_undo.hh"
65
66#include "RNA_access.hh"
67#include "RNA_define.hh"
68
69#include "bmesh.hh"
70
72
73/* -------------------------------------------------------------------- */
76
78{
79 const bke::AttributeAccessor attributes = mesh.attributes();
80 const VArraySpan<int> face_sets = *attributes.lookup<int>(".sculpt_face_set",
83 face_sets.index_range(),
84 4096,
85 1,
86 [&](const IndexRange range, int max) {
87 for (const int id : face_sets.slice(range)) {
88 max = std::max(max, id);
89 }
90 return max;
91 },
92 [](const int a, const int b) { return std::max(a, b); });
93 return max + 1;
94}
95
97{
98 SculptSession &ss = *object.runtime->sculpt_session;
99 switch (bke::object::pbvh_get(object)->type()) {
102 Mesh &mesh = *id_cast<Mesh *>(object.data);
104 }
106 BMesh &bm = *ss.bm;
107 const int cd_offset = CustomData_get_offset_named(
108 &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
109 if (cd_offset == -1) {
110 /* Default face set ID is 1, so the next available id should never be 1 */
111 return 2;
112 }
113 int next_face_set = 1;
114 BMIter iter;
115 BMFace *f;
116 BM_ITER_MESH (f, &iter, &bm, BM_FACES_OF_MESH) {
117 const int fset = *static_cast<const int *>(POINTER_OFFSET(f->head.data, cd_offset));
118 next_face_set = std::max(next_face_set, fset);
119 }
120
121 return next_face_set + 1;
122 }
123 }
125 return 0;
126}
127
128void initialize_none_to_id(Mesh *mesh, const int new_id)
129{
130 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
131 bke::SpanAttributeWriter<int> face_sets = attributes.lookup_for_write_span<int>(
132 ".sculpt_face_set");
133 if (!face_sets) {
134 return;
135 }
136
137 for (const int i : face_sets.span.index_range()) {
138 if (face_sets.span[i] == face_set_none_id) {
139 face_sets.span[i] = new_id;
140 }
141 }
142 face_sets.finish();
143}
144
145int active_update_and_get(bContext *C, Object &ob, const float mval[2])
146{
147 if (!ob.runtime->sculpt_session) {
148 return face_set_none_id;
149 }
150
151 if (!cursor_geometry_info_update(C, mval, false)) {
152 return face_set_none_id;
153 }
154
155 return active_face_set_get(ob);
156}
157
159{
160 Mesh &mesh = *id_cast<Mesh *>(object.data);
161 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
162 if (attributes.contains(".sculpt_face_set")) {
163 return false;
164 }
165 attributes.add<int>(".sculpt_face_set", bke::AttrDomain::Face, bke::AttributeInitValue(1));
166 mesh.face_sets_color_default = 1;
167 return true;
168}
169
171{
172 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
173 if (!attributes.contains(".sculpt_face_set")) {
174 attributes.add<int>(".sculpt_face_set", bke::AttrDomain::Face, bke::AttributeInitValue(1));
175 mesh.face_sets_color_default = 1;
176 }
177 return attributes.lookup_or_add_for_write_span<int>(".sculpt_face_set", bke::AttrDomain::Face);
178}
179
181{
182 Mesh &mesh = *id_cast<Mesh *>(object.data);
183 SculptSession &ss = *object.runtime->sculpt_session;
184 BMesh &bm = *ss.bm;
185 if (!CustomData_has_layer_named(&bm.pdata, CD_PROP_INT32, ".sculpt_face_set")) {
186 BM_data_layer_add_named(&bm, &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
187 const int offset = CustomData_get_offset_named(&bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
188 if (offset == -1) {
189 return -1;
190 }
191 BMIter iter;
192 BMFace *face;
193 BM_ITER_MESH (face, &iter, &bm, BM_FACES_OF_MESH) {
194 BM_ELEM_CD_SET_INT(face, offset, 1);
195 }
196 mesh.face_sets_color_default = 1;
197 return offset;
198 }
199 return CustomData_get_offset_named(&bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
200}
201
203{
204 const bke::AttributeAccessor attributes = mesh.attributes();
205 const VArray<int> attribute = *attributes.lookup_or_default(
206 ".sculpt_face_set", bke::AttrDomain::Face, 0);
207 Array<int> face_sets(attribute.size());
209 return face_sets;
210}
211
213 const Span<int> face_sets,
214 const bool unique,
215 const Span<int> verts,
216 const MutableSpan<float> factors)
217{
219 BLI_assert(verts.size() == factors.size());
220
221 for (const int i : verts.index_range()) {
222 if (unique == face_set::vert_has_unique_face_set(vert_to_face_map, face_sets, verts[i])) {
223 factors[i] = 0.0f;
224 }
225 }
226}
227
229 const Span<int> corner_verts,
230 const GroupedSpan<int> vert_to_face_map,
231 const Span<int> face_sets,
232 const SubdivCCG &subdiv_ccg,
233 const bool unique,
234 const Span<int> grids,
235 const MutableSpan<float> factors)
236{
238 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
239 BLI_assert(grids.size() * key.grid_area == factors.size());
240
241 for (const int i : grids.index_range()) {
242 const int node_start = i * key.grid_area;
243 for (const int y : IndexRange(key.grid_size)) {
244 for (const int x : IndexRange(key.grid_size)) {
245 const int offset = CCG_grid_xy_to_index(key.grid_size, x, y);
246 const int node_vert = node_start + offset;
247 if (factors[node_vert] == 0.0f) {
248 continue;
249 }
250
251 SubdivCCGCoord coord{};
252 coord.grid_index = grids[i];
253 coord.x = x;
254 coord.y = y;
256 faces, corner_verts, vert_to_face_map, face_sets, subdiv_ccg, coord))
257 {
258 factors[node_vert] = 0.0f;
259 }
260 }
261 }
262 }
263}
264
266 const bool unique,
267 const Set<BMVert *, 0> &verts,
268 const MutableSpan<float> factors)
269{
271 BLI_assert(verts.size() == factors.size());
272
273 int i = 0;
274 for (const BMVert *vert : verts) {
275 if (unique == face_set::vert_has_unique_face_set(face_set_offset, *vert)) {
276 factors[i] = 0.0f;
277 }
278 i++;
279 }
280}
281
283
284/* -------------------------------------------------------------------- */
288
289static void face_sets_update(const Depsgraph &depsgraph,
290 Object &object,
291 const IndexMask &node_mask,
292 const FunctionRef<void(Span<int>, MutableSpan<int>)> calc_face_sets)
293{
294 SculptSession &ss = *object.runtime->sculpt_session;
296
297 bke::SpanAttributeWriter<int> face_sets = ensure_face_sets_mesh(*id_cast<Mesh *>(object.data));
298
299 struct TLS {
300 Vector<int> face_indices;
301 Vector<int> new_face_sets;
302 };
303
304 Array<bool> node_changed(pbvh.nodes_num(), false);
305
307 if (pbvh.type() == bke::pbvh::Type::Mesh) {
309 node_mask.foreach_index(
310 [&](const int i) {
311 TLS &tls = all_tls.local();
312 const Span<int> faces = nodes[i].faces();
313
314 tls.new_face_sets.resize(faces.size());
315 MutableSpan<int> new_face_sets = tls.new_face_sets;
316 gather_data_mesh(face_sets.span.as_span(), faces, new_face_sets);
317 calc_face_sets(faces, new_face_sets);
318 if (array_utils::indexed_data_equal<int>(face_sets.span, faces, new_face_sets)) {
319 return;
320 }
321
322 undo::push_node(depsgraph, object, &nodes[i], undo::Type::FaceSet);
323 scatter_data_mesh(new_face_sets.as_span(), faces, face_sets.span);
324 node_changed[i] = true;
325 },
327 }
328 else if (pbvh.type() == bke::pbvh::Type::Grids) {
330 node_mask.foreach_index(
331 [&](const int i) {
332 TLS &tls = all_tls.local();
334 *ss.subdiv_ccg, nodes[i], tls.face_indices);
335
336 tls.new_face_sets.resize(faces.size());
337 MutableSpan<int> new_face_sets = tls.new_face_sets;
338 gather_data_mesh(face_sets.span.as_span(), faces, new_face_sets);
339 calc_face_sets(faces, new_face_sets);
340 if (array_utils::indexed_data_equal<int>(face_sets.span, faces, new_face_sets)) {
341 return;
342 }
343
344 undo::push_node(depsgraph, object, &nodes[i], undo::Type::FaceSet);
345 scatter_data_mesh(new_face_sets.as_span(), faces, face_sets.span);
346 node_changed[i] = true;
347 },
349 }
350
351 IndexMaskMemory memory;
352 pbvh.tag_face_sets_changed(IndexMask::from_bools(node_changed, memory));
353 face_sets.finish();
354}
355
356enum class CreateMode {
358 Visible = 1,
359 All = 2,
361};
362
363static void clear_face_sets(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask)
364{
365 Mesh &mesh = *id_cast<Mesh *>(object.data);
366 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
367 if (!attributes.contains(".sculpt_face_set")) {
368 return;
369 }
370 SculptSession &ss = *object.runtime->sculpt_session;
372
373 Array<bool> node_changed(pbvh.nodes_num(), false);
374
375 const int default_face_set = mesh.face_sets_color_default;
376 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
377 if (pbvh.type() == bke::pbvh::Type::Mesh) {
379 node_mask.foreach_index(
380 [&](const int i) {
381 const Span<int> faces = nodes[i].faces();
382 if (std::any_of(faces.begin(),
383 faces.end(),
384 [&](const int face) { return face_sets[face] != default_face_set; }))
385 {
386 undo::push_node(depsgraph, object, &nodes[i], undo::Type::FaceSet);
387 node_changed[i] = true;
388 }
389 },
391 }
392 else if (pbvh.type() == bke::pbvh::Type::Grids) {
395 node_mask.foreach_index(
396 [&](const int i) {
397 Vector<int> &face_indices = all_face_indices.local();
399 *ss.subdiv_ccg, nodes[i], face_indices);
400 if (std::any_of(faces.begin(),
401 faces.end(),
402 [&](const int face) { return face_sets[face] != default_face_set; }))
403 {
404 undo::push_node(depsgraph, object, &nodes[i], undo::Type::FaceSet);
405 node_changed[i] = true;
406 }
407 },
409 }
410 IndexMaskMemory memory;
411 pbvh.tag_face_sets_changed(IndexMask::from_bools(node_changed, memory));
412 attributes.remove(".sculpt_face_set");
413}
414
416{
417 const Scene &scene = *CTX_data_scene(C);
418 Object &object = *CTX_data_active_object(C);
419 Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
420
421 const CreateMode mode = CreateMode(RNA_enum_get(op->ptr, "mode"));
422
423 const View3D *v3d = CTX_wm_view3d(C);
424 const Base *base = CTX_data_active_base(C);
425 if (!BKE_base_is_visible(v3d, base)) {
426 return OPERATOR_CANCELLED;
427 }
428
430
432 if (pbvh.type() == bke::pbvh::Type::BMesh) {
433 /* Dyntopo not supported. */
434 return OPERATOR_CANCELLED;
435 }
436
437 Mesh &mesh = *id_cast<Mesh *>(object.data);
438 const bke::AttributeAccessor attributes = mesh.attributes();
439
440 BKE_sculptsession_update_for_edit(&depsgraph, &object, false);
441
442 undo::push_begin(scene, object, op);
443
444 const int next_face_set = find_next_available_id(object);
445
446 IndexMaskMemory memory;
447 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
448 switch (mode) {
449 case CreateMode::Masked: {
450 if (pbvh.type() == bke::pbvh::Type::Mesh) {
451 const OffsetIndices faces = mesh.faces();
452 const Span<int> corner_verts = mesh.corner_verts();
453 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly",
455 const VArraySpan<float> mask = *attributes.lookup<float>(".sculpt_mask",
457 if (!mask.is_empty()) {
458 face_sets_update(depsgraph,
459 object,
460 node_mask,
461 [&](const Span<int> indices, MutableSpan<int> face_sets) {
462 for (const int i : indices.index_range()) {
463 if (!hide_poly.is_empty() && hide_poly[indices[i]]) {
464 continue;
465 }
466 const Span<int> face_verts = corner_verts.slice(faces[indices[i]]);
467 if (!std::any_of(face_verts.begin(),
468 face_verts.end(),
469 [&](const int vert) { return mask[vert] > 0.5f; }))
470 {
471 continue;
472 }
473 face_sets[i] = next_face_set;
474 }
475 });
476 }
477 }
478 else if (pbvh.type() == bke::pbvh::Type::Grids) {
479 const OffsetIndices<int> faces = mesh.faces();
480 const SculptSession &ss = *object.runtime->sculpt_session;
481 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
482 const int grid_area = subdiv_ccg.grid_area;
483 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly",
485 const Span<float> masks = subdiv_ccg.masks;
486 if (!masks.is_empty()) {
487 face_sets_update(depsgraph,
488 object,
489 node_mask,
490 [&](const Span<int> indices, MutableSpan<int> face_sets) {
491 for (const int i : indices.index_range()) {
492 if (!hide_poly.is_empty() && hide_poly[indices[i]]) {
493 continue;
494 }
495
496 const Span<float> face_masks = masks.slice(
497 bke::ccg::face_range(faces, grid_area, indices[i]));
498 if (!std::any_of(face_masks.begin(),
499 face_masks.end(),
500 [&](const float mask) { return mask > 0.5f; }))
501 {
502 continue;
503 }
504 face_sets[i] = next_face_set;
505 }
506 });
507 }
508 }
509 break;
510 }
511 case CreateMode::Visible: {
512 const VArray<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
513 switch (array_utils::booleans_mix_calc(hide_poly)) {
517 /* If all vertices in the sculpt are visible, remove face sets and update the default
518 * color. This way the new face set will be white, and it is a quick way of disabling all
519 * face sets and the performance hit of rendering the overlay. */
520 clear_face_sets(depsgraph, object, node_mask);
521 break;
523 const VArraySpan<bool> hide_poly_span(hide_poly);
524 face_sets_update(depsgraph,
525 object,
526 node_mask,
527 [&](const Span<int> indices, MutableSpan<int> face_sets) {
528 for (const int i : indices.index_range()) {
529 if (!hide_poly_span[indices[i]]) {
530 face_sets[i] = next_face_set;
531 }
532 }
533 });
534 break;
535 }
536 break;
537 }
538 case CreateMode::All: {
539 face_sets_update(depsgraph,
540 object,
541 node_mask,
542 [&](const Span<int> /*indices*/, MutableSpan<int> face_sets) {
543 face_sets.fill(next_face_set);
544 });
545 break;
546 }
548 const VArraySpan<bool> select_poly = *attributes.lookup_or_default<bool>(
549 ".select_poly", bke::AttrDomain::Face, false);
550 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly",
552
554 depsgraph, object, node_mask, [&](const Span<int> indices, MutableSpan<int> face_sets) {
555 for (const int i : indices.index_range()) {
556 if (select_poly[indices[i]]) {
557 if (!hide_poly.is_empty() && hide_poly[indices[i]]) {
558 continue;
559 }
560 face_sets[i] = next_face_set;
561 }
562 }
563 });
564
565 break;
566 }
567 }
568
569 undo::push_end(object);
570
572
573 return OPERATOR_FINISHED;
574}
575
577{
578 ot->name = "Create Face Set";
579 ot->idname = "SCULPT_OT_face_sets_create";
580 ot->description = "Create a new face set";
581
582 ot->exec = create_op_exec;
584
586
587 static EnumPropertyItem modes[] = {
588 {int(CreateMode::Masked),
589 "MASKED",
590 0,
591 "Face Set from Masked",
592 "Create a new face set from the masked faces"},
594 "VISIBLE",
595 0,
596 "Face Set from Visible",
597 "Create a new face set from the visible vertices"},
598 {int(CreateMode::All),
599 "ALL",
600 0,
601 "Face Set Full Mesh",
602 "Create a unique face set with all faces in the sculpt"},
604 "SELECTION",
605 0,
606 "Face Set from Edit Mode Selection",
607 "Create a face set corresponding to the Edit Mode face selection"},
608 {0, nullptr, 0, nullptr, nullptr},
609 };
610 RNA_def_enum(ot->srna, "mode", modes, int(CreateMode::Masked), "Mode", "");
611}
612
623
624using FaceSetsFloodFillFn = FunctionRef<bool(int from_face, int edge, int to_face)>;
625
626static void init_flood_fill(Object &ob, const FaceSetsFloodFillFn &test_fn)
627{
629 Mesh *mesh = id_cast<Mesh *>(ob.data);
630
631 BitVector<> visited_faces(mesh->faces_num, false);
632
634
635 const Span<int2> edges = mesh->edges();
636 const OffsetIndices faces = mesh->faces();
637 const Span<int> corner_edges = mesh->corner_edges();
638
639 if (ss.edge_to_face_map.is_empty()) {
641 faces, corner_edges, edges.size(), ss.edge_to_face_offsets, ss.edge_to_face_indices);
642 }
643
644 const bke::AttributeAccessor attributes = mesh->attributes();
645 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
646 const Set<int> hidden_face_sets = gather_hidden_face_sets(hide_poly, face_sets.span);
647
648 int next_face_set = 1;
649
650 for (const int i : faces.index_range()) {
651 if (!hide_poly.is_empty() && hide_poly[i]) {
652 continue;
653 }
654 if (visited_faces[i]) {
655 continue;
656 }
657 std::queue<int> queue;
658
659 while (hidden_face_sets.contains(next_face_set)) {
660 next_face_set += 1;
661 }
662 face_sets.span[i] = next_face_set;
663 visited_faces[i].set(true);
664 queue.push(i);
665
666 while (!queue.empty()) {
667 const int face_i = queue.front();
668 queue.pop();
669
670 for (const int edge_i : corner_edges.slice(faces[face_i])) {
671 for (const int neighbor_i : ss.edge_to_face_map[edge_i]) {
672 if (neighbor_i == face_i) {
673 continue;
674 }
675 if (visited_faces[neighbor_i]) {
676 continue;
677 }
678 if (!hide_poly.is_empty() && hide_poly[neighbor_i]) {
679 continue;
680 }
681 if (!test_fn(face_i, edge_i, neighbor_i)) {
682 continue;
683 }
684
685 face_sets.span[neighbor_i] = next_face_set;
686 visited_faces[neighbor_i].set(true);
687 queue.push(neighbor_i);
688 }
689 }
690 }
691
692 next_face_set += 1;
693 }
694
695 face_sets.finish();
696}
697
698Set<int> gather_hidden_face_sets(const Span<bool> hide_poly, const Span<int> face_sets)
699{
700 if (hide_poly.is_empty()) {
701 return {};
702 }
703
704 Set<int> hidden_face_sets;
705 for (const int i : hide_poly.index_range()) {
706 if (hide_poly[i]) {
707 hidden_face_sets.add(face_sets[i]);
708 }
709 }
710
711 return hidden_face_sets;
712}
713
715{
716 const Scene &scene = *CTX_data_scene(C);
718 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
719
720 const InitMode mode = InitMode(RNA_enum_get(op->ptr, "mode"));
721
722 const View3D *v3d = CTX_wm_view3d(C);
723 const Base *base = CTX_data_active_base(C);
724 if (!BKE_base_is_visible(v3d, base)) {
725 return OPERATOR_CANCELLED;
726 }
727
729
730 BKE_sculptsession_update_for_edit(depsgraph, &ob, false);
731
733 /* Dyntopo not supported. */
734 if (pbvh.type() == bke::pbvh::Type::BMesh) {
735 return OPERATOR_CANCELLED;
736 }
737
738 IndexMaskMemory memory;
739 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
740 if (node_mask.is_empty()) {
741 return OPERATOR_CANCELLED;
742 }
743
744 undo::push_begin(scene, ob, op);
745 undo::push_nodes(*depsgraph, ob, node_mask, undo::Type::FaceSet);
746
747 const float threshold = RNA_float_get(op->ptr, "threshold");
748
749 Mesh *mesh = id_cast<Mesh *>(ob.data);
750 const bke::AttributeAccessor attributes = mesh->attributes();
751
752 switch (mode) {
754 const VArray<bool> hide_poly = *attributes.lookup_or_default<bool>(
755 ".hide_poly", bke::AttrDomain::Face, false);
756 init_flood_fill(ob, [&](const int from_face, const int /*edge*/, const int to_face) {
757 return hide_poly[from_face] == hide_poly[to_face];
758 });
759 break;
760 }
761 case InitMode::Materials: {
763 const VArraySpan<int> material_indices = *attributes.lookup_or_default<int>(
764 "material_index", bke::AttrDomain::Face, 0);
765 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly",
767 for (const int i : IndexRange(mesh->faces_num)) {
768 if (!hide_poly.is_empty() && hide_poly[i]) {
769 continue;
770 }
771
772 /* In some cases material face set index could be same as hidden face set index
773 * A more robust implementation is needed to avoid this */
774 face_sets.span[i] = material_indices[i] + 1;
775 }
776
777 face_sets.finish();
778 break;
779 }
780 case InitMode::Normals: {
781 const Span<float3> face_normals = mesh->face_normals();
782 init_flood_fill(ob, [&](const int from_face, const int /*edge*/, const int to_face) -> bool {
783 return std::abs(math::dot(face_normals[from_face], face_normals[to_face])) > threshold;
784 });
785 break;
786 }
787 case InitMode::UVSeams: {
788 const VArraySpan<bool> uv_seams = *mesh->attributes().lookup_or_default<bool>(
789 "uv_seam", bke::AttrDomain::Edge, false);
791 [&](const int /*from_face*/, const int edge, const int /*to_face*/) -> bool {
792 return !uv_seams[edge];
793 });
794 break;
795 }
796 case InitMode::Creases: {
797 const VArraySpan<float> creases = *attributes.lookup_or_default<float>(
798 "crease_edge", bke::AttrDomain::Edge, 0.0f);
800 [&](const int /*from_face*/, const int edge, const int /*to_face*/) -> bool {
801 return creases[edge] < threshold;
802 });
803 break;
804 }
806 const VArraySpan<bool> sharp_edges = *mesh->attributes().lookup_or_default<bool>(
807 "sharp_edge", bke::AttrDomain::Edge, false);
809 [&](const int /*from_face*/, const int edge, const int /*to_face*/) -> bool {
810 return !sharp_edges[edge];
811 });
812 break;
813 }
815 const VArraySpan<float> bevel_weights = *attributes.lookup_or_default<float>(
816 "bevel_weight_edge", bke::AttrDomain::Edge, 0.0f);
818 [&](const int /*from_face*/, const int edge, const int /*to_face*/) -> bool {
819 return bevel_weights[edge] < threshold;
820 });
821 break;
822 }
824 Array<int> face_sets_copy = duplicate_face_sets(*mesh);
825 init_flood_fill(ob, [&](const int from_face, const int /*edge*/, const int to_face) -> bool {
826 return face_sets_copy[from_face] == face_sets_copy[to_face];
827 });
828 break;
829 }
830 }
831
832 undo::push_end(ob);
833
834 pbvh.tag_face_sets_changed(node_mask);
835
837
838 return OPERATOR_FINISHED;
839}
840
842{
843 ot->name = "Init Face Sets";
844 ot->idname = "SCULPT_OT_face_sets_init";
845 ot->description = "Initializes all face sets in the mesh";
846
847 ot->exec = init_op_exec;
849
851
852 static EnumPropertyItem modes[] = {
854 "LOOSE_PARTS",
855 0,
856 "Face Sets from Loose Parts",
857 "Create a face set per loose part in the mesh"},
859 "MATERIALS",
860 0,
861 "Face Sets from Material Slots",
862 "Create a face set per material slot"},
863 {int(InitMode::Normals),
864 "NORMALS",
865 0,
866 "Face Sets from Mesh Normals",
867 "Create face sets for faces that have similar normal"},
868 {int(InitMode::UVSeams),
869 "UV_SEAMS",
870 0,
871 "Face Sets from UV Seams",
872 "Create face sets using UV seams as boundaries"},
873 {int(InitMode::Creases),
874 "CREASES",
875 0,
876 "Face Sets from Edge Creases",
877 "Create face sets using edge creases as boundaries"},
879 "BEVEL_WEIGHT",
880 0,
881 "Face Sets from Bevel Weight",
882 "Create face sets using bevel weights as boundaries"},
884 "SHARP_EDGES",
885 0,
886 "Face Sets from Sharp Edges",
887 "Create face sets using sharp edges as boundaries"},
889 "FACE_SET_BOUNDARIES",
890 0,
891 "Face Sets from Face Set Boundaries",
892 "Create a face set per isolated face set"},
893 {0, nullptr, 0, nullptr, nullptr},
894 };
895 RNA_def_enum(ot->srna, "mode", modes, int(InitMode::LooseParts), "Mode", "");
897 ot->srna,
898 "threshold",
899 0.5f,
900 0.0f,
901 1.0f,
902 "Threshold",
903 "Minimum value to consider a certain attribute a boundary when creating the face sets",
904 0.0f,
905 1.0f);
906}
907
908enum class VisibilityMode {
909 Toggle = 0,
912};
913
914static void face_hide_update(const Depsgraph &depsgraph,
915 Object &object,
916 const IndexMask &node_mask,
917 const FunctionRef<void(Span<int>, MutableSpan<bool>)> calc_hide)
918{
919 SculptSession &ss = *object.runtime->sculpt_session;
921 Mesh &mesh = *id_cast<Mesh *>(object.data);
922 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
924 ".hide_poly", bke::AttrDomain::Face);
925
926 struct TLS {
927 Vector<int> face_indices;
928 Vector<bool> new_hide;
929 };
930
931 Array<bool> node_changed(node_mask.min_array_size(), false);
932
934 if (pbvh.type() == bke::pbvh::Type::Mesh) {
936 node_mask.foreach_index(
937 [&](const int i) {
938 TLS &tls = all_tls.local();
939 const Span<int> faces = nodes[i].faces();
940
941 tls.new_hide.resize(faces.size());
942 MutableSpan<bool> new_hide = tls.new_hide;
943 gather_data_mesh(hide_poly.span.as_span(), faces, new_hide);
944 calc_hide(faces, new_hide);
945 if (array_utils::indexed_data_equal<bool>(hide_poly.span, faces, new_hide)) {
946 return;
947 }
948
949 undo::push_node(depsgraph, object, &nodes[i], undo::Type::HideFace);
950 scatter_data_mesh(new_hide.as_span(), faces, hide_poly.span);
951 node_changed[i] = true;
952 },
954 }
955 else if (pbvh.type() == bke::pbvh::Type::Grids) {
957 node_mask.foreach_index(
958 [&](const int i) {
959 TLS &tls = all_tls.local();
961 *ss.subdiv_ccg, nodes[i], tls.face_indices);
962
963 tls.new_hide.resize(faces.size());
964 MutableSpan<bool> new_hide = tls.new_hide;
965 gather_data_mesh(hide_poly.span.as_span(), faces, new_hide);
966 calc_hide(faces, new_hide);
967 if (array_utils::indexed_data_equal<bool>(hide_poly.span, faces, new_hide)) {
968 return;
969 }
970
971 undo::push_node(depsgraph, object, &nodes[i], undo::Type::HideFace);
972 scatter_data_mesh(new_hide.as_span(), faces, hide_poly.span);
973 node_changed[i] = true;
974 },
976 }
977
978 hide_poly.finish();
979
980 IndexMaskMemory memory;
981 const IndexMask changed_nodes = IndexMask::from_bools(node_changed, memory);
982 if (changed_nodes.is_empty()) {
983 return;
984 }
986 pbvh.tag_visibility_changed(node_mask);
987}
988
989static void show_all(Depsgraph &depsgraph, Object &object, const IndexMask &node_mask)
990{
991 switch (bke::object::pbvh_get(object)->type()) {
993 hide::mesh_show_all(depsgraph, object, node_mask);
994 break;
996 hide::grids_show_all(depsgraph, object, node_mask);
997 break;
1000 break;
1001 }
1002}
1003
1005{
1006 const Scene &scene = *CTX_data_scene(C);
1007 Object &object = *CTX_data_active_object(C);
1008 SculptSession &ss = *object.runtime->sculpt_session;
1009 Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
1010
1012 BKE_sculptsession_update_for_edit(&depsgraph, &object, false);
1013
1015
1016 if (pbvh.type() == bke::pbvh::Type::BMesh) {
1017 /* Not supported for dyntopo. There is no active face. */
1018 return OPERATOR_CANCELLED;
1019 }
1020
1021 const VisibilityMode mode = VisibilityMode(RNA_enum_get(op->ptr, "mode"));
1022 const int active_face_set = RNA_int_get(op->ptr, "active_face_set");
1023
1024 undo::push_begin(scene, object, op);
1025
1026 IndexMaskMemory memory;
1027 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1028
1029 const bke::AttributeAccessor attributes = mesh->attributes();
1030 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1031 const VArraySpan<int> face_sets = *attributes.lookup<int>(".sculpt_face_set",
1033
1034 switch (mode) {
1036 if (hide_poly.contains(true) || face_sets.is_empty()) {
1037 show_all(depsgraph, object, node_mask);
1038 }
1039 else {
1041 depsgraph, object, node_mask, [&](const Span<int> faces, MutableSpan<bool> hide) {
1042 for (const int i : hide.index_range()) {
1043 hide[i] = face_sets[faces[i]] != active_face_set;
1044 }
1045 });
1046 }
1047 break;
1048 }
1050 if (face_sets.is_empty()) {
1051 show_all(depsgraph, object, node_mask);
1052 }
1053 else {
1055 depsgraph, object, node_mask, [&](const Span<int> faces, MutableSpan<bool> hide) {
1056 for (const int i : hide.index_range()) {
1057 if (face_sets[faces[i]] == active_face_set) {
1058 hide[i] = false;
1059 }
1060 }
1061 });
1062 }
1063 break;
1065 if (face_sets.is_empty()) {
1067 depsgraph, object, node_mask, [&](const Span<int> /*faces*/, MutableSpan<bool> hide) {
1068 hide.fill(true);
1069 });
1070 }
1071 else {
1073 depsgraph, object, node_mask, [&](const Span<int> faces, MutableSpan<bool> hide) {
1074 for (const int i : hide.index_range()) {
1075 if (face_sets[faces[i]] == active_face_set) {
1076 hide[i] = true;
1077 }
1078 }
1079 });
1080 }
1081 break;
1082 }
1083
1084 /* For modes that use the cursor active vertex, update the rotation origin for viewport
1085 * navigation. */
1088 bke::PaintRuntime *paint_runtime = paint->runtime;
1089 if (std::holds_alternative<std::monostate>(ss.active_vert())) {
1090 paint_runtime->last_stroke_valid = false;
1091 }
1092 else {
1093 float location[3];
1094 copy_v3_v3(location, ss.active_vert_position(depsgraph, object));
1095 mul_m4_v3(object.object_to_world().ptr(), location);
1097 }
1098 }
1099
1100 undo::push_end(object);
1101
1102 pbvh.update_visibility(object);
1103
1104 islands::invalidate(*object.runtime->sculpt_session);
1106
1107 return OPERATOR_FINISHED;
1108}
1109
1111{
1113
1114 const View3D *v3d = CTX_wm_view3d(C);
1115 const Base *base = CTX_data_active_base(C);
1116 if (!BKE_base_is_visible(v3d, base)) {
1117 return OPERATOR_CANCELLED;
1118 }
1119
1120 /* Update the active vertex and face set using the cursor position to avoid relying on the paint
1121 * cursor updates. */
1122 const float mval_fl[2] = {float(event->mval[0]), float(event->mval[1])};
1124 cursor_geometry_info_update(C, mval_fl, false);
1125
1126 const int active_face_set = active_face_set_get(ob);
1127 RNA_int_set(op->ptr, "active_face_set", active_face_set);
1128
1129 return change_visibility_exec(C, op);
1130}
1131
1133{
1134 ot->name = "Face Sets Visibility";
1135 ot->idname = "SCULPT_OT_face_set_change_visibility";
1136 ot->description = "Change the visibility of the face sets of the sculpt";
1137
1140 ot->poll = sculpt_mode_poll;
1141
1143
1144 static EnumPropertyItem modes[] = {
1146 "TOGGLE",
1147 0,
1148 "Toggle Visibility",
1149 "Hide all face sets except for the active one"},
1151 "SHOW_ACTIVE",
1152 0,
1153 "Show Active Face Set",
1154 "Show the active face set"},
1156 "HIDE_ACTIVE",
1157 0,
1158 "Hide Active Face Set",
1159 "Hide the active face set"},
1160 {0, nullptr, 0, nullptr, nullptr},
1161 };
1162 RNA_def_enum(ot->srna, "mode", modes, int(VisibilityMode::Toggle), "Mode", "");
1163
1164 PropertyRNA *prop = RNA_def_int(
1165 ot->srna, "active_face_set", 0, 0, INT_MAX, "Active Face Set", "", 0, INT_MAX);
1167}
1168
1170{
1172
1173 const View3D *v3d = CTX_wm_view3d(C);
1174 const Base *base = CTX_data_active_base(C);
1175 if (!BKE_base_is_visible(v3d, base)) {
1176 return OPERATOR_CANCELLED;
1177 }
1178
1180
1181 /* Dyntopo not supported. */
1182 if (pbvh.type() == bke::pbvh::Type::BMesh) {
1183 return OPERATOR_CANCELLED;
1184 }
1185
1186 Mesh *mesh = id_cast<Mesh *>(ob.data);
1187 const bke::AttributeAccessor attributes = mesh->attributes();
1188
1189 if (!attributes.contains(".sculpt_face_set")) {
1190 return OPERATOR_CANCELLED;
1191 }
1192
1193 const VArray<int> face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
1194 const int random_index = clamp_i(mesh->faces_num * BLI_hash_int_01(mesh->face_sets_color_seed),
1195 0,
1196 max_ii(0, mesh->faces_num - 1));
1197 mesh->face_sets_color_default = face_sets[random_index];
1198
1199 mesh->face_sets_color_seed += 1;
1200
1201 IndexMaskMemory memory;
1202 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1203 pbvh.tag_face_sets_changed(node_mask);
1204
1206
1207 return OPERATOR_FINISHED;
1208}
1209
1211{
1212 ot->name = "Randomize Face Sets Colors";
1213 ot->idname = "SCULPT_OT_face_sets_randomize_colors";
1214 ot->description = "Generates a new set of random colors to render the face sets in the viewport";
1215
1217 ot->poll = sculpt_mode_poll;
1218
1220}
1221
1229
1230static void edit_grow_shrink(const Depsgraph &depsgraph,
1231 const Scene &scene,
1232 Object &object,
1233 const EditMode mode,
1234 const int active_face_set_id,
1235 const bool modify_hidden,
1236 wmOperator *op)
1237{
1239 Mesh &mesh = *id_cast<Mesh *>(object.data);
1240 const OffsetIndices faces = mesh.faces();
1241 const Span<int> corner_verts = mesh.corner_verts();
1242 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1243 const bke::AttributeAccessor attributes = mesh.attributes();
1244
1245 BLI_assert(attributes.contains(".sculpt_face_set"));
1246
1247 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1248 Array<int> prev_face_sets = duplicate_face_sets(mesh);
1249
1250 undo::push_begin(scene, object, op);
1251
1252 IndexMaskMemory memory;
1253 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1255 depsgraph, object, node_mask, [&](const Span<int> indices, MutableSpan<int> face_sets) {
1256 for (const int i : indices.index_range()) {
1257 const int face = indices[i];
1258 if (!modify_hidden && !hide_poly.is_empty() && hide_poly[face]) {
1259 continue;
1260 }
1261 if (mode == EditMode::Grow) {
1262 for (const int vert : corner_verts.slice(faces[face])) {
1263 for (const int neighbor_face_index : vert_to_face_map[vert]) {
1264 if (neighbor_face_index == face) {
1265 continue;
1266 }
1267 if (prev_face_sets[neighbor_face_index] == active_face_set_id) {
1268 face_sets[i] = active_face_set_id;
1269 }
1270 }
1271 }
1272 }
1273 else {
1274 if (prev_face_sets[face] == active_face_set_id) {
1275 for (const int vert_i : corner_verts.slice(faces[face])) {
1276 for (const int neighbor_face_index : vert_to_face_map[vert_i]) {
1277 if (neighbor_face_index == face) {
1278 continue;
1279 }
1280 if (prev_face_sets[neighbor_face_index] != active_face_set_id) {
1281 face_sets[i] = prev_face_sets[neighbor_face_index];
1282 }
1283 }
1284 }
1285 }
1286 }
1287 }
1288 });
1289
1290 undo::push_end(object);
1291}
1292
1293static bool check_single_face_set(const Object &object, const bool check_visible_only)
1294{
1295 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1296 const bke::AttributeAccessor attributes = mesh.attributes();
1297 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1298 const VArraySpan<int> face_sets = *attributes.lookup<int>(".sculpt_face_set",
1300
1301 if (face_sets.is_empty()) {
1302 return true;
1303 }
1304 int first_face_set = face_set_none_id;
1305 if (check_visible_only) {
1306 for (const int i : face_sets.index_range()) {
1307 if (!hide_poly.is_empty() && hide_poly[i]) {
1308 continue;
1309 }
1310 first_face_set = face_sets[i];
1311 break;
1312 }
1313 }
1314 else {
1315 first_face_set = face_sets[0];
1316 }
1317
1318 if (first_face_set == face_set_none_id) {
1319 return true;
1320 }
1321
1322 for (const int i : face_sets.index_range()) {
1323 if (check_visible_only && !hide_poly.is_empty() && hide_poly[i]) {
1324 continue;
1325 }
1326 if (face_sets[i] != first_face_set) {
1327 return false;
1328 }
1329 }
1330 return true;
1331}
1332
1333static void delete_geometry(Object &ob, const int active_face_set_id, const bool modify_hidden)
1334{
1335 Mesh &mesh = *id_cast<Mesh *>(ob.data);
1336 const bke::AttributeAccessor attributes = mesh.attributes();
1337 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1338 const VArraySpan<int> face_sets = *attributes.lookup<int>(".sculpt_face_set",
1340
1341 const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(&mesh);
1342 BMeshCreateParams create_params{};
1343 create_params.use_toolflags = true;
1344 BMesh *bm = BM_mesh_create(&allocsize, &create_params);
1345
1346 BMeshFromMeshParams convert_params{};
1347 convert_params.calc_vert_normal = true;
1348 convert_params.calc_face_normal = true;
1349 BM_mesh_bm_from_me(bm, &mesh, &convert_params);
1350
1354 BMIter iter;
1355 BMFace *f;
1356 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
1357 const int face_index = BM_elem_index_get(f);
1358 if (!modify_hidden && !hide_poly.is_empty() && hide_poly[face_index]) {
1359 continue;
1360 }
1361 BM_elem_flag_set(f, BM_ELEM_TAG, face_sets[face_index] == active_face_set_id);
1362 }
1365
1366 BMeshToMeshParams bmesh_to_mesh_params{};
1367 bmesh_to_mesh_params.calc_object_remap = false;
1368 BM_mesh_bm_to_me(nullptr, bm, &mesh, &bmesh_to_mesh_params);
1369
1371}
1372
1373static void edit_fairing(const Depsgraph &depsgraph,
1374 const Sculpt &sd,
1375 Object &ob,
1376 const int active_face_set_id,
1377 const eMeshFairingDepth fair_order,
1378 const float strength)
1379{
1381 Mesh &mesh = *id_cast<Mesh *>(ob.data);
1384
1385 const PositionDeformData position_data(depsgraph, ob);
1386 const Span<float3> positions = position_data.eval;
1387 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1388 const BitSpan boundary_verts = ss.boundary_info_cache->verts;
1389 const bke::AttributeAccessor attributes = mesh.attributes();
1390 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1391 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
1392
1393 Array<bool> fair_verts(positions.size(), false);
1394 for (const int vert : positions.index_range()) {
1395 if (boundary::vert_is_boundary(vert_to_face_map, hide_poly, boundary_verts, vert)) {
1396 continue;
1397 }
1398 if (!vert_has_face_set(vert_to_face_map, face_sets, vert, active_face_set_id)) {
1399 continue;
1400 }
1401 if (!vert_has_unique_face_set(vert_to_face_map, face_sets, vert)) {
1402 continue;
1403 }
1404 fair_verts[vert] = true;
1405 }
1406
1407 Array<float3> new_positions = positions;
1408 BKE_mesh_prefair_and_fair_verts(&mesh, new_positions, fair_verts.data(), fair_order);
1409
1410 struct LocalData {
1411 Vector<float3> translations;
1412 };
1413
1414 IndexMaskMemory memory;
1415 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1417
1419 node_mask.foreach_index(
1420 [&](const int i) {
1421 LocalData &tls = all_tls.local();
1422 const Span<int> verts = nodes[i].verts();
1423 tls.translations.resize(verts.size());
1424 const MutableSpan<float3> translations = tls.translations;
1425 for (const int i : verts.index_range()) {
1426 translations[i] = new_positions[verts[i]] - positions[verts[i]];
1427 }
1428 scale_translations(translations, strength);
1429 clip_and_lock_translations(sd, ss, positions, verts, translations);
1430 position_data.deform(translations, verts);
1431 },
1433}
1434
1435static bool edit_is_operation_valid(const Object &object,
1436 const EditMode mode,
1437 const bool modify_hidden)
1438{
1439 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1440 if (pbvh.type() == bke::pbvh::Type::BMesh) {
1441 /* Dyntopo is not supported. */
1442 return false;
1443 }
1444
1445 if (mode == EditMode::DeleteGeometry) {
1446 if (pbvh.type() == bke::pbvh::Type::Grids) {
1447 /* Modification of base mesh geometry requires special remapping of multi-resolution
1448 * displacement, which does not happen here.
1449 * Disable delete operation. It can be supported in the future by doing similar displacement
1450 * data remapping as what happens in the mesh edit mode. */
1451 return false;
1452 }
1453 if (check_single_face_set(object, !modify_hidden)) {
1454 /* Cancel the operator if the mesh only contains one face set to avoid deleting the
1455 * entire object. */
1456 return false;
1457 }
1458 }
1459
1461 if (pbvh.type() == bke::pbvh::Type::Grids) {
1462 /* TODO: Multi-resolution topology representation using grids and duplicates can't be used
1463 * directly by the fair algorithm. Multi-resolution topology needs to be exposed in a
1464 * different way or converted to a mesh for this operation. */
1465 return false;
1466 }
1467 }
1468
1469 if (ELEM(mode, EditMode::Grow, EditMode::Shrink)) {
1470 if (pbvh.type() == bke::pbvh::Type::Mesh) {
1471 const Mesh &mesh = *id_cast<Mesh *>(object.data);
1472 const bke::AttributeAccessor attributes = mesh.attributes();
1473 if (!attributes.contains(".sculpt_face_set")) {
1474 /* If a mesh does not have the face set attribute, growing or shrinking the face set will
1475 * have no effect, exit early in this case. */
1476 return false;
1477 }
1478 }
1479 }
1480
1481 return true;
1482}
1483
1485 bContext *C, Object &ob, const int active_face_set, const bool modify_hidden, wmOperator *op)
1486{
1487 const Scene &scene = *CTX_data_scene(C);
1488 Mesh *mesh = id_cast<Mesh *>(ob.data);
1489 undo::geometry_begin(scene, ob, op);
1490 delete_geometry(ob, active_face_set, modify_hidden);
1496}
1497
1499 bContext *C, Object &ob, const int active_face_set, const EditMode mode, wmOperator *op)
1500{
1501 const Scene &scene = *CTX_data_scene(C);
1502 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
1503 const Sculpt &sd = *CTX_data_tool_settings(C)->sculpt;
1505 IndexMaskMemory memory;
1506 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1507
1508 const float strength = RNA_float_get(op->ptr, "strength");
1509
1510 undo::push_begin(scene, ob, op);
1511 undo::push_nodes(depsgraph, ob, node_mask, undo::Type::Position);
1512
1513 switch (mode) {
1515 edit_fairing(depsgraph, sd, ob, active_face_set, MESH_FAIRING_DEPTH_POSITION, strength);
1516 break;
1518 edit_fairing(depsgraph, sd, ob, active_face_set, MESH_FAIRING_DEPTH_TANGENCY, strength);
1519 break;
1520 default:
1522 }
1523
1524 pbvh.tag_positions_changed(node_mask);
1525 pbvh.update_bounds(depsgraph, ob);
1528 undo::push_end(ob);
1529}
1530
1532{
1534 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
1535 const EditMode mode = EditMode(RNA_enum_get(op->ptr, "mode"));
1536 const bool modify_hidden = RNA_boolean_get(op->ptr, "modify_hidden");
1537
1538 if (!edit_is_operation_valid(*ob, mode, modify_hidden)) {
1539 return false;
1540 }
1541
1542 BKE_sculptsession_update_for_edit(depsgraph, ob, false);
1543
1544 return true;
1545}
1546
1548{
1549 if (!edit_op_init(C, op)) {
1550 return OPERATOR_CANCELLED;
1551 }
1552
1553 const Scene &scene = *CTX_data_scene(C);
1554 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
1556
1557 const int active_face_set = RNA_int_get(op->ptr, "active_face_set");
1558 const EditMode mode = EditMode(RNA_enum_get(op->ptr, "mode"));
1559 const bool modify_hidden = RNA_boolean_get(op->ptr, "modify_hidden");
1560
1561 if (ELEM(mode, EditMode::Grow, EditMode::Shrink)) {
1563 }
1564
1565 switch (mode) {
1567 edit_modify_geometry(C, ob, active_face_set, modify_hidden, op);
1568 break;
1569 case EditMode::Grow:
1570 case EditMode::Shrink:
1571 edit_grow_shrink(depsgraph, scene, ob, mode, active_face_set, modify_hidden, op);
1572 break;
1575 edit_modify_coordinates(C, ob, active_face_set, mode, op);
1576 break;
1577 }
1578
1580
1581 return OPERATOR_FINISHED;
1582}
1583
1585{
1586 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
1588
1589 const View3D *v3d = CTX_wm_view3d(C);
1590 const Base *base = CTX_data_active_base(C);
1591 if (!BKE_base_is_visible(v3d, base)) {
1592 return OPERATOR_CANCELLED;
1593 }
1594
1595 BKE_sculptsession_update_for_edit(depsgraph, &ob, false);
1596
1597 /* Update the current active face set and Vertex as the operator can be used directly from the
1598 * tool without brush cursor. */
1599 const float mval_fl[2] = {float(event->mval[0]), float(event->mval[1])};
1600 if (!cursor_geometry_info_update(C, mval_fl, false)) {
1601 /* The cursor is not over the mesh. Cancel to avoid editing the last updated face set ID. */
1602 return OPERATOR_CANCELLED;
1603 }
1604 RNA_int_set(op->ptr, "active_face_set", active_face_set_get(ob));
1605
1606 return edit_op_exec(C, op);
1607}
1608
1610{
1611 ot->name = "Edit Face Set";
1612 ot->idname = "SCULPT_OT_face_set_edit";
1613 ot->description = "Edits the current active face set";
1614
1615 ot->invoke = edit_op_invoke;
1616 ot->exec = edit_op_exec;
1617 ot->poll = sculpt_mode_poll;
1618
1620
1621 PropertyRNA *prop = RNA_def_int(
1622 ot->srna, "active_face_set", 1, 0, INT_MAX, "Active Face Set", "", 0, 64);
1624
1625 static EnumPropertyItem modes[] = {
1626 {int(EditMode::Grow),
1627 "GROW",
1628 0,
1629 "Grow Face Set",
1630 "Grows the face set boundary by one face based on mesh topology"},
1631 {int(EditMode::Shrink),
1632 "SHRINK",
1633 0,
1634 "Shrink Face Set",
1635 "Shrinks the face set boundary by one face based on mesh topology"},
1637 "DELETE_GEOMETRY",
1638 0,
1639 "Delete Geometry",
1640 "Deletes the faces that are assigned to the face set"},
1642 "FAIR_POSITIONS",
1643 0,
1644 "Fair Positions",
1645 "Creates the smoothest possible geometry patch from the face set minimizing changes in "
1646 "vertex positions"},
1648 "FAIR_TANGENCY",
1649 0,
1650 "Fair Tangency",
1651 "Creates the smoothest possible geometry patch from the face set minimizing changes in "
1652 "vertex tangents"},
1653 {0, nullptr, 0, nullptr, nullptr},
1654 };
1655 RNA_def_enum(ot->srna, "mode", modes, int(EditMode::Grow), "Mode", "");
1656 prop = RNA_def_float(ot->srna, "strength", 1.0f, 0.0f, 1.0f, "Strength", "", 0.0f, 1.0f);
1658
1659 ot->prop = RNA_def_boolean(ot->srna,
1660 "modify_hidden",
1661 false,
1662 "Modify Hidden",
1663 "Apply the edit operation to hidden geometry");
1664}
1665
1667
1668/* -------------------------------------------------------------------- */
1672
1674 const Set<BMFace *, 0L> &faces,
1675 const MutableSpan<float> r_factors)
1676{
1677 BLI_assert(faces.size() == r_factors.size());
1678
1679 /* TODO: Avoid overhead of accessing attributes for every bke::pbvh::Tree node. */
1680 const int mask_offset = CustomData_get_offset_named(&bm.vdata, CD_PROP_FLOAT, ".sculpt_mask");
1681 int i = 0;
1682 for (BMFace *f : faces) {
1684 r_factors[i] = 0.0f;
1685 i++;
1686 continue;
1687 }
1688 if (mask_offset == -1) {
1689 r_factors[i] = 1.0f;
1690 i++;
1691 continue;
1692 }
1693
1694 const BMLoop *l_iter = f->l_first = BM_FACE_FIRST_LOOP(f);
1695 int total_verts = 0;
1696 float sum = 0.0f;
1697 do {
1698 BMVert *vert = l_iter->v;
1699 sum += BM_ELEM_CD_GET_FLOAT(vert, mask_offset);
1700 total_verts++;
1701 } while ((l_iter = l_iter->next) != f->l_first);
1702 r_factors[i] = 1.0f - sum * math::rcp(float(total_verts));
1703 i++;
1704 }
1705}
1706
1708 const Span<int> face_indices,
1709 const MutableSpan<float> r_factors)
1710{
1711 BLI_assert(face_indices.size() == r_factors.size());
1712
1713 const OffsetIndices<int> faces = mesh.faces();
1714 const Span<int> corner_verts = mesh.corner_verts();
1715
1716 /* TODO: Avoid overhead of accessing attributes for every bke::pbvh::Tree node. */
1717 const bke::AttributeAccessor attributes = mesh.attributes();
1718 if (const VArray mask = *attributes.lookup<float>(".sculpt_mask", bke::AttrDomain::Point)) {
1719 const VArraySpan span(mask);
1720 for (const int i : face_indices.index_range()) {
1721 const Span<int> face_verts = corner_verts.slice(faces[face_indices[i]]);
1722 const float inv_size = math::rcp(float(face_verts.size()));
1723 float sum = 0.0f;
1724 for (const int vert : face_verts) {
1725 sum += span[vert];
1726 }
1727 r_factors[i] = 1.0f - sum * inv_size;
1728 }
1729 }
1730 else {
1731 r_factors.fill(1.0f);
1732 }
1733
1734 if (const VArray hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face)) {
1735 const VArraySpan span(hide_poly);
1736 for (const int i : face_indices.index_range()) {
1737 if (span[face_indices[i]]) {
1738 r_factors[i] = 0.0f;
1739 }
1740 }
1741 }
1742}
1743
1745 const Span<int> corner_verts,
1746 const Span<float3> vert_positions,
1747 const Span<int> face_indices,
1748 const MutableSpan<float3> positions)
1749{
1750 BLI_assert(face_indices.size() == positions.size());
1751
1752 for (const int i : face_indices.index_range()) {
1753 positions[i] = bke::mesh::face_center_calc(vert_positions,
1754 corner_verts.slice(faces[face_indices[i]]));
1755 }
1756}
1757
1759{
1760 BLI_assert(faces.size() == centers.size());
1761
1762 int i = 0;
1763 for (const BMFace *f : faces) {
1764 float3 face_center;
1765 BM_face_calc_center_median(f, face_center);
1766
1767 centers[i] = face_center;
1768 i++;
1769 }
1770}
1771
1772void calc_face_indices_grids(const SubdivCCG &subdiv_ccg,
1773 const Span<int> grids,
1774 const MutableSpan<int> &face_indices)
1775{
1776 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1777 BLI_assert(grids.size() * key.grid_area == face_indices.size());
1778
1779 for (const int i : grids.index_range()) {
1780 const int start = i * key.grid_area;
1781 for (const int offset : IndexRange(key.grid_area)) {
1782 face_indices[start + offset] = BKE_subdiv_ccg_grid_to_face_index(subdiv_ccg, grids[i]);
1783 }
1784 }
1785}
1786
1792
1793static void gesture_begin(bContext &C, wmOperator &op, gesture::GestureData &gesture_data)
1794{
1795 const Scene &scene = *CTX_data_scene(&C);
1796 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(&C);
1797 BKE_sculptsession_update_for_edit(depsgraph, gesture_data.vc.obact, false);
1798 undo::push_begin(scene, *gesture_data.vc.obact, &op);
1799}
1800
1801static void gesture_apply_mesh(gesture::GestureData &gesture_data, const IndexMask &node_mask)
1802{
1803 FaceSetOperation *face_set_operation = reinterpret_cast<FaceSetOperation *>(
1804 gesture_data.operation);
1805 const int new_face_set = face_set_operation->new_face_set_id;
1806 const Depsgraph &depsgraph = *gesture_data.vc.depsgraph;
1807 Object &object = *gesture_data.vc.obact;
1808 Mesh &mesh = *id_cast<Mesh *>(object.data);
1809 SculptSession &ss = *gesture_data.ss;
1811
1812 const Span<float3> positions_eval = bke::pbvh::vert_positions_eval(depsgraph, object);
1813 const OffsetIndices<int> faces = mesh.faces();
1814 const Span<int> corner_verts = mesh.corner_verts();
1816
1817 struct TLS {
1818 Vector<int> face_indices;
1819 Vector<float3> positions;
1820 Vector<float> factors;
1821 };
1822
1823 Array<bool> node_changed(pbvh.nodes_num(), false);
1824
1826 if (pbvh.type() == bke::pbvh::Type::Mesh) {
1828 node_mask.foreach_index(
1829 [&](const int i) {
1830 TLS &tls = all_tls.local();
1831 undo::push_node(depsgraph, *gesture_data.vc.obact, &nodes[i], undo::Type::FaceSet);
1832 bool any_updated = false;
1833
1834 const Span<int> face_indices = nodes[i].faces();
1835
1836 tls.positions.resize(face_indices.size());
1837 const MutableSpan<float3> face_centers = tls.positions;
1839 mesh.faces(), corner_verts, positions_eval, face_indices, face_centers);
1840
1841 tls.factors.resize(face_indices.size());
1842 const MutableSpan<float> factors = tls.factors;
1843 fill_factor_from_hide_and_mask(mesh, face_indices, factors);
1844 filter_region_clip_factors(ss, face_centers, factors);
1845
1846 for (const int idx : face_indices.index_range()) {
1847 if (factors[idx] < FACE_SET_MIN_FADE) {
1848 continue;
1849 }
1850
1851 const int face = face_indices[idx];
1852
1853 const Span<int> face_verts = corner_verts.slice(faces[face]);
1854 const float3 face_normal = bke::mesh::face_normal_calc(positions_eval, face_verts);
1855 if (!gesture::is_affected(gesture_data, face_centers[idx], face_normal)) {
1856 continue;
1857 }
1858 face_sets.span[face] = new_face_set;
1859 any_updated = true;
1860 }
1861 if (any_updated) {
1862 node_changed[i] = true;
1863 }
1864 },
1866 }
1867 else if (pbvh.type() == bke::pbvh::Type::Grids) {
1869 node_mask.foreach_index(
1870 [&](const int i) {
1871 TLS &tls = all_tls.local();
1872 undo::push_node(depsgraph, *gesture_data.vc.obact, &nodes[i], undo::Type::FaceSet);
1873
1874 bool any_updated = false;
1875
1876 const Span<int> grids = nodes[i].grids();
1877 const MutableSpan positions = gather_grids_positions(
1878 *ss.subdiv_ccg, grids, tls.positions);
1879
1880 tls.factors.resize(positions.size());
1881 const MutableSpan<float> factors_grid = tls.factors;
1883 ed::sculpt_paint::filter_region_clip_factors(ss, positions_eval, factors_grid);
1884
1885 tls.face_indices.resize(positions.size());
1886 Vector<int> face_indices_grid = tls.face_indices;
1887 calc_face_indices_grids(*ss.subdiv_ccg, grids, face_indices_grid);
1888
1889 for (const int idx : face_indices_grid.index_range()) {
1890 if (factors_grid[idx] < FACE_SET_MIN_FADE) {
1891 continue;
1892 }
1893
1894 const int face = face_indices_grid[idx];
1895
1896 const Span<int> face_verts = corner_verts.slice(faces[face]);
1897 const float3 face_center = bke::mesh::face_center_calc(positions_eval, face_verts);
1898 const float3 face_normal = bke::mesh::face_normal_calc(positions_eval, face_verts);
1899 if (!gesture::is_affected(gesture_data, face_center, face_normal)) {
1900 continue;
1901 }
1902 face_sets.span[face] = new_face_set;
1903 any_updated = true;
1904 }
1905 if (any_updated) {
1906 node_changed[i] = true;
1907 }
1908 },
1910 }
1911
1912 IndexMaskMemory memory;
1913 pbvh.tag_face_sets_changed(IndexMask::from_bools(node_changed, memory));
1914 face_sets.finish();
1915}
1916
1917static void gesture_apply_bmesh(gesture::GestureData &gesture_data, const IndexMask &node_mask)
1918{
1919 SculptSession &ss = *gesture_data.ss;
1920 FaceSetOperation *face_set_operation = reinterpret_cast<FaceSetOperation *>(
1921 gesture_data.operation);
1922 const Depsgraph &depsgraph = *gesture_data.vc.depsgraph;
1923 const int new_face_set = face_set_operation->new_face_set_id;
1926 const int offset = face_set::ensure_face_sets_bmesh(*gesture_data.vc.obact);
1927
1928 struct TLS {
1929 Vector<float3> positions;
1930 Vector<float> factors;
1931 };
1932
1933 Array<bool> node_changed(node_mask.min_array_size(), false);
1934
1936 node_mask.foreach_index(
1937 [&](const int i) {
1938 TLS &tls = all_tls.local();
1939 undo::push_node(depsgraph, *gesture_data.vc.obact, &nodes[i], undo::Type::FaceSet);
1940
1941 bool any_updated = false;
1942
1944
1945 tls.positions.resize(faces.size());
1946 const MutableSpan<float3> face_centers = tls.positions;
1947 calc_face_centers(faces, face_centers);
1948
1949 tls.factors.resize(faces.size());
1950 const MutableSpan<float> factors = tls.factors;
1951 fill_factor_from_hide_and_mask(*ss.bm, faces, factors);
1952 filter_region_clip_factors(ss, face_centers, factors);
1953
1954 int idx = 0;
1955 for (BMFace *face : faces) {
1956 if (factors[idx] < FACE_SET_MIN_FADE) {
1957 idx++;
1958 continue;
1959 }
1960 if (!gesture::is_affected(gesture_data, face_centers[idx], face->no)) {
1961 idx++;
1962 continue;
1963 }
1964 BM_ELEM_CD_SET_INT(face, offset, new_face_set);
1965 any_updated = true;
1966 idx++;
1967 }
1968 if (any_updated) {
1969 node_changed[i] = true;
1970 }
1971 },
1973
1974 IndexMaskMemory memory;
1975 const IndexMask changed_nodes = IndexMask::from_bools(node_changed, memory);
1976 if (changed_nodes.is_empty()) {
1977 return;
1978 }
1979 pbvh.tag_face_sets_changed(node_mask);
1980}
1981
1983{
1984 switch (bke::object::pbvh_get(*gesture_data.vc.obact)->type()) {
1987 gesture_apply_mesh(gesture_data, gesture_data.node_mask);
1988 break;
1990 gesture_apply_bmesh(gesture_data, gesture_data.node_mask);
1991 break;
1992 }
1993}
1994
1995static void gesture_end(bContext & /*C*/, gesture::GestureData &gesture_data)
1996{
1997 undo::push_end(*gesture_data.vc.obact);
1998}
1999
2000static void init_operation(gesture::GestureData &gesture_data, wmOperator & /*op*/)
2001{
2002 Object &object = *gesture_data.vc.obact;
2003 gesture_data.operation = reinterpret_cast<gesture::Operation *>(
2005
2006 FaceSetOperation *face_set_operation = reinterpret_cast<FaceSetOperation *>(
2007 gesture_data.operation);
2008
2009 face_set_operation->op.begin = gesture_begin;
2011 face_set_operation->op.end = gesture_end;
2012
2013 face_set_operation->new_face_set_id = face_set::find_next_available_id(object);
2014}
2015
2017{
2018 const View3D *v3d = CTX_wm_view3d(C);
2019 const Base *base = CTX_data_active_base(C);
2020 if (!BKE_base_is_visible(v3d, base)) {
2021 return OPERATOR_CANCELLED;
2022 }
2023
2024 return WM_gesture_box_invoke(C, op, event);
2025}
2026
2028{
2029 std::unique_ptr<gesture::GestureData> gesture_data = gesture::init_from_box(C, op);
2030 if (!gesture_data) {
2031 return OPERATOR_CANCELLED;
2032 }
2033 init_operation(*gesture_data, *op);
2035 gesture::apply(*C, *gesture_data, *op);
2036 return OPERATOR_FINISHED;
2037}
2038
2040{
2041 const View3D *v3d = CTX_wm_view3d(C);
2042 const Base *base = CTX_data_active_base(C);
2043 if (!BKE_base_is_visible(v3d, base)) {
2044 return OPERATOR_CANCELLED;
2045 }
2046
2047 return WM_gesture_lasso_invoke(C, op, event);
2048}
2049
2051{
2052 std::unique_ptr<gesture::GestureData> gesture_data = gesture::init_from_lasso(C, op);
2053 if (!gesture_data) {
2054 return OPERATOR_CANCELLED;
2055 }
2056 init_operation(*gesture_data, *op);
2058 gesture::apply(*C, *gesture_data, *op);
2059 return OPERATOR_FINISHED;
2060}
2061
2063{
2064 const View3D *v3d = CTX_wm_view3d(C);
2065 const Base *base = CTX_data_active_base(C);
2066 if (!BKE_base_is_visible(v3d, base)) {
2067 return OPERATOR_CANCELLED;
2068 }
2069
2071}
2072
2074{
2075 std::unique_ptr<gesture::GestureData> gesture_data = gesture::init_from_line(C, op);
2076 if (!gesture_data) {
2077 return OPERATOR_CANCELLED;
2078 }
2079 init_operation(*gesture_data, *op);
2081 gesture::apply(*C, *gesture_data, *op);
2082 return OPERATOR_FINISHED;
2083}
2084
2086{
2087 const View3D *v3d = CTX_wm_view3d(C);
2088 const Base *base = CTX_data_active_base(C);
2089 if (!BKE_base_is_visible(v3d, base)) {
2090 return OPERATOR_CANCELLED;
2091 }
2092
2093 return WM_gesture_polyline_invoke(C, op, event);
2094}
2095
2097{
2098 std::unique_ptr<gesture::GestureData> gesture_data = gesture::init_from_polyline(C, op);
2099 if (!gesture_data) {
2100 return OPERATOR_CANCELLED;
2101 }
2102 init_operation(*gesture_data, *op);
2104 gesture::apply(*C, *gesture_data, *op);
2105 return OPERATOR_FINISHED;
2106}
2107
2109{
2110 ot->name = "Face Set Lasso Gesture";
2111 ot->idname = "SCULPT_OT_face_set_polyline_gesture";
2112 ot->description = "Add a face set in a shape defined by the cursor";
2113
2117
2119
2121
2124}
2125
2127{
2128 ot->name = "Face Set Box Gesture";
2129 ot->idname = "SCULPT_OT_face_set_box_gesture";
2130 ot->description = "Add a face set in a rectangle defined by the cursor";
2131
2134 ot->exec = gesture_box_exec;
2135
2137
2138 ot->flag = OPTYPE_REGISTER;
2139
2142}
2143
2145{
2146 ot->name = "Face Set Lasso Gesture";
2147 ot->idname = "SCULPT_OT_face_set_lasso_gesture";
2148 ot->description = "Add a face set in a shape defined by the cursor";
2149
2153
2155
2157
2160}
2161
2163{
2164 ot->name = "Face Set Line Gesture";
2165 ot->idname = "SCULPT_OT_face_set_line_gesture";
2166 ot->description = "Add a face set to one side of a line defined by the cursor";
2167
2170 ot->exec = gesture_line_exec;
2171
2173
2174 ot->flag = OPTYPE_REGISTER;
2175
2178}
2179
2180
2181} // namespace blender::ed::sculpt_paint::face_set
CustomData interface, see also DNA_customdata_types.h.
General operations, lookup, etc. for blender objects.
A BVH for high poly meshes.
#define BLT_I18NCONTEXT_AMOUNT
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define PRF_scope(category)
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define BLI_assert_unreachable()
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
#define BM_ELEM_CD_GET_FLOAT(ele, offset)
#define BM_ELEM_CD_SET_INT(ele, offset, f)
#define BM_FACE_FIRST_LOOP(p)
#define BM_elem_index_get(ele)
#define BM_elem_flag_set(ele, hflag, val)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BMALLOC_TEMPLATE_FROM_ME(...)
btScalar max
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T * data() const
Definition BLI_array.hh:323
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
int64_t size() const
Definition BLI_set.hh:606
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const T * begin() const
Definition BLI_span.hh:224
constexpr bool is_empty() const
Definition BLI_span.hh:264
IndexRange index_range() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
bool contains(StringRef name) const
GAttributeReader lookup(const StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_for_write_span(StringRef name)
constexpr bool is_empty() const
constexpr bool contains(const T &value) const
constexpr IndexRange index_range() const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define INT_MAX
#define attribute(slot)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
bool indexed_data_equal(const Span< T > all_values, const Span< int > indices, const Span< T > values)
BooleanMix booleans_mix_calc(const VArray< bool > &varray, IndexRange range_to_check)
IndexRange face_range(const OffsetIndices< int > faces, const int grid_area, const int face)
float3 face_normal_calc(Span< float3 > vert_positions, Span< int > face_verts)
GroupedSpan< int > build_edge_to_face_map(OffsetIndices< int > faces, Span< int > corner_edges, int edges_num, Array< int > &r_offsets, Array< int > &r_indices)
float3 face_center_calc(Span< float3 > vert_positions, Span< int > face_verts)
pbvh::Tree * pbvh_get(Object &object)
void stroke_set_location(Paint &paint, float3 location)
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
Span< int > node_face_indices_calc_grids(const SubdivCCG &subdiv_ccg, const GridsNode &node, Vector< int > &faces)
Definition pbvh.cc:1666
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
void ensure_boundary_info(Object &object)
Definition sculpt.cc:588
bool vert_is_boundary(const GroupedSpan< int > vert_to_face_map, const Span< bool > hide_poly, const BitSpan boundary_verts, const int vert)
Definition sculpt.cc:620
void calc_face_indices_grids(const SubdivCCG &subdiv_ccg, const Span< int > grids, const MutableSpan< int > &face_indices)
static void face_hide_update(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask, const FunctionRef< void(Span< int >, MutableSpan< bool >)> calc_hide)
static bool edit_is_operation_valid(const Object &object, const EditMode mode, const bool modify_hidden)
void filter_verts_with_unique_face_sets_mesh(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const bool unique, const Span< int > verts, const MutableSpan< float > factors)
static void gesture_apply_for_symmetry_pass(bContext &, gesture::GestureData &gesture_data)
static wmOperatorStatus init_op_exec(bContext *C, wmOperator *op)
static wmOperatorStatus gesture_box_exec(bContext *C, wmOperator *op)
static void edit_fairing(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const int active_face_set_id, const eMeshFairingDepth fair_order, const float strength)
static wmOperatorStatus gesture_line_exec(bContext *C, wmOperator *op)
static wmOperatorStatus change_visibility_invoke(bContext *C, wmOperator *op, const wmEvent *event)
bool vert_has_unique_face_set(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, int vert)
Definition sculpt.cc:324
bool vert_has_face_set(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const int vert, const int face_set)
Definition sculpt.cc:268
void SCULPT_OT_face_set_lasso_gesture(wmOperatorType *ot)
static void gesture_begin(bContext &C, wmOperator &op, gesture::GestureData &gesture_data)
void initialize_none_to_id(Mesh *mesh, int new_id)
static bool edit_op_init(bContext *C, wmOperator *op)
static wmOperatorStatus randomize_colors_exec(bContext *C, wmOperator *)
void SCULPT_OT_face_set_line_gesture(wmOperatorType *ot)
void SCULPT_OT_face_set_polyline_gesture(wmOperatorType *ot)
static void clear_face_sets(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask)
static void delete_geometry(Object &ob, const int active_face_set_id, const bool modify_hidden)
static wmOperatorStatus change_visibility_exec(bContext *C, wmOperator *op)
void SCULPT_OT_face_sets_init(wmOperatorType *ot)
int active_update_and_get(bContext *C, Object &ob, const float mval_fl[2])
static wmOperatorStatus gesture_lasso_exec(bContext *C, wmOperator *op)
static void edit_grow_shrink(const Depsgraph &depsgraph, const Scene &scene, Object &object, const EditMode mode, const int active_face_set_id, const bool modify_hidden, wmOperator *op)
static wmOperatorStatus edit_op_exec(bContext *C, wmOperator *op)
void filter_verts_with_unique_face_sets_bmesh(int face_set_offset, const bool unique, const Set< BMVert *, 0 > &verts, const MutableSpan< float > factors)
static void show_all(Depsgraph &depsgraph, Object &object, const IndexMask &node_mask)
Array< int > duplicate_face_sets(const Mesh &mesh)
static wmOperatorStatus gesture_polyline_invoke(bContext *C, wmOperator *op, const wmEvent *event)
FunctionRef< bool(int from_face, int edge, int to_face)> FaceSetsFloodFillFn
void SCULPT_OT_face_sets_create(wmOperatorType *ot)
Set< int > gather_hidden_face_sets(const Span< bool > hide_poly, const Span< int > face_sets)
static void gesture_apply_mesh(gesture::GestureData &gesture_data, const IndexMask &node_mask)
void SCULPT_OT_face_set_change_visibility(wmOperatorType *ot)
void filter_verts_with_unique_face_sets_grids(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const SubdivCCG &subdiv_ccg, const bool unique, const Span< int > grids, const MutableSpan< float > factors)
void SCULPT_OT_face_sets_randomize_colors(wmOperatorType *ot)
static bool check_single_face_set(const Object &object, const bool check_visible_only)
bke::SpanAttributeWriter< int > ensure_face_sets_mesh(Mesh &mesh)
static void face_sets_update(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask, const FunctionRef< void(Span< int >, MutableSpan< int >)> calc_face_sets)
static wmOperatorStatus gesture_line_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus gesture_box_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void SCULPT_OT_face_sets_edit(wmOperatorType *ot)
static void gesture_end(bContext &, gesture::GestureData &gesture_data)
static void gesture_apply_bmesh(gesture::GestureData &gesture_data, const IndexMask &node_mask)
static wmOperatorStatus create_op_exec(bContext *C, wmOperator *op)
static void init_operation(gesture::GestureData &gesture_data, wmOperator &)
static wmOperatorStatus edit_op_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void SCULPT_OT_face_set_box_gesture(wmOperatorType *ot)
static void edit_modify_coordinates(bContext *C, Object &ob, const int active_face_set, const EditMode mode, wmOperator *op)
int find_next_available_id(const Mesh &mesh)
static wmOperatorStatus gesture_polyline_exec(bContext *C, wmOperator *op)
void fill_factor_from_hide_and_mask(const BMesh &bm, const Set< BMFace *, 0L > &faces, const MutableSpan< float > r_factors)
static void init_flood_fill(Object &ob, const FaceSetsFloodFillFn &test_fn)
static wmOperatorStatus gesture_lasso_invoke(bContext *C, wmOperator *op, const wmEvent *event)
int active_face_set_get(const Object &object)
Definition sculpt.cc:202
static void edit_modify_geometry(bContext *C, Object &ob, const int active_face_set, const bool modify_hidden, wmOperator *op)
void calc_face_centers(const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< float3 > vert_positions, const Span< int > face_indices, const MutableSpan< float3 > positions)
std::unique_ptr< GestureData > init_from_box(bContext *C, wmOperator *op)
void operator_properties(wmOperatorType *ot, ShapeType shapeType)
std::unique_ptr< GestureData > init_from_polyline(bContext *C, wmOperator *op)
std::unique_ptr< GestureData > init_from_line(bContext *C, const wmOperator *op)
void apply(bContext &C, GestureData &gesture_data, wmOperator &op)
std::unique_ptr< GestureData > init_from_lasso(bContext *C, wmOperator *op)
bool is_affected(const GestureData &gesture_data, const float3 &position, const float3 &normal)
void tag_update_visibility(const bContext &C)
void grids_show_all(Depsgraph &depsgraph, Object &object, const IndexMask &node_mask)
void mesh_show_all(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask)
void sync_all_from_faces(Object &object)
Definition paint_hide.cc:56
void invalidate(SculptSession &ss)
Definition sculpt.cc:6325
void push_nodes(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask, const Type type)
void geometry_begin(const Scene &scene, Object &ob, const wmOperator *op)
void push_node(const Depsgraph &depsgraph, const Object &object, const bke::pbvh::Node *node, const Type type)
void push_begin(const Scene &scene, Object &ob, const wmOperator *op)
void fill_factor_from_hide_and_mask(Span< bool > hide_vert, Span< float > mask, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7128
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void face_set_overlay_check(bContext &C, wmOperator &op)
Definition sculpt.cc:8320
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:7285
void scale_translations(MutableSpan< float3 > translations, Span< float > factors)
Definition sculpt.cc:7837
bool sculpt_mode_poll(bContext *C)
Definition sculpt.cc:3800
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
void scatter_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
bool sculpt_mode_poll_view3d(bContext *C)
Definition sculpt.cc:3806
void flush_update_step(bContext *C, const UpdateType update_type)
Definition sculpt.cc:5090
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
void flush_update_done(bContext *C, Object &ob, const UpdateType update_type)
Definition sculpt.cc:5131
std::optional< CursorGeometryInfo > cursor_geometry_info_update(bContext *C, const float2 &mval, const bool use_sampled_normal)
Definition sculpt.cc:4584
void tag_update_overlays(bContext *C)
Definition sculpt.cc:895
constexpr ParallelGrainSize grain_size(int grain_size)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T rcp(const T &a)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
void BM_mesh_delete_hflag_context(BMesh *bm, const char hflag, const int type)
void BM_face_calc_center_median(const BMFace *f, float r_cent[3])
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int RNA_enum_get(PointerRNA *ptr, const char *name)
wmOperatorStatus WM_gesture_polyline_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void BKE_mesh_prefair_and_fair_verts(Mesh *mesh, MutableSpan< float3 > deform_vert_positions, const bool affected_verts[], eMeshFairingDepth depth)
Definition mesh_fair.cc:367
const Set< BMFace *, 0 > & BKE_pbvh_bmesh_node_faces(bke::pbvh::BMeshNode *node)
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
wmOperatorStatus WM_gesture_box_invoke(bContext *C, wmOperator *op, const wmEvent *event)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
bool BKE_base_is_visible(const View3D *v3d, const Base *base)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void WM_operator_properties_border(wmOperatorType *ot)
int RNA_int_get(PointerRNA *ptr, const char *name)
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
MINLINE int max_ii(int a, int b)
wmOperatorStatus WM_gesture_straightline_active_side_invoke(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus WM_gesture_lasso_modal(bContext *C, wmOperator *op, const wmEvent *event)
Base * CTX_data_active_base(const bContext *C)
void BKE_sculptsession_free_pbvh(Object &object)
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition BLI_hash_c.hh:94
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
Mesh -> BMesh.
void mul_m4_v3(const float M[4][4], float r[3])
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
void BKE_mesh_batch_cache_dirty_tag(Mesh *mesh, eMeshBatchDirtyMode mode)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
ToolSettings * CTX_data_tool_settings(const bContext *C)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
wmOperatorStatus WM_gesture_polyline_modal(bContext *C, wmOperator *op, const wmEvent *event)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG &subdiv_ccg, const int grid_index)
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
wmOperatorStatus WM_gesture_box_modal(bContext *C, wmOperator *op, const wmEvent *event)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
@ WM_CURSOR_EDIT
Definition wm_cursors.hh:21
wmOperatorStatus WM_gesture_lasso_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Paint * BKE_paint_get_active_from_context(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const StringRef name)
Scene * CTX_data_scene(const bContext *C)
Mesh * BKE_object_get_original_mesh(const Object *object)
Definition object.cc:4461
wmOperatorStatus WM_gesture_straightline_oneshot_modal(bContext *C, wmOperator *op, const wmEvent *event)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *mesh, const BMeshToMeshParams *params)
void BM_mesh_elem_table_init(BMesh *bm, const char htype)
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void WM_operator_properties_gesture_polyline(wmOperatorType *ot)
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
@ BKE_MESH_BATCH_DIRTY_ALL
Definition BKE_mesh.h:48
MINLINE int clamp_i(int value, int min, int max)
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Object * CTX_data_active_object(const bContext *C)
float RNA_float_get(PointerRNA *ptr, const char *name)
View3D * CTX_wm_view3d(const bContext *C)
@ MESH_FAIRING_DEPTH_TANGENCY
@ MESH_FAIRING_DEPTH_POSITION
void WM_operator_properties_gesture_lasso(wmOperatorType *ot)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * next
struct BMVert * v
CustomData pdata
CustomData vdata
bke::ObjectRuntime * runtime
std::unique_ptr< SculptBoundaryInfoCache > boundary_info_cache
Definition BKE_paint.hh:458
Array< int > edge_to_face_offsets
Definition BKE_paint.hh:371
float3 active_vert_position(const Depsgraph &depsgraph, const Object &object) const
Array< int > edge_to_face_indices
Definition BKE_paint.hh:372
GroupedSpan< int > edge_to_face_map
Definition BKE_paint.hh:373
Array< float > masks
Depsgraph * depsgraph
Definition ED_view3d.hh:78
void(*) end(bContext &, GestureData &)
void(*) apply_for_symmetry_pass(bContext &, GestureData &)
void(*) begin(bContext &, wmOperator &, GestureData &)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157