Blender V5.3
curves_remove_and_split.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array_utils.hh"
10
11#include "BKE_attribute.hh"
12#include "BKE_curves.hh"
13#include "BKE_curves_utils.hh"
14#include "BKE_deform.hh"
16
18
19namespace blender::geometry {
20
22 const IndexMask &mask)
23{
24 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
25 const VArray<bool> src_cyclic = curves.cyclic();
26
27 Array<bool> points_to_delete(curves.points_num());
28 mask.to_bools(points_to_delete.as_mutable_span());
29 const int total_points = points_to_delete.as_span().count(false);
30
31 /* Return if deleting everything. */
32 if (total_points == 0) {
33 return {};
34 }
35
36 int curr_dst_point_id = 0;
37 Array<int> dst_to_src_point(total_points);
38 Vector<int> dst_curve_counts;
39 Vector<int> dst_to_src_curve;
40 Vector<bool> dst_cyclic;
41
42 for (const int curve_i : curves.curves_range()) {
43 const IndexRange points = points_by_curve[curve_i];
44 const Span<bool> curve_points_to_delete = points_to_delete.as_span().slice(points);
45 const bool curve_cyclic = src_cyclic[curve_i];
46
47 /* Note, these ranges start at zero and needed to be shifted by `points.first()` */
48 const Vector<IndexRange> ranges_to_keep = array_utils::find_all_ranges(curve_points_to_delete,
49 false);
50
51 if (ranges_to_keep.is_empty()) {
52 continue;
53 }
54
55 const bool is_last_segment_selected = curve_cyclic && ranges_to_keep.first().first() == 0 &&
56 ranges_to_keep.last().last() == points.size() - 1;
57 const bool is_curve_self_joined = is_last_segment_selected && ranges_to_keep.size() != 1;
58 const bool is_cyclic = ranges_to_keep.size() == 1 && is_last_segment_selected;
59
60 IndexRange range_ids = ranges_to_keep.index_range();
61 /* Skip the first range because it is joined to the end of the last range. */
62 for (const int range_i : ranges_to_keep.index_range().drop_front(is_curve_self_joined)) {
63 const IndexRange range = ranges_to_keep[range_i];
64
65 int count = range.size();
66 for (const int src_point : range.shift(points.first())) {
67 dst_to_src_point[curr_dst_point_id++] = src_point;
68 }
69
70 /* Join the first range to the end of the last range. */
71 if (is_curve_self_joined && range_i == range_ids.last()) {
72 const IndexRange first_range = ranges_to_keep[range_ids.first()];
73 for (const int src_point : first_range.shift(points.first())) {
74 dst_to_src_point[curr_dst_point_id++] = src_point;
75 }
76 count += first_range.size();
77 }
78
79 dst_curve_counts.append(count);
80 dst_to_src_curve.append(curve_i);
81 dst_cyclic.append(is_cyclic);
82 }
83 }
84
85 const int total_curves = dst_to_src_curve.size();
86
87 bke::CurvesGeometry dst_curves(total_points, total_curves);
88
89 BKE_defgroup_copy_list(&dst_curves.vertex_group_names, &curves.vertex_group_names);
90
91 MutableSpan<int> new_curve_offsets = dst_curves.offsets_for_write();
92 array_utils::copy(dst_curve_counts.as_span(), new_curve_offsets.drop_back(1));
94
95 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
96 const bke::AttributeAccessor src_attributes = curves.attributes();
97
98 /* Transfer curve attributes. */
99 gather_attributes(src_attributes,
103 dst_to_src_curve,
104 dst_attributes);
105 array_utils::copy(dst_cyclic.as_span(), dst_curves.cyclic_for_write());
106
107 /* Transfer point attributes. */
108 gather_attributes(src_attributes,
111 {},
112 dst_to_src_point,
113 dst_attributes);
114
115 dst_curves.update_curve_types();
117
118 if (curves.nurbs_has_custom_knots()) {
121 }
122 return dst_curves;
123}
124
126 const IndexMask &mask)
127{
129
130 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
131
132 if (bke::SpanAttributeWriter<int> dst_fill_ids = dst_attributes.lookup_for_write_span<int>(
133 "fill_id"))
134 {
135 Array<bool> points_to_delete(curves.points_num());
136 mask.to_bools(points_to_delete.as_mutable_span());
137
138 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
139 const VArray<bool> src_cyclic = curves.cyclic();
140
141 Array<Vector<int>> src_to_dst_curve(curves.curves_num());
142 Vector<int> dst_to_src_curve;
143
144 for (const int curve_i : curves.curves_range()) {
145 const IndexRange points = points_by_curve[curve_i];
146 const Span<bool> curve_points_to_delete = points_to_delete.as_span().slice(points);
147 const bool curve_cyclic = src_cyclic[curve_i];
148
149 /* Note: These ranges start at zero and need to be shifted by `points.first()` */
151 curve_points_to_delete, false);
152
153 if (ranges_to_keep.is_empty()) {
154 continue;
155 }
156
157 const bool is_last_segment_selected = curve_cyclic && ranges_to_keep.first().first() == 0 &&
158 ranges_to_keep.last().last() == points.size() - 1;
159 const bool is_curve_self_joined = is_last_segment_selected && ranges_to_keep.size() != 1;
160
161 /* Skip the first range because it is joined to the end of the last range. */
162 const int num_dst_curves = ranges_to_keep.size() - int(is_curve_self_joined);
163
164 src_to_dst_curve[curve_i].resize(num_dst_curves);
165 array_utils::fill_index_range<int>(src_to_dst_curve[curve_i].as_mutable_span(),
166 dst_to_src_curve.size());
167
168 dst_to_src_curve.append_n_times(curve_i, num_dst_curves);
169 }
170
171 IndexMaskMemory memory;
172 /* Get all the curves that were split off of the original geometry. */
173 const IndexMask non_original_curves = IndexMask::from_predicate(
174 dst_to_src_curve.index_range(), memory, [&](const int64_t dst_curve_index) {
175 /* Skip non-filled curves. */
176 if (dst_fill_ids.span[dst_curve_index] == 0) {
177 return false;
178 }
179 const int src_curve_index = dst_to_src_curve[dst_curve_index];
180 return src_to_dst_curve[src_curve_index].first() != dst_curve_index;
181 });
183 dst_fill_ids.span.varray(), non_original_curves, dst_fill_ids.span);
184
185 dst_fill_ids.finish();
186 }
187
188 return dst_curves;
189}
190
191} // namespace blender::geometry
Low-level operations for curves.
Low-level operations for curves.
support for deformation groups and hooks.
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T & first() const
Definition BLI_array.hh:292
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
constexpr int64_t first() const
constexpr IndexRange shift(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr IndexRange drop_front(int64_t n) const
constexpr MutableSpan drop_back(const int64_t n) const
Definition BLI_span.hh:624
int64_t size() const
void append(const T &value)
const T & last(const int64_t n=0) const
bool is_empty() const
IndexRange index_range() const
Span< T > as_span() const
const T & first() const
void append_n_times(const T &value, const int64_t n)
IndexRange curves_range() const
MutableSpan< int > offsets_for_write()
MutableSpan< bool > cyclic_for_write()
MutableAttributeAccessor attributes_for_write()
GSpanAttributeWriter lookup_for_write_span(StringRef name)
Vector< IndexRange > find_all_ranges(const Span< T > span, const T &value)
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void fill_index_range(MutableSpan< T > span, const T start=0)
void update_custom_knot_modes(const IndexMask &mask, const KnotsMode mode_for_regular, const KnotsMode mode_for_cyclic, bke::CurvesGeometry &curves)
void gather_next_available_fill_ids(const VArray< int > &fill_ids, MutableSpan< int > r_new_fill_ids)
auto attribute_filter_from_skip_ref(const Span< StringRef > skip)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
bke::CurvesGeometry remove_points_and_split(const bke::CurvesGeometry &curves, const IndexMask &mask)
bke::CurvesGeometry grease_pencil_remove_points_and_split(const bke::CurvesGeometry &curves, const IndexMask &mask)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
__int64 int64_t
Definition BKE_text.h:90
@ NURBS_KNOT_MODE_NORMAL
void BKE_defgroup_copy_list(ListBaseT< bDeformGroup > *outbase, const ListBaseT< bDeformGroup > *inbase)
Definition deform.cc:75
ListBaseT< struct bDeformGroup > vertex_group_names