Blender V4.3
curve_catmull_rom.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
9#include "BLI_task.hh"
10
11#include "BKE_attribute_math.hh"
12#include "BKE_curves.hh"
13
15
16int calculate_evaluated_num(const int points_num, const bool cyclic, const int resolution)
17{
18 const int eval_num = resolution * segments_num(points_num, cyclic);
19 if (cyclic) {
20 /* Make sure there is a single evaluated point for the single-point curve case. */
21 return std::max(eval_num, 1);
22 }
23 /* If the curve isn't cyclic, one last point is added to the final point. */
24 return eval_num + 1;
25}
26
27float4 calculate_basis(const float parameter)
28{
29 /* Adapted from Cycles #catmull_rom_basis_eval function. */
30 const float t = parameter;
31 const float s = 1.0f - parameter;
32 return {
33 -t * s * s,
34 2.0f + t * t * (3.0f * t - 5.0f),
35 2.0f + s * s * (3.0f * s - 5.0f),
36 -s * t * t,
37 };
38}
39
40template<typename T>
41static void evaluate_segment(const T &a, const T &b, const T &c, const T &d, MutableSpan<T> dst)
42{
43 const float step = 1.0f / dst.size();
44 dst.first() = b;
45 for (const int i : dst.index_range().drop_front(1)) {
46 dst[i] = interpolate<T>(a, b, c, d, i * step);
47 }
48}
49
55template<typename T, typename RangeForSegmentFn>
56static void interpolate_to_evaluated(const Span<T> src,
57 const bool cyclic,
58 const RangeForSegmentFn &range_fn,
60
61{
62 /* - First deal with one and two point curves need special attention.
63 * - Then evaluate the first and last segment(s) whose control points need to wrap around
64 * to the other side of the source array.
65 * - Finally evaluate all of the segments in the middle in parallel. */
66
67 if (src.size() == 1) {
68 dst.first() = src.first();
69 return;
70 }
71
72 const IndexRange first = range_fn(0);
73
74 if (src.size() == 2) {
75 evaluate_segment(src.first(), src.first(), src.last(), src.last(), dst.slice(first));
76 if (cyclic) {
77 const IndexRange last = range_fn(1);
78 evaluate_segment(src.last(), src.last(), src.first(), src.first(), dst.slice(last));
79 }
80 else {
81 dst.last() = src.last();
82 }
83 return;
84 }
85
86 const IndexRange second_to_last = range_fn(src.index_range().last(1));
87 const IndexRange last = range_fn(src.index_range().last());
88 if (cyclic) {
89 evaluate_segment(src.last(), src[0], src[1], src[2], dst.slice(first));
90 evaluate_segment(src.last(2), src.last(1), src.last(), src.first(), dst.slice(second_to_last));
91 evaluate_segment(src.last(1), src.last(), src[0], src[1], dst.slice(last));
92 }
93 else {
94 evaluate_segment(src[0], src[0], src[1], src[2], dst.slice(first));
95 evaluate_segment(src.last(2), src.last(1), src.last(), src.last(), dst.slice(second_to_last));
96 /* For non-cyclic curves, the last segment should always just have a single point. We could
97 * assert that the size of the provided range is 1 here, but that would require specializing
98 * the #range_fn implementation for the last point, which may have a performance cost. */
99 dst.last() = src.last();
100 }
101
102 /* Evaluate every segment that isn't the first or last. */
103 const IndexRange inner_range = src.index_range().drop_back(2).drop_front(1);
104 threading::parallel_for(inner_range, 512, [&](IndexRange range) {
105 for (const int i : range) {
106 const IndexRange segment = range_fn(i);
107 evaluate_segment(src[i - 1], src[i], src[i + 1], src[i + 2], dst.slice(segment));
108 }
109 });
110}
111
112template<typename T>
113static void interpolate_to_evaluated(const Span<T> src,
114 const bool cyclic,
115 const int resolution,
116 MutableSpan<T> dst)
117
118{
119 BLI_assert(dst.size() == calculate_evaluated_num(src.size(), cyclic, resolution));
121 src,
122 cyclic,
123 [resolution](const int segment_i) -> IndexRange {
124 return {segment_i * resolution, resolution};
125 },
126 dst);
127}
128
129template<typename T>
130static void interpolate_to_evaluated(const Span<T> src,
131 const bool cyclic,
132 const OffsetIndices<int> evaluated_offsets,
133 MutableSpan<T> dst)
134
135{
137 src,
138 cyclic,
139 [evaluated_offsets](const int segment_i) -> IndexRange {
140 return evaluated_offsets[segment_i];
141 },
142 dst);
143}
144
146 const bool cyclic,
147 const int resolution,
148 GMutableSpan dst)
149{
150 attribute_math::convert_to_static_type(src.type(), [&](auto dummy) {
151 using T = decltype(dummy);
152 interpolate_to_evaluated(src.typed<T>(), cyclic, resolution, dst.typed<T>());
153 });
154}
155
157 const bool cyclic,
158 const OffsetIndices<int> evaluated_offsets,
159 GMutableSpan dst)
160{
161 attribute_math::convert_to_static_type(src.type(), [&](auto dummy) {
162 using T = decltype(dummy);
163 interpolate_to_evaluated(src.typed<T>(), cyclic, evaluated_offsets, dst.typed<T>());
164 });
165}
166
167} // namespace blender::bke::curves::catmull_rom
Low-level operations for curves.
#define BLI_assert(a)
Definition BLI_assert.h:50
const CPPType & type() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
constexpr IndexRange drop_front(int64_t n) const
constexpr int64_t size() const
Definition BLI_span.hh:494
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:574
constexpr T & first() const
Definition BLI_span.hh:680
constexpr IndexRange index_range() const
Definition BLI_span.hh:671
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:690
constexpr const T & first() const
Definition BLI_span.hh:316
constexpr int64_t size() const
Definition BLI_span.hh:253
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:326
constexpr IndexRange index_range() const
Definition BLI_span.hh:402
local_group_size(16, 16) .push_constant(Type b
IndexRange range
void convert_to_static_type(const CPPType &cpp_type, const Func &func)
static void evaluate_segment(const T &a, const T &b, const T &c, const T &d, MutableSpan< T > dst)
T interpolate(const T &a, const T &b, const T &c, const T &d, const float parameter)
int calculate_evaluated_num(int points_num, bool cyclic, int resolution)
float4 calculate_basis(const float parameter)
void interpolate_to_evaluated(GSpan src, bool cyclic, int resolution, GMutableSpan dst)
int segments_num(const int points_num, const bool cyclic)
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:95