Blender V5.3
mesh/sculpt_smooth.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_smooth.hh"
9
11#include "BLI_math_base.hh"
12#include "BLI_math_geom_c.hh"
13#include "BLI_math_vector.hh"
14#include "BLI_math_vector_c.hh"
15
16#include "BKE_attribute_math.hh"
17#include "BKE_mesh.hh"
18#include "BKE_object_types.hh"
19#include "BKE_paint.hh"
20#include "BKE_paint_bvh.hh"
21#include "BKE_subdiv_ccg.hh"
22
23#include "mesh_brush_common.hh"
24#include "sculpt_automask.hh"
25#include "sculpt_boundary.hh"
26#include "sculpt_color.hh"
27#include "sculpt_face_set.hh"
28#include "sculpt_hide.hh"
29#include "sculpt_intern.hh"
30
31#include "bmesh.hh"
32
33#include <cstdlib>
34
36
37template<typename T>
39 const Span<int> verts,
40 const GroupedSpan<int> vert_neighbors,
41 const MutableSpan<T> dst)
42{
44 BLI_assert(verts.size() == dst.size());
45 BLI_assert(vert_neighbors.size() == dst.size());
46
47 for (const int i : vert_neighbors.index_range()) {
48 const Span<int> neighbors = vert_neighbors[i];
49 if (neighbors.is_empty()) {
50 dst[i] = src[verts[i]];
51 }
52 else {
53 dst[i] = bke::attribute_math::mix_indices(src, neighbors);
54 }
55 }
56}
57
66
67template<typename T>
69 const GroupedSpan<int> vert_neighbors,
70 const MutableSpan<T> dst)
71{
73 /* TODO: Use #bke::attribute_math::mix_groups when we can control the internal threading. */
74 BLI_assert(vert_neighbors.size() == dst.size());
75
76 for (const int i : vert_neighbors.index_range()) {
77 dst[i] = bke::attribute_math::mix_indices(src, vert_neighbors[i]);
78 }
79}
80
88
90 const Span<float3> positions,
91 const Span<SubdivCCGCoord> coords)
92{
94 const float factor = math::rcp(float(coords.size()));
95 float3 result(0);
96 for (const SubdivCCGCoord coord : coords) {
97 result += positions[coord.to_index(key)] * factor;
98 }
99 return result;
100}
101
102template<bool use_factors>
104 const Span<int> corner_verts,
105 const BitSpan boundary_verts,
106 const Set<OrderedEdge> &boundary_edges,
107 const SubdivCCG &subdiv_ccg,
108 const Span<int> grids,
109 const Span<float> factors,
110 const MutableSpan<float3> new_positions)
111{
113 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
114 const Span<float3> positions = subdiv_ccg.positions;
115
116 BLI_assert(grids.size() * key.grid_area == new_positions.size());
117 if constexpr (use_factors) {
118 BLI_assert(new_positions.size() == factors.size());
119 }
120
121 for (const int i : grids.index_range()) {
122 const int node_verts_start = i * key.grid_area;
123 const int grid = grids[i];
124 const IndexRange grid_range = bke::ccg::grid_range(key, grid);
125
126 /* TODO: This loop could be optimized in the future by skipping unnecessary logic for
127 * non-boundary grid vertices. */
128 for (const int y : IndexRange(key.grid_size)) {
129 for (const int x : IndexRange(key.grid_size)) {
130 const int offset = CCG_grid_xy_to_index(key.grid_size, x, y);
131 const int node_vert_index = node_verts_start + offset;
132 const int vert = grid_range[offset];
133
134 if constexpr (use_factors) {
135 if (factors[node_vert_index] == 0.0f) {
136 new_positions[node_vert_index] = positions[vert];
137 continue;
138 }
139 }
140
141 SubdivCCGCoord coord{};
142 coord.grid_index = grid;
143 coord.x = x;
144 coord.y = y;
145
146 SubdivCCGNeighbors neighbors;
147 BKE_subdiv_ccg_neighbor_coords_get(subdiv_ccg, coord, false, neighbors);
148
150 faces, corner_verts, boundary_verts, boundary_edges, subdiv_ccg, coord))
151 {
152 if (neighbors.coords.size() == 2) {
153 /* Do not include neighbors of corner vertices. */
154 neighbors.coords.clear();
155 }
156 else {
157 /* Only include other boundary vertices as neighbors of boundary vertices. */
158 neighbors.coords.remove_if([&](const SubdivCCGCoord coord) {
160 faces, corner_verts, boundary_verts, boundary_edges, subdiv_ccg, coord);
161 });
162 }
163 }
164
165 if (neighbors.coords.is_empty()) {
166 new_positions[node_vert_index] = positions[vert];
167 }
168 else {
169 new_positions[node_vert_index] = average_positions(key, positions, neighbors.coords);
170 }
171 }
172 }
173 }
174}
175
177 const Span<int> corner_verts,
178 const BitSpan boundary_verts,
179 const Set<OrderedEdge> &boundary_edges,
180 const SubdivCCG &subdiv_ccg,
181 const Span<int> grids,
182 const MutableSpan<float3> new_positions)
183{
185 faces, corner_verts, boundary_verts, boundary_edges, subdiv_ccg, grids, {}, new_positions);
186}
187
189 const Span<int> corner_verts,
190 const BitSpan boundary_verts,
191 const Set<OrderedEdge> &boundary_edges,
192 const SubdivCCG &subdiv_ccg,
193 const Span<int> grids,
194 const Span<float> factors,
195 const MutableSpan<float3> new_positions)
196{
198 corner_verts,
199 boundary_verts,
200 boundary_edges,
201 subdiv_ccg,
202 grids,
203 factors,
204 new_positions);
205}
206
207template<typename T>
208void average_data_grids(const SubdivCCG &subdiv_ccg,
209 const Span<T> src,
210 const Span<int> grids,
211 const MutableSpan<T> dst)
212{
214 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
215
216 BLI_assert(grids.size() * key.grid_area == dst.size());
217
218 for (const int i : grids.index_range()) {
219 const int grid = grids[i];
220 const int node_verts_start = i * key.grid_area;
221
222 /* TODO: This loop could be optimized in the future by skipping unnecessary logic for
223 * non-boundary grid vertices. */
224 for (const int y : IndexRange(key.grid_size)) {
225 for (const int x : IndexRange(key.grid_size)) {
226 const int offset = CCG_grid_xy_to_index(key.grid_size, x, y);
227 const int node_vert_index = node_verts_start + offset;
228
229 SubdivCCGCoord coord{};
230 coord.grid_index = grid;
231 coord.x = x;
232 coord.y = y;
233
234 SubdivCCGNeighbors neighbors;
235 BKE_subdiv_ccg_neighbor_coords_get(subdiv_ccg, coord, false, neighbors);
236
237 T sum{};
238 for (const SubdivCCGCoord neighbor : neighbors.coords) {
239 sum += src[neighbor.to_index(key)];
240 }
241 dst[node_vert_index] = math::safe_divide(sum, float(neighbors.coords.size()));
242 }
243 }
244 }
245}
246
247template<typename T>
248void average_data_bmesh(const Span<T> src, const Set<BMVert *, 0> &verts, const MutableSpan<T> dst)
249{
251 BMeshNeighborVerts neighbor_data;
252
253 int i = 0;
254 for (BMVert *vert : verts) {
255 T sum{};
256 const Span<BMVert *> neighbors = vert_neighbors_get_bmesh(*vert, neighbor_data);
257 for (const BMVert *neighbor : neighbors) {
258 sum += src[BM_elem_index_get(neighbor)];
259 }
260 dst[i] = math::safe_divide(sum, float(neighbors.size()));
261 i++;
262 }
263}
264
267 Span<int>,
271 Span<int>,
274 const Set<BMVert *, 0> &,
277 const Set<BMVert *, 0> &,
279
281{
283 const float factor = math::safe_rcp(float(verts.size()));
284 float3 result(0);
285 for (const BMVert *vert : verts) {
286 result += float3(vert->co) * factor;
287 }
288 return result;
289}
290
292 const MutableSpan<float3> new_positions)
293{
295 BLI_assert(verts.size() == new_positions.size());
296 BMeshNeighborVerts neighbor_data;
297
298 int i = 0;
299 for (BMVert *vert : verts) {
300 const Span<BMVert *> neighbors = vert_neighbors_get_bmesh(*vert, neighbor_data);
301 new_positions[i] = average_positions(neighbors);
302 i++;
303 }
304}
305
306template<bool use_factors>
308 const Span<float> factors,
309 const MutableSpan<float3> new_positions)
310{
312 BLI_assert(verts.size() == new_positions.size());
313 if constexpr (use_factors) {
314 BLI_assert(new_positions.size() == factors.size());
315 }
316 BMeshNeighborVerts neighbor_data;
317
318 int i = 0;
319 for (BMVert *vert : verts) {
320 if constexpr (use_factors) {
321 if (factors[i] == 0.0f) {
322 new_positions[i] = float3(vert->co);
323 i++;
324 continue;
325 }
326 }
327
328 const Span<BMVert *> neighbors = vert_neighbors_get_interior_bmesh(*vert, neighbor_data);
329 if (neighbors.is_empty()) {
330 new_positions[i] = float3(vert->co);
331 }
332 else {
333 new_positions[i] = average_positions(neighbors);
334 }
335 i++;
336 }
337}
339 const Span<float> factors,
340 const MutableSpan<float3> new_positions)
341{
342 neighbor_position_average_interior_bmesh_impl<true>(verts, factors, new_positions);
343}
344
346 const MutableSpan<float3> new_positions)
347{
349}
350
351void bmesh_four_neighbor_average(float avg[3], const float3 &direction, const BMVert *v)
352{
353 float avg_co[3] = {0.0f, 0.0f, 0.0f};
354 float tot_co = 0.0f;
355
356 BMIter eiter;
357 BMEdge *e;
358
359 BM_ITER_ELEM (e, &eiter, const_cast<BMVert *>(v), BM_EDGES_OF_VERT) {
360 if (BM_edge_is_boundary(e)) {
361 copy_v3_v3(avg, v->co);
362 return;
363 }
364 BMVert *v_other = (e->v1 == v) ? e->v2 : e->v1;
365 float vec[3];
366 sub_v3_v3v3(vec, v_other->co, v->co);
367 madd_v3_v3fl(vec, v->no, -dot_v3v3(vec, v->no));
368 normalize_v3(vec);
369
370 /* fac is a measure of how orthogonal or parallel the edge is
371 * relative to the direction. */
372 float fac = dot_v3v3(vec, direction);
373 fac = fac * fac - 0.5f;
374 fac *= fac;
375 madd_v3_v3fl(avg_co, v_other->co, fac);
376 tot_co += fac;
377 }
378
379 /* In case vert has no Edge s. */
380 if (tot_co > 0.0f) {
381 mul_v3_v3fl(avg, avg_co, 1.0f / tot_co);
382
383 /* Preserve volume. */
384 float vec[3];
385 sub_v3_v3(avg, v->co);
386 mul_v3_v3fl(vec, v->no, dot_v3v3(avg, v->no));
387 sub_v3_v3(avg, vec);
388 add_v3_v3(avg, v->co);
389 }
390 else {
391 zero_v3(avg);
392 }
393}
394
396 const Span<int> corner_verts,
397 const GroupedSpan<int> vert_to_face_map,
398 const GSpan color_attribute,
399 const bke::AttrDomain color_domain,
400 const GroupedSpan<int> vert_neighbors,
401 const MutableSpan<float4> smooth_colors)
402{
404 BLI_assert(vert_neighbors.size() == smooth_colors.size());
405
406 for (const int i : vert_neighbors.index_range()) {
407 float4 sum(0);
408 const Span<int> neighbors = vert_neighbors[i];
409 for (const int vert : neighbors) {
411 faces, corner_verts, vert_to_face_map, color_attribute, color_domain, vert);
412 }
413 smooth_colors[i] = math::safe_divide(sum, float(neighbors.size()));
414 }
415}
416
417/* HC Smooth Algorithm. */
418/* From: Improved Laplacian Smoothing of Noisy Surface Meshes */
419
421 const Span<float3> orig_positions,
423 const float alpha,
424 MutableSpan<float3> laplacian_disp,
425 MutableSpan<float3> translations)
426{
428 BLI_assert(positions.size() == orig_positions.size());
429 BLI_assert(positions.size() == average_positions.size());
430 BLI_assert(positions.size() == laplacian_disp.size());
431 BLI_assert(positions.size() == translations.size());
432
433 for (const int i : average_positions.index_range()) {
434 const float3 weighted_o = orig_positions[i] * alpha;
435 const float3 weighted_q = positions[i] * (1.0f - alpha);
436 const float3 d = weighted_o + weighted_q;
437 laplacian_disp[i] = average_positions[i] - d;
438 translations[i] = average_positions[i] - positions[i];
439 }
440}
441
443 const Span<float3> average_laplacian_disp,
444 const float beta,
445 const MutableSpan<float3> translations)
446{
448 BLI_assert(laplacian_disp.size() == average_laplacian_disp.size());
449 BLI_assert(laplacian_disp.size() == translations.size());
450
451 for (const int i : laplacian_disp.index_range()) {
452 float3 b_current_vert = average_laplacian_disp[i] * (1.0f - beta);
453 b_current_vert += laplacian_disp[i] * beta;
454 translations[i] = -b_current_vert;
455 }
456}
457
458static float3 translation_to_plane(const float3 &current_position,
459 const float3 &normal,
460 const float3 &smoothed_position)
461{
462 float4 plane;
463 plane_from_point_normal_v3(plane, current_position, normal);
464
465 float3 smooth_closest_plane;
466 closest_to_plane_v3(smooth_closest_plane, plane, smoothed_position);
467
468 return smooth_closest_plane - current_position;
469}
470
471static float3 calc_boundary_normal_corner(const float3 &current_position,
472 const Span<float3> vert_positions,
473 const Span<int> neighbors)
474{
476 float3 normal(0);
477 for (const int vert : neighbors) {
478 const float3 to_neighbor = vert_positions[vert] - current_position;
479 normal += math::normalize(to_neighbor);
480 }
481 return math::normalize(normal);
482}
483
485 const Span<float3> positions,
486 const float3 &current_position,
487 const Span<SubdivCCGCoord> neighbors)
488{
490 float3 normal(0);
491 for (const SubdivCCGCoord &coord : neighbors) {
492 const float3 to_neighbor = positions[coord.to_index(key)] - current_position;
493 normal += math::normalize(to_neighbor);
494 }
495 return math::normalize(normal);
496}
497
498static float3 calc_boundary_normal_corner(const float3 &current_position,
499 const Span<BMVert *> neighbors)
500{
502 float3 normal(0);
503 for (BMVert *vert : neighbors) {
504 const float3 neighbor_pos = vert->co;
505 const float3 to_neighbor = neighbor_pos - current_position;
506 normal += math::normalize(to_neighbor);
507 }
508 return math::normalize(normal);
509}
510
512 const Span<float3> vert_normals,
513 const OffsetIndices<int> faces,
514 const Span<int> corner_verts,
515 const GroupedSpan<int> vert_to_face_map,
516 const BitSpan boundary_verts,
517 const Set<OrderedEdge> &boundary_edges,
518 const Span<int> face_sets,
519 const Span<bool> hide_poly,
520 const bool filter_boundary_face_sets,
521 const Span<int> verts,
522 const Span<float> factors,
523 const MutableSpan<float3> translations)
524{
526 BLI_assert(verts.size() == factors.size());
527 BLI_assert(verts.size() == translations.size());
528
529 Vector<int> neighbors;
530
531 for (const int i : verts.index_range()) {
532 if (factors[i] == 0.0f) {
533 translations[i] = float3(0);
534 continue;
535 }
536
537 vert_neighbors_get_mesh(faces, corner_verts, vert_to_face_map, hide_poly, verts[i], neighbors);
538
539 /* Don't modify corner vertices */
540 if (neighbors.size() <= 2) {
541 translations[i] = float3(0);
542 continue;
543 }
544
545 const bool is_boundary = boundary_verts[verts[i]];
546 if (is_boundary) {
547 neighbors.remove_if(
548 [&](const int vert) { return !boundary_edges.contains(OrderedEdge(vert, verts[i])); });
549 }
550
551 if (filter_boundary_face_sets) {
552 neighbors.remove_if([&](const int vert) {
553 return face_set::vert_has_unique_face_set(vert_to_face_map, face_sets, vert);
554 });
555 }
556
557 if (neighbors.is_empty()) {
558 translations[i] = float3(0);
559 continue;
560 }
561
562 const float3 smoothed_position = bke::attribute_math::mix_indices(vert_positions, neighbors);
563
564 /* Normal Calculation */
565 float3 normal;
566 if (is_boundary && neighbors.size() == 2) {
567 normal = calc_boundary_normal_corner(vert_positions[verts[i]], vert_positions, neighbors);
568 if (math::is_zero(normal)) {
569 translations[i] = float3(0);
570 continue;
571 }
572 }
573 else {
574 normal = vert_normals[verts[i]];
575 }
576
577 const float3 translation = translation_to_plane(
578 vert_positions[verts[i]], normal, smoothed_position);
579
580 translations[i] = translation * factors[i];
581 }
582}
583
585 const OffsetIndices<int> faces,
586 const Span<int> corner_verts,
587 const Span<int> face_sets,
588 const GroupedSpan<int> vert_to_face_map,
589 const BitSpan boundary_verts,
590 const Set<OrderedEdge> &boundary_edges,
591 const Span<int> grids,
592 const bool filter_boundary_face_sets,
593 const Span<float> factors,
594 const MutableSpan<float3> translations)
595{
597 const Span<float3> positions = subdiv_ccg.positions;
598 const Span<float3> normals = subdiv_ccg.normals;
599 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
600
601 for (const int i : grids.index_range()) {
602 const IndexRange grid_range = bke::ccg::grid_range(key, grids[i]);
603 const int node_start = i * key.grid_area;
604 for (const int y : IndexRange(key.grid_size)) {
605 for (const int x : IndexRange(key.grid_size)) {
606 const int offset = CCG_grid_xy_to_index(key.grid_size, x, y);
607 const int node_vert = node_start + offset;
608 const int vert = grid_range[offset];
609 if (factors[node_vert] == 0.0f) {
610 translations[node_vert] = float3(0);
611 continue;
612 }
613
614 SubdivCCGCoord coord{};
615 coord.grid_index = grids[i];
616 coord.x = x;
617 coord.y = y;
618
619 SubdivCCGNeighbors neighbor_storage;
620 BKE_subdiv_ccg_neighbor_coords_get(subdiv_ccg, coord, false, neighbor_storage);
621 SubdivCCGNeighborCoords &neighbors = neighbor_storage.coords;
622
623 /* Don't modify corner vertices */
624 if (neighbors.size() <= 2) {
625 translations[node_vert] = float3(0);
626 continue;
627 }
628
629 const bool is_boundary = boundary::vert_is_boundary(
630 faces, corner_verts, boundary_verts, boundary_edges, subdiv_ccg, coord);
631
632 if (is_boundary) {
633 neighbors.remove_if([&](const SubdivCCGCoord neighbor) {
635 faces, corner_verts, boundary_verts, boundary_edges, subdiv_ccg, neighbor);
636 });
637 }
638
639 if (filter_boundary_face_sets) {
640 neighbors.remove_if([&](const SubdivCCGCoord neighbor) {
642 faces, corner_verts, vert_to_face_map, face_sets, subdiv_ccg, neighbor);
643 });
644 }
645
646 if (neighbors.is_empty()) {
647 translations[node_vert] = float3(0);
648 continue;
649 }
650
651 const float3 smoothed_position = average_positions(key, positions, neighbors);
652
653 /* Normal Calculation */
654 float3 normal;
655 if (is_boundary && neighbors.size() == 2) {
656 normal = calc_boundary_normal_corner(key, positions, positions[vert], neighbors);
657 if (math::is_zero(normal)) {
658 translations[node_vert] = float3(0);
659 continue;
660 }
661 }
662 else {
663 normal = normals[vert];
664 }
665
666 const float3 translation = translation_to_plane(
667 positions[vert], normal, smoothed_position);
668
669 translations[node_vert] = translation * factors[node_vert];
670 }
671 }
672 }
673}
674
676 const Span<float3> positions,
677 const int face_set_offset,
678 const bool filter_boundary_face_sets,
679 const Span<float> factors,
680 const MutableSpan<float3> translations)
681{
683 BLI_assert(verts.size() == factors.size());
684 BLI_assert(verts.size() == translations.size());
685
686 BMeshNeighborVerts neighbors;
687
688 int i = 0;
689 for (BMVert *vert : verts) {
690 if (factors[i] == 0.0f) {
691 translations[i] = float3(0);
692 i++;
693 continue;
694 }
695
696 vert_neighbors_get_bmesh(*vert, neighbors);
697
698 /* Don't modify corner vertices */
699 if (neighbors.size() <= 2) {
700 translations[i] = float3(0);
701 i++;
702 continue;
703 }
704
705 const bool is_boundary = BM_vert_is_boundary(vert);
706 if (is_boundary) {
707 neighbors.remove_if([&](const BMVert *vert) { return !BM_vert_is_boundary(vert); });
708 }
709
710 if (filter_boundary_face_sets) {
711 neighbors.remove_if([&](const BMVert *vert) {
712 return face_set::vert_has_unique_face_set(face_set_offset, *vert);
713 });
714 }
715
716 if (neighbors.is_empty()) {
717 translations[i] = float3(0);
718 i++;
719 continue;
720 }
721
722 const float3 smoothed_position = average_positions(neighbors);
723
724 /* Normal Calculation */
725 float3 normal;
726 if (is_boundary && neighbors.size() == 2) {
727 normal = calc_boundary_normal_corner(positions[i], neighbors);
728 if (math::is_zero(normal)) {
729 translations[i] = float3(0);
730 i++;
731 continue;
732 }
733 }
734 else {
735 normal = vert->no;
736 }
737
738 const float3 translation = translation_to_plane(positions[i], normal, smoothed_position);
739
740 translations[i] = translation * factors[i];
741 i++;
742 }
743}
744
746 const int iterations,
748{
750 struct LocalData {
751 Vector<int> vert_indices;
752 Vector<int> neighbor_offsets;
753 Vector<int> neighbor_data;
754 Vector<float> new_factors;
755 };
756 const SculptSession &ss = *object.runtime->sculpt_session;
758 IndexMaskMemory memory;
759 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
760
762 switch (pbvh.type()) {
765
766 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
767 const OffsetIndices faces = mesh.faces();
768 const Span<int> corner_verts = mesh.corner_verts();
769 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
770 const bke::AttributeAccessor attributes = mesh.attributes();
771 const VArraySpan<bool> hide_vert = *attributes.lookup<bool>(".hide_vert",
773 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly",
775 for ([[maybe_unused]] const int _ : IndexRange(iterations)) {
776 node_mask.foreach_index(
777 [&](const int i) {
778 LocalData &tls = all_tls.local();
780 const Span<int> verts = hide::node_visible_verts(nodes[i], hide_vert, index_data);
781
782 const GroupedSpan<int> neighbors = calc_vert_neighbors(faces,
783 corner_verts,
784 vert_to_face_map,
785 hide_poly,
786 verts,
787 tls.neighbor_offsets,
788 tls.neighbor_data);
789
792 data.as_span(), neighbors, new_factors.as_mutable_span());
793
794 scatter_data_mesh(new_factors.as_span(), verts, data);
795 },
797 }
798 break;
799 }
802 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
803 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
804 const BitGroupVector<> &grid_hidden = subdiv_ccg.grid_hidden;
805 for ([[maybe_unused]] const int _ : IndexRange(iterations)) {
806 node_mask.foreach_index(
807 [&](const int node_index) {
808 LocalData &tls = all_tls.local();
809 const Span<int> grids = nodes[node_index].grids();
810 const int grid_verts_num = key.grid_area * grids.size();
811
812 tls.new_factors.resize(grid_verts_num);
813 const MutableSpan<float> new_factors = tls.new_factors;
814 smooth::average_data_grids(subdiv_ccg, data.as_span(), grids, new_factors);
815
816 if (grid_hidden.is_empty()) {
817 scatter_data_grids(subdiv_ccg, new_factors.as_span(), grids, data);
818 }
819 else {
820 for (const int i : grids.index_range()) {
821 const int node_start = i * key.grid_area;
823 key, grid_hidden, grids[i], [&](const int offset) {
824 data[i] = new_factors[node_start + offset];
825 });
826 }
827 }
828 },
830 }
831 break;
832 }
835 for ([[maybe_unused]] const int _ : IndexRange(iterations)) {
836 node_mask.foreach_index(
837 [&](const int node_index) {
838 LocalData &tls = all_tls.local();
840 const_cast<bke::pbvh::BMeshNode *>(&nodes[node_index]));
841
842 tls.new_factors.resize(verts.size());
843 const MutableSpan<float> new_factors = tls.new_factors;
844 smooth::average_data_bmesh(data.as_span(), verts, new_factors);
845
846 int i = 0;
847 for (const BMVert *vert : verts) {
849 i++;
850 continue;
851 }
852 data[BM_elem_index_get(vert)] = new_factors[i];
853 i++;
854 }
855 },
857 }
858 break;
859 }
860 }
861}
862
863} // namespace blender::ed::sculpt_paint::smooth
A BVH for high poly meshes.
#define PRF_scope(category)
#define BM_elem_index_get(ele)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
int64_t size() const
Definition BLI_set.hh:606
bool contains(const Key &key) const
Definition BLI_set.hh:310
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
int64_t remove_if(Predicate &&predicate)
bool is_empty() const
GAttributeReader lookup(const StringRef name) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
ccl_device_inline float beta(const float x, const float y)
Definition math_base.h:708
#define T
T mix_indices(const Span< T > src, const Span< int > indices)
IndexRange grid_range(const int grid_area, const int grid)
pbvh::Tree * pbvh_get(Object &object)
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
bool vert_is_boundary(const GroupedSpan< int > vert_to_face_map, const Span< bool > hide_poly, const BitSpan boundary_verts, const int vert)
Definition sculpt.cc:620
float4 color_vert_get(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, GSpan color_attribute, bke::AttrDomain color_domain, int vert)
bool vert_has_unique_face_set(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, int vert)
Definition sculpt.cc:324
Span< int > node_visible_verts(const bke::pbvh::MeshNode &node, const Span< bool > hide_vert, Vector< int, bke::pbvh::MESH_LEAF_LIMIT > &indices)
static void neighbor_position_average_interior_bmesh_impl(const Set< BMVert *, 0 > &verts, const Span< float > factors, const MutableSpan< float3 > new_positions)
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)
static float3 translation_to_plane(const float3 &current_position, const float3 &normal, const float3 &smoothed_position)
static float3 calc_boundary_normal_corner(const float3 &current_position, const Span< float3 > vert_positions, const Span< int > neighbors)
void neighbor_data_average_mesh(const Span< T > src, const GroupedSpan< int > vert_neighbors, const MutableSpan< T > dst)
void neighbor_color_average(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const GSpan color_attribute, const bke::AttrDomain color_domain, const GroupedSpan< int > vert_neighbors, const MutableSpan< float4 > smooth_colors)
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)
static float3 average_positions(const CCGKey &key, const Span< float3 > positions, const Span< SubdivCCGCoord > coords)
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 bmesh_four_neighbor_average(float avg[3], const float3 &direction, const BMVert *v)
void average_data_grids(const SubdivCCG &subdiv_ccg, const Span< T > src, const Span< int > grids, const MutableSpan< T > dst)
void blur_geometry_data_array(const Object &object, const int iterations, const MutableSpan< float > data)
static void neighbor_position_average_interior_grids_impl(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 Span< float > factors, const MutableSpan< float3 > new_positions)
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)
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
Vector< BMVert *, 64 > BMeshNeighborVerts
void scatter_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
Span< BMVert * > vert_neighbors_get_interior_bmesh(BMVert &vert, BMeshNeighborVerts &r_neighbors)
Definition sculpt.cc:505
Span< BMVert * > vert_neighbors_get_bmesh(BMVert &vert, BMeshNeighborVerts &r_neighbors)
Definition sculpt.cc:490
Span< int > vert_neighbors_get_mesh(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face, const Span< bool > hide_poly, const int vert, Vector< int > &r_neighbors)
Definition sculpt.cc:534
void 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 safe_divide(const T &a, const T &b)
bool is_zero(const T &a)
T rcp(const T &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void closest_to_plane_v3(float r_close[3], const float plane[4], const float pt[3])
Definition math_geom.cc:451
MINLINE void copy_v3_v3(float r[3], const float a[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
bool BM_vert_is_boundary(const BMVert *v)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
MINLINE void sub_v3_v3(float r[3], const float a[3])
VecBase< float, 4 > float4
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
BMesh const char void * data
Vector< SubdivCCGCoord, 256 > SubdivCCGNeighborCoords
BLI_INLINE bool BM_edge_is_boundary(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
MINLINE void zero_v3(float r[3])
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void BKE_subdiv_ccg_foreach_visible_grid_vert(const CCGKey &key, const BitGroupVector<> &grid_hidden, const int grid, const Fn &fn)
VecBase< float, 3 > float3
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
OrderedEdge(const int v1, const int v2)
int to_index(const CCGKey &key) const
SubdivCCGNeighborCoords coords
Array< float3 > positions
Array< float3 > normals