Blender V5.3
topology_slide.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_math_vector.hh"
25#include "BLI_task.hh"
26
30
31#include "bmesh.hh"
32
34
35inline namespace topology_slide_cc {
36
45
47 const StrokeCache &cache,
48 const Span<float3> positions,
49 const MutableSpan<float3> r_translations)
50{
52 switch (brush.slide_deform_type) {
54 r_translations.fill(math::normalize(cache.location_symm - cache.last_location_symm));
55 break;
57 for (const int i : positions.index_range()) {
58 r_translations[i] = math::normalize(cache.location_symm - positions[i]);
59 }
60 break;
62 for (const int i : positions.index_range()) {
63 r_translations[i] = math::normalize(positions[i] - cache.location_symm);
64 }
65 break;
66 }
67}
68
69static inline void add_neighbor_influence(const float3 &position,
70 const float3 &dir,
71 const float3 &neighbor_position,
72 float3 &translation)
73{
74 const float3 neighbor_disp = neighbor_position - position;
75 const float3 neighbor_dir = math::normalize(neighbor_disp);
76 if (math::dot(dir, neighbor_dir) > 0.0f) {
77 translation += neighbor_dir * math::dot(dir, neighbor_disp);
78 }
79}
80
81BLI_NOINLINE static void calc_neighbor_influence(const Span<float3> vert_positions,
82 const Span<float3> positions,
83 const GroupedSpan<int> vert_neighbors,
84 const MutableSpan<float3> translations)
85{
87 for (const int i : positions.index_range()) {
88 const float3 &position = positions[i];
89 const float3 &dir = translations[i];
90
91 float3 final_translation(0);
92 for (const int neighbor : vert_neighbors[i]) {
93 add_neighbor_influence(position, dir, vert_positions[neighbor], final_translation);
94 }
95
96 translations[i] = final_translation;
97 }
98}
99
100BLI_NOINLINE static void calc_neighbor_influence(const SubdivCCG &subdiv_ccg,
101 const Span<int> grids,
102 const MutableSpan<float3> translations)
103{
105 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
106 const Span<float3> positions = subdiv_ccg.positions;
107 for (const int i : grids.index_range()) {
108 const int node_start = i * key.grid_area;
109 const int grid = grids[i];
110 const int start = grid * key.grid_area;
111 for (const int y : IndexRange(key.grid_size)) {
112 for (const int x : IndexRange(key.grid_size)) {
113 const int offset = CCG_grid_xy_to_index(key.grid_size, x, y);
114 const int vert = start + offset;
115 const int node_vert = node_start + offset;
116
117 const float3 &position = positions[vert];
118 const float3 &dir = translations[node_vert];
119
120 SubdivCCGCoord coord{};
121 coord.grid_index = grid;
122 coord.x = x;
123 coord.y = y;
124
125 SubdivCCGNeighbors neighbors;
126 BKE_subdiv_ccg_neighbor_coords_get(subdiv_ccg, coord, false, neighbors);
127
128 float3 final_translation(0);
129 for (const SubdivCCGCoord neighbor : neighbors.coords) {
131 position, dir, positions[neighbor.to_index(key)], final_translation);
132 }
133
134 translations[node_vert] = final_translation;
135 }
136 }
137 }
138}
139
141 const Set<BMVert *, 0> &verts,
142 const MutableSpan<float3> translations)
143{
145 BMeshNeighborVerts neighbors;
146 int i = 0;
147 for (BMVert *vert : verts) {
148 const float3 &position = positions[i];
149 const float3 &dir = translations[i];
150
151 float3 final_translation(0);
152 for (BMVert *neighbor : vert_neighbors_get_bmesh(*vert, neighbors)) {
153 add_neighbor_influence(position, dir, neighbor->co, final_translation);
154 }
155
156 translations[i] = final_translation;
157 i++;
158 }
159}
160
161static void calc_faces(const Depsgraph &depsgraph,
162 const Sculpt &sd,
163 const Brush &brush,
164 const OffsetIndices<int> faces,
165 const Span<int> corner_verts,
166 const GroupedSpan<int> vert_to_face_map,
167 const MeshAttributeData &attribute_data,
169 Object &object,
170 LocalData &tls,
171 const PositionDeformData &position_data)
172{
173 SculptSession &ss = *object.runtime->sculpt_session;
174 const StrokeCache &cache = *ss.cache;
175
176 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
177 const Span<int> verts = node.verts();
179 gather_data_mesh(position_data.eval, verts, positions.as_mutable_span());
180
184 brush,
185 object,
186 attribute_data,
187 orig_data.positions,
188 orig_data.normals,
189 node,
190 factors,
191 distances);
192
193 scale_factors(factors, cache.bstrength);
194
195 const GroupedSpan<int> neighbors = calc_vert_neighbors(faces,
196 corner_verts,
197 vert_to_face_map,
198 attribute_data.hide_poly,
199 verts,
200 tls.neighbor_offsets,
201 tls.neighbor_data);
202
204 calc_translation_directions(brush, cache, positions, translations);
205 calc_neighbor_influence(position_data.eval, positions, neighbors, translations);
206 scale_translations(translations, factors);
207
208 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
209 position_data.deform(translations, verts);
210}
211
212static void calc_grids(const Depsgraph &depsgraph,
213 const Sculpt &sd,
214 Object &object,
215 const Brush &brush,
217 LocalData &tls)
218{
219 SculptSession &ss = *object.runtime->sculpt_session;
220 const StrokeCache &cache = *ss.cache;
221 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
222
223 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
224 const Span<int> grids = node.grids();
225 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
226
228 brush,
229 object,
230 orig_data.positions,
231 orig_data.normals,
232 node,
233 tls.factors,
234 tls.distances);
235
236 scale_factors(tls.factors, cache.bstrength);
237
238 tls.translations.resize(positions.size());
239 const MutableSpan<float3> translations = tls.translations;
240 calc_translation_directions(brush, cache, positions, translations);
241 calc_neighbor_influence(subdiv_ccg, grids, translations);
242 scale_translations(translations, tls.factors);
243
244 clip_and_lock_translations(sd, ss, orig_data.positions, translations);
245 apply_translations(translations, grids, subdiv_ccg);
246}
247
248static void calc_bmesh(const Depsgraph &depsgraph,
249 const Sculpt &sd,
250 Object &object,
251 const Brush &brush,
253 LocalData &tls)
254{
255 SculptSession &ss = *object.runtime->sculpt_session;
256 const StrokeCache &cache = *ss.cache;
257
259 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
260
261 Array<float3> orig_positions(verts.size());
262 Array<float3> orig_normals(verts.size());
263 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
264
266 depsgraph, brush, object, orig_positions, orig_normals, node, tls.factors, tls.distances);
267
268 scale_factors(tls.factors, cache.bstrength);
269
270 tls.translations.resize(verts.size());
271 const MutableSpan<float3> translations = tls.translations;
272 calc_translation_directions(brush, cache, positions, translations);
273 calc_neighbor_influence(positions, verts, translations);
274 scale_translations(translations, tls.factors);
275
276 clip_and_lock_translations(sd, ss, orig_positions, translations);
277 apply_translations(translations, verts);
278}
279
280} // namespace topology_slide_cc
281
282void do_topology_slide_brush(const Depsgraph &depsgraph,
283 const Sculpt &sd,
284 Object &object,
285 const IndexMask &node_mask)
286{
288 const SculptSession &ss = *object.runtime->sculpt_session;
291
293 return;
294 }
295
297 switch (pbvh.type()) {
299 Mesh &mesh = *id_cast<Mesh *>(object.data);
300 const PositionDeformData position_data(depsgraph, object);
301 const OffsetIndices faces = mesh.faces();
302 const Span<int> corner_verts = mesh.corner_verts();
303 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
304 const MeshAttributeData attribute_data(mesh);
306 node_mask.foreach_index(
307 [&](const int i) {
308 LocalData &tls = all_tls.local();
309 calc_faces(depsgraph,
310 sd,
311 brush,
312 faces,
313 corner_verts,
314 vert_to_face_map,
315 attribute_data,
316 nodes[i],
317 object,
318 tls,
319 position_data);
320 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
321 },
323 break;
324 }
326 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
327 MutableSpan<float3> positions = subdiv_ccg.positions;
329 node_mask.foreach_index(
330 [&](const int i) {
331 LocalData &tls = all_tls.local();
332 calc_grids(depsgraph, sd, object, brush, nodes[i], tls);
333 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
334 },
336 break;
337 }
340 node_mask.foreach_index(
341 [&](const int i) {
342 LocalData &tls = all_tls.local();
343 calc_bmesh(depsgraph, sd, object, brush, nodes[i], tls);
345 },
347 break;
348 }
349 }
350 pbvh.tag_positions_changed(node_mask);
351 pbvh.flush_bounds_to_parents();
352}
353
354} // 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
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
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 foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define position
pbvh::Tree * pbvh_get(Object &object)
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1216
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1196
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1205
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 void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const MeshAttributeData &attribute_data, const Span< float3 > vert_normals, const bool use_persistent_base, const Span< float3 > persistent_base_positions, const Span< float3 > persistent_base_normals, Object &object, bke::pbvh::MeshNode &node, MutableSpan< float > layer_displacement_factor, const PositionDeformData &position_data)
static BLI_NOINLINE void calc_neighbor_influence(const Span< float3 > vert_positions, const Span< float3 > positions, const GroupedSpan< int > vert_neighbors, const MutableSpan< float3 > translations)
static void add_neighbor_influence(const float3 &position, const float3 &dir, const float3 &neighbor_position, float3 &translation)
static BLI_NOINLINE void calc_translation_directions(const Brush &brush, const StrokeCache &cache, const Span< float3 > positions, const MutableSpan< float3 > r_translations)
void do_topology_slide_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void calc_factors_common_from_orig_data_grids(const Depsgraph &depsgraph, const Brush &brush, const Object &object, Span< float3 > positions, Span< float3 > normals, const bke::pbvh::GridsNode &node, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:7008
void gather_bmesh_positions(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6497
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 calc_factors_common_from_orig_data_mesh(const Depsgraph &depsgraph, const Brush &brush, const Object &object, const MeshAttributeData &attribute_data, Span< float3 > positions, Span< float3 > normals, const bke::pbvh::MeshNode &node, MutableSpan< float > r_factors, MutableSpan< float > r_distances)
Definition sculpt.cc:6976
bool stroke_is_first_brush_step_of_symmetry_pass(const StrokeCache &cache)
Definition sculpt.cc:667
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
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
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 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 calc_factors_common_from_orig_data_bmesh(const Depsgraph &depsgraph, const Brush &brush, const Object &object, Span< float3 > positions, Span< float3 > normals, bke::pbvh::BMeshNode &node, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:7043
constexpr ParallelGrainSize grain_size(int grain_size)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
@ BRUSH_SLIDE_DEFORM_EXPAND
@ BRUSH_SLIDE_DEFORM_DRAG
@ BRUSH_SLIDE_DEFORM_PINCH
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
BMesh const char void * data
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
const Brush * BKE_paint_brush_for_read(const Paint *paint)
VecBase< float, 3 > float3
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
int to_index(const CCGKey &key) const
SubdivCCGNeighborCoords coords
Array< float3 > positions