39 bke::greasepencil::Drawing *from_drawing;
43enum class ActionOnNextRange { Nothing, ReverseExisting, ReverseAddition, ReverseBoth };
45enum class ActiveLayerBehavior { JoinStrokes, SplitAndCopy, SplitPoints };
53Vector<PointsRange> retrieve_selection_ranges(Object &
object,
54 const Span<MutableDrawingInfo> drawings,
55 const ActiveLayerBehavior active_layer_behavior,
56 int64_t &r_total_points_selected,
57 IndexMaskMemory &memory)
59 Vector<PointsRange> selected_ranges{};
60 r_total_points_selected = 0;
63 if (active_layer_behavior == ActiveLayerBehavior::JoinStrokes) {
65 object, info.drawing, info.layer_index, memory);
66 if (curves_selection.is_empty()) {
70 const OffsetIndices<int> points_by_curve = info.drawing.strokes().points_by_curve();
71 curves_selection.foreach_index([&](
const int curve_i) {
72 const IndexRange points = points_by_curve[curve_i];
73 selected_ranges.append({&info.drawing, points});
74 r_total_points_selected += points.size();
81 object, info.drawing, info.layer_index, memory);
82 if (points_selection.is_empty()) {
85 r_total_points_selected += points_selection.size();
87 const Vector<IndexRange> initial_ranges = points_selection.to_ranges();
94 const Array<int> points_map = info.drawing.strokes().point_to_curve_map();
95 for (
const IndexRange initial_range : initial_ranges) {
96 if (points_map[initial_range.first()] == points_map[initial_range.last()]) {
97 selected_ranges.append({&info.drawing, initial_range});
101 IndexRange range = {initial_range.start(), 1};
102 int previous_curve = points_map[range.start()];
103 for (
const int64_t index : initial_range.drop_front(1)) {
104 const int current_curve = points_map[
index];
105 if (previous_curve != current_curve) {
106 selected_ranges.append({&info.drawing, range});
108 previous_curve = current_curve;
111 range = {range.start(), range.size() + 1};
115 selected_ranges.append({&info.drawing, range});
119 return selected_ranges;
122template<
typename T>
void reverse_point_data(
const IndexRange point_range, MutableSpan<T>
data)
124 data.slice(point_range.first(), point_range.size()).reverse();
128void swap_handle_attributes(MutableSpan<T> handles_left, MutableSpan<T> handles_right)
130 BLI_assert(handles_left.size() == handles_right.size());
132 for (const int point : range) {
133 std::swap(handles_left[point], handles_right[point]);
142void reverse_points_of(bke::CurvesGeometry &dst_curves,
const IndexRange points_to_reverse)
144 bke::MutableAttributeAccessor attributes = dst_curves.attributes_for_write();
146 attributes.foreach_attribute([&](
const bke::AttributeIter &iter) {
157 bke::GSpanAttributeWriter
attribute = attributes.lookup_for_write_span(iter.name);
159 reverse_point_data<T>(points_to_reverse, attribute.span.typed<T>());
165 if (attributes.contains(
"handle_left") && attributes.contains(
"handle_right")) {
166 MutableSpan<float3> handles_left = dst_curves.handle_positions_left_for_write().slice(
168 MutableSpan<float3> handles_right = dst_curves.handle_positions_right_for_write().slice(
170 swap_handle_attributes<float3>(handles_left, handles_right);
172 if (attributes.contains(
".selection_handle_left") &&
173 attributes.contains(
".selection_handle_right"))
175 bke::SpanAttributeWriter<bool> writer_left = attributes.lookup_for_write_span<
bool>(
176 ".selection_handle_left");
177 bke::SpanAttributeWriter<bool> writer_right = attributes.lookup_for_write_span<
bool>(
178 ".selection_handle_right");
179 const MutableSpan<bool> selection_left = writer_left.span.slice(points_to_reverse);
180 const MutableSpan<bool> selection_right = writer_right.span.slice(points_to_reverse);
181 swap_handle_attributes<bool>(selection_left, selection_right);
182 writer_left.finish();
183 writer_right.finish();
185 if (attributes.contains(
"handle_type_left") && attributes.contains(
"handle_type_right")) {
186 MutableSpan<int8_t> types_left = dst_curves.handle_types_left_for_write().slice(
188 MutableSpan<int8_t> types_right = dst_curves.handle_types_right_for_write().slice(
190 swap_handle_attributes<int8_t>(types_left, types_right);
194void apply_action(ActionOnNextRange action,
195 const IndexRange working_range,
196 const IndexRange adding_range,
197 bke::CurvesGeometry &dst_curves)
212 case ActionOnNextRange::Nothing:
214 case ActionOnNextRange::ReverseExisting: {
215 reverse_points_of(dst_curves, working_range);
218 case ActionOnNextRange::ReverseAddition: {
219 const IndexRange src_range_on_dst = {working_range.last() + 1, adding_range.size()};
220 reverse_points_of(dst_curves, src_range_on_dst);
223 case ActionOnNextRange::ReverseBoth: {
224 apply_action(ActionOnNextRange::ReverseExisting, working_range, adding_range, dst_curves);
225 apply_action(ActionOnNextRange::ReverseAddition, working_range, adding_range, dst_curves);
238int64_t compute_closest_range_to(PointsRange &range,
239 const Span<PointsRange> &ranges,
241 ActionOnNextRange &r_action)
243 auto get_range_begin_end = [](
const PointsRange &points_range) -> std::pair<float3, float3> {
244 const Span<float3> current_range_positions = points_range.from_drawing->strokes().positions();
245 const float3 range_begin = current_range_positions[points_range.range.first()];
246 const float3 range_end = current_range_positions[points_range.range.last()];
248 return {range_begin, range_end};
251 const auto [cur_range_begin, cur_range_end] = get_range_begin_end(range);
254 int64_t ret_value = starting_from;
255 ActionOnNextRange action = ActionOnNextRange::Nothing;
257 const int64_t iterations = ranges.
size() - starting_from;
259 const auto [range_begin, range_end] = get_range_begin_end(ranges[i]);
262 if (dist < min_dist) {
263 action = ActionOnNextRange::Nothing;
269 if (dist < min_dist) {
270 action = ActionOnNextRange::ReverseExisting;
276 if (dist < min_dist) {
277 action = ActionOnNextRange::ReverseAddition;
283 if (dist < min_dist) {
284 action = ActionOnNextRange::ReverseBoth;
294void copy_range_to_dst(
const PointsRange &points_range,
295 int &dst_starting_point,
296 bke::CurvesGeometry &dst_curves)
298 Array<int> src_raw_offsets(2);
299 Array<int> dst_raw_offsets(2);
301 const int64_t selection_size = points_range.range.size();
302 src_raw_offsets[0] = points_range.range.first();
303 src_raw_offsets[1] = points_range.range.last() + 1;
305 dst_raw_offsets[0] = dst_starting_point;
306 dst_starting_point += selection_size;
307 dst_raw_offsets[1] = dst_starting_point;
309 OffsetIndices<int> src_offsets{src_raw_offsets};
310 OffsetIndices<int> dst_offsets{dst_raw_offsets};
319 dst_curves.attributes_for_write());
322PointsRange copy_point_attributes(MutableSpan<PointsRange> selected_ranges,
323 bke::CurvesGeometry &dst_curves,
324 bke::greasepencil::Drawing &dst_drawing)
338 const PointsRange &first_range = selected_ranges.first();
339 PointsRange working_range = {&dst_drawing, {0, first_range.range.size()}};
341 int next_point_index = 0;
342 copy_range_to_dst(first_range, next_point_index, dst_curves);
346 ActionOnNextRange action;
347 const int64_t closest_range = compute_closest_range_to(
348 working_range, selected_ranges, i, action);
349 std::swap(selected_ranges[i], selected_ranges[closest_range]);
350 PointsRange &next_range = selected_ranges[i];
351 copy_range_to_dst(next_range, next_point_index, dst_curves);
352 apply_action(action, working_range.range, next_range.range, dst_curves);
353 working_range.range = {0, next_point_index};
356 return working_range;
359void copy_curve_attributes(Span<PointsRange> ranges_selected,
360 bke::CurvesGeometry &dst_curves,
361 bke::greasepencil::Drawing &dst_drawing)
374 auto src_range = [&]() ->
const PointsRange & {
375 const auto *it = std::find_if(
376 ranges_selected.
begin(), ranges_selected.
end(), [dst_drawing](
const PointsRange &range) {
377 return range.from_drawing == &dst_drawing;
380 return it != ranges_selected.
end() ? *it : ranges_selected.
first();
383 const bke::CurvesGeometry &src_curves = src_range.from_drawing->strokes();
384 const Array<int> points_map = src_curves.point_to_curve_map();
385 const int first_selected_curve = points_map[src_range.range.first()];
387 const int final_curve_index = dst_curves.curves_num() - 1;
388 const Array<int> dst_curves_raw_offsets = {final_curve_index, dst_curves.curves_num()};
389 const OffsetIndices<int> dst_curve_offsets{dst_curves_raw_offsets};
396 IndexMask({first_selected_curve, 1}),
397 dst_curves.attributes_for_write());
398 dst_curves.cyclic_for_write().first() =
false;
405void clear_selection_attribute(Span<PointsRange> ranges_selected,
408 for (
const PointsRange &range : ranges_selected) {
409 bke::CurvesGeometry &curves = range.from_drawing->strokes_for_write();
410 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
411 if (bke::SpanAttributeWriter<bool> selection = attributes.lookup_or_add_for_write_span<
bool>(
412 ".selection", selection_domain))
414 selection.span.fill(
false);
417 if (bke::GSpanAttributeWriter selection = attributes.lookup_for_write_span(
".selection_left"))
422 if (bke::GSpanAttributeWriter selection = attributes.lookup_for_write_span(
".selection_right"))
430void remove_selected_points(Span<PointsRange> ranges_selected)
434 using RangesMap = Map<bke::greasepencil::Drawing *, Vector<IndexMask>>;
435 RangesMap ranges_by_drawing;
436 for (
const PointsRange &points_range : ranges_selected) {
437 BLI_assert(points_range.from_drawing !=
nullptr);
438 Vector<IndexMask> &ranges = ranges_by_drawing.lookup_or_add(points_range.from_drawing, {});
439 ranges.append(points_range.range);
442 for (
const RangesMap::Item &item : ranges_by_drawing.items()) {
443 bke::CurvesGeometry &dst_curves = item.key->strokes_for_write();
444 IndexMaskMemory memory;
446 dst_curves.remove_points(combined_mask, {});
447 item.key->tag_topology_changed();
451void append_strokes_from(bke::CurvesGeometry &&other, bke::CurvesGeometry &dst)
453 const int initial_points_num = dst.points_num();
454 const int initial_curves_num = dst.curves_num();
455 const int other_points_num = other.points_num();
456 const int other_curves_num = other.curves_num();
458 dst.resize(initial_points_num + other_points_num, initial_curves_num + other_curves_num);
460 Array<int> other_raw_offsets{0, other_points_num};
461 Array<int> dst_raw_offsets{initial_points_num, initial_points_num + other_points_num};
463 OffsetIndices<int> other_point_offsets{other_raw_offsets};
464 OffsetIndices<int> dst_point_offsets{dst_raw_offsets};
473 dst.attributes_for_write());
475 other_raw_offsets = {0, other_curves_num};
476 dst_raw_offsets = {initial_curves_num, initial_curves_num + other_curves_num};
478 OffsetIndices<int> other_curve_offsets{other_raw_offsets};
479 OffsetIndices<int> dst_curve_offsets{dst_raw_offsets};
488 dst.attributes_for_write());
501 using namespace bke::greasepencil;
506 scene->toolsettings,
object);
507 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
508 if (!grease_pencil.has_active_layer()) {
513 const ActiveLayerBehavior active_layer_behavior =
static_cast<ActiveLayerBehavior
>(
515 const Layer &active_layer = *grease_pencil.get_active_layer();
517 Drawing *dst_drawing = grease_pencil.get_editable_drawing_at(active_layer, scene->r.cfra);
518 if (dst_drawing ==
nullptr) {
522 IndexMaskMemory memory;
526 Vector<PointsRange> ranges_selected = retrieve_selection_ranges(
527 *
object, editable_drawings, active_layer_behavior, selected_points_count, memory);
528 if (ranges_selected.size() <= 1) {
536 tmp_drawing.strokes_for_write() = bke::CurvesGeometry(selected_points_count, 1);
537 bke::CurvesGeometry &tmp_curves = tmp_drawing.strokes_for_write();
539 const PointsRange working_range = copy_point_attributes(
540 ranges_selected, tmp_curves, tmp_drawing);
541 copy_curve_attributes(ranges_selected, tmp_curves, *dst_drawing);
543 clear_selection_attribute(ranges_selected, selection_domain);
545 Array<PointsRange> working_range_collection = {working_range};
546 clear_selection_attribute(working_range_collection, selection_domain);
548 bke::CurvesGeometry &dst_curves = dst_drawing->strokes_for_write();
549 if (
ELEM(active_layer_behavior,
550 ActiveLayerBehavior::SplitPoints,
551 ActiveLayerBehavior::JoinStrokes))
553 remove_selected_points(ranges_selected);
556 const int tmp_curves_num = tmp_curves.curves_num();
557 const int tmp_points_num = tmp_curves.points_num();
558 append_strokes_from(std::move(tmp_curves), dst_curves);
560 if (active_layer_behavior != ActiveLayerBehavior::JoinStrokes) {
573 dst_curves.update_curve_types();
574 dst_curves.tag_topology_changed();
575 dst_drawing->tag_topology_changed();
583void GREASE_PENCIL_OT_join_selection(wmOperatorType *ot)
585 static const EnumPropertyItem active_layer_behavior[] = {
586 {int(ActiveLayerBehavior::JoinStrokes),
590 "Join the selected strokes into one stroke"},
591 {int(ActiveLayerBehavior::SplitAndCopy),
595 "Copy the selected points to a new stroke"},
596 {int(ActiveLayerBehavior::SplitPoints),
600 "Split the selected point to a new stroke"},
601 {0,
nullptr, 0,
nullptr,
nullptr},
605 ot->name =
"Join Selection";
606 ot->idname =
"GREASE_PENCIL_OT_join_selection";
607 ot->description =
"New stroke from selected points/strokes";
611 ot->exec = grease_pencil_join_selection_exec;
619 active_layer_behavior,
620 int(ActiveLayerBehavior::JoinStrokes),
622 "Defines how the operator will behave on the selection in the active layer");
Low-level operations for grease pencil.
constexpr const T & first() const
constexpr int64_t size() const
constexpr const T * end() const
constexpr const T * begin() const
static IndexMask from_union(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
constexpr IndexRange()=default
void to_static_type(const CPPType &cpp_type, Fn &&fn)
auto attribute_filter_from_skip_ref(const Span< StringRef > skip)
void gather_attributes_to_groups(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, OffsetIndices< int > dst_offsets, const IndexMask &src_selection, MutableAttributeAccessor dst_attributes)
void copy_attributes_group_to_group(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
void fill_selection_false(GMutableSpan selection)
void fill_selection_true(GMutableSpan selection)
bke::GSpanAttributeWriter ensure_selection_attribute(bke::CurvesGeometry &curves, bke::AttrDomain selection_domain, bke::AttrType create_type, StringRef attribute_name)
IndexMask retrieve_editable_and_selected_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
bool editable_grease_pencil_poll(bContext *C)
IndexMask retrieve_editable_and_selected_points(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
Vector< MutableDrawingInfo > retrieve_editable_drawings(const Scene &scene, GreasePencil &grease_pencil)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
BMesh const char void * data
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bke::AttrDomain ED_grease_pencil_selection_domain_get(const ToolSettings *tool_settings, const Object *object)
void ED_operatortypes_grease_pencil_join()
Scene * CTX_data_scene(const bContext *C)
VecBase< float, 3 > float3
Object * CTX_data_active_object(const bContext *C)