Blender V5.3
MOD_grease_pencil_length.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_hash_c.hh"
10#include "BLI_rand_c.hh"
11
12#include "BLT_translation.hh"
13
14#include "BLO_read_write.hh"
15
17#include "DNA_node_types.h" /* For `GeometryNodeCurveSampleMode` */
18#include "DNA_object_types.h"
19#include "DNA_screen_types.h"
20
21#include "BKE_curves.hh"
22#include "BKE_geometry_set.hh"
23#include "BKE_grease_pencil.hh"
24#include "BKE_modifier.hh"
25
27#include "UI_resources.hh"
28
30#include "MOD_modifiertypes.hh"
31#include "MOD_ui_common.hh"
32
33#include "RNA_access.hh"
34#include "RNA_prototypes.hh"
35
36#include "GEO_extend_curves.hh"
37#include "GEO_trim_curves.hh"
38
39namespace blender {
40
47
48static void copy_data(const ModifierData *md, ModifierData *target, int flags)
49{
50 const auto *omd = reinterpret_cast<const GreasePencilLengthModifierData *>(md);
51 auto *tomd = reinterpret_cast<GreasePencilLengthModifierData *>(target);
52
54
55 BKE_modifier_copydata_generic(md, target, flags);
56 modifier::greasepencil::copy_influence_data(&omd->influence, &tomd->influence, flags);
57}
58
59static void free_data(ModifierData *md)
60{
61 auto *omd = reinterpret_cast<GreasePencilLengthModifierData *>(md);
63}
64
65static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
66{
67 auto *omd = reinterpret_cast<GreasePencilLengthModifierData *>(md);
68 modifier::greasepencil::foreach_influence_ID_link(&omd->influence, ob, walk, user_data);
69}
70
71static void blend_write(BlendWriter *writer, const ID * /*id_owner*/, const ModifierData *md)
72{
74 reinterpret_cast<const GreasePencilLengthModifierData *>(md);
75
76 writer->write_struct(mmd);
78}
79
86
87static Array<float> noise_table(int len, int offset, int seed)
88{
89 Array<float> table(len);
90 for (const int i : table.index_range()) {
91 table[i] = BLI_hash_int_01(BLI_hash_int_2d(seed, i + offset + 1));
92 }
93 return table;
94}
95
96static float table_sample(MutableSpan<float> table, float x)
97{
98 return math::interpolate(table[int(math::floor(x))], table[int(math::ceil(x))], math::fract(x));
99}
100
101static void deform_drawing(const ModifierData &md,
102 const Object &ob,
104 const int current_time)
105{
107 reinterpret_cast<const GreasePencilLengthModifierData &>(md);
110
111 if (curves.is_empty()) {
112 return;
113 }
114
115 IndexMaskMemory memory;
117 &ob, curves, mmd.influence, memory);
118
119 const int curves_num = curves.curves_num();
120
121 /* Variable for tagging shrinking when values are adjusted after random. */
122 std::atomic<bool> needs_additional_shrinking = false;
123
124 VArray<float> use_starts = VArray<float>::from_single(mmd.start_fac, curves_num);
125 VArray<float> use_ends = VArray<float>::from_single(mmd.end_fac, curves_num);
126
127 Array<float> modified_starts;
128 Array<float> modified_ends;
129 if (mmd.rand_start_fac != 0.0 || mmd.rand_end_fac != 0.0) {
130 modified_starts = Array<float>(curves.curves_num(), mmd.start_fac);
131 modified_ends = Array<float>(curves.curves_num(), mmd.end_fac);
132
133 /* Use random to modify start/end factors. Put the modified values outside the
134 * branch so it could be accessed in later stretching/shrinking stages. */
135 use_starts = VArray<float>::from_span(modified_starts.as_mutable_span());
136 use_ends = VArray<float>::from_span(modified_ends.as_mutable_span());
137
138 int seed = mmd.seed;
139
140 /* Make sure different modifiers get different seeds. */
141 seed += BLI_hash_string(ob.id.name + 2);
142 seed += BLI_hash_string(md.name);
143
144 if (mmd.flag & GP_LENGTH_USE_RANDOM) {
145 seed += current_time / mmd.step;
146 }
147
148 float rand_offset = BLI_hash_int_01(seed);
149
150 Array<float> noise_table_length = noise_table(
151 4 + curves_num, int(math::floor(mmd.rand_offset)), seed + 2);
152
153 threading::parallel_for(IndexRange(curves_num), 512, [&](const IndexRange parallel_range) {
154 for (const int i : parallel_range) {
155 /* To ensure a nice distribution, we use halton sequence and offset using the seed. */
156 double r[2];
157 const uint primes[2] = {2, 3};
158 double offset[2] = {0.0f, 0.0f};
159 BLI_halton_2d(primes, offset, i, r);
160
161 float rand[2] = {0.0f, 0.0f};
162 for (int j = 0; j < 2; j++) {
163 float noise = table_sample(noise_table_length, i + j * 2 + math::fract(mmd.rand_offset));
164
165 rand[j] = math::mod(float(r[j] + rand_offset), 1.0f);
166 rand[j] = math::abs(
167 math::mod(sin(rand[j] * 12.9898f + j * 78.233f) * 43758.5453f, 1.0f) + noise);
168 }
169
170 modified_starts[i] = modified_starts[i] + rand[0] * mmd.rand_start_fac;
171 modified_ends[i] = modified_ends[i] + rand[1] * mmd.rand_end_fac;
172
173 if (modified_starts[i] <= 0.0f || modified_ends[i] <= 0.0f) {
174 needs_additional_shrinking.store(true, std::memory_order_relaxed);
175 }
176 }
177 });
178 }
179
181 selection,
182 use_starts,
183 use_ends,
184 mmd.overshoot_fac,
185 (mmd.flag & GP_LENGTH_USE_CURVATURE) != 0,
186 mmd.point_density,
188 mmd.max_angle,
189 (mmd.flag & GP_LENGTH_INVERT_CURVATURE) != 0,
190 ((mmd.mode & GP_LENGTH_ABSOLUTE) != 0) ?
193 {});
194
195 /* Always do the stretching first since it might depend on points which could be deleted by the
196 * shrink. */
197 if (mmd.start_fac < 0.0f || mmd.end_fac < 0.0f || needs_additional_shrinking) {
198 /* #trim_curves() accepts the `end` values if it's sampling from the beginning of the
199 * curve, so we need to get the lengths of the curves and subtract it from the back when the
200 * modifier is in Absolute mode. For convenience, we always call #trim_curves() in LENGTH
201 * mode since the function itself will need length to be sampled anyway. */
202 Array<float> starts(curves.curves_num());
203 Array<float> ends(curves.curves_num());
204 Array<bool> needs_removal(curves.curves_num());
205 needs_removal.fill(false);
206
207 curves.ensure_evaluated_lengths();
208 const VArray<bool> &cyclic = curves.cyclic();
209
210 threading::parallel_for(curves.curves_range(), 512, [&](const IndexRange parallel_range) {
211 for (const int curve : parallel_range) {
212 float length = curves.evaluated_length_total_for_curve(curve, cyclic[curve]);
213 if (mmd.mode & GP_LENGTH_ABSOLUTE) {
214 starts[curve] = -math::min(use_starts[curve], 0.0f);
215 ends[curve] = length - -math::min(use_ends[curve], 0.0f);
216 }
217 else {
218 starts[curve] = -math::min(use_starts[curve], 0.0f) * length;
219 ends[curve] = (1 + math::min(use_ends[curve], 0.0f)) * length;
220 }
221 if (starts[curve] > ends[curve]) {
222 needs_removal[curve] = true;
223 }
224 }
225 });
227 selection,
228 VArray<float>::from_span(starts.as_span()),
229 VArray<float>::from_span(ends.as_span()),
231 {});
232
233 /* #trim_curves() will leave the last segment there when trimmed length is greater than
234 * curve original length, thus we need to remove those curves afterwards. */
235 IndexMaskMemory memory_remove;
236 const IndexMask to_remove = IndexMask::from_bools(needs_removal.as_span(), memory_remove);
237 if (!to_remove.is_empty()) {
238 curves.remove_curves(to_remove, {});
239 }
240 }
241
242 drawing.tag_topology_changed();
243}
244
246 const ModifierEvalContext *ctx,
247 bke::GeometrySet *geometry_set)
248{
250
251 if (!geometry_set->has_grease_pencil()) {
252 return;
253 }
254
256
257 IndexMaskMemory mask_memory;
259 grease_pencil, mmd->influence, mask_memory);
262 grease_pencil, layer_mask, grease_pencil.runtime->eval_frame);
263
265 deform_drawing(*md, *ctx->object, *drawing, grease_pencil.runtime->eval_frame);
266 });
267}
268
269static void panel_draw(const bContext *C, Panel *panel)
270{
271 ui::Layout &layout = *panel->layout;
272
274
275 layout.use_property_split_set(true);
276 layout.prop(ptr, "mode", UI_ITEM_NONE, std::nullopt, ICON_NONE);
277
278 ui::Layout &col = layout.column(true);
279
280 if (RNA_enum_get(ptr, "mode") == GP_LENGTH_RELATIVE) {
281 col.prop(ptr, "start_factor", UI_ITEM_NONE, IFACE_("Start"), ICON_NONE);
282 col.prop(ptr, "end_factor", UI_ITEM_NONE, IFACE_("End"), ICON_NONE);
283 }
284 else {
285 col.prop(ptr, "start_length", UI_ITEM_NONE, IFACE_("Start"), ICON_NONE);
286 col.prop(ptr, "end_length", UI_ITEM_NONE, IFACE_("End"), ICON_NONE);
287 }
288
289 layout.prop(ptr, "overshoot_factor", ui::ITEM_R_SLIDER, IFACE_("Used Length"), ICON_NONE);
290 ui::PanelLayout random_panel_layout = layout.panel_prop_with_bool_header(
291 C, ptr, "open_random_panel", ptr, "use_random", IFACE_("Randomize"));
292 if (ui::Layout *random_layout = random_panel_layout.body) {
293 ui::Layout &subcol = random_layout->column(false);
294 subcol.use_property_split_set(true);
295 subcol.active_set(RNA_boolean_get(ptr, "use_random"));
296
297 subcol.prop(ptr, "step", UI_ITEM_NONE, std::nullopt, ICON_NONE);
298
299 subcol.prop(ptr, "random_start_factor", UI_ITEM_NONE, IFACE_("Offset Start"), ICON_NONE);
300 subcol.prop(ptr, "random_end_factor", UI_ITEM_NONE, IFACE_("End"), ICON_NONE);
301 subcol.prop(ptr, "random_offset", UI_ITEM_NONE, IFACE_("Noise Offset"), ICON_NONE);
302 subcol.prop(ptr, "seed", UI_ITEM_NONE, std::nullopt, ICON_NONE);
303 }
304 ui::PanelLayout curvature_panel_layout = layout.panel_prop_with_bool_header(
305 C, ptr, "open_curvature_panel", ptr, "use_curvature", IFACE_("Curvature"));
306 if (ui::Layout *curvature_layout = curvature_panel_layout.body) {
307 ui::Layout &subcol = curvature_layout->column(false);
308 subcol.use_property_split_set(true);
309 subcol.active_set(RNA_boolean_get(ptr, "use_curvature"));
310
311 subcol.prop(ptr, "point_density", UI_ITEM_NONE, std::nullopt, ICON_NONE);
312 subcol.prop(ptr, "segment_influence", UI_ITEM_NONE, std::nullopt, ICON_NONE);
313 subcol.prop(ptr, "max_angle", UI_ITEM_NONE, std::nullopt, ICON_NONE);
314 subcol.prop(ptr, "invert_curvature", UI_ITEM_NONE, IFACE_("Invert"), ICON_NONE);
315 }
316
317 if (ui::Layout *influence_panel = layout.panel_prop(
318 C, ptr, "open_influence_panel", IFACE_("Influence")))
319 {
322 }
323
324 modifier_error_message_draw(layout, ptr);
325}
326
331
333 /*idname*/ "GreasePencilLengthModifier",
334 /*name*/ N_("Length"),
335 /*struct_name*/ "GreasePencilLengthModifierData",
336 /*struct_size*/ sizeof(GreasePencilLengthModifierData),
337 /*srna*/ &RNA_GreasePencilLengthModifier,
339 /*flags*/
342 /*icon*/ ICON_MOD_LENGTH,
343
344 /*copy_data*/ copy_data,
345
346 /*deform_verts*/ nullptr,
347 /*deform_matrices*/ nullptr,
348 /*deform_verts_EM*/ nullptr,
349 /*deform_matrices_EM*/ nullptr,
350 /*modify_mesh*/ nullptr,
351 /*modify_geometry_set*/ modify_geometry_set,
352
353 /*init_data*/ init_data,
354 /*required_data_mask*/ nullptr,
355 /*free_data*/ free_data,
356 /*is_disabled*/ nullptr,
357 /*update_depsgraph*/ nullptr,
358 /*depends_on_time*/ nullptr,
359 /*depends_on_normals*/ nullptr,
360 /*foreach_ID_link*/ foreach_ID_link,
361 /*foreach_tex_link*/ nullptr,
362 /*free_runtime_data*/ nullptr,
363 /*panel_register*/ panel_register,
364 /*blend_write*/ blend_write,
365 /*blend_read*/ blend_read,
366};
367
368} // namespace blender
Low-level operations for curves.
Low-level operations for grease pencil.
Random number functions.
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
IndexRange index_range() const
Definition BLI_array.hh:371
void fill(const T &value) const
Definition BLI_array.hh:283
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr IndexRange()=default
static VArray from_single(T value, const int64_t size)
static VArray from_span(Span< T > values)
bke::CurvesGeometry & strokes_for_write()
#define sin
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
bke::CurvesGeometry trim_curves(const bke::CurvesGeometry &src_curves, const IndexMask &selection, const VArray< float > &starts, const VArray< float > &ends, GeometryNodeCurveSampleMode mode, const bke::AttributeFilter &attribute_filter)
bke::CurvesGeometry extend_curves(bke::CurvesGeometry &src_curves, const IndexMask &selection, const VArray< float > &start_lengths, const VArray< float > &end_lengths, float overshoot_fac, bool follow_curvature, float point_density, float segment_influence, float max_angle, bool invert_curvature, GeometryNodeCurveSampleMode sample_mode, const bke::AttributeFilter &attribute_filter)
T floor(const T &a)
T interpolate(const T &a, const T &b, const FactorT &t)
T fract(const T &a)
T ceil(const T &a)
T mod(const T &a, const T &b)
T abs(const T &a)
void read_influence_data(BlendDataReader *reader, GreasePencilModifierInfluenceData *influence_data)
void draw_material_filter_settings(const bContext *, ui::Layout &layout, PointerRNA *ptr)
void init_influence_data(GreasePencilModifierInfluenceData *influence_data, const bool has_custom_curve)
void write_influence_data(BlendWriter *writer, const GreasePencilModifierInfluenceData *influence_data)
void draw_layer_filter_settings(const bContext *, ui::Layout &layout, PointerRNA *ptr)
IndexMask get_filtered_layer_mask(const GreasePencil &grease_pencil, const GreasePencilModifierInfluenceData &influence_data, IndexMaskMemory &memory)
Vector< bke::greasepencil::Drawing * > get_drawings_for_write(GreasePencil &grease_pencil, const IndexMask &layer_mask, const int frame)
IndexMask get_filtered_stroke_mask(const Object *ob, const bke::CurvesGeometry &curves, const GreasePencilModifierInfluenceData &influence_data, IndexMaskMemory &memory)
void free_influence_data(GreasePencilModifierInfluenceData *influence_data)
void foreach_influence_ID_link(GreasePencilModifierInfluenceData *influence_data, Object *ob, IDWalkFunc walk, void *user_data)
void copy_influence_data(const GreasePencilModifierInfluenceData *influence_data_src, GreasePencilModifierInfluenceData *influence_data_dst, const int)
void ensure_no_bezier_curves(Drawing &drawing)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:31
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:69
BMesh Iterators.
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
static float table_sample(MutableSpan< float > table, float x)
int RNA_enum_get(PointerRNA *ptr, const char *name)
static void blend_write(BlendWriter *writer, const ID *, const ModifierData *md)
Definition MOD_bevel.cc:454
void BLI_halton_2d(const unsigned int prime[2], double offset[2], int n, double *r)
Definition rand.cc:239
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
unsigned int uint
Definition BKE_text.h:55
BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
Definition BLI_hash_c.hh:53
ModifierTypeInfo modifierType_GreasePencilLength
@ eModifierType_GreasePencilLength
static void init_data(ModifierData *md)
void(*)(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag) IDWalkFunc
static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void *user_data)
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition BLI_hash_c.hh:94
static void modify_geometry_set(ModifierData *md, const ModifierEvalContext *ctx, bke::GeometrySet *geometry_set)
static void deform_drawing(const ModifierData &md, const Object &ob, bke::greasepencil::Drawing &drawing)
@ GP_LENGTH_INVERT_CURVATURE
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
static void free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
@ eModifierTypeFlag_AcceptsGreasePencil
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
static void blend_read(BlendDataReader *, ModifierData *md)
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition BLI_hash_c.hh:69
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
static void panel_draw(const bContext *, Panel *panel)
static Array< float > noise_table(int len, int offset, int seed)
@ GEO_NODE_CURVE_SAMPLE_LENGTH
@ GEO_NODE_CURVE_SAMPLE_FACTOR
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
GreasePencilModifierInfluenceData influence
char name[258]
Definition DNA_ID.h:433
ui::Layout * layout
GreasePencil * get_grease_pencil_for_write()
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & column(bool align)
PanelLayout panel_prop_with_bool_header(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name, PointerRNA *bool_prop_owner, StringRefNull bool_prop_name, std::optional< StringRef > label)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)
#define N_(msgid)