Blender V5.3
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
5#include "sculpt_intern.hh"
6
7#include "BLI_math_geom_c.hh"
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 "WM_api.hh"
34
35#include <numeric>
36
44
46
47using bke::CurvesGeometry;
48
53 private:
55 float2 brush_pos_last_re_;
56
58 CurvesBrush3D brush_3d_;
59
61 CurvesConstraintSolver constraint_solver_;
62
63 Array<float> curve_lengths_;
64
65 friend struct CombOperationExecutor;
66
67 public:
68 void on_stroke_extended(const PaintStroke &stroke,
69 const StrokeExtension &stroke_extension) override;
70};
71
77 CombOperation *self_ = nullptr;
79
81 const Brush *brush_ = nullptr;
85
89
93
97
99
100 CombOperationExecutor(const PaintStroke &stroke) : ctx_(stroke) {}
101
102 void execute(CombOperation &self, const StrokeExtension &stroke_extension)
103 {
104 self_ = &self;
105
106 BLI_SCOPED_DEFER([&]() { self_->brush_pos_last_re_ = stroke_extension.mouse_position; });
107
109 curves_id_orig_ = id_cast<Curves *>(curves_ob_orig_->data);
110 curves_orig_ = &curves_id_orig_->geometry.wrap();
111 if (curves_orig_->is_empty()) {
112 return;
113 }
114
118 brush_radius_factor_ = brush_radius_factor(*brush_, stroke_extension);
120
122
124
126 ".selection", bke::AttrDomain::Point, 1.0f);
128
129 brush_pos_prev_re_ = self_->brush_pos_last_re_;
130 brush_pos_re_ = stroke_extension.mouse_position;
132
133 if (stroke_extension.is_first) {
134 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE || (U.uiflag & USER_ORBIT_SELECTION)) {
136 }
137 self_->constraint_solver_.initialize(*curves_orig_,
140 curves_id_orig_->surface_collision_distance);
141
142 self_->curve_lengths_.reinitialize(curves_orig_->curves_num());
143 const Span<float> segment_lengths = self_->constraint_solver_.segment_lengths();
144 const OffsetIndices points_by_curve = curves_orig_->points_by_curve();
145 curve_selection_.foreach_segment(
146 [&](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(
151 lengths.begin(), lengths.end(), 0.0f);
152 }
153 },
155 /* Combing does nothing when there is no mouse movement, so return directly. */
156 return;
157 }
158
159 Array<bool> changed_curves(curves_orig_->curves_num(), false);
160
161 if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
162 this->comb_projected_with_symmetry(changed_curves);
163 }
164 else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
165 this->comb_spherical_with_symmetry(changed_curves);
166 }
167 else {
169 }
170
171 const Mesh *surface = curves_id_orig_->surface && curves_id_orig_->surface->type == OB_MESH ?
172 id_cast<Mesh *>(curves_id_orig_->surface->data) :
173 nullptr;
174
175 IndexMaskMemory memory;
176 const IndexMask changed_curves_mask = IndexMask::from_bools(changed_curves, memory);
177 self_->constraint_solver_.solve_step(*curves_orig_, changed_curves_mask, surface, transforms_);
178
183 }
184
189 {
190 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
192 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
193 this->comb_projected(r_changed_curves, brush_transform);
194 }
195 }
196
197 void comb_projected(MutableSpan<bool> r_changed_curves, const float4x4 &brush_transform)
198 {
199 const float4x4 brush_transform_inv = math::invert(brush_transform);
200
202 const bke::crazyspace::GeometryDeformation deformation =
204 const OffsetIndices points_by_curve = curves_orig_->points_by_curve();
205
207
208 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
209 const float brush_radius_sq_re = pow2f(brush_radius_re);
210
211 CurveMapping &curve_parameter_falloff_mapping =
213 BKE_curvemapping_init(&curve_parameter_falloff_mapping);
214
215 const Span<float> segment_lengths = self_->constraint_solver_.segment_lengths();
216
218 [&](const IndexMaskSegment segment) {
219 for (const int curve_i : segment) {
220 bool curve_changed = false;
221 const IndexRange points = points_by_curve[curve_i];
222
223 const float total_length = self_->curve_lengths_[curve_i];
224 const float total_length_inv = math::safe_rcp(total_length);
225 float current_length = 0.0f;
226 for (const int point_i : points.drop_front(1)) {
227 current_length += segment_lengths[point_i - 1];
228
229 const float3 old_pos_cu = deformation.positions[point_i];
230 const float3 old_symm_pos_cu = math::transform_point(brush_transform_inv,
231 old_pos_cu);
232
233 /* Find the position of the point in screen space. */
234 const float2 old_symm_pos_re = ED_view3d_project_float_v2_m4(
235 ctx_.region, old_symm_pos_cu, projection);
236
237 const float distance_to_brush_sq_re = dist_squared_to_line_segment_v2(
238 old_symm_pos_re, brush_pos_prev_re_, brush_pos_re_);
239 if (distance_to_brush_sq_re > brush_radius_sq_re) {
240 /* Ignore the point because it's too far away. */
241 continue;
242 }
243
244 const float distance_to_brush_re = std::sqrt(distance_to_brush_sq_re);
245 /* A falloff that is based on how far away the point is from the stroke. */
246 const float radius_falloff = BKE_brush_curve_strength(
247 brush_, distance_to_brush_re, brush_radius_re);
248 const float curve_parameter = current_length * total_length_inv;
249 const float curve_falloff = BKE_curvemapping_evaluateF(
250 &curve_parameter_falloff_mapping, 0, curve_parameter);
251 /* Combine the falloff and brush strength. */
252 const float weight = brush_strength_ * curve_falloff * radius_falloff *
253 point_factors_[point_i];
254
255 /* Offset the old point position in screen space and transform it back into 3D space.
256 */
257 const float2 new_symm_pos_re = old_symm_pos_re + brush_pos_diff_re_ * weight;
258 float3 new_symm_pos_wo;
260 ctx_.v3d,
261 ctx_.region,
263 new_symm_pos_re,
264 new_symm_pos_wo);
265 const float3 new_pos_cu = math::transform_point(
266 brush_transform,
268
269 const float3 translation_eval = new_pos_cu - old_pos_cu;
270 const float3 translation_orig = deformation.translation_from_deformed_to_original(
271 point_i, translation_eval);
272 positions_cu_orig[point_i] += translation_orig;
273
274 curve_changed = true;
275 }
276 if (curve_changed) {
277 r_changed_curves[curve_i] = true;
278 }
279 }
280 },
282 }
283
288 {
289 float3 brush_start_wo, brush_end_wo;
291 ctx_.v3d,
292 ctx_.region,
295 brush_start_wo);
297 ctx_.v3d,
298 ctx_.region,
301 brush_end_wo);
303 brush_start_wo);
304 const float3 brush_end_cu = math::transform_point(transforms_.world_to_curves, brush_end_wo);
305
306 const float brush_radius_cu = self_->brush_3d_.radius_cu * brush_radius_factor_;
307
308 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
310 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
311 this->comb_spherical(r_changed_curves,
312 math::transform_point(brush_transform, brush_start_cu),
313 math::transform_point(brush_transform, brush_end_cu),
314 brush_radius_cu);
315 }
316 }
317
318 void comb_spherical(MutableSpan<bool> r_changed_curves,
319 const float3 &brush_start_cu,
320 const float3 &brush_end_cu,
321 const float brush_radius_cu)
322 {
324 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
325 const float3 brush_diff_cu = brush_end_cu - brush_start_cu;
326
327 CurveMapping &curve_parameter_falloff_mapping =
329 BKE_curvemapping_init(&curve_parameter_falloff_mapping);
330
331 const bke::crazyspace::GeometryDeformation deformation =
333 const OffsetIndices points_by_curve = curves_orig_->points_by_curve();
334 const Span<float> segment_lengths = self_->constraint_solver_.segment_lengths();
335
337 [&](const IndexMaskSegment segment) {
338 for (const int curve_i : segment) {
339 bool curve_changed = false;
340 const IndexRange points = points_by_curve[curve_i];
341
342 const float total_length = self_->curve_lengths_[curve_i];
343 const float total_length_inv = math::safe_rcp(total_length);
344 float current_length = 0.0f;
345 for (const int point_i : points.drop_front(1)) {
346 current_length += segment_lengths[point_i - 1];
347
348 const float3 pos_old_cu = deformation.positions[point_i];
349
350 /* Compute distance to the brush. */
351 const float distance_to_brush_sq_cu = dist_squared_to_line_segment_v3(
352 pos_old_cu, brush_start_cu, brush_end_cu);
353 if (distance_to_brush_sq_cu > brush_radius_sq_cu) {
354 /* Ignore the point because it's too far away. */
355 continue;
356 }
357
358 const float distance_to_brush_cu = std::sqrt(distance_to_brush_sq_cu);
359
360 /* A falloff that is based on how far away the point is from the stroke. */
361 const float radius_falloff = BKE_brush_curve_strength(
362 brush_, distance_to_brush_cu, brush_radius_cu);
363 const float curve_parameter = current_length * total_length_inv;
364 const float curve_falloff = BKE_curvemapping_evaluateF(
365 &curve_parameter_falloff_mapping, 0, curve_parameter);
366 /* Combine the falloff and brush strength. */
367 const float weight = brush_strength_ * curve_falloff * radius_falloff *
368 point_factors_[point_i];
369
370 const float3 translation_eval_cu = weight * brush_diff_cu;
371 const float3 translation_orig_cu = deformation.translation_from_deformed_to_original(
372 point_i, translation_eval_cu);
373
374 /* Update the point position. */
375 positions_cu[point_i] += translation_orig_cu;
376 curve_changed = true;
377 }
378 if (curve_changed) {
379 r_changed_curves[curve_i] = true;
380 }
381 }
382 },
384 }
385
390 {
391 std::optional<CurvesBrush3D> brush_3d = sample_curves_3d_brush(*ctx_.depsgraph,
392 *ctx_.region,
393 *ctx_.v3d,
394 *ctx_.rv3d,
398 if (brush_3d.has_value()) {
399 self_->brush_3d_ = *brush_3d;
403 }
404 }
405};
406
408 const StrokeExtension &stroke_extension)
409{
410 CombOperationExecutor executor{stroke};
411 executor.execute(*this, stroke_extension);
412}
413
414std::unique_ptr<CurvesSculptStrokeOperation> new_comb_operation()
415{
416 return std::make_unique<CombOperation>();
417}
418
419} // 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
#define BLI_assert_unreachable()
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &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:141
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
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
void on_stroke_extended(const PaintStroke &stroke, const StrokeExtension &stroke_extension) override
void foreach_segment(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)
std::unique_ptr< CurvesSculptStrokeOperation > new_comb_operation()
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)
Vector< float4x4 > get_symmetry_brush_transforms(const eCurvesSymmetryType symmetry)
float brush_radius_factor(const Brush &brush, const StrokeExtension &stroke_extension)
constexpr ParallelGrainSize grain_size(int grain_size)
T safe_rcp(const T &a)
CartesianBasis invert(const CartesianBasis &basis)
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
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
void BKE_curvemapping_init(CurveMapping *cumap)
VecBase< float, 2 > float2
void DEG_id_tag_update(ID *id, unsigned int flags)
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])
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
struct CurveMapping * curve_parameter_falloff
eBrushFalloffShape falloff_shape
struct BrushCurvesSculptSettings * curves_sculpt_settings
struct ToolSettings * toolsettings
CurvesSculpt * curves_sculpt
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 execute(CombOperation &self, const StrokeExtension &stroke_extension)
void comb_spherical(MutableSpan< bool > r_changed_curves, const float3 &brush_start_cu, const float3 &brush_end_cu, const float brush_radius_cu)
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)