Blender V4.3
grease_pencil_sculpt_clone.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_curves.hh"
8#include "BKE_paint.hh"
9
10#include "ED_curves.hh"
11#include "ED_grease_pencil.hh"
12#include "ED_view3d.hh"
13
14#include "WM_api.hh"
15#include "WM_types.hh"
16
18#include "paint_intern.hh"
19
21
23 public:
25
26 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override;
27 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override;
28 void on_stroke_done(const bContext & /*C*/) override {}
29};
30
32{
33 return std::accumulate(values.begin(), values.end(), float2(0)) / values.size();
34}
35
36void CloneOperation::on_stroke_begin(const bContext &C, const InputSample &start_sample)
37{
38 Main &bmain = *CTX_data_main(&C);
39 Object &object = *CTX_data_active_object(&C);
40 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(object.data);
41
42 this->init_stroke(C, start_sample);
43
44 /* NOTE: Only one copy is created at the beginning of each stroke.
45 * GPv2 supposedly has 2 modes:
46 * - Stamp: Clone on stroke start and then transform (the transform part doesn't work)
47 * - Continuous: Create multiple copies during the stroke (disabled)
48 *
49 * Here we only have the GPv2 behavior that actually works for now. */
51 C, [&](const GreasePencilStrokeParams &params, const DeltaProjectionFunc &projection_fn) {
52 /* Only insert on the active layer. */
53 if (&params.layer != grease_pencil.get_active_layer()) {
54 return false;
55 }
56
57 /* TODO Could become a tool setting. */
58 const bool keep_world_transform = false;
59 const float4x4 clipboard_to_layer = math::invert(params.layer.to_world_space(object));
61 bmain, object, params.drawing, clipboard_to_layer, keep_world_transform, false);
62 if (pasted_curves.is_empty()) {
63 return false;
64 }
65
67 bke::CurvesGeometry &curves = params.drawing.strokes_for_write();
68 const OffsetIndices<int> pasted_points_by_curve = curves.points_by_curve().slice(
69 pasted_curves);
70 const IndexRange pasted_points = IndexRange::from_begin_size(
71 pasted_points_by_curve[0].start(), pasted_points_by_curve.total_size());
72 if (pasted_points.is_empty()) {
73 return false;
74 }
75
76 Array<float2> view_positions = calculate_view_positions(params, pasted_points);
77 const float2 center = arithmetic_mean(
78 view_positions.as_mutable_span().slice(pasted_points));
79 const float2 &mouse_delta = start_sample.mouse_position - center;
80
81 MutableSpan<float3> positions = curves.positions_for_write();
82 threading::parallel_for(pasted_points, 4096, [&](const IndexRange range) {
83 for (const int point_i : range) {
84 positions[point_i] = projection_fn(deformation.positions[point_i], mouse_delta);
85 }
86 });
87 params.drawing.tag_positions_changed();
88
89 return true;
90 });
91}
92
94 const InputSample &extension_sample)
95{
96 this->stroke_extended(extension_sample);
97}
98
99std::unique_ptr<GreasePencilStrokeOperation> new_clone_operation(const BrushStrokeMode stroke_mode)
100{
101 return std::make_unique<CloneOperation>(stroke_mode);
102}
103
104} // namespace blender::ed::sculpt_paint::greasepencil
Object * CTX_data_active_object(const bContext *C)
Main * CTX_data_main(const bContext *C)
Low-level operations for curves.
Low-level operations for grease pencil.
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:237
constexpr bool is_empty() const
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override
void on_stroke_begin(const bContext &C, const InputSample &start_sample) override
void foreach_editable_drawing(const bContext &C, FunctionRef< bool(const GreasePencilStrokeParams &params)> fn) const
OffsetIndices slice(const IndexRange range) const
IndexRange range
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
IndexRange clipboard_paste_strokes(Main &bmain, Object &object, bke::greasepencil::Drawing &drawing, const float4x4 &transform, const bool keep_world_transform, const bool paste_back)
static float2 arithmetic_mean(Span< float2 > values)
std::unique_ptr< GreasePencilStrokeOperation > new_clone_operation(BrushStrokeMode stroke_mode)
Array< float2 > calculate_view_positions(const GreasePencilStrokeParams &params, const IndexMask &selection)
bke::crazyspace::GeometryDeformation get_drawing_deformation(const GreasePencilStrokeParams &params)
std::function< float3(const float3 position, const float2 &screen_delta)> DeltaProjectionFunc
CartesianBasis invert(const CartesianBasis &basis)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:95
BrushStrokeMode