Blender V4.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 <algorithm>
6
8
10#include "BLI_task.hh"
11#include "BLI_vector.hh"
12
13#include "DEG_depsgraph.hh"
14
15#include "BKE_brush.hh"
16#include "BKE_context.hh"
17#include "BKE_curves.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#include "DNA_space_types.h"
26
27#include "ED_screen.hh"
28#include "ED_view3d.hh"
29
30#include "WM_api.hh"
31
41
43 private:
44 bool invert_pinch_;
45
47 CurvesConstraintSolver constraint_solver_;
48
50 CurvesBrush3D brush_3d_;
51
53
54 public:
55 PinchOperation(const bool invert_pinch) : invert_pinch_(invert_pinch) {}
56
57 void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override;
58};
59
63
64 Object *object_ = nullptr;
65 Curves *curves_id_ = nullptr;
67
71
73
74 const CurvesSculpt *curves_sculpt_ = nullptr;
75 const Brush *brush_ = nullptr;
79
81
83
85
86 void execute(PinchOperation &self, const bContext &C, const StrokeExtension &stroke_extension)
87 {
88 self_ = &self;
89
91 curves_id_ = static_cast<Curves *>(object_->data);
92 curves_ = &curves_id_->geometry.wrap();
93 if (curves_->curves_num() == 0) {
94 return;
95 }
96
100 brush_radius_factor_ = brush_radius_factor(*brush_, stroke_extension);
102
103 invert_factor_ = self_->invert_pinch_ ? -1.0f : 1.0f;
104
106
108 ".selection", bke::AttrDomain::Point, 1.0f);
110
111 brush_pos_re_ = stroke_extension.mouse_position;
113
114 if (stroke_extension.is_first) {
115 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
117 *ctx_.region,
118 *ctx_.v3d,
119 *ctx_.rv3d,
120 *object_,
123 }
124
125 self_->constraint_solver_.initialize(
127 }
128
129 Array<bool> changed_curves(curves_->curves_num(), false);
130 if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
131 this->pinch_projected_with_symmetry(changed_curves);
132 }
133 else if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
134 this->pinch_spherical_with_symmetry(changed_curves);
135 }
136 else {
138 }
139
140 IndexMaskMemory memory;
141 const IndexMask changed_curves_mask = IndexMask::from_bools(changed_curves, memory);
142 const Mesh *surface = curves_id_->surface && curves_id_->surface->type == OB_MESH ?
143 static_cast<const Mesh *>(curves_id_->surface->data) :
144 nullptr;
145 self_->constraint_solver_.solve_step(*curves_, changed_curves_mask, surface, transforms_);
146
151 }
152
154 {
155 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
157 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
158 this->pinch_projected(brush_transform, r_changed_curves);
159 }
160 }
161
162 void pinch_projected(const float4x4 &brush_transform, MutableSpan<bool> r_changed_curves)
163 {
164 const float4x4 brush_transform_inv = math::invert(brush_transform);
165
166 const bke::crazyspace::GeometryDeformation deformation =
168 const OffsetIndices points_by_curve = curves_->points_by_curve();
169
172 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
173 const float brush_radius_sq_re = pow2f(brush_radius_re);
174
176 for (const int curve_i : segment) {
177 const IndexRange points = points_by_curve[curve_i];
178 for (const int point_i : points.drop_front(1)) {
179 const float3 old_pos_cu = deformation.positions[point_i];
180 const float3 old_symm_pos_cu = math::transform_point(brush_transform_inv, old_pos_cu);
181 const float2 old_symm_pos_re = ED_view3d_project_float_v2_m4(
182 ctx_.region, old_symm_pos_cu, projection);
183
184 const float dist_to_brush_sq_re = math::distance_squared(old_symm_pos_re, brush_pos_re_);
185 if (dist_to_brush_sq_re > brush_radius_sq_re) {
186 continue;
187 }
188
189 const float dist_to_brush_re = std::sqrt(dist_to_brush_sq_re);
190 const float t = math::safe_divide(dist_to_brush_re, brush_radius_base_re_);
191 const float radius_falloff = t * BKE_brush_curve_strength(brush_, t, 1.0f);
192 const float weight = invert_factor_ * 0.1f * brush_strength_ * radius_falloff *
193 point_factors_[point_i];
194
195 const float2 new_symm_pos_re = math::interpolate(old_symm_pos_re, brush_pos_re_, weight);
196
197 float3 new_symm_pos_wo;
199 ctx_.region,
201 new_symm_pos_re,
202 new_symm_pos_wo);
203
204 float3 new_pos_cu = math::transform_point(transforms_.world_to_curves, new_symm_pos_wo);
205 new_pos_cu = math::transform_point(brush_transform, new_pos_cu);
206 const float3 translation_eval = new_pos_cu - old_pos_cu;
207 const float3 translation_orig = deformation.translation_from_deformed_to_original(
208 point_i, translation_eval);
209 positions_cu[point_i] += translation_orig;
210 r_changed_curves[curve_i] = true;
211 }
212 }
213 });
214 }
215
217 {
218 float3 brush_pos_wo;
220 ctx_.v3d,
221 ctx_.region,
224 brush_pos_wo);
225 const float3 brush_pos_cu = math::transform_point(transforms_.world_to_curves, brush_pos_wo);
226 const float brush_radius_cu = self_->brush_3d_.radius_cu * brush_radius_factor_;
227
228 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
230 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
231 this->pinch_spherical(
232 math::transform_point(brush_transform, brush_pos_cu), brush_radius_cu, r_changed_curves);
233 }
234 }
235
236 void pinch_spherical(const float3 &brush_pos_cu,
237 const float brush_radius_cu,
238 MutableSpan<bool> r_changed_curves)
239 {
241 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
242
243 const bke::crazyspace::GeometryDeformation deformation =
245 const OffsetIndices points_by_curve = curves_->points_by_curve();
246
248 for (const int curve_i : segment) {
249 const IndexRange points = points_by_curve[curve_i];
250 for (const int point_i : points.drop_front(1)) {
251 const float3 old_pos_cu = deformation.positions[point_i];
252
253 const float dist_to_brush_sq_cu = math::distance_squared(old_pos_cu, brush_pos_cu);
254 if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
255 continue;
256 }
257
258 const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
259 const float t = math::safe_divide(dist_to_brush_cu, brush_radius_cu);
260 const float radius_falloff = t * BKE_brush_curve_strength(brush_, t, 1.0f);
261 const float weight = invert_factor_ * 0.1f * brush_strength_ * radius_falloff *
262 point_factors_[point_i];
263
264 const float3 new_pos_cu = math::interpolate(old_pos_cu, brush_pos_cu, weight);
265 const float3 translation_eval = new_pos_cu - old_pos_cu;
266 const float3 translation_orig = deformation.translation_from_deformed_to_original(
267 point_i, translation_eval);
268 positions_cu[point_i] += translation_orig;
269
270 r_changed_curves[curve_i] = true;
271 }
272 }
273 });
274 }
275};
276
277void PinchOperation::on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension)
278{
279 PinchOperationExecutor executor{C};
280 executor.execute(*this, C, stroke_extension);
281}
282
283std::unique_ptr<CurvesSculptStrokeOperation> new_pinch_operation(const BrushStrokeMode brush_mode,
284 const bContext &C)
285{
286 const Scene &scene = *CTX_data_scene(&C);
287 const Brush &brush = *BKE_paint_brush_for_read(&scene.toolsettings->curves_sculpt->paint);
288
289 const bool invert_pinch = (brush_mode == BRUSH_STROKE_INVERT) !=
290 ((brush.flag & BRUSH_DIR_IN) != 0);
291 return std::make_unique<PinchOperation>(invert_pinch);
292}
293
294} // namespace blender::ed::sculpt_paint
int BKE_brush_size_get(const Scene *scene, const Brush *brush)
Definition brush.cc:1075
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1388
float BKE_brush_alpha_get(const Scene *scene, const Brush *brush)
Definition brush.cc:1153
Object * CTX_data_active_object(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
Low-level operations for curves.
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Definition paint.cc:654
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
MINLINE float pow2f(float x)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1041
@ BRUSH_DIR_IN
eBrushFalloffShape
@ PAINT_FALLOFF_SHAPE_SPHERE
@ PAINT_FALLOFF_SHAPE_TUBE
eCurvesSymmetryType
@ CV_SCULPT_COLLISION_ENABLED
Object is a sort of wrapper for general info.
@ OB_MESH
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:634
blender::float2 ED_view3d_project_float_v2_m4(const ARegion *region, const float co[3], const blender::float4x4 &mat)
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])
blender::float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
#define NC_GEOM
Definition WM_types.hh:360
#define ND_DATA
Definition WM_types.hh:475
PyObject * self
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const void *default_value=nullptr) const
MutableSpan< float3 > positions_for_write()
OffsetIndices< int > points_by_curve() const
void on_stroke_extended(const bContext &C, const StrokeExtension &stroke_extension) override
static IndexMask from_bools(Span< bool > bools, IndexMaskMemory &memory)
void foreach_segment(Fn &&fn) const
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, IndexMaskMemory &memory)
std::unique_ptr< CurvesSculptStrokeOperation > new_pinch_operation(const BrushStrokeMode brush_mode, const bContext &C)
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)
Vector< float4x4 > get_symmetry_brush_transforms(const eCurvesSymmetryType symmetry)
float brush_radius_factor(const Brush &brush, const StrokeExtension &stroke_extension)
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)
BrushStrokeMode
@ BRUSH_STROKE_INVERT
char falloff_shape
CurvesGeometry geometry
struct Object * surface
struct ToolSettings * toolsettings
CurvesSculpt * curves_sculpt
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)
void execute(PinchOperation &self, const bContext &C, const StrokeExtension &stroke_extension)
void pinch_spherical_with_symmetry(MutableSpan< bool > r_changed_curves)
void pinch_projected(const float4x4 &brush_transform, MutableSpan< bool > r_changed_curves)
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)
void WM_main_add_notifier(uint type, void *reference)