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

geometry_component_curve.cc « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 898869c3c4457c687f191bb22c7ab0b950c7037b (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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "BLI_task.hh"

#include "DNA_ID_enums.h"
#include "DNA_curve_types.h"

#include "BKE_attribute_access.hh"
#include "BKE_attribute_math.hh"
#include "BKE_curve.h"
#include "BKE_geometry_set.hh"
#include "BKE_lib_id.h"
#include "BKE_spline.hh"

#include "attribute_access_intern.hh"

using blender::GMutableSpan;
using blender::GSpan;
using blender::GVArray;
using blender::GVArray_GSpan;

/* -------------------------------------------------------------------- */
/** \name Geometry Component Implementation
 * \{ */

CurveComponentLegacy::CurveComponentLegacy() : GeometryComponent(GEO_COMPONENT_TYPE_CURVE)
{
}

CurveComponentLegacy::~CurveComponentLegacy()
{
  this->clear();
}

GeometryComponent *CurveComponentLegacy::copy() const
{
  CurveComponentLegacy *new_component = new CurveComponentLegacy();
  if (curve_ != nullptr) {
    new_component->curve_ = new CurveEval(*curve_);
    new_component->ownership_ = GeometryOwnershipType::Owned;
  }
  return new_component;
}

void CurveComponentLegacy::clear()
{
  BLI_assert(this->is_mutable());
  if (curve_ != nullptr) {
    if (ownership_ == GeometryOwnershipType::Owned) {
      delete curve_;
    }
    curve_ = nullptr;
  }
}

bool CurveComponentLegacy::has_curve() const
{
  return curve_ != nullptr;
}

void CurveComponentLegacy::replace(CurveEval *curve, GeometryOwnershipType ownership)
{
  BLI_assert(this->is_mutable());
  this->clear();
  curve_ = curve;
  ownership_ = ownership;
}

CurveEval *CurveComponentLegacy::release()
{
  BLI_assert(this->is_mutable());
  CurveEval *curve = curve_;
  curve_ = nullptr;
  return curve;
}

const CurveEval *CurveComponentLegacy::get_for_read() const
{
  return curve_;
}

CurveEval *CurveComponentLegacy::get_for_write()
{
  BLI_assert(this->is_mutable());
  if (ownership_ == GeometryOwnershipType::ReadOnly) {
    curve_ = new CurveEval(*curve_);
    ownership_ = GeometryOwnershipType::Owned;
  }
  return curve_;
}

bool CurveComponentLegacy::is_empty() const
{
  return curve_ == nullptr;
}

bool CurveComponentLegacy::owns_direct_data() const
{
  return ownership_ == GeometryOwnershipType::Owned;
}

void CurveComponentLegacy::ensure_owns_direct_data()
{
  BLI_assert(this->is_mutable());
  if (ownership_ != GeometryOwnershipType::Owned) {
    curve_ = new CurveEval(*curve_);
    ownership_ = GeometryOwnershipType::Owned;
  }
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Attribute Access Helper Functions
 * \{ */

int CurveComponentLegacy::attribute_domain_num(const eAttrDomain domain) const
{
  if (curve_ == nullptr) {
    return 0;
  }
  if (domain == ATTR_DOMAIN_POINT) {
    int total = 0;
    for (const SplinePtr &spline : curve_->splines()) {
      total += spline->size();
    }
    return total;
  }
  if (domain == ATTR_DOMAIN_CURVE) {
    return curve_->splines().size();
  }
  return 0;
}

namespace blender::bke {

namespace {
struct PointIndices {
  int spline_index;
  int point_index;
};
}  // namespace
static PointIndices lookup_point_indices(Span<int> offsets, const int index)
{
  const int spline_index = std::upper_bound(offsets.begin(), offsets.end(), index) -
                           offsets.begin() - 1;
  const int index_in_spline = index - offsets[spline_index];
  return {spline_index, index_in_spline};
}

/**
 * Mix together all of a spline's control point values.
 *
 * \note Theoretically this interpolation does not need to compute all values at once.
 * However, doing that makes the implementation simpler, and this can be optimized in the future if
 * only some values are required.
 */
template<typename T>
static void adapt_curve_domain_point_to_spline_impl(const CurveEval &curve,
                                                    const VArray<T> &old_values,
                                                    MutableSpan<T> r_values)
{
  const int splines_len = curve.splines().size();
  Array<int> offsets = curve.control_point_offsets();
  BLI_assert(r_values.size() == splines_len);
  attribute_math::DefaultMixer<T> mixer(r_values);

  for (const int i_spline : IndexRange(splines_len)) {
    const int spline_offset = offsets[i_spline];
    const int spline_point_len = offsets[i_spline + 1] - spline_offset;
    for (const int i_point : IndexRange(spline_point_len)) {
      const T value = old_values[spline_offset + i_point];
      mixer.mix_in(i_spline, value);
    }
  }

  mixer.finalize();
}

/**
 * A spline is selected if all of its control points were selected.
 *
 * \note Theoretically this interpolation does not need to compute all values at once.
 * However, doing that makes the implementation simpler, and this can be optimized in the future if
 * only some values are required.
 */
template<>
void adapt_curve_domain_point_to_spline_impl(const CurveEval &curve,
                                             const VArray<bool> &old_values,
                                             MutableSpan<bool> r_values)
{
  const int splines_len = curve.splines().size();
  Array<int> offsets = curve.control_point_offsets();
  BLI_assert(r_values.size() == splines_len);

  r_values.fill(true);

  for (const int i_spline : IndexRange(splines_len)) {
    const int spline_offset = offsets[i_spline];
    const int spline_point_len = offsets[i_spline + 1] - spline_offset;

    for (const int i_point : IndexRange(spline_point_len)) {
      if (!old_values[spline_offset + i_point]) {
        r_values[i_spline] = false;
        break;
      }
    }
  }
}

static GVArray adapt_curve_domain_point_to_spline(const CurveEval &curve, GVArray varray)
{
  GVArray new_varray;
  attribute_math::convert_to_static_type(varray.type(), [&](auto dummy) {
    using T = decltype(dummy);
    if constexpr (!std::is_void_v<attribute_math::DefaultMixer<T>>) {
      Array<T> values(curve.splines().size());
      adapt_curve_domain_point_to_spline_impl<T>(curve, varray.typed<T>(), values);
      new_varray = VArray<T>::ForContainer(std::move(values));
    }
  });
  return new_varray;
}

/**
 * A virtual array implementation for the conversion of spline attributes to control point
 * attributes. The goal is to avoid copying the spline value for every one of its control points
 * unless it is necessary (in that case the materialize functions will be called).
 */
template<typename T> class VArray_For_SplineToPoint final : public VArrayImpl<T> {
  GVArray original_varray_;
  /* Store existing data materialized if it was not already a span. This is expected
   * to be worth it because a single spline's value will likely be accessed many times. */
  VArray_Span<T> original_data_;
  Array<int> offsets_;

 public:
  VArray_For_SplineToPoint(GVArray original_varray, Array<int> offsets)
      : VArrayImpl<T>(offsets.last()),
        original_varray_(std::move(original_varray)),
        original_data_(original_varray_.typed<T>()),
        offsets_(std::move(offsets))
  {
  }

  T get(const int64_t index) const final
  {
    const PointIndices indices = lookup_point_indices(offsets_, index);
    return original_data_[indices.spline_index];
  }

  void materialize(const IndexMask mask, MutableSpan<T> r_span) const final
  {
    const int total_num = offsets_.last();
    if (mask.is_range() && mask.as_range() == IndexRange(total_num)) {
      for (const int spline_index : original_data_.index_range()) {
        const int offset = offsets_[spline_index];
        const int next_offset = offsets_[spline_index + 1];
        r_span.slice(offset, next_offset - offset).fill(original_data_[spline_index]);
      }
    }
    else {
      int spline_index = 0;
      for (const int dst_index : mask) {
        while (offsets_[spline_index] < dst_index) {
          spline_index++;
        }
        r_span[dst_index] = original_data_[spline_index];
      }
    }
  }

  void materialize_to_uninitialized(const IndexMask mask, MutableSpan<T> r_span) const final
  {
    T *dst = r_span.data();
    const int total_num = offsets_.last();
    if (mask.is_range() && mask.as_range() == IndexRange(total_num)) {
      for (const int spline_index : original_data_.index_range()) {
        const int offset = offsets_[spline_index];
        const int next_offset = offsets_[spline_index + 1];
        uninitialized_fill_n(dst + offset, next_offset - offset, original_data_[spline_index]);
      }
    }
    else {
      int spline_index = 0;
      for (const int dst_index : mask) {
        while (offsets_[spline_index] < dst_index) {
          spline_index++;
        }
        new (dst + dst_index) T(original_data_[spline_index]);
      }
    }
  }
};

static GVArray adapt_curve_domain_spline_to_point(const CurveEval &curve, GVArray varray)
{
  GVArray new_varray;
  attribute_math::convert_to_static_type(varray.type(), [&](auto dummy) {
    using T = decltype(dummy);

    Array<int> offsets = curve.control_point_offsets();
    new_varray = VArray<T>::template For<VArray_For_SplineToPoint<T>>(std::move(varray),
                                                                      std::move(offsets));
  });
  return new_varray;
}

}  // namespace blender::bke

GVArray CurveComponentLegacy::attribute_try_adapt_domain_impl(const GVArray &varray,
                                                              const eAttrDomain from_domain,
                                                              const eAttrDomain to_domain) const
{
  if (!varray) {
    return {};
  }
  if (varray.is_empty()) {
    return {};
  }
  if (from_domain == to_domain) {
    return varray;
  }

  if (from_domain == ATTR_DOMAIN_POINT && to_domain == ATTR_DOMAIN_CURVE) {
    return blender::bke::adapt_curve_domain_point_to_spline(*curve_, std::move(varray));
  }
  if (from_domain == ATTR_DOMAIN_CURVE && to_domain == ATTR_DOMAIN_POINT) {
    return blender::bke::adapt_curve_domain_spline_to_point(*curve_, std::move(varray));
  }

  return {};
}

static CurveEval *get_curve_from_component_for_write(GeometryComponent &component)
{
  BLI_assert(component.type() == GEO_COMPONENT_TYPE_CURVE);
  CurveComponentLegacy &curve_component = static_cast<CurveComponentLegacy &>(component);
  return curve_component.get_for_write();
}

static const CurveEval *get_curve_from_component_for_read(const GeometryComponent &component)
{
  BLI_assert(component.type() == GEO_COMPONENT_TYPE_CURVE);
  const CurveComponentLegacy &curve_component = static_cast<const CurveComponentLegacy &>(
      component);
  return curve_component.get_for_read();
}

/** \} */

namespace blender::bke {

/* -------------------------------------------------------------------- */
/** \name Builtin Spline Attributes
 *
 * Attributes with a value for every spline, stored contiguously or in every spline separately.
 * \{ */

class BuiltinSplineAttributeProvider final : public BuiltinAttributeProvider {
  using AsReadAttribute = GVArray (*)(const CurveEval &data);
  using AsWriteAttribute = GVMutableArray (*)(CurveEval &data);
  const AsReadAttribute as_read_attribute_;
  const AsWriteAttribute as_write_attribute_;

 public:
  BuiltinSplineAttributeProvider(std::string attribute_name,
                                 const eCustomDataType attribute_type,
                                 const WritableEnum writable,
                                 const AsReadAttribute as_read_attribute,
                                 const AsWriteAttribute as_write_attribute)
      : BuiltinAttributeProvider(std::move(attribute_name),
                                 ATTR_DOMAIN_CURVE,
                                 attribute_type,
                                 BuiltinAttributeProvider::NonCreatable,
                                 writable,
                                 BuiltinAttributeProvider::NonDeletable),
        as_read_attribute_(as_read_attribute),
        as_write_attribute_(as_write_attribute)
  {
  }

  GVArray try_get_for_read(const GeometryComponent &component) const final
  {
    const CurveEval *curve = get_curve_from_component_for_read(component);
    if (curve == nullptr) {
      return {};
    }
    return as_read_attribute_(*curve);
  }

  WriteAttributeLookup try_get_for_write(GeometryComponent &component) const final
  {
    if (writable_ != Writable) {
      return {};
    }
    CurveEval *curve = get_curve_from_component_for_write(component);
    if (curve == nullptr) {
      return {};
    }
    return {as_write_attribute_(*curve), domain_};
  }

  bool try_delete(GeometryComponent &UNUSED(component)) const final
  {
    return false;
  }

  bool try_create(GeometryComponent &UNUSED(component),
                  const AttributeInit &UNUSED(initializer)) const final
  {
    return false;
  }

  bool exists(const GeometryComponent &component) const final
  {
    return component.attribute_domain_num(ATTR_DOMAIN_CURVE) != 0;
  }
};

static int get_spline_resolution(const SplinePtr &spline)
{
  if (const BezierSpline *bezier_spline = dynamic_cast<const BezierSpline *>(spline.get())) {
    return bezier_spline->resolution();
  }
  if (const NURBSpline *nurb_spline = dynamic_cast<const NURBSpline *>(spline.get())) {
    return nurb_spline->resolution();
  }
  return 1;
}

static void set_spline_resolution(SplinePtr &spline, const int resolution)
{
  if (BezierSpline *bezier_spline = dynamic_cast<BezierSpline *>(spline.get())) {
    bezier_spline->set_resolution(std::max(resolution, 1));
  }
  if (NURBSpline *nurb_spline = dynamic_cast<NURBSpline *>(spline.get())) {
    nurb_spline->set_resolution(std::max(resolution, 1));
  }
}

static GVArray make_resolution_read_attribute(const CurveEval &curve)
{
  return VArray<int>::ForDerivedSpan<SplinePtr, get_spline_resolution>(curve.splines());
}

static GVMutableArray make_resolution_write_attribute(CurveEval &curve)
{
  return VMutableArray<int>::
      ForDerivedSpan<SplinePtr, get_spline_resolution, set_spline_resolution>(curve.splines());
}

static bool get_cyclic_value(const SplinePtr &spline)
{
  return spline->is_cyclic();
}

static void set_cyclic_value(SplinePtr &spline, const bool value)
{
  if (spline->is_cyclic() != value) {
    spline->set_cyclic(value);
    spline->mark_cache_invalid();
  }
}

static GVArray make_cyclic_read_attribute(const CurveEval &curve)
{
  return VArray<bool>::ForDerivedSpan<SplinePtr, get_cyclic_value>(curve.splines());
}

static GVMutableArray make_cyclic_write_attribute(CurveEval &curve)
{
  return VMutableArray<bool>::ForDerivedSpan<SplinePtr, get_cyclic_value, set_cyclic_value>(
      curve.splines());
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Builtin Control Point Attributes
 *
 * Attributes with a value for every control point. Most of the complexity here is due to the fact
 * that we must provide access to the attribute data as if it was a contiguous array when it is
 * really stored separately on each spline. That will be inherently rather slow, but these virtual
 * array implementations try to make it workable in common situations.
 * \{ */

/**
 * Individual spans in \a data may be empty if that spline contains no data for the attribute.
 */
template<typename T>
static void point_attribute_materialize(Span<Span<T>> data,
                                        Span<int> offsets,
                                        const IndexMask mask,
                                        MutableSpan<T> r_span)
{
  const int total_num = offsets.last();
  if (mask.is_range() && mask.as_range() == IndexRange(total_num)) {
    for (const int spline_index : data.index_range()) {
      const int offset = offsets[spline_index];
      const int next_offset = offsets[spline_index + 1];

      Span<T> src = data[spline_index];
      MutableSpan<T> dst = r_span.slice(offset, next_offset - offset);
      if (src.is_empty()) {
        dst.fill(T());
      }
      else {
        dst.copy_from(src);
      }
    }
  }
  else {
    int spline_index = 0;
    for (const int dst_index : mask) {
      /* Skip splines that don't have any control points in the mask. */
      while (dst_index >= offsets[spline_index + 1]) {
        spline_index++;
      }

      const int index_in_spline = dst_index - offsets[spline_index];
      Span<T> src = data[spline_index];
      if (src.is_empty()) {
        r_span[dst_index] = T();
      }
      else {
        r_span[dst_index] = src[index_in_spline];
      }
    }
  }
}

/**
 * Individual spans in \a data may be empty if that spline contains no data for the attribute.
 */
template<typename T>
static void point_attribute_materialize_to_uninitialized(Span<Span<T>> data,
                                                         Span<int> offsets,
                                                         const IndexMask mask,
                                                         MutableSpan<T> r_span)
{
  T *dst = r_span.data();
  const int total_num = offsets.last();
  if (mask.is_range() && mask.as_range() == IndexRange(total_num)) {
    for (const int spline_index : data.index_range()) {
      const int offset = offsets[spline_index];
      const int next_offset = offsets[spline_index + 1];

      Span<T> src = data[spline_index];
      if (src.is_empty()) {
        uninitialized_fill_n(dst + offset, next_offset - offset, T());
      }
      else {
        uninitialized_copy_n(src.data(), next_offset - offset, dst + offset);
      }
    }
  }
  else {
    int spline_index = 0;
    for (const int dst_index : mask) {
      /* Skip splines that don't have any control points in the mask. */
      while (dst_index >= offsets[spline_index + 1]) {
        spline_index++;
      }

      const int index_in_spline = dst_index - offsets[spline_index];
      Span<T> src = data[spline_index];
      if (src.is_empty()) {
        new (dst + dst_index) T();
      }
      else {
        new (dst + dst_index) T(src[index_in_spline]);
      }
    }
  }
}

static GVArray varray_from_initializer(const AttributeInit &initializer,
                                       const eCustomDataType data_type,
                                       const Span<SplinePtr> splines)
{
  switch (initializer.type) {
    case AttributeInit::Type::Default:
      /* This function shouldn't be called in this case, since there
       * is no need to copy anything to the new custom data array. */
      BLI_assert_unreachable();
      return {};
    case AttributeInit::Type::VArray:
      return static_cast<const AttributeInitVArray &>(initializer).varray;
    case AttributeInit::Type::MoveArray:
      int total_num = 0;
      for (const SplinePtr &spline : splines) {
        total_num += spline->size();
      }
      return GVArray::ForSpan(GSpan(*bke::custom_data_type_to_cpp_type(data_type),
                                    static_cast<const AttributeInitMove &>(initializer).data,
                                    total_num));
  }
  BLI_assert_unreachable();
  return {};
}

static bool create_point_attribute(GeometryComponent &component,
                                   const AttributeIDRef &attribute_id,
                                   const AttributeInit &initializer,
                                   const eCustomDataType data_type)
{
  CurveEval *curve = get_curve_from_component_for_write(component);
  if (curve == nullptr || curve->splines().size() == 0) {
    return false;
  }

  MutableSpan<SplinePtr> splines = curve->splines();

  /* First check the one case that allows us to avoid copying the input data. */
  if (splines.size() == 1 && initializer.type == AttributeInit::Type::MoveArray) {
    void *source_data = static_cast<const AttributeInitMove &>(initializer).data;
    if (!splines.first()->attributes.create_by_move(attribute_id, data_type, source_data)) {
      MEM_freeN(source_data);
      return false;
    }
    return true;
  }

  /* Otherwise just create a custom data layer on each of the splines. */
  for (const int i : splines.index_range()) {
    if (!splines[i]->attributes.create(attribute_id, data_type)) {
      /* If attribute creation fails on one of the splines, we cannot leave the custom data
       * layers in the previous splines around, so delete them before returning. However,
       * this is not an expected case. */
      BLI_assert_unreachable();
      return false;
    }
  }

  /* With a default initializer type, we can keep the values at their initial values. */
  if (initializer.type == AttributeInit::Type::Default) {
    return true;
  }

  WriteAttributeLookup write_attribute = component.attribute_try_get_for_write(attribute_id);
  /* We just created the attribute, it should exist. */
  BLI_assert(write_attribute);

  GVArray source_varray = varray_from_initializer(initializer, data_type, splines);
  /* TODO: When we can call a variant of #set_all with a virtual array argument,
   * this theoretically unnecessary materialize step could be removed. */
  GVArray_GSpan source_varray_span{source_varray};
  write_attribute.varray.set_all(source_varray_span.data());

  if (initializer.type == AttributeInit::Type::MoveArray) {
    MEM_freeN(static_cast<const AttributeInitMove &>(initializer).data);
  }

  return true;
}

static bool remove_point_attribute(GeometryComponent &component,
                                   const AttributeIDRef &attribute_id)
{
  CurveEval *curve = get_curve_from_component_for_write(component);
  if (curve == nullptr) {
    return false;
  }

  /* Reuse the boolean for all splines; we expect all splines to have the same attributes. */
  bool layer_freed = false;
  for (SplinePtr &spline : curve->splines()) {
    layer_freed = spline->attributes.remove(attribute_id);
  }
  return layer_freed;
}

/**
 * Mutable virtual array for any control point data accessed with spans and an offset array.
 */
template<typename T> class VArrayImpl_For_SplinePoints final : public VMutableArrayImpl<T> {
 private:
  Array<MutableSpan<T>> data_;
  Array<int> offsets_;

 public:
  VArrayImpl_For_SplinePoints(Array<MutableSpan<T>> data, Array<int> offsets)
      : VMutableArrayImpl<T>(offsets.last()), data_(std::move(data)), offsets_(std::move(offsets))
  {
  }

  T get(const int64_t index) const final
  {
    const PointIndices indices = lookup_point_indices(offsets_, index);
    return data_[indices.spline_index][indices.point_index];
  }

  void set(const int64_t index, T value) final
  {
    const PointIndices indices = lookup_point_indices(offsets_, index);
    data_[indices.spline_index][indices.point_index] = value;
  }

  void set_all(Span<T> src) final
  {
    for (const int spline_index : data_.index_range()) {
      const int offset = offsets_[spline_index];
      const int next_offsets = offsets_[spline_index + 1];
      data_[spline_index].copy_from(src.slice(offset, next_offsets - offset));
    }
  }

  void materialize(const IndexMask mask, MutableSpan<T> r_span) const final
  {
    point_attribute_materialize({(Span<T> *)data_.data(), data_.size()}, offsets_, mask, r_span);
  }

  void materialize_to_uninitialized(const IndexMask mask, MutableSpan<T> r_span) const final
  {
    point_attribute_materialize_to_uninitialized(
        {(Span<T> *)data_.data(), data_.size()}, offsets_, mask, r_span);
  }
};

template<typename T> VArray<T> point_data_varray(Array<MutableSpan<T>> spans, Array<int> offsets)
{
  return VArray<T>::template For<VArrayImpl_For_SplinePoints<T>>(std::move(spans),
                                                                 std::move(offsets));
}

template<typename T>
VMutableArray<T> point_data_varray_mutable(Array<MutableSpan<T>> spans, Array<int> offsets)
{
  return VMutableArray<T>::template For<VArrayImpl_For_SplinePoints<T>>(std::move(spans),
                                                                        std::move(offsets));
}

/**
 * Virtual array implementation specifically for control point positions. This is only needed for
 * Bezier splines, where adjusting the position also requires adjusting handle positions depending
 * on handle types. We pay a small price for this when other spline types are mixed with Bezier.
 *
 * \note There is no need to check the handle type to avoid changing auto handles, since
 * retrieving write access to the position data will mark them for recomputation anyway.
 */
class VArrayImpl_For_SplinePosition final : public VMutableArrayImpl<float3> {
 private:
  MutableSpan<SplinePtr> splines_;
  Array<int> offsets_;

 public:
  VArrayImpl_For_SplinePosition(MutableSpan<SplinePtr> splines, Array<int> offsets)
      : VMutableArrayImpl<float3>(offsets.last()), splines_(splines), offsets_(std::move(offsets))
  {
  }

  float3 get(const int64_t index) const final
  {
    const PointIndices indices = lookup_point_indices(offsets_, index);
    return splines_[indices.spline_index]->positions()[indices.point_index];
  }

  void set(const int64_t index, float3 value) final
  {
    const PointIndices indices = lookup_point_indices(offsets_, index);
    Spline &spline = *splines_[indices.spline_index];
    spline.positions()[indices.point_index] = value;
  }

  void set_all(Span<float3> src) final
  {
    for (const int spline_index : splines_.index_range()) {
      Spline &spline = *splines_[spline_index];
      const int offset = offsets_[spline_index];
      const int next_offset = offsets_[spline_index + 1];
      spline.positions().copy_from(src.slice(offset, next_offset - offset));
    }
  }

  /** Utility so we can pass positions to the materialize functions above. */
  Array<Span<float3>> get_position_spans() const
  {
    Array<Span<float3>> spans(splines_.size());
    for (const int i : spans.index_range()) {
      spans[i] = splines_[i]->positions();
    }
    return spans;
  }

  void materialize(const IndexMask mask, MutableSpan<float3> r_span) const final
  {
    Array<Span<float3>> spans = this->get_position_spans();
    point_attribute_materialize(spans.as_span(), offsets_, mask, r_span);
  }

  void materialize_to_uninitialized(const IndexMask mask, MutableSpan<float3> r_span) const final
  {
    Array<Span<float3>> spans = this->get_position_spans();
    point_attribute_materialize_to_uninitialized(spans.as_span(), offsets_, mask, r_span);
  }
};

class VArrayImpl_For_BezierHandles final : public VMutableArrayImpl<float3> {
 private:
  MutableSpan<SplinePtr> splines_;
  Array<int> offsets_;
  bool is_right_;

 public:
  VArrayImpl_For_BezierHandles(MutableSpan<SplinePtr> splines,
                               Array<int> offsets,
                               const bool is_right)
      : VMutableArrayImpl<float3>(offsets.last()),
        splines_(splines),
        offsets_(std::move(offsets)),
        is_right_(is_right)
  {
  }

  float3 get(const int64_t index) const final
  {
    const PointIndices indices = lookup_point_indices(offsets_, index);
    const Spline &spline = *splines_[indices.spline_index];
    if (spline.type() == CURVE_TYPE_BEZIER) {
      const BezierSpline &bezier_spline = static_cast<const BezierSpline &>(spline);
      return is_right_ ? bezier_spline.handle_positions_right()[indices.point_index] :
                         bezier_spline.handle_positions_left()[indices.point_index];
    }
    return float3(0);
  }

  void set(const int64_t index, float3 value) final
  {
    const PointIndices indices = lookup_point_indices(offsets_, index);
    Spline &spline = *splines_[indices.spline_index];
    if (spline.type() == CURVE_TYPE_BEZIER) {
      BezierSpline &bezier_spline = static_cast<BezierSpline &>(spline);
      if (is_right_) {
        bezier_spline.handle_positions_right()[indices.point_index] = value;
      }
      else {
        bezier_spline.handle_positions_left()[indices.point_index] = value;
      }
      bezier_spline.mark_cache_invalid();
    }
  }

  void set_all(Span<float3> src) final
  {
    for (const int spline_index : splines_.index_range()) {
      Spline &spline = *splines_[spline_index];
      if (spline.type() == CURVE_TYPE_BEZIER) {
        const int offset = offsets_[spline_index];

        BezierSpline &bezier_spline = static_cast<BezierSpline &>(spline);
        if (is_right_) {
          for (const int i : IndexRange(bezier_spline.size())) {
            bezier_spline.handle_positions_right()[i] = src[offset + i];
          }
        }
        else {
          for (const int i : IndexRange(bezier_spline.size())) {
            bezier_spline.handle_positions_left()[i] = src[offset + i];
          }
        }
        bezier_spline.mark_cache_invalid();
      }
    }
  }

  void materialize(const IndexMask mask, MutableSpan<float3> r_span) const final
  {
    Array<Span<float3>> spans = get_handle_spans(splines_, is_right_);
    point_attribute_materialize(spans.as_span(), offsets_, mask, r_span);
  }

  void materialize_to_uninitialized(const IndexMask mask, MutableSpan<float3> r_span) const final
  {
    Array<Span<float3>> spans = get_handle_spans(splines_, is_right_);
    point_attribute_materialize_to_uninitialized(spans.as_span(), offsets_, mask, r_span);
  }

  /**
   * Utility so we can pass handle positions to the materialize functions above.
   *
   * \note This relies on the ability of the materialize implementations to
   * handle empty spans, since only Bezier splines have handles.
   */
  static Array<Span<float3>> get_handle_spans(Span<SplinePtr> splines, const bool is_right)
  {
    Array<Span<float3>> spans(splines.size());
    for (const int i : spans.index_range()) {
      if (splines[i]->type() == CURVE_TYPE_BEZIER) {
        BezierSpline &bezier_spline = static_cast<BezierSpline &>(*splines[i]);
        spans[i] = is_right ? bezier_spline.handle_positions_right() :
                              bezier_spline.handle_positions_left();
      }
      else {
        spans[i] = {};
      }
    }
    return spans;
  }
};

/**
 * Provider for any builtin control point attribute that doesn't need
 * special handling like access to other arrays in the spline.
 */
template<typename T> class BuiltinPointAttributeProvider : public BuiltinAttributeProvider {
 protected:
  using GetSpan = Span<T> (*)(const Spline &spline);
  using GetMutableSpan = MutableSpan<T> (*)(Spline &spline);
  using UpdateOnWrite = void (*)(Spline &spline);
  const GetSpan get_span_;
  const GetMutableSpan get_mutable_span_;
  const UpdateOnWrite update_on_write_;
  bool stored_in_custom_data_;

 public:
  BuiltinPointAttributeProvider(std::string attribute_name,
                                const CreatableEnum creatable,
                                const DeletableEnum deletable,
                                const GetSpan get_span,
                                const GetMutableSpan get_mutable_span,
                                const UpdateOnWrite update_on_write,
                                const bool stored_in_custom_data)
      : BuiltinAttributeProvider(std::move(attribute_name),
                                 ATTR_DOMAIN_POINT,
                                 bke::cpp_type_to_custom_data_type(CPPType::get<T>()),
                                 creatable,
                                 WritableEnum::Writable,
                                 deletable),
        get_span_(get_span),
        get_mutable_span_(get_mutable_span),
        update_on_write_(update_on_write),
        stored_in_custom_data_(stored_in_custom_data)
  {
  }

  GVArray try_get_for_read(const GeometryComponent &component) const override
  {
    const CurveEval *curve = get_curve_from_component_for_read(component);
    if (curve == nullptr) {
      return {};
    }

    if (!this->exists(component)) {
      return {};
    }

    Span<SplinePtr> splines = curve->splines();
    if (splines.size() == 1) {
      return GVArray::ForSpan(get_span_(*splines.first()));
    }

    Array<int> offsets = curve->control_point_offsets();
    Array<MutableSpan<T>> spans(splines.size());
    for (const int i : splines.index_range()) {
      Span<T> span = get_span_(*splines[i]);
      /* Use const-cast because the underlying virtual array implementation is shared between const
       * and non const data. */
      spans[i] = MutableSpan<T>(const_cast<T *>(span.data()), span.size());
    }

    return point_data_varray(spans, offsets);
  }

  WriteAttributeLookup try_get_for_write(GeometryComponent &component) const override
  {
    CurveEval *curve = get_curve_from_component_for_write(component);
    if (curve == nullptr) {
      return {};
    }

    if (!this->exists(component)) {
      return {};
    }

    std::function<void()> tag_modified_fn;
    if (update_on_write_ != nullptr) {
      tag_modified_fn = [curve, update = update_on_write_]() {
        for (SplinePtr &spline : curve->splines()) {
          update(*spline);
        }
      };
    }

    MutableSpan<SplinePtr> splines = curve->splines();
    if (splines.size() == 1) {
      return {GVMutableArray::ForSpan(get_mutable_span_(*splines.first())),
              domain_,
              std::move(tag_modified_fn)};
    }

    Array<int> offsets = curve->control_point_offsets();
    Array<MutableSpan<T>> spans(splines.size());
    for (const int i : splines.index_range()) {
      spans[i] = get_mutable_span_(*splines[i]);
    }

    return {point_data_varray_mutable(spans, offsets), domain_, tag_modified_fn};
  }

  bool try_delete(GeometryComponent &component) const final
  {
    if (deletable_ == DeletableEnum::NonDeletable) {
      return false;
    }
    return remove_point_attribute(component, name_);
  }

  bool try_create(GeometryComponent &component, const AttributeInit &initializer) const final
  {
    if (createable_ == CreatableEnum::NonCreatable) {
      return false;
    }
    return create_point_attribute(component, name_, initializer, CD_PROP_INT32);
  }

  bool exists(const GeometryComponent &component) const final
  {
    const CurveEval *curve = get_curve_from_component_for_read(component);
    if (curve == nullptr) {
      return false;
    }

    Span<SplinePtr> splines = curve->splines();
    if (splines.size() == 0) {
      return false;
    }

    if (stored_in_custom_data_) {
      if (!curve->splines().first()->attributes.get_for_read(name_)) {
        return false;
      }
    }

    bool has_point = false;
    for (const SplinePtr &spline : curve->splines()) {
      if (spline->size() != 0) {
        has_point = true;
        break;
      }
    }

    if (!has_point) {
      return false;
    }

    return true;
  }
};

/**
 * Special attribute provider for the position attribute. Keeping this separate means we don't
 * need to make #BuiltinPointAttributeProvider overly generic, and the special handling for the
 * positions is more clear.
 */
class PositionAttributeProvider final : public BuiltinPointAttributeProvider<float3> {
 public:
  PositionAttributeProvider()
      : BuiltinPointAttributeProvider(
            "position",
            BuiltinAttributeProvider::NonCreatable,
            BuiltinAttributeProvider::NonDeletable,
            [](const Spline &spline) { return spline.positions(); },
            [](Spline &spline) { return spline.positions(); },
            [](Spline &spline) { spline.mark_cache_invalid(); },
            false)
  {
  }

  WriteAttributeLookup try_get_for_write(GeometryComponent &component) const final
  {
    CurveEval *curve = get_curve_from_component_for_write(component);
    if (curve == nullptr) {
      return {};
    }

    /* Use the regular position virtual array when there aren't any Bezier splines
     * to avoid the overhead of checking the spline type for every point. */
    if (!curve->has_spline_with_type(CURVE_TYPE_BEZIER)) {
      return BuiltinPointAttributeProvider<float3>::try_get_for_write(component);
    }

    auto tag_modified_fn = [curve]() {
      /* Changing the positions requires recalculation of cached evaluated data in many cases.
       * This could set more specific flags in the future to avoid unnecessary recomputation. */
      curve->mark_cache_invalid();
    };

    Array<int> offsets = curve->control_point_offsets();
    return {VMutableArray<float3>::For<VArrayImpl_For_SplinePosition>(curve->splines(),
                                                                      std::move(offsets)),
            domain_,
            tag_modified_fn};
  }
};

class BezierHandleAttributeProvider : public BuiltinAttributeProvider {
 private:
  bool is_right_;

 public:
  BezierHandleAttributeProvider(const bool is_right)
      : BuiltinAttributeProvider(is_right ? "handle_right" : "handle_left",
                                 ATTR_DOMAIN_POINT,
                                 CD_PROP_FLOAT3,
                                 BuiltinAttributeProvider::NonCreatable,
                                 BuiltinAttributeProvider::Writable,
                                 BuiltinAttributeProvider::NonDeletable),
        is_right_(is_right)
  {
  }

  GVArray try_get_for_read(const GeometryComponent &component) const override
  {
    const CurveEval *curve = get_curve_from_component_for_read(component);
    if (curve == nullptr) {
      return {};
    }

    if (!curve->has_spline_with_type(CURVE_TYPE_BEZIER)) {
      return {};
    }

    Array<int> offsets = curve->control_point_offsets();
    /* Use const-cast because the underlying virtual array implementation is shared between const
     * and non const data. */
    return VArray<float3>::For<VArrayImpl_For_BezierHandles>(
        const_cast<CurveEval *>(curve)->splines(), std::move(offsets), is_right_);
  }

  WriteAttributeLookup try_get_for_write(GeometryComponent &component) const override
  {
    CurveEval *curve = get_curve_from_component_for_write(component);
    if (curve == nullptr) {
      return {};
    }

    if (!curve->has_spline_with_type(CURVE_TYPE_BEZIER)) {
      return {};
    }

    auto tag_modified_fn = [curve]() { curve->mark_cache_invalid(); };

    Array<int> offsets = curve->control_point_offsets();
    return {VMutableArray<float3>::For<VArrayImpl_For_BezierHandles>(
                curve->splines(), std::move(offsets), is_right_),
            domain_,
            tag_modified_fn};
  }

  bool try_delete(GeometryComponent &UNUSED(component)) const final
  {
    return false;
  }

  bool try_create(GeometryComponent &UNUSED(component),
                  const AttributeInit &UNUSED(initializer)) const final
  {
    return false;
  }

  bool exists(const GeometryComponent &component) const final
  {
    const CurveEval *curve = get_curve_from_component_for_read(component);
    if (curve == nullptr) {
      return false;
    }

    return curve->has_spline_with_type(CURVE_TYPE_BEZIER) &&
           component.attribute_domain_num(ATTR_DOMAIN_POINT) != 0;
  }
};

/** \} */

/* -------------------------------------------------------------------- */
/** \name Dynamic Control Point Attributes
 *
 * The dynamic control point attribute implementation is very similar to the builtin attribute
 * implementation-- it uses the same virtual array types. In order to work, this code depends on
 * the fact that all a curve's splines will have the same attributes and they all have the same
 * type.
 * \{ */

class DynamicPointAttributeProvider final : public DynamicAttributesProvider {
 private:
  static constexpr uint64_t supported_types_mask = CD_MASK_PROP_FLOAT | CD_MASK_PROP_FLOAT2 |
                                                   CD_MASK_PROP_FLOAT3 | CD_MASK_PROP_INT32 |
                                                   CD_MASK_PROP_COLOR | CD_MASK_PROP_BOOL |
                                                   CD_MASK_PROP_INT8;

 public:
  ReadAttributeLookup try_get_for_read(const GeometryComponent &component,
                                       const AttributeIDRef &attribute_id) const final
  {
    const CurveEval *curve = get_curve_from_component_for_read(component);
    if (curve == nullptr || curve->splines().size() == 0) {
      return {};
    }

    Span<SplinePtr> splines = curve->splines();
    Vector<GSpan> spans; /* GSpan has no default constructor. */
    spans.reserve(splines.size());
    std::optional<GSpan> first_span = splines[0]->attributes.get_for_read(attribute_id);
    if (!first_span) {
      return {};
    }
    spans.append(*first_span);
    for (const int i : IndexRange(1, splines.size() - 1)) {
      std::optional<GSpan> span = splines[i]->attributes.get_for_read(attribute_id);
      if (!span) {
        /* All splines should have the same set of data layers. It would be possible to recover
         * here and return partial data instead, but that would add a lot of complexity for a
         * situation we don't even expect to encounter. */
        BLI_assert_unreachable();
        return {};
      }
      if (span->type() != spans.last().type()) {
        /* Data layer types on separate splines do not match. */
        BLI_assert_unreachable();
        return {};
      }
      spans.append(*span);
    }

    /* First check for the simpler situation when we can return a simpler span virtual array. */
    if (spans.size() == 1) {
      return {GVArray::ForSpan(spans.first()), ATTR_DOMAIN_POINT};
    }

    ReadAttributeLookup attribute = {};
    Array<int> offsets = curve->control_point_offsets();
    attribute_math::convert_to_static_type(spans[0].type(), [&](auto dummy) {
      using T = decltype(dummy);
      Array<MutableSpan<T>> data(splines.size());
      for (const int i : splines.index_range()) {
        Span<T> span = spans[i].typed<T>();
        /* Use const-cast because the underlying virtual array implementation is shared between
         * const and non const data. */
        data[i] = MutableSpan<T>(const_cast<T *>(span.data()), span.size());
        BLI_assert(data[i].data() != nullptr);
      }
      attribute = {point_data_varray(data, offsets), ATTR_DOMAIN_POINT};
    });
    return attribute;
  }

  /* This function is almost the same as #try_get_for_read, but without const. */
  WriteAttributeLookup try_get_for_write(GeometryComponent &component,
                                         const AttributeIDRef &attribute_id) const final
  {
    CurveEval *curve = get_curve_from_component_for_write(component);
    if (curve == nullptr || curve->splines().size() == 0) {
      return {};
    }

    MutableSpan<SplinePtr> splines = curve->splines();
    Vector<GMutableSpan> spans; /* GMutableSpan has no default constructor. */
    spans.reserve(splines.size());
    std::optional<GMutableSpan> first_span = splines[0]->attributes.get_for_write(attribute_id);
    if (!first_span) {
      return {};
    }
    spans.append(*first_span);
    for (const int i : IndexRange(1, splines.size() - 1)) {
      std::optional<GMutableSpan> span = splines[i]->attributes.get_for_write(attribute_id);
      if (!span) {
        /* All splines should have the same set of data layers. It would be possible to recover
         * here and return partial data instead, but that would add a lot of complexity for a
         * situation we don't even expect to encounter. */
        BLI_assert_unreachable();
        return {};
      }
      if (span->type() != spans.last().type()) {
        /* Data layer types on separate splines do not match. */
        BLI_assert_unreachable();
        return {};
      }
      spans.append(*span);
    }

    /* First check for the simpler situation when we can return a simpler span virtual array. */
    if (spans.size() == 1) {
      return {GVMutableArray::ForSpan(spans.first()), ATTR_DOMAIN_POINT};
    }

    WriteAttributeLookup attribute = {};
    Array<int> offsets = curve->control_point_offsets();
    attribute_math::convert_to_static_type(spans[0].type(), [&](auto dummy) {
      using T = decltype(dummy);
      Array<MutableSpan<T>> data(splines.size());
      for (const int i : splines.index_range()) {
        data[i] = spans[i].typed<T>();
        BLI_assert(data[i].data() != nullptr);
      }
      attribute = {point_data_varray_mutable(data, offsets), ATTR_DOMAIN_POINT};
    });
    return attribute;
  }

  bool try_delete(GeometryComponent &component, const AttributeIDRef &attribute_id) const final
  {
    return remove_point_attribute(component, attribute_id);
  }

  bool try_create(GeometryComponent &component,
                  const AttributeIDRef &attribute_id,
                  const eAttrDomain domain,
                  const eCustomDataType data_type,
                  const AttributeInit &initializer) const final
  {
    BLI_assert(this->type_is_supported(data_type));
    if (domain != ATTR_DOMAIN_POINT) {
      return false;
    }
    return create_point_attribute(component, attribute_id, initializer, data_type);
  }

  bool foreach_attribute(const GeometryComponent &component,
                         const AttributeForeachCallback callback) const final
  {
    const CurveEval *curve = get_curve_from_component_for_read(component);
    if (curve == nullptr || curve->splines().size() == 0) {
      return false;
    }

    Span<SplinePtr> splines = curve->splines();

    /* In a debug build, check that all corresponding custom data layers have the same type. */
    curve->assert_valid_point_attributes();

    /* Use the first spline as a representative for all the others. */
    splines.first()->attributes.foreach_attribute(callback, ATTR_DOMAIN_POINT);

    return true;
  }

  void foreach_domain(const FunctionRef<void(eAttrDomain)> callback) const final
  {
    callback(ATTR_DOMAIN_POINT);
  }

  bool type_is_supported(eCustomDataType data_type) const
  {
    return ((1ULL << data_type) & supported_types_mask) != 0;
  }
};

/** \} */

/* -------------------------------------------------------------------- */
/** \name Attribute Provider Declaration
 * \{ */

/**
 * In this function all the attribute providers for a curve component are created.
 * Most data in this function is statically allocated, because it does not change over time.
 */
static ComponentAttributeProviders create_attribute_providers_for_curve()
{
  static BuiltinSplineAttributeProvider resolution("resolution",
                                                   CD_PROP_INT32,
                                                   BuiltinAttributeProvider::Writable,
                                                   make_resolution_read_attribute,
                                                   make_resolution_write_attribute);

  static BuiltinSplineAttributeProvider cyclic("cyclic",
                                               CD_PROP_BOOL,
                                               BuiltinAttributeProvider::Writable,
                                               make_cyclic_read_attribute,
                                               make_cyclic_write_attribute);

  static CustomDataAccessInfo spline_custom_data_access = {
      [](GeometryComponent &component) -> CustomData * {
        CurveEval *curve = get_curve_from_component_for_write(component);
        return curve ? &curve->attributes.data : nullptr;
      },
      [](const GeometryComponent &component) -> const CustomData * {
        const CurveEval *curve = get_curve_from_component_for_read(component);
        return curve ? &curve->attributes.data : nullptr;
      },
      nullptr};

  static CustomDataAttributeProvider spline_custom_data(ATTR_DOMAIN_CURVE,
                                                        spline_custom_data_access);

  static PositionAttributeProvider position;
  static BezierHandleAttributeProvider handles_start(false);
  static BezierHandleAttributeProvider handles_end(true);

  static BuiltinPointAttributeProvider<int> id(
      "id",
      BuiltinAttributeProvider::Creatable,
      BuiltinAttributeProvider::Deletable,
      [](const Spline &spline) {
        std::optional<GSpan> span = spline.attributes.get_for_read("id");
        return span ? span->typed<int>() : Span<int>();
      },
      [](Spline &spline) {
        std::optional<GMutableSpan> span = spline.attributes.get_for_write("id");
        return span ? span->typed<int>() : MutableSpan<int>();
      },
      {},
      true);

  static BuiltinPointAttributeProvider<float> radius(
      "radius",
      BuiltinAttributeProvider::NonCreatable,
      BuiltinAttributeProvider::NonDeletable,
      [](const Spline &spline) { return spline.radii(); },
      [](Spline &spline) { return spline.radii(); },
      nullptr,
      false);

  static BuiltinPointAttributeProvider<float> tilt(
      "tilt",
      BuiltinAttributeProvider::NonCreatable,
      BuiltinAttributeProvider::NonDeletable,
      [](const Spline &spline) { return spline.tilts(); },
      [](Spline &spline) { return spline.tilts(); },
      [](Spline &spline) { spline.mark_cache_invalid(); },
      false);

  static DynamicPointAttributeProvider point_custom_data;

  return ComponentAttributeProviders(
      {&position, &id, &radius, &tilt, &handles_start, &handles_end, &resolution, &cyclic},
      {&spline_custom_data, &point_custom_data});
}

/** \} */

}  // namespace blender::bke

const blender::bke::ComponentAttributeProviders *CurveComponentLegacy::get_attribute_providers()
    const
{
  static blender::bke::ComponentAttributeProviders providers =
      blender::bke::create_attribute_providers_for_curve();
  return &providers;
}