Blender V5.3
grease_pencil/sculpt_smooth.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_attribute.hh"
6#include "BKE_brush.hh"
7#include "BKE_colortools.hh"
8#include "BKE_context.hh"
9#include "BKE_curves.hh"
10#include "BKE_grease_pencil.hh"
11#include "BKE_paint.hh"
12#include "BKE_paint_types.hh"
13
14#include "DNA_brush_enums.h"
15#include "DNA_brush_types.h"
16
17#include "GEO_smooth_curves.hh"
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 private:
32 bool temp_smooth_;
33
34 /* Used when temporarily switching to smooth brush, save the previous active brush. */
35 Brush *saved_active_brush_;
36 int saved_smooth_size_; /* Smooth tool copies the size of the current tool. */
37
38 void toggle_smooth_brush_on(const bContext &C);
39 void toggle_smooth_brush_off(const bContext &C);
40
41 public:
43
44 SmoothOperation(const BrushStrokeMode stroke_mode, const bool temp_smooth = false)
45 : GreasePencilStrokeOperationCommon(stroke_mode), temp_smooth_(temp_smooth)
46 {
47 }
48
49 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override;
50 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override;
51 void on_stroke_done(const bContext &C) override;
52};
53
54void SmoothOperation::toggle_smooth_brush_on(const bContext &C)
55{
57 Main *bmain = CTX_data_main(&C);
58 Brush *current_brush = BKE_paint_brush(paint);
59
60 /* Switch to the smooth brush if possible. */
61 if (!BKE_paint_brush_set_essentials(bmain, paint, "Smooth")) {
62 BKE_paint_brush_set(paint, current_brush);
63 init_brush(*current_brush);
64 saved_active_brush_ = nullptr;
65 return;
66 }
67 Brush *smooth_brush = BKE_paint_brush(paint);
68 BLI_assert(smooth_brush != nullptr);
69
70 init_brush(*smooth_brush);
71
72 saved_active_brush_ = current_brush;
73 saved_smooth_size_ = BKE_brush_size_get(paint, smooth_brush);
74
75 const int current_brush_size = BKE_brush_size_get(paint, current_brush);
76 BKE_brush_size_set(paint, smooth_brush, current_brush_size);
77}
78
79void SmoothOperation::toggle_smooth_brush_off(const bContext &C)
80{
83
84 /* If saved_active_brush is not set, brush was not switched/affected in
85 * toggle_temp_on(). */
86 if (saved_active_brush_) {
87 BKE_brush_size_set(paint, &brush, saved_smooth_size_);
88 BKE_paint_brush_set(paint, saved_active_brush_);
89 saved_active_brush_ = nullptr;
90 }
91}
92
93void SmoothOperation::on_stroke_begin(const bContext &C, const InputSample &start_sample)
94{
95 if (temp_smooth_) {
96 toggle_smooth_brush_on(C);
97 this->start_mouse_position = start_sample.mouse_position;
98 this->prev_mouse_position = start_sample.mouse_position;
99 }
100 else {
101 this->init_stroke(C, start_sample);
102 }
103 this->init_auto_masking(C, start_sample);
104}
105
106void SmoothOperation::on_stroke_extended(const bContext &C, const InputSample &extension_sample)
107{
109 const Brush &brush = [&]() -> const Brush & {
110 if (temp_smooth_ && saved_active_brush_) {
113 if (brush != nullptr) {
114 return *brush;
115 }
116 }
117 return *BKE_paint_brush(&paint);
118 }();
119 const int sculpt_mode_flag = brush.gpencil_settings->sculpt_mode_flag;
120
122 C, [&](const GreasePencilStrokeParams &params, const IndexMask &point_mask) {
123 /* Note: smoothing requires full range of view positions regardless of point selection. */
124 const Array<float2> view_positions = view_positions_from_point_mask(
125 params, params.drawing.strokes().points_range());
127 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
128 const OffsetIndices points_by_curve = curves.points_by_curve();
129 const VArray<bool> cyclic = curves.cyclic();
130 const int iterations = 2;
131
132 const VArray<float> influences = VArray<float>::from_func(
133 view_positions.size(), [&](const int64_t point_) {
134 return brush_point_influence(paint,
135 brush,
136 view_positions[point_],
137 extension_sample,
138 params.multi_frame_falloff);
139 });
140 Array<bool> selection_array(curves.points_num());
141 point_mask.to_bools(selection_array);
142 const VArray<bool> selection_varray = VArray<bool>::from_span(selection_array);
143
144 bool changed = false;
145 if (sculpt_mode_flag & GP_SCULPT_FLAGMODE_APPLY_POSITION) {
147 curves.curves_range(),
148 selection_varray,
149 iterations,
150 influences,
151 false,
152 false);
153
155 changed = true;
156 }
157 if (sculpt_mode_flag & GP_SCULPT_FLAGMODE_APPLY_STRENGTH) {
160 points_by_curve,
161 selection_varray,
162 cyclic,
163 iterations,
164 influences,
165 true,
166 false,
167 opacities);
168 changed = true;
169 }
170 if (sculpt_mode_flag & GP_SCULPT_FLAGMODE_APPLY_THICKNESS) {
171 const MutableSpan<float> radii = params.drawing.radii_for_write();
173 points_by_curve,
174 selection_varray,
175 cyclic,
176 iterations,
177 influences,
178 true,
179 false,
180 radii);
181 curves.tag_radii_changed();
182 changed = true;
183 }
184 if (sculpt_mode_flag & GP_SCULPT_FLAGMODE_APPLY_UV) {
185 if (bke::SpanAttributeWriter<float> rotations =
186 attributes.lookup_or_add_for_write_span<float>("rotation",
188 {
190 points_by_curve,
191 selection_varray,
192 cyclic,
193 iterations,
194 influences,
195 true,
196 false,
197 rotations.span);
198 rotations.finish();
199 changed = true;
200 }
201 }
202 return changed;
203 });
204 this->stroke_extended(extension_sample);
205}
206
208{
209 if (temp_smooth_) {
210 toggle_smooth_brush_off(C);
211 }
212}
213
214std::unique_ptr<GreasePencilStrokeOperation> new_smooth_operation(
215 const BrushStrokeMode stroke_mode, const bool temp_smooth)
216{
217 return std::make_unique<SmoothOperation>(stroke_mode, temp_smooth);
218}
219
220} // namespace blender::ed::sculpt_paint::greasepencil
Low-level operations for curves.
Low-level operations for grease pencil.
#define C
Definition RandGen.cpp:29
int64_t size() const
Definition BLI_array.hh:267
static VArray from_func(const int64_t size, GetFunc get_func)
static VArray from_span(Span< T > values)
IndexRange points_range() const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
MutableSpan< float > opacities_for_write()
MutableSpan< float > radii_for_write()
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
void foreach_editable_drawing_with_automask(const bContext &C, FunctionRef< bool(const GreasePencilStrokeParams &params, const IndexMask &points)> fn) const
void init_stroke(const bContext &C, const InputSample &start_sample)
void init_auto_masking(const bContext &C, const InputSample &start_sample)
void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override
SmoothOperation(const BrushStrokeMode stroke_mode, const bool temp_smooth=false)
void on_stroke_begin(const bContext &C, const InputSample &start_sample) override
void to_bools(MutableSpan< bool > r_bools) const
Array< float2 > view_positions_from_point_mask(const GreasePencilStrokeParams &params, const IndexMask &point_mask)
std::unique_ptr< GreasePencilStrokeOperation > new_smooth_operation(BrushStrokeMode stroke_mode, bool temp_smooth=false)
void smooth_curve_positions(bke::CurvesGeometry &curves, const IndexMask &curves_to_smooth, const VArray< bool > &point_selection, int iterations, const VArray< float > &influence_by_point, bool smooth_ends, bool keep_shape)
void smooth_curve_attribute(const IndexMask &curves_to_smooth, const OffsetIndices< int > points_by_curve, const VArray< bool > &point_selection, const VArray< bool > &cyclic, int iterations, float influence, bool smooth_ends, bool keep_shape, GMutableSpan attribute_data)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
Brush * BKE_paint_brush_from_essentials(Main *bmain, bke::paint::AssetCategory asset_category, const char *name)
int BKE_brush_size_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1356
bool BKE_paint_brush_set(Paint *paint, Brush *brush)
void BKE_brush_size_set(Paint *paint, Brush *brush, int size)
Definition brush.cc:1340
Main * CTX_data_main(const bContext *C)
Paint * BKE_paint_get_active_from_context(const bContext *C)
Brush * BKE_paint_brush(Paint *paint)
bool BKE_paint_brush_set_essentials(Main *bmain, Paint *paint, const char *name)
@ GP_SCULPT_FLAGMODE_APPLY_POSITION
@ GP_SCULPT_FLAGMODE_APPLY_UV
@ GP_SCULPT_FLAGMODE_APPLY_THICKNESS
@ GP_SCULPT_FLAGMODE_APPLY_STRENGTH