Blender V5.3
sculpt_grab.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_context.hh"
6#include "BKE_crazyspace.hh"
7#include "BKE_curves.hh"
9#include "BKE_paint.hh"
10
11#include "BLI_index_mask.hh"
12#include "BLI_task.hh"
13
15
17#include "DNA_view3d_types.h"
18
19#include "ED_grease_pencil.hh"
20#include "ED_view3d.hh"
21
22#include "WM_api.hh"
23#include "WM_types.hh"
24
25#include "../paint_intern.hh"
27
29
31 public:
34
35 /* Cached point mask and influence for a particular drawing. */
36 struct PointWeights {
40
41 /* Layer space to view space projection at the start of the stroke. */
43 /* Points that are grabbed at the beginning of the stroke. */
47 /* Influence weights for grabbed points. */
51
53 };
54 /* Cached point data for each affected drawing. */
56
58 FunctionRef<bool(const GreasePencilStrokeParams &params,
59 const DeltaProjectionFunc &projection_fn,
60 const IndexMask &mask,
61 const IndexMask &mask_left,
62 const IndexMask &mask_right,
63 Span<float> weights,
64 Span<float> weights_left,
65 Span<float> weights_right)> fn) const;
66
67 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override;
68 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override;
69 void on_stroke_done(const bContext & /*C*/) override {}
70};
71
73 const bContext &C,
74 FunctionRef<bool(const GreasePencilStrokeParams &params,
75 const DeltaProjectionFunc &projection_fn,
76 const IndexMask &mask,
77 const IndexMask &mask_left,
78 const IndexMask &mask_right,
79 Span<float> weights,
80 Span<float> weights_left,
81 Span<float> weights_right)> fn) const
82{
85
86 const Scene &scene = *CTX_data_scene(&C);
87 Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(&C);
88 ARegion &region = *CTX_wm_region(&C);
90 Object &object = *CTX_data_active_object(&C);
91 Object &object_eval = *DEG_get_evaluated(&depsgraph, &object);
92 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
93
94 bool changed = false;
95 threading::parallel_for_each(this->drawing_data.index_range(), [&](const int i) {
96 const PointWeights &data = this->drawing_data[i];
97 if (data.point_mask.is_empty()) {
98 return;
99 }
100 const Layer &layer = grease_pencil.layer(data.layer_index);
101 /* If a new frame is created, could be impossible find the stroke. */
102 bke::greasepencil::Drawing *drawing = grease_pencil.get_drawing_at(layer, data.frame_number);
103 if (drawing == nullptr) {
104 return;
105 }
106
108 scene,
109 depsgraph,
110 region,
111 rv3d,
112 object,
113 data.layer_index,
114 data.frame_number,
115 data.multi_frame_falloff,
116 *drawing);
117 DeltaProjectionFunc projection_fn = get_screen_projection_fn(params, object_eval, layer);
118 if (fn(params,
119 projection_fn,
120 data.point_mask,
121 data.point_mask_left,
122 data.point_mask_right,
123 data.weights,
124 data.weights_left,
125 data.weights_right))
126 {
127 changed = true;
128 }
129 });
130
131 if (changed) {
134 }
135}
136
137void GrabOperation::on_stroke_begin(const bContext &C, const InputSample &start_sample)
138{
139 const ARegion &region = *CTX_wm_region(&C);
140 const RegionView3D &rv3d = *CTX_wm_region_view3d(&C);
141 const Scene &scene = *CTX_data_scene(&C);
144 const Depsgraph &depsgraph = *CTX_data_depsgraph_pointer(&C);
145 Object &ob_orig = *CTX_data_active_object(&C);
146 Object &ob_eval = *DEG_get_evaluated(&depsgraph, &ob_orig);
147 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(ob_orig.data);
148
150 init_auto_masking(C, start_sample);
151
152 this->prev_mouse_position = start_sample.mouse_position;
153
155 this->drawing_data.reinitialize(drawings.size());
156 threading::parallel_for_each(drawings.index_range(), [&](const int i) {
157 const MutableDrawingInfo &info = drawings[i];
158 const AutoMaskingInfo &auto_mask_info = this->auto_masking_info_per_drawing[i];
159 BLI_assert(info.layer_index >= 0);
160 PointWeights &data = this->drawing_data[i];
161
162 const bke::greasepencil::Layer &layer = grease_pencil.layer(info.layer_index);
163 BLI_assert(grease_pencil.get_drawing_at(layer, info.frame_number) == &info.drawing);
164
165 GreasePencilStrokeParams params = {*scene.toolsettings,
166 region,
167 rv3d,
168 scene,
169 ob_orig,
170 ob_eval,
171 layer,
172 info.layer_index,
173 info.frame_number,
174 info.multi_frame_falloff,
175 info.drawing};
176
177 const Array<float2> view_positions = view_positions_from_point_mask(params,
178 auto_mask_info.point_mask);
179 const Array<float2> view_positions_left = view_positions_left_from_point_mask(
180 params, auto_mask_info.point_mask);
181 const Array<float2> view_positions_right = view_positions_right_from_point_mask(
182 params, auto_mask_info.point_mask);
183
184 /* Cache points under brush influence. */
185 Vector<float> weights;
186 Vector<float> weights_left;
187 Vector<float> weights_right;
189 brush,
190 start_sample.mouse_position,
191 1.0f,
192 info.multi_frame_falloff,
193 auto_mask_info.point_mask,
194 view_positions,
195 weights,
196 data.memory);
198 brush,
199 start_sample.mouse_position,
200 1.0f,
201 info.multi_frame_falloff,
202 auto_mask_info.point_mask,
203 view_positions_left,
204 weights_left,
205 data.memory);
206 IndexMask point_mask_right = brush_point_influence_mask(paint,
207 brush,
208 start_sample.mouse_position,
209 1.0f,
210 info.multi_frame_falloff,
211 auto_mask_info.point_mask,
212 view_positions_right,
213 weights_right,
214 data.memory);
215
216 if (point_mask.is_empty()) {
217 /* Set empty point mask to skip. */
218 data.point_mask = {};
219 return;
220 }
221 data.layer_index = info.layer_index;
222 data.frame_number = info.frame_number;
223 data.multi_frame_falloff = info.multi_frame_falloff;
224 data.layer_to_win = ED_view3d_ob_project_mat_get(&rv3d, &ob_eval) *
225 layer.to_object_space(ob_eval);
226 data.point_mask = std::move(point_mask);
227 data.point_mask_left = std::move(point_mask_left);
228 data.point_mask_right = std::move(point_mask_right);
229 data.weights = std::move(weights);
230 data.weights_left = std::move(weights_left);
231 data.weights_right = std::move(weights_right);
232 });
233}
234
235void GrabOperation::on_stroke_extended(const bContext &C, const InputSample &extension_sample)
236{
238 C,
239 [&](const GreasePencilStrokeParams &params,
240 const DeltaProjectionFunc &projection_fn,
241 const IndexMask &mask,
242 const IndexMask &mask_left,
243 const IndexMask &mask_right,
244 const Span<float> weights,
245 const Span<float> weights_left,
246 const Span<float> weights_right) {
247 /* Transform mouse delta into layer space. */
248 const float2 mouse_delta_win = this->mouse_delta(extension_sample);
249
252 MutableSpan<float3> positions = curves.positions_for_write();
253 mask.foreach_index(
254 [&](const int point_i, const int index) {
255 /* Translate the point with the influence factor. */
256 positions[point_i] += compute_orig_delta(
257 projection_fn, deformation, point_i, mouse_delta_win * weights[index]);
258 },
260
261 if (curves.has_curve_with_type(CURVE_TYPE_BEZIER)) {
262 MutableSpan<float3> handle_positions_left = curves.handle_positions_left_for_write();
263 MutableSpan<float3> handle_positions_right = curves.handle_positions_right_for_write();
264
265 mask_left.foreach_index(
266 [&](const int64_t point_i, const int index) {
267 handle_positions_left[point_i] += compute_orig_delta(
268 projection_fn, deformation, point_i, mouse_delta_win * weights_left[index]);
269 },
271 mask_right.foreach_index(
272 [&](const int64_t point_i, const int index) {
273 handle_positions_right[point_i] += compute_orig_delta(
274 projection_fn, deformation, point_i, mouse_delta_win * weights_right[index]);
275 },
277
278 curves.calculate_bezier_auto_handles();
279 curves.calculate_bezier_aligned_handles();
280 }
281
283 return true;
284 });
285 this->stroke_extended(extension_sample);
286}
287
288std::unique_ptr<GreasePencilStrokeOperation> new_grab_operation(const BrushStrokeMode stroke_mode)
289{
290 return std::make_unique<GrabOperation>(stroke_mode);
291}
292
293} // namespace blender::ed::sculpt_paint::greasepencil
Low-level operations for curves.
Low-level operations for grease pencil.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
IndexRange index_range() const
Definition BLI_array.hh:371
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
int64_t size() const
IndexRange index_range() const
bke::CurvesGeometry & strokes_for_write()
ed::greasepencil::MutableDrawingInfo MutableDrawingInfo
void foreach_grabbed_drawing(const bContext &C, FunctionRef< bool(const GreasePencilStrokeParams &params, const DeltaProjectionFunc &projection_fn, const IndexMask &mask, const IndexMask &mask_left, const IndexMask &mask_right, Span< float > weights, Span< float > weights_left, Span< float > weights_right)> fn) const
void on_stroke_begin(const bContext &C, const InputSample &start_sample) override
void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override
void init_auto_masking(const bContext &C, const InputSample &start_sample)
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define layer
Array< float2 > view_positions_left_from_point_mask(const GreasePencilStrokeParams &params, const IndexMask &selection)
Array< float2 > view_positions_right_from_point_mask(const GreasePencilStrokeParams &params, const IndexMask &selection)
std::unique_ptr< GreasePencilStrokeOperation > new_grab_operation(BrushStrokeMode stroke_mode)
IndexMask brush_point_influence_mask(const Paint &paint, const Brush &brush, const float2 &mouse_position, float pressure, float multi_frame_falloff, const IndexMask &selection, Span< float2 > view_positions, Vector< float > &influences, IndexMaskMemory &memory)
Array< float2 > view_positions_from_point_mask(const GreasePencilStrokeParams &params, const IndexMask &point_mask)
Vector< ed::greasepencil::MutableDrawingInfo > get_drawings_with_masking_for_stroke_operation(const bContext &C)
bke::crazyspace::GeometryDeformation get_drawing_deformation(const GreasePencilStrokeParams &params)
DeltaProjectionFunc get_screen_projection_fn(const GreasePencilStrokeParams &params, const Object &object, const bke::greasepencil::Layer &layer)
float3 compute_orig_delta(const DeltaProjectionFunc &projection_fn, const bke::crazyspace::GeometryDeformation &deformation, int index, const float2 &screen_delta)
std::function< float3(const float3 position, const float2 &screen_delta)> DeltaProjectionFunc
constexpr ParallelGrainSize grain_size(int grain_size)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:31
__int64 int64_t
Definition BKE_text.h:90
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
MatBase< float, 4, 4 > float4x4
VecBase< float, 2 > float2
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
RegionView3D * CTX_wm_region_view3d(const bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
Paint * BKE_paint_get_active_from_context(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
Brush * BKE_paint_brush(Paint *paint)
Scene * CTX_data_scene(const bContext *C)
Object * CTX_data_active_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
static GreasePencilStrokeParams from_context(const Scene &scene, Depsgraph &depsgraph, ARegion &region, RegionView3D &rv3d, Object &object, int layer_index, int frame_number, float multi_frame_falloff, bke::greasepencil::Drawing &drawing)