Blender V4.3
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
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
184void calculate_auto_handles(const bool cyclic,
185 const Span<int8_t> types_left,
186 const Span<int8_t> types_right,
187 const Span<float3> positions,
188 MutableSpan<float3> positions_left,
189 MutableSpan<float3> positions_right)
190{
191 const int points_num = positions.size();
192 if (points_num == 1) {
193 return;
194 }
195
197 HandleType(types_right.first()),
198 positions.first(),
199 cyclic ? positions.last() : 2.0f * positions.first() - positions[1],
200 positions[1],
201 positions_left.first(),
202 positions_right.first());
203
204 threading::parallel_for(IndexRange(1, points_num - 2), 1024, [&](IndexRange range) {
205 for (const int i : range) {
206 calculate_point_handles(HandleType(types_left[i]),
207 HandleType(types_right[i]),
208 positions[i],
209 positions[i - 1],
210 positions[i + 1],
211 positions_left[i],
212 positions_right[i]);
213 }
214 });
215
217 HandleType(types_right.last()),
218 positions.last(),
219 positions.last(1),
220 cyclic ? positions.first() : 2.0f * positions.last() - positions.last(1),
221 positions_left.last(),
222 positions_right.last());
223}
224
225template<typename T>
227 const T &point_0, const T &point_1, const T &point_2, const T &point_3, MutableSpan<T> result)
228{
229 BLI_assert(result.size() > 0);
230 const float inv_len = 1.0f / float(result.size());
231 const float inv_len_squared = inv_len * inv_len;
232 const float inv_len_cubed = inv_len_squared * inv_len;
233
234 const T rt1 = 3.0f * (point_1 - point_0) * inv_len;
235 const T rt2 = 3.0f * (point_0 - 2.0f * point_1 + point_2) * inv_len_squared;
236 const T rt3 = (point_3 - point_0 + 3.0f * (point_1 - point_2)) * inv_len_cubed;
237
238 T q0 = point_0;
239 T q1 = rt1 + rt2 + rt3;
240 T q2 = 2.0f * rt2 + 6.0f * rt3;
241 T q3 = 6.0f * rt3;
242 for (const int i : result.index_range()) {
243 result[i] = q0;
244 q0 += q1;
245 q1 += q2;
246 q2 += q3;
247 }
248}
249template<>
250void evaluate_segment(const float3 &point_0,
251 const float3 &point_1,
252 const float3 &point_2,
253 const float3 &point_3,
254 MutableSpan<float3> result)
255{
256 evaluate_segment_ex<float3>(point_0, point_1, point_2, point_3, result);
257}
258template<>
259void evaluate_segment(const float2 &point_0,
260 const float2 &point_1,
261 const float2 &point_2,
262 const float2 &point_3,
263 MutableSpan<float2> result)
264{
265 evaluate_segment_ex<float2>(point_0, point_1, point_2, point_3, result);
266}
267
269 const Span<float3> handles_left,
270 const Span<float3> handles_right,
271 const OffsetIndices<int> evaluated_offsets,
272 MutableSpan<float3> evaluated_positions)
273{
274 BLI_assert(evaluated_offsets.total_size() == evaluated_positions.size());
275 if (evaluated_offsets.total_size() == 1) {
276 evaluated_positions.first() = positions.first();
277 return;
278 }
279
280 /* Evaluate the first segment. */
281 evaluate_segment(positions.first(),
282 handles_right.first(),
283 handles_left[1],
284 positions[1],
285 evaluated_positions.slice(evaluated_offsets[0]));
286
287 /* Give each task fewer segments as the resolution gets larger. */
288 const int grain_size = std::max<int>(evaluated_positions.size() / positions.size() * 32, 1);
289 const IndexRange inner_segments = positions.index_range().drop_back(1).drop_front(1);
290 threading::parallel_for(inner_segments, grain_size, [&](IndexRange range) {
291 for (const int i : range) {
292 const IndexRange evaluated_range = evaluated_offsets[i];
293 if (evaluated_range.size() == 1) {
294 evaluated_positions[evaluated_range.first()] = positions[i];
295 }
296 else {
297 evaluate_segment(positions[i],
298 handles_right[i],
299 handles_left[i + 1],
300 positions[i + 1],
301 evaluated_positions.slice(evaluated_range));
302 }
303 }
304 });
305
306 /* Evaluate the final cyclic segment if necessary. */
307 const IndexRange last_segment_points = evaluated_offsets[positions.index_range().last()];
308 if (last_segment_points.size() == 1) {
309 evaluated_positions.last() = positions.last();
310 }
311 else {
312 evaluate_segment(positions.last(),
313 handles_right.last(),
314 handles_left.first(),
315 positions.first(),
316 evaluated_positions.slice(last_segment_points));
317 }
318}
319
320template<typename T>
321static inline void linear_interpolation(const T &a, const T &b, MutableSpan<T> dst)
322{
323 dst.first() = a;
324 const float step = 1.0f / dst.size();
325 for (const int i : dst.index_range().drop_front(1)) {
326 dst[i] = attribute_math::mix2(i * step, a, b);
327 }
328}
329
330template<typename T>
331static void interpolate_to_evaluated(const Span<T> src,
332 const OffsetIndices<int> evaluated_offsets,
333 MutableSpan<T> dst)
334{
335 BLI_assert(!src.is_empty());
336 BLI_assert(evaluated_offsets.total_size() == dst.size());
337 if (src.size() == 1) {
338 BLI_assert(dst.size() == 1);
339 dst.first() = src.first();
340 return;
341 }
342
343 linear_interpolation(src.first(), src[1], dst.slice(evaluated_offsets[0]));
344
346 src.index_range().drop_back(1).drop_front(1), 512, [&](IndexRange range) {
347 for (const int i : range) {
348 const IndexRange segment = evaluated_offsets[i];
349 linear_interpolation(src[i], src[i + 1], dst.slice(segment));
350 }
351 });
352
353 const IndexRange last_segment = evaluated_offsets[src.index_range().last()];
354 linear_interpolation(src.last(), src.first(), dst.slice(last_segment));
355}
356
358 const OffsetIndices<int> evaluated_offsets,
359 GMutableSpan dst)
360{
361 attribute_math::convert_to_static_type(src.type(), [&](auto dummy) {
362 using T = decltype(dummy);
363 if constexpr (!std::is_void_v<attribute_math::DefaultMixer<T>>) {
364 interpolate_to_evaluated(src.typed<T>(), evaluated_offsets, dst.typed<T>());
365 }
366 });
367}
368
369} // namespace blender::bke::curves::bezier
Low-level operations for curves.
#define BLI_assert(a)
Definition BLI_assert.h:50
#define ELEM(...)
HandleType
@ BEZIER_HANDLE_ALIGN
@ BEZIER_HANDLE_VECTOR
@ BEZIER_HANDLE_AUTO
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
const CPPType & type() const
constexpr int64_t first() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t size() const
constexpr bool contains(int64_t value) const
constexpr IndexRange index_range() 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
constexpr bool is_empty() const
Definition BLI_span.hh:261
local_group_size(16, 16) .push_constant(Type b
int len
draw_view in_light_buf[] float
IndexRange range
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)
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:95