Blender V5.3
grab.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
9#include <queue>
10
12
13#include "DNA_brush_types.h"
14#include "DNA_mesh_types.h"
15#include "DNA_object_types.h"
16#include "DNA_scene_types.h"
17
18#include "BKE_attribute.hh"
19#include "BKE_mesh.hh"
20#include "BKE_object_types.hh"
21#include "BKE_paint.hh"
22#include "BKE_paint_bvh.hh"
23#include "BKE_subdiv_ccg.hh"
24
25#include "BLI_array.hh"
27#include "BLI_math_vector.hh"
28#include "BLI_task.hh"
29
33
34#include "bmesh.hh"
35
36namespace blender {
37
39inline namespace grab_cc {
40
46
48 const float3 &offset,
49 const Span<float3> normals,
50 const MutableSpan<float> factors)
51{
53 BLI_assert(normals.size() == factors.size());
54
55 const float sign = math::sign(math::dot(cache.initial_normal_symm, cache.grab_delta_symm));
56 const float3 test_dir = math::normalize(offset) * sign;
57 for (const int i : factors.index_range()) {
58 factors[i] *= std::max(math::dot(test_dir, normals[i]), 0.0f);
59 }
60}
61
62static void calc_faces(const Depsgraph &depsgraph,
63 const Sculpt &sd,
64 const Brush &brush,
65 const float3 &offset,
66 const MeshAttributeData &attribute_data,
68 Object &object,
69 const PositionDeformData &position_data)
70{
71 SculptSession &ss = *object.runtime->sculpt_session;
72 const StrokeCache &cache = *ss.cache;
73
74 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
75 const Span<int> verts = node.verts();
76
80 brush,
81 object,
82 attribute_data,
83 orig_data.positions,
84 orig_data.normals,
85 node,
86 factors,
87 distances);
88
89 if (brush.flag2 & BRUSH_GRAB_SILHOUETTE) {
90 calc_silhouette_factors(cache, offset, orig_data.normals, factors);
91 }
92
94 translations_from_offset_and_factors(offset, factors, translations);
95
96 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
97 position_data.deform(translations, verts);
98}
99
100static void calc_grids(const Depsgraph &depsgraph,
101 const Sculpt &sd,
102 Object &object,
103 const Brush &brush,
104 const float3 &offset,
106 LocalData &tls)
107{
108 SculptSession &ss = *object.runtime->sculpt_session;
109 const StrokeCache &cache = *ss.cache;
110 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
111 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
112
113 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
114 const Span<int> grids = node.grids();
115 const int grid_verts_num = grids.size() * key.grid_area;
116
118 brush,
119 object,
120 orig_data.positions,
121 orig_data.normals,
122 node,
123 tls.factors,
124 tls.distances);
125
126 if (brush.flag2 & BRUSH_GRAB_SILHOUETTE) {
127 calc_silhouette_factors(cache, offset, orig_data.normals, tls.factors);
128 }
129
130 tls.translations.resize(grid_verts_num);
131 const MutableSpan<float3> translations = tls.translations;
132 translations_from_offset_and_factors(offset, tls.factors, translations);
133
134 clip_and_lock_translations(sd, ss, orig_data.positions, translations);
135 apply_translations(translations, grids, subdiv_ccg);
136}
137
138static void calc_bmesh(const Depsgraph &depsgraph,
139 const Sculpt &sd,
140 Object &object,
141 const Brush &brush,
142 const float3 &offset,
144 LocalData &tls)
145{
146 SculptSession &ss = *object.runtime->sculpt_session;
147 const StrokeCache &cache = *ss.cache;
148
150
151 Array<float3> orig_positions(verts.size());
152 Array<float3> orig_normals(verts.size());
153 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
154
156 depsgraph, brush, object, orig_positions, orig_normals, node, tls.factors, tls.distances);
157
158 if (brush.flag2 & BRUSH_GRAB_SILHOUETTE) {
159 calc_silhouette_factors(cache, offset, orig_normals, tls.factors);
160 }
161
162 tls.translations.resize(verts.size());
163 const MutableSpan<float3> translations = tls.translations;
164 translations_from_offset_and_factors(offset, tls.factors, translations);
165
166 clip_and_lock_translations(sd, ss, orig_positions, translations);
167 apply_translations(translations, verts);
168}
169
170} // namespace grab_cc
171
172void do_grab_brush(const Depsgraph &depsgraph,
173 const Sculpt &sd,
174 Object &object,
175 const IndexMask &node_mask)
176{
178 const SculptSession &ss = *object.runtime->sculpt_session;
181
182 const float3 grab_delta = grab_delta_get(brush, *ss.cache) * ss.cache->bstrength;
183
185 switch (pbvh.type()) {
187 const Mesh &mesh = *id_cast<Mesh *>(object.data);
188 const MeshAttributeData attribute_data(mesh);
189 const PositionDeformData position_data(depsgraph, object);
191 node_mask.foreach_index(
192 [&](const int i) {
194 depsgraph, sd, brush, grab_delta, attribute_data, nodes[i], object, position_data);
195 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
196 },
198 break;
199 }
201 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
202 MutableSpan<float3> positions = subdiv_ccg.positions;
204 node_mask.foreach_index(
205 [&](const int i) {
206 LocalData &tls = all_tls.local();
207 calc_grids(depsgraph, sd, object, brush, grab_delta, nodes[i], tls);
208 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
209 },
211 break;
212 }
215 node_mask.foreach_index(
216 [&](const int i) {
217 LocalData &tls = all_tls.local();
218 calc_bmesh(depsgraph, sd, object, brush, grab_delta, nodes[i], tls);
220 },
222 break;
223 }
224 }
225 pbvh.tag_positions_changed(node_mask);
226 pbvh.flush_bounds_to_parents();
227}
228} // namespace ed::sculpt_paint::brushes
229
230namespace ed::sculpt_paint {
231
232void geometry_preview_lines_update(Depsgraph &depsgraph,
233 Object &object,
234 SculptSession &ss,
235 float radius)
236{
237 ss.preview_verts = {};
238
239 /* This function is called from the cursor drawing code, so the tree may not be built yet. */
241 if (!pbvh) {
242 return;
243 }
244
245 if (!ss.deform_modifiers_active) {
246 return;
247 }
248
249 if (pbvh->type() != bke::pbvh::Type::Mesh) {
250 return;
251 }
252
253 BKE_sculptsession_update_for_edit(&depsgraph, &object, false);
254
255 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
256 /* Always grab active shape key if the sculpt happens on shapekey. */
257 const Span<float3> positions = ss.shapekey_active ?
258 bke::pbvh::vert_positions_eval(depsgraph, object) :
259 mesh.vert_positions();
260 const OffsetIndices faces = mesh.faces();
261 const Span<int> corner_verts = mesh.corner_verts();
262 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
263 const bke::AttributeAccessor attributes = mesh.attributes();
264 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
265
266 const int active_vert = std::get<int>(ss.active_vert());
267 const float3 brush_co = positions[active_vert];
268 const float radius_sq = radius * radius;
269
270 Vector<int> preview_verts;
271 Vector<int> neighbors;
272 BitVector<> visited_verts(positions.size());
273 std::queue<int> queue;
274 queue.push(active_vert);
275 while (!queue.empty()) {
276 const int from_vert = queue.front();
277 queue.pop();
278
279 neighbors.clear();
280 for (const int neighbor : vert_neighbors_get_mesh(
281 faces, corner_verts, vert_to_face_map, hide_poly, from_vert, neighbors))
282 {
283 preview_verts.append(from_vert);
284 preview_verts.append(neighbor);
285 if (visited_verts[neighbor]) {
286 continue;
287 }
288 visited_verts[neighbor].set();
289 if (math::distance_squared(brush_co, positions[neighbor]) < radius_sq) {
290 queue.push(neighbor);
291 }
292 }
293 }
294
295 ss.preview_verts = preview_verts.as_span();
296}
297
298} // namespace ed::sculpt_paint
299
300} // namespace blender
A BVH for high poly meshes.
Object is a sort of wrapper for general info.
#define PRF_scope(category)
#define BLI_NOINLINE
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
int64_t size() const
Definition BLI_set.hh:606
constexpr int64_t size() const
Definition BLI_span.hh:256
void append(const T &value)
Span< T > as_span() const
GAttributeReader lookup(const StringRef name) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
constexpr T sign(T) RET
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
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
static BLI_NOINLINE void calc_silhouette_factors(const StrokeCache &cache, const float3 &offset, const Span< float3 > normals, const MutableSpan< float > factors)
Definition grab.cc:47
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)
void do_grab_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
Definition grab.cc:172
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 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
void orig_position_data_gather_bmesh(const BMLog &bm_log, const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions, MutableSpan< float3 > normals)
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
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)
OrigPositionData orig_position_data_get_grids(const Object &object, const bke::pbvh::GridsNode &node)
float3 grab_delta_get(const Brush &brush, const StrokeCache &cache)
Definition sculpt.cc:718
void translations_from_offset_and_factors(const float3 &offset, Span< float > factors, MutableSpan< float3 > r_translations)
Definition sculpt.cc:7877
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 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
void geometry_preview_lines_update(Depsgraph &depsgraph, Object &object, SculptSession &ss, float radius)
Definition grab.cc:232
constexpr ParallelGrainSize grain_size(int grain_size)
T sign(const T &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ BRUSH_GRAB_SILHOUETTE
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
BMesh const char void * data
const Brush * BKE_paint_brush_for_read(const Paint *paint)
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
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
KeyBlock * shapekey_active
Definition BKE_paint.hh:368
Array< int > preview_verts
Definition BKE_paint.hh:432
Array< float3 > positions