Blender V5.3
fcurve_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#include "testing/testing.h"
5
6#include "BKE_fcurve.hh"
7#include "BKE_gtest_base.hh"
8
9#include "ANIM_fcurve.hh"
10
11#include "ED_keyframing.hh"
12
13#include "DNA_anim_types.h"
14
16
17namespace blender::bke::tests {
18using namespace blender::animrig;
19
20/* Epsilon for floating point comparisons. */
21static const float EPSILON = 1e-7f;
22
24
26{
28 EXPECT_EQ(evaluate_fcurve(fcu, 47.0f), 0.0f);
29 BKE_fcurve_free(fcu);
30}
31
33{
35
36 const KeyframeSettings settings = get_keyframe_settings(false);
37 insert_vert_fcurve(fcu, {1.0f, 7.0f}, settings, INSERTKEY_NOFLAGS);
38 insert_vert_fcurve(fcu, {2.0f, 13.0f}, settings, INSERTKEY_NOFLAGS);
39 insert_vert_fcurve(fcu, {3.0f, 19.0f}, settings, INSERTKEY_NOFLAGS);
40
41 EXPECT_NEAR(evaluate_fcurve(fcu, 1.0f), 7.0f, EPSILON); /* hits 'on or before first' function */
42 EXPECT_NEAR(evaluate_fcurve(fcu, 2.0f), 13.0f, EPSILON); /* hits 'between' function */
43 EXPECT_NEAR(evaluate_fcurve(fcu, 3.0f), 19.0f, EPSILON); /* hits 'on or after last' function */
44
45 /* Also test within a specific time epsilon of the keys, as this was an issue in #39207.
46 * This epsilon is just slightly smaller than the epsilon given to
47 * BKE_fcurve_bezt_binarysearch_index_ex() in fcurve_eval_between_keyframes(), so it should hit
48 * the "exact" code path. */
49 float time_epsilon = 0.00008f;
50 EXPECT_NEAR(evaluate_fcurve(fcu, 2.0f - time_epsilon), 13.0f, EPSILON);
51 EXPECT_NEAR(evaluate_fcurve(fcu, 2.0f + time_epsilon), 13.0f, EPSILON);
52
53 BKE_fcurve_free(fcu);
54}
55
56TEST_F(EvaluateFCurveTest, InterpolationConstant)
57{
59
60 const KeyframeSettings settings = get_keyframe_settings(false);
61 insert_vert_fcurve(fcu, {1.0f, 7.0f}, settings, INSERTKEY_NOFLAGS);
62 insert_vert_fcurve(fcu, {2.0f, 13.0f}, settings, INSERTKEY_NOFLAGS);
63 EXPECT_EQ(fcu->bezt[0].vec[1][0], 1.0f);
64 EXPECT_EQ(fcu->bezt[0].vec[1][1], 7.0f);
65 EXPECT_EQ(fcu->bezt[1].vec[1][0], 2.0f);
66 EXPECT_EQ(fcu->bezt[1].vec[1][1], 13.0f);
67
68 fcu->bezt[0].ipo = BEZT_IPO_CONST;
69 fcu->bezt[1].ipo = BEZT_IPO_CONST;
70
71 EXPECT_NEAR(evaluate_fcurve(fcu, 1.25f), 7.0f, EPSILON);
72 EXPECT_NEAR(evaluate_fcurve(fcu, 1.50f), 7.0f, EPSILON);
73
74 BKE_fcurve_free(fcu);
75}
76
77TEST_F(EvaluateFCurveTest, InterpolationLinear)
78{
80
81 const KeyframeSettings settings = get_keyframe_settings(false);
82 insert_vert_fcurve(fcu, {1.0f, 7.0f}, settings, INSERTKEY_NOFLAGS);
83 insert_vert_fcurve(fcu, {2.0f, 13.0f}, settings, INSERTKEY_NOFLAGS);
84 EXPECT_EQ(fcu->bezt[0].vec[1][0], 1.0f);
85 EXPECT_EQ(fcu->bezt[0].vec[1][1], 7.0f);
86 EXPECT_EQ(fcu->bezt[1].vec[1][0], 2.0f);
87 EXPECT_EQ(fcu->bezt[1].vec[1][1], 13.0f);
88
89 fcu->bezt[0].ipo = BEZT_IPO_LIN;
90 fcu->bezt[1].ipo = BEZT_IPO_LIN;
91
92 EXPECT_NEAR(evaluate_fcurve(fcu, 1.25f), 8.5f, EPSILON);
93 EXPECT_NEAR(evaluate_fcurve(fcu, 1.50f), 10.0f, EPSILON);
94 EXPECT_NEAR(evaluate_fcurve(fcu, 1.75f), 11.5f, EPSILON);
95
96 BKE_fcurve_free(fcu);
97}
98
99TEST_F(EvaluateFCurveTest, InterpolationBezier)
100{
101 FCurve *fcu = BKE_fcurve_create();
102
103 const KeyframeSettings settings = get_keyframe_settings(false);
104 insert_vert_fcurve(fcu, {1.0f, 7.0f}, settings, INSERTKEY_NOFLAGS);
105 insert_vert_fcurve(fcu, {2.0f, 13.0f}, settings, INSERTKEY_NOFLAGS);
106 EXPECT_EQ(fcu->bezt[0].vec[1][0], 1.0f);
107 EXPECT_EQ(fcu->bezt[0].vec[1][1], 7.0f);
108 EXPECT_EQ(fcu->bezt[1].vec[1][0], 2.0f);
109 EXPECT_EQ(fcu->bezt[1].vec[1][1], 13.0f);
110
111 EXPECT_EQ(fcu->bezt[0].ipo, BEZT_IPO_BEZ);
112 EXPECT_EQ(fcu->bezt[1].ipo, BEZT_IPO_BEZ);
113
114 /* Test with default handles. */
115 EXPECT_NEAR(evaluate_fcurve(fcu, 1.25f), 7.8297067f, EPSILON);
116 EXPECT_NEAR(evaluate_fcurve(fcu, 1.50f), 10.0f, EPSILON);
117 EXPECT_NEAR(evaluate_fcurve(fcu, 1.75f), 12.170294f, EPSILON);
118
119 /* Test with modified handles. */
120 fcu->bezt[0].vec[0][0] = 0.71855f; /* left handle X */
121 fcu->bezt[0].vec[0][1] = 6.22482f; /* left handle Y */
122 fcu->bezt[0].vec[2][0] = 1.35148f; /* right handle X */
123 fcu->bezt[0].vec[2][1] = 7.96806f; /* right handle Y */
124
125 fcu->bezt[1].vec[0][0] = 1.66667f; /* left handle X */
126 fcu->bezt[1].vec[0][1] = 10.4136f; /* left handle Y */
127 fcu->bezt[1].vec[2][0] = 2.33333f; /* right handle X */
128 fcu->bezt[1].vec[2][1] = 15.5864f; /* right handle Y */
129
130 EXPECT_NEAR(evaluate_fcurve(fcu, 1.25f), 7.945497f, EPSILON);
131 EXPECT_NEAR(evaluate_fcurve(fcu, 1.50f), 9.3495407f, EPSILON);
132 EXPECT_NEAR(evaluate_fcurve(fcu, 1.75f), 11.088551f, EPSILON);
133
134 BKE_fcurve_free(fcu);
135}
136
137TEST_F(EvaluateFCurveTest, InterpolationBounce)
138{
139 FCurve *fcu = BKE_fcurve_create();
140
141 const KeyframeSettings settings = get_keyframe_settings(false);
142 insert_vert_fcurve(fcu, {1.0f, 7.0f}, settings, INSERTKEY_NOFLAGS);
143 insert_vert_fcurve(fcu, {2.0f, 13.0f}, settings, INSERTKEY_NOFLAGS);
144 EXPECT_EQ(fcu->bezt[0].vec[1][0], 1.0f);
145 EXPECT_EQ(fcu->bezt[0].vec[1][1], 7.0f);
146 EXPECT_EQ(fcu->bezt[1].vec[1][0], 2.0f);
147 EXPECT_EQ(fcu->bezt[1].vec[1][1], 13.0f);
148
149 fcu->bezt[0].ipo = BEZT_IPO_BOUNCE;
150 fcu->bezt[1].ipo = BEZT_IPO_BOUNCE;
151
152 fcu->bezt[0].easing = BEZT_IPO_EASE_IN;
154
155 EXPECT_NEAR(evaluate_fcurve(fcu, 1.4f), 8.3649998f, EPSILON);
156 EXPECT_NEAR(evaluate_fcurve(fcu, 1.5f), 8.4062500f, EPSILON);
157 EXPECT_NEAR(evaluate_fcurve(fcu, 1.8f), 11.184999f, EPSILON);
158
159 BKE_fcurve_free(fcu);
160}
161
162TEST_F(EvaluateFCurveTest, ExtrapolationLinearKeys)
163{
164 FCurve *fcu = BKE_fcurve_create();
165
166 const KeyframeSettings settings = get_keyframe_settings(false);
167 insert_vert_fcurve(fcu, {1.0f, 7.0f}, settings, INSERTKEY_NOFLAGS);
168 insert_vert_fcurve(fcu, {2.0f, 13.0f}, settings, INSERTKEY_NOFLAGS);
169 EXPECT_EQ(fcu->bezt[0].vec[1][0], 1.0f);
170 EXPECT_EQ(fcu->bezt[0].vec[1][1], 7.0f);
171 EXPECT_EQ(fcu->bezt[1].vec[1][0], 2.0f);
172 EXPECT_EQ(fcu->bezt[1].vec[1][1], 13.0f);
173
174 fcu->bezt[0].ipo = BEZT_IPO_LIN;
175 fcu->bezt[1].ipo = BEZT_IPO_LIN;
176
178 /* Before first keyframe. */
179 EXPECT_NEAR(evaluate_fcurve(fcu, 0.75f), 5.5f, EPSILON);
180 EXPECT_NEAR(evaluate_fcurve(fcu, 0.50f), 4.0f, EPSILON);
181 EXPECT_NEAR(evaluate_fcurve(fcu, -1.50f), -8.0f, EPSILON);
182 /* After last keyframe. */
183 EXPECT_NEAR(evaluate_fcurve(fcu, 2.75f), 17.5f, EPSILON);
184 EXPECT_NEAR(evaluate_fcurve(fcu, 3.50f), 22.0f, EPSILON);
185
187 /* Before first keyframe. */
188 EXPECT_NEAR(evaluate_fcurve(fcu, 0.75f), 7.0f, EPSILON);
189 EXPECT_NEAR(evaluate_fcurve(fcu, -1.50f), 7.0f, EPSILON);
190 /* After last keyframe. */
191 EXPECT_NEAR(evaluate_fcurve(fcu, 2.75f), 13.0f, EPSILON);
192 EXPECT_NEAR(evaluate_fcurve(fcu, 3.50f), 13.0f, EPSILON);
193
194 BKE_fcurve_free(fcu);
195}
196
197TEST_F(EvaluateFCurveTest, ExtrapolationBezierKeys)
198{
199 FCurve *fcu = BKE_fcurve_create();
200
201 const KeyframeSettings settings = get_keyframe_settings(false);
202 insert_vert_fcurve(fcu, {1.0f, 7.0f}, settings, INSERTKEY_NOFLAGS);
203 insert_vert_fcurve(fcu, {2.0f, 13.0f}, settings, INSERTKEY_NOFLAGS);
204 EXPECT_EQ(fcu->bezt[0].vec[1][0], 1.0f);
205 EXPECT_EQ(fcu->bezt[0].vec[1][1], 7.0f);
206 EXPECT_EQ(fcu->bezt[1].vec[1][0], 2.0f);
207 EXPECT_EQ(fcu->bezt[1].vec[1][1], 13.0f);
208
209 fcu->bezt[0].vec[0][0] = 0.71855f; /* left handle X */
210 fcu->bezt[0].vec[0][1] = 6.22482f; /* left handle Y */
211 fcu->bezt[0].vec[2][0] = 1.35148f; /* right handle X */
212 fcu->bezt[0].vec[2][1] = 7.96806f; /* right handle Y */
213
214 fcu->bezt[1].vec[0][0] = 1.66667f; /* left handle X */
215 fcu->bezt[1].vec[0][1] = 10.4136f; /* left handle Y */
216 fcu->bezt[1].vec[2][0] = 2.33333f; /* right handle X */
217 fcu->bezt[1].vec[2][1] = 15.5864f; /* right handle Y */
218
220 /* Before first keyframe. */
221 EXPECT_NEAR(evaluate_fcurve(fcu, 0.75f), 6.3114409f, EPSILON);
222 EXPECT_NEAR(evaluate_fcurve(fcu, -0.50f), 2.8686447f, EPSILON);
223 /* After last keyframe. */
224 EXPECT_NEAR(evaluate_fcurve(fcu, 2.75f), 18.81946f, EPSILON);
225 EXPECT_NEAR(evaluate_fcurve(fcu, 3.50f), 24.63892f, EPSILON);
226
228 /* Before first keyframe. */
229 EXPECT_NEAR(evaluate_fcurve(fcu, 0.75f), 7.0f, EPSILON);
230 EXPECT_NEAR(evaluate_fcurve(fcu, -1.50f), 7.0f, EPSILON);
231 /* After last keyframe. */
232 EXPECT_NEAR(evaluate_fcurve(fcu, 2.75f), 13.0f, EPSILON);
233 EXPECT_NEAR(evaluate_fcurve(fcu, 3.50f), 13.0f, EPSILON);
234
235 BKE_fcurve_free(fcu);
236}
237
239
241{
242 FCurve *fcu = BKE_fcurve_create();
243
244 const KeyframeSettings settings = get_keyframe_settings(false);
245 /* Insert two keyframes and set handles to something non-default. */
246 insert_vert_fcurve(fcu, {1.0f, 0.0f}, settings, INSERTKEY_NOFLAGS);
247 insert_vert_fcurve(fcu, {13.0f, 2.0f}, settings, INSERTKEY_NOFLAGS);
248 EXPECT_EQ(fcu->bezt[0].vec[1][0], 1.0f);
249 EXPECT_EQ(fcu->bezt[0].vec[1][1], 0.0f);
250 EXPECT_EQ(fcu->bezt[1].vec[1][0], 13.0f);
251 EXPECT_EQ(fcu->bezt[1].vec[1][1], 2.0f);
252
253 fcu->bezt[0].h1 = fcu->bezt[0].h2 = HD_FREE;
254 fcu->bezt[0].vec[0][0] = -5.0f;
255 fcu->bezt[0].vec[0][1] = 0.0f;
256 fcu->bezt[0].vec[2][0] = 2.0f;
257 fcu->bezt[0].vec[2][1] = 4.0f;
258
259 fcu->bezt[1].h1 = fcu->bezt[1].h2 = HD_FREE;
260 fcu->bezt[1].vec[0][0] = 13.0f;
261 fcu->bezt[1].vec[0][1] = -2.0f;
262 fcu->bezt[1].vec[2][0] = 16.0f;
263 fcu->bezt[1].vec[2][1] = -3.0f;
264
265 /* Create new keyframe point with defaults from insert_vert_fcurve(). */
266 BezTriple beztr;
267 const float x = 7.375f; /* at this X-coord, the FCurve should evaluate to 1.000f. */
268 const float y = 1.000f;
269 beztr.vec[0][0] = x - 1.0f;
270 beztr.vec[0][1] = y;
271 beztr.vec[1][0] = x;
272 beztr.vec[1][1] = y;
273 beztr.vec[2][0] = x + 1.0f;
274 beztr.vec[2][1] = y;
275 beztr.h1 = beztr.h2 = HD_AUTO_ANIM;
276 beztr.ipo = BEZT_IPO_BEZ;
277
278 /* This should update the existing handles as well as the new BezTriple. */
279 float y_delta;
280 BKE_fcurve_bezt_subdivide_handles(&beztr, &fcu->bezt[0], &fcu->bezt[1], &y_delta);
281
282 EXPECT_FLOAT_EQ(y_delta, 0.0f);
283
284 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[0][0], -5.0f); /* Left handle should not be touched. */
285 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[0][1], 0.0f);
286 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[1][0], 1.0f); /* Coordinates should not be touched. */
287 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[1][1], 0.0f);
288 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[2][0], 1.5f); /* Right handle should be updated. */
289 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[2][1], 2.0f);
290
291 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][0], 13.0f); /* Left handle should be updated. */
292 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][1], 0.0f);
293 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][0], 13.0f); /* Coordinates should not be touched. */
294 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][1], 2.0f);
295 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][0], 16.0f); /* Right handle should not be touched */
296 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][1], -3.0f);
297
298 EXPECT_FLOAT_EQ(beztr.vec[0][0], 4.5f); /* Left handle should be updated. */
299 EXPECT_FLOAT_EQ(beztr.vec[0][1], 1.5f);
300 EXPECT_FLOAT_EQ(beztr.vec[1][0], 7.375f); /* Coordinates should not be touched. */
301 EXPECT_FLOAT_EQ(beztr.vec[1][1], 1.0f);
302 EXPECT_FLOAT_EQ(beztr.vec[2][0], 10.250); /* Right handle should be updated. */
303 EXPECT_FLOAT_EQ(beztr.vec[2][1], 0.5);
304
305 BKE_fcurve_free(fcu);
306}
307
309
311{
312 FCurve *fcu = BKE_fcurve_create();
313
314 /* There should be no active keyframe with no points. */
316
317 const KeyframeSettings settings = get_keyframe_settings(false);
318 /* Check that adding new points sets the active index. */
319 insert_vert_fcurve(fcu, {1.0f, 7.5f}, settings, INSERTKEY_NOFLAGS);
320 EXPECT_EQ(fcu->bezt[0].vec[1][0], 1.0f);
321 EXPECT_EQ(fcu->bezt[0].vec[1][1], 7.5f);
323 insert_vert_fcurve(fcu, {8.0f, 15.0f}, settings, INSERTKEY_NOFLAGS);
324 EXPECT_EQ(fcu->bezt[1].vec[1][0], 8.0f);
325 EXPECT_EQ(fcu->bezt[1].vec[1][1], 15.0f);
327 insert_vert_fcurve(fcu, {14.0f, 8.2f}, settings, INSERTKEY_NOFLAGS);
328 EXPECT_EQ(fcu->bezt[2].vec[1][0], 14.0f);
329 EXPECT_EQ(fcu->bezt[2].vec[1][1], 8.2f);
331
332 /* Check clearing the index. */
333 BKE_fcurve_active_keyframe_set(fcu, nullptr);
336
337 /* Check a "normal" action. */
338 fcu->bezt[2].f2 |= BEZT_FLAG_SELECT;
341
342 /* Check setting an unselected keyframe as active. */
343 fcu->bezt[2].f1 = fcu->bezt[2].f2 = fcu->bezt[2].f3 = eBezTriple_Flag{};
344 EXPECT_BLI_ASSERT(BKE_fcurve_active_keyframe_set(fcu, &fcu->bezt[2]),
345 "active keyframe must be selected");
347
348 /* Check out of bounds (lower). */
349 BKE_fcurve_active_keyframe_set(fcu, fcu->bezt - 20);
351 << "Setting out-of-bounds value via the API should result in valid active_keyframe_index";
353
354 fcu->active_keyframe_index = -20;
356 << "Even with active_keyframe_index out of bounds, getting it via the API should produce a "
357 "valid value";
358
359 /* Check out of bounds (higher). */
362 << "Setting out-of-bounds value via the API should result in valid active_keyframe_index";
364
365 fcu->active_keyframe_index = fcu->totvert;
367 << "Even with active_keyframe_index out of bounds, getting it via the API should produce a "
368 "valid value";
369
370 BKE_fcurve_free(fcu);
371}
372
374
376{
377 FCurve *fcu = BKE_fcurve_create();
378
379 const KeyframeSettings settings = get_keyframe_settings(false);
380 insert_vert_fcurve(fcu, {1.0f, 7.5f}, settings, INSERTKEY_NOFLAGS);
381 insert_vert_fcurve(fcu, {8.0f, 15.0f}, settings, INSERTKEY_NOFLAGS);
382 insert_vert_fcurve(fcu, {14.0f, 8.2f}, settings, INSERTKEY_NOFLAGS);
383
384 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][0], 5.2671194f);
385 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][1], 15.0f);
386
387 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][0], 8.0f);
388 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][1], 15.0f);
389
390 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][0], 10.342469f);
391 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][1], 15.0f);
392
394
395 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][0], 5.2671194f) << "Left handle should not move in time";
396 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][1], 47.0f) << "Left handle value should have been updated";
397
398 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][0], 8.0f) << "Frame should not move in time";
399 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][1], 47.0f) << "Frame value should have been updated";
400
401 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][0], 10.342469f) << "Right handle should not move in time";
402 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][1], 47.0f) << "Right handle value should have been updated";
403
404 BKE_fcurve_free(fcu);
405}
406
408{
409 FCurve *fcu = BKE_fcurve_create();
410
411 const KeyframeSettings settings = get_keyframe_settings(false);
412 insert_vert_fcurve(fcu, {1.0f, 7.5f}, settings, INSERTKEY_NOFLAGS);
413 insert_vert_fcurve(fcu, {8.0f, 15.0f}, settings, INSERTKEY_NOFLAGS);
414 insert_vert_fcurve(fcu, {14.0f, 8.2f}, settings, INSERTKEY_NOFLAGS);
415
416 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][0], 5.2671194f);
417 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][1], 15.0f);
418
419 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][0], 8.0f);
420 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][1], 15.0f);
421
422 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][0], 10.342469f);
423 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][1], 15.0f);
424
426
427 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][0], 44.2671194f) << "Left handle time should be updated";
428 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[0][1], 15.0f) << "Left handle should not move in value";
429
430 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][0], 47.0f) << "Frame time should have been updated";
431 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][1], 15.0f) << "Frame should not move in value";
432
433 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][0], 49.342469f) << "Right handle time should be updated";
434 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[2][1], 15.0f) << "Right handle should not move in value";
435
436 BKE_fcurve_free(fcu);
437}
438
440{
441 FCurve *fcu = BKE_fcurve_create();
442
443 const KeyframeSettings settings = get_keyframe_settings(false);
444 insert_vert_fcurve(fcu, {1.0f, 7.5f}, settings, INSERTKEY_NOFLAGS);
445 insert_vert_fcurve(fcu, {4.0f, -15.0f}, settings, INSERTKEY_NOFLAGS);
446 insert_vert_fcurve(fcu, {8.0f, 15.0f}, settings, INSERTKEY_NOFLAGS);
447 insert_vert_fcurve(fcu, {14.0f, 8.2f}, settings, INSERTKEY_NOFLAGS);
448 insert_vert_fcurve(fcu, {18.2f, -20.0f}, settings, INSERTKEY_NOFLAGS);
449
450 for (int i = 0; i < fcu->totvert; i++) {
451 fcu->bezt[i].f1 &= ~BEZT_FLAG_SELECT;
452 fcu->bezt[i].f2 &= ~BEZT_FLAG_SELECT;
453 fcu->bezt[i].f3 &= ~BEZT_FLAG_SELECT;
454 }
455
456 float min, max;
457 bool success;
458
459 /* All keys. */
460 success = BKE_fcurve_calc_range(fcu, &min, &max, false);
461 EXPECT_TRUE(success) << "A non-empty FCurve should have a range.";
462 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[1][0], min);
463 EXPECT_FLOAT_EQ(fcu->bezt[4].vec[1][0], max);
464
465 /* Only selected. */
466 success = BKE_fcurve_calc_range(fcu, &min, &max, true);
467 EXPECT_FALSE(success)
468 << "Using selected keyframes only should not find a range if nothing is selected.";
469
470 fcu->bezt[1].f2 |= BEZT_FLAG_SELECT;
471 fcu->bezt[3].f2 |= BEZT_FLAG_SELECT;
472
473 success = BKE_fcurve_calc_range(fcu, &min, &max, true);
474 EXPECT_TRUE(success) << "Range of selected keyframes should have been found.";
475 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][0], min);
476 EXPECT_FLOAT_EQ(fcu->bezt[3].vec[1][0], max);
477
478 /* Curve samples. */
479 const int sample_start = 1;
480 const int sample_end = 20;
481 fcurve_store_samples(fcu, nullptr, sample_start, sample_end, fcurve_samplingcb_evalcurve);
482
483 success = BKE_fcurve_calc_range(fcu, &min, &max, true);
484 EXPECT_TRUE(success) << "FCurve samples should have a range.";
485
486 EXPECT_FLOAT_EQ(sample_start, min);
487 EXPECT_FLOAT_EQ(sample_end, max);
488
489 BKE_fcurve_free(fcu);
490}
491
493{
494 FCurve *fcu = BKE_fcurve_create();
495
496 const KeyframeSettings settings = get_keyframe_settings(false);
497 insert_vert_fcurve(fcu, {1.0f, 7.5f}, settings, INSERTKEY_NOFLAGS);
498 insert_vert_fcurve(fcu, {4.0f, -15.0f}, settings, INSERTKEY_NOFLAGS);
499 insert_vert_fcurve(fcu, {8.0f, 15.0f}, settings, INSERTKEY_NOFLAGS);
500 insert_vert_fcurve(fcu, {14.0f, 8.2f}, settings, INSERTKEY_NOFLAGS);
501 insert_vert_fcurve(fcu, {18.2f, -20.0f}, settings, INSERTKEY_NOFLAGS);
502
503 for (int i = 0; i < fcu->totvert; i++) {
504 fcu->bezt[i].f1 &= ~BEZT_FLAG_SELECT;
505 fcu->bezt[i].f2 &= ~BEZT_FLAG_SELECT;
506 fcu->bezt[i].f3 &= ~BEZT_FLAG_SELECT;
507 }
508
509 fcu->bezt[0].vec[0][0] = -5.0f;
510 fcu->bezt[4].vec[2][0] = 25.0f;
511
512 rctf bounds;
513 bool success;
514
515 /* All keys. */
516 success = BKE_fcurve_calc_bounds(fcu,
517 false /* select only */,
518 false /* include handles */,
519 nullptr /* frame range */,
520 &bounds);
521 EXPECT_TRUE(success) << "A non-empty FCurve should have bounds.";
522 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[1][0], bounds.xmin);
523 EXPECT_FLOAT_EQ(fcu->bezt[4].vec[1][0], bounds.xmax);
524 EXPECT_FLOAT_EQ(fcu->bezt[4].vec[1][1], bounds.ymin);
525 EXPECT_FLOAT_EQ(fcu->bezt[2].vec[1][1], bounds.ymax);
526
527 /* Only selected. */
528 success = BKE_fcurve_calc_bounds(fcu,
529 true /* select only */,
530 false /* include handles */,
531 nullptr /* frame range */,
532 &bounds);
533 EXPECT_FALSE(success)
534 << "Using selected keyframes only should not find bounds if nothing is selected.";
535
536 fcu->bezt[1].f2 |= BEZT_FLAG_SELECT;
537 fcu->bezt[3].f2 |= BEZT_FLAG_SELECT;
538
539 success = BKE_fcurve_calc_bounds(fcu,
540 true /* select only */,
541 false /* include handles */,
542 nullptr /* frame range */,
543 &bounds);
544 EXPECT_TRUE(success) << "Selected keys should have been found.";
545 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][0], bounds.xmin);
546 EXPECT_FLOAT_EQ(fcu->bezt[3].vec[1][0], bounds.xmax);
547 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][1], bounds.ymin);
548 EXPECT_FLOAT_EQ(fcu->bezt[3].vec[1][1], bounds.ymax);
549
550 /* Including handles. */
551 success = BKE_fcurve_calc_bounds(fcu,
552 false /* select only */,
553 true /* include handles */,
554 nullptr /* frame range */,
555 &bounds);
556 EXPECT_TRUE(success) << "A non-empty FCurve should have bounds including handles.";
557 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[0][0], bounds.xmin);
558 EXPECT_FLOAT_EQ(fcu->bezt[4].vec[2][0], bounds.xmax);
559 EXPECT_FLOAT_EQ(fcu->bezt[4].vec[1][1], bounds.ymin);
560 EXPECT_FLOAT_EQ(fcu->bezt[2].vec[1][1], bounds.ymax);
561
562 /* Range. */
563 float range[2];
564
565 range[0] = 25;
566 range[1] = 30;
567 success = BKE_fcurve_calc_bounds(
568 fcu, false /* select only */, false /* include handles */, range /* frame range */, &bounds);
569 EXPECT_FALSE(success) << "A frame range outside the range of keyframes should not find bounds.";
570
571 range[0] = 0;
572 range[1] = 18.2f;
573 success = BKE_fcurve_calc_bounds(
574 fcu, false /* select only */, false /* include handles */, range /* frame range */, &bounds);
575 EXPECT_TRUE(success) << "A frame range within the range of keyframes should find bounds.";
576 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[1][0], bounds.xmin);
577 EXPECT_FLOAT_EQ(fcu->bezt[3].vec[1][0], bounds.xmax);
578 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][1], bounds.ymin);
579 EXPECT_FLOAT_EQ(fcu->bezt[2].vec[1][1], bounds.ymax);
580
581 /* Range and handles. */
582 success = BKE_fcurve_calc_bounds(
583 fcu, false /* select only */, true /* include handles */, range /* frame range */, &bounds);
584 EXPECT_TRUE(success)
585 << "A frame range within the range of keyframes should find bounds with handles.";
586 EXPECT_FLOAT_EQ(fcu->bezt[0].vec[0][0], bounds.xmin);
587 EXPECT_FLOAT_EQ(fcu->bezt[3].vec[2][0], bounds.xmax);
588 EXPECT_FLOAT_EQ(fcu->bezt[1].vec[1][1], bounds.ymin);
589 EXPECT_FLOAT_EQ(fcu->bezt[2].vec[1][1], bounds.ymax);
590
591 /* Range, handles and only selection. */
592 range[0] = 8.0f;
593 range[1] = 18.2f;
594 success = BKE_fcurve_calc_bounds(
595 fcu, true /* select only */, true /* include handles */, range /* frame range */, &bounds);
596 EXPECT_TRUE(success)
597 << "A frame range within the range of keyframes should find bounds of selected keyframes.";
598 EXPECT_FLOAT_EQ(fcu->bezt[3].vec[0][0], bounds.xmin);
599 EXPECT_FLOAT_EQ(fcu->bezt[3].vec[2][0], bounds.xmax);
600 EXPECT_FLOAT_EQ(fcu->bezt[3].vec[2][1], bounds.ymin);
601 EXPECT_FLOAT_EQ(fcu->bezt[3].vec[0][1], bounds.ymax);
602
603 /* Curve samples. */
604 const int sample_start = 1;
605 const int sample_end = 20;
606 fcurve_store_samples(fcu, nullptr, sample_start, sample_end, fcurve_samplingcb_evalcurve);
607
608 success = BKE_fcurve_calc_bounds(fcu,
609 false /* select only */,
610 false /* include handles */,
611 nullptr /* frame range */,
612 &bounds);
613 EXPECT_TRUE(success) << "FCurve samples should have a range.";
614
615 EXPECT_FLOAT_EQ(sample_start, bounds.xmin);
616 EXPECT_FLOAT_EQ(sample_end, bounds.xmax);
617 EXPECT_FLOAT_EQ(-20.0f, bounds.ymin);
618 EXPECT_FLOAT_EQ(15.0f, bounds.ymax);
619
620 range[0] = 8.0f;
621 range[1] = 20.0f;
622 success = BKE_fcurve_calc_bounds(
623 fcu, false /* select only */, false /* include handles */, range /* frame range */, &bounds);
624 EXPECT_TRUE(success) << "FCurve samples should have a range.";
625
626 EXPECT_FLOAT_EQ(range[0], bounds.xmin);
627 EXPECT_FLOAT_EQ(range[1], bounds.xmax);
628 EXPECT_FLOAT_EQ(-20.0f, bounds.ymin);
629 EXPECT_FLOAT_EQ(15.0f, bounds.ymax);
630
631 range[0] = 20.1f;
632 range[1] = 30.0f;
633 success = BKE_fcurve_calc_bounds(
634 fcu, false /* select only */, false /* include handles */, range /* frame range */, &bounds);
635 EXPECT_FALSE(success)
636 << "A frame range outside the range of keyframe samples should not have bounds.";
637
638 BKE_fcurve_free(fcu);
639}
640
641static void set_key(FCurve *fcu, const int index, const float x, const float y)
642{
643 fcu->bezt[index].vec[0][0] = x - 0.5f;
644 fcu->bezt[index].vec[1][0] = x;
645 fcu->bezt[index].vec[2][0] = x + 0.5f;
646
647 fcu->bezt[index].vec[0][1] = y;
648 fcu->bezt[index].vec[1][1] = y;
649 fcu->bezt[index].vec[2][1] = y;
650}
651
653{
654 /* Create a curve with some duplicate keys. The first ones are all with Y=1, the later repeats
655 * increase Y-coordinates on every repeat. */
656 FCurve *fcu = BKE_fcurve_create();
657 ED_keyframes_add(fcu, 10); /* Avoid `insert_vert_fcurve`, that de-duplicates the keys. */
658 set_key(fcu, 0, 1.0f, 1.0f);
659 set_key(fcu, 1, 327.16f, 1.0f);
660 set_key(fcu, 2, 7.0f, 1.0f);
661 set_key(fcu, 3, 47.0f, 1.0f);
662 set_key(fcu, 4, 7.0f, 2.0f);
663 set_key(fcu, 5, 47.0f, 2.0f);
664 set_key(fcu, 6, 47.0f + BEZT_BINARYSEARCH_THRESH, 3.0f);
665 set_key(fcu, 7, 7.0f, 3.0f);
666 set_key(fcu, 8, 3.0f, 1.0f);
667 set_key(fcu, 9, 2.0f, 1.0f);
668 return fcu;
669}
670
671TEST_F(BKE_FCurveTest, sort_time_fcurve_stability)
672{
674 ASSERT_EQ(fcu->totvert, 10);
675
676 sort_time_fcurve(*fcu);
677
678 /* The sorting should be stable, i.e. retain the original order when the
679 * X-coordinates are identical. */
680 ASSERT_EQ(fcu->totvert, 10) << "sorting should not influence number of keys";
681 EXPECT_V2_NEAR(fcu->bezt[0].vec[1], float2(1.0f, 1.0f), 1e-3);
682 EXPECT_V2_NEAR(fcu->bezt[1].vec[1], float2(2.0f, 1.0f), 1e-3);
683 EXPECT_V2_NEAR(fcu->bezt[2].vec[1], float2(3.0f, 1.0f), 1e-3);
684 EXPECT_V2_NEAR(fcu->bezt[3].vec[1], float2(7.0f, 1.0f), 1e-3);
685 EXPECT_V2_NEAR(fcu->bezt[4].vec[1], float2(7.0f, 2.0f), 1e-3);
686 EXPECT_V2_NEAR(fcu->bezt[5].vec[1], float2(7.0f, 3.0f), 1e-3);
687 EXPECT_V2_NEAR(fcu->bezt[6].vec[1], float2(47.0f, 1.0f), 1e-3);
688 EXPECT_V2_NEAR(fcu->bezt[7].vec[1], float2(47.0f, 2.0f), 1e-3);
689 EXPECT_V2_NEAR(fcu->bezt[8].vec[1], float2(47.0f + BEZT_BINARYSEARCH_THRESH, 3.0f), 1e-3);
690 EXPECT_V2_NEAR(fcu->bezt[9].vec[1], float2(327.16f, 1.0f), 1e-3);
691
692 BKE_fcurve_free(fcu);
693}
694
696{
698 ASSERT_EQ(fcu->totvert, 10);
699 sort_time_fcurve(*fcu);
700
702 ASSERT_GE(fcu->totvert, 6); /* Protect against out-of-bounds access. */
703 EXPECT_EQ(fcu->totvert, 6); /* The actual expected value. */
704 EXPECT_V2_NEAR(fcu->bezt[0].vec[1], float2(1.0f, 1.0f), 1e-3);
705 EXPECT_V2_NEAR(fcu->bezt[1].vec[1], float2(2.0f, 1.0f), 1e-3);
706 EXPECT_V2_NEAR(fcu->bezt[2].vec[1], float2(3.0f, 1.0f), 1e-3);
707 EXPECT_V2_NEAR(fcu->bezt[3].vec[1], float2(7.0f, 3.0f), 1e-3);
708 EXPECT_V2_NEAR(fcu->bezt[4].vec[1], float2(47.0f, 3.0f), 1e-3);
709 EXPECT_V2_NEAR(fcu->bezt[5].vec[1], float2(327.16f, 1.0f), 1e-3);
710
711 BKE_fcurve_free(fcu);
712}
713
714TEST_F(BKE_FCurveTest, BKE_fcurve_deduplicate_keys_edge_cases)
715{
717 ASSERT_EQ(fcu->totvert, 10);
718
719 /* Update the 2nd and 2nd-to-last keys to test the edge cases. */
720 set_key(fcu, 0, 1, 1);
721 set_key(fcu, 1, 1, 2);
722 set_key(fcu, 8, 327.16f, 1);
723 set_key(fcu, 9, 327.16f, 2);
724
725 sort_time_fcurve(*fcu);
726
728 ASSERT_EQ(fcu->totvert, 4);
729 EXPECT_V2_NEAR(fcu->bezt[0].vec[1], float2(1.0f, 2.0f), 1e-3);
730 EXPECT_V2_NEAR(fcu->bezt[1].vec[1], float2(7.0f, 3.0f), 1e-3);
731 EXPECT_V2_NEAR(fcu->bezt[2].vec[1], float2(47.0f, 3.0f), 1e-3);
732 EXPECT_V2_NEAR(fcu->bezt[3].vec[1], float2(327.16f, 2.0f), 1e-3);
733
734 BKE_fcurve_free(fcu);
735}
736
737TEST_F(BKE_FCurveTest, BKE_fcurve_deduplicate_keys_prefer_whole_frames)
738{
740 ASSERT_EQ(fcu->totvert, 10);
741
742 /* Update the first key around 47.0 to be slightly before the frame. This gives us three keys on
743 * 47-epsilon, 47, and 47+epsilon. The keys at index 5 and 6 already have this value, so the
744 * `set_key` calls are unnecessary, but this way this test has a more local overview of the
745 * situation under test. */
746 set_key(fcu, 3, 47.0f - BEZT_BINARYSEARCH_THRESH, 1.0f);
747 set_key(fcu, 5, 47.0f, 2.0f);
748 set_key(fcu, 6, 47.0f + BEZT_BINARYSEARCH_THRESH, 3.0f);
749
750 sort_time_fcurve(*fcu);
751
753 ASSERT_EQ(fcu->totvert, 6);
754 EXPECT_V2_NEAR(fcu->bezt[0].vec[1], float2(1.0f, 1.0f), 1e-3);
755 EXPECT_V2_NEAR(fcu->bezt[1].vec[1], float2(2.0f, 1.0f), 1e-3);
756 EXPECT_V2_NEAR(fcu->bezt[2].vec[1], float2(3.0f, 1.0f), 1e-3);
757 EXPECT_V2_NEAR(fcu->bezt[3].vec[1], float2(7.0f, 3.0f), 1e-3);
758 EXPECT_V2_NEAR(fcu->bezt[4].vec[1], float2(47.0f, 3.0f), 1e-3);
759 EXPECT_V2_NEAR(fcu->bezt[5].vec[1], float2(327.16f, 1.0f), 1e-3);
760
761 BKE_fcurve_free(fcu);
762}
763
764} // namespace blender::bke::tests
Functions to modify FCurves.
#define BEZT_BINARYSEARCH_THRESH
#define FCURVE_ACTIVE_KEYFRAME_NONE
btScalar max
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
#define EXPECT_NEAR(result, expect, threshold)
KeyframeSettings get_keyframe_settings(bool from_userprefs)
SingleKeyingResult insert_vert_fcurve(FCurve *fcu, const float2 position, const KeyframeSettings &settings, eInsertKeyFlags flag)
Main Key-framing API call.
static const float EPSILON
static void set_key(FCurve *fcu, const int index, const float x, const float y)
static FCurve * testcurve_with_duplicates()
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
bool BKE_fcurve_calc_range(const FCurve *fcu, float *r_min, float *r_max, bool selected_keys_only)
void ED_keyframes_add(FCurve *fcu, int num_keys_to_add)
void BKE_fcurve_keyframe_move_value_with_handles(BezTriple *keyframe, float new_value)
bool BKE_fcurve_bezt_subdivide_handles(BezTriple *bezt, BezTriple *prev, BezTriple *next, float *r_pdelta)
void BKE_fcurve_keyframe_move_time_with_handles(BezTriple *keyframe, const float new_time)
VecBase< float, 2 > float2
bool BKE_fcurve_calc_bounds(const FCurve *fcu, bool selected_keys_only, bool include_handles, const float frame_range[2], rctf *r_bounds)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BKE_fcurve_free(FCurve *fcu)
void BKE_fcurve_active_keyframe_set(FCurve *fcu, const BezTriple *active_bezt)
int BKE_fcurve_active_keyframe_index(const FCurve *fcu)
void fcurve_store_samples(FCurve *fcu, void *data, int start, int end, FcuSampleFunc sample_cb)
void BKE_fcurve_deduplicate_keys(FCurve *fcu)
float fcurve_samplingcb_evalcurve(FCurve *fcu, void *data, float evaltime)
@ FCURVE_EXTRAPOLATE_CONSTANT
@ FCURVE_EXTRAPOLATE_LINEAR
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
FCurve * BKE_fcurve_create()
void sort_time_fcurve(FCurve &fcu)
float evaluate_fcurve(const FCurve *fcu, float evaltime)
#define min(a, b)
Definition sort.cc:38
eBezTriple_Flag f2
eBezTriple_Easing easing
eBezTriple_Handle h2
eBezTriple_Flag f3
eBezTriple_Flag f1
eBezTriple_Interpolation ipo
eBezTriple_Handle h1
eFCurve_Extend extend
unsigned int totvert