Blender V5.3
sculpt_expand.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include "sculpt_expand.hh"
9
10#include <cmath>
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_array_utils.hh"
15#include "BLI_bit_vector.hh"
16#include "BLI_linklist_stack.hh"
17#include "BLI_math_vector.hh"
18
19#include "DNA_brush_types.h"
20#include "DNA_object_types.h"
21
22#include "BKE_attribute.hh"
23#include "BKE_brush.hh"
24#include "BKE_ccg.hh"
25#include "BKE_colortools.hh"
26#include "BKE_context.hh"
27#include "BKE_image.hh"
28#include "BKE_layer.hh"
29#include "BKE_mesh.hh"
30#include "BKE_mesh_mapping.hh"
31#include "BKE_object_types.hh"
32#include "BKE_paint.hh"
33#include "BKE_paint_bvh.hh"
34#include "BKE_report.hh"
35#include "BKE_subdiv_ccg.hh"
36
37#include "BLT_translation.hh"
38
39#include "WM_api.hh"
40#include "WM_types.hh"
41
42#include "RNA_access.hh"
43#include "RNA_define.hh"
44
45#include "ED_screen.hh"
46#include "ED_sculpt.hh"
47
48#include "../paint_intern.hh"
49#include "paint_mask.hh"
50#include "sculpt_boundary.hh"
51#include "sculpt_color.hh"
52#include "sculpt_face_set.hh"
53#include "sculpt_flood_fill.hh"
54#include "sculpt_geodesic.hh"
55#include "sculpt_intern.hh"
56#include "sculpt_islands.hh"
57#include "sculpt_smooth.hh"
58#include "sculpt_undo.hh"
59
61#include "IMB_imbuf.hh"
62
63#include "bmesh.hh"
64
65namespace blender {
66
68
69/* Sculpt Expand. */
70/* Operator for creating selections and patterns in Sculpt Mode. Expand can create masks, face sets
71 * and fill vertex colors. */
72/* The main functionality of the operator
73 * - The operator initializes a value per vertex, called "falloff". There are multiple algorithms
74 * to generate these falloff values which will create different patterns in the result when using
75 * the operator. These falloff values require algorithms that rely on mesh connectivity, so they
76 * are only valid on parts of the mesh that are in the same connected component as the given
77 * initial vertices. If needed, these falloff values are propagated from vertex or grids into the
78 * base mesh faces.
79 *
80 * - On each modal callback, the operator gets the active vertex and face and gets its falloff
81 * value from its precalculated falloff. This is now the active falloff value.
82 * - Using the active falloff value and the settings of the expand operation (which can be modified
83 * during execution using the modal key-map), the operator loops over all elements in the mesh to
84 * check if they are enabled of not.
85 * - Based on each element state after evaluating the settings, the desired mesh data (mask, face
86 * sets, colors...) is updated.
87 */
88
92#define SCULPT_EXPAND_VERTEX_NONE -1
93
95#define EXPAND_ACTIVE_COMPONENT_NONE -1
100#define SCULPT_EXPAND_TEXTURE_DISTORTION_STEP 0.01f
101
107#define SCULPT_EXPAND_LOOP_THRESHOLD 0.00001f
108
114#define SCULPT_EXPAND_NORMALS_FALLOFF_EDGE_SENSITIVITY 300
115
116/* Expand Modal Key-map. */
117enum {
137};
138
139/* Functions for getting the state of mesh elements (vertices and base mesh faces). When the main
140 * functions for getting the state of an element return true it means that data associated to that
141 * element will be modified by expand. */
142
148 const Cache &expand_cache,
149 const int vert)
150{
151 for (int i = 0; i < EXPAND_SYMM_AREAS; i++) {
152 if (islands::vert_id_get(ss, vert) == expand_cache.active_connected_islands[i]) {
153 return true;
154 }
155 }
156 return false;
157}
158
162static bool is_face_in_active_component(const Object &object,
163 const OffsetIndices<int> faces,
164 const Span<int> corner_verts,
165 const Cache &expand_cache,
166 const int f)
167{
168 const SculptSession &ss = *object.runtime->sculpt_session;
169 switch (bke::object::pbvh_get(object)->type()) {
171 return is_vert_in_active_component(ss, expand_cache, corner_verts[faces[f].start()]);
174 ss,
175 expand_cache,
176 faces[f].start() * BKE_subdiv_ccg_key_top_level(*ss.subdiv_ccg).grid_area);
179 ss, expand_cache, BM_elem_index_get(ss.bm->ftable[f]->l_first->v));
180 }
182 return false;
183}
184
190 const Cache &expand_cache,
191 const float3 &position,
192 const int vert)
193{
194 if (expand_cache.texture_distortion_strength == 0.0f) {
195 return expand_cache.vert_falloff[vert];
196 }
197 const Brush *brush = expand_cache.brush;
199 if (!mtex->tex) {
200 return expand_cache.vert_falloff[vert];
201 }
202
203 float4 rgba;
204 const float avg = BKE_brush_sample_tex_3d(
205 expand_cache.paint, brush, mtex, position, rgba, 0, ss.tex_pool);
206
207 const float distortion = (avg - 0.5f) * expand_cache.texture_distortion_strength *
208 expand_cache.max_vert_falloff;
209 return expand_cache.vert_falloff[vert] + distortion;
210}
211
216static float max_vert_falloff_get(const Cache &expand_cache)
217{
218 if (expand_cache.texture_distortion_strength == 0.0f) {
219 return expand_cache.max_vert_falloff;
220 }
221
222 const MTex *mask_tex = BKE_brush_mask_texture_get(expand_cache.brush, PaintMode::Sculpt);
223 if (!mask_tex->tex) {
224 return expand_cache.max_vert_falloff;
225 }
226
227 return expand_cache.max_vert_falloff +
228 (0.5f * expand_cache.texture_distortion_strength * expand_cache.max_vert_falloff);
229}
230
232 const Cache &expand_cache,
233 const float3 &position,
234 const int vert)
235{
236 const float max_falloff_factor = max_vert_falloff_get(expand_cache);
237 const float loop_len = (max_falloff_factor / expand_cache.loop_count) +
239
240 const float vertex_falloff_factor = falloff_value_vertex_get(ss, expand_cache, position, vert);
241 const float active_factor = fmod(expand_cache.active_falloff, loop_len);
242 const float falloff_factor = fmod(vertex_falloff_factor, loop_len);
243
244 return falloff_factor < active_factor;
245}
246
251static bool face_state_get(const Object &object,
252 const OffsetIndices<int> faces,
253 const Span<int> corner_verts,
254 const Span<bool> hide_poly,
255 const Span<int> face_sets,
256 const Cache &expand_cache,
257 const int face)
258{
259 if (!hide_poly.is_empty() && hide_poly[face]) {
260 return false;
261 }
262
263 if (!is_face_in_active_component(object, faces, corner_verts, expand_cache, face)) {
264 return false;
265 }
266
267 if (expand_cache.all_enabled) {
268 if (expand_cache.invert) {
269 return false;
270 }
271 return true;
272 }
273
274 bool enabled = false;
275
276 if (expand_cache.snap_enabled_face_sets) {
277 const int face_set = expand_cache.original_face_sets[face];
278 enabled = expand_cache.snap_enabled_face_sets->contains(face_set);
279 }
280 else {
281 const float loop_len = (expand_cache.max_face_falloff / expand_cache.loop_count) +
283
284 const float active_factor = fmod(expand_cache.active_falloff, loop_len);
285 const float falloff_factor = fmod(expand_cache.face_falloff[face], loop_len);
286 enabled = falloff_factor < active_factor;
287 }
288
289 if (expand_cache.falloff_type == FalloffType::ActiveFaceSet) {
290 if (face_sets[face] == expand_cache.initial_active_face_set) {
291 enabled = false;
292 }
293 }
294
295 if (expand_cache.invert) {
296 enabled = !enabled;
297 }
298
299 return enabled;
300}
301
306static float gradient_value_get(const SculptSession &ss,
307 const Cache &expand_cache,
308 const float3 &position,
309 const int vert)
310{
311 if (!expand_cache.falloff_gradient) {
312 return 1.0f;
313 }
314
315 const float max_falloff_factor = max_vert_falloff_get(expand_cache);
316 const float loop_len = (max_falloff_factor / expand_cache.loop_count) +
318
319 const float vertex_falloff_factor = falloff_value_vertex_get(ss, expand_cache, position, vert);
320 const float active_factor = fmod(expand_cache.active_falloff, loop_len);
321 const float falloff_factor = fmod(vertex_falloff_factor, loop_len);
322
323 float linear_falloff;
324
325 if (expand_cache.invert) {
326 /* Active factor is the result of a modulus operation using loop_len, so they will never be
327 * equal and loop_len - active_factor should never be 0. */
328 BLI_assert((loop_len - active_factor) != 0.0f);
329 linear_falloff = (falloff_factor - active_factor) / (loop_len - active_factor);
330 }
331 else {
332 linear_falloff = 1.0f - (falloff_factor / active_factor);
333 }
334
335 if (!expand_cache.brush_gradient) {
336 return linear_falloff;
337 }
338
339 return BKE_brush_curve_strength(expand_cache.brush, linear_falloff, 1.0f);
340}
341
342/* Utility functions for getting all vertices state during expand. */
343
348static BitVector<> enabled_state_to_bitmap(const Depsgraph &depsgraph,
349 const Object &object,
350 const Cache &expand_cache)
351{
352 const SculptSession &ss = *object.runtime->sculpt_session;
353 const int totvert = vertex_count_get(object);
354 BitVector<> enabled_verts(totvert);
355 if (expand_cache.all_enabled) {
356 if (!expand_cache.invert) {
357 enabled_verts.fill(true);
358 }
359 return enabled_verts;
360 }
361 switch (bke::object::pbvh_get(object)->type()) {
363 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
364 const Span<float3> positions = bke::pbvh::vert_positions_eval(depsgraph, object);
365 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
366 const bke::AttributeAccessor attributes = mesh.attributes();
367 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
368 const VArraySpan face_sets = *attributes.lookup_or_default<int>(
369 ".sculpt_face_set", bke::AttrDomain::Face, 0);
371 IndexRange(totvert), 1024, bits::BitsPerInt, [&](const IndexRange range) {
372 for (const int vert : range) {
373 if (!hide_vert.is_empty() && hide_vert[vert]) {
374 continue;
375 }
376 if (!is_vert_in_active_component(ss, expand_cache, vert)) {
377 continue;
378 }
379 if (expand_cache.snap) {
380 enabled_verts[vert].set(face_set::vert_has_any_face_set(
381 vert_to_face_map, face_sets, vert, *expand_cache.snap_enabled_face_sets));
382 continue;
383 }
384 enabled_verts[vert].set(
385 vert_falloff_is_enabled(ss, expand_cache, positions[vert], vert));
386 }
387 });
388 break;
389 }
391 const Mesh &base_mesh = *id_cast<const Mesh *>(object.data);
392 const OffsetIndices<int> faces = base_mesh.faces();
393 const Span<int> corner_verts = base_mesh.corner_verts();
394 const GroupedSpan<int> vert_to_face_map = base_mesh.vert_to_face_map();
395 const bke::AttributeAccessor attributes = base_mesh.attributes();
396 const VArraySpan face_sets = *attributes.lookup_or_default<int>(
397 ".sculpt_face_set", bke::AttrDomain::Face, 0);
398
399 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
400 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
401 const Span<float3> positions = subdiv_ccg.positions;
402 BitGroupVector<> &grid_hidden = subdiv_ccg.grid_hidden;
403 for (const int grid : IndexRange(subdiv_ccg.grids_num)) {
404 const int start = grid * key.grid_area;
405 BKE_subdiv_ccg_foreach_visible_grid_vert(key, grid_hidden, grid, [&](const int offset) {
406 const int vert = start + offset;
407 if (!is_vert_in_active_component(ss, expand_cache, vert)) {
408 return;
409 }
410 if (expand_cache.snap) {
411 const SubdivCCGCoord coord = SubdivCCGCoord::from_index(key, vert);
413 corner_verts,
414 vert_to_face_map,
415 face_sets,
416 subdiv_ccg,
417 coord,
418 *expand_cache.snap_enabled_face_sets))
419 {
420 enabled_verts[vert].set(true);
421 }
422 return;
423 }
424 enabled_verts[vert].set(
425 vert_falloff_is_enabled(ss, expand_cache, positions[vert], vert));
426 });
427 }
428 break;
429 }
431 BMesh &bm = *ss.bm;
432 for (const int vert : IndexRange(totvert)) {
433 const BMVert *bm_vert = BM_vert_at_index(&bm, vert);
434 if (BM_elem_flag_test(bm_vert, BM_ELEM_HIDDEN)) {
435 continue;
436 }
437 if (!is_vert_in_active_component(ss, expand_cache, vert)) {
438 continue;
439 }
440 if (expand_cache.snap) {
441 /* TODO: Support face sets for BMesh. */
442 const int face_set = 0;
443 enabled_verts[vert].set(expand_cache.snap_enabled_face_sets->contains(face_set));
444 continue;
445 }
446 enabled_verts[vert].set(vert_falloff_is_enabled(ss, expand_cache, bm_vert->co, vert));
447 }
448 break;
449 }
450 }
451 if (expand_cache.invert) {
452 bits::invert(MutableBoundedBitSpan(enabled_verts));
453 }
454 return enabled_verts;
455}
456
463 const BitSpan enabled_verts,
464 const bool use_mesh_boundary,
465 IndexMaskMemory &memory)
466{
467 SculptSession &ss = *object.runtime->sculpt_session;
468
469 const IndexMask enabled_mask = IndexMask::from_bits(enabled_verts, memory);
470
471 switch (bke::object::pbvh_get(object)->type()) {
473 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
474 const OffsetIndices faces = mesh.faces();
475 const Span<int> corner_verts = mesh.corner_verts();
476 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
477 const bke::AttributeAccessor attributes = mesh.attributes();
478 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
479 return IndexMask::from_predicate(enabled_mask, memory, [&](const int vert) {
480 Vector<int> neighbors;
481 for (const int neighbor : vert_neighbors_get_mesh(
482 faces, corner_verts, vert_to_face_map, hide_poly, vert, neighbors))
483 {
484 if (!enabled_verts[neighbor]) {
485 return true;
486 }
487 }
488
489 if (use_mesh_boundary &&
491 vert_to_face_map, hide_poly, ss.boundary_info_cache->verts, vert))
492 {
493 return true;
494 }
495
496 return false;
497 });
498 }
500 const Mesh &base_mesh = *id_cast<const Mesh *>(object.data);
501 const OffsetIndices faces = base_mesh.faces();
502 const Span<int> corner_verts = base_mesh.corner_verts();
503
504 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
505 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
506 return IndexMask::from_predicate(enabled_mask, memory, [&](const int vert) {
507 const SubdivCCGCoord coord = SubdivCCGCoord::from_index(key, vert);
508 SubdivCCGNeighbors neighbors;
509 BKE_subdiv_ccg_neighbor_coords_get(subdiv_ccg, coord, false, neighbors);
510 for (const SubdivCCGCoord neighbor : neighbors.coords) {
511 if (!enabled_verts[neighbor.to_index(key)]) {
512 return true;
513 }
514 }
515
516 if (use_mesh_boundary && boundary::vert_is_boundary(faces,
517 corner_verts,
518 ss.boundary_info_cache->verts,
519 ss.boundary_info_cache->edges,
520 subdiv_ccg,
521 coord))
522 {
523 return true;
524 }
525
526 return false;
527 });
528 }
530 return IndexMask::from_predicate(enabled_mask, memory, [&](const int vert) {
531 BMVert *bm_vert = BM_vert_at_index(ss.bm, vert);
532 BMeshNeighborVerts neighbors;
533 for (const BMVert *neighbor : vert_neighbors_get_bmesh(*bm_vert, neighbors)) {
534 if (!enabled_verts[BM_elem_index_get(neighbor)]) {
535 return true;
536 }
537 }
538
539 if (use_mesh_boundary && BM_vert_is_boundary(bm_vert)) {
540 return true;
541 }
542
543 return false;
544 });
545 }
546 }
548 return {};
549}
550
551static void check_topology_islands(Object &ob, FalloffType falloff_type)
552{
554 Cache &expand_cache = *ss.expand_cache;
555
556 expand_cache.check_islands = ELEM(falloff_type,
562
563 if (expand_cache.check_islands) {
565 }
566}
567
568} // namespace ed::sculpt_paint::expand
569
570namespace ed::sculpt_paint {
571
572/* Functions implementing different algorithms for initializing falloff values. */
573
574Vector<int> find_symm_verts_mesh(const Depsgraph &depsgraph,
575 const Object &object,
576 const int original_vert,
577 const float max_distance)
578{
579 const ePaintSymmetryFlags symm = mesh_symmetry_xyz_get(object);
581 const bool use_original = false;
582
583 Vector<int> symm_verts;
584 symm_verts.append(original_vert);
585
586 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
587 const Span<float3> positions = bke::pbvh::vert_positions_eval(depsgraph, object);
588 const bke::AttributeAccessor attributes = mesh.attributes();
589 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
590
591 const float3 location = positions[original_vert];
592 for (int symm_it = 1; symm_it <= symm; symm_it++) {
593 if (!is_symmetry_iteration_valid(symm_it, symm)) {
594 continue;
595 }
596 const float3 symm_location = symmetry_flip(location, ePaintSymmetryFlags(symm_it));
597 const std::optional<int> nearest = nearest_vert_calc_mesh(
598 pbvh, positions, hide_vert, symm_location, max_distance, use_original);
599 if (!nearest) {
600 continue;
601 }
602 symm_verts.append(*nearest);
603 }
604
605 std::ranges::sort(symm_verts);
606 return symm_verts;
607}
608
610 const int original_vert,
611 const float max_distance)
612{
613 const ePaintSymmetryFlags symm = mesh_symmetry_xyz_get(object);
615 const bool use_original = false;
616
617 Vector<int> symm_verts;
618 symm_verts.append(original_vert);
619
620 const SculptSession &ss = *object.runtime->sculpt_session;
621 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
622 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
623 const Span<float3> positions = subdiv_ccg.positions;
624 const float3 location = positions[original_vert];
625 for (int symm_it = 1; symm_it <= symm; symm_it++) {
626 if (!is_symmetry_iteration_valid(symm_it, symm)) {
627 continue;
628 }
629 const float3 symm_location = symmetry_flip(location, ePaintSymmetryFlags(symm_it));
630 const std::optional<SubdivCCGCoord> nearest = nearest_vert_calc_grids(
631 pbvh, subdiv_ccg, symm_location, max_distance, use_original);
632 if (!nearest) {
633 continue;
634 }
635 symm_verts.append(nearest->to_index(key));
636 }
637
638 std::ranges::sort(symm_verts);
639 return symm_verts;
640}
641
643 const int original_vert,
644 const float max_distance)
645{
646 const ePaintSymmetryFlags symm = mesh_symmetry_xyz_get(object);
648 const bool use_original = false;
649
650 Vector<int> symm_verts;
651 symm_verts.append(original_vert);
652
653 const SculptSession &ss = *object.runtime->sculpt_session;
654 BMesh &bm = *ss.bm;
655 const BMVert *original_bm_vert = BM_vert_at_index(&bm, original_vert);
656 const float3 location = original_bm_vert->co;
657 for (int symm_it = 1; symm_it <= symm; symm_it++) {
658 if (!is_symmetry_iteration_valid(symm_it, symm)) {
659 continue;
660 }
661 const float3 symm_location = symmetry_flip(location, ePaintSymmetryFlags(symm_it));
662 const std::optional<BMVert *> nearest = nearest_vert_calc_bmesh(
663 pbvh, symm_location, max_distance, use_original);
664 if (!nearest) {
665 continue;
666 }
667 symm_verts.append(BM_elem_index_get(*nearest));
668 }
669
670 std::ranges::sort(symm_verts);
671 return symm_verts;
672}
673
674Vector<int> find_symm_verts(const Depsgraph &depsgraph,
675 const Object &object,
676 const int original_vert,
677 const float max_distance)
678{
680 switch (pbvh.type()) {
682 return find_symm_verts_mesh(depsgraph, object, original_vert, max_distance);
684 return find_symm_verts_grids(object, original_vert, max_distance);
686 return find_symm_verts_bmesh(object, original_vert, max_distance);
687 }
689 return {};
690}
691
692std::array<int, PAINT_SYMM_AREAS> find_all_symm_verts(const Depsgraph &depsgraph,
693 const Object &object,
694 const int original_vert,
695 const float max_distance)
696{
698 switch (pbvh.type()) {
700 return find_all_symm_verts_mesh(depsgraph, object, original_vert, max_distance);
702 return find_all_symm_verts_grids(object, original_vert, max_distance);
704 return find_all_symm_verts_bmesh(object, original_vert, max_distance);
705 }
707 return {};
708}
709
710std::array<int, PAINT_SYMM_AREAS> find_all_symm_verts_mesh(const Depsgraph &depsgraph,
711 const Object &object,
712 const int original_vert,
713 const float max_distance)
714{
715 const ePaintSymmetryFlags symm = mesh_symmetry_xyz_get(object);
717 const bool use_original = false;
718
719 std::array<int, PAINT_SYMM_AREAS> symm_verts;
720 symm_verts.fill(-1);
721 symm_verts[0] = original_vert;
722
723 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
724 const Span<float3> positions = bke::pbvh::vert_positions_eval(depsgraph, object);
725 const bke::AttributeAccessor attributes = mesh.attributes();
726 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
727
728 const float3 location = positions[original_vert];
729 for (int symm_it = 1; symm_it <= PAINT_SYMM_AREAS; symm_it++) {
730 if (!is_symmetry_iteration_valid(symm_it, symm)) {
731 continue;
732 }
733 const float3 symm_location = symmetry_flip(location, ePaintSymmetryFlags(symm_it));
734 const std::optional<int> nearest = nearest_vert_calc_mesh(
735 pbvh, positions, hide_vert, symm_location, max_distance, use_original);
736 if (!nearest) {
737 continue;
738 }
739 symm_verts[symm_it] = *nearest;
740 }
741
742 return symm_verts;
743}
744
745std::array<int, PAINT_SYMM_AREAS> find_all_symm_verts_grids(const Object &object,
746 const int original_vert,
747 const float max_distance)
748{
749 const ePaintSymmetryFlags symm = mesh_symmetry_xyz_get(object);
751 const bool use_original = false;
752
753 std::array<int, PAINT_SYMM_AREAS> symm_verts;
754 symm_verts.fill(-1);
755 symm_verts[0] = original_vert;
756
757 const SculptSession &ss = *object.runtime->sculpt_session;
758 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
759 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
760 const Span<float3> positions = subdiv_ccg.positions;
761 const float3 location = positions[original_vert];
762 for (int symm_it = 1; symm_it <= PAINT_SYMM_AREAS; symm_it++) {
763 if (!is_symmetry_iteration_valid(symm_it, symm)) {
764 continue;
765 }
766 const float3 symm_location = symmetry_flip(location, ePaintSymmetryFlags(symm_it));
767 const std::optional<SubdivCCGCoord> nearest = nearest_vert_calc_grids(
768 pbvh, subdiv_ccg, symm_location, max_distance, use_original);
769 if (!nearest) {
770 continue;
771 }
772 symm_verts[symm_it] = nearest->to_index(key);
773 }
774
775 return symm_verts;
776}
777
778std::array<int, PAINT_SYMM_AREAS> find_all_symm_verts_bmesh(const Object &object,
779 const int original_vert,
780 const float max_distance)
781{
782 const ePaintSymmetryFlags symm = mesh_symmetry_xyz_get(object);
784 const bool use_original = false;
785
786 std::array<int, PAINT_SYMM_AREAS> symm_verts;
787 symm_verts.fill(-1);
788 symm_verts[0] = original_vert;
789
790 const SculptSession &ss = *object.runtime->sculpt_session;
791 BMesh &bm = *ss.bm;
792 const BMVert *original_bm_vert = BM_vert_at_index(&bm, original_vert);
793 const float3 location = original_bm_vert->co;
794 for (int symm_it = 1; symm_it <= PAINT_SYMM_AREAS; symm_it++) {
795 if (!is_symmetry_iteration_valid(symm_it, symm)) {
796 continue;
797 }
798 const float3 symm_location = symmetry_flip(location, ePaintSymmetryFlags(symm_it));
799 const std::optional<BMVert *> nearest = nearest_vert_calc_bmesh(
800 pbvh, symm_location, max_distance, use_original);
801 if (!nearest) {
802 continue;
803 }
804 symm_verts[symm_it] = BM_elem_index_get(*nearest);
805 }
806
807 return symm_verts;
808}
809
810} // namespace ed::sculpt_paint
811
812namespace ed::sculpt_paint::expand {
813
818static Array<float> geodesic_falloff_create(const Depsgraph &depsgraph,
819 Object &ob,
820 const IndexMask &initial_verts)
821{
822 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
823 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
824 const Span<int2> edges = mesh.edges();
825 const OffsetIndices faces = mesh.faces();
826 const Span<int> corner_verts = mesh.corner_verts();
827 const Span<int> corner_edges = mesh.corner_edges();
828 const bke::AttributeAccessor attributes = mesh.attributes();
829 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
830
832 if (ss.edge_to_face_map.is_empty()) {
833 ss.edge_to_face_map = bke::mesh::build_edge_to_face_map(
834 faces, corner_edges, edges.size(), ss.edge_to_face_offsets, ss.edge_to_face_indices);
835 }
836 if (ss.vert_to_edge_map.is_empty()) {
837 ss.vert_to_edge_map = bke::mesh::build_vert_to_edge_map(
838 edges, mesh.verts_num, ss.vert_to_edge_offsets, ss.vert_to_edge_indices);
839 }
840
841 Set<int> verts;
842 initial_verts.foreach_index([&](const int vert) { verts.add(vert); });
843
844 return geodesic::distances_create(vert_positions,
845 edges,
846 faces,
847 corner_verts,
848 ss.vert_to_edge_map,
849 ss.edge_to_face_map,
850 hide_poly,
851 verts,
852 FLT_MAX);
853}
854static Array<float> geodesic_falloff_create(const Depsgraph &depsgraph,
855 Object &ob,
856 const int initial_vert)
857{
858 const Vector<int> symm_verts = find_symm_verts(depsgraph, ob, initial_vert);
859
860 IndexMaskMemory memory;
861 const IndexMask mask = IndexMask::from_indices(symm_verts.as_span(), memory);
862
863 return geodesic_falloff_create(depsgraph, ob, mask);
864}
865
871 const IndexMask &initial_verts,
872 MutableSpan<float> distances)
873{
875 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
876 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
878 const int totvert = vertex_count_get(ob);
879
880 switch (pbvh.type()) {
882 flood_fill::FillDataMesh flood(totvert);
883 initial_verts.foreach_index([&](const int vert) { flood.add_and_skip_initial(vert); });
884 flood.execute(ob, vert_to_face_map, [&](const int from_vert, const int to_vert) {
885 distances[to_vert] = distances[from_vert] + 1.0f;
886 return true;
887 });
888 break;
889 }
891 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
892 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
893
894 flood_fill::FillDataGrids flood(totvert);
895 initial_verts.foreach_index([&](const int vert) {
896 const SubdivCCGCoord orig_coord = SubdivCCGCoord::from_index(key, vert);
897 flood.add_and_skip_initial(orig_coord, vert);
898 });
899 flood.execute(
900 ob,
901 subdiv_ccg,
902 [&](const SubdivCCGCoord from, const SubdivCCGCoord to, const bool is_duplicate) {
903 const int from_vert = from.to_index(key);
904 const int to_vert = to.to_index(key);
905 if (is_duplicate) {
906 distances[to_vert] = distances[from_vert];
907 }
908 else {
909 distances[to_vert] = distances[from_vert] + 1.0f;
910 }
911 return true;
912 });
913 break;
914 }
916 BMesh &bm = *ss.bm;
917 flood_fill::FillDataBMesh flood(totvert);
918 initial_verts.foreach_index(
919 [&](const int vert) { flood.add_and_skip_initial(BM_vert_at_index(&bm, vert), vert); });
920 flood.execute(ob, [&](BMVert *from_bm_vert, BMVert *to_bm_vert) {
921 const int from_vert = BM_elem_index_get(from_bm_vert);
922 const int to_vert = BM_elem_index_get(to_bm_vert);
923 distances[to_vert] = distances[from_vert] + 1.0f;
924 return true;
925 });
926 break;
927 }
928 }
929}
930
931static Array<float> topology_falloff_create(const Depsgraph &depsgraph,
932 Object &ob,
933 const int initial_vert)
934{
935 const Vector<int> symm_verts = find_symm_verts(depsgraph, ob, initial_vert);
936
937 IndexMaskMemory memory;
938 const IndexMask mask = IndexMask::from_indices(symm_verts.as_span(), memory);
939
940 Array<float> dists(vertex_count_get(ob), 0.0f);
942 return dists;
943}
944
950static Array<float> normals_falloff_create(const Depsgraph &depsgraph,
951 Object &ob,
952 const int vert,
953 const float edge_sensitivity,
954 const int blur_steps)
955{
958 const int totvert = vertex_count_get(ob);
959 Array<float> dists(totvert, 0.0f);
960 Array<float> edge_factors(totvert, 1.0f);
961
962 switch (pbvh.type()) {
964 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
965 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
966 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
967
968 const float3 orig_normal = vert_normals[vert];
969 flood_fill::FillDataMesh flood(totvert);
970 flood.add_initial(find_symm_verts(depsgraph, ob, vert));
971 flood.execute(ob, vert_to_face_map, [&](const int from_vert, const int to_vert) {
972 const float3 &from_normal = vert_normals[from_vert];
973 const float3 &to_normal = vert_normals[to_vert];
974 const float from_edge_factor = edge_factors[from_vert];
975 const float dist = math::dot(orig_normal, to_normal) *
976 powf(from_edge_factor, edge_sensitivity);
977 edge_factors[to_vert] = math::dot(to_normal, from_normal) * from_edge_factor;
978 dists[to_vert] = std::clamp(dist, 0.0f, 1.0f);
979 return true;
980 });
981 break;
982 }
984 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
985 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
986 const Span<float3> normals = subdiv_ccg.normals;
987 const float3 orig_normal = normals[vert];
988 flood_fill::FillDataGrids flood(totvert);
989 flood.add_initial(key, find_symm_verts(depsgraph, ob, vert));
990 flood.execute(
991 ob,
992 subdiv_ccg,
993 [&](const SubdivCCGCoord from, const SubdivCCGCoord to, const bool is_duplicate) {
994 const int from_vert = from.to_index(key);
995 const int to_vert = to.to_index(key);
996 if (is_duplicate) {
997 edge_factors[to_vert] = edge_factors[from_vert];
998 dists[to_vert] = dists[from_vert];
999 }
1000 else {
1001 const float3 &from_normal = normals[from_vert];
1002 const float3 &to_normal = normals[to_vert];
1003 const float from_edge_factor = edge_factors[from_vert];
1004 const float dist = math::dot(orig_normal, to_normal) *
1005 powf(from_edge_factor, edge_sensitivity);
1006 edge_factors[to_vert] = math::dot(to_normal, from_normal) * from_edge_factor;
1007 dists[to_vert] = std::clamp(dist, 0.0f, 1.0f);
1008 }
1009 return true;
1010 });
1011 break;
1012 }
1014 flood_fill::FillDataBMesh flood(totvert);
1015 BMVert *orig_vert = BM_vert_at_index(ss.bm, vert);
1016 const float3 orig_normal = orig_vert->no;
1017 flood.add_initial(*ss.bm, find_symm_verts(depsgraph, ob, vert));
1018 flood.execute(ob, [&](BMVert *from_bm_vert, BMVert *to_bm_vert) {
1019 const float3 from_normal = from_bm_vert->no;
1020 const float3 to_normal = to_bm_vert->no;
1021 const int from_vert = BM_elem_index_get(from_bm_vert);
1022 const int to_vert = BM_elem_index_get(to_bm_vert);
1023 const float from_edge_factor = edge_factors[from_vert];
1024 const float dist = math::dot(orig_normal, to_normal) *
1025 powf(from_edge_factor, edge_sensitivity);
1026 edge_factors[to_vert] = math::dot(to_normal, from_normal) * from_edge_factor;
1027 dists[to_vert] = std::clamp(dist, 0.0f, 1.0f);
1028 return true;
1029 });
1030 break;
1031 }
1032 }
1033
1034 smooth::blur_geometry_data_array(ob, blur_steps, dists);
1035
1036 for (int i = 0; i < totvert; i++) {
1037 dists[i] = 1.0 - dists[i];
1038 }
1039
1040 return dists;
1041}
1042
1047static Array<float> spherical_falloff_create(const Depsgraph &depsgraph,
1048 const Object &object,
1049 const int vert)
1050{
1051 SculptSession &ss = *object.runtime->sculpt_session;
1052 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1053 Array<float> dists(vertex_count_get(object));
1054
1055 const Vector<int> symm_verts = find_symm_verts(depsgraph, object, vert);
1056
1057 switch (pbvh.type()) {
1058 case bke::pbvh::Type::Mesh: {
1059 const Span<float3> positions = bke::pbvh::vert_positions_eval(depsgraph, object);
1060
1061 Array<float3> locations(symm_verts.size());
1062 array_utils::gather(positions, symm_verts.as_span(), locations.as_mutable_span());
1063
1064 threading::parallel_for(positions.index_range(), 1024, [&](const IndexRange range) {
1065 for (const int vert : range) {
1066 float dist = std::numeric_limits<float>::max();
1067 for (const float3 &location : locations) {
1068 dist = std::min(dist, math::distance(positions[vert], location));
1069 }
1070 dists[vert] = dist;
1071 }
1072 });
1073 break;
1074 }
1076 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1077 const Span<float3> positions = subdiv_ccg.positions;
1078
1079 Array<float3> locations(symm_verts.size());
1080 for (const int i : symm_verts.index_range()) {
1081 locations[i] = positions[symm_verts[i]];
1082 }
1083
1084 threading::parallel_for(positions.index_range(), 1024, [&](const IndexRange range) {
1085 for (const int vert : range) {
1086 float dist = std::numeric_limits<float>::max();
1087 for (const float3 &location : locations) {
1088 dist = std::min(dist, math::distance(positions[vert], location));
1089 }
1090 dists[vert] = dist;
1091 }
1092 });
1093 break;
1094 }
1096 BMesh &bm = *ss.bm;
1097
1098 Array<float3> locations(symm_verts.size());
1099 for (const int i : symm_verts.index_range()) {
1100 locations[i] = BM_vert_at_index(&bm, symm_verts[i])->co;
1101 }
1102
1103 threading::parallel_for(IndexRange(bm.totvert), 1024, [&](const IndexRange range) {
1104 for (const int vert : range) {
1105 float dist = std::numeric_limits<float>::max();
1106 for (const float3 &location : locations) {
1107 dist = std::min(dist,
1108 math::distance(float3(BM_vert_at_index(&bm, vert)->co), location));
1109 }
1110 dists[vert] = dist;
1111 }
1112 });
1113 break;
1114 }
1115 }
1116
1117 return dists;
1118}
1119
1125static Array<float> boundary_topology_falloff_create(const Depsgraph &depsgraph,
1126 Object &ob,
1127 const int initial_vert)
1128{
1129 const Vector<int> symm_verts = find_symm_verts(depsgraph, ob, initial_vert);
1130
1131 BitVector<> boundary_verts(vertex_count_get(ob));
1132 for (const int vert : symm_verts) {
1133 if (std::unique_ptr<boundary::SculptBoundary> boundary = boundary::data_init(
1134 depsgraph, ob, nullptr, vert, FLT_MAX))
1135 {
1136 for (const int vert : boundary->verts) {
1137 boundary_verts[vert].set();
1138 }
1139 }
1140 }
1141
1142 IndexMaskMemory memory;
1143 const IndexMask boundary_mask = IndexMask::from_bits(boundary_verts, memory);
1144
1145 Array<float> dists(vertex_count_get(ob), 0.0f);
1146 calc_topology_falloff_from_verts(ob, boundary_mask, dists);
1147 return dists;
1148}
1149
1155static Array<float> diagonals_falloff_create(const Depsgraph &depsgraph,
1156 Object &ob,
1157 const int vert)
1158{
1160 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
1161 const OffsetIndices<int> faces = mesh.faces();
1162 const Span<int> corner_verts = mesh.corner_verts();
1163 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1164 const int totvert = vertex_count_get(ob);
1165 Array<float> dists(totvert, 0.0f);
1166
1167 /* This algorithm uses mesh data (faces and loops), so this falloff type can't be initialized for
1168 * Multires. It also does not make sense to implement it for dyntopo as the result will be the
1169 * same as Topology falloff. */
1170 if (pbvh.type() != bke::pbvh::Type::Mesh) {
1171 return dists;
1172 }
1173
1174 const Vector<int> symm_verts = find_symm_verts(depsgraph, ob, vert);
1175
1176 /* Search and mask as visited the initial vertices using the enabled symmetry passes. */
1177 BitVector<> visited_verts(totvert);
1178 std::queue<int> queue;
1179 for (const int vert : symm_verts) {
1180 queue.push(vert);
1181 visited_verts[vert].set();
1182 }
1183
1184 if (queue.empty()) {
1185 return dists;
1186 }
1187
1188 /* Propagate the falloff increasing the value by 1 each time a new vertex is visited. */
1189 while (!queue.empty()) {
1190 const int next_vert = queue.front();
1191 queue.pop();
1192
1193 for (const int face : vert_to_face_map[next_vert]) {
1194 for (const int vert : corner_verts.slice(faces[face])) {
1195 if (visited_verts[vert]) {
1196 continue;
1197 }
1198 dists[vert] = dists[next_vert] + 1.0f;
1199 visited_verts[vert].set();
1200 queue.push(vert);
1201 }
1202 }
1203 }
1204
1205 return dists;
1206}
1207
1212static void update_max_vert_falloff_value(const Object &object, Cache &expand_cache)
1213{
1214 SculptSession &ss = *object.runtime->sculpt_session;
1217 4096,
1218 std::numeric_limits<float>::lowest(),
1219 [&](const IndexRange range, float max) {
1220 for (const int vert : range) {
1221 if (expand_cache.vert_falloff[vert] == FLT_MAX) {
1222 continue;
1223 }
1224 if (!is_vert_in_active_component(ss, expand_cache, vert)) {
1225 continue;
1226 }
1227 max = std::max(max, expand_cache.vert_falloff[vert]);
1228 }
1229 return max;
1230 },
1231 [](const float a, const float b) { return std::max(a, b); });
1232}
1233
1238static void update_max_face_falloff_factor(const Object &object, Mesh &mesh, Cache &expand_cache)
1239{
1240 const OffsetIndices<int> faces = mesh.faces();
1241 const Span<int> corner_verts = mesh.corner_verts();
1243 faces.index_range(),
1244 4096,
1245 std::numeric_limits<float>::lowest(),
1246 [&](const IndexRange range, float max) {
1247 for (const int face : range) {
1248 if (expand_cache.face_falloff[face] == FLT_MAX) {
1249 continue;
1250 }
1251 if (!is_face_in_active_component(object, faces, corner_verts, expand_cache, face)) {
1252 continue;
1253 }
1254 max = std::max(max, expand_cache.face_falloff[face]);
1255 }
1256 return max;
1257 },
1258 [](const float a, const float b) { return std::max(a, b); });
1259}
1260
1267static void vert_to_face_falloff_grids(SculptSession &ss, Mesh *mesh, Cache &expand_cache)
1268{
1269 const OffsetIndices faces = mesh->faces();
1271
1272 threading::parallel_for(faces.index_range(), 1024, [&](const IndexRange range) {
1273 for (const int face : range) {
1274 float accum = 0.0f;
1275 for (const int corner : faces[face]) {
1276 const int grid_loop_index = corner * key.grid_area;
1277 for (int g = 0; g < key.grid_area; g++) {
1278 accum += expand_cache.vert_falloff[grid_loop_index + g];
1279 }
1280 }
1281 expand_cache.face_falloff[face] = accum / (faces[face].size() * key.grid_area);
1282 }
1283 });
1284}
1285
1286static void vert_to_face_falloff_mesh(Mesh *mesh, Cache &expand_cache)
1287{
1288 const OffsetIndices faces = mesh->faces();
1289 const Span<int> corner_verts = mesh->corner_verts();
1290
1291 threading::parallel_for(faces.index_range(), 1024, [&](const IndexRange range) {
1292 for (const int face : range) {
1293 const Span<int> face_verts = corner_verts.slice(faces[face]);
1294 float accum = 0.0f;
1295 for (const int vert : face_verts) {
1296 accum += expand_cache.vert_falloff[vert];
1297 }
1298 expand_cache.face_falloff[face] = accum / face_verts.size();
1299 }
1300 });
1301}
1302
1306static void vert_to_face_falloff(Object &object, Mesh *mesh, Cache &expand_cache)
1307{
1308 BLI_assert(!expand_cache.vert_falloff.is_empty());
1309
1310 expand_cache.face_falloff.reinitialize(mesh->faces_num);
1311 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1312
1313 if (pbvh.type() == bke::pbvh::Type::Mesh) {
1314 vert_to_face_falloff_mesh(mesh, expand_cache);
1315 }
1316 else if (pbvh.type() == bke::pbvh::Type::Grids) {
1317 vert_to_face_falloff_grids(*object.runtime->sculpt_session, mesh, expand_cache);
1318 }
1319 else {
1320 BLI_assert(false);
1321 }
1322}
1323
1324/* Recursions. These functions will generate new falloff values based on the state of the vertices
1325 * from the current Cache options and falloff values. */
1326
1331static void geodesics_from_state_boundary(const Depsgraph &depsgraph,
1332 Object &ob,
1333 Cache &expand_cache,
1334 const BitSpan enabled_verts)
1335{
1337
1338 IndexMaskMemory memory;
1339 const IndexMask boundary_verts = boundary_from_enabled(ob, enabled_verts, false, memory);
1340
1341 expand_cache.face_falloff = {};
1342
1343 expand_cache.vert_falloff = geodesic_falloff_create(depsgraph, ob, boundary_verts);
1344}
1345
1351 Cache &expand_cache,
1352 const BitSpan enabled_verts)
1353{
1354 expand_cache.face_falloff = {};
1355
1356 expand_cache.vert_falloff.reinitialize(vertex_count_get(ob));
1357 expand_cache.vert_falloff.fill(0);
1358
1359 IndexMaskMemory memory;
1360 const IndexMask boundary_verts = boundary_from_enabled(ob, enabled_verts, false, memory);
1361
1362 calc_topology_falloff_from_verts(ob, boundary_verts, expand_cache.vert_falloff);
1363}
1364
1368static void resursion_step_add(const Depsgraph &depsgraph,
1369 Object &ob,
1370 Cache &expand_cache,
1371 const RecursionType recursion_type)
1372{
1374 if (pbvh.type() != bke::pbvh::Type::Mesh) {
1375 return;
1376 }
1377
1378 const BitVector<> enabled_verts = enabled_state_to_bitmap(depsgraph, ob, expand_cache);
1379
1380 /* Each time a new recursion step is created, reset the distortion strength. This is the expected
1381 * result from the recursion, as otherwise the new falloff will render with undesired distortion
1382 * from the beginning. */
1383 expand_cache.texture_distortion_strength = 0.0f;
1384
1385 switch (recursion_type) {
1387 geodesics_from_state_boundary(depsgraph, ob, expand_cache, enabled_verts);
1388 break;
1390 topology_from_state_boundary(ob, expand_cache, enabled_verts);
1391 break;
1392 }
1393
1394 update_max_vert_falloff_value(ob, expand_cache);
1395 if (expand_cache.target == TargetType::FaceSets) {
1396 Mesh &mesh = *id_cast<Mesh *>(ob.data);
1397 vert_to_face_falloff(ob, &mesh, expand_cache);
1398 update_max_face_falloff_factor(ob, mesh, expand_cache);
1399 }
1400}
1401
1402/* Face Set Boundary falloff. */
1403
1408static void init_from_face_set_boundary(const Depsgraph &depsgraph,
1409 Object &ob,
1410 Cache &expand_cache,
1411 const int active_face_set,
1412 const bool internal_falloff)
1413{
1415 const int totvert = vertex_count_get(ob);
1416
1417 Array<bool> vert_has_face_set(totvert);
1418 Array<bool> vert_has_unique_face_set(totvert);
1419 switch (pbvh.type()) {
1420 case bke::pbvh::Type::Mesh: {
1421 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
1422 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1423 const bke::AttributeAccessor attributes = mesh.attributes();
1424 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set",
1426 threading::parallel_for(IndexRange(totvert), 1024, [&](const IndexRange range) {
1427 for (const int vert : range) {
1428 vert_has_face_set[vert] = face_set::vert_has_face_set(
1429 vert_to_face_map, face_sets, vert, active_face_set);
1430 vert_has_unique_face_set[vert] = face_set::vert_has_unique_face_set(
1431 vert_to_face_map, face_sets, vert);
1432 }
1433 });
1434 break;
1435 }
1437 const Mesh &base_mesh = *id_cast<const Mesh *>(ob.data);
1438 const OffsetIndices<int> faces = base_mesh.faces();
1439 const Span<int> corner_verts = base_mesh.corner_verts();
1440 const GroupedSpan<int> vert_to_face_map = base_mesh.vert_to_face_map();
1441 const bke::AttributeAccessor attributes = base_mesh.attributes();
1442 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set",
1444 const SubdivCCG &subdiv_ccg = *ob.runtime->sculpt_session->subdiv_ccg;
1445 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1446 threading::parallel_for(IndexRange(totvert), 1024, [&](const IndexRange range) {
1447 for (const int vert : range) {
1448 const SubdivCCGCoord coord = SubdivCCGCoord::from_index(key, vert);
1449 vert_has_face_set[vert] = face_set::coord_has_face_set(faces,
1450 corner_verts,
1451 vert_to_face_map,
1452 face_sets,
1453 subdiv_ccg,
1454 coord,
1455 active_face_set);
1456 vert_has_unique_face_set[vert] = face_set::vert_has_unique_face_set(
1457 faces, corner_verts, vert_to_face_map, face_sets, subdiv_ccg, coord);
1458 }
1459 });
1460 break;
1461 }
1463 BMesh &bm = *ob.runtime->sculpt_session->bm;
1464 const int offset = CustomData_get_offset_named(&bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
1466 threading::parallel_for(IndexRange(totvert), 1024, [&](const IndexRange range) {
1467 for (const int vert : range) {
1468 const BMVert *bm_vert = BM_vert_at_index(&bm, vert);
1469 vert_has_face_set[vert] = face_set::vert_has_face_set(offset, *bm_vert, active_face_set);
1470 vert_has_unique_face_set[vert] = face_set::vert_has_unique_face_set(offset, *bm_vert);
1471 }
1472 });
1473 break;
1474 }
1475 }
1476
1477 BitVector<> enabled_verts(totvert);
1478 for (int i = 0; i < totvert; i++) {
1479 if (!vert_has_unique_face_set[i]) {
1480 continue;
1481 }
1482 if (!vert_has_face_set[i]) {
1483 continue;
1484 }
1485 enabled_verts[i].set();
1486 }
1487
1488 if (pbvh.type() == bke::pbvh::Type::Mesh) {
1489 geodesics_from_state_boundary(depsgraph, ob, expand_cache, enabled_verts);
1490 }
1491 else {
1492 topology_from_state_boundary(ob, expand_cache, enabled_verts);
1493 }
1494
1495 if (internal_falloff) {
1496 for (int i = 0; i < totvert; i++) {
1497 if (!(vert_has_face_set[i] && vert_has_unique_face_set[i])) {
1498 continue;
1499 }
1500 expand_cache.vert_falloff[i] *= -1.0f;
1501 }
1502
1503 float min_factor = FLT_MAX;
1504 for (int i = 0; i < totvert; i++) {
1505 min_factor = min_ff(expand_cache.vert_falloff[i], min_factor);
1506 }
1507
1508 const float additional_falloff = fabsf(min_factor);
1509 for (int i = 0; i < totvert; i++) {
1510 expand_cache.vert_falloff[i] += additional_falloff;
1511 }
1512 }
1513 else {
1514 for (int i = 0; i < totvert; i++) {
1515 if (!vert_has_face_set[i]) {
1516 continue;
1517 }
1518 expand_cache.vert_falloff[i] = 0.0f;
1519 }
1520 }
1521}
1522
1527static void calc_falloff_from_vert_and_symmetry(const Depsgraph &depsgraph,
1528 Cache &expand_cache,
1529 Object &ob,
1530 const int vert,
1531 FalloffType falloff_type)
1532{
1533 expand_cache.falloff_type = falloff_type;
1534
1536 const bool has_topology_info = pbvh.type() == bke::pbvh::Type::Mesh;
1537
1538 switch (falloff_type) {
1540 expand_cache.vert_falloff = has_topology_info ?
1541 geodesic_falloff_create(depsgraph, ob, vert) :
1542 spherical_falloff_create(depsgraph, ob, vert);
1543 break;
1545 expand_cache.vert_falloff = topology_falloff_create(depsgraph, ob, vert);
1546 break;
1548 expand_cache.vert_falloff = has_topology_info ?
1549 diagonals_falloff_create(depsgraph, ob, vert) :
1550 topology_falloff_create(depsgraph, ob, vert);
1551 break;
1553 expand_cache.vert_falloff = normals_falloff_create(
1554 depsgraph,
1555 ob,
1556 vert,
1558 expand_cache.normal_falloff_blur_steps);
1559 break;
1561 expand_cache.vert_falloff = spherical_falloff_create(depsgraph, ob, vert);
1562 break;
1564 expand_cache.vert_falloff = boundary_topology_falloff_create(depsgraph, ob, vert);
1565 break;
1568 depsgraph, ob, expand_cache, expand_cache.initial_active_face_set, true);
1569 break;
1572 depsgraph, ob, expand_cache, expand_cache.initial_active_face_set, false);
1573 break;
1574 }
1575
1576 /* Update max falloff values and propagate to base mesh faces if needed. */
1577 update_max_vert_falloff_value(ob, expand_cache);
1578 if (expand_cache.target == TargetType::FaceSets) {
1579 Mesh &mesh = *id_cast<Mesh *>(ob.data);
1580 vert_to_face_falloff(ob, &mesh, expand_cache);
1581 update_max_face_falloff_factor(ob, mesh, expand_cache);
1582 }
1583}
1584
1590static void snap_init_from_enabled(const Depsgraph &depsgraph,
1591 const Object &object,
1592 Cache &expand_cache)
1593{
1594 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1595 if (pbvh.type() != bke::pbvh::Type::Mesh) {
1596 return;
1597 }
1598 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1599 const OffsetIndices<int> faces = mesh.faces();
1600 const Span<int> corner_verts = mesh.corner_verts();
1601 /* Make sure this code runs with snapping and invert disabled. This simplifies the code and
1602 * prevents using this function with snapping already enabled. */
1603 const bool prev_snap_state = expand_cache.snap;
1604 const bool prev_invert_state = expand_cache.invert;
1605 expand_cache.snap = false;
1606 expand_cache.invert = false;
1607
1608 const BitVector<> enabled_verts = enabled_state_to_bitmap(depsgraph, object, expand_cache);
1609
1610 for (const int i : faces.index_range()) {
1611 const int face_set = expand_cache.original_face_sets[i];
1612 expand_cache.snap_enabled_face_sets->add(face_set);
1613 }
1614
1615 for (const int i : faces.index_range()) {
1616 const Span<int> face_verts = corner_verts.slice(faces[i]);
1617 const bool any_disabled = std::any_of(face_verts.begin(),
1618 face_verts.end(),
1619 [&](const int vert) { return !enabled_verts[vert]; });
1620 if (any_disabled) {
1621 const int face_set = expand_cache.original_face_sets[i];
1622 expand_cache.snap_enabled_face_sets->remove(face_set);
1623 }
1624 }
1625
1626 expand_cache.snap = prev_snap_state;
1627 expand_cache.invert = prev_invert_state;
1628}
1629
1631{
1632 MEM_delete<Cache>(ss.expand_cache);
1633 /* Needs to be set to nullptr as the paint cursor relies on checking this pointer detecting if an
1634 * expand operation is running. */
1635 ss.expand_cache = nullptr;
1636}
1637
1641static void restore_face_set_data(Object &object, Cache &expand_cache)
1642{
1645 *id_cast<Mesh *>(object.data));
1646 face_sets.span.copy_from(expand_cache.original_face_sets);
1647 face_sets.finish();
1648
1649 IndexMaskMemory memory;
1650 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1651 pbvh.tag_face_sets_changed(node_mask);
1652}
1653
1654static void restore_color_data(Object &ob, Cache &expand_cache)
1655{
1658 Mesh &mesh = *id_cast<Mesh *>(ob.data);
1659 IndexMaskMemory memory;
1660 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1661
1662 const OffsetIndices<int> faces = mesh.faces();
1663 const Span<int> corner_verts = mesh.corner_verts();
1664 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1666 node_mask.foreach_index([&](const int i) {
1667 for (const int vert : nodes[i].verts()) {
1669 corner_verts,
1670 vert_to_face_map,
1671 color_attribute.domain,
1672 vert,
1673 expand_cache.original_colors[vert],
1674 color_attribute.span);
1675 }
1676 });
1677 pbvh.tag_attribute_changed(node_mask, mesh.active_color_attribute);
1678 color_attribute.finish();
1679}
1680
1681static void write_mask_data(Object &object, const Span<float> mask)
1682{
1683 SculptSession &ss = *object.runtime->sculpt_session;
1685 IndexMaskMemory memory;
1686 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1687 switch (pbvh.type()) {
1688 case bke::pbvh::Type::Mesh: {
1689 Mesh &mesh = *id_cast<Mesh *>(object.data);
1690 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
1691 attributes.remove(".sculpt_mask");
1692 attributes.add<float>(".sculpt_mask",
1696 break;
1697 }
1699 BMesh &bm = *ss.bm;
1700 const int offset = CustomData_get_offset_named(&bm.vdata, CD_PROP_FLOAT, ".sculpt_mask");
1702 for (const int i : mask.index_range()) {
1703 BM_ELEM_CD_SET_FLOAT(BM_vert_at_index(&bm, i), offset, mask[i]);
1704 }
1706 break;
1707 }
1709 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1710 subdiv_ccg.masks.as_mutable_span().copy_from(mask);
1711 bke::pbvh::update_mask_grids(subdiv_ccg, node_mask, pbvh);
1712 break;
1713 }
1714 }
1715 pbvh.tag_masks_changed(node_mask);
1716}
1717
1718/* Main function to restore the original state of the data to how it was before starting the expand
1719 * operation. */
1720static void restore_original_state(bContext *C, Object &ob, Cache &expand_cache)
1721{
1722 switch (expand_cache.target) {
1723 case TargetType::Mask:
1724 write_mask_data(ob, expand_cache.original_mask);
1728 break;
1730 restore_face_set_data(ob, expand_cache);
1734 break;
1735 case TargetType::Colors:
1736 restore_color_data(ob, expand_cache);
1739 break;
1740 }
1741}
1742
1747{
1750
1752
1753 undo::push_end(ob);
1755
1756 ED_workspace_status_text(C, nullptr);
1757}
1758
1759/* Functions to update the sculpt mesh data. */
1760
1761static void calc_new_mask_mesh(const SculptSession &ss,
1762 const Span<float3> positions,
1763 const BitSpan enabled_verts,
1764 const Span<int> verts,
1766{
1767 const Cache &expand_cache = *ss.expand_cache;
1768
1769 for (const int i : verts.index_range()) {
1770 const int vert = verts[i];
1771 const bool enabled = enabled_verts[vert];
1772
1773 if (expand_cache.check_islands && !is_vert_in_active_component(ss, expand_cache, vert)) {
1774 continue;
1775 }
1776
1777 if (enabled) {
1778 mask[i] = gradient_value_get(ss, expand_cache, positions[vert], vert);
1779 }
1780 else {
1781 mask[i] = 0.0f;
1782 }
1783
1784 if (expand_cache.preserve) {
1785 if (expand_cache.invert) {
1786 mask[i] = min_ff(mask[i], expand_cache.original_mask[vert]);
1787 }
1788 else {
1789 mask[i] = max_ff(mask[i], expand_cache.original_mask[vert]);
1790 }
1791 }
1792 }
1793
1795}
1796
1797static bool update_mask_grids(const SculptSession &ss,
1798 const BitSpan enabled_verts,
1800 SubdivCCG &subdiv_ccg)
1801{
1802 const Cache &expand_cache = *ss.expand_cache;
1803 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1804 const Span<float3> positions = subdiv_ccg.positions;
1805 MutableSpan<float> masks = subdiv_ccg.masks;
1806
1807 bool any_changed = false;
1808 for (const int grid : node.grids()) {
1809 for (const int vert : bke::ccg::grid_range(key, grid)) {
1810 const float initial_mask = masks[vert];
1811
1812 if (expand_cache.check_islands && !is_vert_in_active_component(ss, expand_cache, vert)) {
1813 continue;
1814 }
1815
1816 float new_mask;
1817
1818 if (enabled_verts[vert]) {
1819 new_mask = gradient_value_get(ss, expand_cache, positions[vert], vert);
1820 }
1821 else {
1822 new_mask = 0.0f;
1823 }
1824
1825 if (expand_cache.preserve) {
1826 if (expand_cache.invert) {
1827 new_mask = min_ff(new_mask, expand_cache.original_mask[vert]);
1828 }
1829 else {
1830 new_mask = max_ff(new_mask, expand_cache.original_mask[vert]);
1831 }
1832 }
1833
1834 if (new_mask == initial_mask) {
1835 continue;
1836 }
1837
1838 masks[vert] = clamp_f(new_mask, 0.0f, 1.0f);
1839 any_changed = true;
1840 }
1841 }
1842 if (any_changed) {
1844 }
1845 return any_changed;
1846}
1847
1848static float calc_new_mask_bmesh(const SculptSession &ss,
1849 const Cache &expand_cache,
1850 const float old_mask,
1851 const BitSpan enabled_verts,
1852 const BMVert *vert)
1853{
1854 const int vert_index = BM_elem_index_get(vert);
1855 if (expand_cache.check_islands && !is_vert_in_active_component(ss, expand_cache, vert_index)) {
1856 return old_mask;
1857 }
1858 float new_mask;
1859 if (enabled_verts[vert_index]) {
1860 new_mask = gradient_value_get(ss, expand_cache, vert->co, vert_index);
1861 }
1862 else {
1863 new_mask = 0.0f;
1864 }
1865 if (expand_cache.preserve) {
1866 if (expand_cache.invert) {
1867 new_mask = min_ff(new_mask, expand_cache.original_mask[vert_index]);
1868 }
1869 else {
1870 new_mask = max_ff(new_mask, expand_cache.original_mask[vert_index]);
1871 }
1872 }
1873 return clamp_f(new_mask, 0.0f, 1.0f);
1874}
1875
1877 const BitSpan enabled_verts,
1878 const int mask_offset,
1879 const Span<float> old_mask,
1881{
1882 const Cache &expand_cache = *ss.expand_cache;
1883
1884 bool any_changed = false;
1886 const float initial_mask = BM_ELEM_CD_GET_FLOAT(vert, mask_offset);
1887 float new_mask = calc_new_mask_bmesh(ss, expand_cache, initial_mask, enabled_verts, vert);
1888 if (new_mask != initial_mask) {
1889 any_changed = true;
1890 }
1891 BM_ELEM_CD_SET_FLOAT(vert, mask_offset, new_mask);
1892 }
1893 if (!any_changed) {
1894 int i = 0;
1896 if (calc_new_mask_bmesh(ss, expand_cache, old_mask[i], enabled_verts, vert) != old_mask[i]) {
1897 any_changed = true;
1898 break;
1899 }
1900 i++;
1901 }
1902 }
1903 if (!any_changed) {
1904 return false;
1905 }
1907 return true;
1908}
1909
1913static void face_sets_update(Object &object, Cache &expand_cache)
1914{
1915 Mesh &mesh = *id_cast<Mesh *>(object.data);
1917 const OffsetIndices<int> faces = mesh.faces();
1918 const Span<int> corner_verts = mesh.corner_verts();
1919 const bke::AttributeAccessor attributes = mesh.attributes();
1920 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1921 for (const int f : face_sets.span.index_range()) {
1922 const bool enabled = face_state_get(
1923 object, faces, corner_verts, hide_poly, face_sets.span, expand_cache, f);
1924 if (!enabled) {
1925 continue;
1926 }
1927 if (expand_cache.preserve) {
1928 face_sets.span[f] += expand_cache.next_face_set;
1929 }
1930 else {
1931 face_sets.span[f] = expand_cache.next_face_set;
1932 }
1933 }
1934
1935 face_sets.finish();
1936
1938 pbvh.tag_face_sets_changed(expand_cache.node_mask);
1939}
1940
1944static bool colors_update_task(const Depsgraph &depsgraph,
1945 Object &object,
1946 const Span<float3> vert_positions,
1947 const OffsetIndices<int> faces,
1948 const Span<int> corner_verts,
1949 const GroupedSpan<int> vert_to_face_map,
1950 const Span<bool> hide_vert,
1951 const Span<float> mask,
1953 bke::GSpanAttributeWriter &color_attribute)
1954{
1955 const SculptSession &ss = *object.runtime->sculpt_session;
1956 const Cache &expand_cache = *ss.expand_cache;
1957
1958 const BitVector<> enabled_verts = enabled_state_to_bitmap(depsgraph, object, expand_cache);
1959
1960 bool any_changed = false;
1961 const Span<int> verts = node->verts();
1962 for (const int i : verts.index_range()) {
1963 const int vert = verts[i];
1964 if (!hide_vert.is_empty() && hide_vert[vert]) {
1965 continue;
1966 }
1967
1968 float4 initial_color = color::color_vert_get(
1969 faces, corner_verts, vert_to_face_map, color_attribute.span, color_attribute.domain, vert);
1970
1971 float fade;
1972
1973 if (enabled_verts[vert]) {
1974 fade = gradient_value_get(ss, expand_cache, vert_positions[vert], vert);
1975 }
1976 else {
1977 fade = 0.0f;
1978 }
1979
1980 if (!mask.is_empty()) {
1981 fade *= 1.0f - mask[vert];
1982 }
1983 fade = clamp_f(fade, 0.0f, 1.0f);
1984
1985 float4 final_color;
1986 float4 final_fill_color;
1987 mul_v4_v4fl(final_fill_color, expand_cache.fill_color, fade);
1988 IMB_blend_color_float(final_color,
1989 expand_cache.original_colors[vert],
1990 final_fill_color,
1991 IMB_BlendMode(expand_cache.blend_mode));
1992
1993 if (initial_color == final_color) {
1994 continue;
1995 }
1996
1998 corner_verts,
1999 vert_to_face_map,
2000 color_attribute.domain,
2001 vert,
2002 final_color,
2003 color_attribute.span);
2004
2005 any_changed = true;
2006 }
2007 return any_changed;
2008}
2009
2010/* Store the original mesh data state in the expand cache. */
2011static void original_state_store(Object &ob, Cache &expand_cache)
2012{
2013 Mesh &mesh = *id_cast<Mesh *>(ob.data);
2014 const int totvert = vertex_count_get(ob);
2015
2017
2018 /* Face sets are always stored as they are needed for snapping. */
2021
2022 if (expand_cache.target == TargetType::Mask) {
2023 expand_cache.original_mask = mask::duplicate_mask(ob);
2024 }
2025
2026 if (expand_cache.target == TargetType::Colors) {
2027 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
2028 const OffsetIndices<int> faces = mesh.faces();
2029 const Span<int> corner_verts = mesh.corner_verts();
2030 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
2032 const GVArraySpan colors = *color_attribute;
2033
2034 expand_cache.original_colors = Array<float4>(totvert);
2035 for (int i = 0; i < totvert; i++) {
2036 expand_cache.original_colors[i] = color::color_vert_get(
2037 faces, corner_verts, vert_to_face_map, colors, color_attribute.domain, i);
2038 }
2039 }
2040}
2041
2045static void face_sets_restore(Object &object, Cache &expand_cache)
2046{
2047 Mesh &mesh = *id_cast<Mesh *>(object.data);
2048 const OffsetIndices<int> faces = mesh.faces();
2049 const Span<int> corner_verts = mesh.corner_verts();
2051 for (const int i : faces.index_range()) {
2052 if (expand_cache.original_face_sets[i] <= 0) {
2053 /* Do not modify hidden face sets, even when restoring the IDs state. */
2054 continue;
2055 }
2056 if (!is_face_in_active_component(object, faces, corner_verts, expand_cache, i)) {
2057 continue;
2058 }
2059 face_sets.span[i] = expand_cache.initial_face_sets[i];
2060 }
2061 face_sets.finish();
2062}
2063
2064static void update_for_vert(bContext *C, Object &ob, const std::optional<int> vertex)
2065{
2066 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
2069 Cache &expand_cache = *ss.expand_cache;
2070
2071 /* Update the active factor in the cache. */
2072 if (!vertex) {
2073 /* This means that the cursor is not over the mesh, so a valid active falloff can't be
2074 * determined. In this situations, don't evaluate enabled states and default all vertices in
2075 * connected components to enabled. */
2076 expand_cache.active_falloff = expand_cache.max_vert_falloff;
2077 expand_cache.all_enabled = true;
2078 }
2079 else {
2080 expand_cache.active_falloff = expand_cache.vert_falloff[*vertex];
2081 expand_cache.all_enabled = false;
2082 }
2083
2084 if (expand_cache.target == TargetType::FaceSets) {
2085 /* Face sets needs to be restored their initial state on each iteration as the overwrite
2086 * existing data. */
2087 face_sets_restore(ob, expand_cache);
2088 }
2089
2090 const IndexMask &node_mask = expand_cache.node_mask;
2091
2092 const BitVector<> enabled_verts = enabled_state_to_bitmap(depsgraph, ob, expand_cache);
2093
2094 switch (expand_cache.target) {
2095 case TargetType::Mask: {
2096 switch (pbvh.type()) {
2097 case bke::pbvh::Type::Mesh: {
2098 const Span<float3> positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
2100 depsgraph, ob, node_mask, [&](const MutableSpan<float> mask, const Span<int> verts) {
2101 calc_new_mask_mesh(ss, positions, enabled_verts, verts, mask);
2102 });
2103 break;
2104 }
2106 Array<bool> node_changed(node_mask.min_array_size(), false);
2107
2109 node_mask.foreach_index(
2110 [&](const int i) {
2111 node_changed[i] = update_mask_grids(ss, enabled_verts, nodes[i], *ss.subdiv_ccg);
2112 },
2114
2115 IndexMaskMemory memory;
2116 pbvh.tag_masks_changed(IndexMask::from_bools(node_changed, memory));
2117
2119 break;
2120 }
2122 const int mask_offset = CustomData_get_offset_named(
2123 &ss.bm->vdata, CD_PROP_FLOAT, ".sculpt_mask");
2125
2126 Array<Vector<float>> old_masks(node_mask.min_array_size());
2127 node_mask.foreach_index(
2128 [&](const int i) {
2130 old_masks[i].resize(other.size());
2131 int j = 0;
2132 for (BMVert *vert : other) {
2133 old_masks[i][j] = BM_ELEM_CD_GET_FLOAT(vert, mask_offset);
2134 j++;
2135 }
2136 },
2138 Array<bool> node_changed(node_mask.min_array_size(), false);
2139 node_mask.foreach_index(
2140 [&](const int i) {
2141 node_changed[i] = update_mask_bmesh(
2142 ss, enabled_verts, mask_offset, old_masks[i].as_span(), &nodes[i]);
2143 },
2145
2146 IndexMaskMemory memory;
2147 pbvh.tag_masks_changed(IndexMask::from_bools(node_changed, memory));
2148 break;
2149 }
2150 }
2152 break;
2153 }
2155 face_sets_update(ob, expand_cache);
2157 break;
2158 case TargetType::Colors: {
2159 Mesh &mesh = *id_cast<Mesh *>(ob.data);
2160 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
2161 const OffsetIndices<int> faces = mesh.faces();
2162 const Span<int> corner_verts = mesh.corner_verts();
2163 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
2164 const bke::AttributeAccessor attributes = mesh.attributes();
2165 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
2166 const VArraySpan mask = *attributes.lookup<float>(".sculpt_mask", bke::AttrDomain::Point);
2168
2169 Array<bool> node_changed(node_mask.min_array_size(), false);
2170
2172 node_mask.foreach_index(
2173 [&](const int i) {
2174 node_changed[i] = colors_update_task(depsgraph,
2175 ob,
2176 vert_positions,
2177 faces,
2178 corner_verts,
2179 vert_to_face_map,
2180 hide_vert,
2181 mask,
2182 &nodes[i],
2183 color_attribute);
2184 },
2186
2187 IndexMaskMemory memory;
2188 pbvh.tag_attribute_changed(IndexMask::from_bools(node_changed, memory),
2189 mesh.active_color_attribute);
2190
2191 color_attribute.finish();
2193 break;
2194 }
2195 }
2196}
2197
2202static std::optional<int> target_vert_update_and_get(bContext *C, Object &ob, const float mval[2])
2203{
2205 if (cursor_geometry_info_update(C, mval, false)) {
2206 return ss.active_vert_index();
2207 }
2208 return std::nullopt;
2209}
2210
2215static void reposition_pivot(bContext *C, Object &ob, Cache &expand_cache)
2216{
2218 const char symm = mesh_symmetry_xyz_get(ob);
2219 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
2220
2221 const bool initial_invert_state = expand_cache.invert;
2222 expand_cache.invert = false;
2223 const BitVector<> enabled_verts = enabled_state_to_bitmap(depsgraph, ob, expand_cache);
2224
2225 /* For boundary topology, position the pivot using only the boundary of the enabled vertices,
2226 * without taking mesh boundary into account. This allows to create deformations like bending the
2227 * mesh from the boundary of the mask that was just created. */
2228 const float use_mesh_boundary = expand_cache.falloff_type != FalloffType::BoundaryTopology;
2229
2230 IndexMaskMemory memory;
2231 const IndexMask boundary_verts = boundary_from_enabled(
2232 ob, enabled_verts, use_mesh_boundary, memory);
2233
2234 /* Ignore invert state, as this is the expected behavior in most cases and mask are created in
2235 * inverted state by default. */
2236 expand_cache.invert = initial_invert_state;
2237
2238 double3 average(0);
2239 int total = 0;
2240 switch (bke::object::pbvh_get(ob)->type()) {
2241 case bke::pbvh::Type::Mesh: {
2242 const Span<float3> positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
2243 const float3 expand_init_co = positions[expand_cache.initial_active_vert];
2244 boundary_verts.foreach_index([&](const int vert) {
2245 if (!is_vert_in_active_component(ss, expand_cache, vert)) {
2246 return;
2247 }
2248 const float3 &position = positions[vert];
2249 if (!check_vertex_pivot_symmetry(position, expand_init_co, symm)) {
2250 return;
2251 }
2253 total++;
2254 });
2255 break;
2256 }
2258 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
2259 const Span<float3> positions = subdiv_ccg.positions;
2260 const float3 expand_init_co = positions[expand_cache.initial_active_vert];
2261 boundary_verts.foreach_index([&](const int vert) {
2262 if (!is_vert_in_active_component(ss, expand_cache, vert)) {
2263 return;
2264 }
2265 const float3 position = positions[vert];
2266 if (!check_vertex_pivot_symmetry(position, expand_init_co, symm)) {
2267 return;
2268 }
2270 total++;
2271 });
2272 break;
2273 }
2275 BMesh &bm = *ss.bm;
2276 const float3 expand_init_co = BM_vert_at_index(&bm, expand_cache.initial_active_vert)->co;
2277 boundary_verts.foreach_index([&](const int vert) {
2278 if (!is_vert_in_active_component(ss, expand_cache, vert)) {
2279 return;
2280 }
2281 const float3 position = BM_vert_at_index(&bm, vert)->co;
2282 if (!check_vertex_pivot_symmetry(position, expand_init_co, symm)) {
2283 return;
2284 }
2286 total++;
2287 });
2288 break;
2289 }
2290 }
2291
2292 if (total > 0) {
2293 ss.pivot_pos = float3(average / total);
2294 }
2295
2297}
2298
2299static void finish(bContext *C)
2300{
2303 undo::push_end(ob);
2304
2305 switch (ss.expand_cache->target) {
2306 case TargetType::Mask:
2308 break;
2311 break;
2312 case TargetType::Colors:
2314 break;
2315 }
2316
2318 ED_workspace_status_text(C, nullptr);
2319}
2320
2325static void find_active_connected_components_from_vert(const Depsgraph &depsgraph,
2326 Object &ob,
2327 Cache &expand_cache,
2328 const int initial_vertex)
2329{
2331 for (int i = 0; i < EXPAND_SYMM_AREAS; i++) {
2333 }
2334
2336
2337 const Vector<int> symm_verts = find_symm_verts(depsgraph, ob, initial_vertex);
2338
2339 int valid_index = 0;
2340 for (int symm_it = 0; symm_it <= symm; symm_it++) {
2341 if (!is_symmetry_iteration_valid(symm_it, symm)) {
2342 continue;
2343 }
2344 expand_cache.active_connected_islands[symm_it] = islands::vert_id_get(ss,
2345 symm_verts[valid_index]);
2346 valid_index++;
2347 }
2348}
2349
2355 Object &ob,
2356 Cache &expand_cache,
2357 const float mval[2])
2358{
2360 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
2361
2362 std::optional<int> initial_vert = target_vert_update_and_get(C, ob, mval);
2363 if (!initial_vert) {
2364 /* Cursor not over the mesh, for creating valid initial falloffs, fall back to the last active
2365 * vertex in the sculpt session. */
2366 const int last_active_vert_index = ss.last_active_vert_index();
2367 /* It still may be the case that there is no last active vert in rare circumstances for
2368 * everyday usage.
2369 * (i.e. if the cursor has never been over the mesh at all). A solution to both this problem
2370 * and needing to store this data is to figure out which is the nearest vertex to the current
2371 * cursor position */
2372 if (last_active_vert_index == -1) {
2373 return false;
2374 }
2375 initial_vert = last_active_vert_index;
2376 }
2377
2379 expand_cache.initial_active_vert = *initial_vert;
2381
2382 if (expand_cache.next_face_set == face_set_none_id) {
2383 /* Only set the next face set once, otherwise this ID will constantly update to a new one each
2384 * time this function is called for using a new initial vertex from a different cursor
2385 * position. */
2386 if (expand_cache.modify_active_face_set) {
2388 }
2389 else {
2391 }
2392 }
2393
2394 /* The new mouse position can be over a different connected component, so this needs to be
2395 * updated. */
2396 find_active_connected_components_from_vert(depsgraph, ob, expand_cache, *initial_vert);
2397 return true;
2398}
2399
2405 Object &ob,
2406 const wmEvent *event,
2407 Cache &expand_cache)
2408{
2409 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
2410 const float mval_fl[2] = {float(event->mval[0]), float(event->mval[1])};
2411 float move_disp[2];
2412 sub_v2_v2v2(move_disp, mval_fl, expand_cache.initial_mouse_move);
2413
2414 float new_mval[2];
2415 add_v2_v2v2(new_mval, move_disp, expand_cache.original_mouse_move);
2416
2417 set_initial_components_for_mouse(C, ob, expand_cache, new_mval);
2419 expand_cache,
2420 ob,
2421 expand_cache.initial_active_vert,
2422 expand_cache.move_preview_falloff_type);
2423}
2424
2429{
2434 if (!ss.tex_pool) {
2436 }
2437}
2438
2442static int active_face_set_id_get(Object &object, Cache &expand_cache)
2443{
2444 SculptSession &ss = *object.runtime->sculpt_session;
2445 switch (bke::object::pbvh_get(object)->type()) {
2447 if (!ss.active_face_index) {
2448 return face_set_none_id;
2449 }
2450 return expand_cache.original_face_sets[*ss.active_face_index];
2452 if (!ss.active_grid_index) {
2453 return face_set_none_id;
2454 }
2455 const int face_index = BKE_subdiv_ccg_grid_to_face_index(*ss.subdiv_ccg,
2456 *ss.active_grid_index);
2457 return expand_cache.original_face_sets[face_index];
2458 }
2460 /* Dyntopo does not support face set functionality. */
2461 BLI_assert(false);
2462 }
2463 }
2464 return face_set_none_id;
2465}
2466
2467static void sculpt_expand_status(bContext *C, wmOperator *op, Cache *expand_cache)
2468{
2469 WorkspaceStatus status(C);
2470
2471 status.opmodal(IFACE_("Confirm"), op->type, SCULPT_EXPAND_MODAL_CONFIRM);
2472 status.opmodal(IFACE_("Cancel"), op->type, SCULPT_EXPAND_MODAL_CANCEL);
2473 status.opmodal(IFACE_("Invert"), op->type, SCULPT_EXPAND_MODAL_INVERT, expand_cache->invert);
2474 status.opmodal(IFACE_("Snap"), op->type, SCULPT_EXPAND_MODAL_SNAP_TOGGLE, expand_cache->snap);
2475 status.opmodal(IFACE_("Move"), op->type, SCULPT_EXPAND_MODAL_MOVE_TOGGLE, expand_cache->move);
2476 status.opmodal(
2477 IFACE_("Preserve"), op->type, SCULPT_EXPAND_MODAL_PRESERVE_TOGGLE, expand_cache->preserve);
2478
2479 if (expand_cache->target != TargetType::FaceSets) {
2480 status.opmodal(IFACE_("Falloff Gradient"),
2481 op->type,
2483 expand_cache->falloff_gradient);
2484 status.opmodal(IFACE_("Brush Gradient"),
2485 op->type,
2487 expand_cache->brush_gradient);
2488 }
2489
2490 if (ELEM(expand_cache->falloff_type,
2495 {
2496 status.item(IFACE_("Falloff:"), 0);
2497 status.opmodal(IFACE_("Geodesic"),
2498 op->type,
2500 expand_cache->falloff_type == FalloffType::Geodesic);
2501 status.opmodal(IFACE_("Topology"),
2502 op->type,
2504 expand_cache->falloff_type == FalloffType::Topology);
2505 status.opmodal(IFACE_("Diagonals"),
2506 op->type,
2509 status.opmodal(IFACE_("Spherical"),
2510 op->type,
2512 expand_cache->falloff_type == FalloffType::Sphere);
2513 }
2514
2516 status.item("/", 0);
2517 status.separator(-1.2f);
2518 status.opmodal(IFACE_("Loop Count"), op->type, SCULPT_EXPAND_MODAL_LOOP_COUNT_DECREASE);
2519
2520 status.opmodal(IFACE_("Geodesic Step"), op->type, SCULPT_EXPAND_MODAL_RECURSION_STEP_GEODESIC);
2521 status.opmodal(IFACE_("Topology Step"), op->type, SCULPT_EXPAND_MODAL_RECURSION_STEP_TOPOLOGY);
2522
2523 if (expand_cache->brush) {
2524 const MTex *mask_tex = BKE_brush_mask_texture_get(expand_cache->brush, PaintMode::Sculpt);
2525 if (mask_tex->tex) {
2527 status.opmodal(
2528 IFACE_("Texture Distortion"), op->type, SCULPT_EXPAND_MODAL_TEXTURE_DISTORTION_DECREASE);
2529 }
2530 }
2531}
2532
2534{
2537
2538 /* Skips INBETWEEN_MOUSEMOVE events and other events that may cause unnecessary updates. */
2539 if (!ELEM(event->type, MOUSEMOVE, EVT_MODAL_MAP)) {
2541 }
2542
2543 /* Update SculptSession data. */
2544 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
2545 BKE_sculptsession_update_for_edit(depsgraph, &ob, false);
2547
2548 /* Update and get the active vertex (and face) from the cursor. */
2549 const float mval_fl[2] = {float(event->mval[0]), float(event->mval[1])};
2550 const std::optional<int> target_expand_vertex = target_vert_update_and_get(C, ob, mval_fl);
2551
2552 /* Handle the modal keymap state changes. */
2553 Cache &expand_cache = *ss.expand_cache;
2554 if (event->type == EVT_MODAL_MAP) {
2555 switch (event->val) {
2558 return OPERATOR_FINISHED;
2559 }
2561 expand_cache.invert = !expand_cache.invert;
2562 break;
2563 }
2565 expand_cache.preserve = !expand_cache.preserve;
2566 break;
2567 }
2569 expand_cache.falloff_gradient = !expand_cache.falloff_gradient;
2570 break;
2571 }
2573 if (!expand_cache.brush) {
2574 break;
2575 }
2576 expand_cache.brush_gradient = !expand_cache.brush_gradient;
2577 if (expand_cache.brush_gradient) {
2578 expand_cache.falloff_gradient = true;
2579 }
2580 break;
2581 }
2583 if (expand_cache.snap) {
2584 expand_cache.snap = false;
2585 if (expand_cache.snap_enabled_face_sets) {
2586 expand_cache.snap_enabled_face_sets.reset();
2587 }
2588 }
2589 else {
2590 expand_cache.snap = true;
2591 expand_cache.snap_enabled_face_sets = std::make_unique<Set<int>>();
2592 snap_init_from_enabled(*depsgraph, ob, expand_cache);
2593 }
2594 break;
2595 }
2597 if (expand_cache.move) {
2598 expand_cache.move = false;
2600 expand_cache,
2601 ob,
2602 expand_cache.initial_active_vert,
2603 expand_cache.move_original_falloff_type);
2604 break;
2605 }
2606 expand_cache.move = true;
2607 expand_cache.move_original_falloff_type = expand_cache.falloff_type;
2608 copy_v2_v2(expand_cache.initial_mouse_move, mval_fl);
2609 copy_v2_v2(expand_cache.original_mouse_move, expand_cache.initial_mouse);
2610 if (expand_cache.falloff_type == FalloffType::Geodesic &&
2612 {
2613 /* Set to spherical falloff for preview in high poly meshes as it is the fastest one.
2614 * In most cases it should match closely the preview from geodesic. */
2616 }
2617 else {
2618 expand_cache.move_preview_falloff_type = expand_cache.falloff_type;
2619 }
2620 break;
2621 }
2623 resursion_step_add(*depsgraph, ob, expand_cache, RecursionType::Geodesic);
2624 break;
2625 }
2627 resursion_step_add(*depsgraph, ob, expand_cache, RecursionType::Topology);
2628 break;
2629 }
2631 update_for_vert(C, ob, target_expand_vertex);
2632
2633 if (expand_cache.reposition_pivot) {
2634 reposition_pivot(C, ob, expand_cache);
2635 }
2636
2637 finish(C);
2638 return OPERATOR_FINISHED;
2639 }
2642
2644 *depsgraph, expand_cache, ob, expand_cache.initial_active_vert, FalloffType::Geodesic);
2645 break;
2646 }
2649
2651 *depsgraph, expand_cache, ob, expand_cache.initial_active_vert, FalloffType::Topology);
2652 break;
2653 }
2656
2658 expand_cache,
2659 ob,
2660 expand_cache.initial_active_vert,
2662 break;
2663 }
2665 expand_cache.check_islands = false;
2667 *depsgraph, expand_cache, ob, expand_cache.initial_active_vert, FalloffType::Sphere);
2668 break;
2669 }
2671 expand_cache.loop_count += 1;
2672 break;
2673 }
2675 expand_cache.loop_count -= 1;
2676 expand_cache.loop_count = max_ii(expand_cache.loop_count, 1);
2677 break;
2678 }
2680 if (!expand_cache.brush) {
2681 break;
2682 }
2683 if (expand_cache.texture_distortion_strength == 0.0f) {
2684 const MTex *mask_tex = BKE_brush_mask_texture_get(expand_cache.brush, PaintMode::Sculpt);
2685 if (mask_tex->tex == nullptr) {
2686 BKE_report(op->reports,
2688 "Active brush does not contain any texture to distort the expand boundary");
2689 break;
2690 }
2691 if (mask_tex->brush_map_mode != MTEX_MAP_MODE_3D) {
2692 BKE_report(op->reports,
2694 "Texture mapping not set to 3D, results may be unpredictable");
2695 }
2696 }
2698 break;
2699 }
2703 0.0f);
2704 break;
2705 }
2706 }
2707 }
2708
2709 /* Handle expand origin movement if enabled. */
2710 if (expand_cache.move) {
2711 move_propagation_origin(C, ob, event, expand_cache);
2712 }
2713
2714 /* Add new face set IDs to the snapping set if enabled. */
2715 if (expand_cache.snap) {
2716 const int active_face_set_id = active_face_set_id_get(ob, expand_cache);
2717 /* The key may exist, in that case this does nothing. */
2718 expand_cache.snap_enabled_face_sets->add(active_face_set_id);
2719 }
2720
2721 /* Update the sculpt data with the current state of the #Cache. */
2722 update_for_vert(C, ob, target_expand_vertex);
2723
2724 sculpt_expand_status(C, op, &expand_cache);
2725
2727}
2728
2736 int *r_face_sets, Object &object, Cache &expand_cache, Mesh *mesh, const int delete_id)
2737{
2738 const GroupedSpan<int> vert_to_face_map = mesh->vert_to_face_map();
2739 const OffsetIndices faces = mesh->faces();
2740 const Span<int> corner_verts = mesh->corner_verts();
2741
2742 /* Check that all the face set IDs in the mesh are not equal to `delete_id`
2743 * before attempting to delete it. */
2744 bool all_same_id = true;
2745 for (const int i : faces.index_range()) {
2746 if (!is_face_in_active_component(object, faces, corner_verts, expand_cache, i)) {
2747 continue;
2748 }
2749 if (r_face_sets[i] != delete_id) {
2750 all_same_id = false;
2751 break;
2752 }
2753 }
2754 if (all_same_id) {
2755 return;
2756 }
2757
2758 BLI_LINKSTACK_DECLARE(queue, void *);
2759 BLI_LINKSTACK_DECLARE(queue_next, void *);
2760
2761 BLI_LINKSTACK_INIT(queue);
2762 BLI_LINKSTACK_INIT(queue_next);
2763
2764 for (const int i : faces.index_range()) {
2765 if (r_face_sets[i] == delete_id) {
2767 }
2768 }
2769
2770 while (BLI_LINKSTACK_SIZE(queue)) {
2771 bool any_updated = false;
2772 while (BLI_LINKSTACK_SIZE(queue)) {
2773 const int f_index = POINTER_AS_INT(BLI_LINKSTACK_POP(queue));
2774 int other_id = delete_id;
2775 for (const int vert : corner_verts.slice(faces[f_index])) {
2776 for (const int neighbor_face_index : vert_to_face_map[vert]) {
2777 if (expand_cache.original_face_sets[neighbor_face_index] <= 0) {
2778 /* Skip picking IDs from hidden face sets. */
2779 continue;
2780 }
2781 if (r_face_sets[neighbor_face_index] != delete_id) {
2782 other_id = r_face_sets[neighbor_face_index];
2783 }
2784 }
2785 }
2786
2787 if (other_id != delete_id) {
2788 any_updated = true;
2789 r_face_sets[f_index] = other_id;
2790 }
2791 else {
2792 BLI_LINKSTACK_PUSH(queue_next, POINTER_FROM_INT(f_index));
2793 }
2794 }
2795 if (!any_updated) {
2796 /* No face sets were updated in this iteration, which means that no more content to keep
2797 * filling the faces of the deleted face set was found. Break to avoid entering an infinite
2798 * loop trying to search for those faces again. */
2799 break;
2800 }
2801
2802 BLI_LINKSTACK_SWAP(queue, queue_next);
2803 }
2804
2805 BLI_LINKSTACK_FREE(queue);
2806 BLI_LINKSTACK_FREE(queue_next);
2807}
2808
2809static void cache_initial_config_set(bContext *C, wmOperator *op, Cache &expand_cache)
2810{
2811 expand_cache.normal_falloff_blur_steps = RNA_int_get(op->ptr, "normal_falloff_smooth");
2812 expand_cache.invert = RNA_boolean_get(op->ptr, "invert");
2813 expand_cache.preserve = RNA_boolean_get(op->ptr, "use_mask_preserve");
2814 expand_cache.falloff_gradient = RNA_boolean_get(op->ptr, "use_falloff_gradient");
2815 expand_cache.target = TargetType(RNA_enum_get(op->ptr, "target"));
2816 expand_cache.modify_active_face_set = RNA_boolean_get(op->ptr, "use_modify_active");
2817 expand_cache.reposition_pivot = RNA_boolean_get(op->ptr, "use_reposition_pivot");
2818 expand_cache.max_geodesic_move_preview = RNA_int_get(op->ptr, "max_geodesic_move_preview");
2819
2820 /* These can be exposed in RNA if needed. */
2821 expand_cache.loop_count = 1;
2822 expand_cache.brush_gradient = false;
2823
2824 /* Texture and color data from the active Brush. */
2826 const Sculpt &sd = *CTX_data_tool_settings(C)->sculpt;
2827 expand_cache.paint = paint;
2828 expand_cache.brush = BKE_paint_brush_for_read(&sd.paint);
2829
2830 if (expand_cache.brush) {
2832 copy_v3_v3(expand_cache.fill_color, BKE_brush_color_get(paint, expand_cache.brush));
2833 expand_cache.fill_color[3] = 1.0f;
2834 expand_cache.blend_mode = expand_cache.brush->blend;
2835 }
2836 else {
2837 copy_v4_fl(expand_cache.fill_color, 1.0f);
2838 expand_cache.blend_mode = IMB_BLEND_MIX;
2839 }
2840
2841 expand_cache.scene = CTX_data_scene(C);
2842 expand_cache.texture_distortion_strength = 0.0f;
2843}
2844
2848static void undo_push(const Depsgraph &depsgraph, Object &ob, Cache &expand_cache)
2849{
2850 IndexMaskMemory memory;
2851 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(*bke::object::pbvh_get(ob), memory);
2852
2853 switch (expand_cache.target) {
2854 case TargetType::Mask:
2855 undo::push_nodes(depsgraph, ob, node_mask, undo::Type::Mask);
2856 break;
2858 undo::push_nodes(depsgraph, ob, node_mask, undo::Type::FaceSet);
2859 break;
2860 case TargetType::Colors: {
2861 undo::push_nodes(depsgraph, ob, node_mask, undo::Type::Color);
2862 break;
2863 }
2864 }
2865}
2866
2867static bool any_nonzero_mask(const Object &object)
2868{
2869 const SculptSession &ss = *object.runtime->sculpt_session;
2870 switch (bke::object::pbvh_get(object)->type()) {
2871 case bke::pbvh::Type::Mesh: {
2872 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
2873 const bke::AttributeAccessor attributes = mesh.attributes();
2874 const VArraySpan mask = *attributes.lookup<float>(".sculpt_mask");
2875 if (mask.is_empty()) {
2876 return false;
2877 }
2878 return std::any_of(
2879 mask.begin(), mask.end(), [&](const float value) { return value > 0.0f; });
2880 }
2882 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
2883 const Span<float> mask = subdiv_ccg.masks;
2884 if (mask.is_empty()) {
2885 return false;
2886 }
2887 return std::any_of(
2888 mask.begin(), mask.end(), [&](const float value) { return value > 0.0f; });
2889 }
2891 BMesh &bm = *ss.bm;
2892 const int offset = CustomData_get_offset_named(&bm.vdata, CD_PROP_FLOAT, ".sculpt_mask");
2893 if (offset == -1) {
2894 return false;
2895 }
2896 BMIter iter;
2897 BMVert *vert;
2898 BM_ITER_MESH (vert, &iter, &bm, BM_VERTS_OF_MESH) {
2899 if (BM_ELEM_CD_GET_FLOAT(vert, offset) > 0.0f) {
2900 return true;
2901 }
2902 }
2903 return false;
2904 }
2905 }
2906 return false;
2907}
2908
2910{
2911 const Scene &scene = *CTX_data_scene(C);
2912 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2915 Mesh *mesh = id_cast<Mesh *>(ob.data);
2916
2917 const View3D *v3d = CTX_wm_view3d(C);
2918 const Base *base = CTX_data_active_base(C);
2919 if (!BKE_base_is_visible(v3d, base)) {
2920 return OPERATOR_CANCELLED;
2921 }
2922
2923 /* Create and configure the Expand Cache. */
2924 ss.expand_cache = MEM_new<Cache>(__func__);
2926
2927 /* Update object. */
2928 const bool needs_colors = ss.expand_cache->target == TargetType::Colors;
2929
2930 if (needs_colors) {
2931 /* CTX_data_ensure_evaluated_depsgraph should be used at the end to include the updates of
2932 * earlier steps modifying the data. */
2935 }
2936
2938 Scene &scene = *CTX_data_scene(C);
2940 BKE_sculpt_mask_layers_ensure(depsgraph, CTX_data_main(C), &ob, mmd);
2941
2942 if (RNA_boolean_get(op->ptr, "use_auto_mask")) {
2943 if (any_nonzero_mask(ob)) {
2945 }
2946 }
2947 }
2948
2949 BKE_sculptsession_update_for_edit(depsgraph, &ob, needs_colors);
2950
2953 }
2954 else if (ss.expand_cache->target == TargetType::FaceSets) {
2956 }
2957
2958 /* Do nothing when the mesh has 0 vertices. */
2959 const int totvert = vertex_count_get(ob);
2960 if (totvert == 0) {
2962 return OPERATOR_CANCELLED;
2963 }
2965
2966 /* Face set operations are not supported in dyntopo. */
2969 return OPERATOR_CANCELLED;
2970 }
2971
2973
2974 /* Set the initial element for expand from the event position. */
2975 const float mouse[2] = {float(event->mval[0]), float(event->mval[1])};
2976
2977 /* When getting the initial active vert, in cases where the cursor is not over the mesh and
2978 * the mesh type has changed, we cannot proceed with the expand operator, as there is no
2979 * sensible last active vertex when switching between backing implementations. */
2980 if (!set_initial_components_for_mouse(C, ob, *ss.expand_cache, mouse)) {
2982 return OPERATOR_CANCELLED;
2983 }
2984
2985 /* Initialize undo. */
2986 undo::push_begin(scene, ob, op);
2987 undo_push(*depsgraph, ob, *ss.expand_cache);
2988
2989 /* Cache bke::pbvh::Tree nodes. */
2991
2992 /* Store initial state. */
2994
2997 ob,
2998 *ss.expand_cache,
2999 mesh,
3001 }
3002
3003 const int initial_vert = ss.expand_cache->initial_active_vert;
3004
3005 /* Initialize the falloff. */
3006 FalloffType falloff_type = FalloffType(RNA_enum_get(op->ptr, "falloff_type"));
3007
3008 /* When starting from a boundary vertex, set the initial falloff to boundary. */
3009 switch (pbvh.type()) {
3010 case bke::pbvh::Type::Mesh: {
3011 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
3012 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
3013 const bke::AttributeAccessor attributes = mesh.attributes();
3014 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
3016 vert_to_face_map, hide_poly, ss.boundary_info_cache->verts, initial_vert))
3017 {
3018 falloff_type = FalloffType::BoundaryTopology;
3019 }
3020 break;
3021 }
3023 const Mesh &base_mesh = *id_cast<const Mesh *>(ob.data);
3024 const OffsetIndices<int> faces = base_mesh.faces();
3025 const Span<int> corner_verts = base_mesh.corner_verts();
3026 const bke::AttributeAccessor attributes = base_mesh.attributes();
3027 const VArraySpan face_sets = *attributes.lookup_or_default<int>(
3028 ".sculpt_face_set", bke::AttrDomain::Face, 0);
3029 const SubdivCCG &subdiv_ccg = *ob.runtime->sculpt_session->subdiv_ccg;
3030 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
3032 corner_verts,
3033 ss.boundary_info_cache->verts,
3034 ss.boundary_info_cache->edges,
3035 subdiv_ccg,
3036 SubdivCCGCoord::from_index(key, initial_vert)))
3037 {
3038 falloff_type = FalloffType::BoundaryTopology;
3039 }
3040 break;
3041 }
3043 BMesh &bm = *ob.runtime->sculpt_session->bm;
3045 if (boundary::vert_is_boundary(BM_vert_at_index(&bm, initial_vert))) {
3046 falloff_type = FalloffType::BoundaryTopology;
3047 }
3048 break;
3049 }
3050 }
3051
3053 *depsgraph, *ss.expand_cache, ob, initial_vert, falloff_type);
3054
3055 check_topology_islands(ob, falloff_type);
3056
3057 /* Initial mesh data update, resets all target data in the sculpt mesh. */
3058 update_for_vert(C, ob, initial_vert);
3059
3061
3064}
3065
3067{
3068 static const EnumPropertyItem modal_items[] = {
3069 {SCULPT_EXPAND_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
3070 {SCULPT_EXPAND_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
3071 {SCULPT_EXPAND_MODAL_INVERT, "INVERT", 0, "Invert", ""},
3072 {SCULPT_EXPAND_MODAL_PRESERVE_TOGGLE, "PRESERVE", 0, "Toggle Preserve State", ""},
3073 {SCULPT_EXPAND_MODAL_GRADIENT_TOGGLE, "GRADIENT", 0, "Toggle Gradient", ""},
3075 "RECURSION_STEP_GEODESIC",
3076 0,
3077 "Geodesic recursion step",
3078 ""},
3080 "RECURSION_STEP_TOPOLOGY",
3081 0,
3082 "Topology recursion Step",
3083 ""},
3084 {SCULPT_EXPAND_MODAL_MOVE_TOGGLE, "MOVE_TOGGLE", 0, "Move Origin", ""},
3085 {SCULPT_EXPAND_MODAL_FALLOFF_GEODESIC, "FALLOFF_GEODESICS", 0, "Geodesic Falloff", ""},
3086 {SCULPT_EXPAND_MODAL_FALLOFF_TOPOLOGY, "FALLOFF_TOPOLOGY", 0, "Topology Falloff", ""},
3088 "FALLOFF_TOPOLOGY_DIAGONALS",
3089 0,
3090 "Diagonals Falloff",
3091 ""},
3092 {SCULPT_EXPAND_MODAL_FALLOFF_SPHERICAL, "FALLOFF_SPHERICAL", 0, "Spherical Falloff", ""},
3093 {SCULPT_EXPAND_MODAL_SNAP_TOGGLE, "SNAP_TOGGLE", 0, "Snap expand to Face Sets", ""},
3095 "LOOP_COUNT_INCREASE",
3096 0,
3097 "Loop Count Increase",
3098 ""},
3100 "LOOP_COUNT_DECREASE",
3101 0,
3102 "Loop Count Decrease",
3103 ""},
3105 "BRUSH_GRADIENT_TOGGLE",
3106 0,
3107 "Toggle Brush Gradient",
3108 ""},
3110 "TEXTURE_DISTORTION_INCREASE",
3111 0,
3112 "Texture Distortion Increase",
3113 ""},
3115 "TEXTURE_DISTORTION_DECREASE",
3116 0,
3117 "Texture Distortion Decrease",
3118 ""},
3119 {0, nullptr, 0, nullptr, nullptr},
3120 };
3121
3122 static const char *name = "Sculpt Expand Modal";
3123 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, name);
3124
3125 /* This function is called for each space-type, only needs to add map once. */
3126 if (keymap && keymap->modal_items) {
3127 return;
3128 }
3129
3130 keymap = WM_modalkeymap_ensure(keyconf, name, modal_items);
3131 WM_modalkeymap_assign(keymap, "SCULPT_OT_expand");
3132}
3133
3135{
3136 ot->name = "Expand";
3137 ot->idname = "SCULPT_OT_expand";
3138 ot->description = "Generic sculpt expand operator";
3139
3143 ot->poll = sculpt_mode_poll;
3144
3146
3147 static EnumPropertyItem prop_sculpt_expand_falloff_type_items[] = {
3148 {int(FalloffType::Geodesic), "GEODESIC", 0, "Geodesic", ""},
3149 {int(FalloffType::Topology), "TOPOLOGY", 0, "Topology", ""},
3150 {int(FalloffType::TopologyNormals), "TOPOLOGY_DIAGONALS", 0, "Topology Diagonals", ""},
3151 {int(FalloffType::Normals), "NORMALS", 0, "Normals", ""},
3152 {int(FalloffType::Sphere), "SPHERICAL", 0, "Spherical", ""},
3153 {int(FalloffType::BoundaryTopology), "BOUNDARY_TOPOLOGY", 0, "Boundary Topology", ""},
3154 {int(FalloffType::BoundaryFaceSet), "BOUNDARY_FACE_SET", 0, "Boundary Face Set", ""},
3155 {int(FalloffType::ActiveFaceSet), "ACTIVE_FACE_SET", 0, "Active Face Set", ""},
3156 {0, nullptr, 0, nullptr, nullptr},
3157 };
3158
3159 static EnumPropertyItem prop_sculpt_expand_target_type_items[] = {
3160 {int(TargetType::Mask), "MASK", 0, "Mask", ""},
3161 {int(TargetType::FaceSets), "FACE_SETS", 0, "Face Sets", ""},
3162 {int(TargetType::Colors), "COLOR", 0, "Color", ""},
3163 {0, nullptr, 0, nullptr, nullptr},
3164 };
3165
3166 RNA_def_enum(ot->srna,
3167 "target",
3168 prop_sculpt_expand_target_type_items,
3169 int(TargetType::Mask),
3170 "Data Target",
3171 "Data that is going to be modified in the expand operation");
3172
3173 RNA_def_enum(ot->srna,
3174 "falloff_type",
3175 prop_sculpt_expand_falloff_type_items,
3177 "Falloff Type",
3178 "Initial falloff of the expand operation");
3179
3180 ot->prop = RNA_def_boolean(
3181 ot->srna, "invert", false, "Invert", "Invert the expand active elements");
3182 ot->prop = RNA_def_boolean(ot->srna,
3183 "use_mask_preserve",
3184 false,
3185 "Preserve Previous",
3186 "Preserve the previous state of the target data");
3187 ot->prop = RNA_def_boolean(ot->srna,
3188 "use_falloff_gradient",
3189 false,
3190 "Falloff Gradient",
3191 "Expand Using a linear falloff");
3192
3193 ot->prop = RNA_def_boolean(ot->srna,
3194 "use_modify_active",
3195 false,
3196 "Modify Active",
3197 "Modify the active face set instead of creating a new one");
3198
3199 ot->prop = RNA_def_boolean(
3200 ot->srna,
3201 "use_reposition_pivot",
3202 true,
3203 "Reposition Pivot",
3204 "Reposition the sculpt transform pivot to the boundary of the expand active area");
3205
3206 ot->prop = RNA_def_int(ot->srna,
3207 "max_geodesic_move_preview",
3208 10000,
3209 0,
3210 INT_MAX,
3211 "Max Vertex Count for Geodesic Move Preview",
3212 "Maximum number of vertices in the mesh for using geodesic falloff when "
3213 "moving the origin of expand. If the total number of vertices is greater "
3214 "than this value, the falloff will be set to spherical when moving",
3215 0,
3216 1000000);
3217 ot->prop = RNA_def_boolean(ot->srna,
3218 "use_auto_mask",
3219 false,
3220 "Auto Create",
3221 "Fill in mask if nothing is already masked");
3222 ot->prop = RNA_def_int(ot->srna,
3223 "normal_falloff_smooth",
3224 2,
3225 0,
3226 10,
3227 "Normal Smooth",
3228 "Blurring steps for normal falloff",
3229 0,
3230 10);
3231}
3232
3233} // namespace ed::sculpt_paint::expand
3234
3235} // namespace blender
#define PAINT_SYMM_AREAS
Definition BKE_paint.hh:109
A BVH for high poly meshes.
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_SELECT
Definition WM_types.hh:551
#define BLI_assert_unreachable()
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define ELEM(...)
#define BM_ELEM_CD_SET_FLOAT(ele, offset, f)
#define BM_ELEM_CD_GET_FLOAT(ele, offset)
#define BM_elem_index_get(ele)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_MESH(ele, iter, bm, itype)
btScalar max
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T * data() const
Definition BLI_array.hh:323
void fill(const T &value) const
Definition BLI_array.hh:283
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
bool is_empty() const
Definition BLI_array.hh:275
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
static IndexMask from_bits(BitSpan bits, LinearAllocator<> &memory)
static IndexMask from_indices(Span< T > indices, LinearAllocator<> &memory)
constexpr IndexRange()=default
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
static VArray from_span(Span< T > values)
int64_t size() const
void append(const T &value)
IndexRange index_range() const
Span< T > as_span() const
void separator(float factor=1.0f)
Definition area.cc:1083
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
void opmodal(std::string text, const wmOperatorType *ot, int propvalue, bool inverted=false)
Definition area.cc:1107
void fill(const bool value)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
constexpr bool is_empty() const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define powf(x, y)
#define FLT_MAX
#define INT_MAX
#define position
#define vertex
float fade(float t)
float average(float2 a)
ccl_device_inline float2 fmod(const float2 a, const float b)
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void invert(BitSpanT &&data)
IndexRange grid_range(const int grid_area, const int grid)
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)
GroupedSpan< int > build_vert_to_edge_map(Span< int2 > edges, int verts_num, Array< int > &r_offsets, Array< int > &r_indices)
pbvh::Tree * pbvh_get(Object &object)
void update_mask_bmesh(const BMesh &bm, const IndexMask &node_mask, Tree &pbvh)
Definition pbvh.cc:1421
void update_mask_mesh(const Mesh &mesh, const IndexMask &node_mask, Tree &pbvh)
Definition pbvh.cc:1356
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
void node_update_mask_bmesh(int mask_offset, BMeshNode &node)
Definition pbvh.cc:1404
void node_update_mask_grids(const CCGKey &key, Span< float > masks, GridsNode &node)
Definition pbvh.cc:1373
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
void update_mask_grids(const SubdivCCG &subdiv_ccg, const IndexMask &node_mask, Tree &pbvh)
Definition pbvh.cc:1387
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
std::unique_ptr< SculptBoundary > data_init(const Depsgraph &depsgraph, Object &object, const Brush *brush, const int initial_vert, const float radius)
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
bke::GSpanAttributeWriter active_color_attribute_for_write(Mesh &mesh)
float4 color_vert_get(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, GSpan color_attribute, bke::AttrDomain color_domain, int vert)
void color_vert_set(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, bke::AttrDomain color_domain, int vert, const float4 &color, GMutableSpan color_attribute)
bke::GAttributeReader active_color_attribute(const Mesh &mesh)
static Array< float > geodesic_falloff_create(const Depsgraph &depsgraph, Object &ob, const IndexMask &initial_verts)
static float calc_new_mask_bmesh(const SculptSession &ss, const Cache &expand_cache, const float old_mask, const BitSpan enabled_verts, const BMVert *vert)
static void calc_falloff_from_vert_and_symmetry(const Depsgraph &depsgraph, Cache &expand_cache, Object &ob, const int vert, FalloffType falloff_type)
static bool any_nonzero_mask(const Object &object)
static void ensure_sculptsession_data(Object &ob)
static void vert_to_face_falloff_mesh(Mesh *mesh, Cache &expand_cache)
static Array< float > spherical_falloff_create(const Depsgraph &depsgraph, const Object &object, const int vert)
static wmOperatorStatus sculpt_expand_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void calc_topology_falloff_from_verts(Object &ob, const IndexMask &initial_verts, MutableSpan< float > distances)
static void geodesics_from_state_boundary(const Depsgraph &depsgraph, Object &ob, Cache &expand_cache, const BitSpan enabled_verts)
static void undo_push(const Depsgraph &depsgraph, Object &ob, Cache &expand_cache)
static void topology_from_state_boundary(Object &ob, Cache &expand_cache, const BitSpan enabled_verts)
static void move_propagation_origin(bContext *C, Object &ob, const wmEvent *event, Cache &expand_cache)
static void check_topology_islands(Object &ob, FalloffType falloff_type)
static BitVector enabled_state_to_bitmap(const Depsgraph &depsgraph, const Object &object, const Cache &expand_cache)
static float falloff_value_vertex_get(const SculptSession &ss, const Cache &expand_cache, const float3 &position, const int vert)
static void sculpt_expand_status(bContext *C, wmOperator *op, Cache *expand_cache)
static wmOperatorStatus sculpt_expand_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void original_state_store(Object &ob, Cache &expand_cache)
static void finish(bContext *C)
static void update_max_vert_falloff_value(const Object &object, Cache &expand_cache)
static void vert_to_face_falloff(Object &object, Mesh *mesh, Cache &expand_cache)
void modal_keymap(wmKeyConfig *keyconf)
static bool update_mask_grids(const SculptSession &ss, const BitSpan enabled_verts, bke::pbvh::GridsNode &node, SubdivCCG &subdiv_ccg)
static void init_from_face_set_boundary(const Depsgraph &depsgraph, Object &ob, Cache &expand_cache, const int active_face_set, const bool internal_falloff)
static std::optional< int > target_vert_update_and_get(bContext *C, Object &ob, const float mval[2])
static void restore_color_data(Object &ob, Cache &expand_cache)
static bool face_state_get(const Object &object, const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< bool > hide_poly, const Span< int > face_sets, const Cache &expand_cache, const int face)
static bool is_vert_in_active_component(const SculptSession &ss, const Cache &expand_cache, const int vert)
static void expand_cache_free(SculptSession &ss)
static Array< float > normals_falloff_create(const Depsgraph &depsgraph, Object &ob, const int vert, const float edge_sensitivity, const int blur_steps)
static void delete_face_set_id(int *r_face_sets, Object &object, Cache &expand_cache, Mesh *mesh, const int delete_id)
static void reposition_pivot(bContext *C, Object &ob, Cache &expand_cache)
static void update_for_vert(bContext *C, Object &ob, const std::optional< int > vertex)
static void face_sets_restore(Object &object, Cache &expand_cache)
static void calc_new_mask_mesh(const SculptSession &ss, const Span< float3 > positions, const BitSpan enabled_verts, const Span< int > verts, const MutableSpan< float > mask)
static void face_sets_update(Object &object, Cache &expand_cache)
static void find_active_connected_components_from_vert(const Depsgraph &depsgraph, Object &ob, Cache &expand_cache, const int initial_vertex)
void SCULPT_OT_expand(wmOperatorType *ot)
static void update_max_face_falloff_factor(const Object &object, Mesh &mesh, Cache &expand_cache)
static void vert_to_face_falloff_grids(SculptSession &ss, Mesh *mesh, Cache &expand_cache)
static bool is_face_in_active_component(const Object &object, const OffsetIndices< int > faces, const Span< int > corner_verts, const Cache &expand_cache, const int f)
static Array< float > diagonals_falloff_create(const Depsgraph &depsgraph, Object &ob, const int vert)
static void restore_original_state(bContext *C, Object &ob, Cache &expand_cache)
static Array< float > topology_falloff_create(const Depsgraph &depsgraph, Object &ob, const int initial_vert)
static void resursion_step_add(const Depsgraph &depsgraph, Object &ob, Cache &expand_cache, const RecursionType recursion_type)
static IndexMask boundary_from_enabled(Object &object, const BitSpan enabled_verts, const bool use_mesh_boundary, IndexMaskMemory &memory)
static void cache_initial_config_set(bContext *C, wmOperator *op, Cache &expand_cache)
static Array< float > boundary_topology_falloff_create(const Depsgraph &depsgraph, Object &ob, const int initial_vert)
static float gradient_value_get(const SculptSession &ss, const Cache &expand_cache, const float3 &position, const int vert)
static bool vert_falloff_is_enabled(const SculptSession &ss, const Cache &expand_cache, const float3 &position, const int vert)
static bool colors_update_task(const Depsgraph &depsgraph, Object &object, const Span< float3 > vert_positions, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const Span< bool > hide_vert, const Span< float > mask, bke::pbvh::MeshNode *node, bke::GSpanAttributeWriter &color_attribute)
static void restore_face_set_data(Object &object, Cache &expand_cache)
static void sculpt_expand_cancel(bContext *C, wmOperator *)
static void snap_init_from_enabled(const Depsgraph &depsgraph, const Object &object, Cache &expand_cache)
static int active_face_set_id_get(Object &object, Cache &expand_cache)
static bool update_mask_bmesh(SculptSession &ss, const BitSpan enabled_verts, const int mask_offset, const Span< float > old_mask, bke::pbvh::BMeshNode *node)
static float max_vert_falloff_get(const Cache &expand_cache)
static void write_mask_data(Object &object, const Span< float > mask)
static bool set_initial_components_for_mouse(bContext *C, Object &ob, Cache &expand_cache, const float mval[2])
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 coord_has_face_set(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 SubdivCCGCoord coord, const int face_set)
Definition sculpt.cc:410
bool vert_has_any_face_set(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, int vert, const Set< int > &allowed_face_sets)
Definition sculpt.cc:308
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
Array< int > duplicate_face_sets(const Mesh &mesh)
bke::SpanAttributeWriter< int > ensure_face_sets_mesh(Mesh &mesh)
bool coord_has_any_face_set(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 SubdivCCGCoord coord, const Set< int > &allowed_face_sets)
Definition sculpt.cc:432
int find_next_available_id(const Mesh &mesh)
int active_face_set_get(const Object &object)
Definition sculpt.cc:202
Array< float > distances_create(const Span< float3 > vert_positions, const Span< int2 > edges, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_edge_map, const GroupedSpan< int > edge_to_face_map, const Span< bool > hide_poly, const Set< int > &initial_verts, const float limit_radius)
void ensure_cache(Object &object)
Definition sculpt.cc:6448
int vert_id_get(const SculptSession &ss, const int vert)
Definition sculpt.cc:6310
void update_mask_mesh(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask, FunctionRef< void(MutableSpan< float >, Span< int >)> update_fn)
Array< float > duplicate_mask(const Object &object)
Definition paint_mask.cc:56
void clamp_mask(const MutableSpan< float > masks)
void blur_geometry_data_array(const Object &object, const int iterations, const MutableSpan< float > data)
void push_nodes(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask, const Type type)
void push_begin(const Scene &scene, Object &ob, const wmOperator *op)
std::array< int, PAINT_SYMM_AREAS > find_all_symm_verts_bmesh(const Object &object, const int original_vert, const float max_distance)
void face_set_overlay_check(bContext &C, wmOperator &op)
Definition sculpt.cc:8320
bool check_vertex_pivot_symmetry(const float vco[3], const float pco[3], const char symm)
Definition sculpt.cc:672
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
std::optional< int > nearest_vert_calc_mesh(const bke::pbvh::Tree &pbvh, const Span< float3 > vert_positions, const Span< bool > hide_vert, const float3 &location, const float max_distance, const bool use_original)
Definition sculpt.cc:730
Vector< int > find_symm_verts_grids(const Object &object, const int original_vert, const float max_distance)
Vector< BMVert *, 64 > BMeshNeighborVerts
int vertex_count_get(const Object &object)
Definition sculpt.cc:163
float3 symmetry_flip(const float3 &src, const ePaintSymmetryFlags symm)
ePaintSymmetryFlags mesh_symmetry_xyz_get(const Object &object)
Definition sculpt.cc:192
Vector< int > find_symm_verts_mesh(const Depsgraph &depsgraph, const Object &object, const int original_vert, const float max_distance)
bool sculpt_mode_poll(bContext *C)
Definition sculpt.cc:3800
std::array< int, PAINT_SYMM_AREAS > find_all_symm_verts_grids(const Object &object, const int original_vert, const float max_distance)
Span< BMVert * > vert_neighbors_get_bmesh(BMVert &vert, BMeshNeighborVerts &r_neighbors)
Definition sculpt.cc:490
void flush_update_step(bContext *C, const UpdateType update_type)
Definition sculpt.cc:5090
void flush_update_done(bContext *C, Object &ob, const UpdateType update_type)
Definition sculpt.cc:5131
std::optional< BMVert * > nearest_vert_calc_bmesh(const bke::pbvh::Tree &pbvh, const float3 &location, const float max_distance, const bool use_original)
Definition sculpt.cc:831
std::optional< CursorGeometryInfo > cursor_geometry_info_update(bContext *C, const float2 &mval, const bool use_sampled_normal)
Definition sculpt.cc:4584
std::optional< SubdivCCGCoord > nearest_vert_calc_grids(const bke::pbvh::Tree &pbvh, const SubdivCCG &subdiv_ccg, const float3 &location, const float max_distance, const bool use_original)
Definition sculpt.cc:777
Vector< int > find_symm_verts(const Depsgraph &depsgraph, const Object &object, const int original_vert, const float max_distance)
Span< int > vert_neighbors_get_mesh(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face, const Span< bool > hide_poly, const int vert, Vector< int > &r_neighbors)
Definition sculpt.cc:534
void mask_overlay_check(bContext &C, wmOperator &op)
Definition sculpt.cc:8298
std::array< int, PAINT_SYMM_AREAS > find_all_symm_verts_mesh(const Depsgraph &depsgraph, const Object &object, const int original_vert, const float max_distance)
void tag_update_overlays(bContext *C)
Definition sculpt.cc:895
std::array< int, PAINT_SYMM_AREAS > find_all_symm_verts(const Depsgraph &depsgraph, const Object &object, const int original_vert, const float max_distance)
Vector< int > find_symm_verts_bmesh(const Object &object, const int original_vert, const float max_distance)
bool is_symmetry_iteration_valid(const char i, const char symm)
constexpr ParallelGrainSize grain_size(int grain_size)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
void parallel_for_aligned(const IndexRange range, const int64_t grain_size, const int64_t alignment, const Function &function)
Definition BLI_task.hh:115
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BMesh Iterators.
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int RNA_enum_get(PointerRNA *ptr, const char *name)
@ RPT_WARNING
Definition BKE_report.hh:40
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
MultiresModifierData * BKE_sculpt_multires_active(const Scene *scene, Object *ob)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
float BKE_brush_sample_tex_3d(const Paint *paint, const Brush *br, const MTex *mtex, const float3 &point, float4 &rgba, int thread, ImagePool *pool)
Definition brush.cc:985
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)
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void mul_v4_v4fl(float r[4], const float a[4], float f)
@ IMB_BLEND_MIX
Definition IMB_imbuf.hh:204
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
int RNA_int_get(PointerRNA *ptr, const char *name)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE void copy_v4_fl(float r[4], float f)
MINLINE int max_ii(int a, int b)
void BKE_subdiv_ccg_average_grids(SubdivCCG &subdiv_ccg)
Base * CTX_data_active_base(const bContext *C)
bool BM_vert_is_boundary(const BMVert *v)
void BKE_sculpt_mask_layers_ensure(Depsgraph *depsgraph, Main *bmain, Object *ob, MultiresModifierData *mmd)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
VecBase< float, 4 > float4
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
void BKE_curvemapping_init(CurveMapping *cumap)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
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)
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1691
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_other_verts(bke::pbvh::BMeshNode *node)
void IMB_blend_color_float(float dst[4], const float src1[4], const float src2[4], IMB_BlendMode mode)
Definition rectop.cc:118
ToolSettings * CTX_data_tool_settings(const bContext *C)
VecBase< double, 3 > double3
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
const MTex * BKE_brush_mask_texture_get(const Brush *brush, PaintMode paint_mode)
Definition brush.cc:969
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BKE_sculpt_color_layer_create_if_needed(Object *object)
int BKE_subdiv_ccg_grid_to_face_index(const SubdivCCG &subdiv_ccg, const int grid_index)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
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)
Main * CTX_data_main(const bContext *C)
Paint * BKE_paint_get_active_from_context(const bContext *C)
MINLINE float max_ff(float a, float b)
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
void BKE_subdiv_ccg_foreach_visible_grid_vert(const CCGKey &key, const BitGroupVector<> &grid_hidden, const int grid, const Fn &fn)
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
Scene * CTX_data_scene(const bContext *C)
float3 BKE_brush_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1253
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
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)
Object * CTX_data_active_object(const bContext *C)
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
View3D * CTX_wm_view3d(const bContext *C)
MINLINE float clamp_f(float value, float min, float max)
#define fabsf
#define SCULPT_EXPAND_LOOP_THRESHOLD
#define SCULPT_EXPAND_TEXTURE_DISTORTION_STEP
#define EXPAND_ACTIVE_COMPONENT_NONE
#define SCULPT_EXPAND_NORMALS_FALLOFF_EDGE_SENSITIVITY
#define EXPAND_SYMM_AREAS
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMVert * v
CustomData pdata
CustomData vdata
BMFace ** ftable
struct CurveMapping * curve_distance_falloff
eMTex_BrushMapMode brush_map_mode
bke::ObjectRuntime * runtime
std::unique_ptr< SculptBoundaryInfoCache > boundary_info_cache
Definition BKE_paint.hh:458
std::optional< int > active_face_index
Definition BKE_paint.hh:414
std::optional< int > active_grid_index
Definition BKE_paint.hh:415
ed::sculpt_paint::expand::Cache * expand_cache
Definition BKE_paint.hh:411
static SubdivCCGCoord from_index(const CCGKey &key, int index)
int to_index(const CCGKey &key) const
SubdivCCGNeighborCoords coords
Array< float3 > positions
Array< float3 > normals
Array< float > masks
std::unique_ptr< Set< int > > snap_enabled_face_sets
int active_connected_islands[EXPAND_SYMM_AREAS]
void execute(Object &object, FunctionRef< bool(BMVert *from_v, BMVert *to_v)> func)
void execute(Object &object, const SubdivCCG &subdiv_ccg, FunctionRef< bool(SubdivCCGCoord from_v, SubdivCCGCoord to_v, bool is_duplicate)> func)
void add_and_skip_initial(SubdivCCGCoord vertex, int index)
void execute(Object &object, GroupedSpan< int > vert_to_face_map, FunctionRef< bool(int from_v, int to_v)> func)
wmEventType type
Definition WM_types.hh:817
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
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
struct ReportList * reports
struct wmOperatorType * type