Welcome to mirror list, hosted at ThFree Co, Russian Federation.

set_curve_type.cc « intern « geometry « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e069732ca9b425e6cb9c8f20b6e36ae5a374f95f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "BKE_attribute.hh"
#include "BKE_attribute_math.hh"
#include "BKE_curves.hh"
#include "BKE_curves_utils.hh"

#include "BLI_task.hh"

#include "GEO_set_curve_type.hh"

namespace blender::geometry {

/**
 * This function answers the question about possible conversion method for NURBS-to-Bezier. In
 * general for 3rd degree NURBS curves there is one-to-one relation with 3rd degree Bezier curves
 * that can be exploit for conversion - Bezier handles sit on NURBS hull segments and in the middle
 * between those handles are Bezier anchor points.
 */
static bool is_nurbs_to_bezier_one_to_one(const KnotsMode knots_mode)
{
  if (ELEM(knots_mode, NURBS_KNOT_MODE_NORMAL, NURBS_KNOT_MODE_ENDPOINT)) {
    return true;
  }
  return false;
}

/**
 * As an optimization, just change the types on a mutable curves data-block when the conversion is
 * simple. This could be expanded to more cases where the number of points doesn't change in the
 * future, though that might require properly initializing some attributes, or removing others.
 */
static bool conversion_can_change_point_num(const CurveType dst_type)
{
  if (ELEM(dst_type, CURVE_TYPE_CATMULL_ROM, CURVE_TYPE_POLY)) {
    /* The conversion to Catmull Rom or Poly should never change the number of points, no matter
     * the source type (Bezier to Catmull Rom conversion cannot maintain the same shape anyway). */
    return false;
  }
  return true;
}

template<typename T>
static void scale_input_assign(const Span<T> src,
                               const int scale,
                               const int offset,
                               MutableSpan<T> dst)
{
  for (const int i : dst.index_range()) {
    dst[i] = src[i * scale + offset];
  }
}

/**
 * The Bezier control point and its handles become three control points on the NURBS curve,
 * so each attribute value is duplicated three times.
 */
template<typename T> static void bezier_generic_to_nurbs(const Span<T> src, MutableSpan<T> dst)
{
  for (const int i : src.index_range()) {
    dst[i * 3] = src[i];
    dst[i * 3 + 1] = src[i];
    dst[i * 3 + 2] = src[i];
  }
}

static void bezier_generic_to_nurbs(const GSpan src, GMutableSpan dst)
{
  attribute_math::convert_to_static_type(src.type(), [&](auto dummy) {
    using T = decltype(dummy);
    bezier_generic_to_nurbs(src.typed<T>(), dst.typed<T>());
  });
}

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)
{
  for (const int i : src_positions.index_range()) {
    dst_positions[i * 3] = src_handles_l[i];
    dst_positions[i * 3 + 1] = src_positions[i];
    dst_positions[i * 3 + 2] = src_handles_r[i];
  }
}

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)
{
  /* Catmull Rom curves are the same as Bezier curves with automatically defined handle positions.
   * This constant defines the portion of the distance between the next/previous points to use for
   * the length of the handles. */
  constexpr float handle_scale = 1.0f / 6.0f;

  if (src_positions.size() == 1) {
    dst_handles_l.first() = src_positions.first();
    dst_handles_r.first() = src_positions.first();
    return;
  }

  const float3 first_offset = cyclic ? src_positions[1] - src_positions.last() :
                                       src_positions[1] - src_positions[0];
  dst_handles_r.first() = src_positions.first() + first_offset * handle_scale;
  dst_handles_l.first() = src_positions.first() - first_offset * handle_scale;

  const float3 last_offset = cyclic ? src_positions.first() - src_positions.last(1) :
                                      src_positions.last() - src_positions.last(1);
  dst_handles_l.last() = src_positions.last() - last_offset * handle_scale;
  dst_handles_r.last() = src_positions.last() + last_offset * handle_scale;

  for (const int i : src_positions.index_range().drop_front(1).drop_back(1)) {
    const float3 left_offset = src_positions[i - 1] - src_positions[i + 1];
    dst_handles_l[i] = src_positions[i] + left_offset * handle_scale;

    const float3 right_offset = src_positions[i + 1] - src_positions[i - 1];
    dst_handles_r[i] = src_positions[i] + right_offset * handle_scale;
  }
}

static void catmull_rom_to_nurbs_positions(const Span<float3> src_positions,
                                           const bool cyclic,
                                           MutableSpan<float3> dst_positions)
{
  /* Convert the Catmull Rom position data to Bezier handles in order to reuse the Bezier to
   * NURBS positions assignment. If this becomes a bottleneck, this step could be avoided. */
  Array<float3, 32> bezier_handles_l(src_positions.size());
  Array<float3, 32> bezier_handles_r(src_positions.size());
  catmull_rom_to_bezier_handles(src_positions, cyclic, bezier_handles_l, bezier_handles_r);
  bezier_positions_to_nurbs(src_positions, bezier_handles_l, bezier_handles_r, dst_positions);
}

template<typename T>
static void nurbs_to_bezier_assign(const Span<T> src,
                                   const MutableSpan<T> dst,
                                   const KnotsMode knots_mode)
{
  switch (knots_mode) {
    case NURBS_KNOT_MODE_NORMAL:
      for (const int i : dst.index_range()) {
        dst[i] = src[(i + 1) % src.size()];
      }
      break;
    case NURBS_KNOT_MODE_ENDPOINT:
      for (const int i : dst.index_range().drop_back(1).drop_front(1)) {
        dst[i] = src[i + 1];
      }
      dst.first() = src.first();
      dst.last() = src.last();
      break;
    default:
      /* Every 3rd NURBS position (starting from index 1) should have its attributes transferred.
       */
      scale_input_assign<T>(src, 3, 1, dst);
  }
}

static void nurbs_to_bezier_assign(const GSpan src, const KnotsMode knots_mode, GMutableSpan dst)
{
  attribute_math::convert_to_static_type(src.type(), [&](auto dummy) {
    using T = decltype(dummy);
    nurbs_to_bezier_assign(src.typed<T>(), dst.typed<T>(), knots_mode);
  });
}

static Vector<float3> create_nurbs_to_bezier_handles(const Span<float3> nurbs_positions,
                                                     const KnotsMode knots_mode)
{
  const int nurbs_positions_num = nurbs_positions.size();
  Vector<float3> handle_positions;

  if (is_nurbs_to_bezier_one_to_one(knots_mode)) {
    const bool is_periodic = knots_mode == NURBS_KNOT_MODE_NORMAL;
    if (is_periodic) {
      handle_positions.append(nurbs_positions[1] +
                              ((nurbs_positions[0] - nurbs_positions[1]) / 3));
    }
    else {
      handle_positions.append(2 * nurbs_positions[0] - nurbs_positions[1]);
      handle_positions.append(nurbs_positions[1]);
    }

    /* Place Bezier handles on interior NURBS hull segments. Those handles can be either placed on
     * endpoints, midpoints or 1/3 of the distance of a hull segment. */
    const int segments_num = nurbs_positions_num - 1;
    const bool ignore_interior_segment = segments_num == 3 && is_periodic == false;
    if (ignore_interior_segment == false) {
      const float mid_offset = float(segments_num - 1) / 2.0f;
      for (const int i : IndexRange(1, segments_num - 2)) {
        /* Divisor can have values: 1, 2 or 3. */
        const int divisor = is_periodic ?
                                3 :
                                std::min(3, int(-std::abs(i - mid_offset) + mid_offset + 1.0f));
        const float3 &p1 = nurbs_positions[i];
        const float3 &p2 = nurbs_positions[i + 1];
        const float3 displacement = (p2 - p1) / divisor;
        const int num_handles_on_segment = divisor < 3 ? 1 : 2;
        for (int j : IndexRange(1, num_handles_on_segment)) {
          handle_positions.append(p1 + (displacement * j));
        }
      }
    }

    const int last_index = nurbs_positions_num - 1;
    if (is_periodic) {
      handle_positions.append(
          nurbs_positions[last_index - 1] +
          ((nurbs_positions[last_index] - nurbs_positions[last_index - 1]) / 3));
    }
    else {
      handle_positions.append(nurbs_positions[last_index - 1]);
      handle_positions.append(2 * nurbs_positions[last_index] - nurbs_positions[last_index - 1]);
    }
  }
  else {
    for (const int i : IndexRange(nurbs_positions_num)) {
      if (i % 3 == 1) {
        continue;
      }
      handle_positions.append(nurbs_positions[i]);
    }
    if (nurbs_positions_num % 3 == 1) {
      handle_positions.pop_last();
    }
    else if (nurbs_positions_num % 3 == 2) {
      const int last_index = nurbs_positions_num - 1;
      handle_positions.append(2 * nurbs_positions[last_index] - nurbs_positions[last_index - 1]);
    }
  }

  return handle_positions;
}

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)
{
  if (is_nurbs_to_bezier_one_to_one(knots_mode)) {
    for (const int i : bezier_positions.index_range()) {
      bezier_positions[i] = math::interpolate(
          handle_positions[i * 2], handle_positions[i * 2 + 1], 0.5f);
    }
  }
  else {
    /* Every 3rd NURBS position (starting from index 1) should be converted to Bezier position. */
    scale_input_assign(nurbs_positions, 3, 1, bezier_positions);
  }
}

static int to_bezier_size(const CurveType src_type,
                          const bool cyclic,
                          const KnotsMode knots_mode,
                          const int src_size)
{
  switch (src_type) {
    case CURVE_TYPE_NURBS: {
      if (is_nurbs_to_bezier_one_to_one(knots_mode)) {
        return cyclic ? src_size : std::max(1, src_size - 2);
      }
      return (src_size + 1) / 3;
    }
    default:
      return src_size;
  }
}

static int to_nurbs_size(const CurveType src_type, const int src_size)
{
  switch (src_type) {
    case CURVE_TYPE_BEZIER:
    case CURVE_TYPE_CATMULL_ROM:
      return src_size * 3;
    default:
      return src_size;
  }
}

static void retrieve_curve_sizes(const bke::CurvesGeometry &curves, MutableSpan<int> sizes)
{
  threading::parallel_for(curves.curves_range(), 4096, [&](IndexRange range) {
    for (const int i : range) {
      sizes[i] = curves.points_for_curve(i).size();
    }
  });
}

static bke::CurvesGeometry convert_curves_to_bezier(const bke::CurvesGeometry &src_curves,
                                                    const IndexMask selection)
{
  const VArray<int8_t> src_knot_modes = src_curves.nurbs_knots_modes();
  const VArray<int8_t> src_types = src_curves.curve_types();
  const VArray<bool> src_cyclic = src_curves.cyclic();
  const Span<float3> src_positions = src_curves.positions();

  bke::CurvesGeometry dst_curves = bke::curves::copy_only_curve_domain(src_curves);
  dst_curves.fill_curve_types(selection, CURVE_TYPE_BEZIER);

  MutableSpan<int> dst_offsets = dst_curves.offsets_for_write();
  retrieve_curve_sizes(src_curves, dst_curves.offsets_for_write());
  threading::parallel_for(selection.index_range(), 1024, [&](IndexRange range) {
    for (const int i : selection.slice(range)) {
      dst_offsets[i] = to_bezier_size(
          CurveType(src_types[i]), src_cyclic[i], KnotsMode(src_knot_modes[i]), dst_offsets[i]);
    }
  });
  bke::curves::accumulate_counts_to_offsets(dst_offsets);
  dst_curves.resize(dst_offsets.last(), dst_curves.curves_num());

  const bke::AttributeAccessor src_attributes = src_curves.attributes();
  bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();

  Vector<bke::AttributeTransferData> generic_attributes = bke::retrieve_attributes_for_transfer(
      src_attributes,
      dst_attributes,
      ATTR_DOMAIN_MASK_POINT,
      {"position",
       "handle_type_left",
       "handle_type_right",
       "handle_right",
       "handle_left",
       "nurbs_weight"});

  MutableSpan<float3> dst_positions = dst_curves.positions_for_write();
  MutableSpan<float3> dst_handles_l = dst_curves.handle_positions_left_for_write();
  MutableSpan<float3> dst_handles_r = dst_curves.handle_positions_right_for_write();
  MutableSpan<int8_t> dst_types_l = dst_curves.handle_types_left_for_write();
  MutableSpan<int8_t> dst_types_r = dst_curves.handle_types_right_for_write();
  MutableSpan<float> dst_weights = dst_curves.nurbs_weights_for_write();

  auto catmull_rom_to_bezier = [&](IndexMask selection) {
    bke::curves::fill_points<int8_t>(dst_curves, selection, BEZIER_HANDLE_ALIGN, dst_types_l);
    bke::curves::fill_points<int8_t>(dst_curves, selection, BEZIER_HANDLE_ALIGN, dst_types_r);
    bke::curves::copy_point_data(src_curves, dst_curves, selection, src_positions, dst_positions);

    threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {
      for (const int i : selection.slice(range)) {
        const IndexRange src_points = src_curves.points_for_curve(i);
        const IndexRange dst_points = dst_curves.points_for_curve(i);
        catmull_rom_to_bezier_handles(src_positions.slice(src_points),
                                      src_cyclic[i],
                                      dst_handles_l.slice(dst_points),
                                      dst_handles_r.slice(dst_points));
      }
    });

    for (bke::AttributeTransferData &attribute : generic_attributes) {
      bke::curves::copy_point_data(
          src_curves, dst_curves, selection, attribute.src, attribute.dst.span);
    }
  };

  auto poly_to_bezier = [&](IndexMask selection) {
    bke::curves::copy_point_data(src_curves, dst_curves, selection, src_positions, dst_positions);
    bke::curves::fill_points<int8_t>(dst_curves, selection, BEZIER_HANDLE_VECTOR, dst_types_l);
    bke::curves::fill_points<int8_t>(dst_curves, selection, BEZIER_HANDLE_VECTOR, dst_types_r);
    dst_curves.calculate_bezier_auto_handles();
    for (bke::AttributeTransferData &attribute : generic_attributes) {
      bke::curves::copy_point_data(
          src_curves, dst_curves, selection, attribute.src, attribute.dst.span);
    }
  };

  auto bezier_to_bezier = [&](IndexMask selection) {
    const VArraySpan<int8_t> src_types_l = src_curves.handle_types_left();
    const VArraySpan<int8_t> src_types_r = src_curves.handle_types_right();
    const Span<float3> src_handles_l = src_curves.handle_positions_left();
    const Span<float3> src_handles_r = src_curves.handle_positions_right();

    bke::curves::copy_point_data(src_curves, dst_curves, selection, src_positions, dst_positions);
    bke::curves::copy_point_data(src_curves, dst_curves, selection, src_handles_l, dst_handles_l);
    bke::curves::copy_point_data(src_curves, dst_curves, selection, src_handles_r, dst_handles_r);
    bke::curves::copy_point_data(src_curves, dst_curves, selection, src_types_l, dst_types_l);
    bke::curves::copy_point_data(src_curves, dst_curves, selection, src_types_r, dst_types_r);

    dst_curves.calculate_bezier_auto_handles();

    for (bke::AttributeTransferData &attribute : generic_attributes) {
      bke::curves::copy_point_data(
          src_curves, dst_curves, selection, attribute.src, attribute.dst.span);
    }
  };

  auto nurbs_to_bezier = [&](IndexMask selection) {
    bke::curves::fill_points<int8_t>(dst_curves, selection, BEZIER_HANDLE_ALIGN, dst_types_l);
    bke::curves::fill_points<int8_t>(dst_curves, selection, BEZIER_HANDLE_ALIGN, dst_types_r);
    bke::curves::fill_points<float>(dst_curves, selection, 0.0f, dst_weights);

    threading::parallel_for(selection.index_range(), 64, [&](IndexRange range) {
      for (const int i : selection.slice(range)) {
        const IndexRange src_points = src_curves.points_for_curve(i);
        const IndexRange dst_points = dst_curves.points_for_curve(i);
        const Span<float3> src_curve_positions = src_positions.slice(src_points);
        if (dst_points.size() == 1) {
          const float3 &position = src_positions[src_points.first()];
          dst_positions[dst_points.first()] = position;
          dst_handles_l[dst_points.first()] = position;
          dst_handles_r[dst_points.first()] = position;
          continue;
        }

        KnotsMode knots_mode = KnotsMode(src_knot_modes[i]);
        Span<float3> nurbs_positions = src_curve_positions;
        Vector<float3> nurbs_positions_vector;
        if (src_cyclic[i] && is_nurbs_to_bezier_one_to_one(knots_mode)) {
          /* For conversion treat this as periodic closed curve. Extend NURBS hull to first and
           * second point which will act as a skeleton for placing Bezier handles. */
          nurbs_positions_vector.extend(src_curve_positions);
          nurbs_positions_vector.append(src_curve_positions[0]);
          nurbs_positions_vector.append(src_curve_positions[1]);
          nurbs_positions = nurbs_positions_vector;
          knots_mode = NURBS_KNOT_MODE_NORMAL;
        }

        const Vector<float3> handle_positions = create_nurbs_to_bezier_handles(nurbs_positions,
                                                                               knots_mode);

        scale_input_assign(handle_positions.as_span(), 2, 0, dst_handles_l.slice(dst_points));
        scale_input_assign(handle_positions.as_span(), 2, 1, dst_handles_r.slice(dst_points));

        create_nurbs_to_bezier_positions(
            nurbs_positions, handle_positions, knots_mode, dst_positions.slice(dst_points));
      }
    });

    for (bke::AttributeTransferData &attribute : generic_attributes) {
      threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {
        for (const int i : selection.slice(range)) {
          const IndexRange src_points = src_curves.points_for_curve(i);
          const IndexRange dst_points = dst_curves.points_for_curve(i);
          nurbs_to_bezier_assign(attribute.src.slice(src_points),
                                 KnotsMode(src_knot_modes[i]),
                                 attribute.dst.span.slice(dst_points));
        }
      });
    }
  };

  bke::curves::foreach_curve_by_type(src_curves.curve_types(),
                                     src_curves.curve_type_counts(),
                                     selection,
                                     catmull_rom_to_bezier,
                                     poly_to_bezier,
                                     bezier_to_bezier,
                                     nurbs_to_bezier);

  const Vector<IndexRange> unselected_ranges = selection.extract_ranges_invert(
      src_curves.curves_range());

  for (bke::AttributeTransferData &attribute : generic_attributes) {
    bke::curves::copy_point_data(
        src_curves, dst_curves, unselected_ranges, attribute.src, attribute.dst.span);
  }

  for (bke::AttributeTransferData &attribute : generic_attributes) {
    attribute.dst.finish();
  }

  return dst_curves;
}

static bke::CurvesGeometry convert_curves_to_nurbs(const bke::CurvesGeometry &src_curves,
                                                   const IndexMask selection)
{
  const VArray<int8_t> src_types = src_curves.curve_types();
  const VArray<bool> src_cyclic = src_curves.cyclic();
  const Span<float3> src_positions = src_curves.positions();

  bke::CurvesGeometry dst_curves = bke::curves::copy_only_curve_domain(src_curves);
  dst_curves.fill_curve_types(selection, CURVE_TYPE_NURBS);

  MutableSpan<int> dst_offsets = dst_curves.offsets_for_write();
  retrieve_curve_sizes(src_curves, dst_curves.offsets_for_write());
  threading::parallel_for(selection.index_range(), 1024, [&](IndexRange range) {
    for (const int i : selection.slice(range)) {
      dst_offsets[i] = to_nurbs_size(CurveType(src_types[i]), dst_offsets[i]);
    }
  });
  bke::curves::accumulate_counts_to_offsets(dst_offsets);
  dst_curves.resize(dst_offsets.last(), dst_curves.curves_num());

  const bke::AttributeAccessor src_attributes = src_curves.attributes();
  bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();

  Vector<bke::AttributeTransferData> generic_attributes = bke::retrieve_attributes_for_transfer(
      src_attributes,
      dst_attributes,
      ATTR_DOMAIN_MASK_POINT,
      {"position",
       "handle_type_left",
       "handle_type_right",
       "handle_right",
       "handle_left",
       "nurbs_weight"});

  MutableSpan<float3> dst_positions = dst_curves.positions_for_write();

  auto fill_weights_if_necessary = [&](const IndexMask selection) {
    if (!src_curves.nurbs_weights().is_empty()) {
      bke::curves::fill_points(dst_curves, selection, 1.0f, dst_curves.nurbs_weights_for_write());
    }
  };

  auto catmull_rom_to_nurbs = [&](IndexMask selection) {
    dst_curves.nurbs_orders_for_write().fill_indices(selection, 4);
    dst_curves.nurbs_knots_modes_for_write().fill_indices(selection, NURBS_KNOT_MODE_BEZIER);
    fill_weights_if_necessary(selection);

    threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {
      for (const int i : selection.slice(range)) {
        const IndexRange src_points = src_curves.points_for_curve(i);
        const IndexRange dst_points = dst_curves.points_for_curve(i);
        catmull_rom_to_nurbs_positions(
            src_positions.slice(src_points), src_cyclic[i], dst_positions.slice(dst_points));
      }
    });

    for (bke::AttributeTransferData &attribute : generic_attributes) {
      threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {
        for (const int i : selection.slice(range)) {
          const IndexRange src_points = src_curves.points_for_curve(i);
          const IndexRange dst_points = dst_curves.points_for_curve(i);
          bezier_generic_to_nurbs(attribute.src.slice(src_points),
                                  attribute.dst.span.slice(dst_points));
        }
      });
    }
  };

  auto poly_to_nurbs = [&](IndexMask selection) {
    dst_curves.nurbs_orders_for_write().fill_indices(selection, 4);
    bke::curves::copy_point_data(src_curves, dst_curves, selection, src_positions, dst_positions);
    fill_weights_if_necessary(selection);

    /* Avoid using "Endpoint" knots modes for cyclic curves, since it adds a sharp point at the
     * start/end. */
    if (src_cyclic.is_single()) {
      dst_curves.nurbs_knots_modes_for_write().fill_indices(
          selection,
          src_cyclic.get_internal_single() ? NURBS_KNOT_MODE_NORMAL : NURBS_KNOT_MODE_ENDPOINT);
    }
    else {
      VArraySpan<bool> cyclic{src_cyclic};
      MutableSpan<int8_t> knots_modes = dst_curves.nurbs_knots_modes_for_write();
      threading::parallel_for(selection.index_range(), 1024, [&](IndexRange range) {
        for (const int i : selection.slice(range)) {
          knots_modes[i] = cyclic[i] ? NURBS_KNOT_MODE_NORMAL : NURBS_KNOT_MODE_ENDPOINT;
        }
      });
    }

    for (bke::AttributeTransferData &attribute : generic_attributes) {
      bke::curves::copy_point_data(
          src_curves, dst_curves, selection, attribute.src, attribute.dst.span);
    }
  };

  auto bezier_to_nurbs = [&](IndexMask selection) {
    const Span<float3> src_handles_l = src_curves.handle_positions_left();
    const Span<float3> src_handles_r = src_curves.handle_positions_right();

    dst_curves.nurbs_orders_for_write().fill_indices(selection, 4);
    dst_curves.nurbs_knots_modes_for_write().fill_indices(selection, NURBS_KNOT_MODE_BEZIER);
    fill_weights_if_necessary(selection);

    threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {
      for (const int i : selection.slice(range)) {
        const IndexRange src_points = src_curves.points_for_curve(i);
        const IndexRange dst_points = dst_curves.points_for_curve(i);
        bezier_positions_to_nurbs(src_positions.slice(src_points),
                                  src_handles_l.slice(src_points),
                                  src_handles_r.slice(src_points),
                                  dst_positions.slice(dst_points));
      }
    });

    for (bke::AttributeTransferData &attribute : generic_attributes) {
      threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {
        for (const int i : selection.slice(range)) {
          const IndexRange src_points = src_curves.points_for_curve(i);
          const IndexRange dst_points = dst_curves.points_for_curve(i);
          bezier_generic_to_nurbs(attribute.src.slice(src_points),
                                  attribute.dst.span.slice(dst_points));
        }
      });
    }
  };

  auto nurbs_to_nurbs = [&](IndexMask selection) {
    bke::curves::copy_point_data(src_curves, dst_curves, selection, src_positions, dst_positions);

    if (!src_curves.nurbs_weights().is_empty()) {
      bke::curves::copy_point_data(src_curves,
                                   dst_curves,
                                   selection,
                                   src_curves.nurbs_weights(),
                                   dst_curves.nurbs_weights_for_write());
    }

    for (bke::AttributeTransferData &attribute : generic_attributes) {
      bke::curves::copy_point_data(
          src_curves, dst_curves, selection, attribute.src, attribute.dst.span);
    }
  };

  bke::curves::foreach_curve_by_type(src_curves.curve_types(),
                                     src_curves.curve_type_counts(),
                                     selection,
                                     catmull_rom_to_nurbs,
                                     poly_to_nurbs,
                                     bezier_to_nurbs,
                                     nurbs_to_nurbs);

  const Vector<IndexRange> unselected_ranges = selection.extract_ranges_invert(
      src_curves.curves_range());

  for (bke::AttributeTransferData &attribute : generic_attributes) {
    bke::curves::copy_point_data(
        src_curves, dst_curves, unselected_ranges, attribute.src, attribute.dst.span);
  }

  for (bke::AttributeTransferData &attribute : generic_attributes) {
    attribute.dst.finish();
  }

  return dst_curves;
}

static bke::CurvesGeometry convert_curves_trivial(const bke::CurvesGeometry &src_curves,
                                                  const IndexMask selection,
                                                  const CurveType dst_type)
{
  bke::CurvesGeometry dst_curves(src_curves);
  dst_curves.fill_curve_types(selection, dst_type);
  dst_curves.remove_attributes_based_on_types();
  return dst_curves;
}

bke::CurvesGeometry convert_curves(const bke::CurvesGeometry &src_curves,
                                   const IndexMask selection,
                                   const CurveType dst_type)
{
  switch (dst_type) {
    case CURVE_TYPE_CATMULL_ROM:
    case CURVE_TYPE_POLY:
      return convert_curves_trivial(src_curves, selection, dst_type);
    case CURVE_TYPE_BEZIER:
      return convert_curves_to_bezier(src_curves, selection);
    case CURVE_TYPE_NURBS:
      return convert_curves_to_nurbs(src_curves, selection);
  }
  BLI_assert_unreachable();
  return {};
}

bool try_curves_conversion_in_place(const IndexMask selection,
                                    const CurveType dst_type,
                                    FunctionRef<bke::CurvesGeometry &()> get_writable_curves_fn)
{
  if (conversion_can_change_point_num(dst_type)) {
    return false;
  }
  bke::CurvesGeometry &curves = get_writable_curves_fn();
  curves.fill_curve_types(selection, dst_type);
  curves.remove_attributes_based_on_types();
  return true;
}

}  // namespace blender::geometry