Blender V5.3
set_curve_type.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_attribute.hh"
10#include "BKE_attribute_math.hh"
11#include "BKE_curves.hh"
12#include "BKE_curves_utils.hh"
13#include "BKE_deform.hh"
14
15#include "BLI_array_utils.hh"
16#include "BLI_task.hh"
17
18#include "GEO_set_curve_type.hh"
19
20namespace blender::geometry {
21
28static bool is_nurbs_to_bezier_one_to_one(const KnotsMode knots_mode)
29{
31 return true;
32 }
33 return false;
34}
35
36template<typename T>
37static void scale_input_assign(const Span<T> src,
38 const int scale,
39 const int offset,
41{
42 for (const int i : dst.index_range()) {
43 dst[i] = src[i * scale + offset];
44 }
45}
46
47static void bezier_positions_to_nurbs(const Span<float3> src_positions,
48 const Span<float3> src_handles_l,
49 const Span<float3> src_handles_r,
50 MutableSpan<float3> dst_positions)
51{
52 for (const int i : src_positions.index_range()) {
53 dst_positions[i * 3] = src_handles_l[i];
54 dst_positions[i * 3 + 1] = src_positions[i];
55 dst_positions[i * 3 + 2] = src_handles_r[i];
56 }
57}
58
59static void catmull_rom_to_bezier_handles(const Span<float3> src_positions,
60 const bool cyclic,
61 MutableSpan<float3> dst_handles_l,
62 MutableSpan<float3> dst_handles_r)
63{
64 /* Catmull Rom curves are the same as Bezier curves with automatically defined handle positions.
65 * This constant defines the portion of the distance between the next/previous points to use for
66 * the length of the handles. */
67 constexpr float handle_scale = 1.0f / 6.0f;
68
69 if (src_positions.size() == 1) {
70 dst_handles_l.first() = src_positions.first();
71 dst_handles_r.first() = src_positions.first();
72 return;
73 }
74
75 const float3 first_offset = cyclic ? src_positions[1] - src_positions.last() :
76 src_positions[1] - src_positions[0];
77 dst_handles_r.first() = src_positions.first() + first_offset * handle_scale;
78 dst_handles_l.first() = src_positions.first() - first_offset * handle_scale;
79
80 const float3 last_offset = cyclic ? src_positions.first() - src_positions.last(1) :
81 src_positions.last() - src_positions.last(1);
82 dst_handles_l.last() = src_positions.last() - last_offset * handle_scale;
83 dst_handles_r.last() = src_positions.last() + last_offset * handle_scale;
84
85 for (const int i : src_positions.index_range().drop_front(1).drop_back(1)) {
86 const float3 left_offset = src_positions[i - 1] - src_positions[i + 1];
87 dst_handles_l[i] = src_positions[i] + left_offset * handle_scale;
88
89 const float3 right_offset = src_positions[i + 1] - src_positions[i - 1];
90 dst_handles_r[i] = src_positions[i] + right_offset * handle_scale;
91 }
92}
93
94static void catmull_rom_to_nurbs_positions(const Span<float3> src_positions,
95 const bool cyclic,
96 MutableSpan<float3> dst_positions)
97{
98 /* Convert the Catmull Rom position data to Bezier handles in order to reuse the Bezier to
99 * NURBS positions assignment. If this becomes a bottleneck, this step could be avoided. */
100 Array<float3, 32> bezier_handles_l(src_positions.size());
101 Array<float3, 32> bezier_handles_r(src_positions.size());
102 catmull_rom_to_bezier_handles(src_positions, cyclic, bezier_handles_l, bezier_handles_r);
103 bezier_positions_to_nurbs(src_positions, bezier_handles_l, bezier_handles_r, dst_positions);
104}
105
107 const KnotsMode knots_mode)
108{
109 const int nurbs_positions_num = nurbs_positions.size();
110 Vector<float3> handle_positions;
111
112 if (is_nurbs_to_bezier_one_to_one(knots_mode)) {
113 const bool is_periodic = knots_mode == NURBS_KNOT_MODE_NORMAL;
114 if (is_periodic) {
115 handle_positions.append(nurbs_positions[1] +
116 ((nurbs_positions[0] - nurbs_positions[1]) / 3));
117 }
118 else {
119 handle_positions.append(2 * nurbs_positions[0] - nurbs_positions[1]);
120 handle_positions.append(nurbs_positions[1]);
121 }
122
123 /* Place Bezier handles on interior NURBS hull segments. Those handles can be either placed on
124 * endpoints, midpoints or 1/3 of the distance of a hull segment. */
125 const int segments_num = nurbs_positions_num - 1;
126 const bool ignore_interior_segment = segments_num == 3 && is_periodic == false;
127 if (ignore_interior_segment == false) {
128 const float mid_offset = float(segments_num - 1) / 2.0f;
129 for (const int i : IndexRange(1, segments_num - 2)) {
130 /* Divisor can have values: 1, 2 or 3. */
131 const int divisor = is_periodic ?
132 3 :
133 std::min(3, int(-std::abs(i - mid_offset) + mid_offset + 1.0f));
134 const float3 &p1 = nurbs_positions[i];
135 const float3 &p2 = nurbs_positions[i + 1];
136 const float3 displacement = (p2 - p1) / divisor;
137 const int num_handles_on_segment = divisor < 3 ? 1 : 2;
138 for (int j : IndexRange(1, num_handles_on_segment)) {
139 handle_positions.append(p1 + (displacement * j));
140 }
141 }
142 }
143
144 const int last_index = nurbs_positions_num - 1;
145 if (is_periodic) {
146 handle_positions.append(
147 nurbs_positions[last_index - 1] +
148 ((nurbs_positions[last_index] - nurbs_positions[last_index - 1]) / 3));
149 }
150 else {
151 handle_positions.append(nurbs_positions[last_index - 1]);
152 handle_positions.append(2 * nurbs_positions[last_index] - nurbs_positions[last_index - 1]);
153 }
154 }
155 else {
156 for (const int i : IndexRange(nurbs_positions_num)) {
157 if (i % 3 == 1) {
158 continue;
159 }
160 handle_positions.append(nurbs_positions[i]);
161 }
162 if (nurbs_positions_num % 3 == 1) {
163 handle_positions.pop_last();
164 }
165 else if (nurbs_positions_num % 3 == 2) {
166 const int last_index = nurbs_positions_num - 1;
167 handle_positions.append(2 * nurbs_positions[last_index] - nurbs_positions[last_index - 1]);
168 }
169 }
170
171 return handle_positions;
172}
173
174static void create_nurbs_to_bezier_positions(const Span<float3> nurbs_positions,
175 const Span<float3> handle_positions,
176 const KnotsMode knots_mode,
177 MutableSpan<float3> bezier_positions)
178{
179 if (is_nurbs_to_bezier_one_to_one(knots_mode)) {
180 for (const int i : bezier_positions.index_range()) {
181 bezier_positions[i] = math::interpolate(
182 handle_positions[i * 2], handle_positions[i * 2 + 1], 0.5f);
183 }
184 }
185 else {
186 /* Every 3rd NURBS position (starting from index 1) should be converted to Bezier position. */
187 scale_input_assign(nurbs_positions, 3, 1, bezier_positions);
188 }
189}
190
191static int to_bezier_size(const CurveType src_type,
192 const bool cyclic,
193 const KnotsMode knots_mode,
194 const int src_size)
195{
196 switch (src_type) {
197 case CURVE_TYPE_NURBS: {
198 if (is_nurbs_to_bezier_one_to_one(knots_mode)) {
199 return cyclic ? src_size : std::max(1, src_size - 2);
200 }
201 return (src_size + 1) / 3;
202 }
203 default:
204 return src_size;
205 }
206}
207
208static int to_nurbs_size(const CurveType src_type, const int src_size)
209{
210 switch (src_type) {
213 return src_size * 3;
214 default:
215 return src_size;
216 }
217}
218
220 const IndexMask &selection,
221 const bke::AttributeFilter &attribute_filter)
222{
223 const OffsetIndices src_points_by_curve = src_curves.points_by_curve();
224 const VArray<int8_t> src_knot_modes = src_curves.nurbs_knots_modes();
225 const VArray<int8_t> src_types = src_curves.curve_types();
226 const VArray<bool> src_cyclic = src_curves.cyclic();
227 const Span<float3> src_positions = src_curves.positions();
228 const bke::AttributeAccessor src_attributes = src_curves.attributes();
229 IndexMaskMemory memory;
230 const IndexMask unselected = selection.complement(src_curves.curves_range(), memory);
231
233 /* Copy vertex groups from source curves to allow copying vertex group attributes. */
235 dst_curves.fill_curve_types(selection, CURVE_TYPE_BEZIER);
236
237 MutableSpan<int> dst_offsets = dst_curves.offsets_for_write();
238 offset_indices::copy_group_sizes(src_points_by_curve, unselected, dst_offsets);
239 selection.foreach_index(
240 [&](const int i) {
241 dst_offsets[i] = to_bezier_size(CurveType(src_types[i]),
242 src_cyclic[i],
243 KnotsMode(src_knot_modes[i]),
244 src_points_by_curve[i].size());
245 },
248 dst_curves.resize(dst_offsets.last(), dst_curves.curves_num());
249 const OffsetIndices dst_points_by_curve = dst_curves.points_by_curve();
250
251 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
252 MutableSpan<float3> dst_positions = dst_curves.positions_for_write();
253 MutableSpan<float3> dst_handles_l = dst_curves.handle_positions_left_for_write();
254 MutableSpan<float3> dst_handles_r = dst_curves.handle_positions_right_for_write();
255 MutableSpan<int8_t> dst_types_l = dst_curves.handle_types_left_for_write();
256 MutableSpan<int8_t> dst_types_r = dst_curves.handle_types_right_for_write();
258 src_attributes, dst_attributes, {bke::AttrDomain::Point}, attribute_filter);
259 Set<StringRef> attributes_to_skip = {
260 "position", "handle_type_left", "handle_type_right", "handle_right", "handle_left"};
261 if (!dst_curves.has_curve_with_type(CURVE_TYPE_NURBS)) {
262 attributes_to_skip.add_new("nurbs_weight");
263 }
264
265 auto catmull_rom_to_bezier = [&](const IndexMask &selection) {
267 dst_points_by_curve, selection, BEZIER_HANDLE_ALIGN, dst_types_l);
269 dst_points_by_curve, selection, BEZIER_HANDLE_ALIGN, dst_types_r);
271 src_points_by_curve, dst_points_by_curve, selection, src_positions, dst_positions);
272
273 selection.foreach_index(
274 [&](const int i) {
275 const IndexRange src_points = src_points_by_curve[i];
276 const IndexRange dst_points = dst_points_by_curve[i];
277 catmull_rom_to_bezier_handles(src_positions.slice(src_points),
278 src_cyclic[i],
279 dst_handles_l.slice(dst_points),
280 dst_handles_r.slice(dst_points));
281 },
283
284 for (bke::AttributeTransferData &attribute : generic_attributes) {
285 if (attributes_to_skip.contains(attribute.name)) {
286 continue;
287 }
289 src_points_by_curve, dst_points_by_curve, selection, attribute.src, attribute.dst.span);
290 }
291 };
292
293 auto poly_to_bezier = [&](const IndexMask &selection) {
295 src_points_by_curve, dst_points_by_curve, selection, src_positions, dst_positions);
297 dst_points_by_curve, selection, BEZIER_HANDLE_VECTOR, dst_types_l);
299 dst_points_by_curve, selection, BEZIER_HANDLE_VECTOR, dst_types_r);
301 for (bke::AttributeTransferData &attribute : generic_attributes) {
302 if (attributes_to_skip.contains(attribute.name)) {
303 continue;
304 }
306 src_points_by_curve, dst_points_by_curve, selection, attribute.src, attribute.dst.span);
307 }
308 };
309
310 auto bezier_to_bezier = [&](const IndexMask &selection) {
311 const VArraySpan<int8_t> src_types_l = src_curves.handle_types_left();
312 const VArraySpan<int8_t> src_types_r = src_curves.handle_types_right();
313 const Span<float3> src_handles_l = *src_curves.handle_positions_left();
314 const Span<float3> src_handles_r = *src_curves.handle_positions_right();
315
317 src_points_by_curve, dst_points_by_curve, selection, src_positions, dst_positions);
319 src_points_by_curve, dst_points_by_curve, selection, src_handles_l, dst_handles_l);
321 src_points_by_curve, dst_points_by_curve, selection, src_handles_r, dst_handles_r);
323 src_points_by_curve, dst_points_by_curve, selection, src_types_l, dst_types_l);
325 src_points_by_curve, dst_points_by_curve, selection, src_types_r, dst_types_r);
326
328
329 for (bke::AttributeTransferData &attribute : generic_attributes) {
330 if (attributes_to_skip.contains(attribute.name)) {
331 continue;
332 }
334 src_points_by_curve, dst_points_by_curve, selection, attribute.src, attribute.dst.span);
335 }
336 };
337
338 auto nurbs_to_bezier = [&](const IndexMask &selection) {
340 dst_points_by_curve, selection, BEZIER_HANDLE_ALIGN, dst_types_l);
342 dst_points_by_curve, selection, BEZIER_HANDLE_ALIGN, dst_types_r);
343
344 selection.foreach_index(
345 [&](const int i) {
346 const IndexRange src_points = src_points_by_curve[i];
347 const IndexRange dst_points = dst_points_by_curve[i];
348 const Span<float3> src_curve_positions = src_positions.slice(src_points);
349 if (dst_points.size() == 1) {
350 const float3 &position = src_positions[src_points.first()];
351 dst_positions[dst_points.first()] = position;
352 dst_handles_l[dst_points.first()] = position;
353 dst_handles_r[dst_points.first()] = position;
354 return;
355 }
356
357 KnotsMode knots_mode = KnotsMode(src_knot_modes[i]);
358 Span<float3> nurbs_positions = src_curve_positions;
359 Vector<float3> nurbs_positions_vector;
360 if (src_cyclic[i] && is_nurbs_to_bezier_one_to_one(knots_mode)) {
361 /* For conversion treat this as periodic closed curve. Extend NURBS hull to first and
362 * second point which will act as a skeleton for placing Bezier handles. */
363 nurbs_positions_vector.extend(src_curve_positions);
364 nurbs_positions_vector.append(src_curve_positions[0]);
365 nurbs_positions_vector.append(src_curve_positions[1]);
366 nurbs_positions = nurbs_positions_vector;
367 knots_mode = NURBS_KNOT_MODE_NORMAL;
368 }
369
370 const Vector<float3> handle_positions = create_nurbs_to_bezier_handles(nurbs_positions,
371 knots_mode);
372
373 scale_input_assign(handle_positions.as_span(), 2, 0, dst_handles_l.slice(dst_points));
374 scale_input_assign(handle_positions.as_span(), 2, 1, dst_handles_r.slice(dst_points));
375
377 nurbs_positions, handle_positions, knots_mode, dst_positions.slice(dst_points));
378 },
380
381 const IndexMask selection_points = bke::curves::curve_to_point_selection(
382 dst_points_by_curve, selection, memory);
383 Array<int> src_point_by_dst_point(selection_points.min_array_size());
384 selection.foreach_index(
385 [&](const int curve) {
386 const IndexRange src_points = src_points_by_curve[curve];
387 const IndexRange dst_points = dst_points_by_curve[curve];
388 MutableSpan<int> dst = src_point_by_dst_point.as_mutable_span().slice(dst_points);
389 switch (KnotsMode(src_knot_modes[curve])) {
391 for (const int i : dst.index_range()) {
392 dst[i] = src_points[(i + 1) % src_points.size()];
393 }
394 break;
396 for (const int i : dst.index_range().drop_back(1).drop_front(1)) {
397 dst[i] = src_points[i + 1];
398 }
399 dst.first() = src_points.first();
400 dst.last() = src_points.last();
401 break;
402 default:
403 /* Every 3rd NURBS position (starting from index 1) should have data transferred. */
404 for (const int i : dst.index_range()) {
405 dst[i] = src_points[i * 3 + 1];
406 }
407 break;
408 }
409 },
411
412 for (bke::AttributeTransferData &attribute : generic_attributes) {
413 if (attributes_to_skip.contains(attribute.name)) {
414 continue;
415 }
417 attribute.src, src_point_by_dst_point, selection_points, attribute.dst.span);
418 }
419 };
420
422 src_curves.curve_type_counts(),
423 selection,
424 catmull_rom_to_bezier,
425 poly_to_bezier,
426 bezier_to_bezier,
427 nurbs_to_bezier);
428
429 for (bke::AttributeTransferData &attribute : generic_attributes) {
431 src_points_by_curve, dst_points_by_curve, unselected, attribute.src, attribute.dst.span);
432
433 attribute.dst.finish();
434 }
435 bke::curves::nurbs::copy_custom_knots(src_curves, selection, dst_curves);
436 return dst_curves;
437}
438
440 const IndexMask &selection,
441 const bke::AttributeFilter &attribute_filter)
442{
443 const OffsetIndices src_points_by_curve = src_curves.points_by_curve();
444 const VArray<int8_t> src_types = src_curves.curve_types();
445 const VArray<bool> src_cyclic = src_curves.cyclic();
446 const Span<float3> src_positions = src_curves.positions();
447 const bke::AttributeAccessor src_attributes = src_curves.attributes();
448 IndexMaskMemory memory;
449 const IndexMask unselected = selection.complement(src_curves.curves_range(), memory);
450
452 /* Copy vertex groups from source curves to allow copying vertex group attributes. */
454 dst_curves.fill_curve_types(selection, CURVE_TYPE_NURBS);
455
456 MutableSpan<int> dst_offsets = dst_curves.offsets_for_write();
457 offset_indices::copy_group_sizes(src_points_by_curve, unselected, dst_offsets);
458 selection.foreach_index(
459 [&](const int i) {
460 dst_offsets[i] = to_nurbs_size(CurveType(src_types[i]), src_points_by_curve[i].size());
461 },
464 dst_curves.resize(dst_offsets.last(), dst_curves.curves_num());
465 const OffsetIndices dst_points_by_curve = dst_curves.points_by_curve();
466
467 MutableSpan<float3> dst_positions = dst_curves.positions_for_write();
468 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
470 src_attributes, dst_attributes, {bke::AttrDomain::Point}, attribute_filter);
471 const Set<StringRef> attributes_to_skip = {"position",
472 "handle_type_left",
473 "handle_type_right",
474 "handle_right",
475 "handle_left",
476 "nurbs_weight"};
477
478 auto fill_weights_if_necessary = [&](const IndexMask &selection) {
479 if (src_attributes.contains("nurbs_weight")) {
481 dst_points_by_curve, selection, 1.0f, dst_curves.nurbs_weights_for_write());
482 }
483 };
484
485 auto bezier_to_nurbs_copy_attributes = [&](const IndexMask &selection) {
486 const IndexMask selection_points = bke::curves::curve_to_point_selection(
487 dst_points_by_curve, selection, memory);
488 Array<int> src_point_by_dst_point(selection_points.min_array_size());
489 selection.foreach_index(
490 [&](const int curve) {
491 const IndexRange src_points = src_points_by_curve[curve];
492 const IndexRange dst_points = dst_points_by_curve[curve];
493 MutableSpan<int> dst = src_point_by_dst_point.as_mutable_span().slice(dst_points);
494 /* The Bezier control point and its handles become three control points on the NURBS
495 * curve, so each attribute value is duplicated three times. */
496 for (const int i : src_points.index_range()) {
497 dst[i * 3] = src_points[i];
498 dst[i * 3 + 1] = src_points[i];
499 dst[i * 3 + 2] = src_points[i];
500 }
501 },
503
504 for (bke::AttributeTransferData &attribute : generic_attributes) {
505 if (attributes_to_skip.contains(attribute.name)) {
506 continue;
507 }
509 attribute.src, src_point_by_dst_point, selection_points, attribute.dst.span);
510 }
511 };
512
513 auto catmull_rom_to_nurbs = [&](const IndexMask &selection) {
516 dst_curves.nurbs_knots_modes_for_write(), NURBS_KNOT_MODE_BEZIER, selection);
517 fill_weights_if_necessary(selection);
518
519 selection.foreach_segment(
520 [&](const IndexMaskSegment segment) {
521 for (const int i : segment) {
522 const IndexRange src_points = src_points_by_curve[i];
523 const IndexRange dst_points = dst_points_by_curve[i];
525 src_positions.slice(src_points), src_cyclic[i], dst_positions.slice(dst_points));
526 }
527 },
529
530 bezier_to_nurbs_copy_attributes(selection);
531 };
532
533 auto poly_to_nurbs = [&](const IndexMask &selection) {
536 src_points_by_curve, dst_points_by_curve, selection, src_positions, dst_positions);
537 fill_weights_if_necessary(selection);
538
539 /* Avoid using "Endpoint" knots modes for cyclic curves, since it adds a sharp point at the
540 * start/end. */
541 if (src_cyclic.is_single()) {
545 selection);
546 }
547 else {
548 VArraySpan<bool> cyclic{src_cyclic};
549 MutableSpan<int8_t> knots_modes = dst_curves.nurbs_knots_modes_for_write();
550 selection.foreach_index_optimized<int>(
551 [&](const int i) {
552 knots_modes[i] = cyclic[i] ? NURBS_KNOT_MODE_NORMAL : NURBS_KNOT_MODE_ENDPOINT;
553 },
555 }
556
557 for (bke::AttributeTransferData &attribute : generic_attributes) {
558 if (attributes_to_skip.contains(attribute.name)) {
559 continue;
560 }
562 src_points_by_curve, dst_points_by_curve, selection, attribute.src, attribute.dst.span);
563 }
564 };
565
566 auto bezier_to_nurbs = [&](const IndexMask &selection) {
567 const Span<float3> src_handles_l = *src_curves.handle_positions_left();
568 const Span<float3> src_handles_r = *src_curves.handle_positions_right();
569
572 dst_curves.nurbs_knots_modes_for_write(), NURBS_KNOT_MODE_BEZIER, selection);
573 fill_weights_if_necessary(selection);
574
575 selection.foreach_index(
576 [&](const int i) {
577 const IndexRange src_points = src_points_by_curve[i];
578 const IndexRange dst_points = dst_points_by_curve[i];
579 bezier_positions_to_nurbs(src_positions.slice(src_points),
580 src_handles_l.slice(src_points),
581 src_handles_r.slice(src_points),
582 dst_positions.slice(dst_points));
583 },
585
586 bezier_to_nurbs_copy_attributes(selection);
587 };
588
589 auto nurbs_to_nurbs = [&](const IndexMask &selection) {
591 src_points_by_curve, dst_points_by_curve, selection, src_positions, dst_positions);
592
593 if (const std::optional<Span<float>> nurbs_weights = src_curves.nurbs_weights()) {
594 array_utils::copy_group_to_group(src_points_by_curve,
595 dst_points_by_curve,
596 selection,
597 *nurbs_weights,
598 dst_curves.nurbs_weights_for_write());
599 }
600
601 for (bke::AttributeTransferData &attribute : generic_attributes) {
602 if (attributes_to_skip.contains(attribute.name)) {
603 continue;
604 }
606 src_points_by_curve, dst_points_by_curve, selection, attribute.src, attribute.dst.span);
607 }
608 };
609
611 src_curves.curve_type_counts(),
612 selection,
613 catmull_rom_to_nurbs,
614 poly_to_nurbs,
615 bezier_to_nurbs,
616 nurbs_to_nurbs);
617
618 for (bke::AttributeTransferData &attribute : generic_attributes) {
620 src_points_by_curve, dst_points_by_curve, unselected, attribute.src, attribute.dst.span);
621
622 attribute.dst.finish();
623 }
624 bke::curves::nurbs::copy_custom_knots(src_curves, IndexMask(), dst_curves);
625 return dst_curves;
626}
627
629 const IndexMask &selection,
630 const CurveType dst_type)
631{
632 bke::CurvesGeometry dst_curves(src_curves);
633 dst_curves.fill_curve_types(selection, dst_type);
635 return dst_curves;
636}
637
639 const bke::CurvesGeometry &src_curves,
640 const IndexMask &selection,
641 const CurveType dst_type,
642 const bke::AttributeFilter &attribute_filter,
644{
645 const bool use_bezier_handles = (dst_type == CURVE_TYPE_CATMULL_ROM) ?
646 options.convert_bezier_handles_to_catmull_rom_points :
647 options.convert_bezier_handles_to_poly_points;
648 if (!use_bezier_handles || !src_curves.has_curve_with_type(CURVE_TYPE_BEZIER)) {
649 return convert_curves_trivial(src_curves, selection, dst_type);
650 }
651
652 const OffsetIndices src_points_by_curve = src_curves.points_by_curve();
653 const VArray<int8_t> src_types = src_curves.curve_types();
654 const VArray<bool> src_cyclic = src_curves.cyclic();
655 const Span<float3> src_positions = src_curves.positions();
656 const bke::AttributeAccessor src_attributes = src_curves.attributes();
657 IndexMaskMemory memory;
658 const IndexMask unselected = selection.complement(src_curves.curves_range(), memory);
659
661 /* Copy vertex groups from source curves to allow copying vertex group attributes. */
663 dst_curves.fill_curve_types(selection, dst_type);
664
665 MutableSpan<int> dst_offsets = dst_curves.offsets_for_write();
666 offset_indices::copy_group_sizes(src_points_by_curve, unselected, dst_offsets);
667 selection.foreach_index(
668 [&](const int i) {
669 const IndexRange src_points = src_points_by_curve[i];
670 const CurveType src_curve_type = CurveType(src_types[i]);
671 int &size = dst_offsets[i];
672 if (src_curve_type == CURVE_TYPE_BEZIER) {
673 size = src_points.size() * 3;
674 }
675 else {
676 size = src_points.size();
677 }
678 },
681 dst_curves.resize(dst_offsets.last(), dst_curves.curves_num());
682 const OffsetIndices dst_points_by_curve = dst_curves.points_by_curve();
683
684 MutableSpan<float3> dst_positions = dst_curves.positions_for_write();
685 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
687 src_attributes, dst_attributes, {bke::AttrDomain::Point}, attribute_filter);
688 const Set<StringRef> attributes_to_skip = {"position",
689 "handle_type_left",
690 "handle_type_right",
691 "handle_right",
692 "handle_left",
693 "nurbs_weight"};
694
695 auto convert_from_catmull_rom_or_poly_or_nurbs = [&](const IndexMask &selection) {
697 src_points_by_curve, dst_points_by_curve, selection, src_positions, dst_positions);
698 for (bke::AttributeTransferData &attribute : generic_attributes) {
699 if (attributes_to_skip.contains(attribute.name)) {
700 continue;
701 }
703 src_points_by_curve, dst_points_by_curve, selection, attribute.src, attribute.dst.span);
704 }
705 };
706
707 auto convert_from_bezier = [&](const IndexMask &selection) {
708 const Span<float3> src_left_handles = *src_curves.handle_positions_left();
709 const Span<float3> src_right_handles = *src_curves.handle_positions_right();
710
711 /* Transfer positions. */
712 selection.foreach_index([&](const int curve_i) {
713 const IndexRange src_points = src_points_by_curve[curve_i];
714 const IndexRange dst_points = dst_points_by_curve[curve_i];
715 for (const int i : src_points.index_range()) {
716 const int src_point_i = src_points[i];
717 const int dst_points_start = dst_points.start() + 3 * i;
718 dst_positions[dst_points_start + 0] = src_left_handles[src_point_i];
719 dst_positions[dst_points_start + 1] = src_positions[src_point_i];
720 dst_positions[dst_points_start + 2] = src_right_handles[src_point_i];
721 }
722 });
723 /* Transfer attributes. The handles the same attribute values as their corresponding control
724 * point. */
725 for (bke::AttributeTransferData &attribute : generic_attributes) {
726 if (attributes_to_skip.contains(attribute.name)) {
727 continue;
728 }
729 const CPPType &cpp_type = attribute.src.type();
730 selection.foreach_index([&](const int curve_i) {
731 const IndexRange src_points = src_points_by_curve[curve_i];
732 const IndexRange dst_points = dst_points_by_curve[curve_i];
733 for (const int i : src_points.index_range()) {
734 const int src_point_i = src_points[i];
735 const int dst_points_start = dst_points.start() + 3 * i;
736 const void *src_value = attribute.src[src_point_i];
737 cpp_type.fill_assign_n(src_value, attribute.dst.span[dst_points_start], 3);
738 }
739 });
740 }
741 };
742
744 src_curves.curve_type_counts(),
745 selection,
746 convert_from_catmull_rom_or_poly_or_nurbs,
747 convert_from_catmull_rom_or_poly_or_nurbs,
748 convert_from_bezier,
749 convert_from_catmull_rom_or_poly_or_nurbs);
750
751 for (bke::AttributeTransferData &attribute : generic_attributes) {
753 src_points_by_curve, dst_points_by_curve, unselected, attribute.src, attribute.dst.span);
754
755 attribute.dst.finish();
756 }
757 bke::curves::nurbs::copy_custom_knots(src_curves, selection, dst_curves);
758 return dst_curves;
759}
760
766 const bke::CurvesGeometry &src_curves,
767 const IndexMask &selection,
769{
770 const VArray<int8_t> src_curve_types = src_curves.curve_types();
771 IndexMaskMemory memory;
772 const IndexMask mask = IndexMask::from_predicate(selection, memory, [&](const int curve_i) {
773 const CurveType type = CurveType(src_curve_types[curve_i]);
774 if (!options.keep_bezier_shape_as_nurbs && type == CURVE_TYPE_BEZIER) {
775 return true;
776 }
777 if (!options.keep_catmull_rom_shape_as_nurbs && type == CURVE_TYPE_CATMULL_ROM) {
778 return true;
779 }
780 return false;
781 });
782 return convert_curves_trivial(src_curves, mask, CURVE_TYPE_POLY);
783}
784
786 const IndexMask &selection,
787 const CurveType dst_type,
788 const bke::AttributeFilter &attribute_filter,
790{
791 switch (dst_type) {
793 case CURVE_TYPE_POLY:
795 src_curves, selection, dst_type, attribute_filter, options);
797 return convert_curves_to_bezier(src_curves, selection, attribute_filter);
798 case CURVE_TYPE_NURBS: {
799 if (!options.keep_bezier_shape_as_nurbs || !options.keep_catmull_rom_shape_as_nurbs) {
800 const bke::CurvesGeometry tmp_src_curves =
802 src_curves, selection, options);
803 return convert_curves_to_nurbs(tmp_src_curves, selection, attribute_filter);
804 }
805 return convert_curves_to_nurbs(src_curves, selection, attribute_filter);
806 }
807 }
809 return {};
810}
811
812} // namespace blender::geometry
Low-level operations for curves.
Low-level operations for curves.
support for deformation groups and hooks.
#define BLI_assert_unreachable()
#define ELEM(...)
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
void fill_assign_n(const void *value, void *dst, int64_t n) const
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 drop_back(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr int64_t start() const
constexpr IndexRange index_range() const
constexpr IndexRange drop_front(int64_t n) const
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr T & first() const
Definition BLI_span.hh:685
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void append(const T &value)
void extend(Span< T > array)
Span< T > as_span() const
bool contains(StringRef name) const
MutableSpan< int8_t > handle_types_left_for_write()
VArray< int8_t > curve_types() const
std::optional< Span< float > > nurbs_weights() const
MutableSpan< int8_t > nurbs_orders_for_write()
MutableSpan< float3 > positions_for_write()
MutableSpan< int8_t > handle_types_right_for_write()
std::optional< Span< float3 > > handle_positions_right() const
void resize(int points_num, int curves_num)
VArray< bool > cyclic() const
AttributeAccessor attributes() const
Span< float3 > positions() const
MutableSpan< float3 > handle_positions_right_for_write()
std::optional< Span< float3 > > handle_positions_left() const
IndexRange curves_range() const
VArray< int8_t > nurbs_knots_modes() const
MutableSpan< int8_t > nurbs_knots_modes_for_write()
VArray< int8_t > handle_types_left() const
MutableSpan< float3 > handle_positions_left_for_write()
bool has_curve_with_type(CurveType type) const
MutableSpan< int > offsets_for_write()
void fill_curve_types(CurveType type)
const std::array< int, CURVE_TYPES_NUM > & curve_type_counts() const
MutableSpan< float > nurbs_weights_for_write()
VArray< int8_t > handle_types_right() const
OffsetIndices< int > points_by_curve() const
MutableAttributeAccessor attributes_for_write()
void foreach_index_optimized(Fn &&fn, Mode mode=exec_mode::serial) const
void foreach_segment(Fn &&fn, Mode mode=exec_mode::serial) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
CCL_NAMESPACE_BEGIN struct Options options
#define position
#define attribute(slot)
float3x3 scale(float3x3 mat, float2 scale)
void copy_group_to_group(OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, GSpan src, GMutableSpan dst)
void gather(GSpan src, Span< int > map, GMutableSpan dst)
void copy_custom_knots(const bke::CurvesGeometry &src_curves, const IndexMask &exclude_curves, bke::CurvesGeometry &dst_curves)
bke::CurvesGeometry copy_only_curve_domain(const bke::CurvesGeometry &src_curves)
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)
Vector< AttributeTransferData > retrieve_attributes_for_transfer(const AttributeAccessor src_attributes, MutableAttributeAccessor dst_attributes, Span< AttrDomain > domains, const AttributeFilter &attribute_filter={})
constexpr ParallelGrainSize grain_size(int grain_size)
static bke::CurvesGeometry convert_bezier_or_catmull_rom_to_poly_before_conversion_to_nurbs(const bke::CurvesGeometry &src_curves, const IndexMask &selection, const ConvertCurvesOptions &options)
static bool is_nurbs_to_bezier_one_to_one(const KnotsMode knots_mode)
static void create_nurbs_to_bezier_positions(const Span< float3 > nurbs_positions, const Span< float3 > handle_positions, const KnotsMode knots_mode, MutableSpan< float3 > bezier_positions)
static void bezier_positions_to_nurbs(const Span< float3 > src_positions, const Span< float3 > src_handles_l, const Span< float3 > src_handles_r, MutableSpan< float3 > dst_positions)
static int to_nurbs_size(const CurveType src_type, const int src_size)
static void catmull_rom_to_bezier_handles(const Span< float3 > src_positions, const bool cyclic, MutableSpan< float3 > dst_handles_l, MutableSpan< float3 > dst_handles_r)
static bke::CurvesGeometry convert_curves_trivial(const bke::CurvesGeometry &src_curves, const IndexMask &selection, const CurveType dst_type)
static void catmull_rom_to_nurbs_positions(const Span< float3 > src_positions, const bool cyclic, MutableSpan< float3 > dst_positions)
static Vector< float3 > create_nurbs_to_bezier_handles(const Span< float3 > nurbs_positions, const KnotsMode knots_mode)
static void scale_input_assign(const Span< T > src, const int scale, const int offset, MutableSpan< T > dst)
static bke::CurvesGeometry convert_curves_to_nurbs(const bke::CurvesGeometry &src_curves, const IndexMask &selection, const bke::AttributeFilter &attribute_filter)
static bke::CurvesGeometry convert_curves_to_catmull_rom_or_poly(const bke::CurvesGeometry &src_curves, const IndexMask &selection, const CurveType dst_type, const bke::AttributeFilter &attribute_filter, const ConvertCurvesOptions &options)
static bke::CurvesGeometry convert_curves_to_bezier(const bke::CurvesGeometry &src_curves, const IndexMask &selection, const bke::AttributeFilter &attribute_filter)
static int to_bezier_size(const CurveType src_type, const bool cyclic, const KnotsMode knots_mode, const int src_size)
bke::CurvesGeometry convert_curves(const bke::CurvesGeometry &src_curves, const IndexMask &selection, CurveType dst_type, const bke::AttributeFilter &attribute_filter, const ConvertCurvesOptions &options={})
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
T interpolate(const T &a, const T &b, const FactorT &t)
void copy_group_sizes(OffsetIndices< int > offsets, const IndexMask &mask, MutableSpan< int > sizes)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
@ NURBS_KNOT_MODE_BEZIER
@ NURBS_KNOT_MODE_ENDPOINT
@ NURBS_KNOT_MODE_NORMAL
@ CURVE_TYPE_CATMULL_ROM
void BKE_defgroup_copy_list(ListBaseT< bDeformGroup > *outbase, const ListBaseT< bDeformGroup > *inbase)
Definition deform.cc:75
VecBase< float, 3 > float3
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< struct bDeformGroup > vertex_group_names