Blender V5.3
interpolate_curves.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
10
12#include "BKE_attribute_math.hh"
13#include "BKE_curves.hh"
14
15#include "BLI_array_utils.hh"
16#include "BLI_assert.hh"
18#include "BLI_math_vector.hh"
19#include "BLI_offset_indices.hh"
20#include "BLI_task.hh"
21
24
25namespace blender::geometry {
26
27using bke::CurvesGeometry;
28
29/* Returns a map that places each point in the sample index space.
30 * The map has one additional point at the end to simplify cyclic curve mapping. */
32 const bool cyclic,
33 const int samples_num)
34{
35 const IndexRange points = positions.index_range();
36 Array<float> sample_by_point(points.size() + 1);
37 sample_by_point[0] = 0.0f;
38 for (const int i : points.drop_front(1)) {
39 sample_by_point[i] = sample_by_point[i - 1] + math::distance(positions[i - 1], positions[i]);
40 }
41 sample_by_point.last() = cyclic ? sample_by_point[points.size() - 1] +
42 math::distance(positions.last(), positions.first()) :
43 sample_by_point[points.size() - 1];
44
45 /* If source segment lengths are zero use uniform mapping by index as a fallback. */
46 constexpr float length_epsilon = 1e-4f;
47 if (sample_by_point.last() <= length_epsilon) {
49 }
50
51 const float total_length = sample_by_point.last();
52 /* Factor for mapping segment length to sample index space. */
53 const float length_to_sample_count = math::safe_divide(float(samples_num), total_length);
54 for (float &sample_value : sample_by_point) {
55 sample_value *= length_to_sample_count;
56 }
57
58 return sample_by_point;
59}
60
61static void assign_samples_to_segments(const int num_dst_points,
62 const Span<float3> src_positions,
63 const bool cyclic,
64 MutableSpan<int> dst_sample_offsets)
65{
66 const IndexRange src_points = src_positions.index_range();
67 BLI_assert(src_points.size() > 0);
68 BLI_assert(num_dst_points > 0);
69 BLI_assert(num_dst_points >= src_points.size());
70 BLI_assert(dst_sample_offsets.size() == src_points.size() + 1);
71
72 /* Extra points of the destination curve that need to be distributed on source segments. */
73 const int num_free_samples = num_dst_points - int(src_points.size());
74 const Array<float> sample_by_point = build_point_to_sample_map(
75 src_positions, cyclic, num_free_samples);
76
77 int samples_start = 0;
78 for (const int src_point_i : src_points) {
79 dst_sample_offsets[src_point_i] = samples_start;
80
81 /* Use rounding to distribute samples equally over all segments. */
82 const int free_samples = math::round(sample_by_point[src_point_i + 1]) -
83 math::round(sample_by_point[src_point_i]);
84 samples_start += 1 + free_samples;
85 }
86
87 /* This also assigns any remaining samples in case of rounding error. */
88 dst_sample_offsets.last() = num_dst_points;
89}
90
91void sample_curve_padded(const Span<float3> positions,
92 const bool cyclic,
93 MutableSpan<int> r_indices,
94 MutableSpan<float> r_factors)
95{
96 const int num_dst_points = r_indices.size();
97 BLI_assert(r_factors.size() == num_dst_points);
98 const IndexRange src_points = positions.index_range();
99
100 if (num_dst_points == 0) {
101 return;
102 }
103 if (num_dst_points == 1) {
104 r_indices.first() = 0;
105 r_factors.first() = 0.0f;
106 return;
107 }
108
109 if (src_points.is_empty()) {
110 return;
111 }
112 if (src_points.size() == 1) {
113 r_indices.fill(0);
114 r_factors.fill(0.0f);
115 return;
116 }
117
118 /* If the destination curve has equal or more points then the excess samples are distributed
119 * equally over all the segments.
120 * If the destination curve is shorter the samples are placed equidistant along the source
121 * segments. */
122 if (num_dst_points >= src_points.size()) {
123 /* First destination point in each source segment. */
124 Array<int> dst_sample_offsets(src_points.size() + 1);
125 assign_samples_to_segments(num_dst_points, positions, cyclic, dst_sample_offsets);
126
127 OffsetIndices dst_samples_by_src_point = OffsetIndices<int>(dst_sample_offsets);
128 for (const int src_point_i : src_points.index_range()) {
129 const IndexRange samples = dst_samples_by_src_point[src_point_i];
130
131 r_indices.slice(samples).fill(src_point_i);
132 for (const int sample_i : samples.index_range()) {
133 const int sample = samples[sample_i];
134 const float factor = float(sample_i) / samples.size();
135 r_factors[sample] = factor;
136 }
137 }
138 }
139 else {
140 const Array<float> sample_by_point = build_point_to_sample_map(
141 positions, cyclic, num_dst_points - (cyclic ? 0 : 1));
142
143 for (const int src_point_i : src_points.index_range()) {
144 const float sample_start = sample_by_point[src_point_i];
145 const float sample_end = sample_by_point[src_point_i + 1];
146 const IndexRange samples = IndexRange::from_begin_end(math::ceil(sample_start),
147 math::ceil(sample_end));
148
149 for (const int sample : samples) {
150 r_indices[sample] = src_point_i;
151 r_factors[sample] = math::safe_divide(float(sample) - sample_start,
152 sample_end - sample_start);
153 }
154 }
155 if (!cyclic) {
156 r_indices.last() = src_points.size() - 1;
157 r_factors.last() = 0.0f;
158 }
159 }
160}
161
162static void reverse_samples(const int points_num,
163 MutableSpan<int> r_indices,
164 MutableSpan<float> r_factors)
165{
166 Vector<int> reverse_indices;
167 Vector<float> reverse_factors;
168 reverse_indices.reserve(r_indices.size());
169 reverse_factors.reserve(r_factors.size());
170 /* Indices in the last (cyclic) segment are also in the last segment when reversed. */
171 for (const int i : r_indices.index_range()) {
172 const int index = r_indices[i];
173 const float factor = r_factors[i];
174 const bool is_last_segment = index >= points_num - 1;
175
176 if (is_last_segment && factor > 0.0f) {
177 reverse_indices.append(points_num - 1);
178 reverse_factors.append(1.0f - factor);
179 }
180 }
181 /* Insert reversed indices except the last (cyclic) segment. */
182 for (const int i : r_indices.index_range()) {
183 const int index = r_indices[i];
184 const float factor = r_factors[i];
185 const bool is_last_segment = index >= points_num - 1;
186
187 if (factor > 0.0f) {
188 /* Skip the last (cyclic) segment, handled below. */
189 if (is_last_segment) {
190 continue;
191 }
192 reverse_indices.append(points_num - 2 - index);
193 reverse_factors.append(1.0f - r_factors[i]);
194 }
195 else {
196 /* Move factor 1.0 into the next segment. */
197 reverse_indices.append(points_num - 1 - index);
198 reverse_factors.append(0.0f);
199 }
200 }
201
202 r_indices.copy_from(reverse_indices);
203 r_factors.copy_from(reverse_factors);
204}
205
207 const int curve_index,
208 const bool cyclic,
209 const bool reverse,
210 MutableSpan<int> r_indices,
211 MutableSpan<float> r_factors)
212{
213 BLI_assert(curves.curves_range().contains(curve_index));
214 BLI_assert(r_indices.size() == r_factors.size());
215 const IndexRange points = curves.points_by_curve()[curve_index];
216 const Span<float3> positions = curves.positions().slice(points);
217
218 if (reverse) {
219 const int points_num = positions.size();
220 Array<float3> reverse_positions(points_num);
221 for (const int i : reverse_positions.index_range()) {
222 reverse_positions[i] = positions[points_num - 1 - i];
223 }
224
225 sample_curve_padded(reverse_positions, cyclic, r_indices, r_factors);
226
227 reverse_samples(points_num, r_indices, r_factors);
228 }
229 else {
230 sample_curve_padded(positions, cyclic, r_indices, r_factors);
231 }
232}
233
239 const std::array<int, CURVE_TYPES_NUM> &type_counts)
240{
242 return true;
243 }
244 /* Bezier handles and types are interpolated manually. */
245 if (ELEM(name, "handle_type_left", "handle_type_right", "handle_left", "handle_right")) {
246 return false;
247 }
248 if (ELEM(name, "nurbs_weight")) {
249 return type_counts[CURVE_TYPE_NURBS] != 0;
250 }
251 return true;
252}
253
258{
259 static const Set<StringRef> no_interpolation{{
260 "handle_type_left",
261 "handle_type_right",
262 "handle_right",
263 "handle_left",
264 "nurbs_weight",
265 }};
266 return !no_interpolation.contains(name);
267}
268
275
280 const CurvesGeometry &src_from_curves,
281 const CurvesGeometry &src_to_curves,
282 const bke::AttrDomain domain,
283 CurvesGeometry &dst_curves)
284{
286
287 const bke::AttributeAccessor src_from_attributes = src_from_curves.attributes();
288 const bke::AttributeAccessor src_to_attributes = src_to_curves.attributes();
289 bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write();
290 for (const int i : names.index_range()) {
291 bke::AttrType data_type;
292
293 const GVArray src_from_attribute = *src_from_attributes.lookup(names[i], domain);
294 if (src_from_attribute) {
295 data_type = bke::cpp_type_to_attribute_type(src_from_attribute.type());
296
297 const GVArray src_to_attribute = *src_to_attributes.lookup(names[i], domain, data_type);
298
299 result.src_from.append(src_from_attribute);
300 result.src_to.append(src_to_attribute ? src_to_attribute : GVArraySpan{});
301 }
302 else {
303 const GVArray src_to_attribute = *src_to_attributes.lookup(names[i], domain);
304 /* Attribute should exist on at least one of the geometries. */
305 BLI_assert(src_to_attribute);
306
307 data_type = bke::cpp_type_to_attribute_type(src_to_attribute.type());
308
309 result.src_from.append(GVArraySpan{});
310 result.src_to.append(src_to_attribute);
311 }
312
313 bke::GSpanAttributeWriter dst_attribute = dst_attributes.lookup_or_add_for_write_span(
314 names[i], domain, data_type);
315 result.dst.append(std::move(dst_attribute));
316 }
317
318 return result;
319}
320
325 const CurvesGeometry &from_curves, const CurvesGeometry &to_curves, CurvesGeometry &dst_curves)
326{
328 auto add_attribute = [&](const bke::AttributeIter &iter) {
329 if (iter.domain != bke::AttrDomain::Point) {
330 return;
331 }
332 if (iter.data_type == bke::AttrType::String) {
333 return;
334 }
335 if (!interpolate_attribute_to_curves(iter.name, dst_curves.curve_type_counts())) {
336 return;
337 }
338 if (!interpolate_attribute_to_poly_curve(iter.name)) {
339 return;
340 }
341 /* Position is handled differently since it has non-generic interpolation for Bezier
342 * curves and because the evaluated positions are cached for each evaluated point. */
343 if (iter.name == "position") {
344 return;
345 }
346
347 names.add(iter.name);
348 };
349
350 from_curves.attributes().foreach_attribute(add_attribute);
351 to_curves.attributes().foreach_attribute(add_attribute);
352
354 names, from_curves, to_curves, bke::AttrDomain::Point, dst_curves);
355}
356
361 const CurvesGeometry &from_curves, const CurvesGeometry &to_curves, CurvesGeometry &dst_curves)
362{
364 auto add_attribute = [&](const bke::AttributeIter &iter) {
365 if (iter.domain != bke::AttrDomain::Curve) {
366 return;
367 }
368 if (iter.data_type == bke::AttrType::String) {
369 return;
370 }
371 if (bke::attribute_name_is_anonymous(iter.name)) {
372 return;
373 }
374 /* Interpolation tool always outputs poly curves. */
375 if (iter.name == "curve_type") {
376 return;
377 }
378
379 names.add(iter.name);
380 };
381
382 from_curves.attributes().foreach_attribute(add_attribute);
383 to_curves.attributes().foreach_attribute(add_attribute);
384
386 names, from_curves, to_curves, bke::AttrDomain::Curve, dst_curves);
387}
388
389/* Resample a span of attribute values from source curves to a destination buffer. */
390static void sample_curve_attribute(const bke::CurvesGeometry &src_curves,
391 const Span<int> src_curve_indices,
392 const OffsetIndices<int> dst_points_by_curve,
393 const GSpan src_data,
394 const IndexMask &dst_curve_mask,
395 const Span<int> dst_sample_indices,
396 const Span<float> dst_sample_factors,
397 GMutableSpan dst_data)
398{
399 const CPPType &type = src_data.type();
400 BLI_assert(dst_data.type() == type);
401
402 const OffsetIndices<int> src_points_by_curve = src_curves.points_by_curve();
403 const OffsetIndices<int> src_evaluated_points_by_curve = src_curves.evaluated_points_by_curve();
404 const VArray<int8_t> curve_types = src_curves.curve_types();
405 const VArray<int> resolutions = src_curves.resolution();
406
407#ifndef NDEBUG
408 const int dst_points_num = dst_data.size();
409 BLI_assert(dst_sample_indices.size() == dst_points_num);
410 BLI_assert(dst_sample_factors.size() == dst_points_num);
411#endif
412
413 bke::attribute_math::to_static_type(type, [&]<typename T>() {
414 if constexpr (!std::is_same_v<T, std::string>) {
415 Span<T> src = src_data.typed<T>();
416 MutableSpan<T> dst = dst_data.typed<T>();
417
418 Vector<T> evaluated_data;
419 dst_curve_mask.foreach_index([&](const int i_dst_curve, const int pos) {
420 const int i_src_curve = src_curve_indices[pos];
421 if (i_src_curve < 0) {
422 return;
423 }
424
425 const IndexRange src_points = src_points_by_curve[i_src_curve];
426 const IndexRange dst_points = dst_points_by_curve[i_dst_curve];
427
428 if (curve_types[i_src_curve] == CURVE_TYPE_POLY) {
430 dst_sample_indices.slice(dst_points),
431 dst_sample_factors.slice(dst_points),
432 dst.slice(dst_points));
433 }
434 else {
435 const IndexRange src_evaluated_points = src_evaluated_points_by_curve[i_src_curve];
436 evaluated_data.reinitialize(src_evaluated_points.size());
437 src_curves.interpolate_to_evaluated(
438 i_src_curve, src.slice(src_points), evaluated_data.as_mutable_span());
439
440 Array<int> dst_sample_indices_eval(dst_points.size());
441 Array<float> dst_sample_factors_eval(dst_points.size());
442
443 if (curve_types[i_src_curve] == CURVE_TYPE_BEZIER) {
444 const Span<int> offsets = src_curves.bezier_evaluated_offsets_for_curve(i_src_curve);
445
446 for (const int i : dst_points.index_range()) {
447 const int dst_i = dst_points[i];
448 const int dst_index = dst_sample_indices[dst_i];
449 const float dst_factor = dst_sample_factors[dst_i];
451 offsets[dst_index], offsets[dst_index + 1]);
452
453 const float segment_parameter = segment_eval.first() +
454 dst_factor * segment_eval.size();
455
456 dst_sample_indices_eval[i] = math::floor(segment_parameter);
457 dst_sample_factors_eval[i] = math::mod(segment_parameter, 1.0f);
458 }
459 }
460 else if (curve_types[i_src_curve] == CURVE_TYPE_NURBS) {
461 const int src_size = src_points.size();
462 const int eval_size = src_evaluated_points.size();
463
464 for (const int i : dst_points.index_range()) {
465 const int dst_i = dst_points[i];
466 const int dst_index = dst_sample_indices[dst_i];
467 const float dst_factor = dst_sample_factors[dst_i];
468
469 const float segment_parameter = (dst_index + dst_factor) * float(eval_size) /
470 float(src_size);
471
472 dst_sample_indices_eval[i] = math::floor(segment_parameter);
473 dst_sample_factors_eval[i] = math::mod(segment_parameter, 1.0f);
474 }
475 }
476 else {
477 const int resolution = resolutions[i_src_curve];
478
479 for (const int i : dst_points.index_range()) {
480 const int dst_i = dst_points[i];
481 const int dst_index = dst_sample_indices[dst_i];
482 const float dst_factor = dst_sample_factors[dst_i];
483
484 const float segment_parameter = (dst_index + dst_factor) * resolution;
485
486 dst_sample_indices_eval[i] = math::floor(segment_parameter);
487 dst_sample_factors_eval[i] = math::mod(segment_parameter, 1.0f);
488 }
489 }
490
491 length_parameterize::interpolate(evaluated_data.as_span(),
492 dst_sample_indices_eval,
493 dst_sample_factors_eval,
494 dst.slice(dst_points));
495 }
496 });
497 }
498 });
499}
500
502{
503 const float t = parameter;
504 const float s = 1.0f - parameter;
505 return {
506 s * (3.0f * t - 1.0f),
507 9.0f * t * t - 10.0f * t,
508 10.0f * s - 9.0f * s * s,
509 t * (3.0f * t - 2.0f),
510 };
511}
512
513static int4 get_catmull_rom_indices(const int src_index,
514 const int src_index_last,
515 const bool cyclic)
516{
517 int src_index_a = src_index - 1;
518 int src_index_b = src_index;
519 int src_index_c = src_index + 1;
520 int src_index_d = src_index + 2;
521
522 if (src_index_a == -1) {
523 if (cyclic) {
524 src_index_a = src_index_last;
525 }
526 else {
527 src_index_a = 0;
528 }
529 }
530
531 if (src_index_c > src_index_last) {
532 if (cyclic) {
533 src_index_c -= src_index_last;
534 }
535 else {
536 src_index_c = src_index_last;
537 }
538 }
539
540 if (src_index_d > src_index_last) {
541 if (cyclic) {
542 src_index_d -= src_index_last;
543 }
544 else {
545 src_index_d = src_index_last;
546 }
547 }
548
549 return int4(src_index_a, src_index_b, src_index_c, src_index_d);
550}
551
552static void sample_poly_curve_positions_handles(const bool cyclic,
553 const Span<float3> src_pos,
554 const Span<int> dst_indices,
555 const Span<float> dst_factors,
556 const IndexRange dst_points,
557 const int8_t dst_type,
558 MutableSpan<float3> dst_pos,
559 MutableSpan<float3> dst_left,
560 MutableSpan<float3> dst_right,
561 MutableSpan<int8_t> dst_types_left,
562 MutableSpan<int8_t> dst_types_right)
563{
564 length_parameterize::interpolate(src_pos, dst_indices, dst_factors, dst_pos);
565
566 if (dst_type == CURVE_TYPE_BEZIER) {
567 dst_types_left.fill(BEZIER_HANDLE_VECTOR);
568 dst_types_right.fill(BEZIER_HANDLE_VECTOR);
569
570 for (const int i : dst_points.index_range()) {
571 const int i_prev = (i - 1 + dst_points.size()) % dst_points.size();
572 const int i_next = (i + 1) % dst_points.size();
573
574 /* Vector handles are one third the length of the edge. */
575 if (cyclic || i != 0) {
576 dst_left[i] = math::interpolate(dst_pos[i], dst_pos[i_prev], 1.0f / 3.0f);
577 }
578 else {
579 dst_left[i] = math::interpolate(dst_pos[i], dst_pos[i_next], -1.0f / 3.0f);
580 }
581
582 if (cyclic || i != dst_points.size() - 1) {
583 dst_right[i] = math::interpolate(dst_pos[i], dst_pos[i_next], 1.0f / 3.0f);
584 }
585 else {
586 dst_right[i] = math::interpolate(dst_pos[i], dst_pos[i_prev], -1.0f / 3.0f);
587 }
588 }
589 }
590}
591
592static void sample_catmull_rom_curve_positions_handles(const bool cyclic,
593 const IndexRange src_points,
594 const Span<float3> src_pos,
595 const Span<int> dst_indices,
596 const Span<float> dst_factors,
597 const IndexRange dst_points,
598 const int8_t dst_type,
599 MutableSpan<float3> dst_pos,
600 MutableSpan<float3> dst_left,
601 MutableSpan<float3> dst_right,
602 MutableSpan<int8_t> dst_types_left,
603 MutableSpan<int8_t> dst_types_right)
604{
605 dst_types_left.fill(BEZIER_HANDLE_ALIGN);
606 dst_types_right.fill(BEZIER_HANDLE_ALIGN);
607
608 for (const int i : dst_points.index_range()) {
609 const int src_index = dst_indices[i];
610 const float src_factor = dst_factors[i];
611
612 const int i_prev = (i - 1 + dst_points.size()) % dst_points.size();
613 const float src_factor_prev = dst_factors[i_prev];
614
615 const int i_next = (i + 1) % dst_points.size();
616 const float src_factor_next = dst_factors[i_next];
617
618 const int4 src_indices = get_catmull_rom_indices(src_index, src_points.size() - 1, cyclic);
619
620 const float3 &pos_a = src_pos[src_indices[0]];
621 const float3 &pos_b = src_pos[src_indices[1]];
622 const float3 &pos_c = src_pos[src_indices[2]];
623 const float3 &pos_d = src_pos[src_indices[3]];
624
625 if (src_factor == 0.0f) {
626 dst_pos[i] = src_pos[src_index];
627
628 if (dst_type == CURVE_TYPE_BEZIER) {
629 const float3 derivative = 0.5f * (pos_c - pos_a);
630 dst_right[i] = dst_pos[i] + derivative / 3.0f;
631 dst_left[i] = dst_pos[i] - derivative / 3.0f;
632
633 if ((cyclic || i != 0) && dst_indices[i_prev] == src_index - 1) {
634 dst_left[i] = dst_pos[i] + (dst_left[i] - dst_pos[i]) * (1.0f - src_factor_prev);
635 }
636 if ((cyclic || i != dst_points.size() - 1) && dst_indices[i_next] == src_index) {
637 dst_right[i] = dst_pos[i] + (dst_right[i] - dst_pos[i]) * src_factor_next;
638 }
639 }
640 }
641 else {
642 const float4 weights = bke::curves::catmull_rom::calculate_basis(src_factor);
643
644 dst_pos[i] = 0.5f * bke::attribute_math::mix4<float3>(weights, pos_a, pos_b, pos_c, pos_d);
645 if (dst_type == CURVE_TYPE_BEZIER) {
646 const float4 dwdt = calculate_catmull_rom_basis_derivative(src_factor);
647
648 const float3 derivative = 0.5f * bke::attribute_math::mix4<float3>(
649 dwdt, pos_a, pos_b, pos_c, pos_d);
650
651 /* Bezier handles are one third the length the derivative at the control points. */
652 dst_right[i] = dst_pos[i] + derivative / 3.0f;
653 dst_left[i] = dst_pos[i] - derivative / 3.0f;
654
655 if ((cyclic || i != 0) && dst_indices[i_prev] == src_index - 1) {
656 dst_left[i] = dst_pos[i] + (dst_left[i] - dst_pos[i]) * (src_factor - src_factor_prev);
657 }
658 if ((cyclic || i != dst_points.size() - 1) && dst_indices[i_next] == src_index) {
659 dst_right[i] = dst_pos[i] + (dst_right[i] - dst_pos[i]) * (src_factor_next - src_factor);
660 }
661 }
662 }
663 }
664}
665
666static void sample_bezier_curve_positions_handles(const bool cyclic,
667 const IndexRange src_points,
668 const Span<float3> src_pos,
669 const Span<float3> src_handle_left,
670 const Span<float3> src_handle_right,
671 const Span<int8_t> src_types_left,
672 const Span<int8_t> src_types_right,
673 const Span<int> dst_indices,
674 const Span<float> dst_factors,
675 const IndexRange dst_points,
676 MutableSpan<float3> dst_pos,
677 MutableSpan<float3> dst_left,
678 MutableSpan<float3> dst_right,
679 MutableSpan<int8_t> dst_types_left,
680 MutableSpan<int8_t> dst_types_right)
681{
682 const Span<float3> src_left = src_handle_left.slice(src_points);
683 const Span<float3> src_right = src_handle_right.slice(src_points);
684
685 for (const int i : dst_points.index_range()) {
686 const int src_index = dst_indices[i];
687 const float src_factor = dst_factors[i];
688
689 const int i_prev = (i - 1 + dst_points.size()) % dst_points.size();
690 const float src_factor_prev = dst_factors[i_prev];
691
692 const int i_next = (i + 1) % dst_points.size();
693 const float src_factor_next = dst_factors[i_next];
694
695 if (src_factor == 0.0f) {
696 dst_pos[i] = src_pos[src_index];
697 dst_left[i] = src_left[src_index];
698 dst_right[i] = src_right[src_index];
699
700 if ((cyclic || i != 0) && dst_indices[i_prev] == src_index - 1) {
701 dst_left[i] = dst_pos[i] + (dst_left[i] - dst_pos[i]) * (1.0f - src_factor_prev);
702 }
703 if ((cyclic || i != dst_points.size() - 1) && dst_indices[i_next] == src_index) {
704 dst_right[i] = dst_pos[i] + (dst_right[i] - dst_pos[i]) * src_factor_next;
705 }
706
707 dst_types_left[i] = src_types_left[src_index];
708 dst_types_right[i] = src_types_right[src_index];
709 }
710 else {
711 const int src_index_next = (src_index + 1) % src_pos.size();
712
714 src_pos[src_index],
715 src_right[src_index],
716 src_left[src_index_next],
717 src_pos[src_index_next],
718 src_factor);
719
720 dst_pos[i] = insert_point.position;
721 dst_left[i] = insert_point.left_handle;
722 dst_right[i] = insert_point.right_handle;
723
724 if ((cyclic || i != 0) && dst_indices[i_prev] == src_index) {
725 /* The handles already have been scaled by `src_factor`, so we divide to remove. */
726 dst_left[i] = dst_pos[i] +
727 (dst_left[i] - dst_pos[i]) * (src_factor - src_factor_prev) / src_factor;
728 }
729 if ((cyclic || i != dst_points.size() - 1) && dst_indices[i_next] == src_index) {
730 /* The handles already have been scaled by `1.0f - src_factor`, so we divide to remove.
731 */
732 dst_right[i] = dst_pos[i] + (dst_right[i] - dst_pos[i]) * (src_factor_next - src_factor) /
733 (1.0f - src_factor);
734 }
735
736 /* Output Vector type if the segment is also Vector, otherwise be aligned. */
737 if (src_types_left[src_index] == BEZIER_HANDLE_VECTOR &&
738 src_types_left[src_index_next] == BEZIER_HANDLE_VECTOR)
739 {
740 dst_types_left[i] = BEZIER_HANDLE_VECTOR;
741 dst_types_right[i] = BEZIER_HANDLE_VECTOR;
742 }
743 else {
744 dst_types_left[i] = BEZIER_HANDLE_ALIGN;
745 dst_types_right[i] = BEZIER_HANDLE_ALIGN;
746 }
747 }
748 }
749}
750
751/* Resample the positions and handles. */
753 const Span<int> src_curve_indices,
754 const OffsetIndices<int> dst_points_by_curve,
755 const VArray<int8_t> dst_types,
756 const IndexMask &dst_curve_mask,
757 const Span<int> dst_sample_indices,
758 const Span<float> dst_sample_factors,
759 MutableSpan<float3> dst_positions,
760 MutableSpan<float3> dst_handles_left,
761 MutableSpan<float3> dst_handles_right,
762 MutableSpan<int8_t> dst_handle_types_left,
763 MutableSpan<int8_t> dst_handle_types_right)
764{
765 const OffsetIndices<int> src_points_by_curve = src_curves.points_by_curve();
766 const VArray<int8_t> src_types = src_curves.curve_types();
767 const Span<float3> src_positions = src_curves.positions();
768 const VArray<bool> src_cyclic = src_curves.cyclic();
769 const std::optional<Span<float3>> src_handle_left = src_curves.handle_positions_left();
770 const std::optional<Span<float3>> src_handle_right = src_curves.handle_positions_right();
771 const VArraySpan<int8_t> src_handle_types_left = src_curves.handle_types_left();
772 const VArraySpan<int8_t> src_handle_types_right = src_curves.handle_types_right();
773
774#ifndef NDEBUG
775 const int dst_points_num = dst_positions.size();
776 BLI_assert(dst_handles_left.size() == dst_points_num);
777 BLI_assert(dst_handles_right.size() == dst_points_num);
778 BLI_assert(dst_sample_indices.size() == dst_points_num);
779 BLI_assert(dst_sample_factors.size() == dst_points_num);
780#endif
781
782 dst_curve_mask.foreach_index([&](const int i_dst_curve, const int pos) {
783 const int i_src_curve = src_curve_indices[pos];
784 if (i_src_curve < 0) {
785 return;
786 }
787
788 const bool cyclic = src_cyclic[i_src_curve];
789
790 const IndexRange src_points = src_points_by_curve[i_src_curve];
791 const IndexRange dst_points = dst_points_by_curve[i_dst_curve];
792
793 const Span<float3> src_pos = src_positions.slice(src_points);
794 const Span<int8_t> src_types_left = src_handle_types_left.slice(src_points);
795 const Span<int8_t> src_types_right = src_handle_types_right.slice(src_points);
796 const Span<int> dst_indices = dst_sample_indices.slice(dst_points);
797 const Span<float> dst_factors = dst_sample_factors.slice(dst_points);
798
799 MutableSpan<float3> dst_pos = dst_positions.slice(dst_points);
800 MutableSpan<float3> dst_left = dst_handles_left.slice(dst_points);
801 MutableSpan<float3> dst_right = dst_handles_right.slice(dst_points);
802 MutableSpan<int8_t> dst_types_left = dst_handle_types_left.slice(dst_points);
803 MutableSpan<int8_t> dst_types_right = dst_handle_types_right.slice(dst_points);
804
805 if (src_types[i_src_curve] == CURVE_TYPE_POLY) {
807 src_pos,
808 dst_indices,
809 dst_factors,
810 dst_points,
811 dst_types[i_dst_curve],
812 dst_pos,
813 dst_left,
814 dst_right,
815 dst_types_left,
816 dst_types_right);
817 }
818 else if (src_types[i_src_curve] == CURVE_TYPE_NURBS) {
819 /* NURBS take priority over Bézier, so we should never be trying to be Bézier. */
820 BLI_assert(dst_types[i_dst_curve] != CURVE_TYPE_BEZIER);
821
822 length_parameterize::interpolate(src_pos, dst_indices, dst_factors, dst_pos);
823 }
824 else if (src_types[i_src_curve] == CURVE_TYPE_CATMULL_ROM) {
826 src_points,
827 src_pos,
828 dst_indices,
829 dst_factors,
830 dst_points,
831 dst_types[i_dst_curve],
832 dst_pos,
833 dst_left,
834 dst_right,
835 dst_types_left,
836 dst_types_right);
837 }
838 else if (src_types[i_src_curve] == CURVE_TYPE_BEZIER) {
839 BLI_assert(src_handle_left);
840 BLI_assert(src_handle_right);
841
843 src_points,
844 src_pos,
845 *src_handle_left,
846 *src_handle_right,
847 src_types_left,
848 src_types_right,
849 dst_indices,
850 dst_factors,
851 dst_points,
852 dst_pos,
853 dst_left,
854 dst_right,
855 dst_types_left,
856 dst_types_right);
857 }
858 else {
860 }
861 });
862}
863
864template<typename T>
865static void mix_arrays(const Span<T> from,
866 const Span<T> to,
867 const float mix_factor,
868 const MutableSpan<T> dst)
869{
870 if (mix_factor == 0.0f) {
871 dst.copy_from(from);
872 }
873 else if (mix_factor == 1.0f) {
874 dst.copy_from(to);
875 }
876 else {
877 for (const int i : dst.index_range()) {
878 dst[i] = math::interpolate(from[i], to[i], mix_factor);
879 }
880 }
881}
882
883static void mix_arrays(const GSpan src_from,
884 const GSpan src_to,
885 const Span<float> mix_factors,
886 const IndexMask &selection,
887 const GMutableSpan dst)
888{
889 bke::attribute_math::to_static_type(dst.type(), [&]<typename T>() {
890 if constexpr (!std::is_same_v<T, std::string>) {
891 const Span<T> from = src_from.typed<T>();
892 const Span<T> to = src_to.typed<T>();
893 const MutableSpan<T> dst_typed = dst.typed<T>();
894 selection.foreach_index(
895 [&](const int curve) {
896 const float mix_factor = mix_factors[curve];
897 if (mix_factor == 0.0f) {
898 dst_typed[curve] = from[curve];
899 }
900 else if (mix_factor == 1.0f) {
901 dst_typed[curve] = to[curve];
902 }
903 else {
904 dst_typed[curve] = math::interpolate(from[curve], to[curve], mix_factor);
905 }
906 },
907 exec_mode::grain_size(512));
908 }
909 });
910}
911
912static void mix_arrays(const GSpan src_from,
913 const GSpan src_to,
914 const Span<float> mix_factors,
915 const IndexMask &group_selection,
916 const OffsetIndices<int> groups,
917 const GMutableSpan dst)
918{
919 group_selection.foreach_index(
920 [&](const int curve) {
921 const IndexRange range = groups[curve];
922 bke::attribute_math::to_static_type(dst.type(), [&]<typename T>() {
923 if constexpr (!std::is_same_v<T, std::string>) {
924 const Span<T> from = src_from.typed<T>();
925 const Span<T> to = src_to.typed<T>();
926 const MutableSpan<T> dst_typed = dst.typed<T>();
927 mix_arrays(
928 from.slice(range), to.slice(range), mix_factors[curve], dst_typed.slice(range));
929 }
930 });
931 },
933}
934
935static int8_t mix_handle_type(const int8_t from_type, const int8_t to_type)
936{
937 /* Vector handles can only be mixed with other vector handles, otherwise use free handle as
938 * fallback. */
939 if (from_type == BEZIER_HANDLE_VECTOR && to_type == BEZIER_HANDLE_VECTOR) {
941 }
942 if (from_type == BEZIER_HANDLE_VECTOR || to_type == BEZIER_HANDLE_VECTOR) {
943 return BEZIER_HANDLE_FREE;
944 }
945
946 if (from_type == BEZIER_HANDLE_FREE || to_type == BEZIER_HANDLE_FREE) {
947 return BEZIER_HANDLE_FREE;
948 }
949 if (from_type == BEZIER_HANDLE_ALIGN || to_type == BEZIER_HANDLE_ALIGN) {
950 return BEZIER_HANDLE_ALIGN;
951 }
952 return BEZIER_HANDLE_AUTO;
953}
954
955static void mix_handle_type_arrays(const Span<int8_t> src_from,
956 const Span<int8_t> src_to,
957 const IndexMask &group_selection,
958 const OffsetIndices<int> groups,
959 const MutableSpan<int8_t> dst)
960{
961 group_selection.foreach_index(
962 [&](const int curve) {
963 for (const int i : groups[curve]) {
964 dst[i] = mix_handle_type(src_from[i], src_to[i]);
965 }
966 },
968}
969
970/* Calculate the new curve's type by using the type with highest priority. */
971static void mix_curve_type(const Span<int> from_curve_indices,
972 const Span<int> to_curve_indices,
973 const VArray<int8_t> &from_types,
974 const VArray<int8_t> &to_types,
975 const IndexMask &dst_curve_mask,
976 MutableSpan<int8_t> dst_curve_types)
977{
978 dst_curve_mask.foreach_index([&](const int i_dst_curve, const int pos) {
979 const int i_from_curve = from_curve_indices[pos];
980 const int i_to_curve = to_curve_indices[pos];
981
982 if (i_from_curve < 0) {
983 dst_curve_types[i_dst_curve] = to_types[i_to_curve];
984 return;
985 }
986 if (i_to_curve < 0) {
987 dst_curve_types[i_dst_curve] = from_types[i_from_curve];
988 return;
989 }
990
991 const int8_t from_type = from_types[i_from_curve];
992 const int8_t to_type = to_types[i_to_curve];
993
994 if (from_type == CURVE_TYPE_NURBS || to_type == CURVE_TYPE_NURBS) {
995 dst_curve_types[i_dst_curve] = CURVE_TYPE_NURBS;
996 return;
997 }
998 if (from_type == CURVE_TYPE_BEZIER || to_type == CURVE_TYPE_BEZIER) {
999 dst_curve_types[i_dst_curve] = CURVE_TYPE_BEZIER;
1000 return;
1001 }
1002 if (from_type == CURVE_TYPE_CATMULL_ROM || to_type == CURVE_TYPE_CATMULL_ROM) {
1003 dst_curve_types[i_dst_curve] = CURVE_TYPE_CATMULL_ROM;
1004 return;
1005 }
1006 dst_curve_types[i_dst_curve] = CURVE_TYPE_POLY;
1007 });
1008}
1009
1011 const CurvesGeometry &to_curves,
1012 const Span<int> from_curve_indices,
1013 const Span<int> to_curve_indices,
1014 const Span<int> from_sample_indices,
1015 const Span<int> to_sample_indices,
1016 const Span<float> from_sample_factors,
1017 const Span<float> to_sample_factors,
1018 const IndexMask &dst_curve_mask,
1019 const float mix_factor,
1020 CurvesGeometry &dst_curves,
1021 IndexMaskMemory &memory)
1022{
1023 BLI_assert(from_curve_indices.size() == dst_curve_mask.size());
1024 BLI_assert(to_curve_indices.size() == dst_curve_mask.size());
1025 BLI_assert(from_sample_indices.size() == dst_curves.points_num());
1026 BLI_assert(to_sample_indices.size() == dst_curves.points_num());
1027 BLI_assert(from_sample_factors.size() == dst_curves.points_num());
1028 BLI_assert(to_sample_factors.size() == dst_curves.points_num());
1029
1030 if (from_curves.is_empty() || to_curves.is_empty()) {
1031 return;
1032 }
1033
1036
1037 mix_curve_type(from_curve_indices,
1038 to_curve_indices,
1039 from_curves.curve_types(),
1040 to_curves.curve_types(),
1041 dst_curve_mask,
1042 dst_curves.curve_types_for_write());
1043
1044 dst_curves.update_curve_types();
1045
1046 MutableSpan<float3> dst_positions = dst_curves.positions_for_write();
1049 MutableSpan<int8_t> dst_types_left = dst_curves.handle_types_left_for_write();
1050 MutableSpan<int8_t> dst_types_right = dst_curves.handle_types_right_for_write();
1051
1053 from_curves, to_curves, dst_curves);
1055 from_curves, to_curves, dst_curves);
1056
1057 const OffsetIndices dst_points_by_curve = dst_curves.points_by_curve();
1058
1059 Array<bool> mix_from_to(dst_curves.curves_num());
1060 Array<bool> exclusive_from(dst_curves.curves_num());
1061 Array<bool> exclusive_to(dst_curves.curves_num());
1062 Array<float> mix_factors(dst_curves.curves_num());
1063 dst_curve_mask.foreach_index(
1064 [&](const int i_dst_curve, const int pos) {
1065 const int i_from_curve = from_curve_indices[pos];
1066 const int i_to_curve = to_curve_indices[pos];
1067 if (i_from_curve >= 0 && i_to_curve >= 0) {
1068 mix_factors[i_dst_curve] = mix_factor;
1069 mix_from_to[i_dst_curve] = true;
1070 exclusive_from[i_dst_curve] = false;
1071 exclusive_to[i_dst_curve] = false;
1072 }
1073 else if (i_to_curve >= 0) {
1074 mix_factors[i_dst_curve] = 1.0f;
1075 mix_from_to[i_dst_curve] = false;
1076 exclusive_from[i_dst_curve] = false;
1077 exclusive_to[i_dst_curve] = true;
1078 }
1079 else {
1080 mix_factors[i_dst_curve] = 0.0f;
1081 mix_from_to[i_dst_curve] = false;
1082 exclusive_from[i_dst_curve] = true;
1083 exclusive_to[i_dst_curve] = false;
1084 }
1085 },
1087
1088 /* Curve mask contains indices that may not be valid for both "from" and "to" curves. These need
1089 * to be filtered out before use with the generic array utils. These masks are exclusive so that
1090 * each element is only mixed in by one mask. */
1091 const IndexMask mix_curve_mask = IndexMask::from_bools(dst_curve_mask, mix_from_to, memory);
1092 const IndexMask from_curve_mask = IndexMask::from_bools(dst_curve_mask, exclusive_from, memory);
1093 const IndexMask to_curve_mask = IndexMask::from_bools(dst_curve_mask, exclusive_to, memory);
1094
1095 /* For every attribute, evaluate attributes from every curve in the range in the original
1096 * curve's "evaluated points", then use linear interpolation to sample to the result. */
1097 for (const int i_attribute : point_attributes.dst.index_range()) {
1098 /* Attributes that exist already on another domain can not be written to. */
1099 if (!point_attributes.dst[i_attribute]) {
1100 continue;
1101 }
1102
1103 const GSpan src_from = point_attributes.src_from[i_attribute];
1104 const GSpan src_to = point_attributes.src_to[i_attribute];
1105 GMutableSpan dst = point_attributes.dst[i_attribute].span;
1106
1107 /* Mix factors depend on which of the from/to curves geometries has attribute data. If
1108 * only one geometry has attribute data it gets the full mix weight. */
1109 if (!src_from.is_empty() && !src_to.is_empty()) {
1110 GArray<> from_samples(dst.type(), dst.size());
1111 GArray<> to_samples(dst.type(), dst.size());
1112 sample_curve_attribute(from_curves,
1113 from_curve_indices,
1114 dst_points_by_curve,
1115 src_from,
1116 dst_curve_mask,
1117 from_sample_indices,
1118 from_sample_factors,
1119 from_samples);
1120 sample_curve_attribute(to_curves,
1121 to_curve_indices,
1122 dst_points_by_curve,
1123 src_to,
1124 dst_curve_mask,
1125 to_sample_indices,
1126 to_sample_factors,
1127 to_samples);
1128 mix_arrays(from_samples, to_samples, mix_factors, dst_curve_mask, dst_points_by_curve, dst);
1129 }
1130 else if (!src_from.is_empty()) {
1131 sample_curve_attribute(from_curves,
1132 from_curve_indices,
1133 dst_points_by_curve,
1134 src_from,
1135 dst_curve_mask,
1136 from_sample_indices,
1137 from_sample_factors,
1138 dst);
1139 }
1140 else if (!src_to.is_empty()) {
1141 sample_curve_attribute(to_curves,
1142 to_curve_indices,
1143 dst_points_by_curve,
1144 src_to,
1145 dst_curve_mask,
1146 to_sample_indices,
1147 to_sample_factors,
1148 dst);
1149 }
1150 }
1151
1152 {
1153 const VArray<int8_t> dst_types = dst_curves.curve_types();
1154
1155 Array<float3> from_pos(dst_positions.size());
1156 Array<float3> to_pos(dst_positions.size());
1157 Array<float3> from_left(dst_left.size());
1158 Array<float3> to_left(dst_left.size());
1159 Array<float3> from_right(dst_right.size());
1160 Array<float3> to_right(dst_right.size());
1161
1162 Array<int8_t> from_types_left(dst_left.size());
1163 Array<int8_t> to_types_left(dst_left.size());
1164 Array<int8_t> from_types_right(dst_right.size());
1165 Array<int8_t> to_types_right(dst_right.size());
1166
1167 /* Interpolate the positions and handles to the resampled curves. */
1169 from_curve_indices,
1170 dst_points_by_curve,
1171 dst_types,
1172 dst_curve_mask,
1173 from_sample_indices,
1174 from_sample_factors,
1175 from_pos.as_mutable_span(),
1176 from_left.as_mutable_span(),
1177 from_right.as_mutable_span(),
1178 from_types_left.as_mutable_span(),
1179 from_types_right.as_mutable_span());
1181 to_curve_indices,
1182 dst_points_by_curve,
1183 dst_types,
1184 dst_curve_mask,
1185 to_sample_indices,
1186 to_sample_factors,
1187 to_pos.as_mutable_span(),
1188 to_left.as_mutable_span(),
1189 to_right.as_mutable_span(),
1190 to_types_left.as_mutable_span(),
1191 to_types_right.as_mutable_span());
1192
1193 mix_arrays(from_pos.as_span(),
1194 to_pos.as_span(),
1195 mix_factors,
1196 dst_curve_mask,
1197 dst_points_by_curve,
1198 dst_positions);
1199 mix_arrays(from_left.as_span(),
1200 to_left.as_span(),
1201 mix_factors,
1202 dst_curve_mask,
1203 dst_points_by_curve,
1204 dst_left);
1205 mix_arrays(from_right.as_span(),
1206 to_right.as_span(),
1207 mix_factors,
1208 dst_curve_mask,
1209 dst_points_by_curve,
1210 dst_right);
1211
1212 mix_handle_type_arrays(from_types_left.as_span(),
1213 to_types_left.as_span(),
1214 dst_curve_mask,
1215 dst_points_by_curve,
1216 dst_types_left);
1217 mix_handle_type_arrays(from_types_right.as_span(),
1218 to_types_right.as_span(),
1219 dst_curve_mask,
1220 dst_points_by_curve,
1221 dst_types_right);
1222
1223 dst_curves.calculate_bezier_auto_handles();
1224 }
1225
1226 for (const int i_attribute : curve_attributes.dst.index_range()) {
1227 /* Attributes that exist already on another domain can not be written to. */
1228 if (!curve_attributes.dst[i_attribute]) {
1229 continue;
1230 }
1231
1232 const GSpan src_from = curve_attributes.src_from[i_attribute];
1233 const GSpan src_to = curve_attributes.src_to[i_attribute];
1234 GMutableSpan dst = curve_attributes.dst[i_attribute].span;
1235
1236 /* Only mix "safe" attribute types for now. Other types (int, bool, etc.) are just copied from
1237 * the first curve of each pair. */
1238 const bool can_mix_attribute = ELEM(bke::cpp_type_to_attribute_type(dst.type()),
1243 if (can_mix_attribute && !src_from.is_empty() && !src_to.is_empty()) {
1244 array_utils::copy(GVArray::from_span(src_from), from_curve_mask, dst);
1245 array_utils::copy(GVArray::from_span(src_to), to_curve_mask, dst);
1246
1247 GArray<> from_samples(dst.type(), dst.size());
1248 GArray<> to_samples(dst.type(), dst.size());
1249 array_utils::copy(GVArray::from_span(src_from), mix_curve_mask, from_samples);
1250 array_utils::copy(GVArray::from_span(src_to), mix_curve_mask, to_samples);
1251 mix_arrays(from_samples, to_samples, mix_factors, mix_curve_mask, dst);
1252 }
1253 else if (!src_from.is_empty()) {
1254 array_utils::copy(GVArray::from_span(src_from), from_curve_mask, dst);
1255 array_utils::copy(GVArray::from_span(src_from), mix_curve_mask, dst);
1256 }
1257 else if (!src_to.is_empty()) {
1258 array_utils::copy(GVArray::from_span(src_to), to_curve_mask, dst);
1259 array_utils::copy(GVArray::from_span(src_to), mix_curve_mask, dst);
1260 }
1261 }
1262
1263 for (bke::GSpanAttributeWriter &attribute : point_attributes.dst) {
1264 attribute.finish();
1265 }
1266 for (bke::GSpanAttributeWriter &attribute : curve_attributes.dst) {
1267 attribute.finish();
1268 }
1269
1270 dst_curves.tag_topology_changed();
1271}
1272
1274 const int curve_index,
1275 const bool cyclic,
1276 const bool reverse,
1277 MutableSpan<int> r_segment_indices,
1278 MutableSpan<float> r_factors)
1279{
1280 const Span<float> segment_lengths = curves.evaluated_lengths_for_curve(curve_index, cyclic);
1281 if (segment_lengths.is_empty()) {
1282 /* Handle curves with only one evaluated point. */
1283 r_segment_indices.fill(0);
1284 r_factors.fill(0.0f);
1285 return;
1286 }
1287
1288 if (reverse) {
1290 segment_lengths, !cyclic, r_segment_indices, r_factors);
1291 }
1292 else {
1293 length_parameterize::sample_uniform(segment_lengths, !cyclic, r_segment_indices, r_factors);
1294 }
1295};
1296
1297void interpolate_curves(const CurvesGeometry &from_curves,
1298 const CurvesGeometry &to_curves,
1299 const Span<int> from_curve_indices,
1300 const Span<int> to_curve_indices,
1301 const IndexMask &dst_curve_mask,
1302 const Span<bool> dst_curve_flip_direction,
1303 const float mix_factor,
1304 CurvesGeometry &dst_curves,
1305 IndexMaskMemory &memory)
1306{
1307 const VArray<bool> from_curves_cyclic = from_curves.cyclic();
1308 const VArray<bool> to_curves_cyclic = to_curves.cyclic();
1309 const OffsetIndices dst_points_by_curve = dst_curves.points_by_curve();
1310
1311 /* Sampling arbitrary attributes works by first interpolating them to the curve's standard
1312 * "evaluated points" and then interpolating that result with the uniform samples. This is
1313 * potentially wasteful when down-sampling a curve to many fewer points. There are two possible
1314 * solutions: only sample the necessary points for interpolation, or first sample curve
1315 * parameter/segment indices and evaluate the curve directly. */
1316 Array<int> from_sample_indices(dst_curves.points_num());
1317 Array<int> to_sample_indices(dst_curves.points_num());
1318 Array<float> from_sample_factors(dst_curves.points_num());
1319 Array<float> to_sample_factors(dst_curves.points_num());
1320
1321 from_curves.ensure_evaluated_lengths();
1322 to_curves.ensure_evaluated_lengths();
1323
1324 /* Gather uniform samples based on the accumulated lengths of the original curve. */
1325 dst_curve_mask.foreach_index(
1326 [&](const int i_dst_curve, const int pos) {
1327 const int i_from_curve = from_curve_indices[pos];
1328 const int i_to_curve = to_curve_indices[pos];
1329
1330 const IndexRange dst_points = dst_points_by_curve[i_dst_curve];
1331 /* First curve is sampled in forward direction, second curve may be reversed. */
1332 sample_curve_uniform(from_curves,
1333 i_from_curve,
1334 from_curves_cyclic[i_from_curve],
1335 false,
1336 from_sample_indices.as_mutable_span().slice(dst_points),
1337 from_sample_factors.as_mutable_span().slice(dst_points));
1338 sample_curve_uniform(to_curves,
1339 i_to_curve,
1340 to_curves_cyclic[i_to_curve],
1341 dst_curve_flip_direction[i_dst_curve],
1342 to_sample_indices.as_mutable_span().slice(dst_points),
1343 to_sample_factors.as_mutable_span().slice(dst_points));
1344 },
1346
1348 to_curves,
1349 from_curve_indices,
1350 to_curve_indices,
1351 from_sample_indices,
1352 to_sample_indices,
1353 from_sample_factors,
1354 to_sample_factors,
1355 dst_curve_mask,
1356 mix_factor,
1357 dst_curves,
1358 memory);
1359}
1360
1361} // namespace blender::geometry
Low-level operations for curves.
#define BLI_assert_unreachable()
#define ELEM(...)
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T & last(const int64_t n=0) const
Definition BLI_array.hh:307
IndexRange index_range() const
Definition BLI_array.hh:371
const CPPType & type() const
MutableSpan< T > typed() const
Span< T > typed() const
const CPPType & type() const
bool is_empty() const
static GVArray from_span(GSpan span)
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr int64_t first() const
constexpr int64_t size() const
constexpr bool is_empty() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr IndexRange index_range() const
static constexpr IndexRange from_begin_end_inclusive(const int64_t begin, const int64_t last)
constexpr IndexRange drop_front(int64_t n) const
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr T & first() const
Definition BLI_span.hh:685
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
bool contains(const Key &key) const
Definition BLI_set.hh:310
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
bool add(const Key &key)
void append(const T &value)
void reserve(const int64_t min_capacity)
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
GAttributeReader lookup(const StringRef name) const
MutableSpan< int8_t > handle_types_left_for_write()
VArray< int8_t > curve_types() const
void ensure_can_interpolate_to_evaluated() const
MutableSpan< float3 > positions_for_write()
MutableSpan< int8_t > handle_types_right_for_write()
std::optional< Span< float3 > > handle_positions_right() const
VArray< bool > cyclic() const
AttributeAccessor attributes() const
Span< float3 > positions() const
void interpolate_to_evaluated(int curve_index, GSpan src, GMutableSpan dst) const
MutableSpan< float3 > handle_positions_right_for_write()
std::optional< Span< float3 > > handle_positions_left() const
Span< int > bezier_evaluated_offsets_for_curve(int curve_index) const
MutableSpan< int8_t > curve_types_for_write()
VArray< int8_t > handle_types_left() const
OffsetIndices< int > evaluated_points_by_curve() const
MutableSpan< float3 > handle_positions_left_for_write()
const std::array< int, CURVE_TYPES_NUM > & curve_type_counts() const
VArray< int > resolution() const
VArray< int8_t > handle_types_right() const
OffsetIndices< int > points_by_curve() const
MutableAttributeAccessor attributes_for_write()
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
constexpr Span slice(int64_t start, int64_t size) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define attribute(slot)
uint to_type(bool)
#define T
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void fill_index_range(MutableSpan< T > span, const T start=0)
T mix4(const float4 &weights, const T &v0, const T &v1, const T &v2, const T &v3)
void to_static_type(const CPPType &cpp_type, Fn &&fn)
Insertion insert(const float3 &point_prev, const float3 &handle_prev, const float3 &handle_next, const float3 &point_next, float parameter)
float4 calculate_basis(const float parameter)
bool attribute_name_is_anonymous(const StringRef name)
AttrType cpp_type_to_attribute_type(const CPPType &type)
constexpr ParallelGrainSize grain_size(int grain_size)
static void sample_catmull_rom_curve_positions_handles(const bool cyclic, const IndexRange src_points, const Span< float3 > src_pos, const Span< int > dst_indices, const Span< float > dst_factors, const IndexRange dst_points, const int8_t dst_type, MutableSpan< float3 > dst_pos, MutableSpan< float3 > dst_left, MutableSpan< float3 > dst_right, MutableSpan< int8_t > dst_types_left, MutableSpan< int8_t > dst_types_right)
static AttributesForInterpolation retrieve_attribute_spans(const Span< StringRef > names, const CurvesGeometry &src_from_curves, const CurvesGeometry &src_to_curves, const bke::AttrDomain domain, CurvesGeometry &dst_curves)
static AttributesForInterpolation gather_curve_attributes_to_interpolate(const CurvesGeometry &from_curves, const CurvesGeometry &to_curves, CurvesGeometry &dst_curves)
static void mix_arrays(const Span< T > from, const Span< T > to, const float mix_factor, const MutableSpan< T > dst)
void interpolate_curves_with_samples(const bke::CurvesGeometry &from_curves, const bke::CurvesGeometry &to_curves, Span< int > from_curve_indices, Span< int > to_curve_indices, Span< int > from_sample_indices, Span< int > to_sample_indices, Span< float > from_sample_factors, Span< float > to_sample_factors, const IndexMask &dst_curve_mask, float mix_factor, bke::CurvesGeometry &dst_curves, IndexMaskMemory &memory)
static void sample_poly_curve_positions_handles(const bool cyclic, const Span< float3 > src_pos, const Span< int > dst_indices, const Span< float > dst_factors, const IndexRange dst_points, const int8_t dst_type, MutableSpan< float3 > dst_pos, MutableSpan< float3 > dst_left, MutableSpan< float3 > dst_right, MutableSpan< int8_t > dst_types_left, MutableSpan< int8_t > dst_types_right)
static void sample_curve_positions_and_handles(const bke::CurvesGeometry &src_curves, const Span< int > src_curve_indices, const OffsetIndices< int > dst_points_by_curve, const VArray< int8_t > dst_types, const IndexMask &dst_curve_mask, const Span< int > dst_sample_indices, const Span< float > dst_sample_factors, MutableSpan< float3 > dst_positions, MutableSpan< float3 > dst_handles_left, MutableSpan< float3 > dst_handles_right, MutableSpan< int8_t > dst_handle_types_left, MutableSpan< int8_t > dst_handle_types_right)
static float4 calculate_catmull_rom_basis_derivative(const float parameter)
static Array< float > build_point_to_sample_map(const Span< float3 > positions, const bool cyclic, const int samples_num)
static void mix_curve_type(const Span< int > from_curve_indices, const Span< int > to_curve_indices, const VArray< int8_t > &from_types, const VArray< int8_t > &to_types, const IndexMask &dst_curve_mask, MutableSpan< int8_t > dst_curve_types)
void sample_curve_padded(const Span< float3 > positions, bool cyclic, MutableSpan< int > r_indices, MutableSpan< float > r_factors)
static void sample_curve_attribute(const bke::CurvesGeometry &src_curves, const Span< int > src_curve_indices, const OffsetIndices< int > dst_points_by_curve, const GSpan src_data, const IndexMask &dst_curve_mask, const Span< int > dst_sample_indices, const Span< float > dst_sample_factors, GMutableSpan dst_data)
static void mix_handle_type_arrays(const Span< int8_t > src_from, const Span< int8_t > src_to, const IndexMask &group_selection, const OffsetIndices< int > groups, const MutableSpan< int8_t > dst)
static int4 get_catmull_rom_indices(const int src_index, const int src_index_last, const bool cyclic)
static void sample_curve_uniform(const bke::CurvesGeometry &curves, const int curve_index, const bool cyclic, const bool reverse, MutableSpan< int > r_segment_indices, MutableSpan< float > r_factors)
void interpolate_curves(const bke::CurvesGeometry &from_curves, const bke::CurvesGeometry &to_curves, Span< int > from_curve_indices, Span< int > to_curve_indices, const IndexMask &dst_curve_mask, Span< bool > dst_curve_flip_direction, float mix_factor, bke::CurvesGeometry &dst_curves, IndexMaskMemory &memory)
static bool interpolate_attribute_to_curves(const StringRef name, const std::array< int, CURVE_TYPES_NUM > &type_counts)
static void assign_samples_to_segments(const int num_dst_points, const Span< float3 > src_positions, const bool cyclic, MutableSpan< int > dst_sample_offsets)
static int8_t mix_handle_type(const int8_t from_type, const int8_t to_type)
static AttributesForInterpolation gather_point_attributes_to_interpolate(const CurvesGeometry &from_curves, const CurvesGeometry &to_curves, CurvesGeometry &dst_curves)
static bool interpolate_attribute_to_poly_curve(const StringRef name)
static void reverse_samples(const int points_num, MutableSpan< int > r_indices, MutableSpan< float > r_factors)
static void sample_bezier_curve_positions_handles(const bool cyclic, const IndexRange src_points, const Span< float3 > src_pos, const Span< float3 > src_handle_left, const Span< float3 > src_handle_right, const Span< int8_t > src_types_left, const Span< int8_t > src_types_right, const Span< int > dst_indices, const Span< float > dst_factors, const IndexRange dst_points, MutableSpan< float3 > dst_pos, MutableSpan< float3 > dst_left, MutableSpan< float3 > dst_right, MutableSpan< int8_t > dst_types_left, MutableSpan< int8_t > dst_types_right)
void sample_uniform_reverse(Span< float > accumulated_segment_lengths, bool include_first_point, MutableSpan< int > r_segment_indices, MutableSpan< float > r_factors)
void interpolate(const Span< T > src, const Span< int > indices, const Span< float > factors, MutableSpan< T > dst)
void sample_uniform(Span< float > accumulated_segment_lengths, bool include_last_point, MutableSpan< int > r_segment_indices, MutableSpan< float > r_factors)
T safe_divide(const T &a, const T &b)
T floor(const T &a)
T distance(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T ceil(const T &a)
T mod(const T &a, const T &b)
T round(const T &a)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< int32_t, 4 > int4
VecBase< float, 4 > float4
@ CURVE_TYPE_CATMULL_ROM
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Vector< bke::GSpanAttributeWriter > dst