Blender V5.3
sculpt_selection_paint.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_geom_c.hh"
8#include "BLI_math_matrix.hh"
9#include "BLI_task.hh"
10
11#include "DNA_brush_types.h"
12
13#include "BKE_attribute.hh"
14#include "BKE_brush.hh"
15#include "BKE_context.hh"
16#include "BKE_curves.hh"
17#include "BKE_paint.hh"
18
19#include "DEG_depsgraph.hh"
20
21#include "ED_screen.hh"
22#include "ED_view3d.hh"
23
24#include "WM_api.hh"
25
26#include "sculpt_intern.hh"
27
34
36
37using bke::CurvesGeometry;
38
40 private:
41 bool use_select_;
42 bool clear_selection_;
43
44 CurvesBrush3D brush_3d_;
45
47
48 public:
49 SelectionPaintOperation(const bool use_select, const bool clear_selection)
50 : use_select_(use_select), clear_selection_(clear_selection)
51 {
52 }
53 void on_stroke_extended(const PaintStroke &stroke,
54 const StrokeExtension &stroke_extension) override;
55};
56
60
61 Object *object_ = nullptr;
62 Curves *curves_id_ = nullptr;
64
66
68 const Brush *brush_ = nullptr;
72
74
76
78
80
81 void execute(SelectionPaintOperation &self, const StrokeExtension &stroke_extension)
82 {
83 self_ = &self;
85
86 curves_id_ = id_cast<Curves *>(object_->data);
87 curves_ = &curves_id_->geometry.wrap();
88 if (curves_->is_empty()) {
89 return;
90 }
92 if (!selection_) {
93 return;
94 }
95
101
102 brush_pos_re_ = stroke_extension.mouse_position;
103
104 if (self.clear_selection_) {
105 if (stroke_extension.is_first) {
107 }
108 }
109
111
113
114 selection_goal_ = self_->use_select_ ? 1.0f : 0.0f;
115
116 if (stroke_extension.is_first) {
117 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE || (U.uiflag & USER_ORBIT_SELECTION)) {
119 }
120 }
121
123 if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
125 }
126 else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
128 }
129 }
130 else {
131 if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
133 }
134 else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
136 }
137 }
138
140
141 /* Use #ID_RECALC_GEOMETRY instead of #ID_RECALC_SELECT because
142 * selection is handled as a generic attribute for now. */
147 }
148
150 {
151 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
153 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
154 this->paint_point_selection_projected(brush_transform, selection);
155 }
156 }
157
158 void paint_point_selection_projected(const float4x4 &brush_transform,
159 MutableSpan<float> selection)
160 {
161 const float4x4 brush_transform_inv = math::invert(brush_transform);
162
164
165 const bke::crazyspace::GeometryDeformation deformation =
167
168 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
169 const float brush_radius_sq_re = pow2f(brush_radius_re);
170
171 threading::parallel_for(curves_->points_range(), 1024, [&](const IndexRange point_range) {
172 for (const int point_i : point_range) {
173 const float3 pos_cu = math::transform_point(brush_transform_inv,
174 deformation.positions[point_i]);
175
176 /* Find the position of the point in screen space. */
177 const float2 pos_re = ED_view3d_project_float_v2_m4(ctx_.region, pos_cu, projection);
178
179 const float distance_to_brush_sq_re = math::distance_squared(pos_re, brush_pos_re_);
180 if (distance_to_brush_sq_re > brush_radius_sq_re) {
181 /* Ignore the point because it's too far away. */
182 continue;
183 }
184
185 const float distance_to_brush_re = std::sqrt(distance_to_brush_sq_re);
186 /* A falloff that is based on how far away the point is from the stroke. */
187 const float radius_falloff = BKE_brush_curve_strength(
188 brush_, distance_to_brush_re, brush_radius_re);
189 /* Combine the falloff and brush strength. */
190 const float weight = brush_strength_ * radius_falloff;
191
192 selection[point_i] = math::interpolate(selection[point_i], selection_goal_, weight);
193 }
194 });
195 }
196
198 {
199 float3 brush_wo;
201 ctx_.v3d,
202 ctx_.region,
203 math::transform_point(transforms_.curves_to_world, self_->brush_3d_.position_cu),
205 brush_wo);
206 const float3 brush_cu = math::transform_point(transforms_.world_to_curves, brush_wo);
207
208 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
210
211 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
212 this->paint_point_selection_spherical(selection,
213 math::transform_point(brush_transform, brush_cu));
214 }
215 }
216
218 {
219 const bke::crazyspace::GeometryDeformation deformation =
221
222 const float brush_radius_cu = self_->brush_3d_.radius_cu;
223 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
224
225 threading::parallel_for(curves_->points_range(), 1024, [&](const IndexRange point_range) {
226 for (const int i : point_range) {
227 const float3 pos_old_cu = deformation.positions[i];
228
229 /* Compute distance to the brush. */
230 const float distance_to_brush_sq_cu = math::distance_squared(pos_old_cu, brush_cu);
231 if (distance_to_brush_sq_cu > brush_radius_sq_cu) {
232 /* Ignore the point because it's too far away. */
233 continue;
234 }
235
236 const float distance_to_brush_cu = std::sqrt(distance_to_brush_sq_cu);
237
238 /* A falloff that is based on how far away the point is from the stroke. */
239 const float radius_falloff = BKE_brush_curve_strength(
240 brush_, distance_to_brush_cu, brush_radius_cu);
241 /* Combine the falloff and brush strength. */
242 const float weight = brush_strength_ * radius_falloff;
243
244 selection[i] = math::interpolate(selection[i], selection_goal_, weight);
245 }
246 });
247 }
248
250 {
251 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
253 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
254 this->paint_curve_selection_projected(brush_transform, selection);
255 }
256 }
257
258 void paint_curve_selection_projected(const float4x4 &brush_transform,
259 MutableSpan<float> selection)
260 {
261 const float4x4 brush_transform_inv = math::invert(brush_transform);
262
263 const bke::crazyspace::GeometryDeformation deformation =
265 const OffsetIndices points_by_curve = curves_->points_by_curve();
266
267 const float4x4 projection = ED_view3d_ob_project_mat_get(ctx_.rv3d, object_);
268
269 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
270 const float brush_radius_sq_re = pow2f(brush_radius_re);
271
272 threading::parallel_for(curves_->curves_range(), 1024, [&](const IndexRange curves_range) {
273 for (const int curve_i : curves_range) {
274 const float max_weight = threading::parallel_reduce(
275 points_by_curve[curve_i].drop_back(1),
276 1024,
277 0.0f,
278 [&](const IndexRange segment_range, const float init) {
279 float max_weight = init;
280 for (const int segment_i : segment_range) {
281 const float3 pos1_cu = math::transform_point(brush_transform_inv,
282 deformation.positions[segment_i]);
283 const float3 pos2_cu = math::transform_point(brush_transform_inv,
284 deformation.positions[segment_i + 1]);
285
286 const float2 pos1_re = ED_view3d_project_float_v2_m4(
287 ctx_.region, pos1_cu, projection);
288 const float2 pos2_re = ED_view3d_project_float_v2_m4(
289 ctx_.region, pos2_cu, projection);
290
291 const float distance_sq_re = dist_squared_to_line_segment_v2(
292 brush_pos_re_, pos1_re, pos2_re);
293 if (distance_sq_re > brush_radius_sq_re) {
294 continue;
295 }
296 const float radius_falloff = BKE_brush_curve_strength(
297 brush_, std::sqrt(distance_sq_re), brush_radius_re);
298 const float weight = brush_strength_ * radius_falloff;
299 max_weight = std::max(max_weight, weight);
300 }
301 return max_weight;
302 },
303 [](float a, float b) { return std::max(a, b); });
304 selection[curve_i] = math::interpolate(selection[curve_i], selection_goal_, max_weight);
305 }
306 });
307 }
308
310 {
311 float3 brush_wo;
313 ctx_.v3d,
314 ctx_.region,
315 math::transform_point(transforms_.curves_to_world, self_->brush_3d_.position_cu),
317 brush_wo);
318 const float3 brush_cu = math::transform_point(transforms_.world_to_curves, brush_wo);
319
320 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
322
323 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
324 this->paint_curve_selection_spherical(selection,
325 math::transform_point(brush_transform, brush_cu));
326 }
327 }
328
330 {
331 const bke::crazyspace::GeometryDeformation deformation =
333 const OffsetIndices points_by_curve = curves_->points_by_curve();
334
335 const float brush_radius_cu = self_->brush_3d_.radius_cu;
336 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
337
338 threading::parallel_for(curves_->curves_range(), 1024, [&](const IndexRange curves_range) {
339 for (const int curve_i : curves_range) {
340 const float max_weight = threading::parallel_reduce(
341 points_by_curve[curve_i].drop_back(1),
342 1024,
343 0.0f,
344 [&](const IndexRange segment_range, const float init) {
345 float max_weight = init;
346 for (const int segment_i : segment_range) {
347 const float3 &pos1_cu = deformation.positions[segment_i];
348 const float3 &pos2_cu = deformation.positions[segment_i + 1];
349
350 const float distance_sq_cu = dist_squared_to_line_segment_v3(
351 brush_cu, pos1_cu, pos2_cu);
352 if (distance_sq_cu > brush_radius_sq_cu) {
353 continue;
354 }
355 const float radius_falloff = BKE_brush_curve_strength(
356 brush_, std::sqrt(distance_sq_cu), brush_radius_cu);
357 const float weight = brush_strength_ * radius_falloff;
358 max_weight = std::max(max_weight, weight);
359 }
360 return max_weight;
361 },
362 [](float a, float b) { return std::max(a, b); });
363 selection[curve_i] = math::interpolate(selection[curve_i], selection_goal_, max_weight);
364 }
365 });
366 }
367
369 {
370 std::optional<CurvesBrush3D> brush_3d = sample_curves_3d_brush(*ctx_.depsgraph,
371 *ctx_.region,
372 *ctx_.v3d,
373 *ctx_.rv3d,
374 *object_,
377 if (brush_3d.has_value()) {
378 self_->brush_3d_ = *brush_3d;
381 math::transform_point(transforms_.curves_to_world, self_->brush_3d_.position_cu));
382 }
383 }
384};
385
387 const StrokeExtension &stroke_extension)
388{
389 SelectionPaintOperationExecutor executor{stroke};
390 executor.execute(*this, stroke_extension);
391}
392
393std::unique_ptr<CurvesSculptStrokeOperation> new_selection_paint_operation(
394 const BrushStrokeMode brush_mode, const BrushSwitchMode brush_switch_mode, const Scene &scene)
395{
397 const bool use_select = ELEM(brush_mode, BrushStrokeMode::Invert) ==
398 ((brush.flag & BRUSH_DIR_IN) != 0);
399 const bool clear_selection = use_select && brush_switch_mode != BrushSwitchMode::Smooth;
400
401 return std::make_unique<SelectionPaintOperation>(use_select, clear_selection);
402}
403
404} // namespace blender::ed::sculpt_paint
Low-level operations for curves.
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define ELEM(...)
IndexRange points_range() const
void on_stroke_extended(const PaintStroke &stroke, const StrokeExtension &stroke_extension) override
SelectionPaintOperation(const bool use_select, const bool clear_selection)
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
void fill_selection_false(GMutableSpan selection)
float brush_strength_get(const Paint &paint, const Brush &brush, const StrokeExtension &stroke_extension)
std::unique_ptr< CurvesSculptStrokeOperation > new_selection_paint_operation(BrushStrokeMode brush_mode, BrushSwitchMode brush_switch_mode, const Scene &scene)
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)
bke::SpanAttributeWriter< float > float_selection_ensure(Curves &curves_id)
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)
CartesianBasis invert(const CartesianBasis &basis)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
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
MatBase< float, 4, 4 > float4x4
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
MINLINE float pow2f(float x)
VecBase< float, 2 > float2
void DEG_id_tag_update(ID *id, unsigned int flags)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
Brush * BKE_paint_brush(Paint *paint)
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
eRegionView3D_Flag rflag
struct ToolSettings * toolsettings
CurvesSculpt * curves_sculpt
void paint_point_selection_spherical_with_symmetry(MutableSpan< float > selection)
void paint_point_selection_spherical(MutableSpan< float > selection, const float3 &brush_cu)
void paint_curve_selection_projected(const float4x4 &brush_transform, MutableSpan< float > selection)
void paint_point_selection_projected_with_symmetry(MutableSpan< float > selection)
void paint_curve_selection_spherical(MutableSpan< float > selection, const float3 &brush_cu)
void paint_point_selection_projected(const float4x4 &brush_transform, MutableSpan< float > selection)
void paint_curve_selection_spherical_with_symmetry(MutableSpan< float > selection)
void paint_curve_selection_projected_with_symmetry(MutableSpan< float > selection)
void execute(SelectionPaintOperation &self, const StrokeExtension &stroke_extension)