Blender V5.0
editors/sculpt_paint/brushes/rotate.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_matrix.hh"
20#include "BLI_math_rotation.hh"
21#include "BLI_task.hh"
22
26
27#include "bmesh.hh"
28
30
31inline namespace rotate_cc {
32
38
39BLI_NOINLINE static void calc_translations(const Span<float3> positions,
40 const float3 &axis,
41 const Span<float> angles,
42 const float3 &center,
43 const MutableSpan<float3> translations)
44{
45 BLI_assert(positions.size() == angles.size());
46 BLI_assert(positions.size() == translations.size());
47
48 for (const int i : positions.index_range()) {
49 const math::AxisAngle rotation(axis, angles[i]);
50 const float3x3 matrix = math::from_rotation<float3x3>(rotation);
51 const float3 rotated = math::transform_point(matrix, positions[i] - center);
52 translations[i] = rotated + center - positions[i];
53 }
54}
55
56static void calc_faces(const Depsgraph &depsgraph,
57 const Sculpt &sd,
58 const Brush &brush,
59 const float angle,
60 const MeshAttributeData &attribute_data,
61 const bke::pbvh::MeshNode &node,
62 Object &object,
63 LocalData &tls,
64 const PositionDeformData &position_data)
65{
66 SculptSession &ss = *object.sculpt;
67 const StrokeCache &cache = *ss.cache;
68
69 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
70 const Span<int> verts = node.verts();
71
73 brush,
74 object,
75 attribute_data,
76 orig_data.positions,
77 orig_data.normals,
78 node,
79 tls.factors,
80 tls.distances);
81
83
84 tls.translations.resize(verts.size());
85 const MutableSpan<float3> translations = tls.translations;
88 tls.factors,
89 cache.location_symm,
90 translations);
91
92 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
93 position_data.deform(translations, verts);
94}
95
96static void calc_grids(const Depsgraph &depsgraph,
97 const Sculpt &sd,
98 Object &object,
99 const Brush &brush,
100 const float angle,
102 LocalData &tls)
103{
104 SculptSession &ss = *object.sculpt;
105 const StrokeCache &cache = *ss.cache;
106 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
107 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
108
109 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
110 const Span<int> grids = node.grids();
111 const int grid_verts_num = grids.size() * key.grid_area;
112
114 brush,
115 object,
116 orig_data.positions,
117 orig_data.normals,
118 node,
119 tls.factors,
120 tls.distances);
121
123
124 tls.translations.resize(grid_verts_num);
125 const MutableSpan<float3> translations = tls.translations;
127 cache.sculpt_normal_symm,
128 tls.factors,
129 cache.location_symm,
130 translations);
131
132 clip_and_lock_translations(sd, ss, orig_data.positions, translations);
133 apply_translations(translations, grids, subdiv_ccg);
134}
135
136static void calc_bmesh(const Depsgraph &depsgraph,
137 const Sculpt &sd,
138 Object &object,
139 const Brush &brush,
140 const float angle,
142 LocalData &tls)
143{
144 SculptSession &ss = *object.sculpt;
145 const StrokeCache &cache = *ss.cache;
146
148
149 Array<float3> orig_positions(verts.size());
150 Array<float3> orig_normals(verts.size());
151 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
152
154 depsgraph, brush, object, orig_positions, orig_normals, node, tls.factors, tls.distances);
155
157
158 tls.translations.resize(verts.size());
159 const MutableSpan<float3> translations = tls.translations;
161 orig_positions, cache.sculpt_normal_symm, tls.factors, cache.location_symm, translations);
162
163 clip_and_lock_translations(sd, ss, orig_positions, translations);
164 apply_translations(translations, verts);
165}
166
167} // namespace rotate_cc
168
169void do_rotate_brush(const Depsgraph &depsgraph,
170 const Sculpt &sd,
171 Object &object,
172 const IndexMask &node_mask)
173{
174 const SculptSession &ss = *object.sculpt;
175 bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
176 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
177
178 constexpr std::array<int, 8> flip{1, -1, -1, 1, -1, 1, 1, -1};
179 const float angle = ss.cache->vertex_rotation * flip[ss.cache->mirror_symmetry_pass];
180
182 switch (pbvh.type()) {
184 const Mesh &mesh = *static_cast<Mesh *>(object.data);
185 const MeshAttributeData attribute_data(mesh);
186 const PositionDeformData position_data(depsgraph, object);
188 node_mask.foreach_index(GrainSize(1), [&](const int i) {
189 LocalData &tls = all_tls.local();
191 depsgraph, sd, brush, angle, attribute_data, nodes[i], object, tls, position_data);
192 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
193 });
194 break;
195 }
197 SubdivCCG &subdiv_ccg = *object.sculpt->subdiv_ccg;
198 MutableSpan<float3> positions = subdiv_ccg.positions;
200 node_mask.foreach_index(GrainSize(1), [&](const int i) {
201 LocalData &tls = all_tls.local();
202 calc_grids(depsgraph, sd, object, brush, angle, nodes[i], tls);
203 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
204 });
205 break;
206 }
209 node_mask.foreach_index(GrainSize(1), [&](const int i) {
210 LocalData &tls = all_tls.local();
211 calc_bmesh(depsgraph, sd, object, brush, angle, nodes[i], tls);
213 });
214 break;
215 }
216 }
217 pbvh.tag_positions_changed(node_mask);
219}
220
221} // 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)
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_NOINLINE
Object is a sort of wrapper for general info.
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
BPy_StructRNA * depsgraph
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
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_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float angle, bke::pbvh::BMeshNode &node, LocalData &tls)
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float angle, bke::pbvh::GridsNode &node, LocalData &tls)
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const float angle, const MeshAttributeData &attribute_data, const bke::pbvh::MeshNode &node, Object &object, LocalData &tls, const PositionDeformData &position_data)
static BLI_NOINLINE void calc_translations(const Span< float3 > positions, const float3 &axis, const Span< float > angles, const float3 &center, const MutableSpan< float3 > translations)
void do_rotate_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
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 scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7512
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 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
AxisAngleBase< float, AngleRadianBase< float > > AxisAngle
MatT from_rotation(const RotationT &rotation)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
MatBase< float, 3, 3 > float3x3
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