Blender V5.3
GEO_interpolate_curves_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_array_utils.hh"
6
7#include "BKE_curves.hh"
8#include "BKE_gtest_base.hh"
9
11
12#include "testing/testing.h"
13
14namespace blender::bke::tests {
15
17 public:
18 enum class TestCurveShape {
19 Zero,
21 Eight,
22 Helix,
23 };
24
25 static void create_test_shape(const TestCurveShape shape, MutableSpan<float3> positions)
26 {
27 switch (shape) {
29 positions.fill(float3(0.0f));
30 break;
32 for (const int point_i : positions.index_range()) {
33 const float angle = 2.0f * M_PI * float(point_i) / float(positions.size());
34 positions[point_i] = float3(math::cos(angle), math::sin(angle), 0.0f);
35 }
36 break;
38 for (const int point_i : positions.index_range()) {
39 const float angle = 2.0f * M_PI * float(point_i) / float(positions.size());
40 positions[point_i] = float3(math::cos(angle), math::sin(angle * 2.0f), 0.0f);
41 }
42 break;
44 const int turns = 3;
45 const float pitch = 0.3f;
46 for (const int point_i : positions.index_range()) {
47 const float factor = float(turns) * float(point_i) / float(positions.size() - 1);
48 const float angle = 2.0f * M_PI * factor;
49 const float height = pitch * factor;
50 positions[point_i] = float3(math::cos(angle), math::sin(angle), height);
51 }
52 break;
53 }
54 }
55
57 Span<bool> cyclic,
58 TestCurveShape shape)
59 {
60 BLI_assert(!offsets.is_empty());
61 const int curves_num = offsets.size() - 1;
62 BLI_assert(cyclic.size() == curves_num);
63 const int points_num = offsets.last();
64
65 bke::CurvesGeometry curves(points_num, curves_num);
66 curves.offsets_for_write().copy_from(offsets);
67 curves.cyclic_for_write().copy_from(cyclic);
68
69 MutableSpan<float3> positions = curves.positions_for_write();
70 for (const int curve_i : curves.curves_range()) {
71 const IndexRange points = curves.points_by_curve()[curve_i];
72 create_test_shape(shape, positions.slice(points));
73 }
74
75 /* Attribute storing original indices to test point remapping. */
76 SpanAttributeWriter<int> test_indices_writer =
77 curves.attributes_for_write().lookup_or_add_for_write_span<int>(
79 array_utils::fill_index_range(test_indices_writer.span);
80 test_indices_writer.finish();
81
82 return curves;
83 }
84
86 const int curve_index,
87 const bool reverse,
88 const Span<int> expected_indices,
89 const Span<float> expected_factors,
90 const float threshold = 1e-4f)
91 {
92 const int num_dst_points = expected_indices.size();
93 BLI_assert(expected_factors.size() == num_dst_points);
94
95 const bool cyclic = curves.cyclic()[curve_index];
96
97 Array<int> indices(num_dst_points, -9999);
98 Array<float> factors(num_dst_points, -12345.6f);
99 geometry::sample_curve_padded(curves, curve_index, cyclic, reverse, indices, factors);
100
101 EXPECT_EQ_SPAN(expected_indices, indices.as_span());
102
103 EXPECT_EQ(expected_factors.size(), factors.size());
104 if (expected_factors.size() == factors.size()) {
105 for (const int i : expected_factors.index_range()) {
106 EXPECT_NEAR(expected_factors[i], factors[i], threshold)
107 << "Element mismatch at index " << i;
108 }
109 }
110 }
111};
112
113TEST_F(GreasePencilInterpolate, sample_curve_empty_output)
114{
115 bke::CurvesGeometry curves = create_test_curves(
116 {0, 1, 3}, {false, false}, TestCurveShape::Eight);
117
118 test_sample_curve(curves, 0, false, {}, {});
119 test_sample_curve(curves, 1, false, {}, {});
120}
121
122TEST_F(GreasePencilInterpolate, sample_curve_same_length)
123{
124 bke::CurvesGeometry curves = create_test_curves(
125 {0, 1, 3, 13, 14, 16, 26}, {false, false, false, true, true, true}, TestCurveShape::Eight);
126
127 test_sample_curve(curves, 0, false, {0}, {0.0f});
128 test_sample_curve(curves, 0, true, {0}, {0.0f});
129
130 test_sample_curve(curves, 1, false, {0, 1}, {0.0f, 0.0f});
131 test_sample_curve(curves, 1, true, {1, 0}, {0.0f, 0.0f});
132
133 test_sample_curve(curves,
134 2,
135 false,
136 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
137 {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f});
138 test_sample_curve(curves,
139 2,
140 true,
141 {9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
142 {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f});
143
144 test_sample_curve(curves, 3, false, {0}, {0.0f});
145 test_sample_curve(curves, 3, true, {0}, {0.0f});
146
147 test_sample_curve(curves, 4, false, {0, 1}, {0.0f, 0.0f});
148 test_sample_curve(curves, 4, true, {1, 0}, {0.0f, 0.0f});
149
150 test_sample_curve(curves,
151 5,
152 false,
153 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
154 {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f});
155 test_sample_curve(curves,
156 5,
157 true,
158 {9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
159 {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f});
160}
161
162TEST_F(GreasePencilInterpolate, sample_curve_shorter)
163{
164 bke::CurvesGeometry curves = create_test_curves(
165 {0, 1, 3, 13, 14, 16, 26}, {false, false, false, true, true, true}, TestCurveShape::Eight);
166
167 test_sample_curve(curves, 1, false, {0}, {0.0f});
168 test_sample_curve(curves, 1, true, {1}, {0.0f});
169
170 test_sample_curve(curves, 2, false, {0, 2, 5, 9}, {0.0f, 0.82178f, 0.88113f, 0.0f});
171 test_sample_curve(curves, 2, true, {9, 5, 2, 0}, {0.0f, 0.88113f, 0.82178f, 0.0f});
172
173 test_sample_curve(curves, 4, false, {0}, {0.0f});
174 test_sample_curve(curves, 4, true, {1}, {0.0f});
175
176 test_sample_curve(curves, 5, false, {0, 2, 5, 7}, {0.0f, 0.5f, 0.0f, 0.5f});
177 test_sample_curve(curves, 5, true, {9, 6, 4, 1}, {0.0f, 0.50492f, 0.0f, 0.50492f});
178}
179
180TEST_F(GreasePencilInterpolate, sample_curve_longer)
181{
182 bke::CurvesGeometry curves = create_test_curves(
183 {0, 1, 3, 13, 14, 16, 26}, {false, false, false, true, true, true}, TestCurveShape::Eight);
184
185 test_sample_curve(curves,
186 1,
187 false,
188 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
189 {0.0f,
190 0.09091f,
191 0.18182f,
192 0.27273f,
193 0.36364f,
194 0.45455f,
195 0.54545f,
196 0.63636f,
197 0.72727f,
198 0.81818f,
199 0.90909f,
200 0.0f});
201 test_sample_curve(curves,
202 1,
203 true,
204 {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
205 {0.0f,
206 0.90909f,
207 0.81818f,
208 0.72727f,
209 0.63636f,
210 0.54545f,
211 0.45455f,
212 0.36364f,
213 0.27273f,
214 0.18182f,
215 0.09091f,
216 0.0f});
217
218 test_sample_curve(curves,
219 2,
220 false,
221 {0, 1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 9},
222 {0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f});
223 test_sample_curve(curves,
224 2,
225 true,
226 {9, 8, 7, 6, 6, 5, 4, 3, 2, 2, 1, 0},
227 {0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f});
228
229 test_sample_curve(curves,
230 4,
231 false,
232 {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1},
233 {0.0f,
234 0.16667f,
235 0.33333f,
236 0.5f,
237 0.66667f,
238 0.83333f,
239 0.0f,
240 0.16667f,
241 0.33333f,
242 0.5f,
243 0.66667f,
244 0.83333f});
245 test_sample_curve(curves,
246 4,
247 true,
248 {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
249 {0.83333f,
250 0.66667f,
251 0.5f,
252 0.33333f,
253 0.16667f,
254 0.0f,
255 0.83333f,
256 0.66667f,
257 0.5f,
258 0.33333f,
259 0.16667f,
260 0.0f});
261
262 test_sample_curve(curves,
263 5,
264 false,
265 {0, 1, 2, 2, 3, 4, 5, 6, 7, 7, 8, 9},
266 {0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f});
267 test_sample_curve(curves,
268 5,
269 true,
270 {9, 8, 7, 6, 6, 5, 4, 3, 2, 1, 1, 0},
271 {0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f});
272}
273
274TEST_F(GreasePencilInterpolate, sample_zero_length_curve)
275{
276 bke::CurvesGeometry curves = create_test_curves(
277 {0, 10, 20}, {false, true}, TestCurveShape::Zero);
278
279 test_sample_curve(curves,
280 0,
281 false,
282 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
283 {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f});
284 test_sample_curve(curves,
285 1,
286 false,
287 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
288 {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f});
289}
290
291} // namespace blender::bke::tests
Low-level operations for curves.
#define M_PI
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
int64_t size() const
Definition BLI_array.hh:267
Span< T > as_span() const
Definition BLI_array.hh:254
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 IndexRange index_range() const
Definition BLI_span.hh:676
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
static void create_test_shape(const TestCurveShape shape, MutableSpan< float3 > positions)
void test_sample_curve(const bke::CurvesGeometry &curves, const int curve_index, const bool reverse, const Span< int > expected_indices, const Span< float > expected_factors, const float threshold=1e-4f)
static bke::CurvesGeometry create_test_curves(Span< int > offsets, Span< bool > cyclic, TestCurveShape shape)
#define EXPECT_NEAR(result, expect, threshold)
void fill_index_range(MutableSpan< T > span, const T start=0)
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
void sample_curve_padded(const Span< float3 > positions, bool cyclic, MutableSpan< int > r_indices, MutableSpan< float > r_factors)
T cos(const AngleRadianBase< T > &a)
T sin(const AngleRadianBase< T > &a)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
VecBase< float, 3 > float3
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float