Blender V5.3
sculpt_grow_shrink.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 <algorithm>
6
7#include "BLI_math_vector.hh"
8
10#include "BLI_math_geom_c.hh"
11#include "BLI_math_matrix.hh"
12#include "BLI_task.hh"
13#include "BLI_vector.hh"
14
15#include "DEG_depsgraph.hh"
16
17#include "BKE_brush.hh"
18#include "BKE_context.hh"
19#include "BKE_curves.hh"
20#include "BKE_paint.hh"
21
22#include "DNA_brush_enums.h"
23#include "DNA_brush_types.h"
24#include "DNA_curves_types.h"
25#include "DNA_object_types.h"
26
27#include "ED_screen.hh"
28#include "ED_view3d.hh"
29
30#include "WM_api.hh"
31
32#include "sculpt_intern.hh"
33
41
43
44using bke::CurvesGeometry;
45
52 public:
53 virtual ~CurvesEffect() = default;
55 const IndexMask &curve_mask,
56 Span<float> move_distances_cu,
57 MutableSpan<float3> positions_cu) = 0;
58};
59
64 private:
65 const Brush &brush_;
66
68 struct ParameterizationBuffers {
69 Vector<float3> old_positions;
70 Vector<float> old_lengths;
71 Vector<float> sample_lengths;
72 Vector<int> indices;
73 Vector<float> factors;
74
75 void resize(const int points_num)
76 {
77 this->old_positions.resize(points_num);
78 this->old_lengths.resize(length_parameterize::segments_num(points_num, false));
79 this->sample_lengths.resize(points_num);
80 this->indices.resize(points_num);
81 this->factors.resize(points_num);
82 }
83 };
84
85 public:
86 ShrinkCurvesEffect(const Brush &brush) : brush_(brush) {}
87
89 const IndexMask &curve_mask,
90 const Span<float> move_distances_cu,
91 MutableSpan<float3> positions_cu) override
92 {
93 const OffsetIndices points_by_curve = curves.points_by_curve();
94 curve_mask.foreach_segment(
95 [&](IndexMaskSegment segment) {
96 ParameterizationBuffers data;
97 for (const int curve_i : segment) {
98 const float move_distance_cu = move_distances_cu[curve_i];
99 const IndexRange points = points_by_curve[curve_i];
100 this->shrink_curve(positions_cu.slice(points), move_distance_cu, data);
101 }
102 },
104 }
105
106 private:
107 void shrink_curve(MutableSpan<float3> positions,
108 const float shrink_length,
109 ParameterizationBuffers &data) const
110 {
111 namespace lp = length_parameterize;
112 data.resize(positions.size());
113
114 /* Copy the old positions to facilitate mixing from neighbors for the resulting curve. */
115 data.old_positions.as_mutable_span().copy_from(positions);
116
117 lp::accumulate_lengths<float3>(data.old_positions, false, data.old_lengths);
118
119 const float min_length = brush_.curves_sculpt_settings->minimum_length;
120 const float old_length = data.old_lengths.last();
121 const float new_length = std::max(min_length, old_length - shrink_length);
122 const float length_factor = std::clamp(new_length / old_length, 0.0f, 1.0f);
123
124 data.sample_lengths.first() = 0.0f;
125 for (const int i : data.old_lengths.index_range()) {
126 data.sample_lengths[i + 1] = data.old_lengths[i] * length_factor;
127 }
128
129 lp::sample_at_lengths(data.old_lengths, data.sample_lengths, data.indices, data.factors);
130
131 lp::interpolate<float3>(data.old_positions, data.indices, data.factors, positions);
132 }
133};
134
139 void execute(CurvesGeometry &curves,
140 const IndexMask &curve_mask,
141 const Span<float> move_distances_cu,
142 MutableSpan<float3> positions_cu) override
143 {
144 const OffsetIndices points_by_curve = curves.points_by_curve();
145 curve_mask.foreach_segment(
146 [&](IndexMaskSegment segment) {
147 MoveAndResampleBuffers resample_buffer;
148 for (const int curve_i : segment) {
149 const float move_distance_cu = move_distances_cu[curve_i];
150 const IndexRange points = points_by_curve[curve_i];
151 if (points.size() <= 1) {
152 continue;
153 }
154
155 const float3 old_last_pos_cu = positions_cu[points.last()];
156 /* Use some point within the curve rather than the end point to smooth out some random
157 * variation. */
158 const float3 direction_reference_point =
159 positions_cu[points.size() > 2 ? points[points.size() / 2] : points.first()];
160 const float3 direction = math::normalize(old_last_pos_cu - direction_reference_point);
161
162 const float3 new_last_pos_cu = old_last_pos_cu + direction * move_distance_cu;
164 resample_buffer, positions_cu.slice(points), new_last_pos_cu);
165 }
166 },
168 }
169};
170
175 private:
176 bool scale_up_;
177 const Brush &brush_;
178
179 public:
180 ScaleCurvesEffect(bool scale_up, const Brush &brush) : scale_up_(scale_up), brush_(brush) {}
181
183 const IndexMask &curve_mask,
184 const Span<float> move_distances_cu,
185 MutableSpan<float3> positions_cu) override
186 {
187 const OffsetIndices points_by_curve = curves.points_by_curve();
188 curve_mask.foreach_index(
189 [&](const int64_t curve_i) {
190 const float move_distance_cu = move_distances_cu[curve_i];
191 const IndexRange points = points_by_curve[curve_i];
192
193 const float old_length = this->compute_poly_curve_length(positions_cu.slice(points));
194 const float length_diff = scale_up_ ? move_distance_cu : -move_distance_cu;
195 const float min_length = brush_.curves_sculpt_settings->minimum_length;
196 const float new_length = std::max(min_length, old_length + length_diff);
197 const float scale_factor = math::safe_divide(new_length, old_length);
198
199 const float3 &root_pos_cu = positions_cu[points[0]];
200 for (float3 &pos_cu : positions_cu.slice(points.drop_front(1))) {
201 pos_cu = (pos_cu - root_pos_cu) * scale_factor + root_pos_cu;
202 }
203 },
205 }
206
208 {
209 float length = 0.0f;
210 const int segments_num = positions.size() - 1;
211 for (const int segment_i : IndexRange(segments_num)) {
212 const float3 &p1 = positions[segment_i];
213 const float3 &p2 = positions[segment_i + 1];
214 length += math::distance(p1, p2);
215 }
216 return length;
217 }
218};
219
221 private:
222 std::unique_ptr<CurvesEffect> effect_;
223 float2 last_mouse_position_;
224 CurvesBrush3D brush_3d_;
225
227
228 public:
229 CurvesEffectOperation(std::unique_ptr<CurvesEffect> effect) : effect_(std::move(effect)) {}
230
231 void on_stroke_extended(const PaintStroke &stroke,
232 const StrokeExtension &stroke_extension) override;
233};
234
242
243 Object *object_ = nullptr;
244 Curves *curves_id_ = nullptr;
246
250
252 const Brush *brush_ = nullptr;
256
258
260
263
265
266 void execute(CurvesEffectOperation &self, const StrokeExtension &stroke_extension)
267 {
268 BLI_SCOPED_DEFER([&]() { self.last_mouse_position_ = stroke_extension.mouse_position; });
269
270 self_ = &self;
272
273 curves_id_ = id_cast<Curves *>(object_->data);
274 curves_ = &curves_id_->geometry.wrap();
275 if (curves_->is_empty()) {
276 return;
277 }
278
280 ".selection", bke::AttrDomain::Curve, 1.0f);
282
286 brush_radius_factor_ = brush_radius_factor(*brush_, stroke_extension);
288
290
292
293 brush_pos_start_re_ = self.last_mouse_position_;
294 brush_pos_end_re_ = stroke_extension.mouse_position;
295
296 if (stroke_extension.is_first) {
298 if (std::optional<CurvesBrush3D> brush_3d = sample_curves_3d_brush(
300 *ctx_.region,
301 *ctx_.v3d,
302 *ctx_.rv3d,
303 *object_,
304 stroke_extension.mouse_position,
306 {
307 self.brush_3d_ = *brush_3d;
310 math::transform_point(transforms_.curves_to_world, self_->brush_3d_.position_cu));
311 }
312 }
313
314 return;
315 }
316
317 Array<float> move_distances_cu(curves_->curves_num());
318
319 /* Compute influences. */
321 this->gather_influences_projected(move_distances_cu);
322 }
324 this->gather_influences_spherical(move_distances_cu);
325 }
326
327 IndexMaskMemory memory;
328 const IndexMask curves_mask = IndexMask::from_predicate(
329 curve_selection_, memory, [&](const int64_t curve_i) {
330 return move_distances_cu[curve_i] > 0.0f;
331 });
332
333 /* Execute effect. */
335 self_->effect_->execute(*curves_, curves_mask, move_distances_cu, positions_cu);
336
341 }
342
344 {
345 const bke::crazyspace::GeometryDeformation deformation =
347 const OffsetIndices points_by_curve = curves_->points_by_curve();
348
350
351 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
353 Vector<float4x4> symmetry_brush_transforms_inv;
354 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
355 /* Use explicit template call as MSVC 2019 has issues deducing the right template. */
356 symmetry_brush_transforms_inv.append(math::invert<float, 4>(brush_transform));
357 }
358
359 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
360 const float brush_radius_sq_re = pow2f(brush_radius_re);
361
363 [&](int64_t curve_i) {
364 const IndexRange points = points_by_curve[curve_i];
365
366 const float curve_selection_factor = curve_selection_factors_[curve_i];
367
368 float max_move_distance_cu = 0.0f;
369 for (const float4x4 &brush_transform_inv : symmetry_brush_transforms_inv) {
370 for (const int segment_i : points.drop_back(1)) {
371 const float3 p1_cu = math::transform_point(brush_transform_inv,
372 deformation.positions[segment_i]);
373 const float3 p2_cu = math::transform_point(brush_transform_inv,
374 deformation.positions[segment_i + 1]);
375
376 const float2 p1_re = ED_view3d_project_float_v2_m4(ctx_.region, p1_cu, projection);
377 const float2 p2_re = ED_view3d_project_float_v2_m4(ctx_.region, p2_cu, projection);
378
379 float2 closest_on_brush_re;
380 float2 closest_on_segment_re;
381 float lambda_on_brush;
382 float lambda_on_segment;
383 const float dist_to_brush_sq_re = closest_seg_seg_v2(closest_on_brush_re,
384 closest_on_segment_re,
385 &lambda_on_brush,
386 &lambda_on_segment,
389 p1_re,
390 p2_re);
391
392 if (dist_to_brush_sq_re > brush_radius_sq_re) {
393 continue;
394 }
395
396 const float dist_to_brush_re = std::sqrt(dist_to_brush_sq_re);
397 const float radius_falloff = BKE_brush_curve_strength(
398 brush_, dist_to_brush_re, brush_radius_re);
399 const float weight = brush_strength_ * radius_falloff * curve_selection_factor;
400
401 const float3 closest_on_segment_cu = math::interpolate(
402 p1_cu, p2_cu, lambda_on_segment);
403
404 float3 brush_start_pos_wo, brush_end_pos_wo;
406 ctx_.v3d,
407 ctx_.region,
408 math::transform_point(transforms_.curves_to_world, closest_on_segment_cu),
410 brush_start_pos_wo);
412 ctx_.v3d,
413 ctx_.region,
414 math::transform_point(transforms_.curves_to_world, closest_on_segment_cu),
416 brush_end_pos_wo);
417 const float3 brush_start_pos_cu = math::transform_point(transforms_.world_to_curves,
418 brush_start_pos_wo);
420 brush_end_pos_wo);
421
422 const float move_distance_cu = weight *
423 math::distance(brush_start_pos_cu, brush_end_pos_cu);
424 max_move_distance_cu = std::max(max_move_distance_cu, move_distance_cu);
425 }
426 }
427 move_distances_cu[curve_i] = max_move_distance_cu;
428 },
430 }
431
433 {
434 const bke::crazyspace::GeometryDeformation deformation =
436
438 self_->brush_3d_.position_cu);
439
440 float3 brush_pos_start_wo, brush_pos_end_wo;
442 ctx_.v3d, ctx_.region, brush_pos_wo, brush_pos_start_re_, brush_pos_start_wo);
443 ED_view3d_win_to_3d(ctx_.v3d, ctx_.region, brush_pos_wo, brush_pos_end_re_, brush_pos_end_wo);
444 const float3 brush_pos_start_cu = math::transform_point(transforms_.world_to_curves,
445 brush_pos_start_wo);
447 brush_pos_end_wo);
448 const float3 brush_pos_diff_cu = brush_pos_end_cu - brush_pos_start_cu;
449 const float brush_pos_diff_length_cu = math::length(brush_pos_diff_cu);
450 const float brush_radius_cu = self_->brush_3d_.radius_cu * brush_radius_factor_;
451 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
452
453 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
455 const OffsetIndices points_by_curve = curves_->points_by_curve();
456
458 [&](int64_t curve_i) {
459 const IndexRange points = points_by_curve[curve_i];
460
461 const float curve_selection_factor = curve_selection_factors_[curve_i];
462
463 float max_move_distance_cu = 0.0f;
464 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
465 const float3 brush_pos_start_transformed_cu = math::transform_point(
466 brush_transform, brush_pos_start_cu);
467 const float3 brush_pos_end_transformed_cu = math::transform_point(brush_transform,
468 brush_pos_end_cu);
469
470 for (const int segment_i : points.drop_back(1)) {
471 const float3 &p1_cu = deformation.positions[segment_i];
472 const float3 &p2_cu = deformation.positions[segment_i + 1];
473
474 float3 closest_on_segment_cu;
475 float3 closest_on_brush_cu;
476 isect_seg_seg_v3(p1_cu,
477 p2_cu,
478 brush_pos_start_transformed_cu,
479 brush_pos_end_transformed_cu,
480 closest_on_segment_cu,
481 closest_on_brush_cu);
482
483 const float dist_to_brush_sq_cu = math::distance_squared(closest_on_segment_cu,
484 closest_on_brush_cu);
485 if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
486 continue;
487 }
488
489 const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
490 const float radius_falloff = BKE_brush_curve_strength(
491 brush_, dist_to_brush_cu, brush_radius_cu);
492 const float weight = brush_strength_ * radius_falloff * curve_selection_factor;
493
494 const float move_distance_cu = weight * brush_pos_diff_length_cu;
495 max_move_distance_cu = std::max(max_move_distance_cu, move_distance_cu);
496 }
497 }
498 move_distances_cu[curve_i] = max_move_distance_cu;
499 },
501 }
502};
503
505 const StrokeExtension &stroke_extension)
506{
507 CurvesEffectOperationExecutor executor{stroke};
508 executor.execute(*this, stroke_extension);
509}
510
511std::unique_ptr<CurvesSculptStrokeOperation> new_grow_shrink_operation(
512 const BrushStrokeMode brush_mode, const Scene &scene)
513{
515 const bool use_scale_uniform = brush.curves_sculpt_settings->flag &
517 const bool use_grow = (brush_mode == BrushStrokeMode::Invert) ==
518 ((brush.flag & BRUSH_DIR_IN) != 0);
519
520 if (use_grow) {
521 if (use_scale_uniform) {
522 return std::make_unique<CurvesEffectOperation>(
523 std::make_unique<ScaleCurvesEffect>(true, brush));
524 }
525 return std::make_unique<CurvesEffectOperation>(std::make_unique<ExtrapolateCurvesEffect>());
526 }
527 if (use_scale_uniform) {
528 return std::make_unique<CurvesEffectOperation>(
529 std::make_unique<ScaleCurvesEffect>(false, brush));
530 }
531 return std::make_unique<CurvesEffectOperation>(std::make_unique<ShrinkCurvesEffect>(brush));
532}
533
534} // namespace blender::ed::sculpt_paint
Low-level operations for curves.
#define BLI_SCOPED_DEFER(function_to_defer)
Object is a sort of wrapper for general info.
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
constexpr int64_t first() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr IndexRange drop_front(int64_t n) const
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr int64_t size() const
Definition BLI_span.hh:256
void append(const T &value)
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
CurvesEffectOperation(std::unique_ptr< CurvesEffect > effect)
void on_stroke_extended(const PaintStroke &stroke, const StrokeExtension &stroke_extension) override
virtual void execute(CurvesGeometry &curves, const IndexMask &curve_mask, Span< float > move_distances_cu, MutableSpan< float3 > positions_cu)=0
ScaleCurvesEffect(bool scale_up, const Brush &brush)
void execute(CurvesGeometry &curves, const IndexMask &curve_mask, const Span< float > move_distances_cu, MutableSpan< float3 > positions_cu) override
float compute_poly_curve_length(const Span< float3 > positions)
void execute(CurvesGeometry &curves, const IndexMask &curve_mask, const Span< float > move_distances_cu, MutableSpan< float3 > positions_cu) override
void foreach_segment(Fn &&fn, Mode mode=exec_mode::serial) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
float length(VecOp< float, D >) RET
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)
std::unique_ptr< CurvesSculptStrokeOperation > new_grow_shrink_operation(const BrushStrokeMode brush_mode, const Scene &scene)
Vector< float4x4 > get_symmetry_brush_transforms(const eCurvesSymmetryType symmetry)
float brush_radius_factor(const Brush &brush, const StrokeExtension &stroke_extension)
void move_last_point_and_resample(MoveAndResampleBuffers &buffer, MutableSpan< float3 > positions, const float3 &new_last_position)
constexpr ParallelGrainSize grain_size(int grain_size)
int segments_num(const int points_num, const bool cyclic)
T safe_divide(const T &a, const T &b)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
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)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &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
void isect_seg_seg_v3(const float a0[3], const float a1[3], const float b0[3], const float b1[3], float r_a[3], float r_b[3])
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)
@ BRUSH_CURVES_SCULPT_FLAG_SCALE_UNIFORM
VecBase< float, 2 > float2
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
float closest_seg_seg_v2(float r_closest_a[2], float r_closest_b[2], float *r_lambda_a, float *r_lambda_b, const float a1[2], const float a2[2], const float b1[2], const float b2[2])
Definition math_geom.cc:321
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1691
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 BrushCurvesSculptSettings * curves_sculpt_settings
struct ToolSettings * toolsettings
CurvesSculpt * curves_sculpt
void gather_influences_projected(MutableSpan< float > move_distances_cu)
void gather_influences_spherical(MutableSpan< float > move_distances_cu)
void execute(CurvesEffectOperation &self, const StrokeExtension &stroke_extension)