Blender V5.3
curves/sculpt_pinch.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
8#include "BLI_task.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_paint.hh"
17
18#include "DNA_brush_enums.h"
19#include "DNA_brush_types.h"
20#include "DNA_curves_types.h"
21#include "DNA_mesh_types.h"
22#include "DNA_object_types.h"
23#include "DNA_screen_types.h"
24
25#include "ED_screen.hh"
26#include "ED_view3d.hh"
27
28#include "WM_api.hh"
29
37
39
41 private:
42 bool invert_pinch_;
43
45 CurvesConstraintSolver constraint_solver_;
46
48 CurvesBrush3D brush_3d_;
49
51
52 public:
53 PinchOperation(const bool invert_pinch) : invert_pinch_(invert_pinch) {}
54
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
70
72
74 const Brush *brush_ = nullptr;
78
80
82
83 PinchOperationExecutor(const PaintStroke &stroke) : ctx_(stroke) {}
84
85 void execute(PinchOperation &self, const StrokeExtension &stroke_extension)
86 {
87 self_ = &self;
88
90 curves_id_ = id_cast<Curves *>(object_->data);
91 curves_ = &curves_id_->geometry.wrap();
92 if (curves_->is_empty()) {
93 return;
94 }
95
101
102 invert_factor_ = self_->invert_pinch_ ? -1.0f : 1.0f;
103
105
107 ".selection", bke::AttrDomain::Point, 1.0f);
109
110 brush_pos_re_ = stroke_extension.mouse_position;
112
113 if (stroke_extension.is_first) {
114 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE || (U.uiflag & USER_ORBIT_SELECTION)) {
116 *ctx_.region,
117 *ctx_.v3d,
118 *ctx_.rv3d,
119 *object_,
125 }
126
127 self_->constraint_solver_.initialize(*curves_,
130 curves_id_->surface_collision_distance);
131 }
132
133 Array<bool> changed_curves(curves_->curves_num(), false);
134 if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
135 this->pinch_projected_with_symmetry(changed_curves);
136 }
137 else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
138 this->pinch_spherical_with_symmetry(changed_curves);
139 }
140 else {
142 }
143
144 IndexMaskMemory memory;
145 const IndexMask changed_curves_mask = IndexMask::from_bools(changed_curves, memory);
146 const Mesh *surface = curves_id_->surface && curves_id_->surface->type == OB_MESH ?
147 id_cast<const Mesh *>(curves_id_->surface->data) :
148 nullptr;
149 self_->constraint_solver_.solve_step(*curves_, changed_curves_mask, surface, transforms_);
150
155 }
156
158 {
159 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
161 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
162 this->pinch_projected(brush_transform, r_changed_curves);
163 }
164 }
165
166 void pinch_projected(const float4x4 &brush_transform, MutableSpan<bool> r_changed_curves)
167 {
168 const float4x4 brush_transform_inv = math::invert(brush_transform);
169
170 const bke::crazyspace::GeometryDeformation deformation =
172 const OffsetIndices points_by_curve = curves_->points_by_curve();
173
176 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
177 const float brush_radius_sq_re = pow2f(brush_radius_re);
178
180 [&](const IndexMaskSegment segment) {
181 for (const int curve_i : segment) {
182 const IndexRange points = points_by_curve[curve_i];
183 for (const int point_i : points.drop_front(1)) {
184 const float3 old_pos_cu = deformation.positions[point_i];
185 const float3 old_symm_pos_cu = math::transform_point(brush_transform_inv,
186 old_pos_cu);
187 const float2 old_symm_pos_re = ED_view3d_project_float_v2_m4(
188 ctx_.region, old_symm_pos_cu, projection);
189
190 const float dist_to_brush_sq_re = math::distance_squared(old_symm_pos_re,
192 if (dist_to_brush_sq_re > brush_radius_sq_re) {
193 continue;
194 }
195
196 const float dist_to_brush_re = std::sqrt(dist_to_brush_sq_re);
197 const float t = math::safe_divide(dist_to_brush_re, brush_radius_base_re_);
198 const float radius_falloff = t * BKE_brush_curve_strength(brush_, t, 1.0f);
199 const float weight = invert_factor_ * 0.1f * brush_strength_ * radius_falloff *
200 point_factors_[point_i];
201
202 const float2 new_symm_pos_re = math::interpolate(
203 old_symm_pos_re, brush_pos_re_, weight);
204
205 float3 new_symm_pos_wo;
207 ctx_.v3d,
208 ctx_.region,
210 new_symm_pos_re,
211 new_symm_pos_wo);
212
214 new_symm_pos_wo);
215 new_pos_cu = math::transform_point(brush_transform, new_pos_cu);
216 const float3 translation_eval = new_pos_cu - old_pos_cu;
217 const float3 translation_orig = deformation.translation_from_deformed_to_original(
218 point_i, translation_eval);
219 positions_cu[point_i] += translation_orig;
220 r_changed_curves[curve_i] = true;
221 }
222 }
223 },
225 }
226
228 {
229 float3 brush_pos_wo;
231 ctx_.v3d,
232 ctx_.region,
235 brush_pos_wo);
236 const float3 brush_pos_cu = math::transform_point(transforms_.world_to_curves, brush_pos_wo);
237 const float brush_radius_cu = self_->brush_3d_.radius_cu * brush_radius_factor_;
238
239 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
241 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
242 this->pinch_spherical(
243 math::transform_point(brush_transform, brush_pos_cu), brush_radius_cu, r_changed_curves);
244 }
245 }
246
247 void pinch_spherical(const float3 &brush_pos_cu,
248 const float brush_radius_cu,
249 MutableSpan<bool> r_changed_curves)
250 {
252 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
253
254 const bke::crazyspace::GeometryDeformation deformation =
256 const OffsetIndices points_by_curve = curves_->points_by_curve();
257
259 [&](const IndexMaskSegment segment) {
260 for (const int curve_i : segment) {
261 const IndexRange points = points_by_curve[curve_i];
262 for (const int point_i : points.drop_front(1)) {
263 const float3 old_pos_cu = deformation.positions[point_i];
264
265 const float dist_to_brush_sq_cu = math::distance_squared(old_pos_cu, brush_pos_cu);
266 if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
267 continue;
268 }
269
270 const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
271 const float t = math::safe_divide(dist_to_brush_cu, brush_radius_cu);
272 const float radius_falloff = t * BKE_brush_curve_strength(brush_, t, 1.0f);
273 const float weight = invert_factor_ * 0.1f * brush_strength_ * radius_falloff *
274 point_factors_[point_i];
275
276 const float3 new_pos_cu = math::interpolate(old_pos_cu, brush_pos_cu, weight);
277 const float3 translation_eval = new_pos_cu - old_pos_cu;
278 const float3 translation_orig = deformation.translation_from_deformed_to_original(
279 point_i, translation_eval);
280 positions_cu[point_i] += translation_orig;
281
282 r_changed_curves[curve_i] = true;
283 }
284 }
285 },
287 }
288};
289
291 const StrokeExtension &stroke_extension)
292{
293 PinchOperationExecutor executor{stroke};
294 executor.execute(*this, stroke_extension);
295}
296
297std::unique_ptr<CurvesSculptStrokeOperation> new_pinch_operation(const BrushStrokeMode brush_mode,
298 const Scene &scene)
299{
301
302 const bool invert_pinch = (brush_mode == BrushStrokeMode::Invert) !=
303 ((brush.flag & BRUSH_DIR_IN) != 0);
304 return std::make_unique<PinchOperation>(invert_pinch);
305}
306
307} // 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 IndexRange drop_front(int64_t n) const
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_pinch_operation(BrushStrokeMode brush_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)
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_divide(const T &a, const T &b)
CartesianBasis invert(const CartesianBasis &basis)
T interpolate(const T &a, const T &b, const FactorT &t)
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
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)
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
@ 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_brush_alpha_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1454
eBrushFalloffShape falloff_shape
struct ToolSettings * toolsettings
CurvesSculpt * curves_sculpt
float3 translation_from_deformed_to_original(const int position_i, const float3 &translation) const
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)
void pinch_spherical_with_symmetry(MutableSpan< bool > r_changed_curves)
void pinch_projected(const float4x4 &brush_transform, MutableSpan< bool > r_changed_curves)
void execute(PinchOperation &self, const StrokeExtension &stroke_extension)
void pinch_projected_with_symmetry(MutableSpan< bool > r_changed_curves)
void pinch_spherical(const float3 &brush_pos_cu, const float brush_radius_cu, MutableSpan< bool > r_changed_curves)