Blender V4.5
curves_sculpt_comb.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
6
7#include "BLI_math_geom.h"
9#include "BLI_vector.hh"
10
11#include "DEG_depsgraph.hh"
13
14#include "BKE_brush.hh"
15#include "BKE_colortools.hh"
16#include "BKE_context.hh"
17#include "BKE_crazyspace.hh"
18#include "BKE_curves.hh"
19#include "BKE_geometry_set.hh"
20#include "BKE_mesh.hh"
21#include "BKE_mesh_runtime.hh"
22#include "BKE_paint.hh"
23
24#include "DNA_brush_enums.h"
25#include "DNA_brush_types.h"
26#include "DNA_curves_types.h"
27#include "DNA_object_types.h"
28#include "DNA_screen_types.h"
29
30#include "ED_screen.hh"
31#include "ED_view3d.hh"
32
33#include "UI_interface.hh"
34
35#include "WM_api.hh"
36
37#include <numeric>
38
46
48
49using blender::bke::CurvesGeometry;
50
55 private:
57 float2 brush_pos_last_re_;
58
60 CurvesBrush3D brush_3d_;
61
63 CurvesConstraintSolver constraint_solver_;
64
65 Array<float> curve_lengths_;
66
67 friend struct CombOperationExecutor;
68
69 public:
70 void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override;
71};
72
78 CombOperation *self_ = nullptr;
80
81 const CurvesSculpt *curves_sculpt_ = nullptr;
82 const Brush *brush_ = nullptr;
86
90
94
98
100
102
103 void execute(CombOperation &self, const bContext &C, const StrokeExtension &stroke_extension)
104 {
105 self_ = &self;
106
107 BLI_SCOPED_DEFER([&]() { self_->brush_pos_last_re_ = stroke_extension.mouse_position; });
108
110 curves_id_orig_ = static_cast<Curves *>(curves_ob_orig_->data);
111 curves_orig_ = &curves_id_orig_->geometry.wrap();
112 if (curves_orig_->is_empty()) {
113 return;
114 }
115
116 curves_sculpt_ = ctx_.scene->toolsettings->curves_sculpt;
119 brush_radius_factor_ = brush_radius_factor(*brush_, stroke_extension);
120 brush_strength_ = brush_strength_get(*ctx_.scene, *brush_, stroke_extension);
121
122 const eBrushFalloffShape falloff_shape = eBrushFalloffShape(brush_->falloff_shape);
123
125
126 point_factors_ = *curves_orig_->attributes().lookup_or_default<float>(
127 ".selection", bke::AttrDomain::Point, 1.0f);
129
130 brush_pos_prev_re_ = self_->brush_pos_last_re_;
131 brush_pos_re_ = stroke_extension.mouse_position;
133
134 if (stroke_extension.is_first) {
135 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE || (U.uiflag & USER_ORBIT_SELECTION)) {
137 }
138 self_->constraint_solver_.initialize(*curves_orig_,
141 curves_id_orig_->surface_collision_distance);
142
143 self_->curve_lengths_.reinitialize(curves_orig_->curves_num());
144 const Span<float> segment_lengths = self_->constraint_solver_.segment_lengths();
145 const OffsetIndices points_by_curve = curves_orig_->points_by_curve();
146 curve_selection_.foreach_segment(GrainSize(512), [&](const IndexMaskSegment segment) {
147 for (const int curve_i : segment) {
148 const IndexRange points = points_by_curve[curve_i];
149 const Span<float> lengths = segment_lengths.slice(points.drop_back(1));
150 self_->curve_lengths_[curve_i] = std::accumulate(lengths.begin(), lengths.end(), 0.0f);
151 }
152 });
153 /* Combing does nothing when there is no mouse movement, so return directly. */
154 return;
155 }
156
157 Array<bool> changed_curves(curves_orig_->curves_num(), false);
158
159 if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
160 this->comb_projected_with_symmetry(changed_curves);
161 }
162 else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
163 this->comb_spherical_with_symmetry(changed_curves);
164 }
165 else {
167 }
168
169 const Mesh *surface = curves_id_orig_->surface && curves_id_orig_->surface->type == OB_MESH ?
170 static_cast<Mesh *>(curves_id_orig_->surface->data) :
171 nullptr;
172
173 IndexMaskMemory memory;
174 const IndexMask changed_curves_mask = IndexMask::from_bools(changed_curves, memory);
175 self_->constraint_solver_.solve_step(*curves_orig_, changed_curves_mask, surface, transforms_);
176
177 curves_orig_->tag_positions_changed();
181 }
182
187 {
188 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
190 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
191 this->comb_projected(r_changed_curves, brush_transform);
192 }
193 }
194
195 void comb_projected(MutableSpan<bool> r_changed_curves, const float4x4 &brush_transform)
196 {
197 const float4x4 brush_transform_inv = math::invert(brush_transform);
198
199 MutableSpan<float3> positions_cu_orig = curves_orig_->positions_for_write();
200 const bke::crazyspace::GeometryDeformation deformation =
202 const OffsetIndices points_by_curve = curves_orig_->points_by_curve();
203
205
206 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
207 const float brush_radius_sq_re = pow2f(brush_radius_re);
208
209 CurveMapping &curve_parameter_falloff_mapping =
210 *brush_->curves_sculpt_settings->curve_parameter_falloff;
211 BKE_curvemapping_init(&curve_parameter_falloff_mapping);
212
213 const Span<float> segment_lengths = self_->constraint_solver_.segment_lengths();
214
215 curve_selection_.foreach_segment(GrainSize(256), [&](const IndexMaskSegment segment) {
216 for (const int curve_i : segment) {
217 bool curve_changed = false;
218 const IndexRange points = points_by_curve[curve_i];
219
220 const float total_length = self_->curve_lengths_[curve_i];
221 const float total_length_inv = math::safe_rcp(total_length);
222 float current_length = 0.0f;
223 for (const int point_i : points.drop_front(1)) {
224 current_length += segment_lengths[point_i - 1];
225
226 const float3 old_pos_cu = deformation.positions[point_i];
227 const float3 old_symm_pos_cu = math::transform_point(brush_transform_inv, old_pos_cu);
228
229 /* Find the position of the point in screen space. */
230 const float2 old_symm_pos_re = ED_view3d_project_float_v2_m4(
231 ctx_.region, old_symm_pos_cu, projection);
232
233 const float distance_to_brush_sq_re = dist_squared_to_line_segment_v2(
234 old_symm_pos_re, brush_pos_prev_re_, brush_pos_re_);
235 if (distance_to_brush_sq_re > brush_radius_sq_re) {
236 /* Ignore the point because it's too far away. */
237 continue;
238 }
239
240 const float distance_to_brush_re = std::sqrt(distance_to_brush_sq_re);
241 /* A falloff that is based on how far away the point is from the stroke. */
242 const float radius_falloff = BKE_brush_curve_strength(
243 brush_, distance_to_brush_re, brush_radius_re);
244 const float curve_parameter = current_length * total_length_inv;
245 const float curve_falloff = BKE_curvemapping_evaluateF(
246 &curve_parameter_falloff_mapping, 0, curve_parameter);
247 /* Combine the falloff and brush strength. */
248 const float weight = brush_strength_ * curve_falloff * radius_falloff *
249 point_factors_[point_i];
250
251 /* Offset the old point position in screen space and transform it back into 3D space.
252 */
253 const float2 new_symm_pos_re = old_symm_pos_re + brush_pos_diff_re_ * weight;
254 float3 new_symm_pos_wo;
256 ctx_.region,
257 math::transform_point(transforms_.curves_to_world, old_symm_pos_cu),
258 new_symm_pos_re,
259 new_symm_pos_wo);
260 const float3 new_pos_cu = math::transform_point(
261 brush_transform,
262 math::transform_point(transforms_.world_to_curves, new_symm_pos_wo));
263
264 const float3 translation_eval = new_pos_cu - old_pos_cu;
265 const float3 translation_orig = deformation.translation_from_deformed_to_original(
266 point_i, translation_eval);
267 positions_cu_orig[point_i] += translation_orig;
268
269 curve_changed = true;
270 }
271 if (curve_changed) {
272 r_changed_curves[curve_i] = true;
273 }
274 }
275 });
276 }
277
282 {
283 float3 brush_start_wo, brush_end_wo;
285 ctx_.v3d,
286 ctx_.region,
287 math::transform_point(transforms_.curves_to_world, self_->brush_3d_.position_cu),
289 brush_start_wo);
291 ctx_.v3d,
292 ctx_.region,
293 math::transform_point(transforms_.curves_to_world, self_->brush_3d_.position_cu),
295 brush_end_wo);
296 const float3 brush_start_cu = math::transform_point(transforms_.world_to_curves,
297 brush_start_wo);
298 const float3 brush_end_cu = math::transform_point(transforms_.world_to_curves, brush_end_wo);
299
300 const float brush_radius_cu = self_->brush_3d_.radius_cu * brush_radius_factor_;
301
302 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
304 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
305 this->comb_spherical(r_changed_curves,
306 math::transform_point(brush_transform, brush_start_cu),
307 math::transform_point(brush_transform, brush_end_cu),
308 brush_radius_cu);
309 }
310 }
311
312 void comb_spherical(MutableSpan<bool> r_changed_curves,
313 const float3 &brush_start_cu,
314 const float3 &brush_end_cu,
315 const float brush_radius_cu)
316 {
317 MutableSpan<float3> positions_cu = curves_orig_->positions_for_write();
318 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
319 const float3 brush_diff_cu = brush_end_cu - brush_start_cu;
320
321 CurveMapping &curve_parameter_falloff_mapping =
322 *brush_->curves_sculpt_settings->curve_parameter_falloff;
323 BKE_curvemapping_init(&curve_parameter_falloff_mapping);
324
325 const bke::crazyspace::GeometryDeformation deformation =
327 const OffsetIndices points_by_curve = curves_orig_->points_by_curve();
328 const Span<float> segment_lengths = self_->constraint_solver_.segment_lengths();
329
330 curve_selection_.foreach_segment(GrainSize(256), [&](const IndexMaskSegment segment) {
331 for (const int curve_i : segment) {
332 bool curve_changed = false;
333 const IndexRange points = points_by_curve[curve_i];
334
335 const float total_length = self_->curve_lengths_[curve_i];
336 const float total_length_inv = math::safe_rcp(total_length);
337 float current_length = 0.0f;
338 for (const int point_i : points.drop_front(1)) {
339 current_length += segment_lengths[point_i - 1];
340
341 const float3 pos_old_cu = deformation.positions[point_i];
342
343 /* Compute distance to the brush. */
344 const float distance_to_brush_sq_cu = dist_squared_to_line_segment_v3(
345 pos_old_cu, brush_start_cu, brush_end_cu);
346 if (distance_to_brush_sq_cu > brush_radius_sq_cu) {
347 /* Ignore the point because it's too far away. */
348 continue;
349 }
350
351 const float distance_to_brush_cu = std::sqrt(distance_to_brush_sq_cu);
352
353 /* A falloff that is based on how far away the point is from the stroke. */
354 const float radius_falloff = BKE_brush_curve_strength(
355 brush_, distance_to_brush_cu, brush_radius_cu);
356 const float curve_parameter = current_length * total_length_inv;
357 const float curve_falloff = BKE_curvemapping_evaluateF(
358 &curve_parameter_falloff_mapping, 0, curve_parameter);
359 /* Combine the falloff and brush strength. */
360 const float weight = brush_strength_ * curve_falloff * radius_falloff *
361 point_factors_[point_i];
362
363 const float3 translation_eval_cu = weight * brush_diff_cu;
364 const float3 translation_orig_cu = deformation.translation_from_deformed_to_original(
365 point_i, translation_eval_cu);
366
367 /* Update the point position. */
368 positions_cu[point_i] += translation_orig_cu;
369 curve_changed = true;
370 }
371 if (curve_changed) {
372 r_changed_curves[curve_i] = true;
373 }
374 }
375 });
376 }
377
382 {
383 std::optional<CurvesBrush3D> brush_3d = sample_curves_3d_brush(*ctx_.depsgraph,
384 *ctx_.region,
385 *ctx_.v3d,
386 *ctx_.rv3d,
390 if (brush_3d.has_value()) {
391 self_->brush_3d_ = *brush_3d;
393 *ctx_.scene,
394 math::transform_point(transforms_.curves_to_world, self_->brush_3d_.position_cu));
395 }
396 }
397};
398
399void CombOperation::on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension)
400{
401 CombOperationExecutor executor{C};
402 executor.execute(*this, C, stroke_extension);
403}
404
405std::unique_ptr<CurvesSculptStrokeOperation> new_comb_operation()
406{
407 return std::make_unique<CombOperation>();
408}
409
410} // namespace blender::ed::sculpt_paint
int BKE_brush_size_get(const Scene *scene, const Brush *brush)
Definition brush.cc:1210
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1510
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
void BKE_curvemapping_init(CurveMapping *cumap)
Object * CTX_data_active_object(const bContext *C)
Low-level operations for curves.
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Definition paint.cc:641
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
MINLINE float pow2f(float x)
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:519
float dist_squared_to_line_segment_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:291
#define BLI_SCOPED_DEFER(function_to_defer)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:982
eBrushFalloffShape
@ PAINT_FALLOFF_SHAPE_SPHERE
@ PAINT_FALLOFF_SHAPE_TUBE
eCurvesSymmetryType
@ CV_SCULPT_COLLISION_ENABLED
Object is a sort of wrapper for general info.
@ OB_MESH
@ USER_ORBIT_SELECTION
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:639
blender::float2 ED_view3d_project_float_v2_m4(const ARegion *region, const float co[3], const blender::float4x4 &mat)
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])
blender::float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:390
#define ND_DATA
Definition WM_types.hh:506
#define U
PyObject * self
static IndexMask from_bools(Span< bool > bools, IndexMaskMemory &memory)
constexpr IndexRange drop_back(int64_t n) const
constexpr IndexRange drop_front(int64_t n) const
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:137
constexpr const T * end() const
Definition BLI_span.hh:224
constexpr const T * begin() const
Definition BLI_span.hh:220
void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, IndexMaskMemory &memory)
std::unique_ptr< CurvesSculptStrokeOperation > new_comb_operation()
void remember_stroke_position(Scene &scene, const float3 &brush_position_wo)
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)
float brush_strength_get(const Scene &scene, const Brush &brush, const StrokeExtension &stroke_extension)
Vector< float4x4 > get_symmetry_brush_transforms(const eCurvesSymmetryType symmetry)
float brush_radius_factor(const Brush &brush, const StrokeExtension &stroke_extension)
T safe_rcp(const T &a)
CartesianBasis invert(const CartesianBasis &basis)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
MatBase< float, 4, 4 > float4x4
VecBase< float, 2 > float2
VecBase< float, 3 > float3
float3 translation_from_deformed_to_original(const int position_i, const float3 &translation) const
void comb_spherical_with_symmetry(MutableSpan< bool > r_changed_curves)
void comb_projected_with_symmetry(MutableSpan< bool > r_changed_curves)
void comb_projected(MutableSpan< bool > r_changed_curves, const float4x4 &brush_transform)
void comb_spherical(MutableSpan< bool > r_changed_curves, const float3 &brush_start_cu, const float3 &brush_end_cu, const float brush_radius_cu)
void execute(CombOperation &self, const bContext &C, const StrokeExtension &stroke_extension)
void initialize(const bke::CurvesGeometry &curves, const IndexMask &curve_selection, const bool use_surface_collision, const float surface_collision_distance)
void WM_main_add_notifier(uint type, void *reference)