Blender V5.0
thumb.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
6
7#include "DNA_brush_types.h"
8#include "DNA_mesh_types.h"
9#include "DNA_object_types.h"
10#include "DNA_scene_types.h"
11
12#include "BKE_mesh.hh"
13#include "BKE_paint.hh"
14#include "BKE_paint_bvh.hh"
15#include "BKE_subdiv_ccg.hh"
16
17#include "BLI_array.hh"
19#include "BLI_math_vector.hh"
20#include "BLI_task.hh"
21
25
26#include "bmesh.hh"
27
29
30inline namespace thumb_cc {
31
37
38static void calc_faces(const Depsgraph &depsgraph,
39 const Sculpt &sd,
40 const Brush &brush,
41 const float3 &offset,
42 const MeshAttributeData &attribute_data,
43 const bke::pbvh::MeshNode &node,
44 Object &object,
45 LocalData &tls,
46 const PositionDeformData &position_data)
47{
48 SculptSession &ss = *object.sculpt;
49
50 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
51 const Span<int> verts = node.verts();
52
54 brush,
55 object,
56 attribute_data,
57 orig_data.positions,
58 orig_data.normals,
59 node,
60 tls.factors,
61 tls.distances);
62
63 tls.translations.resize(verts.size());
64 const MutableSpan<float3> translations = tls.translations;
65 translations_from_offset_and_factors(offset, tls.factors, translations);
66
67 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
68 position_data.deform(translations, verts);
69}
70
71static void calc_grids(const Depsgraph &depsgraph,
72 const Sculpt &sd,
73 Object &object,
74 const Brush &brush,
75 const float3 &offset,
77 LocalData &tls)
78{
79 SculptSession &ss = *object.sculpt;
80 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
81 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
82
83 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
84 const Span<int> grids = node.grids();
85 const int grid_verts_num = grids.size() * key.grid_area;
86
88 brush,
89 object,
90 orig_data.positions,
91 orig_data.normals,
92 node,
93 tls.factors,
94 tls.distances);
95
96 tls.translations.resize(grid_verts_num);
97 const MutableSpan<float3> translations = tls.translations;
98 translations_from_offset_and_factors(offset, tls.factors, translations);
99
100 clip_and_lock_translations(sd, ss, orig_data.positions, translations);
101 apply_translations(translations, grids, subdiv_ccg);
102}
103
104static void calc_bmesh(const Depsgraph &depsgraph,
105 const Sculpt &sd,
106 Object &object,
107 const Brush &brush,
108 const float3 &offset,
110 LocalData &tls)
111{
112 SculptSession &ss = *object.sculpt;
113
115
116 Array<float3> orig_positions(verts.size());
117 Array<float3> orig_normals(verts.size());
118 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
119
121 depsgraph, brush, object, orig_positions, orig_normals, node, tls.factors, tls.distances);
122
123 tls.translations.resize(verts.size());
124 const MutableSpan<float3> translations = tls.translations;
125 translations_from_offset_and_factors(offset, tls.factors, translations);
126
127 clip_and_lock_translations(sd, ss, orig_positions, translations);
128 apply_translations(translations, verts);
129}
130
131} // namespace thumb_cc
132
133void do_thumb_brush(const Depsgraph &depsgraph,
134 const Sculpt &sd,
135 Object &object,
136 const IndexMask &node_mask)
137{
138 const SculptSession &ss = *object.sculpt;
139 bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
140 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
141
142 const float3 &grab_delta = ss.cache->grab_delta_symm;
143 const float3 &normal = ss.cache->sculpt_normal_symm;
144 const float3 offset = math::cross(math::cross(normal, grab_delta), normal) * ss.cache->bstrength;
145
147 switch (pbvh.type()) {
149 const Mesh &mesh = *static_cast<Mesh *>(object.data);
150 const MeshAttributeData attribute_data(mesh);
151 const PositionDeformData position_data(depsgraph, object);
153 node_mask.foreach_index(GrainSize(1), [&](const int i) {
154 LocalData &tls = all_tls.local();
156 depsgraph, sd, brush, offset, attribute_data, nodes[i], object, tls, position_data);
157 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
158 });
159 break;
160 }
162 SubdivCCG &subdiv_ccg = *object.sculpt->subdiv_ccg;
163 MutableSpan<float3> positions = subdiv_ccg.positions;
165 node_mask.foreach_index(GrainSize(1), [&](const int i) {
166 LocalData &tls = all_tls.local();
167 calc_grids(depsgraph, sd, object, brush, offset, nodes[i], tls);
168 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
169 });
170 break;
171 }
174 node_mask.foreach_index(GrainSize(1), [&](const int i) {
175 LocalData &tls = all_tls.local();
176 calc_bmesh(depsgraph, sd, object, brush, offset, nodes[i], tls);
178 });
179 break;
180 }
181 }
182 pbvh.tag_positions_changed(node_mask);
184}
185
186} // namespace blender::ed::sculpt_paint::brushes
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Definition paint.cc:650
A BVH for high poly meshes.
const blender::Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(blender::bke::pbvh::BMeshNode *node)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
Object is a sort of wrapper for general info.
BPy_StructRNA * depsgraph
constexpr int64_t size() const
Definition BLI_span.hh:252
void tag_positions_changed(const IndexMask &node_mask)
Definition pbvh.cc:635
Span< NodeT > nodes() const
void flush_bounds_to_parents()
Definition pbvh.cc:1306
void foreach_index(Fn &&fn) const
static float verts[][3]
pbvh::Tree * pbvh_get(Object &object)
Definition paint.cc:3052
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1294
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1274
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1283
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float3 &direction, const float strength, bke::pbvh::BMeshNode &node, LocalData &tls)
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const float4 &test_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)
Definition clay.cc:56
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float4 &test_plane, const float strength, bke::pbvh::GridsNode &node, LocalData &tls)
Definition clay.cc:93
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const float3 &offset, const MeshAttributeData &attribute_data, const bke::pbvh::MeshNode &node, Object &object, LocalData &tls, const PositionDeformData &position_data)
Definition thumb.cc:38
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float3 &offset, bke::pbvh::BMeshNode &node, LocalData &tls)
Definition thumb.cc:104
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float3 &offset, bke::pbvh::GridsNode &node, LocalData &tls)
Definition thumb.cc:71
void do_thumb_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition thumb.cc:133
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:6706
void orig_position_data_gather_bmesh(const BMLog &bm_log, const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions, MutableSpan< float3 > normals)
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, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6670
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7335
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7268
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)
void translations_from_offset_and_factors(const float3 &offset, Span< float > factors, MutableSpan< float3 > r_translations)
Definition sculpt.cc:7531
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:6741
AxisSigned cross(const AxisSigned a, const AxisSigned b)
VecBase< float, 3 > float3
int grid_area
Definition BKE_ccg.hh:35
blender::ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:417
BMLog * bm_log
Definition BKE_paint.hh:392
SubdivCCG * subdiv_ccg
Definition BKE_paint.hh:395
blender::Array< blender::float3 > positions
i
Definition text_draw.cc:230