Blender V4.3
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_key.hh"
13#include "BKE_mesh.hh"
14#include "BKE_paint.hh"
15#include "BKE_pbvh.hh"
16#include "BKE_subdiv_ccg.hh"
17
18#include "BLI_array.hh"
20#include "BLI_math_matrix.hh"
21#include "BLI_math_rotation.hh"
22#include "BLI_math_vector.hh"
23#include "BLI_task.h"
24#include "BLI_task.hh"
25
29
31
32inline namespace rotate_cc {
33
39
40BLI_NOINLINE static void calc_translations(const Span<float3> positions,
41 const float3 &axis,
42 const Span<float> angles,
43 const float3 &center,
44 const MutableSpan<float3> translations)
45{
46 BLI_assert(positions.size() == angles.size());
47 BLI_assert(positions.size() == translations.size());
48
49 for (const int i : positions.index_range()) {
50 const math::AxisAngle rotation(axis, angles[i]);
51 const float3x3 matrix = math::from_rotation<float3x3>(rotation);
52 const float3 rotated = math::transform_point(matrix, positions[i] - center);
53 translations[i] = rotated + center - positions[i];
54 }
55}
56
57static void calc_faces(const Depsgraph &depsgraph,
58 const Sculpt &sd,
59 const Brush &brush,
60 const float angle,
61 const MeshAttributeData &attribute_data,
62 const bke::pbvh::MeshNode &node,
63 Object &object,
64 LocalData &tls,
65 const PositionDeformData &position_data)
66{
67 SculptSession &ss = *object.sculpt;
68 const StrokeCache &cache = *ss.cache;
69
70 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
71 const Span<int> verts = node.verts();
72
74 brush,
75 object,
76 attribute_data,
77 orig_data.positions,
78 orig_data.normals,
79 node,
80 tls.factors,
81 tls.distances);
82
83 scale_factors(tls.factors, angle);
84
85 tls.translations.resize(verts.size());
86 const MutableSpan<float3> translations = tls.translations;
89 tls.factors,
90 cache.location_symm,
91 translations);
92
93 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
94 position_data.deform(translations, verts);
95}
96
97static void calc_grids(const Depsgraph &depsgraph,
98 const Sculpt &sd,
99 Object &object,
100 const Brush &brush,
101 const float angle,
103 LocalData &tls)
104{
105 SculptSession &ss = *object.sculpt;
106 const StrokeCache &cache = *ss.cache;
107 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
108 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
109
110 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
111 const Span<int> grids = node.grids();
112 const int grid_verts_num = grids.size() * key.grid_area;
113
115 brush,
116 object,
117 orig_data.positions,
118 orig_data.normals,
119 node,
120 tls.factors,
121 tls.distances);
122
123 scale_factors(tls.factors, angle);
124
125 tls.translations.resize(grid_verts_num);
126 const MutableSpan<float3> translations = tls.translations;
128 cache.sculpt_normal_symm,
129 tls.factors,
130 cache.location_symm,
131 translations);
132
133 clip_and_lock_translations(sd, ss, orig_data.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 float angle,
143 LocalData &tls)
144{
145 SculptSession &ss = *object.sculpt;
146 const StrokeCache &cache = *ss.cache;
147
149
150 Array<float3> orig_positions(verts.size());
151 Array<float3> orig_normals(verts.size());
152 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
153
155 depsgraph, brush, object, orig_positions, orig_normals, node, tls.factors, tls.distances);
156
157 scale_factors(tls.factors, angle);
158
159 tls.translations.resize(verts.size());
160 const MutableSpan<float3> translations = tls.translations;
162 orig_positions, cache.sculpt_normal_symm, tls.factors, cache.location_symm, translations);
163
164 clip_and_lock_translations(sd, ss, orig_positions, translations);
165 apply_translations(translations, verts);
166}
167
168} // namespace rotate_cc
169
170void do_rotate_brush(const Depsgraph &depsgraph,
171 const Sculpt &sd,
172 Object &object,
173 const IndexMask &node_mask)
174{
175 const SculptSession &ss = *object.sculpt;
176 bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
177 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
178
179 constexpr std::array<int, 8> flip{1, -1, -1, 1, -1, 1, 1, -1};
180 const float angle = ss.cache->vertex_rotation * flip[ss.cache->mirror_symmetry_pass];
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);
189 node_mask.foreach_index(GrainSize(1), [&](const int i) {
190 LocalData &tls = all_tls.local();
192 depsgraph, sd, brush, angle, attribute_data, nodes[i], object, tls, position_data);
193 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
194 });
195 break;
196 }
198 SubdivCCG &subdiv_ccg = *object.sculpt->subdiv_ccg;
199 MutableSpan<float3> positions = subdiv_ccg.positions;
201 node_mask.foreach_index(GrainSize(1), [&](const int i) {
202 LocalData &tls = all_tls.local();
203 calc_grids(depsgraph, sd, object, brush, angle, nodes[i], tls);
204 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
205 });
206 break;
207 }
210 node_mask.foreach_index(GrainSize(1), [&](const int i) {
211 LocalData &tls = all_tls.local();
212 calc_bmesh(depsgraph, sd, object, brush, angle, nodes[i], tls);
214 });
215 break;
216 }
217 }
218 pbvh.tag_positions_changed(node_mask);
220}
221
222} // 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)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_NOINLINE
Object is a sort of wrapper for general info.
constexpr int64_t size() const
Definition BLI_span.hh:494
constexpr int64_t size() const
Definition BLI_span.hh:253
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
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_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:58
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:95
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_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float angle, bke::pbvh::BMeshNode &node, LocalData &tls)
static BLI_NOINLINE void calc_translations(const Span< float3 > positions, const float3 &axis, const Span< float > angles, const float3 &center, const MutableSpan< float3 > translations)
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)
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:6373
void do_rotate_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
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:7227
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:6337
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 apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6958
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:6408
MatT from_rotation(const RotationT &rotation)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
int grid_area
Definition BKE_ccg.hh:35
blender::ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:427
BMLog * bm_log
Definition BKE_paint.hh:402
SubdivCCG * subdiv_ccg
Definition BKE_paint.hh:405
blender::Array< blender::float3 > positions