Blender V5.3
sculpt_snake_hook.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_attribute.hh"
14#include "BKE_brush.hh"
15#include "BKE_context.hh"
16#include "BKE_curves.hh"
17#include "BKE_mesh.hh"
18#include "BKE_mesh_runtime.hh"
19#include "BKE_paint.hh"
20
21#include "DNA_brush_enums.h"
22#include "DNA_brush_types.h"
23#include "DNA_curves_types.h"
24#include "DNA_object_types.h"
25#include "DNA_screen_types.h"
26
27#include "ED_screen.hh"
28#include "ED_view3d.hh"
29
30#include "WM_api.hh"
31
39
41
42using bke::CurvesGeometry;
43
48 private:
49 float2 last_mouse_position_re_;
50
51 CurvesBrush3D brush_3d_;
52
54
55 public:
56 void on_stroke_extended(const PaintStroke &stroke,
57 const StrokeExtension &stroke_extension) override;
58};
59
67
69 const Brush *brush_ = nullptr;
73
74 Object *object_ = nullptr;
75 Curves *curves_id_ = nullptr;
77
81
83
87
88 SnakeHookOperatorExecutor(const PaintStroke &stroke) : ctx_(stroke) {}
89
90 void execute(SnakeHookOperation &self, const StrokeExtension &stroke_extension)
91 {
92 BLI_SCOPED_DEFER([&]() { self.last_mouse_position_re_ = stroke_extension.mouse_position; });
93
94 self_ = &self;
96
99
101 brush_radius_factor_ = brush_radius_factor(*brush_, stroke_extension);
103
105
106 curves_id_ = id_cast<Curves *>(object_->data);
107 curves_ = &curves_id_->geometry.wrap();
108 if (curves_->is_empty()) {
109 return;
110 }
111
113
115 ".selection", bke::AttrDomain::Curve, 1.0f);
117
118 brush_pos_prev_re_ = self.last_mouse_position_re_;
119 brush_pos_re_ = stroke_extension.mouse_position;
121
122 if (stroke_extension.is_first) {
123 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE || (U.uiflag & USER_ORBIT_SELECTION)) {
124 std::optional<CurvesBrush3D> brush_3d = sample_curves_3d_brush(*ctx_.depsgraph,
125 *ctx_.region,
126 *ctx_.v3d,
127 *ctx_.rv3d,
128 *object_,
131 if (brush_3d.has_value()) {
132 self_->brush_3d_ = *brush_3d;
135 math::transform_point(transforms_.curves_to_world, self_->brush_3d_.position_cu));
136 }
137 }
138 return;
139 }
140
141 if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
143 }
144 else if (falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
146 }
147 else {
149 }
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->projected_snake_hook(brush_transform);
163 }
164 }
165
166 void projected_snake_hook(const float4x4 &brush_transform)
167 {
168 const float4x4 brush_transform_inv = math::invert(brush_transform);
169 const bke::crazyspace::GeometryDeformation deformation =
171 const OffsetIndices points_by_curve = curves_->points_by_curve();
172
174
176
177 const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
178 const float brush_radius_sq_re = pow2f(brush_radius_re);
179
181 [&](const IndexMaskSegment segment) {
182 MoveAndResampleBuffers resample_buffer;
183 for (const int curve_i : segment) {
184 const IndexRange points = points_by_curve[curve_i];
185 const int last_point_i = points.last();
186 const float3 old_pos_cu = deformation.positions[last_point_i];
187 const float3 old_symm_pos_cu = math::transform_point(brush_transform_inv, old_pos_cu);
188
189 const float2 old_symm_pos_re = ED_view3d_project_float_v2_m4(
190 ctx_.region, old_symm_pos_cu, projection);
191
192 const float distance_to_brush_sq_re = math::distance_squared(old_symm_pos_re,
194 if (distance_to_brush_sq_re > brush_radius_sq_re) {
195 continue;
196 }
197
198 const float radius_falloff = BKE_brush_curve_strength(
199 brush_, std::sqrt(distance_to_brush_sq_re), brush_radius_re);
200 const float weight = brush_strength_ * radius_falloff * curve_factors_[curve_i];
201
202 const float2 new_symm_pos_re = old_symm_pos_re + brush_pos_diff_re_ * weight;
203 float3 new_symm_pos_wo;
205 ctx_.v3d,
206 ctx_.region,
208 new_symm_pos_re,
209 new_symm_pos_wo);
210 const float3 new_pos_cu = math::transform_point(
211 brush_transform,
213 const float3 translation_eval = new_pos_cu - old_pos_cu;
214 const float3 translation_orig = deformation.translation_from_deformed_to_original(
215 last_point_i, translation_eval);
216
217 const float3 last_point_cu = positions_cu[last_point_i] + translation_orig;
219 resample_buffer, positions_cu.slice(points), last_point_cu);
220 }
221 },
223 }
224
226 {
227 float3 brush_start_wo, brush_end_wo;
229 ctx_.v3d,
230 ctx_.region,
233 brush_start_wo);
235 ctx_.v3d,
236 ctx_.region,
239 brush_end_wo);
241 brush_start_wo);
242 const float3 brush_end_cu = math::transform_point(transforms_.world_to_curves, brush_end_wo);
243
244 const float brush_radius_cu = self_->brush_3d_.radius_cu * brush_radius_factor_;
245
246 const Vector<float4x4> symmetry_brush_transforms = get_symmetry_brush_transforms(
248 for (const float4x4 &brush_transform : symmetry_brush_transforms) {
249 this->spherical_snake_hook(math::transform_point(brush_transform, brush_start_cu),
250 math::transform_point(brush_transform, brush_end_cu),
251 brush_radius_cu);
252 }
253 }
254
255 void spherical_snake_hook(const float3 &brush_start_cu,
256 const float3 &brush_end_cu,
257 const float brush_radius_cu)
258 {
259 const bke::crazyspace::GeometryDeformation deformation =
261 const OffsetIndices points_by_curve = curves_->points_by_curve();
262
264 const float3 brush_diff_cu = brush_end_cu - brush_start_cu;
265 const float brush_radius_sq_cu = pow2f(brush_radius_cu);
266
268 [&](const IndexMaskSegment segment) {
269 MoveAndResampleBuffers resample_buffer;
270 for (const int curve_i : segment) {
271 const IndexRange points = points_by_curve[curve_i];
272 const int last_point_i = points.last();
273 const float3 old_pos_cu = deformation.positions[last_point_i];
274
275 const float distance_to_brush_sq_cu = dist_squared_to_line_segment_v3(
276 old_pos_cu, brush_start_cu, brush_end_cu);
277 if (distance_to_brush_sq_cu > brush_radius_sq_cu) {
278 continue;
279 }
280
281 const float distance_to_brush_cu = std::sqrt(distance_to_brush_sq_cu);
282
283 const float radius_falloff = BKE_brush_curve_strength(
284 brush_, distance_to_brush_cu, brush_radius_cu);
285 const float weight = brush_strength_ * radius_falloff * curve_factors_[curve_i];
286
287 const float3 translation_eval = weight * brush_diff_cu;
288 const float3 translation_orig = deformation.translation_from_deformed_to_original(
289 last_point_i, translation_eval);
290
291 const float3 last_point_cu = positions_cu[last_point_i] + translation_orig;
293 resample_buffer, positions_cu.slice(points), last_point_cu);
294 }
295 },
297 }
298};
299
301 const StrokeExtension &stroke_extension)
302{
303 SnakeHookOperatorExecutor executor{stroke};
304 executor.execute(*this, stroke_extension);
305}
306
307std::unique_ptr<CurvesSculptStrokeOperation> new_snake_hook_operation()
308{
309 return std::make_unique<SnakeHookOperation>();
310}
311
312} // 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()
constexpr int64_t last(const int64_t n=0) const
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
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)
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)
std::unique_ptr< CurvesSculptStrokeOperation > new_snake_hook_operation()
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)
void move_last_point_and_resample(MoveAndResampleBuffers &buffer, MutableSpan< float3 > positions, const float3 &new_last_position)
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
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
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
float3 translation_from_deformed_to_original(const int position_i, const float3 &translation) const
void spherical_snake_hook(const float3 &brush_start_cu, const float3 &brush_end_cu, const float brush_radius_cu)
void execute(SnakeHookOperation &self, const StrokeExtension &stroke_extension)
void projected_snake_hook(const float4x4 &brush_transform)