Blender V4.3
grease_pencil_sculpt_twist.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 "BLI_math_rotation.h"
6
7#include "BKE_context.hh"
8#include "BKE_crazyspace.hh"
9#include "BKE_curves.hh"
10#include "BKE_grease_pencil.hh"
11#include "BKE_paint.hh"
12
13#include "ED_grease_pencil.hh"
14#include "ED_view3d.hh"
15
16#include "WM_api.hh"
17#include "WM_types.hh"
18
20#include "paint_intern.hh"
21
23
25 public:
27
28 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override;
29 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override;
30 void on_stroke_done(const bContext & /*C*/) override {}
31};
32
33static float2 rotate_by_angle(const float2 &vec, const float angle)
34{
35 const float cos_angle = math::cos(angle);
36 const float sin_angle = math::sin(angle);
37 return float2(vec.x * cos_angle - vec.y * sin_angle, vec.x * sin_angle + vec.y * cos_angle);
38}
39
40void TwistOperation::on_stroke_begin(const bContext &C, const InputSample &start_sample)
41{
42 this->init_stroke(C, start_sample);
43}
44
45void TwistOperation::on_stroke_extended(const bContext &C, const InputSample &extension_sample)
46{
47 const Scene &scene = *CTX_data_scene(&C);
49 const Brush &brush = *BKE_paint_brush(&paint);
50 const bool invert = this->is_inverted(brush);
51
52 const bool is_masking = GPENCIL_ANY_SCULPT_MASK(
53 eGP_Sculpt_SelectMaskFlag(scene.toolsettings->gpencil_selectmode_sculpt));
54
56 C, [&](const GreasePencilStrokeParams &params, const DeltaProjectionFunc &projection_fn) {
57 IndexMaskMemory selection_memory;
58 const IndexMask selection = point_selection_mask(params, is_masking, selection_memory);
59 if (selection.is_empty()) {
60 return false;
61 }
62
64 Array<float2> view_positions = calculate_view_positions(params, selection);
65 bke::CurvesGeometry &curves = params.drawing.strokes_for_write();
66 MutableSpan<float3> positions = curves.positions_for_write();
67
68 const float2 mouse_pos = extension_sample.mouse_position;
69
70 selection.foreach_index(GrainSize(4096), [&](const int64_t point_i) {
71 const float2 &co = view_positions[point_i];
72 const float influence = brush_point_influence(
73 scene, brush, co, extension_sample, params.multi_frame_falloff);
74 if (influence <= 0.0f) {
75 return;
76 }
77
78 const float angle = DEG2RADF(invert ? -1.0f : 1.0f) * influence;
79 const float2 radial_offset = co - mouse_pos;
80 positions[point_i] = projection_fn(deformation.positions[point_i],
81 rotate_by_angle(radial_offset, angle) -
82 radial_offset);
83 });
84
85 params.drawing.tag_positions_changed();
86 return true;
87 });
88 this->stroke_extended(extension_sample);
89}
90
91std::unique_ptr<GreasePencilStrokeOperation> new_twist_operation(const BrushStrokeMode stroke_mode)
92{
93 return std::make_unique<TwistOperation>(stroke_mode);
94}
95
96} // namespace blender::ed::sculpt_paint::greasepencil
Scene * CTX_data_scene(const bContext *C)
Low-level operations for curves.
Low-level operations for grease pencil.
Paint * BKE_paint_get_active_from_context(const bContext *C)
Definition paint.cc:477
Brush * BKE_paint_brush(Paint *paint)
Definition paint.cc:649
#define DEG2RADF(_deg)
#define GPENCIL_ANY_SCULPT_MASK(flag)
eGP_Sculpt_SelectMaskFlag
void foreach_editable_drawing(const bContext &C, FunctionRef< bool(const GreasePencilStrokeParams &params)> 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
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
Definition invert.h:9
static float2 rotate_by_angle(const float2 &vec, const float angle)
IndexMask point_selection_mask(const GreasePencilStrokeParams &params, const bool use_masking, IndexMaskMemory &memory)
Array< float2 > calculate_view_positions(const GreasePencilStrokeParams &params, const IndexMask &selection)
float brush_point_influence(const Scene &scene, const Brush &brush, const float2 &co, const InputSample &sample, float multi_frame_falloff)
bke::crazyspace::GeometryDeformation get_drawing_deformation(const GreasePencilStrokeParams &params)
std::unique_ptr< GreasePencilStrokeOperation > new_twist_operation(BrushStrokeMode stroke_mode)
std::function< float3(const float3 position, const float2 &screen_delta)> DeltaProjectionFunc
T cos(const AngleRadianBase< T > &a)
T sin(const AngleRadianBase< T > &a)
VecBase< float, 2 > float2
BrushStrokeMode
__int64 int64_t
Definition stdint.h:89