Blender V5.3
curves_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_curves_utils.hh"
10#include "BKE_customdata.hh"
11
12#include "BLI_array_utils.hh"
13#include "BLI_bit_span_ops.hh"
14#include "BLI_bit_vector.hh"
15
16namespace blender::bke::curves {
17
19 const IndexMask &point_mask,
20 LinearAllocator<> &memory)
21{
22 BitVector<1024> selected(points_by_curve.total_size());
23 point_mask.to_bits(selected);
24
25 return IndexMask::from_predicate(points_by_curve.index_range(), memory, [&](const int curve_i) {
26 const IndexRange points = points_by_curve[curve_i];
27 const BitSpan curve_bits = bits::to_best_bit_span(selected).slice(points);
28 return bits::any_bit_set(curve_bits);
29 });
30}
31
33 const IndexMask &curve_selection,
34 LinearAllocator<> &memory)
35{
36 Array<index_mask::IndexMask::Initializer> point_ranges(curve_selection.size());
37 curve_selection.foreach_index(
38 [&](const int curve, const int pos) { point_ranges[pos] = points_by_curve[curve]; },
40 return IndexMask::from_initializers(point_ranges, memory);
41}
42
44 const CurveType curve_type,
45 LinearAllocator<> &memory)
46{
47 return curve_to_point_selection(curves.points_by_curve(),
48 indices_for_type(curves.curve_types(),
49 curves.curve_type_counts(),
50 curve_type,
51 curves.curves_range(),
52 memory),
53 memory);
54}
55
56void fill_points(const OffsetIndices<int> points_by_curve,
57 const IndexMask &curve_selection,
58 const GPointer value,
59 GMutableSpan dst)
60{
61 BLI_assert(*value.type() == dst.type());
62 const CPPType &type = dst.type();
63 curve_selection.foreach_index(
64 [&](const int i) {
65 const IndexRange points = points_by_curve[i];
66 type.fill_assign_n(value.get(), dst.slice(points).data(), points.size());
67 },
69}
70
72{
73 CurvesGeometry dst_curves(0, src_curves.curves_num());
74 copy_attributes(src_curves.attributes(),
77 {},
78 dst_curves.attributes_for_write());
79 dst_curves.runtime->type_counts = src_curves.runtime->type_counts;
80 return dst_curves;
81}
82
84 const std::array<int, CURVE_TYPES_NUM> &type_counts,
85 const CurveType type,
86 const IndexMask &selection,
87 LinearAllocator<> &memory)
88{
89 if (type_counts[type] == types.size()) {
90 return selection;
91 }
92 if (types.is_single()) {
93 return types.get_internal_single() == type ? IndexMask(types.size()) : IndexMask(0);
94 }
95 Span<int8_t> types_span = types.get_internal_span();
97 selection, memory, [&](const int index) { return types_span[index] == type; });
98}
99
101 const std::array<int, CURVE_TYPES_NUM> &counts,
102 const IndexMask &selection,
103 FunctionRef<void(IndexMask)> catmull_rom_fn,
104 FunctionRef<void(IndexMask)> poly_fn,
105 FunctionRef<void(IndexMask)> bezier_fn,
106 FunctionRef<void(IndexMask)> nurbs_fn)
107{
108 auto call_if_not_empty = [&](const CurveType type, FunctionRef<void(IndexMask)> fn) {
109 IndexMaskMemory memory;
110 const IndexMask mask = indices_for_type(types, counts, type, selection, memory);
111 if (!mask.is_empty()) {
112 fn(mask);
113 }
114 };
115 call_if_not_empty(CURVE_TYPE_CATMULL_ROM, catmull_rom_fn);
116 call_if_not_empty(CURVE_TYPE_POLY, poly_fn);
117 call_if_not_empty(CURVE_TYPE_BEZIER, bezier_fn);
118 call_if_not_empty(CURVE_TYPE_NURBS, nurbs_fn);
119}
120
121static void if_has_data_call_callback(const Span<int> offset_data,
122 const int begin,
123 const int end,
124 UnselectedCallback callback)
125{
126 if (begin < end) {
128 const IndexRange points = IndexRange::from_begin_end(offset_data[begin], offset_data[end]);
129 callback(curves, points);
130 }
131};
132
133template<typename Fn>
135 const OffsetIndices<int> points_by_curve,
136 const SelectedCallback selected_fn,
137 const Fn unselected_fn)
138{
139 Vector<IndexRange> ranges;
140 Span<int> offset_data = points_by_curve.data();
141
142 int curve_i = mask.is_empty() ? -1 : 0;
143
144 int range_first = mask.is_empty() ? 0 : mask.first();
145 int range_last = range_first - 1;
146
147 mask.foreach_index([&](const int64_t index) {
148 if (offset_data[curve_i + 1] <= index) {
149 int first_unselected_curve = curve_i;
150 if (range_last >= range_first) {
151 ranges.append(IndexRange::from_begin_end_inclusive(range_first, range_last));
152 selected_fn(curve_i, points_by_curve[curve_i], ranges);
153 ranges.clear();
154 first_unselected_curve++;
155 }
156 do {
157 ++curve_i;
158 } while (offset_data[curve_i + 1] <= index);
159 if constexpr (std::is_invocable_r_v<void, Fn, IndexRange, IndexRange>) {
160 if_has_data_call_callback(offset_data, first_unselected_curve, curve_i, unselected_fn);
161 }
162 range_first = index;
163 }
164 else if (range_last + 1 != index) {
165 ranges.append(IndexRange::from_begin_end_inclusive(range_first, range_last));
166 range_first = index;
167 }
168 range_last = index;
169 });
170
171 if (range_last - range_first >= 0) {
172 ranges.append(IndexRange::from_begin_end_inclusive(range_first, range_last));
173 selected_fn(curve_i, points_by_curve[curve_i], ranges);
174 }
175 if constexpr (std::is_invocable_r_v<void, Fn, IndexRange, IndexRange>) {
176 if_has_data_call_callback(offset_data, curve_i + 1, points_by_curve.size(), unselected_fn);
177 }
178}
179
181 const OffsetIndices<int> points_by_curve,
182 const SelectedCallback selected_fn)
183{
184 foreach_selected_point_ranges_per_curve_<void()>(mask, points_by_curve, selected_fn, nullptr);
185}
186
188 const OffsetIndices<int> points_by_curve,
189 const SelectedCallback selected_fn,
190 const UnselectedCallback unselected_fn)
191{
193 mask, points_by_curve, selected_fn, unselected_fn);
194}
195
196namespace bezier {
197
199 const IndexMask &curves_selection)
200{
201 if (curves.is_empty() || !curves.has_curve_with_type(CURVE_TYPE_BEZIER)) {
202 return {};
203 }
204 const OffsetIndices points_by_curve = curves.points_by_curve();
205 const Span<float3> positions = curves.positions();
206 const std::optional<Span<float3>> handle_positions_left = curves.handle_positions_left();
207 const std::optional<Span<float3>> handle_positions_right = curves.handle_positions_right();
208 if (!handle_positions_left || !handle_positions_right) {
209 return {};
210 }
211
212 Array<float3> all_positions(positions.size() * 3);
213 curves_selection.foreach_index(
214 [&](const int curve) {
215 const IndexRange points = points_by_curve[curve];
216 for (const int point : points) {
217 const int index = point * 3;
218 all_positions[index] = (*handle_positions_left)[point];
219 all_positions[index + 1] = positions[point];
220 all_positions[index + 2] = (*handle_positions_right)[point];
221 }
222 },
224
225 return all_positions;
226}
227
229 const IndexMask &curves_selection,
230 const Span<float3> all_positions)
231{
232 if (curves_selection.is_empty() || curves.is_empty() ||
233 !curves.has_curve_with_type(CURVE_TYPE_BEZIER))
234 {
235 return;
236 }
237
238 const OffsetIndices points_by_curve = curves.points_by_curve();
239 MutableSpan<float3> positions = curves.positions_for_write();
240 MutableSpan<float3> handle_positions_left = curves.handle_positions_left_for_write();
241 MutableSpan<float3> handle_positions_right = curves.handle_positions_right_for_write();
242
243 curves_selection.foreach_index(
244 [&](const int curve) {
245 const IndexRange points = points_by_curve[curve];
246 for (const int point : points) {
247 const int index = point * 3;
248 handle_positions_left[point] = all_positions[index];
249 positions[point] = all_positions[index + 1];
250 handle_positions_right[point] = all_positions[index + 2];
251 }
252 },
254}
255
256} // namespace bezier
257
258namespace nurbs {
259
261 const IndexMask &src_curves,
262 const int dst_curve_offset,
264{
265 const OffsetIndices<int> src_knots_by_curve = src.nurbs_custom_knots_by_curve();
266 const int start_offset = dst.nurbs_custom_knots_by_curve()[dst_curve_offset].start();
267 Array<int> dst_offsets(src_curves.size() + 1);
268
270 src_knots_by_curve, src_curves, start_offset, dst_offsets);
271
272 array_utils::gather_group_to_group(src_knots_by_curve,
273 dst_offsets.as_span(),
274 src_curves,
275 src.nurbs_custom_knots(),
277}
278
280 const KnotsMode mode_for_regular,
281 const KnotsMode mode_for_cyclic,
283{
284 const VArray<bool> cyclic = curves.cyclic();
285 MutableSpan<int8_t> knot_modes = curves.nurbs_knots_modes_for_write();
286 mask.foreach_index(
287 [&](const int64_t curve) {
288 int8_t &knot_mode = knot_modes[curve];
289 if (knot_mode == NURBS_KNOT_MODE_CUSTOM) {
290 knot_mode = cyclic[curve] ? mode_for_cyclic : mode_for_regular;
291 }
292 },
294 curves.nurbs_custom_knots_update_size();
295}
296
298 const IndexMask &exclude_curves,
299 bke::CurvesGeometry &dst_curves)
300{
301 BLI_assert(src_curves.curves_num() == dst_curves.curves_num());
302
303 if (src_curves.nurbs_has_custom_knots()) {
304 /* Ensure excluded curves don't have NURBS_KNOT_MODE_CUSTOM set. */
306 exclude_curves, NURBS_KNOT_MODE_NORMAL, NURBS_KNOT_MODE_NORMAL, dst_curves);
307 IndexMaskMemory memory;
309 src_curves,
311 src_curves.nurbs_custom_knot_curves(memory), exclude_curves, memory),
312 0,
313 dst_curves);
314 }
315}
316
317} // namespace nurbs
318} // namespace blender::bke::curves
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
Span< T > as_span() const
Definition BLI_array.hh:254
void fill_assign_n(const void *value, void *dst, int64_t n) const
GMutableSpan slice(const int64_t start, int64_t size) const
const CPPType & type() const
const CPPType * type() const
const void * get() const
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
static IndexMask from_difference(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
static IndexMask from_initializers(const Span< Initializer > initializers, LinearAllocator<> &memory)
constexpr int64_t size() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
static constexpr IndexRange from_begin_end_inclusive(const int64_t begin, const int64_t last)
constexpr int64_t size() const
Definition BLI_span.hh:256
Span< T > get_internal_span() const
void append(const T &value)
std::array< int, CURVE_TYPES_NUM > type_counts
Definition BKE_curves.hh:87
Span< float > nurbs_custom_knots() const
OffsetIndices< int > nurbs_custom_knots_by_curve() const
AttributeAccessor attributes() const
MutableSpan< float > nurbs_custom_knots_for_write()
IndexMask nurbs_custom_knot_curves(IndexMaskMemory &memory) const
MutableAttributeAccessor attributes_for_write()
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
void to_bits(MutableBitSpan r_bits, int64_t offset=0) const
void gather_group_to_group(const OffsetIndices< int > src_offsets, const OffsetIndices< int > dst_offsets, const IndexMask &selection, const Span< T > src, MutableSpan< T > dst)
Array< float3 > retrieve_all_positions(const bke::CurvesGeometry &curves, const IndexMask &curves_selection)
void write_all_positions(bke::CurvesGeometry &curves, const IndexMask &curves_selection, Span< float3 > all_positions)
void gather_custom_knots(const bke::CurvesGeometry &src, const IndexMask &src_curves, int dst_curve_offset, bke::CurvesGeometry &dst)
void update_custom_knot_modes(const IndexMask &mask, const KnotsMode mode_for_regular, const KnotsMode mode_for_cyclic, bke::CurvesGeometry &curves)
void copy_custom_knots(const bke::CurvesGeometry &src_curves, const IndexMask &exclude_curves, bke::CurvesGeometry &dst_curves)
IndexMask curve_type_point_selection(const bke::CurvesGeometry &curves, CurveType curve_type, LinearAllocator<> &memory)
void foreach_selected_point_ranges_per_curve(const IndexMask &mask, OffsetIndices< int > points_by_curve, SelectedCallback selected_fn)
bke::CurvesGeometry copy_only_curve_domain(const bke::CurvesGeometry &src_curves)
IndexMask indices_for_type(const VArray< int8_t > &types, const std::array< int, CURVE_TYPES_NUM > &type_counts, const CurveType type, const IndexMask &selection, LinearAllocator<> &memory)
FunctionRef< void( int curve_i, IndexRange curve_points, Span< IndexRange > selected_point_ranges)> SelectedCallback
static void foreach_selected_point_ranges_per_curve_(const IndexMask &mask, const OffsetIndices< int > points_by_curve, const SelectedCallback selected_fn, const Fn unselected_fn)
static void if_has_data_call_callback(const Span< int > offset_data, const int begin, const int end, UnselectedCallback callback)
FunctionRef< void(IndexRange curves, IndexRange unselected_points)> UnselectedCallback
IndexMask point_to_curve_selection(OffsetIndices< int > points_by_curve, const IndexMask &point_mask, LinearAllocator<> &memory)
void fill_points(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, GPointer value, GMutableSpan dst)
IndexMask curve_to_point_selection(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, LinearAllocator<> &memory)
void foreach_curve_by_type(const VArray< int8_t > &types, const std::array< int, CURVE_TYPES_NUM > &type_counts, const IndexMask &selection, FunctionRef< void(IndexMask)> catmull_rom_fn, FunctionRef< void(IndexMask)> poly_fn, FunctionRef< void(IndexMask)> bezier_fn, FunctionRef< void(IndexMask)> nurbs_fn)
void copy_attributes(const AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, MutableAttributeAccessor dst_attributes)
constexpr ParallelGrainSize grain_size(int grain_size)
OffsetIndices< int > gather_selected_offsets(OffsetIndices< int > src_offsets, const IndexMask &selection, int start_offset, MutableSpan< int > dst_offsets)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
@ NURBS_KNOT_MODE_NORMAL
@ NURBS_KNOT_MODE_CUSTOM
@ CURVE_TYPE_CATMULL_ROM
iter begin(iter)
signed char int8_t
Definition BKE_text.h:76
bke::CurvesGeometryRuntime * runtime