Blender V5.3
pinch.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 "BKE_mesh.hh"
12#include "BKE_object_types.hh"
13#include "BKE_paint.hh"
14#include "BKE_paint_bvh.hh"
15#include "BKE_subdiv_ccg.hh"
16#include "DNA_brush_types.h"
17#include "DNA_mesh_types.h"
18#include "DNA_object_types.h"
19#include "DNA_scene_types.h"
20
22#include "BLI_math_matrix_c.hh"
23#include "BLI_math_vector.hh"
24#include "BLI_task.hh"
25
29
30#include "bmesh.hh"
31
33
34inline namespace pinch_cc {
35
42
43BLI_NOINLINE static void calc_translations(const Span<float3> positions,
44 const float3 &location,
45 const std::array<float3, 2> &stroke_xz,
46 const MutableSpan<float3> translations)
47{
49 BLI_assert(positions.size() == translations.size());
50
51 for (const int i : positions.index_range()) {
52 /* Calculate displacement from the vertex to the brush center. */
53 const float3 disp_center = location - positions[i];
54
55 /* Project the displacement into the X vector (aligned to the stroke). */
56 const float3 x_disp = stroke_xz[0] * math::dot(disp_center, stroke_xz[0]);
57
58 /* Project the displacement into the Z vector (aligned to the surface normal). */
59 const float3 z_disp = stroke_xz[1] * math::dot(disp_center, stroke_xz[1]);
60
61 /* Add the two projected vectors to calculate the final displacement.
62 * The Y component is removed. */
63 translations[i] = x_disp + z_disp;
64 }
65}
66
67static void calc_faces(const Depsgraph &depsgraph,
68 const Sculpt &sd,
69 const Brush &brush,
70 const std::array<float3, 2> &stroke_xz,
71 const float strength,
72 const MeshAttributeData &attribute_data,
73 const Span<float3> vert_normals,
75 Object &object,
76 const PositionDeformData &position_data)
77{
78 SculptSession &ss = *object.runtime->sculpt_session;
79 const StrokeCache &cache = *ss.cache;
80
81 const Span<int> verts = node.verts();
83 gather_data_mesh<float3>(position_data.eval, verts, positions);
84
88 depsgraph, brush, object, attribute_data, positions, vert_normals, node, factors, distances);
89
90 scale_factors(factors, strength);
91
93 calc_translations(positions, cache.location_symm, stroke_xz, translations);
94 if (brush.falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
95 project_translations(translations, cache.view_normal_symm);
96 }
97
98 scale_translations(translations, factors);
99
100 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
101 position_data.deform(translations, verts);
102}
103
104static void calc_grids(const Depsgraph &depsgraph,
105 const Sculpt &sd,
106 Object &object,
107 const Brush &brush,
108 const std::array<float3, 2> &stroke_xz,
109 const float strength,
111 LocalData &tls)
112{
113 SculptSession &ss = *object.runtime->sculpt_session;
114 const StrokeCache &cache = *ss.cache;
115 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
116
117 const Span<int> grids = node.grids();
118 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
119
120 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
121
122 scale_factors(tls.factors, strength);
123
124 tls.translations.resize(positions.size());
125 const MutableSpan<float3> translations = tls.translations;
126 calc_translations(positions, cache.location_symm, stroke_xz, translations);
127 if (brush.falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
128 project_translations(translations, cache.view_normal_symm);
129 }
130
131 scale_translations(translations, tls.factors);
132
133 clip_and_lock_translations(sd, ss, positions, translations);
134 apply_translations(translations, grids, subdiv_ccg);
135}
136
137static void calc_bmesh(const Depsgraph &depsgraph,
138 const Sculpt &sd,
139 Object &object,
140 const Brush &brush,
141 const std::array<float3, 2> &stroke_xz,
142 const float strength,
144 LocalData &tls)
145{
146 SculptSession &ss = *object.runtime->sculpt_session;
147 const StrokeCache &cache = *ss.cache;
148
150
151 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
152
153 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
154
155 scale_factors(tls.factors, strength);
156
157 tls.translations.resize(verts.size());
158 const MutableSpan<float3> translations = tls.translations;
159 calc_translations(positions, cache.location_symm, stroke_xz, translations);
160 if (brush.falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
161 project_translations(translations, cache.view_normal_symm);
162 }
163
164 scale_translations(translations, tls.factors);
165
166 clip_and_lock_translations(sd, ss, positions, translations);
167 apply_translations(translations, verts);
168}
169
170} // namespace pinch_cc
171
172void do_pinch_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 float3 area_no;
183 float3 area_co;
184 calc_brush_plane(depsgraph, brush, object, node_mask, area_no, area_co);
185
186 /* delay the first daub because grab delta is not setup */
188 return;
189 }
190
192 return;
193 }
194
195 /* Initialize `mat`. */
197 mat.x_axis() = math::cross(area_no, ss.cache->grab_delta_symm);
198 mat.y_axis() = math::cross(area_no, mat.x_axis());
199 mat.z_axis() = area_no;
200 mat.location() = ss.cache->location_symm;
201 normalize_m4(mat.ptr());
202
203 const std::array<float3, 2> stroke_xz{math::normalize(mat.x_axis()),
204 math::normalize(mat.z_axis())};
205
207 switch (pbvh.type()) {
209 const Mesh &mesh = *id_cast<Mesh *>(object.data);
210 const MeshAttributeData attribute_data(mesh);
211 const PositionDeformData position_data(depsgraph, object);
212 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
214 node_mask.foreach_index(
215 [&](const int i) {
216 calc_faces(depsgraph,
217 sd,
218 brush,
219 stroke_xz,
220 ss.cache->bstrength,
221 attribute_data,
222 vert_normals,
223 nodes[i],
224 object,
225 position_data);
226 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
227 },
229 break;
230 }
232 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
233 MutableSpan<float3> positions = subdiv_ccg.positions;
235 node_mask.foreach_index(
236 [&](const int i) {
237 LocalData &tls = all_tls.local();
239 depsgraph, sd, object, brush, stroke_xz, ss.cache->bstrength, nodes[i], tls);
240 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
241 },
243 break;
244 }
247 node_mask.foreach_index(
248 [&](const int i) {
249 LocalData &tls = all_tls.local();
251 depsgraph, sd, object, brush, stroke_xz, ss.cache->bstrength, nodes[i], tls);
253 },
255 break;
256 }
257 }
258 pbvh.tag_positions_changed(node_mask);
259 pbvh.flush_bounds_to_parents();
260}
261
262} // 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
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
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 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 BLI_NOINLINE void calc_translations(const Span< float3 > orig_positions, const Span< float3 > orig_normals, const Span< float3 > positions, const Span< float > displacement_factors, const Span< float > factors, const float height, const MutableSpan< float3 > r_translations)
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_pinch_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition pinch.cc:172
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
bool stroke_is_first_brush_step_of_symmetry_pass(const StrokeCache &cache)
Definition sculpt.cc:667
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
void calc_factors_common_mesh(const Depsgraph &depsgraph, const Brush &brush, const Object &object, const MeshAttributeData &attribute_data, Span< float3 > positions, Span< float3 > vert_normals, const bke::pbvh::MeshNode &node, MutableSpan< float > factors, MutableSpan< float > distances)
Definition sculpt.cc:6772
void project_translations(MutableSpan< float3 > translations, const float3 &plane)
Definition sculpt.cc:7638
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:3062
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 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)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
bool is_zero(const T &a)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ PAINT_FALLOFF_SHAPE_TUBE
MatBase< float, 4, 4 > float4x4
void normalize_m4(float R[4][4]) ATTR_NONNULL()
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)
const c_style_mat & ptr() const
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
Array< float3 > positions