Blender V5.3
vertex_average.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_brush.hh"
6#include "BKE_context.hh"
7#include "BKE_curves.hh"
9#include "BKE_paint.hh"
10
12
14
15class VertexAverageOperation : public GreasePencilStrokeOperationCommon {
17
18 public:
19 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override;
20 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override;
21 void on_stroke_done(const bContext &C) override;
22};
23
25{
26 this->init_stroke(C, start_sample);
27 this->on_stroke_extended(C, start_sample);
28}
29
31 const InputSample &extension_sample)
32{
33 const Scene &scene = *CTX_data_scene(&C);
35 const Brush &brush = *BKE_paint_brush(&paint);
36 const float radius = brush_radius(paint, brush, extension_sample.pressure);
37 const float radius_squared = radius * radius;
38
39 const bool use_selection_masking = ED_grease_pencil_any_vertex_mask_selection(
40 scene.toolsettings);
41
42 const bool do_points = do_vertex_color_points(brush);
43 const bool do_fill = do_vertex_color_fill(brush);
44
45 /* Compute the average color under the brush. */
46 float3 average_color(0.0f);
47 int color_count = 0;
48 this->foreach_editable_drawing(C, [&](const GreasePencilStrokeParams &params) {
49 IndexMaskMemory memory;
50 const IndexMask point_selection = point_mask_for_stroke_operation(
51 params, use_selection_masking, memory);
52 if (!point_selection.is_empty() && do_points) {
53 const Array<float2> view_positions = view_positions_from_point_mask(params, point_selection);
54 const VArray<ColorGeometry4f> vertex_colors = params.drawing.vertex_colors();
55
56 point_selection.foreach_index([&](const int64_t point_i) {
57 const ColorGeometry4f color = vertex_colors[point_i];
58 if (color.a > 0.0f && math::distance_squared(extension_sample.mouse_position,
59 view_positions[point_i]) < radius_squared)
60 {
61 average_color += float3(color.r, color.g, color.b);
62 color_count++;
63 }
64 });
65 }
66 const IndexMask fill_selection = fill_mask_for_stroke_operation(
67 params, use_selection_masking, memory);
68 if (!fill_selection.is_empty() && do_fill) {
69 BLI_assert(params.drawing.fills().has_value());
70 const GroupedSpan<int> fills = *params.drawing.fills();
71 const OffsetIndices<int> points_by_curve = params.drawing.strokes().points_by_curve();
72 const Array<float2> view_positions = view_positions_from_point_mask(params, point_selection);
73 const VArray<ColorGeometry4f> fill_colors = params.drawing.fill_colors();
74 fill_selection.foreach_index([&](const int64_t fill_i) {
75 const Span<int> curves = fills[fill_i];
76 for (const int curve_i : curves) {
77 const IndexRange points = points_by_curve[curve_i];
78 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
79 const ColorGeometry4f color = fill_colors[curve_i];
80 if (color.a > 0.0f && closest_distance_to_surface_2d(extension_sample.mouse_position,
81 curve_view_positions) < radius)
82 {
83 average_color += float3(color.r, color.g, color.b);
84 color_count++;
85 }
86 }
87 });
88 }
89 return true;
90 });
91
92 if (color_count <= 0) {
93 return;
94 }
95 average_color = average_color / color_count;
96 /* The average color is the color that will be mixed in. */
97 const ColorGeometry4f mix_color(average_color.x, average_color.y, average_color.z, 1.0f);
98
100 C,
101 [&](const GreasePencilStrokeParams &params) {
102 IndexMaskMemory memory;
103 const IndexMask point_selection = point_mask_for_stroke_operation(
104 params, use_selection_masking, memory);
105 if (!point_selection.is_empty() && do_points) {
106 const Array<float2> view_positions = view_positions_from_point_mask(params,
107 point_selection);
109
110 point_selection.foreach_index(
111 [&](const int64_t point_i) {
112 const float influence = brush_point_influence(paint,
113 brush,
114 view_positions[point_i],
115 extension_sample,
116 params.multi_frame_falloff);
117
118 ColorGeometry4f &color = vertex_colors[point_i];
119 color = math::interpolate(color, mix_color, influence);
120 },
122 }
123
124 const IndexMask fill_selection = fill_mask_for_stroke_operation(
125 params, use_selection_masking, memory);
126 if (!fill_selection.is_empty() && do_fill) {
127 BLI_assert(params.drawing.fills().has_value());
128 const GroupedSpan<int> fills = *params.drawing.fills();
129 const bke::CurvesGeometry &curves = params.drawing.strokes();
130 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
132 /* TODO. Only calculate needed positions. */
133 const Array<float2> view_positions = view_positions_from_curve_mask(
134 params, curves.curves_range());
135
136 fill_selection.foreach_index(
137 [&](const int64_t fill_i) {
138 const Span<int> fill_curves = fills[fill_i];
139 const IndexMask fill_curve_mask = IndexMask::from_indices(fill_curves, memory);
140
141 float influence = 0.0f;
142 fill_curve_mask.foreach_index([&](const int64_t curve) {
143 const IndexRange points = points_by_curve[curve];
144 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
145 influence = math::max(influence,
147 brush,
148 curve_view_positions,
149 extension_sample,
150 params.multi_frame_falloff));
151 });
152
154 fill_colors[fill_curves.first()], mix_color, influence);
155 index_mask::masked_fill(fill_colors, color, fill_curve_mask);
156 },
158 }
159 return true;
160 },
162}
163
165
166std::unique_ptr<GreasePencilStrokeOperation> new_vertex_average_operation()
167{
168 return std::make_unique<VertexAverageOperation>();
169}
170
171} // namespace blender::ed::sculpt_paint::greasepencil
Low-level operations for curves.
Low-level operations for grease pencil.
#define C
Definition RandGen.cpp:29
Span< T > as_span() const
Definition BLI_array.hh:254
static IndexMask from_indices(Span< T > indices, LinearAllocator<> &memory)
constexpr const T & first() const
Definition BLI_span.hh:319
OffsetIndices< int > points_by_curve() const
VArray< ColorGeometry4f > vertex_colors() const
std::optional< GroupedSpan< int > > fills() const
const bke::CurvesGeometry & strokes() const
VArray< ColorGeometry4f > fill_colors() const
MutableSpan< ColorGeometry4f > fill_colors_for_write()
MutableSpan< ColorGeometry4f > vertex_colors_for_write()
void init_stroke(const bContext &C, const InputSample &start_sample)
void foreach_editable_drawing(const bContext &C, FunctionRef< bool(const GreasePencilStrokeParams &params, const DeltaProjectionFunc &projection_fn)> 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 foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
void mix_color(float fac, float4 col1, float4 col2, float4 &outcol)
float closest_distance_to_surface_2d(const float2 pt, const Span< float2 > verts)
IndexMask fill_mask_for_stroke_operation(const GreasePencilStrokeParams &params, bool use_selection_masking, IndexMaskMemory &memory)
bool do_vertex_color_fill(const Brush &brush)
Array< float2 > view_positions_from_curve_mask(const GreasePencilStrokeParams &params, const IndexMask &curve_mask)
IndexMask point_mask_for_stroke_operation(const GreasePencilStrokeParams &params, bool use_selection_masking, IndexMaskMemory &memory)
float brush_point_influence(const Paint &paint, const Brush &brush, const float2 &co, const InputSample &sample, float multi_frame_falloff)
Array< float2 > view_positions_from_point_mask(const GreasePencilStrokeParams &params, const IndexMask &point_mask)
float brush_fill_influence(const Paint &paint, const Brush &brush, Span< float2 > fill_positions, const InputSample &sample, float multi_frame_falloff)
std::unique_ptr< GreasePencilStrokeOperation > new_vertex_average_operation()
bool do_vertex_color_points(const Brush &brush)
float brush_radius(const Paint &paint, const Brush &brush, float pressure)
constexpr ParallelGrainSize grain_size(int grain_size)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
T interpolate(const T &a, const T &b, const FactorT &t)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T max(const T &a, const T &b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
bool ED_grease_pencil_any_vertex_mask_selection(const ToolSettings *tool_settings)
Paint * BKE_paint_get_active_from_context(const bContext *C)
Brush * BKE_paint_brush(Paint *paint)
Scene * CTX_data_scene(const bContext *C)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
struct ToolSettings * toolsettings