Blender V5.3
sculpt_filter_mesh.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include "sculpt_filter.hh" // TODO- Move this function's initialization to a separate file
9
10#include <fmt/format.h>
11
13
14#include "MEM_guardedalloc.h"
15
17#include "BLI_hash_c.hh"
18#include "BLI_index_range.hh"
19#include "BLI_math_base.hh"
20#include "BLI_math_matrix_c.hh"
21#include "BLI_math_vector.hh"
22#include "BLI_math_vector_c.hh"
24
25#include "BLT_translation.hh"
26
27#include "BKE_brush.hh"
28#include "BKE_context.hh"
29#include "BKE_layer.hh"
30#include "BKE_mesh.hh"
31#include "BKE_modifier.hh"
32#include "BKE_object_types.hh"
33#include "BKE_paint.hh"
34#include "BKE_paint_bvh.hh"
35#include "BKE_paint_types.hh"
36
37#include "WM_api.hh"
38#include "WM_types.hh"
39
40#include "ED_screen.hh"
41#include "ED_sculpt.hh"
42#include "ED_view3d.hh"
43
44#include "../paint_intern.hh"
45#include "mesh_brush_common.hh"
46#include "sculpt_automask.hh"
47#include "sculpt_boundary.hh"
48#include "sculpt_cloth.hh"
49#include "sculpt_face_set.hh"
50#include "sculpt_intern.hh"
51#include "sculpt_smooth.hh"
52#include "sculpt_undo.hh"
53
54#include "RNA_access.hh"
55#include "RNA_define.hh"
56#include "RNA_prototypes.hh"
57
59#include "UI_resources.hh"
60
61#include "bmesh.hh"
62
63#include <algorithm>
64#include <cmath>
65#include <cstdlib>
66
68
70{
71 switch (filter_cache.orientation) {
73 return float3x3::identity();
75 return float3x3(filter_cache.obmat);
77 return float3x3(filter_cache.obmat * filter_cache.viewmat);
78 }
80 return float3x3::identity();
81}
82
84{
85 switch (filter_cache.orientation) {
87 return float3x3::identity();
89 return float3x3(filter_cache.obmat_inv);
91 return float3x3(filter_cache.viewmat_inv * filter_cache.obmat_inv);
92 }
94 return float3x3::identity();
95}
96
98 const MutableSpan<float3> vectors)
99{
100 if (filter_cache.enabled_axis[0] && filter_cache.enabled_axis[1] && filter_cache.enabled_axis[2])
101 {
102 return;
103 }
104
105 if (filter_cache.orientation == FilterOrientation::Local) {
106 for (const int i : vectors.index_range()) {
107 for (int axis = 0; axis < 3; axis++) {
108 if (!filter_cache.enabled_axis[axis]) {
109 vectors[i][axis] = 0.0f;
110 }
111 }
112 }
113 }
114
115 const float3x3 local_to_orientation = to_orientation_space(filter_cache);
116 const float3x3 orientation_to_object = to_object_space(filter_cache);
117 for (const int i : vectors.index_range()) {
118 float3 vector = local_to_orientation * vectors[i];
119 for (int axis = 0; axis < 3; axis++) {
120 if (!filter_cache.enabled_axis[axis]) {
121 vector[axis] = 0.0f;
122 }
123 }
124 vectors[i] = orientation_to_object * vector;
125 }
126}
127
128Cache::~Cache() = default;
129
131 Object &ob,
132 Sculpt &sd,
133 const undo::Type undo_type,
134 const float mval_fl[2],
135 float area_normal_radius,
136 float start_strength)
137{
139 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
141
142 ss.filter_cache = MEM_new<filter::Cache>(__func__);
143 ss.filter_cache->start_filter_strength = start_strength;
144 ss.filter_cache->has_dragged = false;
145 ss.filter_cache->random_seed = rand();
146
149 return !node_fully_masked_or_hidden(node);
150 });
151
152 undo::push_nodes(*depsgraph, ob, ss.filter_cache->node_mask, undo_type);
153
154 /* Setup orientation matrices. */
155 copy_m4_m4(ss.filter_cache->obmat.ptr(), ob.object_to_world().ptr());
156 invert_m4_m4(ss.filter_cache->obmat_inv.ptr(), ob.object_to_world().ptr());
157
159
160 ss.filter_cache->vc = vc;
161 if (vc.rv3d) {
164 }
165
167
168 std::optional<float3> hit_location;
169 if (vc.rv3d) {
170 hit_location = stroke_get_location_bvh(C, mval_fl, false);
171 }
172 if (hit_location) {
173 /* Get radius from brush. */
175
176 float radius;
177 if (brush) {
178 radius = object_space_radius_get(vc, sd.paint, *brush, *hit_location, area_normal_radius);
179 }
180 else {
182 vc, *hit_location, float(ups->size / 2.0f) * area_normal_radius);
183 }
184
185 const float radius_sq = math::square(radius);
186
187 IndexMaskMemory memory;
188 const IndexMask node_mask = bke::pbvh::search_nodes(
189 pbvh, memory, [&](const bke::pbvh::Node &node) {
191 node_in_sphere(node, *hit_location, radius_sq, true);
192 });
193
194 const std::optional<float3> area_normal = calc_area_normal(*depsgraph, *brush, ob, node_mask);
195 if (BKE_paint_brush_for_read(&sd.paint) && area_normal) {
196 ss.filter_cache->initial_normal = *area_normal;
198 }
199 else {
201 }
202
203 /* Update last stroke location */
204
205 mul_m4_v3(ob.object_to_world().ptr(), *hit_location);
206
207 bke::paint::stroke_track_location(sd.paint, *hit_location);
208 }
209 else {
210 /* Use last normal. */
212 }
213
214 /* Update view normal */
215 float3x3 mat;
216 float3 viewDir{0.0f, 0.0f, 1.0f};
217 if (vc.rv3d) {
218 invert_m4_m4(ob.runtime->world_to_object.ptr(), ob.object_to_world().ptr());
219 copy_m3_m4(mat.ptr(), vc.rv3d->viewinv);
220 mul_m3_v3(mat.ptr(), viewDir);
221 copy_m3_m4(mat.ptr(), ob.world_to_object().ptr());
222 mul_m3_v3(mat.ptr(), viewDir);
224 }
225}
226
227enum class MeshFilterType {
228 Smooth = 0,
229 Scale = 1,
230 Inflate = 2,
232 Random = 4,
233 Relax = 5,
236 Sharpen = 8,
239};
240
242 {int(MeshFilterType::Smooth), "SMOOTH", 0, "Smooth", "Smooth mesh"},
243 {int(MeshFilterType::Scale), "SCALE", 0, "Scale", "Scale mesh"},
244 {int(MeshFilterType::Inflate), "INFLATE", 0, "Inflate", "Inflate mesh"},
245 {int(MeshFilterType::Sphere), "SPHERE", 0, "Sphere", "Morph into sphere"},
246 {int(MeshFilterType::Random), "RANDOM", 0, "Random", "Randomize vertex positions"},
247 {int(MeshFilterType::Relax), "RELAX", 0, "Relax", "Relax mesh"},
249 "RELAX_FACE_SETS",
250 0,
251 "Relax Face Sets",
252 "Smooth the edges of all the face sets"},
254 "SURFACE_SMOOTH",
255 0,
256 "Surface Smooth",
257 "Smooth the surface of the mesh, preserving the volume"},
258 {int(MeshFilterType::Sharpen), "SHARPEN", 0, "Sharpen", "Sharpen the cavities of the mesh"},
260 "ENHANCE_DETAILS",
261 0,
262 "Enhance Details",
263 "Enhance the high frequency surface detail"},
265 "ERASE_DISPLACEMENT",
266 0,
267 "Erase Displacement",
268 "Deletes the displacement of the Multires Modifier"},
269 {0, nullptr, 0, nullptr, nullptr},
270};
271
277
279 {MESH_FILTER_DEFORM_X, "X", 0, "X", "Deform in the X axis"},
280 {MESH_FILTER_DEFORM_Y, "Y", 0, "Y", "Deform in the Y axis"},
281 {MESH_FILTER_DEFORM_Z, "Z", 0, "Z", "Deform in the Z axis"},
282 {0, nullptr, 0, nullptr, nullptr},
283};
284
287 "LOCAL",
288 0,
289 "Local",
290 "Use the local axis to limit the displacement"},
292 "WORLD",
293 0,
294 "World",
295 "Use the global axis to limit the displacement"},
297 "VIEW",
298 0,
299 "View",
300 "Use the view axis to limit the displacement"},
301 {0, nullptr, 0, nullptr, nullptr},
302};
303
314
323
325 const float min,
326 const float max)
327{
328 for (float &factor : factors) {
329 factor = std::clamp(factor, min, max);
330 }
331}
332
333static void calc_smooth_filter(const Depsgraph &depsgraph,
334 const Sculpt &sd,
335 const float strength,
336 Object &object,
337 const IndexMask &node_mask,
338 const bool use_original_position)
339{
340 struct LocalData {
341 Vector<float> factors;
342 Vector<float3> positions;
343 Vector<int> neighbor_offsets;
344 Vector<int> neighbor_data;
345 Vector<float3> new_positions;
346 Vector<float3> translations;
347 };
348 SculptSession &ss = *object.runtime->sculpt_session;
350 switch (pbvh.type()) {
352 Mesh &mesh = *id_cast<Mesh *>(object.data);
353 const MeshAttributeData attribute_data(mesh);
354 const PositionDeformData position_data(depsgraph, object);
355 const OffsetIndices faces = mesh.faces();
356 const Span<int> corner_verts = mesh.corner_verts();
357 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
360 node_mask.foreach_index(
361 [&](const int i) {
362 LocalData &tls = all_tls.local();
363 const Span<int> verts = nodes[i].verts();
365 gather_data_mesh(position_data.eval, verts, positions.as_mutable_span());
366 const OrigPositionData orig_data = orig_position_data_get_mesh(object, nodes[i]);
367
370 attribute_data.hide_vert, attribute_data.mask, verts, factors);
372 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
373 scale_factors(factors, strength);
374 clamp_factors(factors, -1.0f, 1.0f);
375
377 faces,
378 corner_verts,
379 vert_to_face_map,
380 ss.boundary_info_cache->verts,
381 ss.boundary_info_cache->edges,
382 attribute_data.hide_poly,
383 verts,
384 tls.neighbor_offsets,
385 tls.neighbor_data);
386
387 Array<float3, bke::pbvh::MESH_LEAF_LIMIT> new_positions(verts.size());
389 position_data.eval, verts, neighbors, new_positions.as_mutable_span());
390
392 if (use_original_position) {
393 translations_from_new_positions(new_positions, orig_data.positions, translations);
394 }
395 else {
396 translations_from_new_positions(new_positions, positions, translations);
397 }
398 scale_translations(translations, factors);
399 if (use_original_position) {
400 reset_translations_to_original(translations, positions, orig_data.positions);
401 }
402
404 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
405 position_data.deform(translations, verts);
406 },
408 break;
409 }
411 const Mesh &base_mesh = *id_cast<const Mesh *>(object.data);
412 const OffsetIndices faces = base_mesh.faces();
413 const Span<int> corner_verts = base_mesh.corner_verts();
414
415 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
418 node_mask.foreach_index(
419 [&](const int i) {
420 LocalData &tls = all_tls.local();
421 const Span<int> grids = nodes[i].grids();
422 const Span<float3> positions = gather_grids_positions(
423 subdiv_ccg, grids, tls.positions);
424 const OrigPositionData orig_data = orig_position_data_get_grids(object, nodes[i]);
425
426 tls.factors.resize(positions.size());
427 const MutableSpan<float> factors = tls.factors;
428 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
430 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
431 scale_factors(factors, strength);
432 clamp_factors(factors, -1.0f, 1.0f);
433
434 tls.new_positions.resize(positions.size());
435 const MutableSpan<float3> new_positions = tls.new_positions;
437 corner_verts,
438 ss.boundary_info_cache->verts,
439 ss.boundary_info_cache->edges,
440 subdiv_ccg,
441 grids,
442 new_positions);
443
444 tls.translations.resize(positions.size());
445 const MutableSpan<float3> translations = tls.translations;
446 if (use_original_position) {
447 translations_from_new_positions(new_positions, orig_data.positions, translations);
448 }
449 else {
450 translations_from_new_positions(new_positions, positions, translations);
451 }
452 scale_translations(translations, factors);
453 if (use_original_position) {
454 reset_translations_to_original(translations, positions, orig_data.positions);
455 }
456
458 clip_and_lock_translations(sd, ss, positions, translations);
459 apply_translations(translations, grids, subdiv_ccg);
460 },
462 break;
463 }
465 BMesh &bm = *ss.bm;
468 node_mask.foreach_index(
469 [&](const int i) {
470 LocalData &tls = all_tls.local();
472 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
473 Array<float3> orig_positions(verts.size());
474 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, {});
475
476 tls.factors.resize(verts.size());
477 const MutableSpan<float> factors = tls.factors;
478 fill_factor_from_hide_and_mask(bm, verts, factors);
480 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
481 scale_factors(factors, strength);
482 clamp_factors(factors, -1.0f, 1.0f);
483
484 tls.new_positions.resize(verts.size());
485 const MutableSpan<float3> new_positions = tls.new_positions;
487
488 tls.translations.resize(verts.size());
489 const MutableSpan<float3> translations = tls.translations;
490 if (use_original_position) {
491 translations_from_new_positions(new_positions, orig_positions, translations);
492 }
493 else {
494 translations_from_new_positions(new_positions, positions, translations);
495 }
496 scale_translations(translations, factors);
497 if (use_original_position) {
498 reset_translations_to_original(translations, positions, orig_positions);
499 }
500
502 clip_and_lock_translations(sd, ss, positions, translations);
503 apply_translations(translations, verts);
504 },
506 break;
507 }
508 }
509}
510
511static void calc_inflate_filter(const Depsgraph &depsgraph,
512 const Sculpt &sd,
513 const float strength,
514 Object &object,
515 const IndexMask &node_mask)
516{
517 struct LocalData {
518 Vector<float> factors;
519 Vector<float3> positions;
520 Vector<float3> translations;
521 };
522 SculptSession &ss = *object.runtime->sculpt_session;
524 switch (pbvh.type()) {
526 Mesh &mesh = *id_cast<Mesh *>(object.data);
527 bke::AttributeAccessor attributes = mesh.attributes();
528 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
529 const VArraySpan mask = *attributes.lookup<float>(".sculpt_mask", bke::AttrDomain::Point);
530 const PositionDeformData position_data(depsgraph, object);
533 node_mask.foreach_index(
534 [&](const int i) {
535 LocalData &tls = all_tls.local();
536 const Span<int> verts = nodes[i].verts();
537 const OrigPositionData orig_data = orig_position_data_get_mesh(object, nodes[i]);
538
539 tls.factors.resize(verts.size());
540 const MutableSpan<float> factors = tls.factors;
541 fill_factor_from_hide_and_mask(hide_vert, mask, verts, factors);
543 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
544 scale_factors(factors, strength);
545
546 tls.translations.resize(verts.size());
547 const MutableSpan<float3> translations = tls.translations;
548 translations.copy_from(orig_data.normals);
549 scale_translations(translations, factors);
550
552 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
553 position_data.deform(translations, verts);
554 },
556 break;
557 }
559 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
562 node_mask.foreach_index(
563 [&](const int i) {
564 LocalData &tls = all_tls.local();
565 const Span<int> grids = nodes[i].grids();
566 const Span<float3> positions = gather_grids_positions(
567 subdiv_ccg, grids, tls.positions);
568 const OrigPositionData orig_data = orig_position_data_get_grids(object, nodes[i]);
569
570 tls.factors.resize(positions.size());
571 const MutableSpan<float> factors = tls.factors;
572 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
574 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
575 scale_factors(factors, strength);
576
577 tls.translations.resize(positions.size());
578 const MutableSpan<float3> translations = tls.translations;
579 translations.copy_from(orig_data.normals);
580 scale_translations(translations, factors);
581
583 clip_and_lock_translations(sd, ss, positions, translations);
584 apply_translations(translations, grids, subdiv_ccg);
585 },
587 break;
588 }
590 BMesh &bm = *ss.bm;
593 node_mask.foreach_index(
594 [&](const int i) {
595 LocalData &tls = all_tls.local();
597 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
598 Array<float3> orig_positions(verts.size());
599 Array<float3> orig_normals(verts.size());
600 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
601
602 tls.factors.resize(verts.size());
603 const MutableSpan<float> factors = tls.factors;
604 fill_factor_from_hide_and_mask(bm, verts, factors);
606 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
607 scale_factors(factors, strength);
608
609 tls.translations.resize(verts.size());
610 const MutableSpan<float3> translations = tls.translations;
611 translations.copy_from(orig_normals);
612 scale_translations(translations, factors);
613
615 clip_and_lock_translations(sd, ss, positions, translations);
616 apply_translations(translations, verts);
617 },
619 break;
620 }
621 }
622}
623
624static void calc_scale_filter(const Depsgraph &depsgraph,
625 const Sculpt &sd,
626 const float strength,
627 Object &object,
628 const IndexMask &node_mask)
629{
630 struct LocalData {
631 Vector<float> factors;
632 Vector<float3> positions;
633 Vector<float3> translations;
634 };
635 SculptSession &ss = *object.runtime->sculpt_session;
637 switch (pbvh.type()) {
639 Mesh &mesh = *id_cast<Mesh *>(object.data);
640 const MeshAttributeData attribute_data(mesh);
641 const PositionDeformData position_data(depsgraph, object);
644 node_mask.foreach_index(
645 [&](const int i) {
646 LocalData &tls = all_tls.local();
647 const Span<int> verts = nodes[i].verts();
648 const OrigPositionData orig_data = orig_position_data_get_mesh(object, nodes[i]);
649
650 tls.factors.resize(verts.size());
651 const MutableSpan<float> factors = tls.factors;
653 attribute_data.hide_vert, attribute_data.mask, verts, factors);
655 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
656 scale_factors(factors, strength);
657
658 tls.translations.resize(verts.size());
659 const MutableSpan<float3> translations = tls.translations;
660 translations.copy_from(orig_data.positions);
661 scale_translations(translations, factors);
662
664 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
665 position_data.deform(translations, verts);
666 },
668 break;
669 }
671 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
674 node_mask.foreach_index(
675 [&](const int i) {
676 LocalData &tls = all_tls.local();
677 const Span<int> grids = nodes[i].grids();
678 const Span<float3> positions = gather_grids_positions(
679 subdiv_ccg, grids, tls.positions);
680 const OrigPositionData orig_data = orig_position_data_get_grids(object, nodes[i]);
681
682 tls.factors.resize(positions.size());
683 const MutableSpan<float> factors = tls.factors;
684 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
686 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
687 scale_factors(factors, strength);
688
689 tls.translations.resize(positions.size());
690 const MutableSpan<float3> translations = tls.translations;
691 translations.copy_from(orig_data.positions);
692 scale_translations(translations, factors);
693
695 clip_and_lock_translations(sd, ss, positions, translations);
696 apply_translations(translations, grids, subdiv_ccg);
697 },
699 break;
700 }
702 BMesh &bm = *ss.bm;
705 node_mask.foreach_index(
706 [&](const int i) {
707 LocalData &tls = all_tls.local();
709 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
710 Array<float3> orig_positions(verts.size());
711 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, {});
712
713 tls.factors.resize(verts.size());
714 const MutableSpan<float> factors = tls.factors;
715 fill_factor_from_hide_and_mask(bm, verts, factors);
717 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
718 scale_factors(factors, strength);
719
720 tls.translations.resize(positions.size());
721 const MutableSpan<float3> translations = tls.translations;
722 translations.copy_from(orig_positions);
723 scale_translations(translations, factors);
724
726 clip_and_lock_translations(sd, ss, positions, translations);
727 apply_translations(translations, verts);
728 },
730 break;
731 }
732 }
733}
734
736 const Span<float> factors,
737 const MutableSpan<float3> translations)
738{
739 for (const int i : positions.index_range()) {
740 translations[i] = math::midpoint(math::normalize(positions[i]), -positions[i]) *
741 math::abs(factors[i]);
742 }
743}
744
745static void calc_sphere_filter(const Depsgraph &depsgraph,
746 const Sculpt &sd,
747 const float strength,
748 Object &object,
749 const IndexMask &node_mask)
750{
751 struct LocalData {
752 Vector<float> factors;
753 Vector<float3> positions;
754 Vector<float3> translations;
755 };
756 SculptSession &ss = *object.runtime->sculpt_session;
758 switch (pbvh.type()) {
760 Mesh &mesh = *id_cast<Mesh *>(object.data);
761 const PositionDeformData position_data(depsgraph, object);
762 const MeshAttributeData attribute_data(mesh);
765 node_mask.foreach_index(
766 [&](const int i) {
767 LocalData &tls = all_tls.local();
768 const Span<int> verts = nodes[i].verts();
769 const Span<float3> positions = gather_data_mesh(
770 position_data.eval, verts, tls.positions);
771
772 tls.factors.resize(verts.size());
773 const MutableSpan<float> factors = tls.factors;
775 attribute_data.hide_vert, attribute_data.mask, verts, factors);
777 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
778 scale_factors(factors, strength);
779
780 tls.translations.resize(verts.size());
781 const MutableSpan<float3> translations = tls.translations;
782 calc_sphere_translations(positions, factors, translations);
783
785 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
786 position_data.deform(translations, verts);
787 },
789 break;
790 }
792 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
795 node_mask.foreach_index(
796 [&](const int i) {
797 LocalData &tls = all_tls.local();
798 const Span<int> grids = nodes[i].grids();
799 const Span<float3> positions = gather_grids_positions(
800 subdiv_ccg, grids, tls.positions);
801
802 tls.factors.resize(positions.size());
803 const MutableSpan<float> factors = tls.factors;
804 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
806 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
807 scale_factors(factors, strength);
808
809 tls.translations.resize(positions.size());
810 const MutableSpan<float3> translations = tls.translations;
811 calc_sphere_translations(positions, factors, translations);
812
814 clip_and_lock_translations(sd, ss, positions, translations);
815 apply_translations(translations, grids, subdiv_ccg);
816 },
818 break;
819 }
821 BMesh &bm = *ss.bm;
824 node_mask.foreach_index(
825 [&](const int i) {
826 LocalData &tls = all_tls.local();
828 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
829
830 tls.factors.resize(verts.size());
831 const MutableSpan<float> factors = tls.factors;
832 fill_factor_from_hide_and_mask(bm, verts, factors);
834 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
835 scale_factors(factors, strength);
836
837 tls.translations.resize(verts.size());
838 const MutableSpan<float3> translations = tls.translations;
839 calc_sphere_translations(positions, factors, translations);
840
842 clip_and_lock_translations(sd, ss, positions, translations);
843 apply_translations(translations, verts);
844 },
846 break;
847 }
848 }
849}
850
851BLI_NOINLINE static void randomize_factors(const Span<float3> positions,
852 const int seed,
853 const MutableSpan<float> factors)
854{
855 BLI_assert(positions.size() == factors.size());
856 for (const int i : positions.index_range()) {
857 const uint *hash_co = reinterpret_cast<const uint *>(&positions[i]);
858 const uint hash = BLI_hash_int_2d(hash_co[0], hash_co[1]) ^ BLI_hash_int_2d(hash_co[2], seed);
859 factors[i] *= (hash * (1.0f / float(0xFFFFFFFF)) - 0.5f);
860 }
861}
862
863static void calc_random_filter(const Depsgraph &depsgraph,
864 const Sculpt &sd,
865 const float strength,
866 Object &object,
867 const IndexMask &node_mask)
868{
869 struct LocalData {
870 Vector<float> factors;
871 Vector<float3> positions;
872 Vector<float3> translations;
873 };
874 SculptSession &ss = *object.runtime->sculpt_session;
876 switch (pbvh.type()) {
878 Mesh &mesh = *id_cast<Mesh *>(object.data);
879 const MeshAttributeData attribute_data(mesh);
880 const PositionDeformData position_data(depsgraph, object);
883 node_mask.foreach_index(
884 [&](const int i) {
885 LocalData &tls = all_tls.local();
886 const Span<int> verts = nodes[i].verts();
887 const OrigPositionData orig_data = orig_position_data_get_mesh(object, nodes[i]);
888 const Span<float3> positions = gather_data_mesh(
889 position_data.eval, verts, tls.positions);
890
891 tls.factors.resize(verts.size());
892 const MutableSpan<float> factors = tls.factors;
894 attribute_data.hide_vert, attribute_data.mask, verts, factors);
896 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
897 scale_factors(factors, strength);
898
899 randomize_factors(positions, ss.filter_cache->random_seed, factors);
900 tls.translations.resize(verts.size());
901 const MutableSpan<float3> translations = tls.translations;
902 translations.copy_from(orig_data.normals);
903 scale_translations(translations, factors);
904
906 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
907 position_data.deform(translations, verts);
908 },
910 break;
911 }
913 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
916 node_mask.foreach_index(
917 [&](const int i) {
918 LocalData &tls = all_tls.local();
919 const Span<int> grids = nodes[i].grids();
920 const Span<float3> positions = gather_grids_positions(
921 subdiv_ccg, grids, tls.positions);
922 const OrigPositionData orig_data = orig_position_data_get_grids(object, nodes[i]);
923
924 tls.factors.resize(positions.size());
925 const MutableSpan<float> factors = tls.factors;
926 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
928 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
929 scale_factors(factors, strength);
930
931 randomize_factors(positions, ss.filter_cache->random_seed, factors);
932 tls.translations.resize(positions.size());
933 const MutableSpan<float3> translations = tls.translations;
934 translations.copy_from(orig_data.normals);
935 scale_translations(translations, factors);
936
938 clip_and_lock_translations(sd, ss, positions, translations);
939 apply_translations(translations, grids, subdiv_ccg);
940 },
942 break;
943 }
945 BMesh &bm = *ss.bm;
948 node_mask.foreach_index(
949 [&](const int i) {
950 LocalData &tls = all_tls.local();
952 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
953 Array<float3> orig_positions(verts.size());
954 Array<float3> orig_normals(verts.size());
955 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
956
957 tls.factors.resize(verts.size());
958 const MutableSpan<float> factors = tls.factors;
959 fill_factor_from_hide_and_mask(bm, verts, factors);
961 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
962 scale_factors(factors, strength);
963
964 randomize_factors(positions, ss.filter_cache->random_seed, factors);
965 tls.translations.resize(verts.size());
966 const MutableSpan<float3> translations = tls.translations;
967 translations.copy_from(orig_normals);
968 scale_translations(translations, factors);
969
971 clip_and_lock_translations(sd, ss, positions, translations);
972 apply_translations(translations, verts);
973 },
975 break;
976 }
977 }
978}
979
980static void calc_relax_filter(const Depsgraph &depsgraph,
981 const Sculpt &sd,
982 const float strength,
983 Object &object,
984 const IndexMask &node_mask)
985{
986 SculptSession &ss = *object.runtime->sculpt_session;
988 bke::pbvh::update_normals(depsgraph, object, pbvh);
989 switch (pbvh.type()) {
990 struct LocalData {
991 Vector<float> factors;
992 Vector<float3> translations;
993 };
995 Mesh &mesh = *id_cast<Mesh *>(object.data);
996 const PositionDeformData position_data(depsgraph, object);
997 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
998 const OffsetIndices faces = mesh.faces();
999 const Span<int> corner_verts = mesh.corner_verts();
1000 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1001 const MeshAttributeData attribute_data(mesh);
1004 node_mask.foreach_index(
1005 [&](const int i) {
1006 LocalData &tls = all_tls.local();
1007 const Span<int> verts = nodes[i].verts();
1008
1009 tls.factors.resize(verts.size());
1010 const MutableSpan<float> factors = tls.factors;
1012 attribute_data.hide_vert, attribute_data.mask, verts, factors);
1014 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1015 scale_factors(factors, strength);
1016 clamp_factors(factors, 0.0f, 1.0f);
1017
1018 tls.translations.resize(verts.size());
1019 const MutableSpan<float3> translations = tls.translations;
1020 smooth::calc_relaxed_translations_faces(position_data.eval,
1021 vert_normals,
1022 faces,
1023 corner_verts,
1024 vert_to_face_map,
1025 ss.boundary_info_cache->verts,
1026 ss.boundary_info_cache->edges,
1027 attribute_data.face_sets,
1028 attribute_data.hide_poly,
1029 false,
1030 verts,
1031 factors,
1032 translations);
1033
1035 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
1036 position_data.deform(translations, verts);
1037 },
1039 break;
1040 }
1042 struct LocalData {
1043 Vector<float> factors;
1044 Vector<float3> positions;
1045 Vector<float3> translations;
1046 };
1047 const Mesh &base_mesh = *id_cast<const Mesh *>(object.data);
1048 const OffsetIndices faces = base_mesh.faces();
1049 const Span<int> corner_verts = base_mesh.corner_verts();
1050 const GroupedSpan<int> vert_to_face_map = base_mesh.vert_to_face_map();
1051 const bke::AttributeAccessor attributes = base_mesh.attributes();
1052 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set",
1054 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1057 node_mask.foreach_index(
1058 [&](const int i) {
1059 LocalData &tls = all_tls.local();
1060 const Span<int> grids = nodes[i].grids();
1061 const Span<float3> positions = gather_grids_positions(
1062 subdiv_ccg, grids, tls.positions);
1063
1064 tls.factors.resize(positions.size());
1065 const MutableSpan<float> factors = tls.factors;
1066 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
1068 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
1069 scale_factors(factors, strength);
1070 clamp_factors(factors, 0.0f, 1.0f);
1071
1072 tls.translations.resize(positions.size());
1073 const MutableSpan<float3> translations = tls.translations;
1075 faces,
1076 corner_verts,
1077 face_sets,
1078 vert_to_face_map,
1079 ss.boundary_info_cache->verts,
1080 ss.boundary_info_cache->edges,
1081 grids,
1082 false,
1083 factors,
1084 translations);
1085
1087 clip_and_lock_translations(sd, ss, positions, translations);
1088 apply_translations(translations, grids, subdiv_ccg);
1089 },
1091 break;
1092 }
1094 struct LocalData {
1095 Vector<float> factors;
1096 Vector<float3> positions;
1097 Vector<float3> translations;
1098 };
1099 BMesh &bm = *ss.bm;
1100 const int face_set_offset = CustomData_get_offset_named(
1101 &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
1102
1105 node_mask.foreach_index(
1106 [&](const int i) {
1107 LocalData &tls = all_tls.local();
1109 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
1110
1111 tls.factors.resize(verts.size());
1112 const MutableSpan<float> factors = tls.factors;
1113 fill_factor_from_hide_and_mask(bm, verts, factors);
1115 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1116 scale_factors(factors, strength);
1117 clamp_factors(factors, 0.0f, 1.0f);
1118
1119 tls.translations.resize(verts.size());
1120 const MutableSpan<float3> translations = tls.translations;
1122 verts, positions, face_set_offset, false, factors, translations);
1123
1125 clip_and_lock_translations(sd, ss, positions, translations);
1126 apply_translations(translations, verts);
1127 },
1129 break;
1130 }
1131 }
1132}
1133
1134static void calc_relax_face_sets_filter(const Depsgraph &depsgraph,
1135 const Sculpt &sd,
1136 const float strength,
1137 Object &object,
1138 const IndexMask &node_mask)
1139{
1140 SculptSession &ss = *object.runtime->sculpt_session;
1142 bke::pbvh::update_normals(depsgraph, object, pbvh);
1143
1144 /* When using the relax face sets meshes filter, each 3 iterations, do a whole mesh relax to
1145 * smooth the contents of the face set. This produces better results as the relax operation is no
1146 * completely focused on the boundaries. */
1147 const bool relax_face_sets = !(ss.filter_cache->iteration_count % 3 == 0);
1148
1149 switch (pbvh.type()) {
1150 case bke::pbvh::Type::Mesh: {
1151 struct LocalData {
1152 Vector<float> factors;
1153 Vector<float3> positions;
1154 Vector<float3> translations;
1155 };
1156 Mesh &mesh = *id_cast<Mesh *>(object.data);
1157 const PositionDeformData position_data(depsgraph, object);
1158 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
1159 const OffsetIndices faces = mesh.faces();
1160 const Span<int> corner_verts = mesh.corner_verts();
1161 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1162 const MeshAttributeData attribute_data(mesh);
1165 node_mask.foreach_index(
1166 [&](const int i) {
1167 LocalData &tls = all_tls.local();
1168 const Span<int> verts = nodes[i].verts();
1169
1170 tls.factors.resize(verts.size());
1171 const MutableSpan<float> factors = tls.factors;
1173 attribute_data.hide_vert, attribute_data.mask, verts, factors);
1175 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1176 scale_factors(factors, strength);
1177 clamp_factors(factors, 0.0f, 1.0f);
1178
1180 vert_to_face_map, attribute_data.face_sets, relax_face_sets, verts, factors);
1181
1182 tls.translations.resize(verts.size());
1183 const MutableSpan<float3> translations = tls.translations;
1184 smooth::calc_relaxed_translations_faces(position_data.eval,
1185 vert_normals,
1186 faces,
1187 corner_verts,
1188 vert_to_face_map,
1189 ss.boundary_info_cache->verts,
1190 ss.boundary_info_cache->edges,
1191 attribute_data.face_sets,
1192 attribute_data.hide_poly,
1193 relax_face_sets,
1194 verts,
1195 factors,
1196 translations);
1197
1199 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
1200 position_data.deform(translations, verts);
1201 },
1203 break;
1204 }
1206 struct LocalData {
1207 Vector<float> factors;
1208 Vector<float3> positions;
1209 Vector<float3> translations;
1210 };
1211 const Mesh &base_mesh = *id_cast<const Mesh *>(object.data);
1212 const OffsetIndices faces = base_mesh.faces();
1213 const Span<int> corner_verts = base_mesh.corner_verts();
1214 const GroupedSpan<int> vert_to_face_map = base_mesh.vert_to_face_map();
1215 const bke::AttributeAccessor attributes = base_mesh.attributes();
1216 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set",
1218
1219 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1222 node_mask.foreach_index(
1223 [&](const int i) {
1224 LocalData &tls = all_tls.local();
1225 const Span<int> grids = nodes[i].grids();
1226 const Span<float3> positions = gather_grids_positions(
1227 subdiv_ccg, grids, tls.positions);
1228
1229 tls.factors.resize(positions.size());
1230 const MutableSpan<float> factors = tls.factors;
1231 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
1233 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
1234 scale_factors(factors, strength);
1235 clamp_factors(factors, 0.0f, 1.0f);
1236
1238 corner_verts,
1239 vert_to_face_map,
1240 face_sets,
1241 subdiv_ccg,
1242 relax_face_sets,
1243 grids,
1244 factors);
1245
1246 tls.translations.resize(positions.size());
1247 const MutableSpan<float3> translations = tls.translations;
1249 faces,
1250 corner_verts,
1251 face_sets,
1252 vert_to_face_map,
1253 ss.boundary_info_cache->verts,
1254 ss.boundary_info_cache->edges,
1255 grids,
1256 relax_face_sets,
1257 factors,
1258 translations);
1259
1261 clip_and_lock_translations(sd, ss, positions, translations);
1262 apply_translations(translations, grids, subdiv_ccg);
1263 },
1265 break;
1266 }
1268 struct LocalData {
1269 Vector<float> factors;
1270 Vector<float3> positions;
1271 Vector<float3> translations;
1272 };
1273 BMesh &bm = *ss.bm;
1274 const int face_set_offset = CustomData_get_offset_named(
1275 &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
1278 node_mask.foreach_index(
1279 [&](const int i) {
1280 LocalData &tls = all_tls.local();
1282 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
1283
1284 tls.factors.resize(verts.size());
1285 const MutableSpan<float> factors = tls.factors;
1286 fill_factor_from_hide_and_mask(bm, verts, factors);
1288 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1289 scale_factors(factors, strength);
1290 clamp_factors(factors, 0.0f, 1.0f);
1291
1293 face_set_offset, relax_face_sets, verts, factors);
1294
1295 tls.translations.resize(verts.size());
1296 const MutableSpan<float3> translations = tls.translations;
1298 verts, positions, face_set_offset, relax_face_sets, factors, translations);
1299
1301 clip_and_lock_translations(sd, ss, positions, translations);
1302 apply_translations(translations, verts);
1303 },
1305 break;
1306 }
1307 }
1308}
1309
1310static void calc_surface_smooth_filter(const Depsgraph &depsgraph,
1311 const Sculpt &sd,
1312 const float strength,
1313 Object &object,
1314 const IndexMask &node_mask)
1315{
1316 struct LocalData {
1317 Vector<float> factors;
1318 Vector<float3> positions;
1319 Vector<int> neighbor_offsets;
1320 Vector<int> neighbor_data;
1321 Vector<float3> average_positions;
1322 Vector<float3> laplacian_disp;
1323 Vector<float3> translations;
1324 };
1325 SculptSession &ss = *object.runtime->sculpt_session;
1327 const float alpha = ss.filter_cache->surface_smooth_shape_preservation;
1329 const MutableSpan<float3> all_laplacian_disp = ss.filter_cache->surface_smooth_laplacian_disp;
1330 switch (pbvh.type()) {
1331 case bke::pbvh::Type::Mesh: {
1332 Mesh &mesh = *id_cast<Mesh *>(object.data);
1333 const MeshAttributeData attribute_data(mesh);
1334 const PositionDeformData position_data(depsgraph, object);
1335 const OffsetIndices faces = mesh.faces();
1336 const Span<int> corner_verts = mesh.corner_verts();
1337 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1340 node_mask.foreach_index(
1341 [&](const int i) {
1342 LocalData &tls = all_tls.local();
1343 const Span<int> verts = nodes[i].verts();
1344 const Span<float3> positions = gather_data_mesh(
1345 position_data.eval, verts, tls.positions);
1346 const OrigPositionData orig_data = orig_position_data_get_mesh(object, nodes[i]);
1347
1348 tls.factors.resize(verts.size());
1349 const MutableSpan<float> factors = tls.factors;
1351 attribute_data.hide_vert, attribute_data.mask, verts, factors);
1353 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1354 scale_factors(factors, strength);
1355 clamp_factors(factors, 0.0f, 1.0f);
1356
1357 const GroupedSpan<int> neighbors = calc_vert_neighbors(faces,
1358 corner_verts,
1359 vert_to_face_map,
1360 attribute_data.hide_poly,
1361 verts,
1362 tls.neighbor_offsets,
1363 tls.neighbor_data);
1364
1365 tls.average_positions.reinitialize(verts.size());
1366 const MutableSpan<float3> average_positions = tls.average_positions;
1368 position_data.eval, verts, neighbors, average_positions);
1369
1370 tls.laplacian_disp.reinitialize(verts.size());
1371 const MutableSpan<float3> laplacian_disp = tls.laplacian_disp;
1372 tls.translations.reinitialize(verts.size());
1373 const MutableSpan<float3> translations = tls.translations;
1375 orig_data.positions,
1376 average_positions,
1377 alpha,
1378 laplacian_disp,
1379 translations);
1380 scale_translations(translations, factors);
1381
1382 scatter_data_mesh(laplacian_disp.as_span(), verts, all_laplacian_disp);
1383
1385 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
1386 position_data.deform(translations, verts);
1387 },
1389 node_mask.foreach_index(
1390 [&](const int i) {
1391 LocalData &tls = all_tls.local();
1392 const Span<int> verts = nodes[i].verts();
1393
1394 tls.factors.resize(verts.size());
1395 const MutableSpan<float> factors = tls.factors;
1397 attribute_data.hide_vert, attribute_data.mask, verts, factors);
1399 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1400 scale_factors(factors, strength);
1401 clamp_factors(factors, 0.0f, 1.0f);
1402
1403 const MutableSpan<float3> laplacian_disp = gather_data_mesh(
1404 all_laplacian_disp.as_span(), verts, tls.laplacian_disp);
1405
1406 const GroupedSpan<int> neighbors = calc_vert_neighbors(faces,
1407 corner_verts,
1408 vert_to_face_map,
1409 attribute_data.hide_poly,
1410 verts,
1411 tls.neighbor_offsets,
1412 tls.neighbor_data);
1413
1414 tls.average_positions.resize(verts.size());
1415 const MutableSpan<float3> average_laplacian_disps = tls.average_positions;
1417 all_laplacian_disp.as_span(), verts, neighbors, average_laplacian_disps);
1418
1419 tls.translations.resize(verts.size());
1420 const MutableSpan<float3> translations = tls.translations;
1422 laplacian_disp, average_laplacian_disps, beta, translations);
1423 scale_translations(translations, factors);
1424
1426 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
1427 position_data.deform(translations, verts);
1428 },
1430 break;
1431 }
1433 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1436 node_mask.foreach_index(
1437 [&](const int i) {
1438 LocalData &tls = all_tls.local();
1439 const Span<int> grids = nodes[i].grids();
1440 const Span<float3> positions = gather_grids_positions(
1441 subdiv_ccg, grids, tls.positions);
1442 const OrigPositionData orig_data = orig_position_data_get_grids(object, nodes[i]);
1443
1444 tls.factors.resize(positions.size());
1445 const MutableSpan<float> factors = tls.factors;
1446 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
1448 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
1449 scale_factors(factors, strength);
1450 clamp_factors(factors, 0.0f, 1.0f);
1451
1452 tls.average_positions.resize(positions.size());
1453 const MutableSpan<float3> average_positions = tls.average_positions;
1455 subdiv_ccg, subdiv_ccg.positions.as_span(), grids, average_positions);
1456
1457 tls.laplacian_disp.resize(positions.size());
1458 const MutableSpan<float3> laplacian_disp = tls.laplacian_disp;
1459 tls.translations.resize(positions.size());
1460 const MutableSpan<float3> translations = tls.translations;
1462 orig_data.positions,
1463 average_positions,
1464 alpha,
1465 laplacian_disp,
1466 translations);
1467 scale_translations(translations, factors);
1468
1469 scatter_data_grids(subdiv_ccg, laplacian_disp.as_span(), grids, all_laplacian_disp);
1470
1472 clip_and_lock_translations(sd, ss, positions, translations);
1473 apply_translations(translations, grids, subdiv_ccg);
1474 },
1476 node_mask.foreach_index(
1477 [&](const int i) {
1478 LocalData &tls = all_tls.local();
1479 const Span<int> grids = nodes[i].grids();
1480 const Span<float3> positions = gather_grids_positions(
1481 subdiv_ccg, grids, tls.positions);
1482
1483 tls.factors.resize(positions.size());
1484 const MutableSpan<float> factors = tls.factors;
1485 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
1487 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
1488 scale_factors(factors, strength);
1489 clamp_factors(factors, 0.0f, 1.0f);
1490
1491 const MutableSpan<float3> laplacian_disp = gather_data_grids(
1492 subdiv_ccg, all_laplacian_disp.as_span(), grids, tls.laplacian_disp);
1493
1494 tls.average_positions.resize(positions.size());
1495 const MutableSpan<float3> average_laplacian_disps = tls.average_positions;
1497 subdiv_ccg, all_laplacian_disp.as_span(), grids, average_laplacian_disps);
1498
1499 tls.translations.resize(positions.size());
1500 const MutableSpan<float3> translations = tls.translations;
1502 laplacian_disp, average_laplacian_disps, beta, translations);
1503 scale_translations(translations, factors);
1504
1506 clip_and_lock_translations(sd, ss, positions, translations);
1507 apply_translations(translations, grids, subdiv_ccg);
1508 },
1510 break;
1511 }
1513 BMesh &bm = *ss.bm;
1516 node_mask.foreach_index(
1517 [&](const int i) {
1518 LocalData &tls = all_tls.local();
1520 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
1521 Array<float3> orig_positions(verts.size());
1522 Array<float3> orig_normals(verts.size());
1523 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
1524
1525 tls.factors.resize(verts.size());
1526 const MutableSpan<float> factors = tls.factors;
1527 fill_factor_from_hide_and_mask(bm, verts, factors);
1529 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1530 scale_factors(factors, strength);
1531 clamp_factors(factors, 0.0f, 1.0f);
1532
1533 tls.average_positions.resize(verts.size());
1534 const MutableSpan<float3> average_positions = tls.average_positions;
1535 smooth::neighbor_position_average_bmesh(verts, average_positions);
1536
1537 tls.laplacian_disp.resize(verts.size());
1538 const MutableSpan<float3> laplacian_disp = tls.laplacian_disp;
1539 tls.translations.resize(verts.size());
1540 const MutableSpan<float3> translations = tls.translations;
1542 positions, orig_positions, average_positions, alpha, laplacian_disp, translations);
1543 scale_translations(translations, factors);
1544
1545 scatter_data_bmesh(laplacian_disp.as_span(), verts, all_laplacian_disp);
1546
1548 clip_and_lock_translations(sd, ss, positions, translations);
1549 apply_translations(translations, verts);
1550 },
1552 node_mask.foreach_index(
1553 [&](const int i) {
1554 LocalData &tls = all_tls.local();
1556 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
1557
1558 tls.factors.resize(verts.size());
1559 const MutableSpan<float> factors = tls.factors;
1560 fill_factor_from_hide_and_mask(bm, verts, factors);
1562 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1563 scale_factors(factors, strength);
1564 clamp_factors(factors, 0.0f, 1.0f);
1565
1566 const MutableSpan<float3> laplacian_disp = gather_data_bmesh(
1567 all_laplacian_disp.as_span(), verts, tls.laplacian_disp);
1568
1569 tls.average_positions.resize(verts.size());
1570 const MutableSpan<float3> average_laplacian_disps = tls.average_positions;
1572 all_laplacian_disp.as_span(), verts, average_laplacian_disps);
1573
1574 tls.translations.resize(verts.size());
1575 const MutableSpan<float3> translations = tls.translations;
1577 laplacian_disp, average_laplacian_disps, beta, translations);
1578 scale_translations(translations, factors);
1579
1581 clip_and_lock_translations(sd, ss, positions, translations);
1582 apply_translations(translations, verts);
1583 },
1585 break;
1586 }
1587 }
1588}
1589
1591 const Span<float3> positions,
1592 const Span<float3> smooth_positions,
1593 const Span<float> sharpen_factors,
1594 const Span<float3> detail_directions,
1595 const MutableSpan<float3> translations)
1596{
1597 for (const int i : positions.index_range()) {
1598 float3 disp_avg = smooth_positions[i] - positions[i];
1599 disp_avg = disp_avg * filter_cache.sharpen_smooth_ratio * pow2f(sharpen_factors[i]);
1600 translations[i] += disp_avg;
1601 /* Intensify details. */
1602 if (filter_cache.sharpen_intensify_detail_strength > 0.0f) {
1603 float3 detail_strength = detail_directions[i];
1604 translations[i] += detail_strength * -filter_cache.sharpen_intensify_detail_strength *
1605 sharpen_factors[i];
1606 }
1607 }
1608}
1609
1610static void calc_sharpen_filter(const Depsgraph &depsgraph,
1611 const Sculpt &sd,
1612 const float strength,
1613 Object &object,
1614 const IndexMask &node_mask)
1615{
1616 struct LocalData {
1617 Vector<float> factors;
1618 Vector<float3> positions;
1619 Vector<int> neighbor_offsets;
1620 Vector<int> neighbor_data;
1621 Vector<float3> smooth_positions;
1622 Vector<float> sharpen_factors;
1623 Vector<float3> detail_directions;
1624 Vector<float3> translations;
1625 };
1626 SculptSession &ss = *object.runtime->sculpt_session;
1628 switch (pbvh.type()) {
1629 case bke::pbvh::Type::Mesh: {
1630 Mesh &mesh = *id_cast<Mesh *>(object.data);
1631 const MeshAttributeData attribute_data(mesh);
1632 const PositionDeformData position_data(depsgraph, object);
1633
1634 const OffsetIndices faces = mesh.faces();
1635 const Span<int> corner_verts = mesh.corner_verts();
1636 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1639 node_mask.foreach_index(
1640 [&](const int node_index) {
1641 LocalData &tls = all_tls.local();
1642 const Span<int> verts = nodes[node_index].verts();
1643 const Span<float3> positions = gather_data_mesh(
1644 position_data.eval, verts, tls.positions);
1645
1646 tls.factors.resize(verts.size());
1647 const MutableSpan<float> factors = tls.factors;
1649 attribute_data.hide_vert, attribute_data.mask, verts, factors);
1651 object,
1652 ss.filter_cache->automasking.get(),
1653 nodes[node_index],
1654 verts,
1655 factors);
1656 scale_factors(factors, strength);
1657
1658 /* This filter can't work at full strength as it needs multiple iterations to reach a
1659 * stable state. */
1660 clamp_factors(factors, 0.0f, 0.5f);
1661
1662 const GroupedSpan<int> neighbors = calc_vert_neighbors(faces,
1663 corner_verts,
1664 vert_to_face_map,
1665 attribute_data.hide_poly,
1666 verts,
1667 tls.neighbor_offsets,
1668 tls.neighbor_data);
1669
1670 tls.smooth_positions.resize(verts.size());
1671 const MutableSpan<float3> smooth_positions = tls.smooth_positions;
1673 position_data.eval, verts, neighbors, smooth_positions);
1674
1675 const Span<float> sharpen_factors = gather_data_mesh(
1676 ss.filter_cache->sharpen_factor.as_span(), verts, tls.sharpen_factors);
1677
1678 tls.translations.resize(verts.size());
1679 const MutableSpan<float3> translations = tls.translations;
1680 for (const int i : verts.index_range()) {
1681 const int vert = verts[i];
1682 const float3 &position = position_data.eval[vert];
1683
1684 float3 disp_sharpen(0.0f);
1685 for (const int neighbor : neighbors[i]) {
1686 float3 disp_n = position_data.eval[neighbor] - position;
1687 disp_n *= ss.filter_cache->sharpen_factor[neighbor];
1688 disp_sharpen += disp_n;
1689 }
1690
1691 disp_sharpen *= (1.0f - sharpen_factors[i]);
1692 translations[i] = disp_sharpen;
1693 }
1694
1695 const Span<float3> detail_directions = gather_data_mesh(
1696 ss.filter_cache->detail_directions.as_span(), verts, tls.detail_directions);
1697
1699 positions,
1700 smooth_positions,
1701 sharpen_factors,
1702 detail_directions,
1703 translations);
1704 scale_translations(translations, factors);
1705
1707 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
1708 position_data.deform(translations, verts);
1709 },
1711 break;
1712 }
1714 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1715 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1716 MutableSpan<float3> vert_positions = subdiv_ccg.positions;
1717
1720 node_mask.foreach_index(
1721 [&](const int node_index) {
1722 LocalData &tls = all_tls.local();
1723 const Span<int> grids = nodes[node_index].grids();
1724 const Span<float3> positions = gather_grids_positions(
1725 subdiv_ccg, grids, tls.positions);
1726
1727 tls.factors.resize(positions.size());
1728 const MutableSpan<float> factors = tls.factors;
1729 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
1731 object,
1732 ss.filter_cache->automasking.get(),
1733 nodes[node_index],
1734 grids,
1735 factors);
1736 scale_factors(factors, strength);
1737
1738 /* This filter can't work at full strength as it needs multiple iterations to reach a
1739 * stable state. */
1740 clamp_factors(factors, 0.0f, 0.5f);
1741
1742 tls.smooth_positions.resize(positions.size());
1743 const MutableSpan<float3> smooth_positions = tls.smooth_positions;
1745 subdiv_ccg, subdiv_ccg.positions.as_span(), grids, smooth_positions);
1746
1747 const Span<float> sharpen_factors = gather_data_grids(
1748 subdiv_ccg, ss.filter_cache->sharpen_factor.as_span(), grids, tls.sharpen_factors);
1749
1750 tls.translations.resize(positions.size());
1751 const MutableSpan<float3> translations = tls.translations;
1752 for (const int i : grids.index_range()) {
1753 const int node_verts_start = i * key.grid_area;
1754 const int grid = grids[i];
1755 for (const short y : IndexRange(key.grid_size)) {
1756 for (const short x : IndexRange(key.grid_size)) {
1757 const int offset = CCG_grid_xy_to_index(key.grid_size, x, y);
1758 const int node_vert = node_verts_start + offset;
1759
1760 const float3 &position = positions[node_vert];
1761
1762 float3 disp_sharpen(0.0f);
1763 SubdivCCGNeighbors neighbors;
1765 subdiv_ccg, SubdivCCGCoord{grid, x, y}, false, neighbors);
1766 for (const SubdivCCGCoord neighbor : neighbors.coords) {
1767 float3 disp_n = vert_positions[neighbor.to_index(key)] - position;
1768 disp_n *= ss.filter_cache->sharpen_factor[neighbor.to_index(key)];
1769 disp_sharpen += disp_n;
1770 }
1771
1772 disp_sharpen *= (1.0f - sharpen_factors[node_vert]);
1773 translations[node_vert] = disp_sharpen;
1774 }
1775 }
1776 }
1777
1778 const Span<float3> detail_directions = gather_data_grids(
1779 subdiv_ccg,
1781 grids,
1782 tls.detail_directions);
1783
1785 positions,
1786 smooth_positions,
1787 sharpen_factors,
1788 detail_directions,
1789 translations);
1790 scale_translations(translations, factors);
1791
1793 clip_and_lock_translations(sd, ss, positions, translations);
1794 apply_translations(translations, grids, subdiv_ccg);
1795 },
1797 break;
1798 }
1800 BMesh &bm = *ss.bm;
1804 node_mask.foreach_index(
1805 [&](const int node_index) {
1806 LocalData &tls = all_tls.local();
1807 const Set<BMVert *, 0> &verts = BKE_pbvh_bmesh_node_unique_verts(&nodes[node_index]);
1808 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
1809
1810 tls.factors.resize(verts.size());
1811 const MutableSpan<float> factors = tls.factors;
1812 fill_factor_from_hide_and_mask(bm, verts, factors);
1814 object,
1815 ss.filter_cache->automasking.get(),
1816 nodes[node_index],
1817 verts,
1818 factors);
1819 scale_factors(factors, strength);
1820
1821 /* This filter can't work at full strength as it needs multiple iterations to reach a
1822 * stable state. */
1823 clamp_factors(factors, 0.0f, 0.5f);
1824
1825 tls.smooth_positions.resize(verts.size());
1826 const MutableSpan<float3> smooth_positions = tls.smooth_positions;
1827 smooth::neighbor_position_average_bmesh(verts, smooth_positions);
1828
1829 const Span<float> sharpen_factors = gather_data_bmesh(
1830 ss.filter_cache->sharpen_factor.as_span(), verts, tls.sharpen_factors);
1831
1832 tls.translations.resize(verts.size());
1833 const MutableSpan<float3> translations = tls.translations;
1834
1835 BMeshNeighborVerts neighbors;
1836
1837 int i = 0;
1838 for (BMVert *vert : verts) {
1839 const float3 position = vert->co;
1840
1841 float3 disp_sharpen(0.0f);
1842 for (const BMVert *neighbor : vert_neighbors_get_bmesh(*vert, neighbors)) {
1843 float3 disp_n = float3(neighbor->co) - position;
1844 disp_n *= ss.filter_cache->sharpen_factor[BM_elem_index_get(neighbor)];
1845 disp_sharpen += disp_n;
1846 }
1847
1848 disp_sharpen *= (1.0f - sharpen_factors[i]);
1849 translations[i] = disp_sharpen;
1850 i++;
1851 }
1852
1853 const Span<float3> detail_directions = gather_data_bmesh(
1854 ss.filter_cache->detail_directions.as_span(), verts, tls.detail_directions);
1855
1857 positions,
1858 smooth_positions,
1859 sharpen_factors,
1860 detail_directions,
1861 translations);
1862 scale_translations(translations, factors);
1863
1865 clip_and_lock_translations(sd, ss, positions, translations);
1866 apply_translations(translations, verts);
1867 },
1869 break;
1870 }
1871 }
1872}
1873
1874static void calc_enhance_details_filter(const Depsgraph &depsgraph,
1875 const Sculpt &sd,
1876 const float strength,
1877 Object &object,
1878 const IndexMask &node_mask)
1879{
1880 const float final_strength = -std::abs(strength);
1881 struct LocalData {
1882 Vector<float> factors;
1883 Vector<float3> positions;
1884 Vector<float3> translations;
1885 };
1886 SculptSession &ss = *object.runtime->sculpt_session;
1888 switch (pbvh.type()) {
1889 case bke::pbvh::Type::Mesh: {
1890 Mesh &mesh = *id_cast<Mesh *>(object.data);
1891 const MeshAttributeData attribute_data(mesh);
1892 const PositionDeformData position_data(depsgraph, object);
1895 node_mask.foreach_index(
1896 [&](const int i) {
1897 LocalData &tls = all_tls.local();
1898 const Span<int> verts = nodes[i].verts();
1899
1900 tls.factors.resize(verts.size());
1901 const MutableSpan<float> factors = tls.factors;
1903 attribute_data.hide_vert, attribute_data.mask, verts, factors);
1905 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1906 scale_factors(factors, final_strength);
1907
1908 const MutableSpan translations = gather_data_mesh(
1909 ss.filter_cache->detail_directions.as_span(), verts, tls.translations);
1910 scale_translations(translations, factors);
1911
1913 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
1914 position_data.deform(translations, verts);
1915 },
1917 break;
1918 }
1920 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1923 node_mask.foreach_index(
1924 [&](const int i) {
1925 LocalData &tls = all_tls.local();
1926 const Span<int> grids = nodes[i].grids();
1927 const Span<float3> positions = gather_grids_positions(
1928 subdiv_ccg, grids, tls.positions);
1929
1930 tls.factors.resize(positions.size());
1931 const MutableSpan<float> factors = tls.factors;
1932 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
1934 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
1935 scale_factors(factors, final_strength);
1936
1937 const MutableSpan translations = gather_data_grids(
1938 subdiv_ccg, ss.filter_cache->detail_directions.as_span(), grids, tls.translations);
1939 scale_translations(translations, factors);
1940
1942 clip_and_lock_translations(sd, ss, positions, translations);
1943 apply_translations(translations, grids, subdiv_ccg);
1944 },
1946 break;
1947 }
1949 BMesh &bm = *ss.bm;
1952 node_mask.foreach_index(
1953 [&](const int i) {
1954 LocalData &tls = all_tls.local();
1956 const Span<float3> positions = gather_bmesh_positions(verts, tls.positions);
1957
1958 tls.factors.resize(verts.size());
1959 const MutableSpan<float> factors = tls.factors;
1960 fill_factor_from_hide_and_mask(bm, verts, factors);
1962 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], verts, factors);
1963 scale_factors(factors, final_strength);
1964
1965 const MutableSpan<float3> translations = gather_data_bmesh(
1966 ss.filter_cache->detail_directions.as_span(), verts, tls.translations);
1967 scale_translations(translations, factors);
1968
1970 clip_and_lock_translations(sd, ss, positions, translations);
1971 apply_translations(translations, verts);
1972 },
1974 break;
1975 }
1976 }
1977}
1978
1979static void calc_erase_displacement_filter(const Depsgraph &depsgraph,
1980 const Sculpt &sd,
1981 const float strength,
1982 Object &object,
1983 const IndexMask &node_mask)
1984{
1985 struct LocalData {
1986 Vector<float> factors;
1987 Vector<float3> positions;
1988 Vector<float3> new_positions;
1989 Vector<float3> translations;
1990 };
1991 SculptSession &ss = *object.runtime->sculpt_session;
1993 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1996 node_mask.foreach_index(
1997 [&](const int i) {
1998 LocalData &tls = all_tls.local();
1999 const Span<int> grids = nodes[i].grids();
2000 const Span<float3> positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
2001
2002 tls.factors.resize(positions.size());
2003 const MutableSpan<float> factors = tls.factors;
2004 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
2006 depsgraph, object, ss.filter_cache->automasking.get(), nodes[i], grids, factors);
2007 scale_factors(factors, strength);
2008 clamp_factors(factors, -1.0f, 1.0f);
2009
2010 const MutableSpan<float3> new_positions = gather_data_grids(
2011 subdiv_ccg, ss.filter_cache->limit_surface_co.as_span(), grids, tls.new_positions);
2012 tls.translations.resize(positions.size());
2013 const MutableSpan<float3> translations = tls.translations;
2014 translations_from_new_positions(new_positions, positions, translations);
2015 scale_translations(translations, factors);
2016
2018 clip_and_lock_translations(sd, ss, positions, translations);
2019 apply_translations(translations, grids, subdiv_ccg);
2020 },
2022}
2023
2025 const float shape_preservation,
2026 const float current_vertex_displacement)
2027{
2028 SculptSession &ss = *object.runtime->sculpt_session;
2029 const int totvert = vertex_count_get(object);
2030 filter::Cache *filter_cache = ss.filter_cache;
2031
2032 filter_cache->surface_smooth_laplacian_disp.reinitialize(totvert);
2033 filter_cache->surface_smooth_laplacian_disp.fill(float3(0.0f));
2034 filter_cache->surface_smooth_shape_preservation = shape_preservation;
2035 filter_cache->surface_smooth_current_vertex = current_vertex_displacement;
2036}
2037
2038static void calc_limit_surface_positions(const Object &object, MutableSpan<float3> limit_positions)
2039{
2040 const SculptSession &ss = *object.runtime->sculpt_session;
2041 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
2042 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
2043
2044 threading::parallel_for(IndexRange(subdiv_ccg.grids_num), 512, [&](const IndexRange range) {
2045 for (const int grid : range) {
2046 MutableSpan grid_dst = limit_positions.slice(bke::ccg::grid_range(key, grid));
2047 BKE_subdiv_ccg_eval_limit_positions(subdiv_ccg, key, grid, grid_dst);
2048 }
2049 });
2050}
2051
2052static void mesh_filter_sharpen_init(const Depsgraph &depsgraph,
2053 const Object &object,
2054 const float smooth_ratio,
2055 const float intensify_detail_strength,
2056 const int curvature_smooth_iterations,
2057 filter::Cache &filter_cache)
2058{
2059 const SculptSession &ss = *object.runtime->sculpt_session;
2060 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
2061 const IndexMask &node_mask = filter_cache.node_mask;
2062 const int totvert = vertex_count_get(object);
2063
2064 filter_cache.sharpen_smooth_ratio = smooth_ratio;
2065 filter_cache.sharpen_intensify_detail_strength = intensify_detail_strength;
2066 filter_cache.sharpen_curvature_smooth_iterations = curvature_smooth_iterations;
2067 filter_cache.sharpen_factor.reinitialize(totvert);
2068 filter_cache.detail_directions.reinitialize(totvert);
2069 MutableSpan<float3> detail_directions = filter_cache.detail_directions;
2070 MutableSpan<float> sharpen_factors = filter_cache.sharpen_factor;
2071
2072 calc_smooth_translations(depsgraph, object, node_mask, filter_cache.detail_directions);
2073
2074 for (int i = 0; i < totvert; i++) {
2075 sharpen_factors[i] = math::length(detail_directions[i]);
2076 }
2077
2078 float max_factor = 0.0f;
2079 for (int i = 0; i < totvert; i++) {
2080 max_factor = std::max(sharpen_factors[i], max_factor);
2081 }
2082
2083 max_factor = math::safe_rcp(max_factor);
2084 for (int i = 0; i < totvert; i++) {
2085 sharpen_factors[i] *= max_factor;
2086 sharpen_factors[i] = 1.0f - pow2f(1.0f - sharpen_factors[i]);
2087 }
2088
2089 /* Smooth the calculated factors and directions to remove high frequency detail. */
2090 struct LocalData {
2091 Vector<int> neighbor_offsets;
2092 Vector<int> neighbor_data;
2093 Vector<float3> smooth_directions;
2094 Vector<float> smooth_factors;
2095 };
2097 for ([[maybe_unused]] const int _ : IndexRange(filter_cache.sharpen_curvature_smooth_iterations))
2098 {
2099 switch (pbvh.type()) {
2100 case bke::pbvh::Type::Mesh: {
2101 Mesh &mesh = *id_cast<Mesh *>(object.data);
2102 const OffsetIndices faces = mesh.faces();
2103 const Span<int> corner_verts = mesh.corner_verts();
2104 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
2106 const MeshAttributeData attribute_data(mesh);
2107 node_mask.foreach_index(
2108 [&](const int i) {
2109 LocalData &tls = all_tls.local();
2110 const Span<int> verts = nodes[i].verts();
2111
2112 const GroupedSpan<int> neighbors = calc_vert_neighbors(faces,
2113 corner_verts,
2114 vert_to_face_map,
2115 attribute_data.hide_poly,
2116 verts,
2117 tls.neighbor_offsets,
2118 tls.neighbor_data);
2119
2120 tls.smooth_directions.resize(verts.size());
2122 detail_directions.as_span(),
2123 verts,
2124 neighbors,
2125 tls.smooth_directions.as_mutable_span());
2126 scatter_data_mesh(tls.smooth_directions.as_span(), verts, detail_directions);
2127
2128 tls.smooth_factors.resize(verts.size());
2130 verts,
2131 neighbors,
2132 tls.smooth_factors.as_mutable_span());
2133 scatter_data_mesh(tls.smooth_factors.as_span(), verts, sharpen_factors);
2134 },
2136 break;
2137 }
2139 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
2140 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
2142 node_mask.foreach_index(
2143 [&](const int i) {
2144 LocalData &tls = all_tls.local();
2145 const Span<int> grids = nodes[i].grids();
2146 const int grid_verts_num = grids.size() * key.grid_area;
2147
2148 tls.smooth_directions.resize(grid_verts_num);
2149 smooth::average_data_grids(subdiv_ccg,
2150 detail_directions.as_span(),
2151 grids,
2152 tls.smooth_directions.as_mutable_span());
2154 subdiv_ccg, tls.smooth_directions.as_span(), grids, detail_directions);
2155
2156 tls.smooth_factors.resize(grid_verts_num);
2157 smooth::average_data_grids(subdiv_ccg,
2158 sharpen_factors.as_span(),
2159 grids,
2160 tls.smooth_factors.as_mutable_span());
2161 scatter_data_grids(subdiv_ccg, tls.smooth_factors.as_span(), grids, sharpen_factors);
2162 },
2164 break;
2165 }
2168 node_mask.foreach_index(
2169 [&](const int i) {
2170 LocalData &tls = all_tls.local();
2172 &const_cast<bke::pbvh::BMeshNode &>(nodes[i]));
2173
2174 tls.smooth_directions.resize(verts.size());
2176 detail_directions.as_span(), verts, tls.smooth_directions.as_mutable_span());
2177 scatter_data_bmesh(tls.smooth_directions.as_span(), verts, detail_directions);
2178
2179 tls.smooth_factors.resize(verts.size());
2181 sharpen_factors.as_span(), verts, tls.smooth_factors.as_mutable_span());
2182 scatter_data_bmesh(tls.smooth_factors.as_span(), verts, sharpen_factors);
2183 },
2185 break;
2186 }
2187 }
2188}
2189
2190enum {
2193};
2194
2196{
2197 static const EnumPropertyItem modal_items[] = {
2198 {FILTER_MESH_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
2199 {FILTER_MESH_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
2200 {0, nullptr, 0, nullptr, nullptr},
2201 };
2202
2203 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "Mesh Filter Modal Map");
2204
2205 /* This function is called for each space-type, only needs to add map once. */
2206 if (keymap && keymap->modal_items) {
2207 return nullptr;
2208 }
2209
2210 keymap = WM_modalkeymap_ensure(keyconf, "Mesh Filter Modal Map", modal_items);
2211
2212 WM_modalkeymap_assign(keymap, "SCULPT_OT_mesh_filter");
2213
2214 return keymap;
2215}
2216
2218{
2219 WorkspaceStatus status(C);
2220 status.item(IFACE_("Confirm"), ICON_EVENT_RETURN);
2221 status.item(IFACE_("Cancel"), ICON_EVENT_ESC, ICON_MOUSE_RMB);
2222}
2223
2224static void sculpt_mesh_filter_apply(bContext *C, wmOperator *op, bool is_replay = false)
2225{
2226 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
2229 const Sculpt &sd = *CTX_data_tool_settings(C)->sculpt;
2230 const MeshFilterType filter_type = MeshFilterType(RNA_enum_get(op->ptr, "type"));
2231 const float strength = RNA_float_get(op->ptr, "strength");
2232
2234
2235 const IndexMask &node_mask = ss.filter_cache->node_mask;
2236 if (auto_mask::is_enabled(sd.paint, ob, nullptr) && ss.filter_cache->automasking &&
2238 {
2239 ss.filter_cache->automasking->calc_cavity_factor(depsgraph, ob, node_mask);
2240 }
2241 switch (filter_type) {
2243 calc_smooth_filter(depsgraph,
2244 sd,
2245 strength,
2246 ob,
2247 node_mask,
2248 is_replay && ss.filter_cache->iteration_count == 0);
2249 break;
2251 calc_scale_filter(depsgraph, sd, strength, ob, node_mask);
2252 break;
2254 calc_inflate_filter(depsgraph, sd, strength, ob, node_mask);
2255 break;
2257 calc_sphere_filter(depsgraph, sd, strength, ob, node_mask);
2258 break;
2260 calc_random_filter(depsgraph, sd, strength, ob, node_mask);
2261 break;
2263 calc_relax_filter(depsgraph, sd, strength, ob, node_mask);
2264 break;
2266 calc_relax_face_sets_filter(depsgraph, sd, strength, ob, node_mask);
2267 break;
2269 calc_surface_smooth_filter(depsgraph, sd, strength, ob, node_mask);
2270 break;
2272 calc_sharpen_filter(depsgraph, sd, strength, ob, node_mask);
2273 break;
2275 calc_enhance_details_filter(depsgraph, sd, strength, ob, node_mask);
2276 break;
2278 calc_erase_displacement_filter(depsgraph, sd, strength, ob, node_mask);
2279 break;
2280 }
2281
2283 pbvh.tag_positions_changed(node_mask);
2284 pbvh.update_bounds(depsgraph, ob);
2285
2287
2289}
2290
2292 SculptSession &ss,
2293 float2 prev_press_mouse,
2294 float2 mouse)
2295{
2296 const float len = prev_press_mouse[0] - mouse[0];
2297
2298 float filter_strength = ss.filter_cache->start_filter_strength * -len * 0.001f * UI_SCALE_FAC;
2299 RNA_float_set(op->ptr, "strength", filter_strength);
2300}
2302{
2303 /* Event history is only stored for smooth and relax filters. */
2304 if (!RNA_collection_length(op->ptr, "event_history")) {
2306 return;
2307 }
2308
2311 float2 start_mouse;
2312 bool first = true;
2313 float initial_strength = ss.filter_cache->start_filter_strength;
2314
2315 RNA_BEGIN (op->ptr, item, "event_history") {
2316 float2 mouse;
2317 RNA_float_get_array(&item, "mouse_event", mouse);
2318
2319 if (first) {
2320 first = false;
2321 start_mouse = mouse;
2322 continue;
2323 }
2324
2325 sculpt_mesh_update_strength(op, ss, start_mouse, mouse);
2326 sculpt_mesh_filter_apply(C, op, true);
2327 }
2328 RNA_END;
2329
2330 RNA_float_set(op->ptr, "strength", initial_strength);
2331}
2332
2334{
2337
2338 MEM_delete(ss.filter_cache);
2339 ss.filter_cache = nullptr;
2341}
2342
2344 wmOperator *op,
2345 const MeshFilterType filter_type)
2346{
2347 float initial_strength = ss.filter_cache->start_filter_strength;
2348 /* Don't update strength property if we're storing an event history. */
2349 if (sculpt_mesh_filter_is_continuous(filter_type)) {
2350 RNA_float_set(op->ptr, "strength", initial_strength);
2351 }
2352
2353 return OPERATOR_FINISHED;
2354}
2355
2357{
2358 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(C);
2362
2363 if (!ss || !pbvh) {
2364 return;
2365 }
2366
2368 bke::pbvh::update_normals(depsgraph, ob, *pbvh);
2369 pbvh->update_bounds(depsgraph, ob);
2370}
2371
2373{
2375 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
2377 const MeshFilterType filter_type = MeshFilterType(RNA_enum_get(op->ptr, "type"));
2378
2381
2382 if (event->type == EVT_MODAL_MAP) {
2384 switch (event->val) {
2387 undo::push_end_ex(ob, true);
2389 break;
2390
2392 ret = sculpt_mesh_filter_confirm(ss, op, filter_type);
2393 undo::push_end_ex(ob, false);
2394 break;
2395 }
2396
2398 ED_workspace_status_text(C, nullptr); /* Clear status bar */
2400
2401 return ret;
2402 }
2403
2404 if (event->type != MOUSEMOVE) {
2406 }
2407
2408 /* NOTE: some filter types are continuous, for these we store an
2409 * event history in RNA for continuous.
2410 * This way the user can tweak the last operator properties
2411 * or repeat the op and get expected results. */
2412 if (sculpt_mesh_filter_is_continuous(filter_type)) {
2413 if (RNA_collection_length(op->ptr, "event_history") == 0) {
2414 /* First entry is the start mouse position, event->prev_press_xy. */
2415 PointerRNA startptr;
2416 RNA_collection_add(op->ptr, "event_history", &startptr);
2417
2418 float2 mouse_start(float(event->prev_press_xy[0]), float(event->prev_press_xy[1]));
2419 RNA_float_set_array(&startptr, "mouse_event", mouse_start);
2420 }
2421
2422 PointerRNA itemptr;
2423 RNA_collection_add(op->ptr, "event_history", &itemptr);
2424
2425 float2 mouse(float(event->xy[0]), float(event->xy[1]));
2426 RNA_float_set_array(&itemptr, "mouse_event", mouse);
2427 RNA_float_set(&itemptr, "pressure", WM_event_tablet_data(event, nullptr, nullptr));
2428 }
2429 else {
2431 }
2432
2433 float2 prev_mval(float(event->prev_press_xy[0]), float(event->prev_press_xy[1]));
2434 float2 mval(float(event->xy[0]), float(event->xy[1]));
2435
2436 sculpt_mesh_update_strength(op, ss, prev_mval, mval);
2437
2438 BKE_sculptsession_update_for_edit(depsgraph, &ob, false);
2439
2441
2443}
2444
2445static void sculpt_filter_specific_init(const Depsgraph &depsgraph,
2446 const MeshFilterType filter_type,
2447 wmOperator *op,
2448 Object &object)
2449{
2450 SculptSession &ss = *object.runtime->sculpt_session;
2451 switch (filter_type) {
2454 RNA_float_get(op->ptr, "surface_smooth_shape_preservation"),
2455 RNA_float_get(op->ptr, "surface_smooth_current_vertex"));
2456 break;
2457 }
2459 mesh_filter_sharpen_init(depsgraph,
2460 object,
2461 RNA_float_get(op->ptr, "sharpen_smooth_ratio"),
2462 RNA_float_get(op->ptr, "sharpen_intensify_detail_strength"),
2463 RNA_int_get(op->ptr, "sharpen_curvature_smooth_iterations"),
2464 *ss.filter_cache);
2465 break;
2466 }
2470 depsgraph, object, ss.filter_cache->node_mask, ss.filter_cache->detail_directions);
2471 break;
2472 }
2476 break;
2477 }
2478 default:
2479 break;
2480 }
2481}
2482
2483/* Returns OPERATOR_PASS_THROUGH on success. */
2485{
2486 const Scene &scene = *CTX_data_scene(C);
2488 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
2490
2491 const View3D *v3d = CTX_wm_view3d(C);
2492 const Base *base = CTX_data_active_base(C);
2493 if (!BKE_base_is_visible(v3d, base)) {
2494 return OPERATOR_CANCELLED;
2495 }
2496
2497 int mval[2];
2498 RNA_int_get_array(op->ptr, "start_mouse", mval);
2499
2500 const MeshFilterType filter_type = MeshFilterType(RNA_enum_get(op->ptr, "type"));
2501 const bool use_automasking = auto_mask::is_enabled(sd.paint, ob, nullptr);
2502 const bool needs_topology_info = sculpt_mesh_filter_needs_pmap(filter_type) || use_automasking;
2503
2504 BKE_sculptsession_update_for_edit(depsgraph, &ob, false);
2505
2506 if (!shape_key_check(ob, op->reports)) {
2507 return OPERATOR_CANCELLED;
2508 }
2509
2511
2513 if (filter_type == MeshFilterType::EraseDisplacement && pbvh.type() != bke::pbvh::Type::Grids) {
2514 return OPERATOR_CANCELLED;
2515 }
2516
2517 const eMeshFilterDeformAxis deform_axis = eMeshFilterDeformAxis(
2518 RNA_enum_get(op->ptr, "deform_axis"));
2519
2520 if (deform_axis == 0) {
2521 /* All axis are disabled, so the filter is not going to produce any deformation. */
2522 return OPERATOR_CANCELLED;
2523 }
2524
2525 float2 mval_fl{float(mval[0]), float(mval[1])};
2526 if (use_automasking) {
2527 /* Update the active face set manually as the paint cursor is not enabled when using the
2528 * Mesh Filter Tool. */
2529 cursor_geometry_info_update(C, mval_fl, false);
2530 }
2531
2533 if (needs_topology_info) {
2535 }
2536
2537 undo::push_begin(scene, ob, op);
2538
2539 cache_init(C,
2540 ob,
2541 sd,
2543 mval_fl,
2544 RNA_float_get(op->ptr, "area_normal_radius"),
2545 RNA_float_get(op->ptr, "strength"));
2546
2547 filter::Cache *filter_cache = ss.filter_cache;
2548 filter_cache->active_face_set = face_set_none_id;
2549 if (auto_mask::is_enabled(sd.paint, ob, nullptr)) {
2550 auto_mask::filter_cache_ensure(*depsgraph, sd.paint, ob);
2551 }
2552
2553 sculpt_filter_specific_init(*depsgraph, filter_type, op, ob);
2554
2555 ss.filter_cache->enabled_axis[0] = deform_axis & MESH_FILTER_DEFORM_X;
2556 ss.filter_cache->enabled_axis[1] = deform_axis & MESH_FILTER_DEFORM_Y;
2557 ss.filter_cache->enabled_axis[2] = deform_axis & MESH_FILTER_DEFORM_Z;
2558
2559 ss.filter_cache->orientation = FilterOrientation(RNA_enum_get(op->ptr, "orientation"));
2560
2561 return OPERATOR_PASS_THROUGH;
2562}
2563
2565 wmOperator *op,
2566 const wmEvent *event)
2567{
2568 RNA_int_set_array(op->ptr, "start_mouse", event->mval);
2570
2571 if (ret == OPERATOR_PASS_THROUGH) {
2574 }
2575
2576 return ret;
2577}
2578
2580{
2582
2583 if (ret == OPERATOR_PASS_THROUGH) {
2584 int iterations = RNA_int_get(op->ptr, "iteration_count");
2585
2586 for (int i = 0; i < iterations; i++) {
2588 }
2589
2592
2593 return OPERATOR_FINISHED;
2594 }
2595
2596 return ret;
2597}
2598
2600{
2602 ot->srna, "start_mouse", 2, nullptr, 0, 1 << 14, "Starting Mouse", "", 0, 1 << 14);
2603
2604 RNA_def_float(ot->srna,
2605 "area_normal_radius",
2606 0.25,
2607 0.001,
2608 5.0,
2609 "Normal Radius",
2610 "Radius used for calculating area normal on initial click,\nin percentage "
2611 "of brush radius",
2612 0.01,
2613 1.0);
2614 PropertyRNA *prop = RNA_def_float(
2615 ot->srna, "strength", 1.0f, -10.0f, 10.0f, "Strength", "Filter strength", -10.0f, 10.0f);
2617 RNA_def_int(ot->srna,
2618 "iteration_count",
2619 1,
2620 1,
2621 10000,
2622 "Repeat",
2623 "How many times to repeat the filter",
2624 1,
2625 100);
2626
2627 /* Smooth filter requires entire event history. */
2628 prop = RNA_def_collection_runtime(ot->srna, "event_history", RNA_OperatorStrokeElement, "", "");
2630}
2631
2632static void sculpt_mesh_ui_exec(bContext * /*C*/, wmOperator *op)
2633{
2634 ui::Layout &layout = *op->layout;
2635
2636 layout.prop(op->ptr, "strength", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2637 layout.prop(op->ptr, "iteration_count", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2638 layout.prop(op->ptr, "orientation", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2639 ui::Layout &row = layout.row(true);
2640 row.prop(op->ptr, "deform_axis", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
2641}
2642
2644{
2645 ot->name = "Filter Mesh";
2646 ot->idname = "SCULPT_OT_mesh_filter";
2647 ot->description = "Applies a filter to modify the current mesh";
2648
2651 ot->poll = sculpt_mode_poll;
2653 ot->ui = sculpt_mesh_ui_exec;
2654
2655 /* Doesn't seem to actually be called?
2656 * Check `sculpt_mesh_filter_modal` to see where it's really called. */
2658
2661
2663
2664 ot->prop = RNA_def_enum(ot->srna,
2665 "type",
2668 "Filter Type",
2669 "Operation that is going to be applied to the mesh");
2672 "deform_axis",
2675 "Deform Axis",
2676 "Apply the deformation in the selected axis");
2677 RNA_def_enum(ot->srna,
2678 "orientation",
2681 "Orientation",
2682 "Orientation of the axis to limit the filter displacement");
2683
2684 /* Surface Smooth Mesh Filter properties. */
2685 RNA_def_float(ot->srna,
2686 "surface_smooth_shape_preservation",
2687 0.5f,
2688 0.0f,
2689 1.0f,
2690 "Shape Preservation",
2691 "How much of the original shape is preserved when smoothing",
2692 0.0f,
2693 1.0f);
2694 RNA_def_float(ot->srna,
2695 "surface_smooth_current_vertex",
2696 0.5f,
2697 0.0f,
2698 1.0f,
2699 "Per Vertex Displacement",
2700 "How much the position of each individual vertex influences the final result",
2701 0.0f,
2702 1.0f);
2703 RNA_def_float(ot->srna,
2704 "sharpen_smooth_ratio",
2705 0.35f,
2706 0.0f,
2707 1.0f,
2708 "Smooth Ratio",
2709 "How much smoothing is applied to polished surfaces",
2710 0.0f,
2711 1.0f);
2712
2713 RNA_def_float(ot->srna,
2714 "sharpen_intensify_detail_strength",
2715 0.0f,
2716 0.0f,
2717 10.0f,
2718 "Intensify Details",
2719 "How much creases and valleys are intensified",
2720 0.0f,
2721 1.0f);
2722
2723 RNA_def_int(ot->srna,
2724 "sharpen_curvature_smooth_iterations",
2725 0,
2726 0,
2727 10,
2728 "Curvature Smooth Iterations",
2729 "How much smooth the resulting shape is, ignoring high frequency details",
2730 0,
2731 10);
2732}
2733
2734} // namespace blender::ed::sculpt_paint::filter
A BVH for high poly meshes.
#define BLT_I18NCONTEXT_AMOUNT
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define UI_SCALE_FAC
Read Blender memory allocation and freeing.
#define RNA_BEGIN(sptr, itemptr, propname)
#define RNA_END
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define BLI_NOINLINE
#define ELEM(...)
#define BM_elem_index_get(ele)
btScalar max
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
void fill(const T &value) const
Definition BLI_array.hh:283
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
int64_t size() const
Definition BLI_set.hh:606
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
GAttributeReader lookup(const StringRef name) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define position
ccl_device_inline float beta(const float x, const float y)
Definition math_base.h:708
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
pbvh::Tree * pbvh_get(Object &object)
void stroke_track_location(Paint &paint, float3 location)
IndexMask search_nodes(const Tree &pbvh, IndexMaskMemory &memory, FunctionRef< bool(const Node &)> filter_fn)
Definition pbvh.cc:2556
void update_normals(const Depsgraph &depsgraph, Object &object_orig, Tree &pbvh)
Definition pbvh.cc:1179
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
bool is_enabled(const Paint &paint, const Object &object, const Brush *br)
void calc_vert_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::MeshNode &node, Span< int > verts, MutableSpan< float > factors)
void calc_grids_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::GridsNode &node, Span< int > grids, MutableSpan< float > factors)
Cache & filter_cache_ensure(const Depsgraph &depsgraph, const Paint &paint, Object &ob)
void ensure_boundary_info(Object &object)
Definition sculpt.cc:588
void filter_verts_with_unique_face_sets_mesh(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const bool unique, const Span< int > verts, const MutableSpan< float > factors)
void filter_verts_with_unique_face_sets_bmesh(int face_set_offset, const bool unique, const Set< BMVert *, 0 > &verts, const MutableSpan< float > factors)
void filter_verts_with_unique_face_sets_grids(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const SubdivCCG &subdiv_ccg, const bool unique, const Span< int > grids, const MutableSpan< float > factors)
static void calc_inflate_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
static wmOperatorStatus sculpt_mesh_filter_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void sculpt_mesh_update_strength(wmOperator *op, SculptSession &ss, float2 prev_press_mouse, float2 mouse)
static EnumPropertyItem prop_mesh_filter_types[]
static void sculpt_mesh_filter_apply_with_history(bContext *C, wmOperator *op)
static void calc_sharpen_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
static void calc_random_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
static bool sculpt_mesh_filter_is_continuous(MeshFilterType type)
static BLI_NOINLINE void calc_sharpen_detail_translations(const filter::Cache &filter_cache, const Span< float3 > positions, const Span< float3 > smooth_positions, const Span< float > sharpen_factors, const Span< float3 > detail_directions, const MutableSpan< float3 > translations)
static void sculpt_filter_specific_init(const Depsgraph &depsgraph, const MeshFilterType filter_type, wmOperator *op, Object &object)
static void sculpt_mesh_filter_end(bContext *C)
static void calc_enhance_details_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
static void calc_smooth_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask, const bool use_original_position)
void zero_disabled_axis_components(const filter::Cache &filter_cache, MutableSpan< float3 > vectors)
static void calc_scale_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
static wmOperatorStatus sculpt_mesh_filter_confirm(SculptSession &ss, wmOperator *op, const MeshFilterType filter_type)
float3x3 to_orientation_space(const filter::Cache &filter_cache)
static void calc_sphere_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
void cache_init(bContext *C, Object &ob, Sculpt &sd, undo::Type undo_type, const float mval_fl[2], float area_normal_radius, float start_strength)
static void mesh_filter_sharpen_init(const Depsgraph &depsgraph, const Object &object, const float smooth_ratio, const float intensify_detail_strength, const int curvature_smooth_iterations, filter::Cache &filter_cache)
static BLI_NOINLINE void calc_sphere_translations(const Span< float3 > positions, const Span< float > factors, const MutableSpan< float3 > translations)
static void calc_relax_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
static wmOperatorStatus sculpt_mesh_filter_exec(bContext *C, wmOperator *op)
static void calc_erase_displacement_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
static BLI_NOINLINE void clamp_factors(const MutableSpan< float > factors, const float min, const float max)
static void sculpt_mesh_ui_exec(bContext *, wmOperator *op)
static void sculpt_mesh_filter_cancel(bContext *C, wmOperator *)
static EnumPropertyItem prop_mesh_filter_orientation_items[]
static wmOperatorStatus sculpt_mesh_filter_start(bContext *C, wmOperator *op)
static void calc_surface_smooth_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
static void mesh_filter_surface_smooth_init(Object &object, const float shape_preservation, const float current_vertex_displacement)
static void calc_limit_surface_positions(const Object &object, MutableSpan< float3 > limit_positions)
float3x3 to_object_space(const filter::Cache &filter_cache)
static void sculpt_mesh_filter_apply(bContext *C, wmOperator *op, bool is_replay=false)
void SCULPT_OT_mesh_filter(wmOperatorType *ot)
static bool sculpt_mesh_filter_needs_pmap(MeshFilterType filter_type)
static EnumPropertyItem prop_mesh_filter_deform_axis_items[]
static BLI_NOINLINE void randomize_factors(const Span< float3 > positions, const int seed, const MutableSpan< float > factors)
wmKeyMap * modal_keymap(wmKeyConfig *keyconf)
static void sculpt_mesh_update_status_bar(bContext *C, wmOperator *)
void register_operator_props(wmOperatorType *ot)
static wmOperatorStatus sculpt_mesh_filter_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void calc_relax_face_sets_filter(const Depsgraph &depsgraph, const Sculpt &sd, const float strength, Object &object, const IndexMask &node_mask)
void average_data_bmesh(const Span< T > src, const Set< BMVert *, 0 > &verts, const MutableSpan< T > dst)
void neighbor_position_average_interior_grids(const OffsetIndices< int > faces, const Span< int > corner_verts, const BitSpan boundary_verts, const Set< OrderedEdge > &boundary_edges, const SubdivCCG &subdiv_ccg, const Span< int > grids, const MutableSpan< float3 > new_positions)
void calc_relaxed_translations_faces(const Span< float3 > vert_positions, const Span< float3 > vert_normals, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const BitSpan boundary_verts, const Set< OrderedEdge > &boundary_edges, const Span< int > face_sets, const Span< bool > hide_poly, const bool filter_boundary_face_sets, const Span< int > verts, const Span< float > factors, const MutableSpan< float3 > translations)
void neighbor_data_average_mesh_check_loose(const Span< T > src, const Span< int > verts, const GroupedSpan< int > vert_neighbors, const MutableSpan< T > dst)
void neighbor_position_average_interior_bmesh(const Set< BMVert *, 0 > &verts, const Span< float > factors, const MutableSpan< float3 > new_positions)
void surface_smooth_laplacian_step(const Span< float3 > positions, const Span< float3 > orig_positions, const Span< float3 > average_positions, const float alpha, MutableSpan< float3 > laplacian_disp, MutableSpan< float3 > translations)
void surface_smooth_displace_step(const Span< float3 > laplacian_disp, const Span< float3 > average_laplacian_disp, const float beta, const MutableSpan< float3 > translations)
void average_data_grids(const SubdivCCG &subdiv_ccg, const Span< T > src, const Span< int > grids, const MutableSpan< T > dst)
void calc_relaxed_translations_bmesh(const Set< BMVert *, 0 > &verts, const Span< float3 > positions, const int face_set_offset, const bool filter_boundary_face_sets, const Span< float > factors, const MutableSpan< float3 > translations)
void neighbor_position_average_bmesh(const Set< BMVert *, 0 > &verts, const MutableSpan< float3 > new_positions)
void calc_relaxed_translations_grids(const SubdivCCG &subdiv_ccg, const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< int > face_sets, const GroupedSpan< int > vert_to_face_map, const BitSpan boundary_verts, const Set< OrderedEdge > &boundary_edges, const Span< int > grids, const bool filter_boundary_face_sets, const Span< float > factors, const MutableSpan< float3 > translations)
void push_nodes(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask, const Type type)
void restore_position_from_undo_step(const Depsgraph &depsgraph, Object &object)
Definition sculpt.cc:1181
void push_begin(const Scene &scene, Object &ob, const wmOperator *op)
void push_end_ex(Object &ob, const bool use_nested_undo)
bool shape_key_check(const Object &ob, ReportList *reports)
Definition sculpt.cc:134
void fill_factor_from_hide_and_mask(Span< bool > hide_vert, Span< float > mask, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7128
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void calc_smooth_translations(const Depsgraph &depsgraph, const Object &object, const IndexMask &node_mask, const MutableSpan< float3 > translations)
bool node_in_sphere(const bke::pbvh::Node &node, const float3 &location, const float radius_sq, const bool original)
Definition sculpt.cc:2511
void scatter_data_bmesh(Span< T > node_data, const Set< BMVert *, 0 > &verts, MutableSpan< T > dst)
Definition sculpt.cc:6577
void gather_bmesh_positions(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6497
void gather_data_grids(const SubdivCCG &subdiv_ccg, Span< T > src, Span< int > grids, MutableSpan< T > node_data)
Definition sculpt.cc:6528
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
GroupedSpan< int > calc_vert_neighbors(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face, Span< bool > hide_poly, Span< int > verts, Vector< int > &r_offset_data, Vector< int > &r_data)
Definition sculpt.cc:7949
bool node_fully_masked_or_hidden(const bke::pbvh::Node &node)
Definition sculpt.cc:2500
Vector< BMVert *, 64 > BMeshNeighborVerts
int vertex_count_get(const Object &object)
Definition sculpt.cc:163
void gather_data_bmesh(Span< T > src, const Set< BMVert *, 0 > &verts, MutableSpan< T > node_data)
Definition sculpt.cc:6545
void reset_translations_to_original(MutableSpan< float3 > translations, Span< float3 > positions, Span< float3 > orig_positions)
Definition sculpt.cc:7572
void orig_position_data_gather_bmesh(const BMLog &bm_log, const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions, MutableSpan< float3 > normals)
void scale_translations(MutableSpan< float3 > translations, Span< float > factors)
Definition sculpt.cc:7837
GroupedSpan< int > calc_vert_neighbors_interior(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face, BitSpan boundary_verts, const Set< OrderedEdge > &boundary_edges, Span< bool > hide_poly, Span< int > verts, Vector< int > &r_offset_data, Vector< int > &r_data)
Definition sculpt.cc:8099
bool sculpt_mode_poll(bContext *C)
Definition sculpt.cc:3800
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
void translations_from_new_positions(Span< float3 > new_positions, Span< int > verts, Span< float3 > old_positions, MutableSpan< float3 > translations)
Definition sculpt.cc:7889
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
std::optional< float3 > stroke_get_location_bvh(Depsgraph &depsgraph, ViewContext &vc, const Sculpt &sd, const Brush *brush, const float mval[2], const bool force_original)
Definition sculpt.cc:4855
void scatter_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
std::optional< float3 > calc_area_normal(const Depsgraph &depsgraph, const Brush &brush, const Object &ob, const IndexMask &node_mask)
Definition sculpt.cc:1926
Span< BMVert * > vert_neighbors_get_bmesh(BMVert &vert, BMeshNeighborVerts &r_neighbors)
Definition sculpt.cc:490
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
OrigPositionData orig_position_data_get_mesh(const Object &object, const bke::pbvh::MeshNode &node)
void flush_update_step(bContext *C, const UpdateType update_type)
Definition sculpt.cc:5090
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
OrigPositionData orig_position_data_get_grids(const Object &object, const bke::pbvh::GridsNode &node)
void flush_update_done(bContext *C, Object &ob, const UpdateType update_type)
Definition sculpt.cc:5131
float object_space_radius_get(const ViewContext &vc, const Paint &paint, const Brush &brush, const float3 &location, const float scale_factor)
Definition sculpt.cc:121
std::optional< CursorGeometryInfo > cursor_geometry_info_update(bContext *C, const float2 &mval, const bool use_sampled_normal)
Definition sculpt.cc:4584
void scatter_data_grids(const SubdivCCG &subdiv_ccg, Span< T > node_data, Span< int > grids, MutableSpan< T > dst)
Definition sculpt.cc:6560
constexpr ParallelGrainSize grain_size(int grain_size)
T safe_rcp(const T &a)
T length(const VecBase< T, Size > &a)
T midpoint(const T &a, const T &b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T square(const T &a)
T abs(const T &a)
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
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
void mul_m3_v3(const float M[3][3], float r[3])
float WM_event_tablet_data(const wmEvent *event, bool *r_pen_flip, float r_tilt[2])
int RNA_enum_get(PointerRNA *ptr, const char *name)
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
Definition BLI_hash_c.hh:53
void copy_m3_m4(float m1[3][3], const float m2[4][4])
bool BKE_base_is_visible(const View3D *v3d, const Base *base)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void WM_cursor_modal_set(wmWindow *win, int val)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
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)
wmWindow * CTX_wm_window(const bContext *C)
PropertyRNA * RNA_def_collection_runtime(StructOrFunctionRNA *cont, const char *identifier, StructRNA *type, const char *ui_name, const char *ui_description)
Base * CTX_data_active_base(const bContext *C)
MINLINE float pow2f(float x)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void mul_m4_v3(const float M[4][4], float r[3])
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
void RNA_int_set_array(PointerRNA *ptr, const char *name, const int *values)
VecBase< float, 2 > float2
BMesh const char void * data
void RNA_int_get_array(PointerRNA *ptr, const char *name, int *values)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
void WM_cursor_modal_restore(wmWindow *win)
ToolSettings * CTX_data_tool_settings(const bContext *C)
MatBase< float, 3, 3 > float3x3
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
MINLINE float normalize_v3_v3(float r[3], const float a[3])
@ BRUSH_AUTOMASKING_CAVITY_ALL
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
@ WM_CURSOR_EW_SCROLL
Definition wm_cursors.hh:56
float paint_calc_object_space_radius(const ViewContext &vc, const float3 &center, float pixel_radius)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
PropertyRNA * RNA_def_int_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
PropertyRNA * RNA_def_enum_flag(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
Scene * CTX_data_scene(const bContext *C)
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_BLOCKING
Definition WM_types.hh:192
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
@ OPTYPE_GRAB_CURSOR_X
Definition WM_types.hh:198
void copy_m4_m4(float m1[4][4], const float m2[4][4])
VecBase< float, 3 > float3
void RNA_collection_add(PointerRNA *ptr, const char *name, PointerRNA *r_value)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Object * CTX_data_active_object(const bContext *C)
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
float RNA_float_get(PointerRNA *ptr, const char *name)
View3D * CTX_wm_view3d(const bContext *C)
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
int RNA_collection_length(PointerRNA *ptr, const char *name)
#define hash
Definition noise_c.cc:156
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
CustomData pdata
const c_style_mat & ptr() const
bke::ObjectRuntime * runtime
struct UnifiedPaintSettings unified_paint_settings
std::unique_ptr< SculptBoundaryInfoCache > boundary_info_cache
Definition BKE_paint.hh:458
ed::sculpt_paint::filter::Cache * filter_cache
Definition BKE_paint.hh:410
int to_index(const CCGKey &key) const
SubdivCCGNeighborCoords coords
Array< float3 > positions
RegionView3D * rv3d
Definition ED_view3d.hh:86
std::unique_ptr< auto_mask::Cache > automasking
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & row(bool align)
int prev_press_xy[2]
Definition WM_types.hh:890
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
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