Blender V5.3
inflate.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
23#include "BLI_task.hh"
24
28
29#include "bmesh.hh"
30
32
33inline namespace inflate_cc {
34
41
42BLI_NOINLINE static void apply_scale(const MutableSpan<float3> translations, const float3 &scale)
43{
45 for (const int i : translations.index_range()) {
46 translations[i] *= scale;
47 }
48}
49
50static void calc_faces(const Depsgraph &depsgraph,
51 const Sculpt &sd,
52 const Brush &brush,
53 const float3 &scale,
54 const MeshAttributeData &attribute_data,
55 const Span<float3> vert_normals,
57 Object &object,
58 const PositionDeformData &position_data)
59{
60 SculptSession &ss = *object.runtime->sculpt_session;
61
62 const Span<int> verts = node.verts();
63
67 brush,
68 object,
69 attribute_data,
70 position_data.eval,
71 vert_normals,
72 node,
73 factors,
74 distances);
75
77 gather_data_mesh<float3>(vert_normals, verts, translations);
78 apply_scale(translations, scale);
79 scale_translations(translations, factors);
80
81 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
82 position_data.deform(translations, verts);
83}
84
85static void calc_grids(const Depsgraph &depsgraph,
86 const Sculpt &sd,
87 Object &object,
88 const Brush &brush,
89 const float3 &scale,
91 LocalData &tls)
92{
93 SculptSession &ss = *object.runtime->sculpt_session;
94 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
95
96 const Span<int> grids = node.grids();
97 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
98
99 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
100
101 tls.translations.resize(positions.size());
102 const MutableSpan<float3> translations = tls.translations;
103 gather_grids_normals(subdiv_ccg, grids, translations);
104 apply_scale(translations, scale);
105 scale_translations(translations, tls.factors);
106
107 clip_and_lock_translations(sd, ss, positions, translations);
108 apply_translations(translations, grids, subdiv_ccg);
109}
110
111static void calc_bmesh(const Depsgraph &depsgraph,
112 const Sculpt &sd,
113 Object &object,
114 const Brush &brush,
115 const float3 &scale,
117 LocalData &tls)
118{
119 SculptSession &ss = *object.runtime->sculpt_session;
120
122 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
123
124 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
125
126 tls.translations.resize(verts.size());
127 const MutableSpan<float3> translations = tls.translations;
128 gather_bmesh_normals(verts, translations);
129 apply_scale(translations, scale);
130 scale_translations(translations, tls.factors);
131
132 clip_and_lock_translations(sd, ss, positions, translations);
133 apply_translations(translations, verts);
134}
135
136} // namespace inflate_cc
137
138void do_inflate_brush(const Depsgraph &depsgraph,
139 const Sculpt &sd,
140 Object &object,
141 const IndexMask &node_mask)
142{
144 const SculptSession &ss = *object.runtime->sculpt_session;
147
148 const float3 scale = ss.cache->scale * ss.cache->radius * ss.cache->bstrength;
149
151 switch (pbvh.type()) {
153 const Mesh &mesh = *id_cast<Mesh *>(object.data);
154 const MeshAttributeData attribute_data(mesh);
155 const PositionDeformData position_data(depsgraph, object);
156 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
158 node_mask.foreach_index(
159 [&](const int i) {
160 calc_faces(depsgraph,
161 sd,
162 brush,
163 scale,
164 attribute_data,
165 vert_normals,
166 nodes[i],
167 object,
168 position_data);
169 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
170 },
172 break;
173 }
175 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
176 MutableSpan<float3> positions = subdiv_ccg.positions;
178 node_mask.foreach_index(
179 [&](const int i) {
180 LocalData &tls = all_tls.local();
181 calc_grids(depsgraph, sd, object, brush, scale, nodes[i], tls);
182 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
183 },
185 break;
186 }
189 node_mask.foreach_index(
190 [&](const int i) {
191 LocalData &tls = all_tls.local();
192 calc_bmesh(depsgraph, sd, object, brush, scale, nodes[i], tls);
194 },
196 break;
197 }
198 }
199 pbvh.tag_positions_changed(node_mask);
200 pbvh.flush_bounds_to_parents();
201}
202
203} // 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
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 foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
float3x3 scale(float3x3 mat, float2 scale)
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
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1205
static BLI_NOINLINE void apply_scale(const MutableSpan< float3 > translations, const float3 &scale)
Definition inflate.cc:42
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_inflate_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
Definition inflate.cc:138
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 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 gather_bmesh_normals(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > normals)
Definition sculpt.cc:6517
void scale_translations(MutableSpan< float3 > translations, Span< float > factors)
Definition sculpt.cc:7837
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 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
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
void gather_grids_normals(const SubdivCCG &subdiv_ccg, Span< int > grids, MutableSpan< float3 > normals)
Definition sculpt.cc:6509
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)
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
Array< float3 > positions