Blender V4.5
curve_bezier.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 <algorithm>
10
11#include "BLI_task.hh"
12
13#include "BKE_attribute_math.hh"
14#include "BKE_curves.hh"
15
17
18bool segment_is_vector(const Span<int8_t> handle_types_left,
19 const Span<int8_t> handle_types_right,
20 const int segment_index)
21{
22 BLI_assert(handle_types_left.index_range().drop_back(1).contains(segment_index));
23 return segment_is_vector(handle_types_right[segment_index],
24 handle_types_left[segment_index + 1]);
25}
26
27bool last_cyclic_segment_is_vector(const Span<int8_t> handle_types_left,
28 const Span<int8_t> handle_types_right)
29{
30 return segment_is_vector(handle_types_right.last(), handle_types_left.first());
31}
32
33void calculate_evaluated_offsets(const Span<int8_t> handle_types_left,
34 const Span<int8_t> handle_types_right,
35 const bool cyclic,
36 const int resolution,
37 MutableSpan<int> evaluated_offsets)
38{
39 const int size = handle_types_left.size();
40 BLI_assert(evaluated_offsets.size() == size + 1);
41
42 evaluated_offsets.first() = 0;
43 if (size == 1) {
44 evaluated_offsets.last() = 1;
45 return;
46 }
47
48 int offset = 0;
49 for (const int i : IndexRange(size - 1)) {
50 evaluated_offsets[i] = offset;
51 offset += segment_is_vector(handle_types_left, handle_types_right, i) ? 1 : resolution;
52 }
53
54 evaluated_offsets.last(1) = offset;
55 if (cyclic) {
56 offset += last_cyclic_segment_is_vector(handle_types_left, handle_types_right) ? 1 :
57 resolution;
58 }
59 else {
60 offset++;
61 }
62
63 evaluated_offsets.last() = offset;
64}
65
66Insertion insert(const float3 &point_prev,
67 const float3 &handle_prev,
68 const float3 &handle_next,
69 const float3 &point_next,
70 float parameter)
71{
72 /* De Casteljau Bezier subdivision. */
73 BLI_assert(parameter <= 1.0f && parameter >= 0.0f);
74
75 const float3 center_point = math::interpolate(handle_prev, handle_next, parameter);
76
78 result.handle_prev = math::interpolate(point_prev, handle_prev, parameter);
79 result.handle_next = math::interpolate(handle_next, point_next, parameter);
80 result.left_handle = math::interpolate(result.handle_prev, center_point, parameter);
81 result.right_handle = math::interpolate(center_point, result.handle_next, parameter);
82 result.position = math::interpolate(result.left_handle, result.right_handle, parameter);
83 return result;
84}
85
86static float3 calculate_aligned_handle(const float3 &position,
87 const float3 &other_handle,
88 const float3 &aligned_handle)
89{
90 /* Keep track of the old length of the opposite handle. */
91 const float length = math::distance(aligned_handle, position);
92 /* Set the other handle to directly opposite from the current handle. */
93 const float3 dir = math::normalize(other_handle - position);
94 return position - dir * length;
95}
96
97static void calculate_point_handles(const HandleType type_left,
98 const HandleType type_right,
99 const float3 position,
100 const float3 prev_position,
101 const float3 next_position,
102 float3 &left,
103 float3 &right)
104{
105 if (ELEM(BEZIER_HANDLE_AUTO, type_left, type_right)) {
106 const float3 prev_diff = position - prev_position;
107 const float3 next_diff = next_position - position;
108 float prev_len = math::length(prev_diff);
109 float next_len = math::length(next_diff);
110 if (prev_len == 0.0f) {
111 prev_len = 1.0f;
112 }
113 if (next_len == 0.0f) {
114 next_len = 1.0f;
115 }
116 const float3 dir = next_diff / next_len + prev_diff / prev_len;
117
118 /* The magic number 2.5614 is derived from approximating a circular arc at the control point.
119 * Given the constraints:
120 *
121 * - `P0=(0,1),P1=(c,1),P2=(1,c),P3=(1,0)`.
122 * - The first derivative of the curve must agree with the circular arc derivative at the
123 * endpoints.
124 * - Minimize the maximum radial drift.
125 * one can compute `c ≈ 0.5519150244935105707435627`.
126 * The distance from P0 to P3 is `sqrt(2)`.
127 *
128 * The magic factor for `len` is `(sqrt(2) / 0.5519150244935105707435627) ≈ 2.562375546255352`.
129 * In older code of blender a slightly worse approximation of 2.5614 is used. It's kept
130 * for compatibility.
131 *
132 * See https://spencermortensen.com/articles/bezier-circle/. */
133 const float len = math::length(dir) * 2.5614f;
134 if (len != 0.0f) {
135 if (type_left == BEZIER_HANDLE_AUTO) {
136 const float prev_len_clamped = std::min(prev_len, next_len * 5.0f);
137 left = position + dir * -(prev_len_clamped / len);
138 }
139 if (type_right == BEZIER_HANDLE_AUTO) {
140 const float next_len_clamped = std::min(next_len, prev_len * 5.0f);
141 right = position + dir * (next_len_clamped / len);
142 }
143 }
144 }
145
146 if (type_left == BEZIER_HANDLE_VECTOR) {
147 left = calculate_vector_handle(position, prev_position);
148 }
149
150 if (type_right == BEZIER_HANDLE_VECTOR) {
151 right = calculate_vector_handle(position, next_position);
152 }
153
154 /* When one of the handles is "aligned" handle, it must be aligned with the other, i.e. point in
155 * the opposite direction. Don't handle the case of two aligned handles, because code elsewhere
156 * should keep the pair consistent, and the relative locations aren't affected by other points
157 * anyway. */
158 if (type_left == BEZIER_HANDLE_ALIGN && type_right != BEZIER_HANDLE_ALIGN) {
159 left = calculate_aligned_handle(position, right, left);
160 }
161 else if (type_left != BEZIER_HANDLE_ALIGN && type_right == BEZIER_HANDLE_ALIGN) {
162 right = calculate_aligned_handle(position, left, right);
163 }
164}
165
166void set_handle_position(const float3 &position,
167 const HandleType type,
168 const HandleType type_other,
169 const float3 &new_handle,
170 float3 &handle,
171 float3 &handle_other)
172{
173 /* Don't bother when the handle positions are calculated automatically anyway. */
175 return;
176 }
177
178 handle = new_handle;
179 if (type_other == BEZIER_HANDLE_ALIGN) {
180 handle_other = calculate_aligned_handle(position, handle, handle_other);
181 }
182}
183
185 const Span<float3> positions,
186 const Span<float3> align_with,
187 MutableSpan<float3> align_handles)
188{
189 selection.foreach_index_optimized<int>(GrainSize(4096), [&](const int point) {
190 align_handles[point] = calculate_aligned_handle(
191 positions[point], align_with[point], align_handles[point]);
192 });
193}
194
195void calculate_auto_handles(const bool cyclic,
196 const Span<int8_t> types_left,
197 const Span<int8_t> types_right,
198 const Span<float3> positions,
199 MutableSpan<float3> positions_left,
200 MutableSpan<float3> positions_right)
201{
202 const int points_num = positions.size();
203 if (points_num == 1) {
204 return;
205 }
206
208 HandleType(types_right.first()),
209 positions.first(),
210 cyclic ? positions.last() : 2.0f * positions.first() - positions[1],
211 positions[1],
212 positions_left.first(),
213 positions_right.first());
214
215 threading::parallel_for(IndexRange(1, points_num - 2), 1024, [&](IndexRange range) {
216 for (const int i : range) {
218 HandleType(types_right[i]),
219 positions[i],
220 positions[i - 1],
221 positions[i + 1],
222 positions_left[i],
223 positions_right[i]);
224 }
225 });
226
228 HandleType(types_right.last()),
229 positions.last(),
230 positions.last(1),
231 cyclic ? positions.first() : 2.0f * positions.last() - positions.last(1),
232 positions_left.last(),
233 positions_right.last());
234}
235
236template<typename T>
238 const T &point_0, const T &point_1, const T &point_2, const T &point_3, MutableSpan<T> result)
239{
240 BLI_assert(result.size() > 0);
241 const float inv_len = 1.0f / float(result.size());
242 const float inv_len_squared = inv_len * inv_len;
243 const float inv_len_cubed = inv_len_squared * inv_len;
244
245 const T rt1 = 3.0f * (point_1 - point_0) * inv_len;
246 const T rt2 = 3.0f * (point_0 - 2.0f * point_1 + point_2) * inv_len_squared;
247 const T rt3 = (point_3 - point_0 + 3.0f * (point_1 - point_2)) * inv_len_cubed;
248
249 T q0 = point_0;
250 T q1 = rt1 + rt2 + rt3;
251 T q2 = 2.0f * rt2 + 6.0f * rt3;
252 T q3 = 6.0f * rt3;
253 for (const int i : result.index_range()) {
254 result[i] = q0;
255 q0 += q1;
256 q1 += q2;
257 q2 += q3;
258 }
259}
260template<>
261void evaluate_segment(const float3 &point_0,
262 const float3 &point_1,
263 const float3 &point_2,
264 const float3 &point_3,
266{
267 evaluate_segment_ex<float3>(point_0, point_1, point_2, point_3, result);
268}
269template<>
270void evaluate_segment(const float2 &point_0,
271 const float2 &point_1,
272 const float2 &point_2,
273 const float2 &point_3,
275{
276 evaluate_segment_ex<float2>(point_0, point_1, point_2, point_3, result);
277}
278
280 const Span<float3> handles_left,
281 const Span<float3> handles_right,
282 const OffsetIndices<int> evaluated_offsets,
283 MutableSpan<float3> evaluated_positions)
284{
285 BLI_assert(evaluated_offsets.total_size() == evaluated_positions.size());
286 if (evaluated_offsets.total_size() == 1) {
287 evaluated_positions.first() = positions.first();
288 return;
289 }
290
291 /* Evaluate the first segment. */
292 evaluate_segment(positions.first(),
293 handles_right.first(),
294 handles_left[1],
295 positions[1],
296 evaluated_positions.slice(evaluated_offsets[0]));
297
298 /* Give each task fewer segments as the resolution gets larger. */
299 const int grain_size = std::max<int>(evaluated_positions.size() / positions.size() * 32, 1);
300 const IndexRange inner_segments = positions.index_range().drop_back(1).drop_front(1);
301 threading::parallel_for(inner_segments, grain_size, [&](IndexRange range) {
302 for (const int i : range) {
303 const IndexRange evaluated_range = evaluated_offsets[i];
304 if (evaluated_range.size() == 1) {
305 evaluated_positions[evaluated_range.first()] = positions[i];
306 }
307 else {
308 evaluate_segment(positions[i],
309 handles_right[i],
310 handles_left[i + 1],
311 positions[i + 1],
312 evaluated_positions.slice(evaluated_range));
313 }
314 }
315 });
316
317 /* Evaluate the final cyclic segment if necessary. */
318 const IndexRange last_segment_points = evaluated_offsets[positions.index_range().last()];
319 if (last_segment_points.size() == 1) {
320 evaluated_positions.last() = positions.last();
321 }
322 else {
323 evaluate_segment(positions.last(),
324 handles_right.last(),
325 handles_left.first(),
326 positions.first(),
327 evaluated_positions.slice(last_segment_points));
328 }
329}
330
331template<typename T>
332static inline void linear_interpolation(const T &a, const T &b, MutableSpan<T> dst)
333{
334 dst.first() = a;
335 const float step = 1.0f / dst.size();
336 for (const int i : dst.index_range().drop_front(1)) {
337 dst[i] = attribute_math::mix2(i * step, a, b);
338 }
339}
340
341template<typename T>
342static void interpolate_to_evaluated(const Span<T> src,
343 const OffsetIndices<int> evaluated_offsets,
344 MutableSpan<T> dst)
345{
346 BLI_assert(!src.is_empty());
347 BLI_assert(evaluated_offsets.total_size() == dst.size());
348 if (src.size() == 1) {
349 BLI_assert(dst.size() == 1);
350 dst.first() = src.first();
351 return;
352 }
353
354 linear_interpolation(src.first(), src[1], dst.slice(evaluated_offsets[0]));
355
357 src.index_range().drop_back(1).drop_front(1), 512, [&](IndexRange range) {
358 for (const int i : range) {
359 const IndexRange segment = evaluated_offsets[i];
360 linear_interpolation(src[i], src[i + 1], dst.slice(segment));
361 }
362 });
363
364 const IndexRange last_segment = evaluated_offsets[src.index_range().last()];
365 linear_interpolation(src.last(), src.first(), dst.slice(last_segment));
366}
367
369 const OffsetIndices<int> evaluated_offsets,
370 GMutableSpan dst)
371{
372 attribute_math::convert_to_static_type(src.type(), [&](auto dummy) {
373 using T = decltype(dummy);
374 if constexpr (!std::is_void_v<attribute_math::DefaultMixer<T>>) {
375 interpolate_to_evaluated(src.typed<T>(), evaluated_offsets, dst.typed<T>());
376 }
377 });
378}
379
380} // namespace blender::bke::curves::bezier
Low-level operations for curves.
#define BLI_assert(a)
Definition BLI_assert.h:46
#define ELEM(...)
HandleType
@ BEZIER_HANDLE_ALIGN
@ BEZIER_HANDLE_VECTOR
@ BEZIER_HANDLE_AUTO
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
const CPPType & type() const
constexpr int64_t first() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr bool contains(int64_t value) const
constexpr IndexRange drop_front(int64_t n) const
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:573
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
constexpr const T & first() const
Definition BLI_span.hh:315
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:325
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
constexpr bool is_empty() const
Definition BLI_span.hh:260
void foreach_index_optimized(Fn &&fn) const
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
static int left
#define T
void convert_to_static_type(const CPPType &cpp_type, const Func &func)
T mix2(float factor, const T &a, const T &b)
Insertion insert(const float3 &point_prev, const float3 &handle_prev, const float3 &handle_next, const float3 &point_next, float parameter)
bool segment_is_vector(const HandleType left, const HandleType right)
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)
float3 calculate_vector_handle(const float3 &point, const float3 &next_point)
static void calculate_point_handles(const HandleType type_left, const HandleType type_right, const float3 position, const float3 prev_position, const float3 next_position, float3 &left, float3 &right)
void calculate_aligned_handles(const IndexMask &selection, Span< float3 > positions, Span< float3 > align_by, MutableSpan< float3 > align)
bool last_cyclic_segment_is_vector(Span< int8_t > handle_types_left, Span< int8_t > handle_types_right)
static float3 calculate_aligned_handle(const float3 &position, const float3 &other_handle, const float3 &aligned_handle)
void evaluate_segment(const T &point_0, const T &point_1, const T &point_2, const T &point_3, MutableSpan< T > result)
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)
void evaluate_segment_ex(const T &point_0, const T &point_1, const T &point_2, const T &point_3, MutableSpan< T > result)
void set_handle_position(const float3 &position, HandleType type, HandleType type_other, const float3 &new_handle, float3 &handle, float3 &handle_other)
static void linear_interpolation(const T &a, const T &b, MutableSpan< T > dst)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
T interpolate(const T &a, const T &b, const FactorT &t)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
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
VecBase< float, 2 > float2
VecBase< float, 3 > float3
i
Definition text_draw.cc:230
uint len