Blender V5.3
sculpt_delete.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"
12
13#include "BKE_brush.hh"
14#include "BKE_context.hh"
15#include "BKE_curves.hh"
16#include "BKE_mesh.hh"
17#include "BKE_mesh_runtime.hh"
18#include "BKE_paint.hh"
19
20#include "DNA_brush_enums.h"
21#include "DNA_brush_types.h"
22#include "DNA_curves_types.h"
23#include "DNA_object_types.h"
24#include "DNA_screen_types.h"
25
26#include "ED_screen.hh"
27#include "ED_view3d.hh"
28
29#include "WM_api.hh"
30
37
39
40using bke::CurvesGeometry;
41
43 private:
44 CurvesBrush3D brush_3d_;
50 Vector<float3> deformed_positions_;
51
53
54 public:
55 void on_stroke_extended(const PaintStroke &stroke,
56 const StrokeExtension &stroke_extension) override;
57};
58
62
63 Object *object_ = nullptr;
64 Curves *curves_id_ = nullptr;
66
69
71 const Brush *brush_ = nullptr;
74
76
78
79 DeleteOperationExecutor(const PaintStroke &stroke) : ctx_(stroke) {}
80
81 void execute(DeleteOperation &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
90
95
96 brush_pos_re_ = stroke_extension.mouse_position;
97
99
101
102 if (stroke_extension.is_first) {
103 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE || (U.uiflag & USER_ORBIT_SELECTION)) {
105 }
106 const bke::crazyspace::GeometryDeformation deformation =
108 self_->deformed_positions_ = deformation.positions;
109 }
110
111 Array<bool> curves_to_keep(curves_->curves_num(), true);
112 if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
113 this->delete_projected_with_symmetry(curves_to_keep);
114 }
115 else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
116 this->delete_spherical_with_symmetry(curves_to_keep);
117 }
118 else {
120 }
121
122 IndexMaskMemory mask_memory;
123 const IndexMask mask_to_keep = IndexMask::from_bools(curves_to_keep, mask_memory);
124
125 /* Remove deleted curves from the stored deformed positions. */
126 const OffsetIndices points_by_curve = curves_->points_by_curve();
127 Vector<float3> new_deformed_positions;
128 mask_to_keep.foreach_index([&](const int64_t i) {
129 new_deformed_positions.extend(
130 self_->deformed_positions_.as_span().slice(points_by_curve[i]));
131 });
132 self_->deformed_positions_ = std::move(new_deformed_positions);
133
134 *curves_ = bke::curves_copy_curve_selection(*curves_, mask_to_keep, {});
135
139 }
140
142 {
143 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
145 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
146 this->delete_projected(brush_transform, curves_to_keep);
147 }
148 }
149
150 void delete_projected(const float4x4 &brush_transform, MutableSpan<bool> curves_to_keep)
151 {
152 const float4x4 brush_transform_inv = math::invert(brush_transform);
153
155
156 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
157 const float brush_radius_sq_re = pow2f(brush_radius_re);
158 const OffsetIndices points_by_curve = curves_->points_by_curve();
159
161 [&](const IndexMaskSegment segment) {
162 for (const int curve_i : segment) {
163 const IndexRange points = points_by_curve[curve_i];
164 if (points.size() == 1) {
165 const float3 pos_cu = math::transform_point(
166 brush_transform_inv, self_->deformed_positions_[points.first()]);
167 const float2 pos_re = ED_view3d_project_float_v2_m4(ctx_.region, pos_cu, projection);
168
169 if (math::distance_squared(brush_pos_re_, pos_re) <= brush_radius_sq_re) {
170 curves_to_keep[curve_i] = false;
171 }
172 continue;
173 }
174
175 for (const int segment_i : points.drop_back(1)) {
176 const float3 pos1_cu = math::transform_point(brush_transform_inv,
177 self_->deformed_positions_[segment_i]);
178 const float3 pos2_cu = math::transform_point(
179 brush_transform_inv, self_->deformed_positions_[segment_i + 1]);
180
182 ctx_.region, pos1_cu, projection);
184 ctx_.region, pos2_cu, projection);
185
186 const float dist_sq_re = dist_squared_to_line_segment_v2(
187 brush_pos_re_, pos1_re, pos2_re);
188 if (dist_sq_re <= brush_radius_sq_re) {
189 curves_to_keep[curve_i] = false;
190 break;
191 }
192 }
193 }
194 },
196 }
197
199 {
200 float3 brush_wo;
202 ctx_.v3d,
203 ctx_.region,
206 brush_wo);
207 const float3 brush_cu = math::transform_point(transforms_.world_to_curves, brush_wo);
208
209 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
211
212 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
213 this->delete_spherical(math::transform_point(brush_transform, brush_cu), curves_to_keep);
214 }
215 }
216
217 void delete_spherical(const float3 &brush_cu, MutableSpan<bool> curves_to_keep)
218 {
219 const float brush_radius_cu = self_->brush_3d_.radius_cu * brush_radius_factor_;
220 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
221 const OffsetIndices points_by_curve = curves_->points_by_curve();
222
224 [&](const IndexMaskSegment segment) {
225 for (const int curve_i : segment) {
226 const IndexRange points = points_by_curve[curve_i];
227
228 if (points.size() == 1) {
229 const float3 &pos_cu = self_->deformed_positions_[points.first()];
230 const float distance_sq_cu = math::distance_squared(pos_cu, brush_cu);
231 if (distance_sq_cu < brush_radius_sq_cu) {
232 curves_to_keep[curve_i] = false;
233 }
234 continue;
235 }
236
237 for (const int segment_i : points.drop_back(1)) {
238 const float3 &pos1_cu = self_->deformed_positions_[segment_i];
239 const float3 &pos2_cu = self_->deformed_positions_[segment_i + 1];
240
241 const float distance_sq_cu = dist_squared_to_line_segment_v3(
242 brush_cu, pos1_cu, pos2_cu);
243 if (distance_sq_cu > brush_radius_sq_cu) {
244 continue;
245 }
246 curves_to_keep[curve_i] = false;
247 break;
248 }
249 }
250 },
252 }
253
255 {
256 std::optional<CurvesBrush3D> brush_3d = sample_curves_3d_brush(*ctx_.depsgraph,
257 *ctx_.region,
258 *ctx_.v3d,
259 *ctx_.rv3d,
260 *object_,
263 if (brush_3d.has_value()) {
264 self_->brush_3d_ = *brush_3d;
268 }
269 }
270};
271
273 const StrokeExtension &stroke_extension)
274{
275 DeleteOperationExecutor executor{stroke};
276 executor.execute(*this, stroke_extension);
277}
278
279std::unique_ptr<CurvesSculptStrokeOperation> new_delete_operation()
280{
281 return std::make_unique<DeleteOperation>();
282}
283
284} // namespace blender::ed::sculpt_paint
Low-level operations for curves.
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()
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr int64_t first() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t size() const
void extend(Span< T > array)
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
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
CurvesGeometry curves_copy_curve_selection(const CurvesGeometry &curves, const IndexMask &curves_to_copy, const AttributeFilter &attribute_filter)
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, LinearAllocator<> &memory)
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_delete_operation()
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)
CartesianBasis invert(const CartesianBasis &basis)
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
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
void DEG_id_tag_update(ID *id, unsigned int flags)
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:535
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 delete_projected(const float4x4 &brush_transform, MutableSpan< bool > curves_to_keep)
void delete_spherical_with_symmetry(MutableSpan< bool > curves_to_keep)
void execute(DeleteOperation &self, const StrokeExtension &stroke_extension)
void delete_projected_with_symmetry(MutableSpan< bool > curves_to_keep)
void delete_spherical(const float3 &brush_cu, MutableSpan< bool > curves_to_keep)