Blender V5.3
sculpt_puff.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_brush.hh"
6#include "BKE_bvh.hh"
7#include "BKE_bvhutils.hh"
8#include "BKE_context.hh"
9#include "BKE_crazyspace.hh"
10#include "BKE_mesh.hh"
11#include "BKE_mesh_runtime.hh"
12#include "BKE_paint.hh"
13
14#include "ED_screen.hh"
15#include "ED_view3d.hh"
16
17#include "DEG_depsgraph.hh"
18
19#include "DNA_brush_types.h"
20
21#include "WM_api.hh"
22
24#include "BLI_math_geom_c.hh"
25#include "BLI_math_matrix.hh"
26#include "BLI_task.hh"
27
29
30#include "sculpt_intern.hh"
31
33
35 private:
37 CurvesBrush3D brush_3d_;
38
40 CurvesConstraintSolver constraint_solver_;
41
42 friend struct PuffOperationExecutor;
43
44 public:
45 void on_stroke_extended(const PaintStroke &stroke,
46 const StrokeExtension &stroke_extension) override;
47};
48
54 PuffOperation *self_ = nullptr;
56
57 Object *object_ = nullptr;
58 Curves *curves_id_ = nullptr;
60
64
66 const Brush *brush_ = nullptr;
71
73
74 const Object *surface_ob_ = nullptr;
75 const Mesh *surface_ = nullptr;
81
82 PuffOperationExecutor(const PaintStroke &stroke) : ctx_(stroke) {}
83
84 void execute(PuffOperation &self, const StrokeExtension &stroke_extension)
85 {
86 self_ = &self;
87
89 curves_id_ = id_cast<Curves *>(object_->data);
90 curves_ = &curves_id_->geometry.wrap();
91 if (curves_->is_empty()) {
92 return;
93 }
94 if (curves_id_->surface == nullptr || curves_id_->surface->type != OB_MESH) {
95 report_missing_surface(stroke_extension.reports);
96 return;
97 }
98
102 brush_radius_factor_ = brush_radius_factor(*brush_, stroke_extension);
104 brush_pos_re_ = stroke_extension.mouse_position;
105
107 ".selection", bke::AttrDomain::Point, 1.0f);
109
111
112 surface_ob_ = curves_id_->surface;
113 surface_ = id_cast<const Mesh *>(surface_ob_->data);
114
116
117 surface_positions_ = surface_->vert_positions();
118 surface_corner_verts_ = surface_->corner_verts();
119 surface_corner_tris_ = surface_->corner_tris();
120 corner_normals_su_ = surface_->corner_normals();
121 surface_bvh_ = &surface_->bvh_tris();
122
123 if (stroke_extension.is_first) {
124 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE || (U.uiflag & USER_ORBIT_SELECTION)) {
125 self.brush_3d_ = *sample_curves_3d_brush(*ctx_.depsgraph,
126 *ctx_.region,
127 *ctx_.v3d,
128 *ctx_.rv3d,
129 *object_,
135 }
136
137 self_->constraint_solver_.initialize(*curves_,
140 curves_id_->surface_collision_distance);
141 }
142
143 Array<float> curve_weights(curves_->curves_num(), 0.0f);
144
145 if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
147 }
148 else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
150 }
151 else {
153 }
154
155 IndexMaskMemory memory;
156 const IndexMask curves_mask = IndexMask::from_predicate(
157 curve_selection_, memory, [&](const int64_t curve_i) {
158 return curve_weights[curve_i] > 0.0f;
159 });
160
161 this->puff(curves_mask, curve_weights);
162
163 self_->constraint_solver_.solve_step(*curves_, curves_mask, surface_, transforms_);
164
169 }
170
172 {
173 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
175 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
176 this->find_curve_weights_projected(brush_transform, r_curve_weights);
177 }
178 }
179
180 void find_curve_weights_projected(const float4x4 &brush_transform,
181 MutableSpan<float> r_curve_weights)
182 {
183 const float4x4 brush_transform_inv = math::invert(brush_transform);
184
186
187 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
188 const float brush_radius_sq_re = pow2f(brush_radius_re);
189
190 const bke::crazyspace::GeometryDeformation deformation =
192 const OffsetIndices points_by_curve = curves_->points_by_curve();
193
195 [&](const int64_t curve_i) {
196 const IndexRange points = points_by_curve[curve_i];
197 const float3 first_pos_cu = math::transform_point(brush_transform_inv,
198 deformation.positions[points[0]]);
200 ctx_.region, first_pos_cu, projection);
201 float max_weight = 0.0f;
202 for (const int point_i : points.drop_front(1)) {
203 const float3 pos_cu = math::transform_point(brush_transform_inv,
204 deformation.positions[point_i]);
205 const float2 pos_re = ED_view3d_project_float_v2_m4(ctx_.region, pos_cu, projection);
206 BLI_SCOPED_DEFER([&]() { prev_pos_re = pos_re; });
207
208 const float dist_to_brush_sq_re = dist_squared_to_line_segment_v2(
209 brush_pos_re_, prev_pos_re, pos_re);
210 if (dist_to_brush_sq_re > brush_radius_sq_re) {
211 continue;
212 }
213
214 const float dist_to_brush_re = std::sqrt(dist_to_brush_sq_re);
215 const float radius_falloff = BKE_brush_curve_strength(
216 brush_, dist_to_brush_re, brush_radius_re);
217 math::max_inplace(max_weight, radius_falloff);
218 }
219 math::max_inplace(r_curve_weights[curve_i], max_weight);
220 },
222 }
223
225 {
226 float3 brush_pos_wo;
228 ctx_.v3d,
229 ctx_.region,
232 brush_pos_wo);
233 const float3 brush_pos_cu = math::transform_point(transforms_.world_to_curves, brush_pos_wo);
234 const float brush_radius_cu = self_->brush_3d_.radius_cu * brush_radius_factor_;
235
236 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
238 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
240 math::transform_point(brush_transform, brush_pos_cu), brush_radius_cu, r_curve_weights);
241 }
242 }
243
244 void find_curves_weights_spherical(const float3 &brush_pos_cu,
245 const float brush_radius_cu,
246 MutableSpan<float> r_curve_weights)
247 {
248 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
249
250 const bke::crazyspace::GeometryDeformation deformation =
252 const OffsetIndices points_by_curve = curves_->points_by_curve();
253
255 [&](const int64_t curve_i) {
256 const IndexRange points = points_by_curve[curve_i];
257 float max_weight = 0.0f;
258 for (const int point_i : points.drop_front(1)) {
259 const float3 &prev_pos_cu = deformation.positions[point_i - 1];
260 const float3 &pos_cu = deformation.positions[point_i];
261 const float dist_to_brush_sq_cu = dist_squared_to_line_segment_v3(
262 brush_pos_cu, prev_pos_cu, pos_cu);
263 if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
264 continue;
265 }
266
267 const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
268 const float radius_falloff = BKE_brush_curve_strength(
269 brush_, dist_to_brush_cu, brush_radius_cu);
270 math::max_inplace(max_weight, radius_falloff);
271 }
272 math::max_inplace(r_curve_weights[curve_i], max_weight);
273 },
275 }
276
277 void puff(const IndexMask &selection, const Span<float> curve_weights)
278 {
279 const OffsetIndices points_by_curve = curves_->points_by_curve();
281
282 selection.foreach_segment(
283 [&](IndexMaskSegment segment) {
284 Vector<float> accumulated_lengths_cu;
285 for (const int curve_i : segment) {
286 const IndexRange points = points_by_curve[curve_i];
287 const int first_point_i = points[0];
288 const float3 first_pos_cu = positions_cu[first_point_i];
290 first_pos_cu);
291
292 /* Find the nearest position on the surface. The curve will be aligned to the normal of
293 * that point. */
294 const std::optional<bke::bvh::ClosestPointResult> nearest =
295 surface_bvh_->closest_point(first_pos_su);
296 if (!nearest) {
297 continue;
298 }
299
300 const int3 &tri = surface_corner_tris_[nearest->index];
301 const float3 closest_pos_su = nearest->position;
302 const float3 &v0_su = surface_positions_[surface_corner_verts_[tri[0]]];
303 const float3 &v1_su = surface_positions_[surface_corner_verts_[tri[1]]];
304 const float3 &v2_su = surface_positions_[surface_corner_verts_[tri[2]]];
305 float3 bary_coords;
306 interp_weights_tri_v3(bary_coords, v0_su, v1_su, v2_su, closest_pos_su);
308 tri, bary_coords, corner_normals_su_);
309 const float3 normal_cu = math::normalize(
311
312 accumulated_lengths_cu.resize(points.size() - 1);
314 positions_cu.slice(points), false, accumulated_lengths_cu);
315
316 /* Align curve to the surface normal while making sure that the curve does not fold up
317 * much in the process (e.g. when the curve was pointing in the opposite direction
318 * before). */
319 for (const int i : IndexRange(points.size()).drop_front(1)) {
320 const int point_i = points[i];
321 const float3 old_pos_cu = positions_cu[point_i];
322
323 /* Compute final position of the point. */
324 const float length_param_cu = accumulated_lengths_cu[i - 1];
325 const float3 goal_pos_cu = first_pos_cu + length_param_cu * normal_cu;
326
327 const float weight = 0.01f * brush_strength_ * point_factors_[point_i] *
328 curve_weights[curve_i];
329 float3 new_pos_cu = math::interpolate(old_pos_cu, goal_pos_cu, weight);
330
331 /* Make sure the point does not move closer to the root point than it was initially.
332 * This makes the curve kind of "rotate up". */
333 const float old_dist_to_root_cu = math::distance(old_pos_cu, first_pos_cu);
334 const float new_dist_to_root_cu = math::distance(new_pos_cu, first_pos_cu);
335 if (new_dist_to_root_cu < old_dist_to_root_cu) {
336 const float3 offset = math::normalize(new_pos_cu - first_pos_cu);
337 new_pos_cu += (old_dist_to_root_cu - new_dist_to_root_cu) * offset;
338 }
339
340 positions_cu[point_i] = new_pos_cu;
341 }
342 }
343 },
345 }
346};
347
349 const StrokeExtension &stroke_extension)
350{
351 PuffOperationExecutor executor{stroke};
352 executor.execute(*this, stroke_extension);
353}
354
355std::unique_ptr<CurvesSculptStrokeOperation> new_puff_operation()
356{
357 return std::make_unique<PuffOperation>();
358}
359
360} // namespace blender::ed::sculpt_paint
#define BLI_SCOPED_DEFER(function_to_defer)
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define BLI_assert_unreachable()
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr IndexRange drop_front(int64_t n) const
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
void resize(const int64_t new_size)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
MutableSpan< float3 > positions_for_write()
AttributeAccessor attributes() const
OffsetIndices< int > points_by_curve() const
std::optional< ClosestPointResult > closest_point(const float3 &point, float radius=std::numeric_limits< float >::max()) const
Definition bvh.cc:709
void on_stroke_extended(const PaintStroke &stroke, const StrokeExtension &stroke_extension) override
void foreach_segment(Fn &&fn, Mode mode=exec_mode::serial) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, LinearAllocator<> &memory)
float brush_strength_get(const Paint &paint, const Brush &brush, const StrokeExtension &stroke_extension)
std::optional< CurvesBrush3D > sample_curves_3d_brush(const Depsgraph &depsgraph, const ARegion &region, const View3D &v3d, const RegionView3D &rv3d, const Object &curves_object, const float2 &brush_pos_re, const float brush_radius_re)
void remember_stroke_position(CurvesSculpt &curves_sculpt, const float3 &brush_position_wo)
void report_missing_surface(ReportList *reports)
Vector< float4x4 > get_symmetry_brush_transforms(const eCurvesSymmetryType symmetry)
std::unique_ptr< CurvesSculptStrokeOperation > new_puff_operation()
float brush_radius_factor(const Brush &brush, const StrokeExtension &stroke_extension)
constexpr ParallelGrainSize grain_size(int grain_size)
float3 compute_surface_point_normal(const int3 &tri, const float3 &bary_coord, Span< float3 > corner_normals)
void accumulate_lengths(const Span< T > values, const bool cyclic, MutableSpan< float > lengths)
T distance(const T &a, const T &b)
CartesianBasis invert(const CartesianBasis &basis)
T interpolate(const T &a, const T &b, const FactorT &t)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
void max_inplace(T &a, const T &b)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void WM_main_add_notifier(uint type, void *reference)
UserDef U
Definition blender.cc:58
__int64 int64_t
Definition BKE_text.h:90
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
float BKE_brush_radius_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1366
@ PAINT_FALLOFF_SHAPE_SPHERE
@ PAINT_FALLOFF_SHAPE_TUBE
float2 ED_view3d_project_float_v2_m4(const ARegion *region, const float co[3], const float4x4 &mat)
MatBase< float, 4, 4 > float4x4
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
MINLINE float pow2f(float x)
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:307
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
void DEG_id_tag_update(ID *id, unsigned int flags)
void interp_weights_tri_v3(float w[3], const float v1[3], const float v2[3], const float v3[3], const float co[3])
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1691
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:535
@ CV_SCULPT_COLLISION_ENABLED
const Brush * BKE_paint_brush_for_read(const Paint *paint)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
VecBase< float, 3 > float3
void ED_view3d_win_to_3d(const View3D *v3d, const ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
eBrushFalloffShape falloff_shape
struct ToolSettings * toolsettings
CurvesSculpt * curves_sculpt
void solve_step(bke::CurvesGeometry &curves, const IndexMask &curve_selection, const Mesh *surface, const CurvesSurfaceTransforms &transforms)
void initialize(const bke::CurvesGeometry &curves, const IndexMask &curve_selection, const bool use_surface_collision, const float surface_collision_distance)
void find_curve_weights_projected_with_symmetry(MutableSpan< float > r_curve_weights)
void find_curves_weights_spherical_with_symmetry(MutableSpan< float > r_curve_weights)
void puff(const IndexMask &selection, const Span< float > curve_weights)
void find_curves_weights_spherical(const float3 &brush_pos_cu, const float brush_radius_cu, MutableSpan< float > r_curve_weights)
PuffOperationExecutor(const PaintStroke &stroke)
void execute(PuffOperation &self, const StrokeExtension &stroke_extension)
void find_curve_weights_projected(const float4x4 &brush_transform, MutableSpan< float > r_curve_weights)