Blender V4.5
curves_geometry.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 <utility>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_array_utils.hh"
14#include "BLI_bounds.hh"
15#include "BLI_index_mask.hh"
17#include "BLI_listbase.h"
18#include "BLI_math_matrix.hh"
20#include "BLI_memory_counter.hh"
21#include "BLI_resource_scope.hh"
22#include "BLI_task.hh"
23
24#include "BLO_read_write.hh"
25
26#include "BLT_translation.hh"
27
28#include "DNA_curves_types.h"
29#include "DNA_material_types.h"
30
31#include "BKE_attribute.hh"
33#include "BKE_attribute_math.hh"
37#include "BKE_curves.hh"
38#include "BKE_curves_utils.hh"
39#include "BKE_customdata.hh"
40#include "BKE_deform.hh"
41#include "BKE_report.hh"
42
43namespace blender::bke {
44
45constexpr StringRef ATTR_POSITION = "position";
46constexpr StringRef ATTR_RADIUS = "radius";
47constexpr StringRef ATTR_TILT = "tilt";
48constexpr StringRef ATTR_CURVE_TYPE = "curve_type";
49constexpr StringRef ATTR_CYCLIC = "cyclic";
50constexpr StringRef ATTR_RESOLUTION = "resolution";
51constexpr StringRef ATTR_NORMAL_MODE = "normal_mode";
52constexpr StringRef ATTR_HANDLE_TYPE_LEFT = "handle_type_left";
53constexpr StringRef ATTR_HANDLE_TYPE_RIGHT = "handle_type_right";
54constexpr StringRef ATTR_HANDLE_POSITION_LEFT = "handle_left";
55constexpr StringRef ATTR_HANDLE_POSITION_RIGHT = "handle_right";
56constexpr StringRef ATTR_NURBS_ORDER = "nurbs_order";
57constexpr StringRef ATTR_NURBS_WEIGHT = "nurbs_weight";
58constexpr StringRef ATTR_NURBS_KNOTS_MODE = "knots_mode";
59constexpr StringRef ATTR_SURFACE_UV_COORDINATE = "surface_uv_coordinate";
60
61/* -------------------------------------------------------------------- */
64
65CurvesGeometry::CurvesGeometry() : CurvesGeometry(0, 0) {}
66
67CurvesGeometry::CurvesGeometry(const int point_num, const int curve_num)
68{
69 this->runtime = MEM_new<CurvesGeometryRuntime>(__func__);
70
71 this->point_num = point_num;
72 this->curve_num = curve_num;
77
80
81 this->custom_knots = nullptr;
82 this->custom_knot_num = 0;
83
84 if (curve_num > 0) {
85 this->curve_offsets = MEM_malloc_arrayN<int>(size_t(this->curve_num) + 1, __func__);
86 this->runtime->curve_offsets_sharing_info = implicit_sharing::info_for_mem_free(
87 this->curve_offsets);
88#ifndef NDEBUG
89 this->offsets_for_write().fill(-1);
90#endif
91 /* Set common values for convenience. */
92 this->curve_offsets[0] = 0;
93 this->curve_offsets[this->curve_num] = this->point_num;
94 }
95 else {
96 this->curve_offsets = nullptr;
97 }
98
99 /* Fill the type counts with the default so they're in a valid state. */
100 this->runtime->type_counts[CURVE_TYPE_CATMULL_ROM] = curve_num;
101}
102
103CurvesGeometry::CurvesGeometry(const CurvesGeometry &other)
104{
105 this->curve_offsets = other.curve_offsets;
106 if (other.runtime->curve_offsets_sharing_info) {
107 other.runtime->curve_offsets_sharing_info->add_user();
108 }
109
110 this->custom_knots = other.custom_knots;
111 this->custom_knot_num = other.custom_knot_num;
112 if (other.runtime->custom_knots_sharing_info) {
113 other.runtime->custom_knots_sharing_info->add_user();
114 }
115
116 CustomData_init_from(&other.point_data, &this->point_data, CD_MASK_ALL, other.point_num);
117 CustomData_init_from(&other.curve_data, &this->curve_data, CD_MASK_ALL, other.curve_num);
118
119 new (&this->attribute_storage.wrap()) AttributeStorage(other.attribute_storage.wrap());
120
121 this->point_num = other.point_num;
122 this->curve_num = other.curve_num;
123
126
128
129 this->runtime = MEM_new<CurvesGeometryRuntime>(
130 __func__,
131 CurvesGeometryRuntime{other.runtime->curve_offsets_sharing_info,
132 other.runtime->custom_knots_sharing_info,
133 other.runtime->type_counts,
134 other.runtime->evaluated_offsets_cache,
135 other.runtime->nurbs_basis_cache,
136 other.runtime->evaluated_position_cache,
137 other.runtime->bounds_cache,
138 other.runtime->bounds_with_radius_cache,
139 other.runtime->evaluated_length_cache,
140 other.runtime->evaluated_tangent_cache,
141 other.runtime->evaluated_normal_cache,
142 other.runtime->max_material_index_cache,
143 other.runtime->custom_knot_offsets_cache,
144 {},
145 true});
146
147 if (other.runtime->bake_materials) {
148 this->runtime->bake_materials = std::make_unique<bake::BakeMaterialsList>(
149 *other.runtime->bake_materials);
150 }
151}
152
153CurvesGeometry &CurvesGeometry::operator=(const CurvesGeometry &other)
154{
155 if (this == &other) {
156 return *this;
157 }
158 std::destroy_at(this);
159 new (this) CurvesGeometry(other);
160 return *this;
161}
162
163CurvesGeometry::CurvesGeometry(CurvesGeometry &&other)
164{
165 this->curve_offsets = other.curve_offsets;
166 other.curve_offsets = nullptr;
167
168 this->custom_knots = other.custom_knots;
169 other.custom_knots = nullptr;
170
171 this->custom_knot_num = other.custom_knot_num;
172 other.custom_knot_num = 0;
173
174 this->point_data = other.point_data;
175 CustomData_reset(&other.point_data);
176
177 this->curve_data = other.curve_data;
178 CustomData_reset(&other.curve_data);
179
180 new (&this->attribute_storage.wrap())
181 AttributeStorage(std::move(other.attribute_storage.wrap()));
182
183 this->point_num = other.point_num;
184 other.point_num = 0;
185
186 this->curve_num = other.curve_num;
187 other.curve_num = 0;
188
189 this->vertex_group_names = other.vertex_group_names;
190 BLI_listbase_clear(&other.vertex_group_names);
191
192 this->vertex_group_active_index = other.vertex_group_active_index;
193 other.vertex_group_active_index = 0;
194
195 this->attributes_active_index = other.attributes_active_index;
196 other.attributes_active_index = 0;
197
198 this->runtime = other.runtime;
199 other.runtime = nullptr;
200}
201
202CurvesGeometry &CurvesGeometry::operator=(CurvesGeometry &&other)
203{
204 if (this == &other) {
205 return *this;
206 }
207 std::destroy_at(this);
208 new (this) CurvesGeometry(std::move(other));
209 return *this;
210}
211
212CurvesGeometry::~CurvesGeometry()
213{
216 this->attribute_storage.wrap().~AttributeStorage();
218 if (this->runtime) {
220 &this->runtime->curve_offsets_sharing_info);
222 &this->runtime->custom_knots_sharing_info);
223 MEM_delete(this->runtime);
224 }
225}
226
228
229/* -------------------------------------------------------------------- */
232
233static int domain_num(const CurvesGeometry &curves, const AttrDomain domain)
234{
235 return domain == AttrDomain::Point ? curves.points_num() : curves.curves_num();
236}
237
239{
240 return domain == AttrDomain::Point ? curves.point_data : curves.curve_data;
241}
242
244{
245 return domain == AttrDomain::Point ? curves.point_data : curves.curve_data;
246}
247
248template<typename T>
250 const AttrDomain domain,
251 const StringRef name,
252 const T default_value)
253{
254 const int num = domain_num(curves, domain);
256 const CustomData &custom_data = domain_custom_data(curves, domain);
257
258 const T *data = (const T *)CustomData_get_layer_named(&custom_data, type, name);
259 if (data != nullptr) {
261 }
262 return VArray<T>::ForSingle(default_value, num);
263}
264
265template<typename T>
267 const AttrDomain domain,
268 const StringRef name)
269{
270 const int num = domain_num(curves, domain);
271 const CustomData &custom_data = domain_custom_data(curves, domain);
273
274 T *data = (T *)CustomData_get_layer_named(&custom_data, type, name);
275 if (data == nullptr) {
276 return {};
277 }
278 return {data, num};
279}
280
281template<typename T>
283 const AttrDomain domain,
284 const StringRef name,
285 const T default_value = T())
286{
287 const int num = domain_num(curves, domain);
288 if (num <= 0) {
289 return {};
290 }
292 CustomData &custom_data = domain_custom_data(curves, domain);
293
294 T *data = (T *)CustomData_get_layer_named_for_write(&custom_data, type, name, num);
295 if (data != nullptr) {
296 return {data, num};
297 }
298 data = (T *)CustomData_add_layer_named(&custom_data, type, CD_SET_DEFAULT, num, name);
299 MutableSpan<T> span = {data, num};
300 if (num > 0 && span.first() != default_value) {
301 span.fill(default_value);
302 }
303 return span;
304}
305
306VArray<int8_t> CurvesGeometry::curve_types() const
307{
310}
311
312MutableSpan<int8_t> CurvesGeometry::curve_types_for_write()
313{
315}
316
317void CurvesGeometry::fill_curve_types(const CurveType type)
318{
319 if (type == CURVE_TYPE_CATMULL_ROM) {
320 /* Avoid creating the attribute for Catmull Rom which is the default when the attribute doesn't
321 * exist anyway. */
322 this->attributes_for_write().remove("curve_type");
323 }
324 else {
325 this->curve_types_for_write().fill(type);
326 }
327 this->runtime->type_counts.fill(0);
328 this->runtime->type_counts[type] = this->curves_num();
329 this->tag_topology_changed();
330}
331
332void CurvesGeometry::fill_curve_types(const IndexMask &selection, const CurveType type)
333{
334 if (selection.size() == this->curves_num()) {
335 this->fill_curve_types(type);
336 return;
337 }
338 if (std::optional<int8_t> single_type = this->curve_types().get_if_single()) {
339 if (single_type == type) {
340 this->fill_curve_types(type);
341 return;
342 }
343 }
344 /* A potential performance optimization is only counting the changed indices. */
346 this->update_curve_types();
347 this->tag_topology_changed();
348}
349
350static bool is_valid_curve_type(const int8_t type)
351{
352 return type >= 0 && type < CURVE_TYPES_NUM;
353}
354
356static bool curve_types_are_valid(const VArray<int8_t> &types)
357{
358 if (const std::optional<int8_t> single = types.get_if_single()) {
359 return is_valid_curve_type(*single);
360 }
361 const Span<int8_t> types_span = types.get_internal_span();
363 types_span.index_range(),
364 2048,
365 true,
366 [&](const IndexRange curves_range, const bool &init) {
367 if (!init) {
368 return false;
369 }
370 for (const int64_t curve_index : curves_range) {
371 if (!is_valid_curve_type(types_span[curve_index])) {
372 return false;
373 }
374 }
375 return true;
376 },
377 [](const bool &a, const bool &b) { return a && b; });
378}
379
382{
383 for (int8_t &type : types) {
384 if (!is_valid_curve_type(type)) {
385 type = CURVE_TYPE_POLY;
386 }
387 }
388}
389
390std::array<int, CURVE_TYPES_NUM> calculate_type_counts(const VArray<int8_t> &types)
391{
392 using CountsType = std::array<int, CURVE_TYPES_NUM>;
393 CountsType counts;
394 counts.fill(0);
395
396 if (types.is_single()) {
397 counts[types.get_internal_single()] = types.size();
398 return counts;
399 }
400
401 Span<int8_t> types_span = types.get_internal_span();
403 types.index_range(),
404 2048,
405 counts,
406 [&](const IndexRange curves_range, const CountsType &init) {
407 CountsType result = init;
408 for (const int curve_index : curves_range) {
409 result[types_span[curve_index]]++;
410 }
411 return result;
412 },
413 [](const CountsType &a, const CountsType &b) {
414 CountsType result = a;
415 for (const int i : IndexRange(CURVE_TYPES_NUM)) {
416 result[i] += b[i];
417 }
418 return result;
419 });
420}
421
422void CurvesGeometry::update_curve_types()
423{
424 this->runtime->type_counts = calculate_type_counts(this->curve_types());
425}
426
427Span<float3> CurvesGeometry::positions() const
428{
430}
431MutableSpan<float3> CurvesGeometry::positions_for_write()
432{
434}
435
436VArray<float> CurvesGeometry::radius() const
437{
439}
440MutableSpan<float> CurvesGeometry::radius_for_write()
441{
443}
444
445Span<int> CurvesGeometry::offsets() const
446{
447 if (this->curve_num == 0) {
448 return {};
449 }
450 return {this->curve_offsets, this->curve_num + 1};
451}
452MutableSpan<int> CurvesGeometry::offsets_for_write()
453{
454 if (this->curve_num == 0) {
455 return {};
456 }
458 &this->curve_offsets, &this->runtime->curve_offsets_sharing_info, this->curve_num + 1);
459 return {this->curve_offsets, this->curve_num + 1};
460}
461
462VArray<bool> CurvesGeometry::cyclic() const
463{
465}
466MutableSpan<bool> CurvesGeometry::cyclic_for_write()
467{
469}
470
471VArray<int> CurvesGeometry::resolution() const
472{
474}
475MutableSpan<int> CurvesGeometry::resolution_for_write()
476{
478}
479
480VArray<int8_t> CurvesGeometry::normal_mode() const
481{
483}
484MutableSpan<int8_t> CurvesGeometry::normal_mode_for_write()
485{
487}
488
489VArray<float> CurvesGeometry::tilt() const
490{
492}
493MutableSpan<float> CurvesGeometry::tilt_for_write()
494{
496}
497
498VArray<int8_t> CurvesGeometry::handle_types_left() const
499{
501}
502MutableSpan<int8_t> CurvesGeometry::handle_types_left_for_write()
503{
505}
506
507VArray<int8_t> CurvesGeometry::handle_types_right() const
508{
510}
511MutableSpan<int8_t> CurvesGeometry::handle_types_right_for_write()
512{
514}
515
516Span<float3> CurvesGeometry::handle_positions_left() const
517{
519}
520MutableSpan<float3> CurvesGeometry::handle_positions_left_for_write()
521{
523}
524
525Span<float3> CurvesGeometry::handle_positions_right() const
526{
528}
529MutableSpan<float3> CurvesGeometry::handle_positions_right_for_write()
530{
532}
533
534VArray<int8_t> CurvesGeometry::nurbs_orders() const
535{
537}
538MutableSpan<int8_t> CurvesGeometry::nurbs_orders_for_write()
539{
541}
542
543Span<float> CurvesGeometry::nurbs_weights() const
544{
546}
547MutableSpan<float> CurvesGeometry::nurbs_weights_for_write()
548{
550}
551
552VArray<int8_t> CurvesGeometry::nurbs_knots_modes() const
553{
555}
556MutableSpan<int8_t> CurvesGeometry::nurbs_knots_modes_for_write()
557{
559}
560
561Span<float2> CurvesGeometry::surface_uv_coords() const
562{
564}
565
566MutableSpan<float2> CurvesGeometry::surface_uv_coords_for_write()
567{
569}
570
571Span<float> CurvesGeometry::nurbs_custom_knots() const
572{
573 if (this->custom_knot_num == 0) {
574 return {};
575 }
576 return {this->custom_knots, this->custom_knot_num};
577}
578
579MutableSpan<float> CurvesGeometry::nurbs_custom_knots_for_write()
580{
581 if (this->custom_knot_num == 0) {
582 return {};
583 }
585 &this->custom_knots, &this->runtime->custom_knots_sharing_info, this->custom_knot_num);
586 return {this->custom_knots, this->custom_knot_num};
587}
588
589IndexMask CurvesGeometry::nurbs_custom_knot_curves(IndexMaskMemory &memory) const
590{
591 const VArray<int8_t> curve_types = this->curve_types();
592 const VArray<int8_t> knot_modes = this->nurbs_knots_modes();
594 this->curves_range(), GrainSize(4096), memory, [&](const int64_t curve) {
595 return curve_types[curve] == CURVE_TYPE_NURBS &&
596 knot_modes[curve] == NURBS_KNOT_MODE_CUSTOM;
597 });
598}
599
600OffsetIndices<int> CurvesGeometry::nurbs_custom_knots_by_curve() const
601{
602 const CurvesGeometryRuntime &runtime = *this->runtime;
603 if (this->is_empty()) {
604 return {};
605 }
606 runtime.custom_knot_offsets_cache.ensure([&](Vector<int> &r_data) {
607 r_data.resize(this->curve_num + 1, 0);
608
609 const OffsetIndices points_by_curve = this->points_by_curve();
610 const VArray<int8_t> knot_modes = this->nurbs_knots_modes();
611 const VArray<int8_t> orders = this->nurbs_orders();
612
613 int knot_count = 0;
614 for (const int curve : this->curves_range()) {
615 knot_count += knot_modes[curve] == NURBS_KNOT_MODE_CUSTOM ?
616 points_by_curve[curve].size() + orders[curve] :
617 0;
618 r_data[curve + 1] = knot_count;
619 }
620 });
622}
623
624void CurvesGeometry::nurbs_custom_knots_update_size()
625{
626 this->runtime->custom_knot_offsets_cache.tag_dirty();
627 const OffsetIndices<int> knots_by_curve = this->nurbs_custom_knots_by_curve();
628 const int knots_num = knots_by_curve.total_size();
629 if (this->custom_knot_num != knots_num) {
631 &this->runtime->custom_knots_sharing_info,
632 this->custom_knot_num,
633 knots_num);
634 this->custom_knot_num = knots_num;
635 }
636}
637
638void CurvesGeometry::nurbs_custom_knots_resize(int knots_num)
639{
641 &this->runtime->custom_knots_sharing_info,
642 this->custom_knot_num,
643 knots_num);
644 this->custom_knot_num = knots_num;
645 this->runtime->custom_knot_offsets_cache.tag_dirty();
646}
647
648Span<MDeformVert> CurvesGeometry::deform_verts() const
649{
650 const MDeformVert *dverts = static_cast<const MDeformVert *>(
652 if (dverts == nullptr) {
653 return {};
654 }
655 return {dverts, this->point_num};
656}
657
658MutableSpan<MDeformVert> CurvesGeometry::deform_verts_for_write()
659{
660 MDeformVert *dvert = static_cast<MDeformVert *>(
662 if (dvert != nullptr) {
663 return {dvert, this->point_num};
664 }
665 return {static_cast<MDeformVert *>(CustomData_add_layer(
667 this->point_num};
668}
669
671
672/* -------------------------------------------------------------------- */
675
676template<typename CountFn> void build_offsets(MutableSpan<int> offsets, const CountFn &count_fn)
677{
678 int offset = 0;
679 for (const int i : offsets.drop_back(1).index_range()) {
680 offsets[i] = offset;
681 offset += count_fn(i);
682 }
683 offsets.last() = offset;
684}
685
687 MutableSpan<int> offsets,
688 MutableSpan<int> all_bezier_offsets)
689{
690 const OffsetIndices points_by_curve = curves.points_by_curve();
691 const VArray<int8_t> types = curves.curve_types();
692 const VArray<int> resolution = curves.resolution();
693 const VArray<bool> cyclic = curves.cyclic();
694
695 VArraySpan<int8_t> handle_types_left;
696 VArraySpan<int8_t> handle_types_right;
697 if (curves.has_curve_with_type(CURVE_TYPE_BEZIER)) {
698 handle_types_left = curves.handle_types_left();
699 handle_types_right = curves.handle_types_right();
700 }
701
702 const VArray<int8_t> nurbs_orders = curves.nurbs_orders();
703 const VArray<int8_t> nurbs_knots_modes = curves.nurbs_knots_modes();
704
705 build_offsets(offsets, [&](const int curve_index) -> int {
706 const IndexRange points = points_by_curve[curve_index];
707 switch (types[curve_index]) {
710 points.size(), cyclic[curve_index], resolution[curve_index]);
711 case CURVE_TYPE_POLY:
712 return points.size();
713 case CURVE_TYPE_BEZIER: {
714 const IndexRange offsets = curves::per_curve_point_offsets_range(points, curve_index);
715 curves::bezier::calculate_evaluated_offsets(handle_types_left.slice(points),
716 handle_types_right.slice(points),
717 cyclic[curve_index],
718 resolution[curve_index],
719 all_bezier_offsets.slice(offsets));
720 return all_bezier_offsets[offsets.last()];
721 }
722 case CURVE_TYPE_NURBS:
724 nurbs_orders[curve_index],
725 cyclic[curve_index],
726 resolution[curve_index],
727 KnotsMode(nurbs_knots_modes[curve_index]));
728 }
730 return 0;
731 });
732}
733
734OffsetIndices<int> CurvesGeometry::evaluated_points_by_curve() const
735{
736 const CurvesGeometryRuntime &runtime = *this->runtime;
737 if (this->is_single_type(CURVE_TYPE_POLY)) {
738 /* When all the curves are poly curves, the evaluated offsets are the same as the control
739 * point offsets, so it's possible to completely avoid building a new offsets array. */
743 });
744 return this->points_by_curve();
745 }
746
748 r_data.evaluated_offsets.resize(this->curves_num() + 1);
749
751 r_data.all_bezier_offsets.resize(this->points_num() + this->curves_num());
752 }
753 else {
755 }
756
758 });
759
760 return OffsetIndices<int>(runtime.evaluated_offsets_cache.data().evaluated_offsets);
761}
762
763IndexMask CurvesGeometry::indices_for_curve_type(const CurveType type,
764 IndexMaskMemory &memory) const
765{
766 return this->indices_for_curve_type(type, this->curves_range(), memory);
767}
768
769IndexMask CurvesGeometry::indices_for_curve_type(const CurveType type,
770 const IndexMask &selection,
771 IndexMaskMemory &memory) const
772{
774 this->curve_types(), this->curve_type_counts(), type, selection, memory);
775}
776
777Array<int> CurvesGeometry::point_to_curve_map() const
778{
779 Array<int> map(this->points_num());
781 return map;
782}
783
784void CurvesGeometry::ensure_nurbs_basis_cache() const
785{
786 const CurvesGeometryRuntime &runtime = *this->runtime;
787 runtime.nurbs_basis_cache.ensure([&](Vector<curves::nurbs::BasisCache> &r_data) {
788 IndexMaskMemory memory;
789 const IndexMask nurbs_mask = this->indices_for_curve_type(CURVE_TYPE_NURBS, memory);
790 if (nurbs_mask.is_empty()) {
791 r_data.clear_and_shrink();
792 return;
793 }
794
795 r_data.resize(this->curves_num());
796
797 const OffsetIndices<int> points_by_curve = this->points_by_curve();
798 const OffsetIndices<int> evaluated_points_by_curve = this->evaluated_points_by_curve();
799 const OffsetIndices<int> custom_knots_by_curve = this->nurbs_custom_knots_by_curve();
800 const VArray<bool> cyclic = this->cyclic();
801 const VArray<int8_t> orders = this->nurbs_orders();
802 const VArray<int8_t> knots_modes = this->nurbs_knots_modes();
803 const Span<float> custom_knots = this->nurbs_custom_knots();
804
805 nurbs_mask.foreach_segment(GrainSize(64), [&](const IndexMaskSegment segment) {
806 Vector<float, 32> knots;
807 for (const int curve_index : segment) {
808 const IndexRange points = points_by_curve[curve_index];
809 const IndexRange evaluated_points = evaluated_points_by_curve[curve_index];
810
811 const int8_t order = orders[curve_index];
812 const bool is_cyclic = cyclic[curve_index];
813 const KnotsMode mode = KnotsMode(knots_modes[curve_index]);
814
815 if (!curves::nurbs::check_valid_num_and_order(points.size(), order, is_cyclic, mode)) {
816 r_data[curve_index].invalid = true;
817 continue;
818 }
819 const int knots_num = curves::nurbs::knots_num(points.size(), order, is_cyclic);
820 knots.reinitialize(knots_num);
821 curves::nurbs::load_curve_knots(mode,
822 points.size(),
823 order,
824 is_cyclic,
825 custom_knots_by_curve[curve_index],
826 custom_knots,
827 knots);
828
829 curves::nurbs::calculate_basis_cache(
830 points.size(), evaluated_points.size(), order, is_cyclic, knots, r_data[curve_index]);
831 }
832 });
833 });
834}
835
836Span<float3> CurvesGeometry::evaluated_positions() const
837{
838 const CurvesGeometryRuntime &runtime = *this->runtime;
839 if (this->is_single_type(CURVE_TYPE_POLY)) {
841 [&](Vector<float3> &r_data) { r_data.clear_and_shrink(); });
842 return this->positions();
843 }
844 this->ensure_nurbs_basis_cache();
846 r_data.resize(this->evaluated_points_num());
848
849 const OffsetIndices<int> points_by_curve = this->points_by_curve();
850 const OffsetIndices<int> evaluated_points_by_curve = this->evaluated_points_by_curve();
851 const Span<float3> positions = this->positions();
852
853 auto evaluate_catmull = [&](const IndexMask &selection) {
854 const VArray<bool> cyclic = this->cyclic();
855 const VArray<int> resolution = this->resolution();
856 selection.foreach_index(GrainSize(128), [&](const int curve_index) {
857 const IndexRange points = points_by_curve[curve_index];
858 const IndexRange evaluated_points = evaluated_points_by_curve[curve_index];
860 cyclic[curve_index],
861 resolution[curve_index],
862 evaluated_positions.slice(evaluated_points));
863 });
864 };
865 auto evaluate_poly = [&](const IndexMask &selection) {
868 };
869 auto evaluate_bezier = [&](const IndexMask &selection) {
870 const Span<float3> handle_positions_left = this->handle_positions_left();
871 const Span<float3> handle_positions_right = this->handle_positions_right();
874 return;
875 }
876 const Span<int> all_bezier_offsets =
877 runtime.evaluated_offsets_cache.data().all_bezier_offsets;
878 selection.foreach_index(GrainSize(128), [&](const int curve_index) {
879 const IndexRange points = points_by_curve[curve_index];
880 const IndexRange evaluated_points = evaluated_points_by_curve[curve_index];
881 const IndexRange offsets = curves::per_curve_point_offsets_range(points, curve_index);
885 all_bezier_offsets.slice(offsets),
886 evaluated_positions.slice(evaluated_points));
887 });
888 };
889 auto evaluate_nurbs = [&](const IndexMask &selection) {
890 this->ensure_nurbs_basis_cache();
891 const VArray<int8_t> nurbs_orders = this->nurbs_orders();
892 const Span<float> nurbs_weights = this->nurbs_weights();
893 const Span<curves::nurbs::BasisCache> nurbs_basis_cache = runtime.nurbs_basis_cache.data();
894 selection.foreach_index(GrainSize(128), [&](const int curve_index) {
895 const IndexRange points = points_by_curve[curve_index];
896 const IndexRange evaluated_points = evaluated_points_by_curve[curve_index];
897 curves::nurbs::interpolate_to_evaluated(nurbs_basis_cache[curve_index],
898 nurbs_orders[curve_index],
900 positions.slice(points),
901 evaluated_positions.slice(evaluated_points));
902 });
903 };
905 this->curve_type_counts(),
906 this->curves_range(),
907 evaluate_catmull,
908 evaluate_poly,
909 evaluate_bezier,
910 evaluate_nurbs);
911 });
912 return runtime.evaluated_position_cache.data();
913}
914
915Span<float3> CurvesGeometry::evaluated_tangents() const
916{
917 const CurvesGeometryRuntime &runtime = *this->runtime;
919 const OffsetIndices<int> evaluated_points_by_curve = this->evaluated_points_by_curve();
920 const Span<float3> evaluated_positions = this->evaluated_positions();
921 const VArray<bool> cyclic = this->cyclic();
922
923 r_data.resize(this->evaluated_points_num());
924 MutableSpan<float3> tangents = r_data;
925
927 for (const int curve_index : curves_range) {
928 const IndexRange evaluated_points = evaluated_points_by_curve[curve_index];
930 cyclic[curve_index],
931 tangents.slice(evaluated_points));
932 }
933 });
934
935 /* Correct the first and last tangents of non-cyclic Bezier curves so that they align with
936 * the inner handles. This is a separate loop to avoid the cost when Bezier type curves are
937 * not used. */
938 IndexMaskMemory memory;
939 const IndexMask bezier_mask = this->indices_for_curve_type(CURVE_TYPE_BEZIER, memory);
940 if (!bezier_mask.is_empty()) {
941 const OffsetIndices<int> points_by_curve = this->points_by_curve();
942 const Span<float3> positions = this->positions();
943 const Span<float3> handles_left = this->handle_positions_left();
944 const Span<float3> handles_right = this->handle_positions_right();
945
946 bezier_mask.foreach_index(GrainSize(1024), [&](const int curve_index) {
947 if (cyclic[curve_index]) {
948 return;
949 }
950 const IndexRange points = points_by_curve[curve_index];
951 const IndexRange evaluated_points = evaluated_points_by_curve[curve_index];
952
953 const float epsilon = 1e-6f;
955 handles_right[points.first()], positions[points.first()], epsilon))
956 {
957 tangents[evaluated_points.first()] = math::normalize(handles_right[points.first()] -
958 positions[points.first()]);
959 }
961 handles_left[points.last()], positions[points.last()], epsilon))
962 {
963 tangents[evaluated_points.last()] = math::normalize(positions[points.last()] -
964 handles_left[points.last()]);
965 }
966 });
967 }
968 });
969 return runtime.evaluated_tangent_cache.data();
970}
971
973 const Span<float3> axes,
974 const Span<float> angles)
975{
976 for (const int i : directions.index_range()) {
977 const float3 axis = axes[i];
978 if (UNLIKELY(math::is_zero(axis))) {
979 continue;
980 }
981 directions[i] = math::rotate_direction_around_axis(directions[i], axis, angles[i]);
982 }
983}
984
986{
987 for (const int i : data.index_range()) {
989 }
990}
991
1003
1004static void evaluate_generic_data_for_curve(const EvalData &eval_data,
1005 const int curve_index,
1006 const GSpan src,
1007 GMutableSpan dst)
1008{
1009 const IndexRange points = eval_data.points_by_curve[curve_index];
1010 switch (eval_data.types[curve_index]) {
1013 src, eval_data.cyclic[curve_index], eval_data.resolution[curve_index], dst);
1014 break;
1015 case CURVE_TYPE_POLY:
1016 dst.copy_from(src);
1017 break;
1018 case CURVE_TYPE_BEZIER: {
1019 const IndexRange offsets = curves::per_curve_point_offsets_range(points, curve_index);
1021 src, eval_data.all_bezier_evaluated_offsets.slice(offsets), dst);
1022 break;
1023 }
1024 case CURVE_TYPE_NURBS:
1026 eval_data.nurbs_orders[curve_index],
1027 eval_data.nurbs_weights.slice_safe(points),
1028 src,
1029 dst);
1030 break;
1031 }
1032}
1033
1034Span<float3> CurvesGeometry::evaluated_normals() const
1035{
1036 const CurvesGeometryRuntime &runtime = *this->runtime;
1037 this->ensure_nurbs_basis_cache();
1038 runtime.evaluated_normal_cache.ensure([&](Vector<float3> &r_data) {
1039 const OffsetIndices<int> points_by_curve = this->points_by_curve();
1040 const OffsetIndices<int> evaluated_points_by_curve = this->evaluated_points_by_curve();
1041 const VArray<int8_t> types = this->curve_types();
1042 const VArray<bool> cyclic = this->cyclic();
1043 const VArray<int8_t> normal_mode = this->normal_mode();
1044 const Span<float3> evaluated_tangents = this->evaluated_tangents();
1045 const AttributeAccessor attributes = this->attributes();
1046 const EvalData eval_data{
1048 types,
1049 cyclic,
1050 this->resolution(),
1051 runtime.evaluated_offsets_cache.data().all_bezier_offsets,
1053 this->nurbs_orders(),
1054 this->nurbs_weights(),
1055 };
1056 const VArray<float> tilt = this->tilt();
1057 VArraySpan<float> tilt_span;
1058 const bool use_tilt = !(tilt.is_single() && tilt.get_internal_single() == 0.0f);
1059 if (use_tilt) {
1060 tilt_span = tilt;
1061 }
1062 VArraySpan<float3> custom_normal_span;
1063 if (const VArray<float3> custom_normal = *attributes.lookup<float3>("custom_normal",
1065 {
1066 custom_normal_span = custom_normal;
1067 }
1068
1069 r_data.resize(this->evaluated_points_num());
1071
1073 /* Reuse a buffer for the evaluated tilts. */
1074 Vector<float> evaluated_tilts;
1075
1076 for (const int curve_index : curves_range) {
1077 const IndexRange evaluated_points = evaluated_points_by_curve[curve_index];
1078 switch (NormalMode(normal_mode[curve_index])) {
1079 case NORMAL_MODE_Z_UP:
1081 evaluated_normals.slice(evaluated_points));
1082 break;
1085 cyclic[curve_index],
1086 evaluated_normals.slice(evaluated_points));
1087 break;
1088 case NORMAL_MODE_FREE:
1089 if (custom_normal_span.is_empty()) {
1091 evaluated_normals.slice(evaluated_points));
1092 }
1093 else {
1094 const Span<float3> src = custom_normal_span.slice(points_by_curve[curve_index]);
1096 evaluated_points_by_curve[curve_index]);
1097 evaluate_generic_data_for_curve(eval_data, curve_index, src, dst);
1098 normalize_span(dst);
1099 }
1100 break;
1101 }
1102
1103 /* If the "tilt" attribute exists, rotate the normals around the tangents by the
1104 * evaluated angles. We can avoid copying the tilts to evaluate them for poly curves. */
1105 if (use_tilt) {
1106 const IndexRange points = points_by_curve[curve_index];
1107 if (types[curve_index] == CURVE_TYPE_POLY) {
1109 evaluated_tangents.slice(evaluated_points),
1110 tilt_span.slice(points));
1111 }
1112 else {
1113 evaluated_tilts.reinitialize(evaluated_points.size());
1115 curve_index,
1116 tilt_span.slice(points),
1117 evaluated_tilts.as_mutable_span());
1119 evaluated_tangents.slice(evaluated_points),
1120 evaluated_tilts.as_span());
1121 }
1122 }
1123 }
1124 });
1125 });
1126 return this->runtime->evaluated_normal_cache.data();
1127}
1128
1129void CurvesGeometry::interpolate_to_evaluated(const int curve_index,
1130 const GSpan src,
1131 GMutableSpan dst) const
1132{
1133 const CurvesGeometryRuntime &runtime = *this->runtime;
1134 const EvalData eval_data{
1135 this->points_by_curve(),
1136 this->curve_types(),
1137 this->cyclic(),
1138 this->resolution(),
1139 runtime.evaluated_offsets_cache.data().all_bezier_offsets,
1141 this->nurbs_orders(),
1142 this->nurbs_weights(),
1143 };
1144 BLI_assert(src.size() == this->points_by_curve()[curve_index].size());
1145 BLI_assert(dst.size() == this->evaluated_points_by_curve()[curve_index].size());
1146 evaluate_generic_data_for_curve(eval_data, curve_index, src, dst);
1147}
1148
1149void CurvesGeometry::interpolate_to_evaluated(const GSpan src, GMutableSpan dst) const
1150{
1151 const CurvesGeometryRuntime &runtime = *this->runtime;
1152 const OffsetIndices points_by_curve = this->points_by_curve();
1153 const EvalData eval_data{
1155 this->curve_types(),
1156 this->cyclic(),
1157 this->resolution(),
1158 runtime.evaluated_offsets_cache.data().all_bezier_offsets,
1160 this->nurbs_orders(),
1161 this->nurbs_weights(),
1162 };
1163 const OffsetIndices evaluated_points_by_curve = this->evaluated_points_by_curve();
1164
1166 for (const int curve_index : curves_range) {
1167 const IndexRange points = points_by_curve[curve_index];
1168 const IndexRange evaluated_points = evaluated_points_by_curve[curve_index];
1170 eval_data, curve_index, src.slice(points), dst.slice(evaluated_points));
1171 }
1172 });
1173}
1174
1175void CurvesGeometry::ensure_evaluated_lengths() const
1176{
1177 const CurvesGeometryRuntime &runtime = *this->runtime;
1178 runtime.evaluated_length_cache.ensure([&](Vector<float> &r_data) {
1179 /* Use an extra length value for the final cyclic segment for a consistent size
1180 * (see comment on #evaluated_length_cache). */
1181 const int total_num = this->evaluated_points_num() + this->curves_num();
1182 r_data.resize(total_num);
1183 MutableSpan<float> evaluated_lengths = r_data;
1184
1185 const OffsetIndices<int> evaluated_points_by_curve = this->evaluated_points_by_curve();
1186 const Span<float3> evaluated_positions = this->evaluated_positions();
1187 const VArray<bool> curves_cyclic = this->cyclic();
1188
1190 for (const int curve_index : curves_range) {
1191 const bool cyclic = curves_cyclic[curve_index];
1192 const IndexRange evaluated_points = evaluated_points_by_curve[curve_index];
1193 const IndexRange lengths_range = this->lengths_range_for_curve(curve_index, cyclic);
1195 cyclic,
1196 evaluated_lengths.slice(lengths_range));
1197 }
1198 });
1199 });
1200}
1201
1202void CurvesGeometry::ensure_can_interpolate_to_evaluated() const
1203{
1204 this->evaluated_points_by_curve();
1205 this->ensure_nurbs_basis_cache();
1206}
1207
1209
1210/* -------------------------------------------------------------------- */
1213
1214void CurvesGeometry::resize(const int points_num, const int curves_num)
1215{
1216 BLI_assert(curves_num >= 0 && points_num >= 0);
1217 if (points_num != this->point_num) {
1218 CustomData_realloc(&this->point_data, this->points_num(), points_num);
1219 this->point_num = points_num;
1220 }
1221 if (curves_num != this->curve_num) {
1222 CustomData_realloc(&this->curve_data, this->curves_num(), curves_num);
1224 &this->runtime->curve_offsets_sharing_info,
1225 this->curve_num == 0 ? 0 : (this->curve_num + 1),
1226 curves_num == 0 ? 0 : (curves_num + 1));
1227 if (curves_num > 0) {
1228 /* Set common values for convenience. */
1229 this->curve_offsets[0] = 0;
1230 this->curve_offsets[curves_num] = this->point_num;
1231 }
1232 this->curve_num = curves_num;
1233 }
1234 this->tag_topology_changed();
1235}
1236
1237void CurvesGeometry::tag_positions_changed()
1238{
1239 this->runtime->evaluated_position_cache.tag_dirty();
1240 this->runtime->evaluated_tangent_cache.tag_dirty();
1241 this->runtime->evaluated_normal_cache.tag_dirty();
1242 this->runtime->evaluated_length_cache.tag_dirty();
1243 this->runtime->bounds_cache.tag_dirty();
1244 this->runtime->bounds_with_radius_cache.tag_dirty();
1245}
1246void CurvesGeometry::tag_topology_changed()
1247{
1248 this->runtime->custom_knot_offsets_cache.tag_dirty();
1249 this->tag_positions_changed();
1250 this->runtime->evaluated_offsets_cache.tag_dirty();
1251 this->runtime->nurbs_basis_cache.tag_dirty();
1252 this->runtime->max_material_index_cache.tag_dirty();
1253 this->runtime->check_type_counts = true;
1254}
1255void CurvesGeometry::tag_normals_changed()
1256{
1257 this->runtime->evaluated_normal_cache.tag_dirty();
1258}
1259void CurvesGeometry::tag_radii_changed()
1260{
1261 this->runtime->bounds_with_radius_cache.tag_dirty();
1262}
1263void CurvesGeometry::tag_material_index_changed()
1264{
1265 this->runtime->max_material_index_cache.tag_dirty();
1266}
1267
1268static void translate_positions(MutableSpan<float3> positions, const float3 &translation)
1269{
1270 threading::parallel_for(positions.index_range(), 2048, [&](const IndexRange range) {
1271 for (float3 &position : positions.slice(range)) {
1272 position += translation;
1273 }
1274 });
1275}
1276
1277static void transform_positions(MutableSpan<float3> positions, const float4x4 &matrix)
1278{
1279 threading::parallel_for(positions.index_range(), 1024, [&](const IndexRange range) {
1280 for (float3 &position : positions.slice(range)) {
1281 position = math::transform_point(matrix, position);
1282 }
1283 });
1284}
1285
1286void CurvesGeometry::calculate_bezier_auto_handles()
1287{
1289 return;
1290 }
1292 return;
1293 }
1294 const OffsetIndices points_by_curve = this->points_by_curve();
1295 const VArray<int8_t> types = this->curve_types();
1296 const VArray<bool> cyclic = this->cyclic();
1297 const VArraySpan<int8_t> types_left{this->handle_types_left()};
1298 const VArraySpan<int8_t> types_right{this->handle_types_right()};
1299 const Span<float3> positions = this->positions();
1301 MutableSpan<float3> positions_right = this->handle_positions_right_for_write();
1302
1303 threading::parallel_for(this->curves_range(), 128, [&](IndexRange range) {
1304 for (const int i_curve : range) {
1305 if (types[i_curve] == CURVE_TYPE_BEZIER) {
1306 const IndexRange points = points_by_curve[i_curve];
1308 types_left.slice(points),
1309 types_right.slice(points),
1310 positions.slice(points),
1311 positions_left.slice(points),
1312 positions_right.slice(points));
1313 }
1314 }
1315 });
1316}
1317
1318void CurvesGeometry::translate(const float3 &translation)
1319{
1320 if (math::is_zero(translation)) {
1321 return;
1322 }
1323
1324 std::optional<Bounds<float3>> bounds;
1325 if (this->runtime->bounds_cache.is_cached()) {
1326 bounds = this->runtime->bounds_cache.data();
1327 }
1328
1329 translate_positions(this->positions_for_write(), translation);
1330 if (!this->handle_positions_left().is_empty()) {
1332 }
1333 if (!this->handle_positions_right().is_empty()) {
1335 }
1336 this->tag_positions_changed();
1337
1338 if (bounds) {
1339 bounds->min += translation;
1340 bounds->max += translation;
1341 this->runtime->bounds_cache.ensure([&](blender::Bounds<float3> &r_data) { r_data = *bounds; });
1342 }
1343}
1344
1345void CurvesGeometry::transform(const float4x4 &matrix)
1346{
1348 if (!this->handle_positions_left().is_empty()) {
1350 }
1351 if (!this->handle_positions_right().is_empty()) {
1353 }
1356 this->tag_positions_changed();
1357}
1358
1359std::optional<Bounds<float3>> CurvesGeometry::bounds_min_max(const bool use_radius) const
1360{
1361 if (this->is_empty()) {
1362 return std::nullopt;
1363 }
1364 if (use_radius) {
1365 this->runtime->bounds_with_radius_cache.ensure([&](Bounds<float3> &r_bounds) {
1366 const VArray<float> radius = this->radius();
1367 if (const std::optional radius_single = radius.get_if_single()) {
1368 r_bounds = *this->bounds_min_max(false);
1369 r_bounds.pad(*radius_single);
1370 return;
1371 }
1372 const Span radius_span = radius.get_internal_span();
1373 if (this->is_single_type(CURVE_TYPE_POLY)) {
1374 r_bounds = *bounds::min_max_with_radii(this->positions(), radius_span);
1375 return;
1376 }
1377 Array<float> radii_eval(this->evaluated_points_num());
1379 this->interpolate_to_evaluated(radius_span, radii_eval.as_mutable_span());
1380 r_bounds = *bounds::min_max_with_radii(this->evaluated_positions(), radii_eval.as_span());
1381 });
1382 }
1383 else {
1384 this->runtime->bounds_cache.ensure([&](Bounds<float3> &r_bounds) {
1385 r_bounds = *bounds::min_max(this->evaluated_positions());
1386 });
1387 }
1388 return use_radius ? this->runtime->bounds_with_radius_cache.data() :
1389 this->runtime->bounds_cache.data();
1390}
1391
1392std::optional<int> CurvesGeometry::material_index_max() const
1393{
1394 this->runtime->max_material_index_cache.ensure([&](std::optional<int> &r_max_material_index) {
1395 r_max_material_index = blender::bounds::max<int>(
1396 this->attributes()
1397 .lookup_or_default<int>("material_index", blender::bke::AttrDomain::Curve, 0)
1398 .varray);
1399 if (r_max_material_index.has_value()) {
1400 r_max_material_index = std::clamp(*r_max_material_index, 0, MAXMAT);
1401 }
1402 });
1403 return this->runtime->max_material_index_cache.data();
1404}
1405
1406void CurvesGeometry::count_memory(MemoryCounter &memory) const
1407{
1408 memory.add_shared(this->runtime->curve_offsets_sharing_info, this->offsets().size_in_bytes());
1409 memory.add_shared(this->runtime->custom_knots_sharing_info,
1410 this->nurbs_custom_knots().size_in_bytes());
1411 CustomData_count_memory(this->point_data, this->point_num, memory);
1412 CustomData_count_memory(this->curve_data, this->curve_num, memory);
1413}
1414
1416 const IndexMask &points_to_copy,
1417 const Span<int> curve_point_counts,
1418 CurvesGeometry &dst_curves)
1419{
1420 const OffsetIndices points_by_curve = curves.points_by_curve();
1421 const VArray<int8_t> orders = curves.nurbs_orders();
1422 const VArray<bool> cyclic = curves.cyclic();
1423
1424 IndexMaskMemory memory;
1425 const IndexMask custom_knot_curves = curves.nurbs_custom_knot_curves(memory);
1426 const IndexMask custom_knot_points = bke::curves::curve_to_point_selection(
1427 points_by_curve, custom_knot_curves, memory);
1428 const IndexMask custom_knot_points_to_copy = IndexMask::from_intersection(
1429 points_to_copy, custom_knot_points, memory);
1430
1431 int dst_knot_count = 0;
1432 custom_knot_curves.foreach_index([&](const int64_t curve) {
1433 dst_knot_count += curves::nurbs::knots_num(
1434 curve_point_counts[curve], orders[curve], cyclic[curve]);
1435 });
1436 const OffsetIndices<int> src_knots_by_curve = curves.nurbs_custom_knots_by_curve();
1437 const Span<float> src_knots = curves.nurbs_custom_knots();
1438
1439 Vector<float> new_knots;
1440 new_knots.reserve(dst_knot_count);
1441
1443 custom_knot_points_to_copy,
1444 points_by_curve,
1445 [&](int curve, IndexRange points, Span<IndexRange> ranges_to_copy) {
1446 const IndexRange src_range = src_knots_by_curve[curve];
1447 const int order = orders[curve];
1448 const int leading_spans = order / 2;
1449 const int point_to_knot = -points.start() + src_range.start();
1450 const int point_to_span = point_to_knot + leading_spans;
1451
1452 const int first_knot = ranges_to_copy.first().start() + point_to_knot;
1453 new_knots.extend(
1454 src_knots.slice(IndexRange::from_begin_size(first_knot, leading_spans + 1)));
1455 float last_knot = new_knots.last();
1456 for (IndexRange range : ranges_to_copy) {
1457 for (const int spans_left_knot : range.shift(point_to_span)) {
1458 last_knot += src_knots[spans_left_knot + 1] - src_knots[spans_left_knot];
1459 new_knots.append(last_knot);
1460 }
1461 }
1462 const int last_spans_left_knot = ranges_to_copy.last().last() + point_to_span + 1;
1463 last_knot += src_knots[last_spans_left_knot + 1] - src_knots[last_spans_left_knot];
1464 new_knots.append(last_knot);
1465 });
1466 dst_curves.nurbs_custom_knots_update_size();
1467 dst_curves.nurbs_custom_knots_for_write().copy_from(new_knots);
1468}
1469
1471 const IndexMask &points_to_copy,
1472 const AttributeFilter &attribute_filter)
1473{
1474 const Array<int> point_to_curve_map = curves.point_to_curve_map();
1475 Array<int> curve_point_counts(curves.curves_num(), 0);
1476 points_to_copy.foreach_index(
1477 [&](const int64_t point_i) { curve_point_counts[point_to_curve_map[point_i]]++; });
1478
1479 IndexMaskMemory memory;
1480 const IndexMask curves_to_copy = IndexMask::from_predicate(
1481 curves.curves_range(), GrainSize(4096), memory, [&](const int64_t i) {
1482 return curve_point_counts[i] > 0;
1483 });
1484
1485 CurvesGeometry dst_curves(points_to_copy.size(), curves_to_copy.size());
1486
1487 BKE_defgroup_copy_list(&dst_curves.vertex_group_names, &curves.vertex_group_names);
1488
1490 dst_curves.curves_num() > 1024,
1491 [&]() {
1492 if (curves_to_copy.is_empty()) {
1493 return;
1494 }
1495 MutableSpan<int> new_curve_offsets = dst_curves.offsets_for_write();
1497 curve_point_counts.as_span(), curves_to_copy, new_curve_offsets.drop_back(1));
1499 },
1500 [&]() {
1501 gather_attributes(curves.attributes(),
1502 AttrDomain::Point,
1503 AttrDomain::Point,
1504 attribute_filter,
1505 points_to_copy,
1506 dst_curves.attributes_for_write());
1507 gather_attributes(curves.attributes(),
1508 AttrDomain::Curve,
1509 AttrDomain::Curve,
1510 attribute_filter,
1511 curves_to_copy,
1512 dst_curves.attributes_for_write());
1513 });
1514
1515 if (curves.nurbs_has_custom_knots()) {
1516 copy_point_selection_custom_knots(curves, points_to_copy, curve_point_counts, dst_curves);
1517 }
1518
1519 if (dst_curves.curves_num() == curves.curves_num()) {
1520 dst_curves.runtime->type_counts = curves.runtime->type_counts;
1521 }
1522 else {
1523 dst_curves.remove_attributes_based_on_types();
1524 }
1525
1526 return dst_curves;
1527}
1528
1529void CurvesGeometry::remove_points(const IndexMask &points_to_delete,
1530 const AttributeFilter &attribute_filter)
1531{
1532 if (points_to_delete.is_empty()) {
1533 return;
1534 }
1535 if (points_to_delete.size() == this->points_num()) {
1536 this->resize(0, 0);
1537 this->update_curve_types();
1538 return;
1539 }
1540 IndexMaskMemory memory;
1541 const IndexMask points_to_copy = points_to_delete.complement(this->points_range(), memory);
1542 *this = curves_copy_point_selection(*this, points_to_copy, attribute_filter);
1543}
1544
1546 const IndexMask &curves_to_copy,
1547 CurvesGeometry &dst_curves)
1548{
1549 IndexMaskMemory memory;
1550 const IndexMask custom_knot_curves = curves.nurbs_custom_knot_curves(memory);
1551 const IndexMask custom_knot_curves_to_copy = IndexMask::from_intersection(
1552 curves_to_copy, custom_knot_curves, memory);
1553
1554 Array<int> dst_knot_offsets_data(custom_knot_curves_to_copy.size() + 1, 0);
1555
1556 const OffsetIndices<int> src_knots_by_curve = curves.nurbs_custom_knots_by_curve();
1558 src_knots_by_curve, custom_knot_curves_to_copy, dst_knot_offsets_data);
1559
1560 dst_curves.nurbs_custom_knots_update_size();
1561 array_utils::gather_group_to_group(src_knots_by_curve,
1562 dst_knots_by_curve,
1563 custom_knot_curves_to_copy,
1564 curves.nurbs_custom_knots(),
1565 dst_curves.nurbs_custom_knots_for_write());
1566}
1567
1569 const IndexMask &curves_to_copy,
1570 const AttributeFilter &attribute_filter)
1571{
1572 const OffsetIndices points_by_curve = curves.points_by_curve();
1573 CurvesGeometry dst_curves(0, curves_to_copy.size());
1574 const OffsetIndices dst_points_by_curve = offset_indices::gather_selected_offsets(
1575 points_by_curve, curves_to_copy, dst_curves.offsets_for_write());
1576 dst_curves.resize(dst_points_by_curve.total_size(), dst_curves.curves_num());
1577
1578 BKE_defgroup_copy_list(&dst_curves.vertex_group_names, &curves.vertex_group_names);
1579
1580 const AttributeAccessor src_attributes = curves.attributes();
1581 MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
1582
1583 gather_attributes_group_to_group(src_attributes,
1586 attribute_filter,
1587 points_by_curve,
1588 dst_points_by_curve,
1589 curves_to_copy,
1590 dst_attributes);
1591
1592 gather_attributes(src_attributes,
1595 attribute_filter,
1596 curves_to_copy,
1597 dst_attributes);
1598
1599 if (curves.nurbs_has_custom_knots()) {
1600 copy_curve_selection_custom_knots(curves, curves_to_copy, dst_curves);
1601 }
1602
1603 dst_curves.update_curve_types();
1605
1606 return dst_curves;
1607}
1608
1609void CurvesGeometry::remove_curves(const IndexMask &curves_to_delete,
1610 const AttributeFilter &attribute_filter)
1611{
1612 if (curves_to_delete.is_empty()) {
1613 return;
1614 }
1615 if (curves_to_delete.size() == this->curves_num()) {
1616 this->resize(0, 0);
1617 this->update_curve_types();
1618 return;
1619 }
1620 IndexMaskMemory memory;
1621 const IndexMask curves_to_copy = curves_to_delete.complement(this->curves_range(), memory);
1622 *this = curves_copy_curve_selection(*this, curves_to_copy, attribute_filter);
1623}
1624
1626{
1627 const float last = custom_knots.last();
1628 custom_knots.reverse();
1629 for (float &knot_value : custom_knots) {
1630 knot_value = last - knot_value;
1631 }
1632}
1633
1634template<typename T>
1636 const IndexMask &curve_selection,
1638{
1639 const OffsetIndices points_by_curve = curves.points_by_curve();
1640 curve_selection.foreach_index(
1641 GrainSize(256), [&](const int curve_i) { data.slice(points_by_curve[curve_i]).reverse(); });
1642}
1643
1644template<typename T>
1646 const IndexMask &curve_selection,
1647 MutableSpan<T> data_a,
1648 MutableSpan<T> data_b)
1649{
1650 const OffsetIndices points_by_curve = curves.points_by_curve();
1651 curve_selection.foreach_index(GrainSize(256), [&](const int curve_i) {
1652 const IndexRange points = points_by_curve[curve_i];
1653 MutableSpan<T> a = data_a.slice(points);
1654 MutableSpan<T> b = data_b.slice(points);
1655 for (const int i : IndexRange(points.size() / 2)) {
1656 const int end_index = points.size() - 1 - i;
1657 std::swap(a[end_index], b[i]);
1658 std::swap(b[end_index], a[i]);
1659 }
1660 if (points.size() % 2) {
1661 const int64_t middle_index = points.size() / 2;
1662 std::swap(a[middle_index], b[middle_index]);
1663 }
1664 });
1665}
1666
1667void CurvesGeometry::reverse_curves(const IndexMask &curves_to_reverse)
1668{
1669 Set<StringRef> bezier_handle_names{{ATTR_HANDLE_POSITION_LEFT,
1673
1675
1676 attributes.foreach_attribute([&](const AttributeIter &iter) {
1677 if (iter.domain != AttrDomain::Point) {
1678 return;
1679 }
1680 if (iter.data_type == CD_PROP_STRING) {
1681 return;
1682 }
1683 if (bezier_handle_names.contains(iter.name)) {
1684 return;
1685 }
1686
1688 attribute_math::convert_to_static_type(attribute.span.type(), [&](auto dummy) {
1689 using T = decltype(dummy);
1690 reverse_curve_point_data<T>(*this, curves_to_reverse, attribute.span.typed<T>());
1691 });
1692 attribute.finish();
1693 return;
1694 });
1695
1696 if (this->nurbs_has_custom_knots()) {
1697 const OffsetIndices custom_knots_by_curve = this->nurbs_custom_knots_by_curve();
1699 curves_to_reverse.foreach_index(GrainSize(256), [&](const int64_t curve) {
1700 const IndexRange curve_knots = custom_knots_by_curve[curve];
1701 if (!custom_knots.is_empty()) {
1703 }
1704 });
1705 }
1706
1707 /* In order to maintain the shape of Bezier curves, handle attributes must reverse, but also the
1708 * values for the left and right must swap. Use a utility to swap and reverse at the same time,
1709 * to avoid loading the attribute twice. Generally we can expect the right layer to exist when
1710 * the left does, but there's no need to count on it, so check for both attributes. */
1711
1714 {
1716 curves_to_reverse,
1719 }
1722 curves_to_reverse,
1725 }
1726
1727 this->tag_topology_changed();
1728}
1729
1748
1749CurvesGeometry curves_new_no_attributes(int point_num, int curve_num)
1750{
1751 CurvesGeometry curves(0, curve_num);
1752 curves.point_num = point_num;
1753 CustomData_free_layer_named(&curves.point_data, "position");
1754 return curves;
1755}
1756
1758
1759/* -------------------------------------------------------------------- */
1762
1770template<typename T>
1772 const VArray<T> &old_values,
1773 MutableSpan<T> r_values)
1774{
1775 attribute_math::DefaultMixer<T> mixer(r_values);
1776
1777 const OffsetIndices points_by_curve = curves.points_by_curve();
1778 threading::parallel_for(curves.curves_range(), 128, [&](const IndexRange range) {
1779 for (const int i_curve : range) {
1780 for (const int i_point : points_by_curve[i_curve]) {
1781 mixer.mix_in(i_curve, old_values[i_point]);
1782 }
1783 }
1784 mixer.finalize(range);
1785 });
1786}
1787
1795template<>
1797 const VArray<bool> &old_values,
1798 MutableSpan<bool> r_values)
1799{
1800 const OffsetIndices points_by_curve = curves.points_by_curve();
1801 r_values.fill(true);
1802 for (const int i_curve : IndexRange(curves.curves_num())) {
1803 for (const int i_point : points_by_curve[i_curve]) {
1804 if (!old_values[i_point]) {
1805 r_values[i_curve] = false;
1806 break;
1807 }
1808 }
1809 }
1810}
1811
1813 const GVArray &varray)
1814{
1815 GVArray new_varray;
1816 attribute_math::convert_to_static_type(varray.type(), [&](auto dummy) {
1817 using T = decltype(dummy);
1818 if constexpr (!std::is_void_v<attribute_math::DefaultMixer<T>>) {
1819 Array<T> values(curves.curves_num());
1820 adapt_curve_domain_point_to_curve_impl<T>(curves, varray.typed<T>(), values);
1821 new_varray = VArray<T>::ForContainer(std::move(values));
1822 }
1823 });
1824 return new_varray;
1825}
1826
1834template<typename T>
1836 const VArray<T> &old_values,
1837 MutableSpan<T> r_values)
1838{
1839 const OffsetIndices points_by_curve = curves.points_by_curve();
1840 for (const int i_curve : IndexRange(curves.curves_num())) {
1841 r_values.slice(points_by_curve[i_curve]).fill(old_values[i_curve]);
1842 }
1843}
1844
1846 const GVArray &varray)
1847{
1848 GVArray new_varray;
1849 attribute_math::convert_to_static_type(varray.type(), [&](auto dummy) {
1850 using T = decltype(dummy);
1851 Array<T> values(curves.points_num());
1852 adapt_curve_domain_curve_to_point_impl<T>(curves, varray.typed<T>(), values);
1853 new_varray = VArray<T>::ForContainer(std::move(values));
1854 });
1855 return new_varray;
1856}
1857
1858GVArray CurvesGeometry::adapt_domain(const GVArray &varray,
1859 const AttrDomain from,
1860 const AttrDomain to) const
1861{
1862 if (!varray) {
1863 return {};
1864 }
1865 if (varray.is_empty()) {
1866 return {};
1867 }
1868 if (from == to) {
1869 return varray;
1870 }
1871 if (varray.is_single()) {
1872 BUFFER_FOR_CPP_TYPE_VALUE(varray.type(), value);
1873 varray.get_internal_single(value);
1874 return GVArray::ForSingle(varray.type(), this->attributes().domain_size(to), value);
1875 }
1876
1877 if (from == AttrDomain::Point && to == AttrDomain::Curve) {
1878 return adapt_curve_domain_point_to_curve(*this, varray);
1879 }
1880 if (from == AttrDomain::Curve && to == AttrDomain::Point) {
1881 return adapt_curve_domain_curve_to_point(*this, varray);
1882 }
1883
1885 return {};
1886}
1887
1888AttributeAccessor CurvesGeometry::attributes() const
1889{
1891}
1892
1893MutableAttributeAccessor CurvesGeometry::attributes_for_write()
1894{
1896}
1897
1899
1900/* -------------------------------------------------------------------- */
1903
1904void CurvesGeometry::blend_read(BlendDataReader &reader)
1905{
1906 this->runtime = MEM_new<blender::bke::CurvesGeometryRuntime>(__func__);
1907
1908 CustomData_blend_read(&reader, &this->point_data, this->point_num);
1909 CustomData_blend_read(&reader, &this->curve_data, this->curve_num);
1910 this->attribute_storage.wrap().blend_read(reader);
1911
1912 if (this->curve_offsets) {
1914 &reader, &this->curve_offsets, [&]() {
1915 BLO_read_int32_array(&reader, this->curve_num + 1, &this->curve_offsets);
1917 });
1918 }
1919
1920 /* Forward compatibility. To be removed when runtime format changes. */
1922
1924
1925 if (this->custom_knot_num) {
1927 &reader, &this->custom_knots, [&]() {
1928 BLO_read_float_array(&reader, this->custom_knot_num, &this->custom_knots);
1929 return implicit_sharing::info_for_mem_free(this->custom_knots);
1930 });
1931 }
1932
1933 /* Curve types are used as array indices when counting them below. There is always one value per
1934 * curve when writing, so a different size means the file is corrupt. */
1935 const VArray<int8_t> types = this->curve_types();
1936 if (types.size() != this->curve_num) {
1937 BLO_read_data_invalidate(&reader, "Invalid curve types");
1938 return;
1939 }
1940
1941 /* Out of range values can be written, since the attribute validator used to allow
1942 * #CURVE_TYPES_NUM. Replace them so such files can still be loaded. */
1943 bool any_invalid = false;
1944 if (!curve_types_are_valid(types)) {
1945 any_invalid = true;
1946 if (this->curve_num > 0) {
1948 }
1949 else {
1950 this->attribute_storage.wrap().remove(ATTR_CURVE_TYPE);
1951 }
1952 }
1953
1954 if (any_invalid) {
1956 BLO_read_data_reports(&reader), RPT_ERROR, "%s", RPT_("Invalid curve type identifiers"));
1957 }
1958
1959 /* Recalculate curve type count cache that isn't saved in files. */
1960 this->update_curve_types();
1961}
1962
1963CurvesGeometry::BlendWriteData::BlendWriteData(ResourceScope &scope)
1964 : scope(scope),
1965 point_layers(scope.construct<Vector<CustomDataLayer, 16>>()),
1966 curve_layers(scope.construct<Vector<CustomDataLayer, 16>>()),
1968{
1969}
1970
1971void CurvesGeometry::blend_write_prepare(CurvesGeometry::BlendWriteData &write_data)
1972{
1974 {{AttrDomain::Point, &write_data.point_layers},
1975 {AttrDomain::Curve, &write_data.curve_layers}},
1976 write_data.attribute_data);
1979 this->points_num(),
1980 write_data.point_layers,
1981 write_data.attribute_data);
1984 this->curves_num(),
1985 write_data.curve_layers,
1986 write_data.attribute_data);
1987 if (write_data.attribute_data.attributes.is_empty()) {
1988 this->attribute_storage.dna_attributes = nullptr;
1990 }
1991 else {
1992 this->attribute_storage.dna_attributes = write_data.attribute_data.attributes.data();
1993 this->attribute_storage.dna_attributes_num = write_data.attribute_data.attributes.size();
1994 }
1995}
1996
1997void CurvesGeometry::blend_write(BlendWriter &writer,
1998 ID &id,
1999 const CurvesGeometry::BlendWriteData &write_data)
2000{
2002 &writer, &this->point_data, write_data.point_layers, this->point_num, CD_MASK_ALL, &id);
2004 &writer, &this->curve_data, write_data.curve_layers, this->curve_num, CD_MASK_ALL, &id);
2005 this->attribute_storage.wrap().blend_write(writer, write_data.attribute_data);
2006
2007 if (this->curve_offsets) {
2009 &writer,
2010 this->curve_offsets,
2011 sizeof(int) * (this->curve_num + 1),
2012 this->runtime->curve_offsets_sharing_info,
2013 [&]() { BLO_write_int32_array(&writer, this->curve_num + 1, this->curve_offsets); });
2014 }
2015
2017
2018 if (this->custom_knot_num) {
2020 &writer,
2021 this->custom_knots,
2022 sizeof(float) * this->custom_knot_num,
2023 this->runtime->custom_knots_sharing_info,
2024 [&]() { BLO_write_float_array(&writer, this->custom_knot_num, this->custom_knots); });
2025 }
2026}
2027
2029
2030} // namespace blender::bke
Low-level operations for curves.
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
void CustomData_blend_write_prepare(CustomData &data, blender::bke::AttrDomain domain, int domain_size, blender::Vector< CustomDataLayer, 16 > &layers_to_write, blender::bke::AttributeStorage::BlendWriteData &write_data)
void CustomData_count_memory(const CustomData &data, int totelem, blender::MemoryCounter &memory)
void CustomData_realloc(CustomData *data, int old_size, int new_size, eCDAllocType alloctype=CD_CONSTRUCT)
void * CustomData_get_layer_named_for_write(CustomData *data, eCustomDataType type, blender::StringRef name, int totelem)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
@ CD_SET_DEFAULT
const void * CustomData_get_layer_named(const CustomData *data, eCustomDataType type, blender::StringRef name)
void * CustomData_add_layer_named(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem, blender::StringRef name)
void CustomData_reset(CustomData *data)
void CustomData_blend_write(BlendWriter *writer, CustomData *data, blender::Span< CustomDataLayer > layers_to_write, int count, eCustomDataMask cddata_mask, ID *id)
void CustomData_free(CustomData *data)
void CustomData_init_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, int totelem)
void CustomData_blend_read(BlendDataReader *reader, CustomData *data, int count)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
bool CustomData_free_layer_named(CustomData *data, blender::StringRef name)
support for deformation groups and hooks.
void BKE_defbase_blend_write(BlendWriter *writer, const ListBase *defbase)
Definition deform.cc:1632
void BKE_defgroup_copy_list(ListBase *outbase, const ListBase *inbase)
Definition deform.cc:71
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
ATTR_WARN_UNUSED_RESULT const size_t num
#define UNLIKELY(x)
BlendFileReadReport * BLO_read_data_reports(BlendDataReader *reader)
Definition readfile.cc:5932
void BLO_read_float_array(BlendDataReader *reader, int64_t array_size, float **ptr_p)
Definition readfile.cc:5758
void BLO_read_int32_array(BlendDataReader *reader, int64_t array_size, int32_t **ptr_p)
Definition readfile.cc:5738
void BLO_read_data_invalidate(BlendDataReader *reader, const char *message)
Definition readfile.cc:5937
#define BLO_read_struct_list(reader, struct_name, list)
void BLO_reportf_wrap(BlendFileReadReport *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BLO_write_shared(BlendWriter *writer, const void *data, size_t approximate_size_in_bytes, const blender::ImplicitSharingInfo *sharing_info, blender::FunctionRef< void()> write_fn)
const blender::ImplicitSharingInfo * BLO_read_shared(BlendDataReader *reader, T **data_ptr, blender::FunctionRef< const blender::ImplicitSharingInfo *()> read_fn)
#define RPT_(msgid)
CurveType
@ CURVE_TYPE_BEZIER
@ CURVE_TYPE_NURBS
@ CURVE_TYPE_POLY
@ CURVE_TYPE_CATMULL_ROM
NormalMode
@ NORMAL_MODE_MINIMUM_TWIST
@ NORMAL_MODE_FREE
@ NORMAL_MODE_Z_UP
#define CURVE_TYPES_NUM
KnotsMode
@ NURBS_KNOT_MODE_CUSTOM
@ CD_MDEFORMVERT
@ CD_PROP_STRING
#define MAXMAT
Read Guarded memory(de)allocation.
for(;discarded_id_iter !=nullptr;discarded_id_iter=static_cast< ID * >(discarded_id_iter->next))
Definition blendfile.cc:634
BMesh const char void * data
void init()
long long int int64_t
constexpr void fill(const T &value) const
Definition BLI_span.hh:517
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:739
constexpr Span slice_safe(const int64_t start, const int64_t size) const
Definition BLI_span.hh:154
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:137
Span< T > as_span() const
Definition BLI_array.hh:232
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:237
static const CPPType & get()
void copy_from(GSpan values)
GMutableSpan slice(const int64_t start, int64_t size) const
const CPPType & type() const
GSpan slice(const int64_t start, int64_t size) const
int64_t size() const
void get_internal_single(void *r_value) const
static GVArray ForSingle(const CPPType &type, int64_t size, const void *value)
static IndexMask from_predicate(const IndexMask &universe, GrainSize grain_size, IndexMaskMemory &memory, Fn &&predicate)
static IndexMask from_intersection(const IndexMask &mask_a, const IndexMask &mask_b, IndexMaskMemory &memory)
constexpr int64_t first() const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr IndexRange()=default
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
constexpr int64_t start() const
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:573
constexpr bool is_empty() const
Definition BLI_span.hh:509
constexpr MutableSpan drop_back(const int64_t n) const
Definition BLI_span.hh:618
constexpr void fill(const T &value) const
Definition BLI_span.hh:517
constexpr void reverse() const
Definition BLI_span.hh:650
constexpr T & first() const
Definition BLI_span.hh:679
constexpr IndexRange index_range() const
Definition BLI_span.hh:670
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:689
bool contains(const Key &key) const
Definition BLI_set.hh:310
void ensure(FunctionRef< void(T &data)> compute_cache)
const T & data() const
constexpr Span slice_safe(const int64_t start, const int64_t size) const
Definition BLI_span.hh:154
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:137
constexpr const T & first() const
Definition BLI_span.hh:315
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
constexpr bool is_empty() const
Definition BLI_span.hh:260
std::optional< T > get_if_single() const
IndexRange index_range() const
Span< T > get_internal_span() const
static VArray ForSingle(T value, const int64_t size)
static VArray ForSpan(Span< T > values)
void append(const T &value)
const T & last(const int64_t n=0) const
void resize(const int64_t new_size)
MutableSpan< T > as_mutable_span()
void reserve(const int64_t min_capacity)
void extend(Span< T > array)
void reinitialize(const int64_t new_size)
Span< T > as_span() const
void clear_and_shrink()
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool contains(StringRef attribute_id) const
GAttributeReader lookup(const StringRef attribute_id) const
SharedCache< Vector< curves::nurbs::BasisCache > > nurbs_basis_cache
Definition BKE_curves.hh:97
SharedCache< Vector< float > > evaluated_length_cache
SharedCache< Bounds< float3 > > bounds_with_radius_cache
SharedCache< Vector< int > > custom_knot_offsets_cache
SharedCache< Vector< float3 > > evaluated_tangent_cache
SharedCache< EvaluatedOffsets > evaluated_offsets_cache
Definition BKE_curves.hh:95
SharedCache< Vector< float3 > > evaluated_normal_cache
const ImplicitSharingInfo * custom_knots_sharing_info
Definition BKE_curves.hh:79
SharedCache< std::optional< int > > max_material_index_cache
SharedCache< Bounds< float3 > > bounds_cache
SharedCache< Vector< float3 > > evaluated_position_cache
const ImplicitSharingInfo * curve_offsets_sharing_info
Definition BKE_curves.hh:76
VArray< int8_t > handle_types_left() const
MutableSpan< float3 > positions_for_write()
MutableSpan< float > nurbs_custom_knots_for_write()
OffsetIndices< int > points_by_curve() const
VArray< int8_t > normal_mode() const
MutableSpan< int8_t > handle_types_right_for_write()
VArray< int8_t > handle_types_right() const
void ensure_can_interpolate_to_evaluated() const
IndexRange curves_range() const
MutableSpan< int8_t > curve_types_for_write()
const std::array< int, CURVE_TYPES_NUM > & curve_type_counts() const
MutableSpan< float3 > handle_positions_left_for_write()
MutableAttributeAccessor attributes_for_write()
MutableSpan< float3 > handle_positions_right_for_write()
VArray< float > radius() const
VArray< float > tilt() const
Span< float3 > evaluated_tangents() const
Span< float > nurbs_weights() const
Span< float3 > handle_positions_left() const
VArray< int > resolution() const
void interpolate_to_evaluated(int curve_index, GSpan src, GMutableSpan dst) const
IndexRange points_range() const
VArray< int8_t > nurbs_knots_modes() const
Span< float3 > evaluated_normals() const
Span< float3 > positions() const
OffsetIndices< int > evaluated_points_by_curve() const
bool has_curve_with_type(CurveType type) const
void resize(int points_num, int curves_num)
Span< float3 > handle_positions_right() const
void fill_curve_types(CurveType type)
AttributeAccessor attributes() const
IndexMask indices_for_curve_type(CurveType type, IndexMaskMemory &memory) const
bool is_single_type(CurveType type) const
MutableSpan< int > offsets_for_write()
std::optional< Bounds< float3 > > bounds_min_max(bool use_radius=true) const
bool nurbs_has_custom_knots() const
OffsetIndices< int > nurbs_custom_knots_by_curve() const
Span< float3 > evaluated_positions() const
VArray< int8_t > curve_types() const
VArray< bool > cyclic() const
VArray< int8_t > nurbs_orders() const
MutableSpan< int8_t > handle_types_left_for_write()
bool remove(const StringRef attribute_id)
bool add(const StringRef attribute_id, const AttrDomain domain, const eCustomDataType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_for_write_span(StringRef attribute_id)
IndexMask complement(const IndexMask &universe, IndexMaskMemory &memory) const
void foreach_index(Fn &&fn) const
void foreach_segment(Fn &&fn) const
void add_shared(const ImplicitSharingInfo *sharing_info, const FunctionRef< void(MemoryCounter &shared_memory)> count_fn)
#define this
#define CD_MASK_ALL
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
#define T
void copy_group_to_group(OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, GSpan src, GMutableSpan dst)
void gather_group_to_group(const OffsetIndices< int > src_offsets, const OffsetIndices< int > dst_offsets, const IndexMask &selection, const Span< T > src, MutableSpan< T > dst)
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, int64_t grain_size=4096)
void convert_to_static_type(const CPPType &cpp_type, const Func &func)
typename DefaultMixerStruct< T >::type DefaultMixer
void calculate_auto_handles(bool cyclic, Span< int8_t > types_left, Span< int8_t > types_right, Span< float3 > positions, MutableSpan< float3 > positions_left, MutableSpan< float3 > positions_right)
void calculate_evaluated_offsets(Span< int8_t > handle_types_left, Span< int8_t > handle_types_right, bool cyclic, int resolution, MutableSpan< int > evaluated_offsets)
void calculate_evaluated_positions(Span< float3 > positions, Span< float3 > handles_left, Span< float3 > handles_right, OffsetIndices< int > evaluated_offsets, MutableSpan< float3 > evaluated_positions)
void interpolate_to_evaluated(GSpan src, OffsetIndices< int > evaluated_offsets, GMutableSpan dst)
int calculate_evaluated_num(int points_num, bool cyclic, int resolution)
void interpolate_to_evaluated(GSpan src, bool cyclic, int resolution, GMutableSpan dst)
int calculate_evaluated_num(int points_num, int8_t order, bool cyclic, int resolution, KnotsMode knots_mode)
int knots_num(int points_num, int8_t order, bool cyclic)
void interpolate_to_evaluated(const BasisCache &basis_cache, int8_t order, Span< float > control_weights, GSpan src, GMutableSpan dst)
void calculate_normals_z_up(Span< float3 > tangents, MutableSpan< float3 > normals)
void calculate_normals_minimum(Span< float3 > tangents, bool cyclic, MutableSpan< float3 > normals)
void calculate_tangents(Span< float3 > positions, bool is_cyclic, MutableSpan< float3 > tangents)
Definition curve_poly.cc:54
IndexRange per_curve_point_offsets_range(const IndexRange points, const int curve_index)
IndexMask indices_for_type(const VArray< int8_t > &types, const std::array< int, CURVE_TYPES_NUM > &type_counts, const CurveType type, const IndexMask &selection, IndexMaskMemory &memory)
void foreach_selected_point_ranges_per_curve(const IndexMask &mask, const OffsetIndices< int > points_by_curve, SelectedCallback selected_fn)
const AttributeAccessorFunctions & get_attribute_accessor_functions()
IndexMask curve_to_point_selection(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, IndexMaskMemory &memory)
void fill_points(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, GPointer value, GMutableSpan dst)
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)
static void normalize_span(MutableSpan< float3 > data)
static CustomData & domain_custom_data(CurvesGeometry &curves, const AttrDomain domain)
static void translate_positions(MutableSpan< float3 > positions, const float3 &translation)
CurvesGeometry curves_copy_curve_selection(const CurvesGeometry &curves, const IndexMask &curves_to_copy, const AttributeFilter &attribute_filter)
CurvesGeometry curves_copy_point_selection(const CurvesGeometry &curves, const IndexMask &points_to_copy, const AttributeFilter &attribute_filter)
constexpr StringRef ATTR_RADIUS
constexpr StringRef ATTR_RESOLUTION
static MutableSpan< T > get_mutable_attribute(CurvesGeometry &curves, const AttrDomain domain, const StringRef name, const T default_value=T())
static void copy_point_selection_custom_knots(const CurvesGeometry &curves, const IndexMask &points_to_copy, const Span< int > curve_point_counts, CurvesGeometry &dst_curves)
static VArray< T > get_varray_attribute(const CurvesGeometry &curves, const AttrDomain domain, const StringRef name, const T default_value)
constexpr StringRef ATTR_CURVE_TYPE
static bool is_valid_curve_type(const int8_t type)
eCustomDataType cpp_type_to_custom_data_type(const CPPType &type)
static GVArray adapt_curve_domain_point_to_curve(const CurvesGeometry &curves, const GVArray &varray)
constexpr StringRef ATTR_CYCLIC
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
CurvesGeometry curves_new_no_attributes(int point_num, int curve_num)
constexpr StringRef ATTR_SURFACE_UV_COORDINATE
constexpr StringRef ATTR_HANDLE_POSITION_RIGHT
std::array< int, CURVE_TYPES_NUM > calculate_type_counts(const VArray< int8_t > &types)
static void evaluate_generic_data_for_curve(const EvalData &eval_data, const int curve_index, const GSpan src, GMutableSpan dst)
static void reverse_custom_knots(MutableSpan< float > custom_knots)
constexpr StringRef ATTR_NORMAL_MODE
constexpr StringRef ATTR_TILT
constexpr StringRef ATTR_HANDLE_POSITION_LEFT
void transform_custom_normal_attribute(const float4x4 &transform, MutableAttributeAccessor &attributes)
static Span< T > get_span_attribute(const CurvesGeometry &curves, const AttrDomain domain, const StringRef name)
void build_offsets(MutableSpan< int > offsets, const CountFn &count_fn)
static void reverse_curve_point_data(const CurvesGeometry &curves, const IndexMask &curve_selection, MutableSpan< T > data)
constexpr StringRef ATTR_HANDLE_TYPE_LEFT
void gather_attributes_group_to_group(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
static int domain_num(const CurvesGeometry &curves, const AttrDomain domain)
static void reverse_swap_curve_point_data(const CurvesGeometry &curves, const IndexMask &curve_selection, MutableSpan< T > data_a, MutableSpan< T > data_b)
constexpr StringRef ATTR_NURBS_KNOTS_MODE
static bool curve_types_are_valid(const VArray< int8_t > &types)
constexpr StringRef ATTR_POSITION
static void transform_positions(MutableSpan< float3 > positions, const float4x4 &matrix)
static GVArray adapt_curve_domain_curve_to_point(const CurvesGeometry &curves, const GVArray &varray)
static void adapt_curve_domain_curve_to_point_impl(const CurvesGeometry &curves, const VArray< T > &old_values, MutableSpan< T > r_values)
constexpr StringRef ATTR_NURBS_ORDER
static void sanitize_curve_types(MutableSpan< int8_t > types)
constexpr StringRef ATTR_NURBS_WEIGHT
constexpr StringRef ATTR_HANDLE_TYPE_RIGHT
void curves_convert_storage_to_customdata(CurvesGeometry &curves)
static void rotate_directions_around_axes(MutableSpan< float3 > directions, const Span< float3 > axes, const Span< float > angles)
static void copy_curve_selection_custom_knots(const CurvesGeometry &curves, const IndexMask &curves_to_copy, CurvesGeometry &dst_curves)
static void calculate_evaluated_offsets(const CurvesGeometry &curves, MutableSpan< int > offsets, MutableSpan< int > all_bezier_offsets)
static void adapt_curve_domain_point_to_curve_impl(const CurvesGeometry &curves, const VArray< T > &old_values, MutableSpan< T > r_values)
void attribute_storage_blend_write_prepare(AttributeStorage &data, const Map< AttrDomain, Vector< CustomDataLayer, 16 > * > &layers_to_write, AttributeStorage::BlendWriteData &write_data)
std::optional< Bounds< T > > min_max_with_radii(const Span< T > values, const Span< RadiusT > radii)
std::optional< Bounds< T > > min_max(const std::optional< Bounds< T > > &a, const T &b)
Definition BLI_bounds.hh:55
std::optional< T > max(const VArray< T > &values)
void resize_trivial_array(T **data, const ImplicitSharingInfo **sharing_info, int64_t old_size, int64_t new_size)
const ImplicitSharingInfo * info_for_mem_free(void *data)
void free_shared_data(T **data, const ImplicitSharingInfo **sharing_info)
void make_trivial_data_mutable(T **data, const ImplicitSharingInfo **sharing_info, const int64_t size)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
void accumulate_lengths(const Span< T > values, const bool cyclic, MutableSpan< float > lengths)
bool is_zero(const T &a)
float3 rotate_direction_around_axis(const float3 &direction, const float3 &axis, float angle)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
bool almost_equal_relative(const VecBase< T, Size > &a, const VecBase< T, Size > &b, const T &epsilon_factor)
void build_reverse_map(OffsetIndices< int > offsets, MutableSpan< int > r_map)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
OffsetIndices< int > gather_selected_offsets(OffsetIndices< int > src_offsets, const IndexMask &selection, int start_offset, MutableSpan< int > dst_offsets)
void parallel_invoke(Functions &&...functions)
Definition BLI_task.hh:221
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:151
MatBase< float, 4, 4 > float4x4
VecBase< float, 3 > float3
struct Attribute * dna_attributes
CustomData point_data
CurvesGeometryRuntimeHandle * runtime
ListBase vertex_group_names
CustomData curve_data
struct AttributeStorage attribute_storage
Definition DNA_ID.h:404
void pad(const PaddingT &padding)
constexpr GrainSize(const int64_t grain_size)
Definition BLI_task.hh:48
Vector< CustomDataLayer, 16 > & curve_layers
Vector< CustomDataLayer, 16 > & point_layers
AttributeStorage::BlendWriteData attribute_data
const VArray< bool > & cyclic
const Span< float > nurbs_weights
const Span< int > all_bezier_evaluated_offsets
const VArray< int > & resolution
const Span< curves::nurbs::BasisCache > nurbs_basis_cache
const VArray< int8_t > & nurbs_orders
const VArray< int8_t > & types
const OffsetIndices< int > points_by_curve
i
Definition text_draw.cc:230