Blender V5.3
smooth.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "DNA_brush_types.h"
12#include "DNA_mesh_types.h"
13#include "DNA_object_types.h"
14#include "DNA_scene_types.h"
15
16#include "BKE_mesh.hh"
17#include "BKE_object_types.hh"
18#include "BKE_paint.hh"
19#include "BKE_paint_bvh.hh"
20#include "BKE_subdiv_ccg.hh"
21
22#include "BLI_array.hh"
24#include "BLI_task.hh"
25
31
32#include "bmesh.hh"
33
35
36inline namespace smooth_cc {
37
38static Vector<float> iteration_strengths(const float strength)
39{
40 constexpr int max_iterations = 4;
41
42 BLI_assert_msg(strength >= 0.0f,
43 "The smooth brush expects a non-negative strength to behave properly");
44 const float clamped_strength = std::min(strength, 1.0f);
45
46 const int count = int(clamped_strength * max_iterations);
47 const float last = max_iterations * (clamped_strength - float(count) / max_iterations);
49 result.append_n_times(1.0f, count);
50 result.append(last);
51 return result;
52}
53
63
66 Object &object,
67 LocalData &tls,
68 const Span<float> factors,
69 const Span<float3> new_positions,
70 const PositionDeformData &position_data)
71{
73 SculptSession &ss = *object.runtime->sculpt_session;
74
75 const Span<int> verts = node.verts();
76
77 tls.translations.resize(verts.size());
78 const MutableSpan<float3> translations = tls.translations;
79 translations_from_new_positions(new_positions, verts, position_data.eval, translations);
80 scale_translations(translations, factors);
81
82 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
83 position_data.deform(translations, verts);
84}
85
86BLI_NOINLINE static void do_smooth_brush_mesh(const Depsgraph &depsgraph,
87 const Sculpt &sd,
88 const Brush &brush,
89 Object &object,
90 const IndexMask &node_mask,
91 const float brush_strength)
92{
93 const SculptSession &ss = *object.runtime->sculpt_session;
96 Mesh &mesh = *id_cast<Mesh *>(object.data);
97 const OffsetIndices faces = mesh.faces();
98 const Span<int> corner_verts = mesh.corner_verts();
99 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
100 const MeshAttributeData attribute_data(mesh);
101
102 const PositionDeformData position_data(depsgraph, object);
103 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
104
105 Array<int> node_offset_data;
106 const OffsetIndices<int> node_vert_offsets = create_node_vert_offsets(
107 nodes, node_mask, node_offset_data);
108 Array<float3> new_positions(node_vert_offsets.total_size());
109 Array<float> all_factors(node_vert_offsets.total_size());
110 Array<float> all_distances(node_vert_offsets.total_size());
111
113
114 /* Calculate the new positions into a separate array in a separate loop because multiple loops
115 * are updated in parallel. Without this there would be non-threadsafe access to changing
116 * positions in other bke::pbvh::Tree nodes. */
117 for (const float strength : iteration_strengths(brush_strength)) {
118 node_mask.foreach_index(
119 [&](const int i, const int pos) {
120 LocalData &tls = all_tls.local();
121 const Span<int> verts = nodes[i].verts();
122 const MutableSpan<float> node_factors = all_factors.as_mutable_span().slice(
123 node_vert_offsets[pos]);
125 depsgraph,
126 brush,
127 object,
128 attribute_data,
129 position_data.eval,
130 vert_normals,
131 nodes[i],
132 node_factors,
133 all_distances.as_mutable_span().slice(node_vert_offsets[pos]));
134 scale_factors(node_factors, strength);
136 faces,
137 corner_verts,
138 vert_to_face_map,
139 ss.boundary_info_cache->verts,
140 ss.boundary_info_cache->edges,
141 attribute_data.hide_poly,
142 verts,
143 node_factors,
144 tls.neighbor_offsets,
145 tls.neighbor_data);
147 position_data.eval,
148 verts,
149 neighbors,
150 new_positions.as_mutable_span().slice(node_vert_offsets[pos]));
151 },
153
154 node_mask.foreach_index(
155 [&](const int i, const int pos) {
156 LocalData &tls = all_tls.local();
158 nodes[i],
159 object,
160 tls,
161 all_factors.as_mutable_span().slice(node_vert_offsets[pos]),
162 new_positions.as_span().slice(node_vert_offsets[pos]),
163 position_data);
164 },
166 }
167}
168
169static void calc_grids(const Depsgraph &depsgraph,
170 const Sculpt &sd,
171 const OffsetIndices<int> faces,
172 const Span<int> corner_verts,
173 const BitSpan boundary_verts,
174 const Set<OrderedEdge> &boundary_edges,
175 Object &object,
176 const Brush &brush,
177 const float strength,
179 LocalData &tls)
180{
181 SculptSession &ss = *object.runtime->sculpt_session;
182 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
183
184 const Span<int> grids = node.grids();
185 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
186
187 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
188
189 scale_factors(tls.factors, strength);
190
191 tls.new_positions.resize(positions.size());
192 const MutableSpan<float3> new_positions = tls.new_positions;
194 corner_verts,
195 boundary_verts,
196 boundary_edges,
197 subdiv_ccg,
198 grids,
199 tls.factors,
200 new_positions);
201
202 tls.translations.resize(positions.size());
203 const MutableSpan<float3> translations = tls.translations;
204 translations_from_new_positions(new_positions, positions, translations);
205 scale_translations(translations, tls.factors);
206
207 clip_and_lock_translations(sd, ss, positions, translations);
208 apply_translations(translations, grids, subdiv_ccg);
209}
210
211static void calc_bmesh(const Depsgraph &depsgraph,
212 const Sculpt &sd,
213 Object &object,
214 const Brush &brush,
215 const float strength,
217 LocalData &tls)
218{
219 SculptSession &ss = *object.runtime->sculpt_session;
220
222 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
223
224 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
225
226 scale_factors(tls.factors, strength);
227
228 tls.new_positions.resize(verts.size());
229 const MutableSpan<float3> new_positions = tls.new_positions;
231
232 tls.translations.resize(verts.size());
233 const MutableSpan<float3> translations = tls.translations;
234 translations_from_new_positions(new_positions, positions, translations);
235 scale_translations(translations, tls.factors);
236
237 clip_and_lock_translations(sd, ss, positions, translations);
238 apply_translations(translations, verts);
239}
240
241} // namespace smooth_cc
242
243void do_smooth_brush(const Depsgraph &depsgraph,
244 const Sculpt &sd,
245 Object &object,
246 const IndexMask &node_mask,
247 const float brush_strength)
248{
250 SculptSession &ss = *object.runtime->sculpt_session;
253
255
256 switch (pbvh.type()) {
258 do_smooth_brush_mesh(depsgraph, sd, brush, object, node_mask, brush_strength);
259 break;
261 const Mesh &base_mesh = *id_cast<const Mesh *>(object.data);
262 const OffsetIndices faces = base_mesh.faces();
263 const Span<int> corner_verts = base_mesh.corner_verts();
264
266 for (const float strength : iteration_strengths(brush_strength)) {
268 node_mask.foreach_index(
269 [&](const int i) {
270 LocalData &tls = all_tls.local();
271 calc_grids(depsgraph,
272 sd,
273 faces,
274 corner_verts,
275 ss.boundary_info_cache->verts,
276 ss.boundary_info_cache->edges,
277 object,
278 brush,
279 strength,
280 nodes[i],
281 tls);
282 },
284 }
285 break;
286 }
290 for (const float strength : iteration_strengths(brush_strength)) {
292 node_mask.foreach_index(
293 [&](const int i) {
294 LocalData &tls = all_tls.local();
295 calc_bmesh(depsgraph, sd, object, brush, strength, nodes[i], tls);
296 },
298 }
299 break;
300 }
301 }
302 pbvh.tag_positions_changed(node_mask);
303 pbvh.update_bounds(depsgraph, object);
304}
305
306} // namespace blender::ed::sculpt_paint::brushes
A BVH for high poly meshes.
Object is a sort of wrapper for general info.
#define PRF_scope(category)
#define BLI_NOINLINE
#define BLI_assert_msg(a, msg)
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
constexpr int64_t size() const
Definition BLI_span.hh:499
int64_t size() const
Definition BLI_set.hh:606
constexpr int64_t size() const
Definition BLI_span.hh:256
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
pbvh::Tree * pbvh_get(Object &object)
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
void ensure_boundary_info(Object &object)
Definition sculpt.cc:588
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, Object &object, bke::pbvh::BMeshNode &node, LocalData &tls, MutableSpan< float > layer_displacement_factor)
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, Object &object, const bool use_persistent_base, const Span< float3 > persistent_base_positions, const Span< float3 > persistent_base_normals, bke::pbvh::GridsNode &node, LocalData &tls, MutableSpan< float > layer_displacement_factor)
static std::array< float, 4 > iteration_strengths(const float strength, const int stroke_iteration)
Definition relax.cc:95
static void apply_positions_faces(const Sculpt &sd, const Span< int > verts, Object &object, const MutableSpan< float3 > translations, const PositionDeformData &position_data)
Definition relax.cc:52
static BLI_NOINLINE void do_smooth_brush_mesh(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, Object &object, const IndexMask &node_mask, const float brush_strength)
Definition smooth.cc:86
void do_smooth_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask, float brush_strength)
Definition smooth.cc:243
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 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)
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void gather_bmesh_positions(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6497
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
void calc_factors_common_grids(const Depsgraph &depsgraph, const Brush &brush, const Object &object, Span< float3 > positions, const bke::pbvh::GridsNode &node, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6804
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
void calc_factors_common_mesh_indexed(const Depsgraph &depsgraph, const Brush &brush, const Object &object, const MeshAttributeData &attribute_data, Span< float3 > vert_positions, Span< float3 > vert_normals, const bke::pbvh::MeshNode &node, MutableSpan< float > factors, MutableSpan< float > distances)
Definition sculpt.cc:6739
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
OffsetIndices< int > create_node_vert_offsets(Span< bke::pbvh::MeshNode > nodes, const IndexMask &node_mask, Array< int > &node_data)
Definition sculpt.cc:7912
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
void calc_factors_common_bmesh(const Depsgraph &depsgraph, const Brush &brush, const Object &object, Span< float3 > positions, bke::pbvh::BMeshNode &node, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6891
constexpr ParallelGrainSize grain_size(int grain_size)
BMesh const char void * data
const Brush * BKE_paint_brush_for_read(const Paint *paint)
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
std::unique_ptr< SculptBoundaryInfoCache > boundary_info_cache
Definition BKE_paint.hh:458