Blender V5.3
editors/sculpt_paint/mesh/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
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
22#include "BLI_array.hh"
24#include "BLI_math_matrix.hh"
25#include "BLI_math_rotation.hh"
26#include "BLI_task.hh"
27
31
32#include "bmesh.hh"
33
35
36inline namespace rotate_cc {
37
43
44BLI_NOINLINE static void calc_translations(const Span<float3> positions,
45 const float3 &axis,
46 const Span<float> angles,
47 const float3 &center,
48 const MutableSpan<float3> translations)
49{
51 BLI_assert(positions.size() == angles.size());
52 BLI_assert(positions.size() == translations.size());
53
54 for (const int i : positions.index_range()) {
55 const math::AxisAngle rotation(axis, angles[i]);
56 const float3x3 matrix = math::from_rotation<float3x3>(rotation);
57 const float3 rotated = math::transform_point(matrix, positions[i] - center);
58 translations[i] = rotated + center - positions[i];
59 }
60}
61
62static void calc_faces(const Depsgraph &depsgraph,
63 const Sculpt &sd,
64 const Brush &brush,
65 const float angle,
66 const MeshAttributeData &attribute_data,
68 Object &object,
69 const PositionDeformData &position_data)
70{
71 SculptSession &ss = *object.runtime->sculpt_session;
72 const StrokeCache &cache = *ss.cache;
73
74 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
75 const Span<int> verts = node.verts();
76
80 brush,
81 object,
82 attribute_data,
83 orig_data.positions,
84 orig_data.normals,
85 node,
86 factors,
87 distances);
88
89 scale_factors(factors, angle);
90
93 orig_data.positions, cache.sculpt_normal_symm, factors, cache.location_symm, translations);
94
95 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
96 position_data.deform(translations, verts);
97}
98
99static void calc_grids(const Depsgraph &depsgraph,
100 const Sculpt &sd,
101 Object &object,
102 const Brush &brush,
103 const float angle,
105 LocalData &tls)
106{
107 SculptSession &ss = *object.runtime->sculpt_session;
108 const StrokeCache &cache = *ss.cache;
109 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
110 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
111
112 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
113 const Span<int> grids = node.grids();
114 const int grid_verts_num = grids.size() * key.grid_area;
115
117 brush,
118 object,
119 orig_data.positions,
120 orig_data.normals,
121 node,
122 tls.factors,
123 tls.distances);
124
126
127 tls.translations.resize(grid_verts_num);
128 const MutableSpan<float3> translations = tls.translations;
130 cache.sculpt_normal_symm,
131 tls.factors,
132 cache.location_symm,
133 translations);
134
135 clip_and_lock_translations(sd, ss, orig_data.positions, translations);
136 apply_translations(translations, grids, subdiv_ccg);
137}
138
139static void calc_bmesh(const Depsgraph &depsgraph,
140 const Sculpt &sd,
141 Object &object,
142 const Brush &brush,
143 const float angle,
145 LocalData &tls)
146{
147 SculptSession &ss = *object.runtime->sculpt_session;
148 const StrokeCache &cache = *ss.cache;
149
151
152 Array<float3> orig_positions(verts.size());
153 Array<float3> orig_normals(verts.size());
154 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
155
157 depsgraph, brush, object, orig_positions, orig_normals, node, tls.factors, tls.distances);
158
160
161 tls.translations.resize(verts.size());
162 const MutableSpan<float3> translations = tls.translations;
164 orig_positions, cache.sculpt_normal_symm, tls.factors, cache.location_symm, translations);
165
166 clip_and_lock_translations(sd, ss, orig_positions, translations);
167 apply_translations(translations, verts);
168}
169
170} // namespace rotate_cc
171
172void do_rotate_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 constexpr std::array<int, 8> flip{1, -1, -1, 1, -1, 1, 1, -1};
183 const float angle = ss.cache->vertex_rotation * flip[ss.cache->mirror_symmetry_pass];
184
186 switch (pbvh.type()) {
188 const Mesh &mesh = *id_cast<Mesh *>(object.data);
189 const MeshAttributeData attribute_data(mesh);
190 const PositionDeformData position_data(depsgraph, object);
192 node_mask.foreach_index(
193 [&](const int i) {
195 depsgraph, sd, brush, angle, attribute_data, nodes[i], object, position_data);
196 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
197 },
199 break;
200 }
202 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
203 MutableSpan<float3> positions = subdiv_ccg.positions;
205 node_mask.foreach_index(
206 [&](const int i) {
207 LocalData &tls = all_tls.local();
208 calc_grids(depsgraph, sd, object, brush, angle, nodes[i], tls);
209 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
210 },
212 break;
213 }
216 node_mask.foreach_index(
217 [&](const int i) {
218 LocalData &tls = all_tls.local();
219 calc_bmesh(depsgraph, sd, object, brush, angle, nodes[i], tls);
221 },
223 break;
224 }
225 }
226 pbvh.tag_positions_changed(node_mask);
227 pbvh.flush_bounds_to_parents();
228}
229
230} // 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
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
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
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_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:7008
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, MutableSpan< float > r_factors, MutableSpan< float > r_distances)
Definition sculpt.cc:6976
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: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 apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
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:7043
constexpr ParallelGrainSize grain_size(int grain_size)
AxisAngleBase< float, AngleRadianBase< float > > AxisAngle
MatT from_rotation(const RotationT &rotation)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
BMesh const char void * data
MatBase< float, 3, 3 > float3x3
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