Blender V4.3
plane.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
13
14#include "DNA_brush_types.h"
15#include "DNA_mesh_types.h"
16#include "DNA_object_types.h"
17#include "DNA_scene_types.h"
18
19#include "BKE_mesh.hh"
20#include "BKE_paint.hh"
21#include "BKE_pbvh.hh"
22#include "BKE_subdiv_ccg.hh"
23
24#include "BLI_array.hh"
26#include "BLI_math_geom.h"
27#include "BLI_math_matrix.hh"
28#include "BLI_math_vector.hh"
29#include "BLI_task.h"
30#include "BLI_task.hh"
31
35
37
38inline namespace flatten_cc {
39
43
50
51static void calc_faces(const Depsgraph &depsgraph,
52 const Sculpt &sd,
53 const Brush &brush,
54 const float4 &plane,
55 const float strength,
56 const MeshAttributeData &attribute_data,
57 const Span<float3> vert_normals,
58 const bke::pbvh::MeshNode &node,
59 Object &object,
60 LocalData &tls,
61 const PositionDeformData &position_data,
62 const IndexedFilterFn filter)
63{
64 SculptSession &ss = *object.sculpt;
65 const StrokeCache &cache = *ss.cache;
66
67 const Span<int> verts = node.verts();
68
70 brush,
71 object,
72 attribute_data,
73 position_data.eval,
74 vert_normals,
75 node,
76 tls.factors,
77 tls.distances);
78
79 scale_factors(tls.factors, strength);
80
81 filter(position_data.eval, verts, plane, tls.factors);
82
83 tls.translations.resize(verts.size());
84 const MutableSpan<float3> translations = tls.translations;
85 calc_translations_to_plane(position_data.eval, verts, plane, translations);
86 filter_plane_trim_limit_factors(brush, cache, translations, tls.factors);
87 scale_translations(translations, tls.factors);
88
89 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
90 position_data.deform(translations, verts);
91}
92
93static void calc_grids(const Depsgraph &depsgraph,
94 const Sculpt &sd,
95 Object &object,
96 const Brush &brush,
97 const float4 &plane,
98 const float strength,
100 LocalData &tls,
101 const GenericFilterFn filter)
102{
103 SculptSession &ss = *object.sculpt;
104 const StrokeCache &cache = *ss.cache;
105 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
106
107 const Span<int> grids = node.grids();
108 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
109
110 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
111
112 scale_factors(tls.factors, strength);
113
114 filter(positions, plane, tls.factors);
115
116 tls.translations.resize(positions.size());
117 const MutableSpan<float3> translations = tls.translations;
118 calc_translations_to_plane(positions, plane, translations);
119 filter_plane_trim_limit_factors(brush, cache, translations, tls.factors);
120 scale_translations(translations, tls.factors);
121
122 clip_and_lock_translations(sd, ss, positions, translations);
123 apply_translations(translations, grids, subdiv_ccg);
124}
125
126static void calc_bmesh(const Depsgraph &depsgraph,
127 const Sculpt &sd,
128 Object &object,
129 const Brush &brush,
130 const float4 &plane,
131 const float strength,
133 LocalData &tls,
134 const GenericFilterFn filter)
135{
136 SculptSession &ss = *object.sculpt;
137 const StrokeCache &cache = *ss.cache;
138
140 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
141
142 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
143
144 scale_factors(tls.factors, strength);
145
146 filter(positions, plane, tls.factors);
147
148 tls.translations.resize(verts.size());
149 const MutableSpan<float3> translations = tls.translations;
150 calc_translations_to_plane(positions, plane, translations);
151 filter_plane_trim_limit_factors(brush, cache, translations, tls.factors);
152 scale_translations(translations, tls.factors);
153
154 clip_and_lock_translations(sd, ss, positions, translations);
155 apply_translations(translations, verts);
156}
157
158static void do_plane_brush(const Depsgraph &depsgraph,
159 const Sculpt &sd,
160 Object &object,
161 const IndexMask &node_mask,
162 const float direction,
163 const IndexedFilterFn indexed_filter,
164 const GenericFilterFn generic_filter)
165{
166 const SculptSession &ss = *object.sculpt;
167 bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
168 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
169
170 float3 area_no;
171 float3 area_co;
172 calc_brush_plane(depsgraph, brush, object, node_mask, area_no, area_co);
174
175 const float offset = SCULPT_brush_plane_offset_get(sd, ss);
176 const float displace = direction * ss.cache->radius * offset;
177 area_co += area_no * ss.cache->scale * displace;
178
179 float4 plane;
180 plane_from_point_normal_v3(plane, area_co, area_no);
181
183 switch (pbvh.type()) {
185 const Mesh &mesh = *static_cast<Mesh *>(object.data);
186 const MeshAttributeData attribute_data(mesh.attributes());
187 const PositionDeformData position_data(depsgraph, object);
188 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
190 node_mask.foreach_index(GrainSize(1), [&](const int i) {
191 LocalData &tls = all_tls.local();
193 sd,
194 brush,
195 plane,
196 ss.cache->bstrength,
197 attribute_data,
198 vert_normals,
199 nodes[i],
200 object,
201 tls,
202 position_data,
203 indexed_filter);
204 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
205 });
206 break;
207 }
209 SubdivCCG &subdiv_ccg = *object.sculpt->subdiv_ccg;
210 MutableSpan<float3> positions = subdiv_ccg.positions;
212 node_mask.foreach_index(GrainSize(1), [&](const int i) {
213 LocalData &tls = all_tls.local();
215 sd,
216 object,
217 brush,
218 plane,
219 ss.cache->bstrength,
220 nodes[i],
221 tls,
222 generic_filter);
223 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
224 });
225 break;
226 }
229 node_mask.foreach_index(GrainSize(1), [&](const int i) {
230 LocalData &tls = all_tls.local();
232 sd,
233 object,
234 brush,
235 plane,
236 ss.cache->bstrength,
237 nodes[i],
238 tls,
239 generic_filter);
241 });
242 break;
243 }
244 }
245 pbvh.tag_positions_changed(node_mask);
247}
248
249} // namespace flatten_cc
250
251void do_flatten_brush(const Depsgraph &depsgraph,
252 const Sculpt &sd,
253 Object &object,
254 const IndexMask &node_mask)
255{
257 depsgraph,
258 sd,
259 object,
260 node_mask,
261 1.0f,
262 [](const Span<float3> /*vert_positions*/,
263 const Span<int> /*verts*/,
264 const float4 & /*plane*/,
265 const MutableSpan<float> /*factors*/) {},
266 [](const Span<float3> /*positions*/,
267 const float4 & /*plane*/,
268 const MutableSpan<float> /*factors*/) {});
269}
270
271void do_fill_brush(const Depsgraph &depsgraph,
272 const Sculpt &sd,
273 Object &object,
274 const IndexMask &node_mask)
275{
277 depsgraph,
278 sd,
279 object,
280 node_mask,
281 1.0f,
282 [](const Span<float3> vert_positions,
283 const Span<int> verts,
284 const float4 &plane,
285 const MutableSpan<float> factors) {
286 filter_above_plane_factors(vert_positions, verts, plane, factors);
287 },
288 [](const Span<float3> positions, const float4 &plane, const MutableSpan<float> factors) {
289 filter_above_plane_factors(positions, plane, factors);
290 });
291}
292
293void do_scrape_brush(const Depsgraph &depsgraph,
294 const Sculpt &sd,
295 Object &object,
296 const IndexMask &node_mask)
297{
299 depsgraph,
300 sd,
301 object,
302 node_mask,
303 -1.0f,
304 [](const Span<float3> vert_positions,
305 const Span<int> verts,
306 const float4 &plane,
307 const MutableSpan<float> factors) {
308 filter_below_plane_factors(vert_positions, verts, plane, factors);
309 },
310 [](const Span<float3> positions, const float4 &plane, const MutableSpan<float> factors) {
311 filter_below_plane_factors(positions, plane, factors);
312 });
313}
314
315} // namespace blender::ed::sculpt_paint
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Definition paint.cc:654
const blender::Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(blender::bke::pbvh::BMeshNode *node)
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:215
Object is a sort of wrapper for general info.
void tag_positions_changed(const IndexMask &node_mask)
Definition pbvh.cc:549
Span< NodeT > nodes() const
void deform(MutableSpan< float3 > translations, Span< int > verts) const
Definition sculpt.cc:7139
void foreach_index(Fn &&fn) const
const Depsgraph * depsgraph
static float verts[][3]
pbvh::Tree * pbvh_get(Object &object)
Definition paint.cc:2846
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1095
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1075
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:2502
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1084
void flush_bounds_to_parents(Tree &pbvh)
Definition pbvh.cc:1132
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float4 &plane, const float strength, bke::pbvh::GridsNode &node, LocalData &tls, const GenericFilterFn filter)
Definition plane.cc:93
static void do_plane_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask, const float direction, const IndexedFilterFn indexed_filter, const GenericFilterFn generic_filter)
Definition plane.cc:158
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const float4 &plane, const float strength, const MeshAttributeData &attribute_data, const Span< float3 > vert_normals, const bke::pbvh::MeshNode &node, Object &object, LocalData &tls, const PositionDeformData &position_data, const IndexedFilterFn filter)
Definition plane.cc:51
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float4 &plane, const float strength, bke::pbvh::BMeshNode &node, LocalData &tls, const GenericFilterFn filter)
Definition plane.cc:126
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:6054
void filter_below_plane_factors(Span< float3 > vert_positions, Span< int > verts, const float4 &plane, MutableSpan< float > factors)
Definition sculpt.cc:7534
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:6270
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, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6199
void scale_translations(MutableSpan< float3 > translations, Span< float > factors)
Definition sculpt.cc:7210
void calc_translations_to_plane(Span< float3 > vert_positions, Span< int > verts, const float4 &plane, MutableSpan< float3 > translations)
Definition sculpt.cc:7479
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7227
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7022
void calc_brush_plane(const Depsgraph &depsgraph, const Brush &brush, Object &ob, const IndexMask &node_mask, float3 &r_area_no, float3 &r_area_co)
Definition sculpt.cc:2789
void filter_plane_trim_limit_factors(const Brush &brush, const StrokeCache &cache, Span< float3 > translations, MutableSpan< float > factors)
Definition sculpt.cc:7518
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6958
void filter_above_plane_factors(Span< float3 > vert_positions, Span< int > verts, const float4 &plane, MutableSpan< float > factors)
Definition sculpt.cc:7557
void do_fill_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition plane.cc:271
void do_flatten_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition plane.cc:251
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:6304
void do_scrape_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition plane.cc:293
void SCULPT_tilt_apply_to_normal(float r_normal[3], blender::ed::sculpt_paint::StrokeCache *cache, const float tilt_strength)
Definition sculpt.cc:2550
float SCULPT_brush_plane_offset_get(const Sculpt &sd, const SculptSession &ss)
Definition sculpt.cc:2883
float tilt_strength_factor
blender::ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:427
SubdivCCG * subdiv_ccg
Definition BKE_paint.hh:405
blender::Array< blender::float3 > positions