Blender V5.3
blenkernel/intern/fcurve.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors, Joshua Leung. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cfloat>
10#include <cmath>
11#include <cstddef>
12#include <cstdio>
13#include <cstring>
14
15#include "MEM_guardedalloc.h"
16
17#include "ANIM_action.hh"
18
19#include "DNA_action_types.h"
20#include "DNA_anim_types.h"
21#include "DNA_curve_types.h"
22
23#include "BLI_easing.hh"
24#include "BLI_ghash.hh"
25#include "BLI_listbase.hh"
26#include "BLI_math_vector_c.hh"
28#include "BLI_rect.hh"
29#include "BLI_sort_utils.hh"
30#include "BLI_string.hh"
31#include "BLI_string_utils.hh"
32#include "BLI_task.hh"
33#include "BLI_vector_set.hh"
34
35#include "BLT_translation.hh"
36
37#include "BKE_anim_data.hh"
38#include "BKE_animsys.hh"
39#include "BKE_context.hh"
40#include "BKE_curve.hh"
41#include "BKE_fcurve.hh"
42#include "BKE_fcurve_driver.h"
43#include "BKE_global.hh"
44#include "BKE_idprop.hh"
45#include "BKE_lib_query.hh"
46#include "BKE_nla.hh"
47
48#include "BLO_read_write.hh"
49
50#include "RNA_access.hh"
51#include "RNA_path.hh"
52
53#include "CLG_log.h"
54
55namespace blender {
56
57#define SMALL -1.0e-10
58#define SELECT 1
59
60static CLG_LogRef LOG = {"anim.fcurve"};
61
62/* -------------------------------------------------------------------- */
65
67{
68 FCurve *fcu = MEM_new<FCurve>(__func__);
69 fcu->runtime = MEM_new<FCurveRuntime>(__func__);
70 return fcu;
71}
72
74
75/* -------------------------------------------------------------------- */
78
80{
81 if (fcu == nullptr) {
82 return;
83 }
84
85 /* Free curve data. */
86 MEM_SAFE_DELETE(fcu->bezt);
87 MEM_SAFE_DELETE(fcu->fpt);
88
89 /* Free RNA-path, as this were allocated when getting the path string. */
90 MEM_SAFE_DELETE(fcu->rna_path_ptr);
91
92 /* Free extra data - i.e. modifiers, and driver. */
95
96 /* Free the f-curve itself. */
97 MEM_delete(fcu->runtime);
98 MEM_delete(fcu);
99}
100
101void BKE_fcurves_free(ListBaseT<FCurve> *list)
102{
103 /* Sanity check. */
104 if (list == nullptr) {
105 return;
106 }
107
108 /* Free data, no need to call #BLI_remlink before freeing each curve,
109 * as we store reference to next, and freeing only touches the curve it's given. */
110 FCurve *fcn = nullptr;
111 for (FCurve *fcu = list->first(); fcu; fcu = fcn) {
112 fcn = fcu->next;
113 BKE_fcurve_free(fcu);
114 }
115
116 /* Clear pointers just in case. */
117 list->clear_no_delete();
118}
119
121
122/* -------------------------------------------------------------------- */
125
127{
128 /* Sanity check. */
129 if (fcu == nullptr) {
130 return nullptr;
131 }
132
133 /* Make a copy. */
134 FCurve *fcu_d = MEM_dupalloc(fcu);
135 fcu_d->runtime = MEM_new<bke::FCurveRuntime>(__func__, *fcu->runtime);
136
137 fcu_d->next = fcu_d->prev = nullptr;
138 fcu_d->grp = nullptr;
139
140 /* Copy curve data. */
141 fcu_d->bezt = MEM_dupalloc(fcu_d->bezt);
142 fcu_d->fpt = MEM_dupalloc(fcu_d->fpt);
143
144 /* Copy rna-path. */
145 fcu_d->rna_path_ptr = MEM_dupalloc(fcu_d->rna_path_ptr);
146
147 /* Copy driver. */
148 fcu_d->driver = fcurve_copy_driver(fcu_d->driver);
149
150 /* Copy modifiers. */
151 copy_fmodifiers(&fcu_d->modifiers, &fcu->modifiers);
152
153 /* Return new data. */
154 return fcu_d;
155}
156
157void BKE_fcurves_copy(ListBaseT<FCurve> *dst, ListBaseT<FCurve> *src)
158{
159 /* Sanity checks. */
160 if (ELEM(nullptr, dst, src)) {
161 return;
162 }
163
164 /* Clear destination list first. */
165 dst->clear_no_delete();
166
167 /* Copy one-by-one. */
168 for (FCurve &sfcu : *src) {
169 FCurve *dfcu = BKE_fcurve_copy(&sfcu);
170 BLI_addtail(dst, dfcu);
171 }
172}
173
174void FCurve::rna_path_set(const StringRef path)
175{
176 MEM_SAFE_DELETE(this->rna_path_ptr);
177 this->rna_path_ptr = BLI_strdupn(path.data(), path.size());
179 StringRefNull(this->rna_path_ptr, path.size()));
180}
181
182void FCurve::rna_path_set_move(char *path)
183{
184 MEM_SAFE_DELETE(this->rna_path_ptr);
185 this->rna_path_ptr = path;
187}
188
189StringRefNull FCurve::rna_path() const
190{
191 return this->rna_path_ptr ? StringRefNull(this->rna_path_ptr) : StringRefNull("");
192}
193
194ParsedRNAPathRef FCurve::rna_path_parsed() const
195{
196 if (!this->runtime->parsed_rna_path) {
197 return {};
198 }
199 return *this->runtime->parsed_rna_path;
200}
201
202void BKE_fmodifier_name_set(FModifier *fcm, const char *name)
203{
204 /* Copy new Modifier name. */
205 STRNCPY(fcm->name, name);
206
207 /* Set default modifier name when name parameter is an empty string.
208 * Ensure the name is unique. */
210 ListBaseT<FModifier> list = BLI_listbase_from_link(fcm);
212 fcm,
214 '.',
215 offsetof(FModifier, name),
216 sizeof(fcm->name));
217}
218
219void BKE_fmodifier_ensure_flag(ListBaseT<FModifier> *modifiers)
220{
221 for (FModifier &fcm : *modifiers) {
222 const FModifierTypeInfo *fmi = get_fmodifier_typeinfo(fcm.type);
223 if (fmi && fmi->requires_flag & FMI_REQUIRES_ORIGINAL_DATA) {
224 SET_FLAG_FROM_TEST(fcm.flag, &fcm != modifiers->first(), FMODIFIER_FLAG_DISABLED);
225 }
226 }
227}
228
230{
231 ChannelDriver *driver = fcu->driver;
232
233 if (driver != nullptr) {
234 for (DriverVar &dvar : driver->variables) {
235 /* only used targets */
238 }
240 }
241 }
242}
243
244/* ----------------- Finding F-Curves -------------------------- */
245
247 ID *id, void *data, StructRNA *type, const char *prop_name, int index, bool *r_driven)
248{
249 /* Anim vars */
251
252 /* Rna vars */
253 PropertyRNA *prop;
254
255 if (r_driven) {
256 *r_driven = false;
257 }
258
259 /* Only use the current action ??? */
260 if (ELEM(nullptr, adt, adt->action)) {
261 return nullptr;
262 }
263
265 prop = RNA_struct_find_property(&ptr, prop_name);
266 if (prop == nullptr) {
267 return nullptr;
268 }
269
270 const std::optional<std::string> path = RNA_path_from_ID_to_property(&ptr, prop);
271 if (!path) {
272 return nullptr;
273 }
274
275 /* FIXME: The way drivers are handled here (always nullptr-ifying `fcu`) is very weird, this
276 * needs to be re-checked I think?. */
277 bool is_driven = false;
279 adt, path->c_str(), index, nullptr, &is_driven);
280 if (is_driven) {
281 if (r_driven != nullptr) {
282 *r_driven = is_driven;
283 }
284 fcu = nullptr;
285 }
286
287 return fcu;
288}
289
290FCurve *BKE_fcurve_find(ListBaseT<FCurve> *list, const char rna_path[], const int array_index)
291{
292 /* Sanity checks. */
293 if (ELEM(nullptr, list, rna_path) || array_index < 0) {
294 return nullptr;
295 }
296
297 /* Check paths of curves, then array indices... */
298 for (FCurve &fcu : *list) {
299 /* Check indices first, much cheaper than a string comparison. */
300 /* Simple string-compare (this assumes that they have the same root...) */
301 if (fcu.array_index == array_index && fcu.rna_path_ptr && fcu.rna_path_ptr[0] == rna_path[0] &&
302 STREQ(fcu.rna_path_ptr, rna_path)) [[unlikely]]
303 {
304 return &fcu;
305 }
306 }
307
308 return nullptr;
309}
310
312
313/* -------------------------------------------------------------------- */
316
318{
319 /* Sanity checks. */
320 if (ELEM(nullptr, fcu_iter, rna_path)) {
321 return nullptr;
322 }
323
324 /* Check paths of curves, then array indices... */
325 for (FCurve *fcu = fcu_iter; fcu; fcu = fcu->next) {
326 /* Simple string-compare (this assumes that they have the same root...) */
327 if (fcu->rna_path_ptr && STREQ(fcu->rna_path_ptr, rna_path)) {
328 return fcu;
329 }
330 }
331
332 return nullptr;
333}
334
336 AnimData *animdata, const char *rna_path, int rna_index, bAction **r_action, bool *r_driven)
337{
338 if (r_driven != nullptr) {
339 *r_driven = false;
340 }
341 if (r_action != nullptr) {
342 *r_action = nullptr;
343 }
344
346 animdata->action, animdata->slot_handle, {rna_path, rna_index});
347 if (fcurve) {
348 /* Action takes priority over drivers. */
349 if (r_action) {
350 *r_action = animdata->action;
351 }
352 return fcurve;
353 }
354
355 /* If not animated, check if driven. */
356 const bool has_drivers = !animdata->drivers.is_empty();
357 if (has_drivers) {
358 FCurve *fcu = BKE_fcurve_find(&animdata->drivers, rna_path, rna_index);
359
360 if (fcu != nullptr) {
361 if (r_driven != nullptr) {
362 *r_driven = true;
363 }
364 return fcu;
365 }
366 }
367
368 return nullptr;
369}
370
372 PropertyRNA *prop,
373 int rnaindex,
374 AnimData **r_adt,
375 bAction **r_action,
376 bool *r_driven,
377 bool *r_special)
378{
380 nullptr, ptr, prop, rnaindex, r_adt, r_action, r_driven, r_special);
381}
382
384 const PointerRNA *ptr,
385 PropertyRNA *prop,
386 int rnaindex,
387 AnimData **r_animdata,
388 bAction **r_action,
389 bool *r_driven,
390 bool *r_special)
391{
392 if (r_animdata != nullptr) {
393 *r_animdata = nullptr;
394 }
395 if (r_action != nullptr) {
396 *r_action = nullptr;
397 }
398 if (r_driven != nullptr) {
399 *r_driven = false;
400 }
401 if (r_special) {
402 *r_special = false;
403 }
404
405 /* Special case for NLA Control Curves... */
407 NlaStrip *strip = static_cast<NlaStrip *>(ptr->data);
408
409 /* Set the special flag, since it cannot be a normal action/driver
410 * if we've been told to start looking here...
411 */
412 if (r_special) {
413 *r_special = true;
414 }
415 if (r_driven) {
416 *r_driven = false;
417 }
418 if (r_animdata) {
419 *r_animdata = nullptr;
420 }
421 if (r_action) {
422 *r_action = nullptr;
423 }
424
425 /* The F-Curve either exists or it doesn't here... */
426 return BKE_fcurve_find(&strip->fcurves, RNA_property_identifier(prop), rnaindex);
427 }
428
429 /* There must be some RNA-pointer + property combo. */
430 if (!prop || !ptr->owner_id || !RNA_property_animateable(ptr, prop)) {
431 return nullptr;
432 }
433
435 if (adt == nullptr) {
436 return nullptr;
437 }
438
439 /* XXX This function call can become a performance bottleneck. */
440 const std::optional<std::string> rna_path = RNA_path_from_ID_to_property(ptr, prop);
441 if (!rna_path) {
442 return nullptr;
443 }
444
445 /* Standard F-Curve from animdata - Animation (Action) or Drivers. */
447 adt, rna_path->c_str(), rnaindex, r_action, r_driven);
448
449 if (fcu != nullptr && r_animdata != nullptr) {
450 *r_animdata = adt;
451 }
452
453 return fcu;
454}
455
457
458/* -------------------------------------------------------------------- */
461
462/* Binary search algorithm for finding where to insert BezTriple,
463 * with optional argument for precision required.
464 * Returns the index to insert at (data already at that index will be offset if replace is 0)
465 */
467 const float frame,
468 const int arraylen,
469 const float threshold,
470 bool *r_replace)
471{
472 int start = 0, end = arraylen;
473
474 /* Initialize replace-flag first. */
475 *r_replace = false;
476
477 /* Sneaky optimizations (don't go through searching process if...):
478 * - Keyframe to be added is to be added out of current bounds.
479 * - Keyframe to be added would replace one of the existing ones on bounds.
480 */
481 if (arraylen <= 0 || array == nullptr) {
482 CLOG_WARN(&LOG, "encountered invalid array");
483 return 0;
484 }
485
486 /* Check whether to add before/after/on. */
487 /* 'First' Keyframe (when only one keyframe, this case is used) */
488 float framenum = array[0].vec[1][0];
489 if (IS_EQT(frame, framenum, threshold)) {
490 *r_replace = true;
491 return 0;
492 }
493 if (frame < framenum) {
494 return 0;
495 }
496
497 /* 'Last' Keyframe */
498 framenum = array[(arraylen - 1)].vec[1][0];
499 if (IS_EQT(frame, framenum, threshold)) {
500 *r_replace = true;
501 return (arraylen - 1);
502 }
503 if (frame > framenum) {
504 return arraylen;
505 }
506
507 while (start <= end) {
508 /* Compute and get midpoint. */
509
510 /* We calculate the midpoint this way to avoid int overflows... */
511 const int mid = start + ((end - start) / 2);
512
513 const float midfra = array[mid].vec[1][0];
514
515 /* Check if exactly equal to midpoint. */
516 if (IS_EQT(frame, midfra, threshold)) {
517 *r_replace = true;
518 return mid;
519 }
520
521 /* Repeat in upper/lower half. */
522 if (frame > midfra) {
523 start = mid + 1;
524 }
525 else {
526 end = mid - 1;
527 }
528 }
529
530 /* Not found, so return where to place it. */
531 return start;
532}
533
535 const float frame,
536 const int arraylen,
537 bool *r_replace)
538{
539 /* This is just a wrapper which uses the default threshold. */
541 array, frame, arraylen, BEZT_BINARYSEARCH_THRESH, r_replace);
542}
543
544/* ...................................... */
545
553static bool get_bounding_bezt_indices(const FCurve *fcu,
554 const bool selected_keys_only,
555 const float frame_range[2],
556 int *r_first,
557 int *r_last)
558{
559 /* Sanity checks. */
560 if (fcu->bezt == nullptr) {
561 return false;
562 }
563
564 *r_first = 0;
565 *r_last = fcu->totvert - 1;
566
567 bool found = false;
568 if (frame_range != nullptr) {
569 /* If a range is passed in find the first and last keyframe within that range. */
570 bool replace = false;
572 fcu->bezt, frame_range[0], fcu->totvert, &replace);
574 fcu->bezt, frame_range[1], fcu->totvert, &replace);
575
576 /* If first and last index are the same, no keyframes were found in the range. */
577 if (*r_first == *r_last) {
578 return false;
579 }
580
581 /* The binary search returns an index where a keyframe would be inserted,
582 * so it needs to be clamped to ensure it is in range of the array. */
583 *r_first = clamp_i(*r_first, 0, fcu->totvert - 1);
584 *r_last = clamp_i(*r_last - 1, 0, fcu->totvert - 1);
585 }
586
587 /* Only include selected items? */
588 if (selected_keys_only) {
589 /* Find first selected. */
590 for (int i = *r_first; i <= *r_last; i++) {
591 BezTriple *bezt = &fcu->bezt[i];
592 if (BEZT_ISSEL_ANY(bezt)) {
593 *r_first = i;
594 found = true;
595 break;
596 }
597 }
598
599 /* Find last selected. */
600 for (int i = *r_last; i >= *r_first; i--) {
601 BezTriple *bezt = &fcu->bezt[i];
602 if (BEZT_ISSEL_ANY(bezt)) {
603 *r_last = i;
604 found = true;
605 break;
606 }
607 }
608 }
609 else {
610 found = true;
611 }
612
613 return found;
614}
615
616static void calculate_bezt_bounds_x(BezTriple *bezt_array,
617 const int index_range[2],
618 const bool include_handles,
619 float *r_min,
620 float *r_max)
621{
622 *r_min = bezt_array[index_range[0]].vec[1][0];
623 *r_max = bezt_array[index_range[1]].vec[1][0];
624
625 if (include_handles) {
626 /* Need to check all handles because they might extend beyond their neighboring keys. */
627 for (int i = index_range[0]; i <= index_range[1]; i++) {
628 const BezTriple *bezt = &bezt_array[i];
629 *r_min = std::min({*r_min, bezt->vec[0][0], bezt->vec[1][0]});
630 *r_max = std::max({*r_max, bezt->vec[1][0], bezt->vec[2][0]});
631 }
632 }
633}
634
635static void calculate_bezt_bounds_y(BezTriple *bezt_array,
636 const int index_range[2],
637 const bool selected_keys_only,
638 const bool include_handles,
639 float *r_min,
640 float *r_max)
641{
642 *r_min = bezt_array[index_range[0]].vec[1][1];
643 *r_max = bezt_array[index_range[0]].vec[1][1];
644
645 for (int i = index_range[0]; i <= index_range[1]; i++) {
646 const BezTriple *bezt = &bezt_array[i];
647
648 if (selected_keys_only && !BEZT_ISSEL_ANY(bezt)) {
649 continue;
650 }
651
652 *r_min = min_ff(*r_min, bezt->vec[1][1]);
653 *r_max = max_ff(*r_max, bezt->vec[1][1]);
654
655 if (include_handles) {
656 *r_min = std::min({*r_min, bezt->vec[0][1], bezt->vec[2][1]});
657 *r_max = std::max({*r_max, bezt->vec[0][1], bezt->vec[2][1]});
658 }
659 }
660}
661
662static bool calculate_bezt_bounds(const FCurve *fcu,
663 const bool selected_keys_only,
664 const bool include_handles,
665 const float frame_range[2],
666 rctf *r_bounds)
667{
668 int index_range[2];
669 const bool found_indices = get_bounding_bezt_indices(
670 fcu, selected_keys_only, frame_range, &index_range[0], &index_range[1]);
671 if (!found_indices) {
672 return false;
673 }
675 fcu->bezt, index_range, include_handles, &r_bounds->xmin, &r_bounds->xmax);
677 index_range,
678 selected_keys_only,
679 include_handles,
680 &r_bounds->ymin,
681 &r_bounds->ymax);
682 return true;
683}
684
685static bool calculate_fpt_bounds(const FCurve *fcu, const float frame_range[2], rctf *r_bounds)
686{
687 r_bounds->xmin = INFINITY;
688 r_bounds->xmax = -INFINITY;
689 r_bounds->ymin = INFINITY;
690 r_bounds->ymax = -INFINITY;
691
692 const int first_index = 0;
693 const int last_index = fcu->totvert - 1;
694 int start_index = first_index;
695 int end_index = last_index;
696
697 if (frame_range != nullptr) {
698 /* Start index can be calculated because fpt has a key on every full frame. */
699 const float start_index_f = frame_range[0] - fcu->fpt[0].vec[0];
700 const float end_index_f = start_index_f + frame_range[1] - frame_range[0];
701
702 if (start_index_f > fcu->totvert - 1 || end_index_f < 0) {
703 /* Range is outside of keyframe samples. */
704 return false;
705 }
706
707 /* Range might be partially covering keyframe samples. */
708 start_index = clamp_i(start_index_f, 0, fcu->totvert - 1);
709 end_index = clamp_i(end_index_f, 0, fcu->totvert - 1);
710 }
711
712 /* X range can be directly calculated from end verts. */
713 r_bounds->xmin = fcu->fpt[start_index].vec[0];
714 r_bounds->xmax = fcu->fpt[end_index].vec[0];
715
716 for (int i = start_index; i <= end_index; i++) {
717 r_bounds->ymin = min_ff(r_bounds->ymin, fcu->fpt[i].vec[1]);
718 r_bounds->ymax = max_ff(r_bounds->ymax, fcu->fpt[i].vec[1]);
719 }
720
721 return BLI_rctf_is_valid(r_bounds);
722}
723
725 const bool selected_keys_only,
726 const bool include_handles,
727 const float frame_range[2],
728 rctf *r_bounds)
729{
730 if (fcu->totvert == 0) {
731 return false;
732 }
733
734 if (fcu->bezt) {
735 const bool found_bounds = calculate_bezt_bounds(
736 fcu, selected_keys_only, include_handles, frame_range, r_bounds);
737 return found_bounds;
738 }
739
740 if (fcu->fpt) {
741 const bool founds_bounds = calculate_fpt_bounds(fcu, frame_range, r_bounds);
742 return founds_bounds;
743 }
744
745 return false;
746}
747
749 float *r_min,
750 float *r_max,
751 const bool selected_keys_only)
752{
753 float min = 0.0f;
754 float max = 0.0f;
755 bool foundvert = false;
756
757 if (fcu->totvert == 0) {
758 return false;
759 }
760
761 if (fcu->bezt) {
762 int index_range[2];
763 foundvert = get_bounding_bezt_indices(
764 fcu, selected_keys_only, nullptr, &index_range[0], &index_range[1]);
765 if (!foundvert) {
766 return false;
767 }
768 const bool include_handles = false;
769 calculate_bezt_bounds_x(fcu->bezt, index_range, include_handles, &min, &max);
770 }
771 else if (fcu->fpt) {
772 min = fcu->fpt[0].vec[0];
773 max = fcu->fpt[fcu->totvert - 1].vec[0];
774
775 foundvert = true;
776 }
777
778 *r_min = min;
779 *r_max = max;
780
781 return foundvert;
782}
783
785 const float interval)
786{
787 /* Use `1e-3f` as the smallest possible value since these are converted to integers
788 * and we can be sure `MAXFRAME / 1e-3f < INT_MAX` as it's around half the size. */
789 const double interval_db = max_ff(interval, 1e-3f);
790 VectorSet<int> frames_unique;
791 for (FCurve *fcu : fcurve_array) {
792 for (int i = 0; i < fcu->totvert; i++) {
793 const BezTriple *bezt = &fcu->bezt[i];
794 const double value = round(double(bezt->vec[1][0]) / interval_db);
795 BLI_assert(value > INT_MIN && value < INT_MAX);
796 frames_unique.add(int(value));
797 }
798 }
799
800 const size_t frames_len = frames_unique.size();
801 Array<float> frames(frames_len);
802
803 for (const int i : frames_unique.index_range()) {
804 const int value = frames_unique[i];
805 frames[i] = double(value) * interval_db;
806 }
807
808 qsort(frames.data(), frames_len, sizeof(float), BLI_sortutil_cmp_float);
809 return frames;
810}
811
813{
814 return BKE_fcurves_calc_keyed_frames_ex(fcurve_array, 1.0f);
815}
816
818
819/* -------------------------------------------------------------------- */
822
823void BKE_fcurve_active_keyframe_set(FCurve *fcu, const BezTriple *active_bezt)
824{
825 if (active_bezt == nullptr) {
827 return;
828 }
829
830 /* Gracefully handle out-of-bounds pointers. Ideally this would do a BLI_assert() as well, but
831 * then the unit tests would break in debug mode. */
832 const ptrdiff_t offset = active_bezt - fcu->bezt;
833 if (offset < 0 || offset >= fcu->totvert) {
835 return;
836 }
837
838 /* The active keyframe should always be selected. */
839 BLI_assert_msg(BEZT_ISSEL_ANY(active_bezt), "active keyframe must be selected");
840
841 fcu->active_keyframe_index = int(offset);
842}
843
845{
846 const int active_keyframe_index = fcu->active_keyframe_index;
847
848 /* Array access boundary checks. */
849 if (fcu->bezt == nullptr || active_keyframe_index >= fcu->totvert || active_keyframe_index < 0) {
851 }
852
853 const BezTriple *active_bezt = &fcu->bezt[active_keyframe_index];
854 if (((active_bezt->f1 | active_bezt->f2 | active_bezt->f3) & SELECT) == 0) {
855 /* The active keyframe should always be selected. If it's not selected, it can't be active. */
857 }
858
859 return active_keyframe_index;
860}
861
863
864void BKE_fcurve_keyframe_move_time_with_handles(BezTriple *keyframe, const float new_time)
865{
866 const float time_delta = new_time - keyframe->vec[1][0];
867 keyframe->vec[0][0] += time_delta;
868 keyframe->vec[1][0] = new_time;
869 keyframe->vec[2][0] += time_delta;
870}
871
872void BKE_fcurve_keyframe_move_value_with_handles(BezTriple *keyframe, const float new_value)
873{
874 const float value_delta = new_value - keyframe->vec[1][1];
875 keyframe->vec[0][1] += value_delta;
876 keyframe->vec[1][1] = new_value;
877 keyframe->vec[2][1] += value_delta;
878}
879
880/* -------------------------------------------------------------------- */
883
885{
886 /* F-Curve must not have samples - samples are mutually exclusive of keyframes. */
887 if (fcu.fpt) {
888 return false;
889 }
890
891 /* If it has modifiers, none of these should "drastically" alter the curve. */
892 if (fcu.modifiers.first()) {
893 /* Check modifiers from last to first, as last will be more influential. */
894 /* TODO: optionally, only check modifier if it is the active one... (Joshua Leung 2010) */
895 for (const FModifier &fcm : fcu.modifiers.items_reversed()) {
896 /* Ignore if muted/disabled. */
898 continue;
899 }
900
901 /* Type checks. */
902 switch (fcm.type) {
903 /* Clearly harmless - do nothing. */
907 break;
908
909 /* Sometimes harmful - depending on whether they're "additive" or not. */
911 FMod_Generator *data = static_cast<FMod_Generator *>(fcm.data);
912
913 if ((data->flag & FCM_GENERATOR_ADDITIVE) == 0) {
914 return false;
915 }
916 break;
917 }
919 FMod_FunctionGenerator *data = static_cast<FMod_FunctionGenerator *>(fcm.data);
920
921 if ((data->flag & FCM_GENERATOR_ADDITIVE) == 0) {
922 return false;
923 }
924 break;
925 }
926 /* Always harmful - cannot allow. */
927 default:
928 return false;
929 }
930 }
931 }
932
933 /* Keyframes are usable. */
934 return true;
935}
936
938{
939 return ((fcu.flag & FCURVE_PROTECTED) || (fcu.grp && (fcu.grp->flag & AGRP_PROTECTED)));
940}
941
943{
944 int i;
945 BezTriple *bezt;
946 for (bezt = fcu.bezt, i = 0; i < fcu.totvert; ++i, ++bezt) {
947 if ((bezt->f2 & SELECT) != 0) {
948 return true;
949 }
950 }
951 return false;
952}
953
955{
956 if (!fcu.bezt) {
957 return;
958 }
959 for (int i = 0; i < fcu.totvert; i++) {
960 BEZT_DESEL_ALL(&fcu.bezt[i]);
961 }
962}
963
965{
966 /* F-Curve's keyframes must be "usable" (i.e. visible + have an effect on final result) */
967 if (BKE_fcurve_are_keyframes_usable(fcu) == 0) {
968 return false;
969 }
970
971 /* F-Curve must currently be editable too. */
972 if (BKE_fcurve_is_protected(fcu)) {
973 return false;
974 }
975
976 /* F-Curve is keyframable. */
977 return true;
978}
979
981
982/* -------------------------------------------------------------------- */
985
986/* Some utilities for working with FPoints (i.e. 'sampled' animation curve data, such as
987 * data imported from BVH/motion-capture files), which are specialized for use with high density
988 * datasets, which BezTriples/Keyframe data are ill equipped to do. */
989
990float fcurve_samplingcb_evalcurve(FCurve *fcu, void * /*data*/, float evaltime)
991{
992 /* Assume any interference from drivers on the curve is intended... */
993 return evaluate_fcurve(fcu, evaltime);
994}
995
996void fcurve_store_samples(FCurve *fcu, void *data, int start, int end, FcuSampleFunc sample_cb)
997{
998 /* Sanity checks. */
999 /* TODO: make these tests report errors using reports not CLOG's (Joshua Leung 2009) */
1000 if (ELEM(nullptr, fcu, sample_cb)) {
1001 CLOG_ERROR(&LOG, "No F-Curve with F-Curve Modifiers to Bake");
1002 return;
1003 }
1004 if (start > end) {
1005 CLOG_ERROR(&LOG, "Error: Frame range for Sampled F-Curve creation is inappropriate");
1006 return;
1007 }
1008
1009 /* Set up sample data. */
1010 FPoint *new_fpt;
1011 FPoint *fpt = new_fpt = MEM_new_array_zeroed<FPoint>((end - start + 1), "FPoint Samples");
1012
1013 /* Use the sampling callback at 1-frame intervals from start to end frames. */
1014 for (int cfra = start; cfra <= end; cfra++, fpt++) {
1015 fpt->vec[0] = float(cfra);
1016 fpt->vec[1] = sample_cb(fcu, data, float(cfra));
1017 }
1018
1019 /* Free any existing sample/keyframe data on curve. */
1020 if (fcu->bezt) {
1021 MEM_delete(fcu->bezt);
1022 }
1023 if (fcu->fpt) {
1024 MEM_delete(fcu->fpt);
1025 }
1026
1027 /* Store the samples. */
1028 fcu->bezt = nullptr;
1029 fcu->fpt = new_fpt;
1030 fcu->totvert = end - start + 1;
1031}
1032
1034{
1035 bezt->f1 = bezt->f2 = bezt->f3 = BEZT_FLAG_SELECT;
1036 /* Baked FCurve points always use linear interpolation. */
1037 bezt->ipo = BEZT_IPO_LIN;
1038 bezt->h1 = bezt->h2 = HD_AUTO_ANIM;
1039}
1040
1041void fcurve_samples_to_keyframes(FCurve *fcu, const int start, const int end)
1042{
1043
1044 /* Sanity checks. */
1045 /* TODO: make these tests report errors using reports not CLOG's (Joshua Leung 2009). */
1046 if (fcu == nullptr) {
1047 CLOG_ERROR(&LOG, "No F-Curve with F-Curve Modifiers to Un-Bake");
1048 return;
1049 }
1050
1051 if (start > end) {
1052 CLOG_ERROR(&LOG, "Error: Frame range to unbake F-Curve is inappropriate");
1053 return;
1054 }
1055
1056 if (fcu->fpt == nullptr) {
1057 /* No data to unbake. */
1058 CLOG_ERROR(&LOG, "Error: Curve contains no baked keyframes");
1059 return;
1060 }
1061
1062 /* Free any existing sample/keyframe data on the curve. */
1063 if (fcu->bezt) {
1064 MEM_delete(fcu->bezt);
1065 }
1066
1067 FPoint *fpt = fcu->fpt;
1068 int keyframes_to_insert = end - start;
1069 int sample_points = fcu->totvert;
1070
1071 BezTriple *bezt = fcu->bezt = MEM_new_array_zeroed<BezTriple>(keyframes_to_insert, __func__);
1072 fcu->totvert = keyframes_to_insert;
1073
1074 /* Get first sample point to 'copy' as keyframe. */
1075 for (; sample_points && (fpt->vec[0] < start); fpt++, sample_points--) {
1076 /* pass */
1077 }
1078
1079 /* Current position in the timeline. */
1080 int cur_pos = start;
1081
1082 /* Add leading dummy flat points if needed. */
1083 for (; keyframes_to_insert && (fpt->vec[0] > start); cur_pos++, bezt++, keyframes_to_insert--) {
1085 bezt->vec[1][0] = float(cur_pos);
1086 bezt->vec[1][1] = fpt->vec[1];
1087 }
1088
1089 /* Copy actual sample points. */
1090 for (; keyframes_to_insert && sample_points;
1091 cur_pos++, bezt++, keyframes_to_insert--, fpt++, sample_points--)
1092 {
1094 copy_v2_v2(bezt->vec[1], fpt->vec);
1095 }
1096
1097 /* Add trailing dummy flat points if needed. */
1098 for (fpt--; keyframes_to_insert; cur_pos++, bezt++, keyframes_to_insert--) {
1100 bezt->vec[1][0] = float(cur_pos);
1101 bezt->vec[1][1] = fpt->vec[1];
1102 }
1103
1104 MEM_SAFE_DELETE(fcu->fpt);
1105
1106 /* Not strictly needed since we use linear interpolation, but better be consistent here. */
1108}
1109
1110/* ***************************** F-Curve Sanity ********************************* */
1111/* The functions here are used in various parts of Blender, usually after some editing
1112 * of keyframe data has occurred. They ensure that keyframe data is properly ordered and
1113 * that the handles are correct.
1114 */
1115
1117{
1118 FModifier *fcm = fcu.modifiers.first();
1119
1120 if (!fcm || fcm->type != FMODIFIER_TYPE_CYCLES) {
1121 return FCU_CYCLE_NONE;
1122 }
1123
1125 return FCU_CYCLE_NONE;
1126 }
1127
1129 return FCU_CYCLE_NONE;
1130 }
1131
1132 FMod_Cycles *data = static_cast<FMod_Cycles *>(fcm->data);
1133
1134 if (data && data->after_cycles == 0 && data->before_cycles == 0) {
1135 if (data->before_mode == FCM_EXTRAPOLATE_CYCLIC && data->after_mode == FCM_EXTRAPOLATE_CYCLIC)
1136 {
1137 return FCU_CYCLE_PERFECT;
1138 }
1139
1142 {
1143 return FCU_CYCLE_OFFSET;
1144 }
1145 }
1146
1147 return FCU_CYCLE_NONE;
1148}
1149
1151{
1153}
1154
1155/* Shifts 'in' by the difference in coordinates between 'to' and 'from',
1156 * using 'out' as the output buffer.
1157 * When 'to' and 'from' are end points of the loop, this moves the 'in' point one loop cycle.
1158 */
1160 bool cycle, BezTriple *out, const BezTriple *in, const BezTriple *from, const BezTriple *to)
1161{
1162 if (!cycle) {
1163 return nullptr;
1164 }
1165
1166 memcpy(out, in, sizeof(BezTriple));
1167
1168 float delta[3];
1169 sub_v3_v3v3(delta, to->vec[1], from->vec[1]);
1170
1171 for (int i = 0; i < 3; i++) {
1172 add_v3_v3(out->vec[i], delta);
1173 }
1174
1175 return out;
1176}
1177
1178void BKE_fcurve_handles_recalc_ex(FCurve &fcu, const eBezTriple_Flag handle_sel_flag)
1179{
1180 /* Error checking:
1181 * - Need at least two points.
1182 * - Need bezier keys.
1183 * - Only bezier-interpolation has handles (for now).
1184 */
1185 if (fcu.bezt == nullptr ||
1186 (fcu.totvert < 2) /* || ELEM(fcu->ipo, BEZT_IPO_CONST, BEZT_IPO_LIN) */)
1187 {
1188 return;
1189 }
1190
1191 /* If the first modifier is Cycles, smooth the curve through the cycle. */
1192 BezTriple *first = &fcu.bezt[0];
1193 BezTriple *last = &fcu.bezt[fcu.totvert - 1];
1194 const bool cycle = BKE_fcurve_is_cyclic(fcu) && BEZT_IS_AUTOH(first) && BEZT_IS_AUTOH(last);
1195
1196 threading::parallel_for(IndexRange(fcu.totvert), 256, [&](const IndexRange range) {
1197 BezTriple tmp;
1198 for (const int i : range) {
1199 BezTriple *bezt = &fcu.bezt[i];
1200 BezTriple *prev = nullptr;
1201 BezTriple *next = nullptr;
1202 if (i > 0) {
1203 prev = (bezt - 1);
1204 }
1205 else {
1206 prev = cycle_offset_triple(cycle, &tmp, &fcu.bezt[fcu.totvert - 2], last, first);
1207 }
1208 if (i < fcu.totvert - 1) {
1209 next = (bezt + 1);
1210 }
1211 else {
1212 next = cycle_offset_triple(cycle, &tmp, &fcu.bezt[1], first, last);
1213 }
1214
1215 /* Clamp timing of handles to be on either side of beztriple. The threshold with
1216 * increment/decrement ulp ensures that the handle length doesn't reach 0 at which point
1217 * there would be no way to ensure that handles stay aligned. This adds an issue where if a
1218 * handle is scaled to 0, the other side is set to be horizontal.
1219 * See #141029 for more info. */
1220 const float threshold = 0.001;
1221 CLAMP_MAX(bezt->vec[0][0], decrement_ulp(bezt->vec[1][0] - threshold));
1222 CLAMP_MIN(bezt->vec[2][0], increment_ulp(bezt->vec[1][0] + threshold));
1223
1224 /* Calculate auto-handles. */
1225 BKE_nurb_handle_calc_ex(bezt, prev, next, handle_sel_flag, true, fcu.auto_smoothing);
1226
1227 /* For automatic ease in and out. */
1228 if (BEZT_IS_AUTOH(bezt) && !cycle) {
1229 /* Only do this on first or last beztriple. */
1230 if (ELEM(i, 0, fcu.totvert - 1)) {
1231 /* Set both handles to have same horizontal value as keyframe. */
1232 if (fcu.extend == FCURVE_EXTRAPOLATE_CONSTANT) {
1233 bezt->vec[0][1] = bezt->vec[2][1] = bezt->vec[1][1];
1234 /* Remember that these keyframes are special, they don't need to be adjusted. */
1235 bezt->auto_handle_type = HD_AUTOTYPE_LOCKED_FINAL;
1236 }
1237 }
1238 }
1239
1240 /* Avoid total smoothing failure on duplicate keyframes (can happen during grab). */
1241 if (prev && prev->vec[1][0] >= bezt->vec[1][0]) {
1242 prev->auto_handle_type = bezt->auto_handle_type = HD_AUTOTYPE_LOCKED_FINAL;
1243 }
1244 }
1245 });
1246
1247 /* If cyclic extrapolation and Auto Clamp has triggered, ensure it is symmetric. */
1248 if (cycle && (first->auto_handle_type != HD_AUTOTYPE_NORMAL ||
1249 last->auto_handle_type != HD_AUTOTYPE_NORMAL))
1250 {
1251 first->vec[0][1] = first->vec[2][1] = first->vec[1][1];
1252 last->vec[0][1] = last->vec[2][1] = last->vec[1][1];
1253 first->auto_handle_type = last->auto_handle_type = HD_AUTOTYPE_LOCKED_FINAL;
1254 }
1255
1256 /* Do a second pass for auto handle: compute the handle to have 0 acceleration step. */
1257 if (fcu.auto_smoothing != FCURVE_SMOOTH_NONE) {
1258 BKE_nurb_handle_smooth_fcurve(fcu.bezt, fcu.totvert, cycle);
1259 }
1260}
1261
1263{
1264 eBezTriple_Handle source;
1265 eBezTriple_Handle *target;
1266 switch (source_side) {
1267 case HandleSide::LEFT: {
1268 source = eBezTriple_Handle(key.h1);
1269 target = &key.h2;
1270 break;
1271 }
1272 case HandleSide::RIGHT: {
1273 source = eBezTriple_Handle(key.h2);
1274 target = &key.h1;
1275 break;
1276 }
1277 }
1278
1279 /*
1280 * Allowed combinations:
1281 * - both handles are the same of any type.
1282 * - Vector <> Align, Blender handles that correctly during transforms.
1283 * - Vector <> Free, simply no conflicts.
1284 *
1285 * Explicitly not allowed:
1286 * - Align <> Free, is not really meaningful, just use Free or Aligned on both.
1287 */
1288
1289 switch (source) {
1290 case HD_AUTO:
1291 case HD_AUTO_ANIM:
1293 *target = source;
1294 break;
1295
1296 case HD_ALIGN:
1297 if (!ELEM(*target, HD_VECT)) {
1298 *target = source;
1299 }
1300 break;
1301
1302 case HD_VECT:
1303 if (!ELEM(*target, HD_VECT, HD_ALIGN)) {
1304 *target = HD_FREE;
1305 }
1306 break;
1307
1308 case HD_FREE:
1309 if (!ELEM(*target, HD_VECT)) {
1310 *target = HD_FREE;
1311 }
1312 break;
1313 }
1314}
1315
1320
1321void testhandles_fcurve(FCurve *fcu, eBezTriple_Flag sel_flag, const bool use_handle)
1322{
1323 /* Only beztriples have handles (bpoints don't though). */
1324 if (ELEM(nullptr, fcu, fcu->bezt)) {
1325 return;
1326 }
1327
1328 /* Loop over beztriples. */
1329 BezTriple *bezt;
1330 uint a;
1331 for (a = 0, bezt = fcu->bezt; a < fcu->totvert; a++, bezt++) {
1333 bezt, sel_flag, use_handle ? NURB_HANDLE_TEST_EACH : NURB_HANDLE_TEST_KNOT_ONLY, false);
1334 }
1335
1336 /* Recalculate handles. */
1337 BKE_fcurve_handles_recalc_ex(*fcu, sel_flag);
1338}
1339
1341{
1342 if (fcu.bezt == nullptr) {
1343 return;
1344 }
1345
1346 /* Keep adjusting order of beztriples until nothing moves (bubble-sort). */
1347 BezTriple *bezt;
1348 uint a;
1349
1350 bool ok = true;
1351 while (ok) {
1352 ok = false;
1353 /* Currently, will only be needed when there are beztriples. */
1354
1355 /* Loop over ALL points to adjust position in array and recalculate handles. */
1356 for (a = 0, bezt = fcu.bezt; a < fcu.totvert; a++, bezt++) {
1357 /* Check if there's a next beztriple which we could try to swap with current. */
1358 if (a < (fcu.totvert - 1)) {
1359 /* Swap if one is after the other (and indicate that order has changed). */
1360 if (bezt->vec[1][0] > (bezt + 1)->vec[1][0]) {
1361 std::swap(*bezt, *(bezt + 1));
1362 if (a == fcu.active_keyframe_index) {
1364 }
1365 else if (a + 1 == fcu.active_keyframe_index) {
1367 }
1368 ok = true;
1369 }
1370 }
1371 }
1372 }
1373
1374 for (a = 0, bezt = fcu.bezt; a < fcu.totvert; a++, bezt++) {
1375 /* If either one of both of the points exceeds crosses over the keyframe time... */
1376 if ((bezt->vec[0][0] > bezt->vec[1][0]) && (bezt->vec[2][0] < bezt->vec[1][0])) {
1377 /* Swap handles if they have switched sides for some reason. */
1378 swap_v2_v2(bezt->vec[0], bezt->vec[2]);
1379 }
1380 else {
1381 /* Clamp handles. */
1382 CLAMP_MAX(bezt->vec[0][0], bezt->vec[1][0]);
1383 CLAMP_MIN(bezt->vec[2][0], bezt->vec[1][0]);
1384 }
1385 }
1386}
1387
1389{
1390 uint a;
1391
1392 /* Currently, only need to test beztriples. */
1393 if (fcu.bezt) {
1394 BezTriple *bezt;
1395
1396 /* Loop through all BezTriples, stopping when one exceeds the one after it. */
1397 for (a = 0, bezt = fcu.bezt; a < (fcu.totvert - 1); a++, bezt++) {
1398 if (bezt->vec[1][0] > (bezt + 1)->vec[1][0]) {
1399 return true;
1400 }
1401 }
1402 }
1403 else if (fcu.fpt) {
1404 FPoint *fpt;
1405
1406 /* Loop through all FPoints, stopping when one exceeds the one after it. */
1407 for (a = 0, fpt = fcu.fpt; a < (fcu.totvert - 1); a++, fpt++) {
1408 if (fpt->vec[0] > (fpt + 1)->vec[0]) {
1409 return true;
1410 }
1411 }
1412 }
1413
1414 /* None need any swapping. */
1415 return false;
1416}
1417
1419
1420/* -------------------------------------------------------------------- */
1423
1424void BKE_fcurve_correct_bezpart(const float v1[2], float v2[2], float v3[2], const float v4[2])
1425{
1426 float h1[2], h2[2], len1, len2, len, fac;
1427
1428 /* Calculate handle deltas. */
1429 h1[0] = v1[0] - v2[0];
1430 h1[1] = v1[1] - v2[1];
1431
1432 h2[0] = v4[0] - v3[0];
1433 h2[1] = v4[1] - v3[1];
1434
1435 /* Calculate distances:
1436 * - len = Span of time between keyframes.
1437 * - len1 = Length of handle of start key.
1438 * - len2 = Length of handle of end key.
1439 */
1440 len = v4[0] - v1[0];
1441 len1 = fabsf(h1[0]);
1442 len2 = fabsf(h2[0]);
1443
1444 /* If the handles have no length, no need to do any corrections. */
1445 if ((len1 + len2) == 0.0f) {
1446 return;
1447 }
1448
1449 /* To prevent looping or rewinding, handles cannot
1450 * exceed the adjacent key-frames time position. */
1451 if (len1 > len) {
1452 fac = len / len1;
1453 v2[0] = (v1[0] - fac * h1[0]);
1454 v2[1] = (v1[1] - fac * h1[1]);
1455 }
1456
1457 if (len2 > len) {
1458 fac = len / len2;
1459 v3[0] = (v4[0] - fac * h2[0]);
1460 v3[1] = (v4[1] - fac * h2[1]);
1461 }
1462}
1463
1470static int solve_cubic(double c0, double c1, double c2, double c3, float *o)
1471{
1472 double a, b, c, p, q, d, t, phi;
1473 int nr = 0;
1474
1475 if (c3 != 0.0) {
1476 a = c2 / c3;
1477 b = c1 / c3;
1478 c = c0 / c3;
1479 a = a / 3;
1480
1481 p = b / 3 - a * a;
1482 q = (2 * a * a * a - a * b + c) / 2;
1483 d = q * q + p * p * p;
1484
1485 if (d > 0.0) {
1486 t = sqrt(d);
1487 o[0] = float(sqrt3d(-q + t) + sqrt3d(-q - t) - a);
1488
1489 if ((o[0] >= float(SMALL)) && (o[0] <= 1.000001f)) {
1490 return 1;
1491 }
1492 return 0;
1493 }
1494
1495 if (d == 0.0) {
1496 t = sqrt3d(-q);
1497 o[0] = float(2 * t - a);
1498
1499 if ((o[0] >= float(SMALL)) && (o[0] <= 1.000001f)) {
1500 nr++;
1501 }
1502 o[nr] = float(-t - a);
1503
1504 if ((o[nr] >= float(SMALL)) && (o[nr] <= 1.000001f)) {
1505 return nr + 1;
1506 }
1507 return nr;
1508 }
1509
1510 phi = acos(-q / sqrt(-(p * p * p)));
1511 t = sqrt(-p);
1512 p = cos(phi / 3);
1513 q = sqrt(3 - 3 * p * p);
1514 o[0] = float(2 * t * p - a);
1515
1516 if ((o[0] >= float(SMALL)) && (o[0] <= 1.000001f)) {
1517 nr++;
1518 }
1519 o[nr] = float(-t * (p + q) - a);
1520
1521 if ((o[nr] >= float(SMALL)) && (o[nr] <= 1.000001f)) {
1522 nr++;
1523 }
1524 o[nr] = float(-t * (p - q) - a);
1525
1526 if ((o[nr] >= float(SMALL)) && (o[nr] <= 1.000001f)) {
1527 return nr + 1;
1528 }
1529 return nr;
1530 }
1531 a = c2;
1532 b = c1;
1533 c = c0;
1534
1535 if (a != 0.0) {
1536 /* Discriminant */
1537 p = b * b - 4 * a * c;
1538
1539 if (p > 0) {
1540 p = sqrt(p);
1541 o[0] = float((-b - p) / (2 * a));
1542
1543 if ((o[0] >= float(SMALL)) && (o[0] <= 1.000001f)) {
1544 nr++;
1545 }
1546 o[nr] = float((-b + p) / (2 * a));
1547
1548 if ((o[nr] >= float(SMALL)) && (o[nr] <= 1.000001f)) {
1549 return nr + 1;
1550 }
1551 return nr;
1552 }
1553
1554 if (p == 0) {
1555 o[0] = float(-b / (2 * a));
1556 if ((o[0] >= float(SMALL)) && (o[0] <= 1.000001f)) {
1557 return 1;
1558 }
1559 }
1560
1561 return 0;
1562 }
1563
1564 if (b != 0.0) {
1565 o[0] = float(-c / b);
1566
1567 if ((o[0] >= float(SMALL)) && (o[0] <= 1.000001f)) {
1568 return 1;
1569 }
1570 return 0;
1571 }
1572
1573 if (c == 0.0) {
1574 o[0] = 0.0;
1575 return 1;
1576 }
1577
1578 return 0;
1579}
1580
1581/* Find root(s) ('zero') of a Bezier curve. */
1582static int findzero(float x, float q0, float q1, float q2, float q3, float *o)
1583{
1584 const double c0 = q0 - x;
1585 const double c1 = 3.0f * (q1 - q0);
1586 const double c2 = 3.0f * (q0 - 2.0f * q1 + q2);
1587 const double c3 = q3 - q0 + 3.0f * (q1 - q2);
1588
1589 return solve_cubic(c0, c1, c2, c3, o);
1590}
1591
1592static void berekeny(float f1, float f2, float f3, float f4, float *o, int b)
1593{
1594 float t, c0, c1, c2, c3;
1595 int a;
1596
1597 c0 = f1;
1598 c1 = 3.0f * (f2 - f1);
1599 c2 = 3.0f * (f1 - 2.0f * f2 + f3);
1600 c3 = f4 - f1 + 3.0f * (f2 - f3);
1601
1602 for (a = 0; a < b; a++) {
1603 t = o[a];
1604 o[a] = c0 + t * c1 + t * t * c2 + t * t * t * c3;
1605 }
1606}
1607
1608static void fcurve_bezt_free(FCurve &fcu)
1609{
1610 MEM_SAFE_DELETE(fcu.bezt);
1611 fcu.totvert = 0;
1612}
1613
1615 BezTriple *prev,
1616 BezTriple *next,
1617 float *r_pdelta)
1618{
1619 /* The four points that make up this section of the Bezier curve. */
1620 const float *prev_coords = prev->vec[1];
1621 float *prev_handle_right = prev->vec[2];
1622 float *next_handle_left = next->vec[0];
1623 const float *next_coords = next->vec[1];
1624
1625 float *new_handle_left = bezt->vec[0];
1626 const float *new_coords = bezt->vec[1];
1627 float *new_handle_right = bezt->vec[2];
1628
1629 if (new_coords[0] <= prev_coords[0] || new_coords[0] >= next_coords[0]) {
1630 /* The new keyframe is outside the (prev_coords, next_coords) range. */
1631 return false;
1632 }
1633
1634 /* Apply evaluation-time limits and compute the effective curve. */
1635 BKE_fcurve_correct_bezpart(prev_coords, prev_handle_right, next_handle_left, next_coords);
1636 float roots[4];
1637 if (!findzero(new_coords[0],
1638 prev_coords[0],
1639 prev_handle_right[0],
1640 next_handle_left[0],
1641 next_coords[0],
1642 roots))
1643 {
1644 return false;
1645 }
1646
1647 const float t = roots[0]; /* Percentage of the curve at which the split should occur. */
1648 if (t <= 0.0f || t >= 1.0f) {
1649 /* The split would occur outside the curve, which isn't possible. */
1650 return false;
1651 }
1652
1653 /* De Casteljau split, requires three iterations of splitting.
1654 * See https://pomax.github.io/bezierinfo/#decasteljau */
1655 float split1[3][2], split2[2][2], split3[2];
1656 interp_v2_v2v2(split1[0], prev_coords, prev_handle_right, t);
1657 interp_v2_v2v2(split1[1], prev_handle_right, next_handle_left, t);
1658 interp_v2_v2v2(split1[2], next_handle_left, next_coords, t);
1659 interp_v2_v2v2(split2[0], split1[0], split1[1], t);
1660 interp_v2_v2v2(split2[1], split1[1], split1[2], t);
1661 interp_v2_v2v2(split3, split2[0], split2[1], t);
1662
1663 /* Update the existing handles. */
1664 copy_v2_v2(prev_handle_right, split1[0]);
1665 copy_v2_v2(next_handle_left, split1[2]);
1666
1667 float diff_coords[2];
1668 sub_v2_v2v2(diff_coords, new_coords, split3);
1669 add_v2_v2v2(new_handle_left, split2[0], diff_coords);
1670 add_v2_v2v2(new_handle_right, split2[1], diff_coords);
1671
1672 *r_pdelta = diff_coords[1];
1673 return true;
1674}
1675
1676void BKE_fcurve_bezt_resize(FCurve &fcu, const int new_totvert)
1677{
1678 BLI_assert(new_totvert >= 0);
1679
1680 /* No early return when new_totvert == fcu->totvert. There is no way to know the intention of the
1681 * caller, nor the history of the FCurve so far, so `fcu->bezt` may actually have allocated space
1682 * for more than `fcu->totvert` keys. */
1683
1684 if (new_totvert == 0) {
1685 fcurve_bezt_free(fcu);
1686 return;
1687 }
1688
1689 fcu.bezt = static_cast<BezTriple *>(
1690 MEM_realloc_uninitialized(fcu.bezt, new_totvert * sizeof(*(fcu.bezt))));
1691
1692 /* Zero out all the newly-allocated beztriples. This is necessary, as it is likely that only some
1693 * of the fields will actually be updated by the caller. */
1694 const int old_totvert = fcu.totvert;
1695 if (new_totvert > old_totvert) {
1696 std::fill_n(fcu.bezt + old_totvert, new_totvert - old_totvert, BezTriple{});
1697 }
1698
1699 fcu.totvert = new_totvert;
1700}
1701
1703{
1704 /* sanity check */
1705 if (fcu == nullptr) {
1706 return;
1707 }
1708
1709 /* verify the index:
1710 * 1) cannot be greater than the number of available keyframes
1711 * 2) negative indices are for specifying a value from the end of the array
1712 */
1713 if (abs(index) >= fcu->totvert) {
1714 return;
1715 }
1716 if (index < 0) {
1717 index += fcu->totvert;
1718 }
1719
1720 /* Delete this keyframe */
1721 memmove(
1722 &fcu->bezt[index], &fcu->bezt[index + 1], sizeof(BezTriple) * (fcu->totvert - index - 1));
1723 fcu->totvert--;
1724
1725 /* Free the array of BezTriples if there are not keyframes */
1726 if (fcu->totvert == 0) {
1727 fcurve_bezt_free(*fcu);
1728 }
1729}
1730
1731void BKE_fcurve_delete_keys(FCurve &fcu, uint2 index_range)
1732{
1733 BLI_assert(fcu.bezt != nullptr);
1734 BLI_assert(index_range[1] > index_range[0]);
1735 BLI_assert(index_range[1] <= fcu.totvert);
1736
1737 const int removed_index_count = index_range[1] - index_range[0];
1738 memmove(&fcu.bezt[index_range[0]],
1739 &fcu.bezt[index_range[1]],
1740 sizeof(BezTriple) * (fcu.totvert - index_range[1]));
1741 fcu.totvert -= removed_index_count;
1742
1743 if (fcu.totvert == 0) {
1744 fcurve_bezt_free(fcu);
1745 }
1746}
1747
1749 const BezTriple *a, const int size_a, const BezTriple *b, const int size_b, int *r_merged_size)
1750{
1751 BezTriple *large_array = MEM_new_array_zeroed<BezTriple>(size_t(size_a) + size_b, "beztriple");
1752
1753 int iterator_a = 0;
1754 int iterator_b = 0;
1755 *r_merged_size = 0;
1756
1757 /* For comparing if keyframes are at the same x-value. */
1758 const int max_ulps = 32;
1759
1760 while (iterator_a < size_a || iterator_b < size_b) {
1761 if (iterator_a >= size_a) {
1762 const int remaining_keys = size_b - iterator_b;
1763 memcpy(&large_array[*r_merged_size], &b[iterator_b], sizeof(BezTriple) * remaining_keys);
1764 (*r_merged_size) += remaining_keys;
1765 break;
1766 }
1767 if (iterator_b >= size_b) {
1768 const int remaining_keys = size_a - iterator_a;
1769 memcpy(&large_array[*r_merged_size], &a[iterator_a], sizeof(BezTriple) * remaining_keys);
1770 (*r_merged_size) += remaining_keys;
1771 break;
1772 }
1773
1775 a[iterator_a].vec[1][0], b[iterator_b].vec[1][0], BEZT_BINARYSEARCH_THRESH, max_ulps))
1776 {
1777 memcpy(&large_array[*r_merged_size], &a[iterator_a], sizeof(BezTriple));
1778 iterator_a++;
1779 iterator_b++;
1780 }
1781 else if (a[iterator_a].vec[1][0] < b[iterator_b].vec[1][0]) {
1782 memcpy(&large_array[*r_merged_size], &a[iterator_a], sizeof(BezTriple));
1783 iterator_a++;
1784 }
1785 else {
1786 memcpy(&large_array[*r_merged_size], &b[iterator_b], sizeof(BezTriple));
1787 iterator_b++;
1788 }
1789 (*r_merged_size)++;
1790 }
1791
1792 BezTriple *minimal_array;
1793 if (*r_merged_size < size_a + size_b) {
1794 minimal_array = static_cast<BezTriple *>(
1795 MEM_realloc_uninitialized(large_array, sizeof(BezTriple) * (*r_merged_size)));
1796 }
1797 else {
1798 minimal_array = large_array;
1799 }
1800
1801 return minimal_array;
1802}
1803
1805{
1806 if (fcu.bezt == nullptr) { /* ignore baked curves */
1807 return false;
1808 }
1809
1810 bool changed = false;
1811
1812 /* Delete selected BezTriples */
1813 for (int i = 0; i < fcu.totvert; i++) {
1814 if (fcu.bezt[i].f2 & SELECT) {
1815 if (i == fcu.active_keyframe_index) {
1816 BKE_fcurve_active_keyframe_set(&fcu, nullptr);
1817 }
1818 memmove(&fcu.bezt[i], &fcu.bezt[i + 1], sizeof(BezTriple) * (fcu.totvert - i - 1));
1819 fcu.totvert--;
1820 i--;
1821 changed = true;
1822 }
1823 }
1824
1825 /* Free the array of BezTriples if there are not keyframes */
1826 if (fcu.totvert == 0) {
1827 fcurve_bezt_free(fcu);
1828 }
1829
1830 return changed;
1831}
1832
1834{
1835 fcurve_bezt_free(fcu);
1836}
1837
1838/* Time + Average value */
1841 float frame; /* frame to cluster around */
1842 float val; /* average value */
1843
1844 size_t tot_count; /* number of keyframes that have been averaged */
1845 size_t del_count; /* number of keyframes of this sort that have been deleted so far */
1846};
1847
1848void BKE_fcurve_merge_duplicate_keys(FCurve *fcu, const int sel_flag, const bool use_handle)
1849{
1850 /* NOTE: We assume that all keys are sorted */
1851 ListBaseT<tRetainedKeyframe> retained_keys = {nullptr, nullptr};
1852 const bool can_average_points = ((fcu->flag & (FCURVE_INT_VALUES | FCURVE_DISCRETE_VALUES)) ==
1853 0);
1854
1855 /* sanity checks */
1856 if ((fcu->totvert == 0) || (fcu->bezt == nullptr)) {
1857 return;
1858 }
1859
1860 /* 1) Identify selected keyframes, and average the values on those
1861 * in case there are collisions due to multiple keys getting scaled
1862 * to all end up on the same frame
1863 */
1864 for (int i = 0; i < fcu->totvert; i++) {
1865 BezTriple *bezt = &fcu->bezt[i];
1866
1867 if (BEZT_ISSEL_ANY(bezt)) {
1868 bool found = false;
1869
1870 /* If there's another selected frame here, merge it */
1871 for (tRetainedKeyframe &rk : retained_keys.items_reversed()) {
1872 if (IS_EQT(rk.frame, bezt->vec[1][0], BEZT_BINARYSEARCH_THRESH)) {
1873 rk.val += bezt->vec[1][1];
1874 rk.tot_count++;
1875
1876 found = true;
1877 break;
1878 }
1879 if (rk.frame < bezt->vec[1][0]) {
1880 /* Terminate early if have passed the supposed insertion point? */
1881 break;
1882 }
1883 }
1884
1885 /* If nothing found yet, create a new one */
1886 if (found == false) {
1887 tRetainedKeyframe *rk = MEM_new_zeroed<tRetainedKeyframe>("tRetainedKeyframe");
1888
1889 rk->frame = bezt->vec[1][0];
1890 rk->val = bezt->vec[1][1];
1891 rk->tot_count = 1;
1892
1893 BLI_addtail(&retained_keys, rk);
1894 }
1895 }
1896 }
1897
1898 if (retained_keys.is_empty()) {
1899 /* This may happen if none of the points were selected... */
1900 if (G.debug & G_DEBUG) {
1901 printf("%s: nothing to do for FCurve %p (rna_path = '%s')\n",
1902 __func__,
1903 fcu,
1904 fcu->rna_path().c_str());
1905 }
1906 return;
1907 }
1908
1909 /* Compute the average values for each retained keyframe */
1910 for (tRetainedKeyframe &rk : retained_keys) {
1911 rk.val = rk.val / float(rk.tot_count);
1912 }
1913
1914 /* 2) Delete all keyframes duplicating the "retained keys" found above
1915 * - Most of these will be unselected keyframes
1916 * - Some will be selected keyframes though. For those, we only keep the last one
1917 * (or else everything is gone), and replace its value with the averaged value.
1918 */
1919 for (int i = fcu->totvert - 1; i >= 0; i--) {
1920 BezTriple *bezt = &fcu->bezt[i];
1921
1922 /* Is this keyframe a candidate for deletion? */
1923 /* TODO: Replace loop with an O(1) lookup instead */
1924 for (tRetainedKeyframe &rk : retained_keys.items_reversed()) {
1925 if (IS_EQT(bezt->vec[1][0], rk.frame, BEZT_BINARYSEARCH_THRESH)) {
1926 /* Selected keys are treated with greater care than unselected ones... */
1927 if (BEZT_ISSEL_ANY(bezt)) {
1928 /* - If this is the last selected key left (based on rk->del_count) ==> UPDATE IT
1929 * (or else we wouldn't have any keyframe left here)
1930 * - Otherwise, there are still other selected keyframes on this frame
1931 * to be merged down still ==> DELETE IT
1932 */
1933 if (rk.del_count == rk.tot_count - 1) {
1934 /* Update keyframe... */
1935 if (can_average_points) {
1936 /* TODO: update handles too? */
1937 bezt->vec[1][1] = rk.val;
1938 }
1939 }
1940 else {
1941 /* Delete Keyframe */
1942 BKE_fcurve_delete_key(fcu, i);
1943 }
1944
1945 /* Update count of how many we've deleted
1946 * - It should only matter that we're doing this for all but the last one
1947 */
1948 rk.del_count++;
1949 }
1950 else {
1951 /* Always delete - Unselected keys don't matter */
1952 BKE_fcurve_delete_key(fcu, i);
1953 }
1954
1955 /* Stop the RK search... we've found our match now */
1956 break;
1957 }
1958 }
1959 }
1960
1961 /* 3) Recalculate handles */
1962 testhandles_fcurve(fcu, eBezTriple_Flag(sel_flag), use_handle);
1963
1964 /* cleanup */
1965 retained_keys.free_no_destruct();
1966}
1967
1969{
1970 if (fcu->totvert < 2) {
1971 return;
1972 }
1973
1974 BLI_assert_msg(fcu->bezt, "this function only works with regular (non-sampled) FCurves");
1975 if (fcu->bezt == nullptr) {
1976 return;
1977 }
1978
1979 int prev_bezt_index = 0;
1980 for (int i = 1; i < fcu->totvert; i++) {
1981 BezTriple *bezt = &fcu->bezt[i];
1982 BezTriple *prev_bezt = &fcu->bezt[prev_bezt_index];
1983
1984 const float bezt_x = bezt->vec[1][0];
1985 const float prev_x = prev_bezt->vec[1][0];
1986
1987 if (bezt_x - prev_x <= BEZT_BINARYSEARCH_THRESH) {
1988 /* Replace 'prev_bezt', as it has the same X-coord as 'bezt' and the last one wins. */
1989 *prev_bezt = *bezt;
1990
1991 if (floor(bezt_x) == bezt_x) {
1992 /* Keep the 'bezt_x' coordinate, as being on a frame is more desirable
1993 * than being ever so slightly off. */
1994 }
1995 else {
1996 /* Move the retained key to the old X-coordinate, to 'anchor' the X-coordinate used for
1997 * subsequent comparisons. Without this, the reference X-coordinate would keep moving
1998 * forward in time, potentially merging in more keys than desired. */
2000 }
2001 continue;
2002 }
2003
2004 /* Next iteration should look at the current element. However, because of the deletions, that
2005 * may not be at index 'i'; after this increment, `prev_bezt_index` points at where the current
2006 * element should go. */
2007 prev_bezt_index++;
2008
2009 if (prev_bezt_index != i) {
2010 /* This bezt should be kept, so copy it to its new location in the array. */
2011 fcu->bezt[prev_bezt_index] = *bezt;
2012 }
2013 }
2014
2015 BKE_fcurve_bezt_resize(*fcu, prev_bezt_index + 1);
2016}
2017
2019
2020/* -------------------------------------------------------------------- */
2023
2025 const BezTriple *bezts,
2026 float evaltime,
2027 int endpoint_offset,
2028 int direction_to_neighbor)
2029{
2030 /* The first/last keyframe. */
2031 const BezTriple *endpoint_bezt = bezts + endpoint_offset;
2032 /* The second (to last) keyframe. */
2033 const BezTriple *neighbor_bezt = endpoint_bezt + direction_to_neighbor;
2034
2035 if (endpoint_bezt->ipo == BEZT_IPO_CONST || fcu->extend == FCURVE_EXTRAPOLATE_CONSTANT ||
2036 (fcu->flag & FCURVE_DISCRETE_VALUES) != 0)
2037 {
2038 /* Constant (BEZT_IPO_HORIZ) extrapolation or constant interpolation, so just extend the
2039 * endpoint's value. */
2040 return endpoint_bezt->vec[1][1];
2041 }
2042
2043 if (endpoint_bezt->ipo == BEZT_IPO_LIN) {
2044 /* Use the next center point instead of our own handle for linear interpolated extrapolate. */
2045 if (fcu->totvert == 1) {
2046 return endpoint_bezt->vec[1][1];
2047 }
2048
2049 const float dx = endpoint_bezt->vec[1][0] - evaltime;
2050 float fac = neighbor_bezt->vec[1][0] - endpoint_bezt->vec[1][0];
2051
2052 /* Prevent division by zero. */
2053 if (fac == 0.0f) {
2054 return endpoint_bezt->vec[1][1];
2055 }
2056
2057 fac = (neighbor_bezt->vec[1][1] - endpoint_bezt->vec[1][1]) / fac;
2058 return endpoint_bezt->vec[1][1] - (fac * dx);
2059 }
2060
2061 /* Use the gradient of the second handle (later) of neighbor to calculate the gradient and thus
2062 * the value of the curve at evaluation time. */
2063 const int handle = direction_to_neighbor > 0 ? 0 : 2;
2064 const float dx = endpoint_bezt->vec[1][0] - evaltime;
2065 float fac = endpoint_bezt->vec[1][0] - endpoint_bezt->vec[handle][0];
2066
2067 /* Prevent division by zero. */
2068 if (fac == 0.0f) {
2069 return endpoint_bezt->vec[1][1];
2070 }
2071
2072 fac = (endpoint_bezt->vec[1][1] - endpoint_bezt->vec[handle][1]) / fac;
2073 return endpoint_bezt->vec[1][1] - (fac * dx);
2074}
2075
2077 const BezTriple *bezts,
2078 float evaltime)
2079{
2080 const float eps = 1.e-8f;
2081 uint a;
2082
2083 /* Evaluation-time occurs somewhere in the middle of the curve. */
2084 bool exact = false;
2085
2086 /* Use binary search to find appropriate keyframes...
2087 *
2088 * The threshold here has the following constraints:
2089 * - 0.001 is too coarse:
2090 * We get artifacts with 2cm driver movements at 1BU = 1m (see #40332).
2091 *
2092 * - 0.00001 is too fine:
2093 * Weird errors, like selecting the wrong keyframe range (see #39207), occur.
2094 * This lower bound was established in b888a32eee8147b028464336ad2404d8155c64dd.
2095 */
2096 a = BKE_fcurve_bezt_binarysearch_index_ex(bezts, evaltime, fcu->totvert, 0.0001, &exact);
2097 const BezTriple *bezt = bezts + a;
2098
2099 if (exact) {
2100 /* Index returned must be interpreted differently when it sits on top of an existing keyframe
2101 * - That keyframe is the start of the segment we need (see action_bug_2.blend in #39207).
2102 */
2103 return bezt->vec[1][1];
2104 }
2105
2106 /* Index returned refers to the keyframe that the eval-time occurs *before*
2107 * - hence, that keyframe marks the start of the segment we're dealing with.
2108 */
2109 const BezTriple *prevbezt = (a > 0) ? (bezt - 1) : bezt;
2110
2111 /* Use if the key is directly on the frame, in rare cases this is needed else we get 0.0 instead.
2112 * XXX: consult #39207 for examples of files where failure of these checks can cause issues. */
2113 if (fabsf(bezt->vec[1][0] - evaltime) < eps) {
2114 return bezt->vec[1][1];
2115 }
2116
2117 if (evaltime < prevbezt->vec[1][0] || bezt->vec[1][0] < evaltime) {
2118 if (G.debug & G_DEBUG) {
2119 printf(" ERROR: failed eval - p=%f b=%f, t=%f (%f)\n",
2120 prevbezt->vec[1][0],
2121 bezt->vec[1][0],
2122 evaltime,
2123 fabsf(bezt->vec[1][0] - evaltime));
2124 }
2125 return 0.0f;
2126 }
2127
2128 /* Evaluation-time occurs within the interval defined by these two keyframes. */
2129 const float begin = prevbezt->vec[1][1];
2130 const float change = bezt->vec[1][1] - prevbezt->vec[1][1];
2131 const float duration = bezt->vec[1][0] - prevbezt->vec[1][0];
2132 const float time = evaltime - prevbezt->vec[1][0];
2133 const float amplitude = prevbezt->amplitude;
2134 const float period = prevbezt->period;
2135
2136 /* Value depends on interpolation mode. */
2137 if ((prevbezt->ipo == BEZT_IPO_CONST) || (fcu->flag & FCURVE_DISCRETE_VALUES) || (duration == 0))
2138 {
2139 /* Constant (evaltime not relevant, so no interpolation needed). */
2140 return prevbezt->vec[1][1];
2141 }
2142
2143 switch (prevbezt->ipo) {
2144 /* Interpolation ...................................... */
2145 case BEZT_IPO_BEZ: {
2146 float v1[2], v2[2], v3[2], v4[2], opl[32];
2147
2148 /* Bezier interpolation. */
2149 /* (v1, v2) are the first keyframe and its 2nd handle. */
2150 v1[0] = prevbezt->vec[1][0];
2151 v1[1] = prevbezt->vec[1][1];
2152 v2[0] = prevbezt->vec[2][0];
2153 v2[1] = prevbezt->vec[2][1];
2154 /* (v3, v4) are the last keyframe's 1st handle + the last keyframe. */
2155 v3[0] = bezt->vec[0][0];
2156 v3[1] = bezt->vec[0][1];
2157 v4[0] = bezt->vec[1][0];
2158 v4[1] = bezt->vec[1][1];
2159
2160 if (fabsf(v1[1] - v4[1]) < FLT_EPSILON && fabsf(v2[1] - v3[1]) < FLT_EPSILON &&
2161 fabsf(v3[1] - v4[1]) < FLT_EPSILON)
2162 {
2163 /* Optimization: If all the handles are flat/at the same values,
2164 * the value is simply the shared value (see #40372 -> F91346).
2165 */
2166 return v1[1];
2167 }
2168 /* Adjust handles so that they don't overlap (forming a loop). */
2169 BKE_fcurve_correct_bezpart(v1, v2, v3, v4);
2170
2171 /* Try to get a value for this position - if failure, try another set of points. */
2172 if (!findzero(evaltime, v1[0], v2[0], v3[0], v4[0], opl)) {
2173 if (G.debug & G_DEBUG) {
2174 printf(" ERROR: findzero() failed at %f with %f %f %f %f\n",
2175 evaltime,
2176 v1[0],
2177 v2[0],
2178 v3[0],
2179 v4[0]);
2180 }
2181 return 0.0;
2182 }
2183
2184 berekeny(v1[1], v2[1], v3[1], v4[1], opl, 1);
2185 return opl[0];
2186 }
2187 case BEZT_IPO_LIN:
2188 /* Linear - simply linearly interpolate between values of the two keyframes. */
2189 return BLI_easing_linear_ease(time, begin, change, duration);
2190
2191 /* Easing ............................................ */
2192 case BEZT_IPO_BACK:
2193 switch (prevbezt->easing) {
2194 case BEZT_IPO_EASE_IN:
2195 return BLI_easing_back_ease_in(time, begin, change, duration, prevbezt->back);
2196 case BEZT_IPO_EASE_OUT:
2197 return BLI_easing_back_ease_out(time, begin, change, duration, prevbezt->back);
2199 return BLI_easing_back_ease_in_out(time, begin, change, duration, prevbezt->back);
2200
2201 default: /* Default/Auto: same as ease out. */
2202 return BLI_easing_back_ease_out(time, begin, change, duration, prevbezt->back);
2203 }
2204 break;
2205
2206 case BEZT_IPO_BOUNCE:
2207 switch (prevbezt->easing) {
2208 case BEZT_IPO_EASE_IN:
2209 return BLI_easing_bounce_ease_in(time, begin, change, duration);
2210 case BEZT_IPO_EASE_OUT:
2211 return BLI_easing_bounce_ease_out(time, begin, change, duration);
2213 return BLI_easing_bounce_ease_in_out(time, begin, change, duration);
2214
2215 default: /* Default/Auto: same as ease out. */
2216 return BLI_easing_bounce_ease_out(time, begin, change, duration);
2217 }
2218 break;
2219
2220 case BEZT_IPO_CIRC:
2221 switch (prevbezt->easing) {
2222 case BEZT_IPO_EASE_IN:
2223 return BLI_easing_circ_ease_in(time, begin, change, duration);
2224 case BEZT_IPO_EASE_OUT:
2225 return BLI_easing_circ_ease_out(time, begin, change, duration);
2227 return BLI_easing_circ_ease_in_out(time, begin, change, duration);
2228
2229 default: /* Default/Auto: same as ease in. */
2230 return BLI_easing_circ_ease_in(time, begin, change, duration);
2231 }
2232 break;
2233
2234 case BEZT_IPO_CUBIC:
2235 switch (prevbezt->easing) {
2236 case BEZT_IPO_EASE_IN:
2237 return BLI_easing_cubic_ease_in(time, begin, change, duration);
2238 case BEZT_IPO_EASE_OUT:
2239 return BLI_easing_cubic_ease_out(time, begin, change, duration);
2241 return BLI_easing_cubic_ease_in_out(time, begin, change, duration);
2242
2243 default: /* Default/Auto: same as ease in. */
2244 return BLI_easing_cubic_ease_in(time, begin, change, duration);
2245 }
2246 break;
2247
2248 case BEZT_IPO_ELASTIC:
2249 switch (prevbezt->easing) {
2250 case BEZT_IPO_EASE_IN:
2251 return BLI_easing_elastic_ease_in(time, begin, change, duration, amplitude, period);
2252 case BEZT_IPO_EASE_OUT:
2253 return BLI_easing_elastic_ease_out(time, begin, change, duration, amplitude, period);
2255 return BLI_easing_elastic_ease_in_out(time, begin, change, duration, amplitude, period);
2256
2257 default: /* Default/Auto: same as ease out. */
2258 return BLI_easing_elastic_ease_out(time, begin, change, duration, amplitude, period);
2259 }
2260 break;
2261
2262 case BEZT_IPO_EXPO:
2263 switch (prevbezt->easing) {
2264 case BEZT_IPO_EASE_IN:
2265 return BLI_easing_expo_ease_in(time, begin, change, duration);
2266 case BEZT_IPO_EASE_OUT:
2267 return BLI_easing_expo_ease_out(time, begin, change, duration);
2269 return BLI_easing_expo_ease_in_out(time, begin, change, duration);
2270
2271 default: /* Default/Auto: same as ease in. */
2272 return BLI_easing_expo_ease_in(time, begin, change, duration);
2273 }
2274 break;
2275
2276 case BEZT_IPO_QUAD:
2277 switch (prevbezt->easing) {
2278 case BEZT_IPO_EASE_IN:
2279 return BLI_easing_quad_ease_in(time, begin, change, duration);
2280 case BEZT_IPO_EASE_OUT:
2281 return BLI_easing_quad_ease_out(time, begin, change, duration);
2283 return BLI_easing_quad_ease_in_out(time, begin, change, duration);
2284
2285 default: /* Default/Auto: same as ease in. */
2286 return BLI_easing_quad_ease_in(time, begin, change, duration);
2287 }
2288 break;
2289
2290 case BEZT_IPO_QUART:
2291 switch (prevbezt->easing) {
2292 case BEZT_IPO_EASE_IN:
2293 return BLI_easing_quart_ease_in(time, begin, change, duration);
2294 case BEZT_IPO_EASE_OUT:
2295 return BLI_easing_quart_ease_out(time, begin, change, duration);
2297 return BLI_easing_quart_ease_in_out(time, begin, change, duration);
2298
2299 default: /* Default/Auto: same as ease in. */
2300 return BLI_easing_quart_ease_in(time, begin, change, duration);
2301 }
2302 break;
2303
2304 case BEZT_IPO_QUINT:
2305 switch (prevbezt->easing) {
2306 case BEZT_IPO_EASE_IN:
2307 return BLI_easing_quint_ease_in(time, begin, change, duration);
2308 case BEZT_IPO_EASE_OUT:
2309 return BLI_easing_quint_ease_out(time, begin, change, duration);
2311 return BLI_easing_quint_ease_in_out(time, begin, change, duration);
2312
2313 default: /* Default/Auto: same as ease in. */
2314 return BLI_easing_quint_ease_in(time, begin, change, duration);
2315 }
2316 break;
2317
2318 case BEZT_IPO_SINE:
2319 switch (prevbezt->easing) {
2320 case BEZT_IPO_EASE_IN:
2321 return BLI_easing_sine_ease_in(time, begin, change, duration);
2322 case BEZT_IPO_EASE_OUT:
2323 return BLI_easing_sine_ease_out(time, begin, change, duration);
2325 return BLI_easing_sine_ease_in_out(time, begin, change, duration);
2326
2327 default: /* Default/Auto: same as ease in. */
2328 return BLI_easing_sine_ease_in(time, begin, change, duration);
2329 }
2330 break;
2331
2332 default:
2333 return prevbezt->vec[1][1];
2334 }
2335
2336 return 0.0f;
2337}
2338
2339/* Calculate F-Curve value for 'evaltime' using #BezTriple keyframes. */
2340static float fcurve_eval_keyframes(const FCurve *fcu, const BezTriple *bezts, float evaltime)
2341{
2342 if (evaltime <= bezts->vec[1][0]) {
2343 return fcurve_eval_keyframes_extrapolate(fcu, bezts, evaltime, 0, +1);
2344 }
2345
2346 const BezTriple *lastbezt = bezts + fcu->totvert - 1;
2347 if (lastbezt->vec[1][0] <= evaltime) {
2348 return fcurve_eval_keyframes_extrapolate(fcu, bezts, evaltime, fcu->totvert - 1, -1);
2349 }
2350
2351 return fcurve_eval_keyframes_interpolate(fcu, bezts, evaltime);
2352}
2353
2354/* Calculate F-Curve value for 'evaltime' using #FPoint samples. */
2355static float fcurve_eval_samples(const FCurve *fcu, const FPoint *fpts, float evaltime)
2356{
2357 float cvalue = 0.0f;
2358
2359 /* Get pointers. */
2360 const FPoint *prevfpt = fpts;
2361 const FPoint *lastfpt = prevfpt + fcu->totvert - 1;
2362
2363 /* Evaluation time at or past endpoints? */
2364 if (prevfpt->vec[0] >= evaltime) {
2365 /* Before or on first sample, so just extend value. */
2366 cvalue = prevfpt->vec[1];
2367 }
2368 else if (lastfpt->vec[0] <= evaltime) {
2369 /* After or on last sample, so just extend value. */
2370 cvalue = lastfpt->vec[1];
2371 }
2372 else {
2373 float t = fabsf(evaltime - floorf(evaltime));
2374
2375 /* Find the one on the right frame (assume that these are spaced on 1-frame intervals). */
2376 const FPoint *fpt = prevfpt + (int(evaltime) - int(prevfpt->vec[0]));
2377
2378 /* If not exactly on the frame, perform linear interpolation with the next one. */
2379 if (t != 0.0f && t < 1.0f) {
2380 cvalue = interpf(fpt->vec[1], (fpt + 1)->vec[1], 1.0f - t);
2381 }
2382 else {
2383 cvalue = fpt->vec[1];
2384 }
2385 }
2386
2387 return cvalue;
2388}
2389
2391
2392/* -------------------------------------------------------------------- */
2395
2396/* Evaluate and return the value of the given F-Curve at the specified frame ("evaltime")
2397 * NOTE: this is also used for drivers.
2398 */
2399static float evaluate_fcurve_ex(const FCurve *fcu, float evaltime, float cvalue)
2400{
2401 /* Evaluate modifiers which modify time to evaluate the base curve at. */
2403 storage.modifier_count = fcu->modifiers.count();
2405 storage.buffer = alloca(storage.modifier_count * storage.size_per_modifier);
2406
2407 const float devaltime = evaluate_time_fmodifiers(
2408 &storage, &fcu->modifiers, fcu, cvalue, evaltime);
2409
2410 /* Evaluate curve-data
2411 * - 'devaltime' instead of 'evaltime', as this is the time that the last time-modifying
2412 * F-Curve modifier on the stack requested the curve to be evaluated at.
2413 */
2414 if (fcu->bezt) {
2415 cvalue = fcurve_eval_keyframes(fcu, fcu->bezt, devaltime);
2416 }
2417 else if (fcu->fpt) {
2418 cvalue = fcurve_eval_samples(fcu, fcu->fpt, devaltime);
2419 }
2420
2421 /* Evaluate modifiers. */
2422 evaluate_value_fmodifiers(&storage, &fcu->modifiers, fcu, &cvalue, devaltime);
2423
2424 /* If curve can only have integral values, perform truncation (i.e. drop the decimal part)
2425 * here so that the curve can be sampled correctly.
2426 */
2427 if (fcu->flag & FCURVE_INT_VALUES) {
2428 cvalue = floorf(cvalue + 0.5f);
2429 }
2430
2431 return cvalue;
2432}
2433
2434float evaluate_fcurve(const FCurve *fcu, float evaltime)
2435{
2436 BLI_assert(fcu->driver == nullptr);
2437
2438 return evaluate_fcurve_ex(fcu, evaltime, 0.0);
2439}
2440
2441float evaluate_fcurve_only_curve(const FCurve *fcu, float evaltime)
2442{
2443 /* Can be used to evaluate the (key-framed) f-curve only.
2444 * Also works for driver-f-curves when the driver itself is not relevant.
2445 * E.g. when inserting a keyframe in a driver f-curve. */
2446 return evaluate_fcurve_ex(fcu, evaltime, 0.0);
2447}
2448
2449float evaluate_fcurve_unmodified(const FCurve *fcu, float evaltime)
2450{
2451 if (fcu->bezt) {
2452 return fcurve_eval_keyframes(fcu, fcu->bezt, evaltime);
2453 }
2454 if (fcu->fpt) {
2455 return fcurve_eval_samples(fcu, fcu->fpt, evaltime);
2456 }
2457
2459 return 0;
2460}
2461
2463 FCurve *fcu,
2464 ChannelDriver *driver_orig,
2465 const AnimationEvalContext *anim_eval_context)
2466{
2467 BLI_assert(fcu->driver != nullptr);
2468 float cvalue = 0.0f;
2469 float evaltime = anim_eval_context->eval_time;
2470
2471 /* If there is a driver (only if this F-Curve is acting as 'driver'),
2472 * evaluate it to find value to use as `evaltime` since drivers essentially act as alternative
2473 * input (i.e. in place of 'time') for F-Curves. */
2474 if (fcu->driver) {
2475 /* Evaluation-time now serves as input for the curve. */
2476 evaltime = evaluate_driver(anim_rna, fcu->driver, driver_orig, anim_eval_context);
2477
2478 /* Only do a default 1-1 mapping if it's unlikely that anything else will set a value... */
2479 if (fcu->totvert == 0) {
2480 bool do_linear = true;
2481
2482 /* Out-of-range F-Modifiers will block, as will those which just plain overwrite the values
2483 * XXX: additive is a bit more dicey; it really depends then if things are in range or not...
2484 */
2485 for (FModifier &fcm : fcu->modifiers) {
2486 /* If there are range-restrictions, we must definitely block #36950. */
2487 if ((fcm.flag & FMODIFIER_FLAG_RANGERESTRICT) == 0 ||
2488 (fcm.sfra <= evaltime && fcm.efra >= evaltime))
2489 {
2490 /* Within range: here it probably doesn't matter,
2491 * though we'd want to check on additive. */
2492 }
2493 else {
2494 /* Outside range: modifier shouldn't contribute to the curve here,
2495 * though it does in other areas, so neither should the driver! */
2496 do_linear = false;
2497 }
2498 }
2499
2500 /* Only copy over results if none of the modifiers disagreed with this. */
2501 if (do_linear) {
2502 cvalue = evaltime;
2503 }
2504 }
2505 }
2506
2507 return evaluate_fcurve_ex(fcu, evaltime, cvalue);
2508}
2509
2511{
2512 return fcu->totvert == 0 && fcu->driver == nullptr &&
2514}
2515
2517 FCurve *fcu,
2518 const AnimationEvalContext *anim_eval_context)
2519{
2520 /* Only calculate + set curval (overriding the existing value) if curve has
2521 * any data which warrants this...
2522 */
2523 if (BKE_fcurve_is_empty(fcu)) {
2524 return 0.0f;
2525 }
2526
2527 /* Calculate and set curval (evaluates driver too if necessary). */
2528 float curval;
2529 if (fcu->driver) {
2530 curval = evaluate_fcurve_driver(anim_rna, fcu, fcu->driver, anim_eval_context);
2531 }
2532 else {
2533 curval = evaluate_fcurve(fcu, anim_eval_context->eval_time);
2534 }
2535 fcu->runtime->curval = curval; /* Debug display only, not thread safe! */
2536 return curval;
2537}
2538
2540
2541/* -------------------------------------------------------------------- */
2544
2545void BKE_fmodifiers_blend_write(BlendWriter *writer, ListBaseT<FModifier> *fmodifiers)
2546{
2547 /* Write all modifiers first (for faster reloading) */
2548 writer->write_struct_list(fmodifiers);
2549
2550 /* Modifiers */
2551 for (FModifier &fcm : *fmodifiers) {
2552 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(&fcm);
2553
2554 /* Write the specific data */
2555 if (fmi && fcm.data) {
2556 /* firstly, just write the plain fmi->data struct */
2557 writer->write_struct_by_name(fmi->struct_name, fcm.data);
2558
2559 /* do any modifier specific stuff */
2560 switch (fcm.type) {
2562 FMod_Generator *data = static_cast<FMod_Generator *>(fcm.data);
2563
2564 /* write coefficients array */
2565 if (data->coefficients) {
2566 writer->write_float_array(data->arraysize, data->coefficients);
2567 }
2568
2569 break;
2570 }
2572 FMod_Envelope *data = static_cast<FMod_Envelope *>(fcm.data);
2573
2574 /* write envelope data */
2575 if (data->data) {
2576 writer->write_struct_array(data->totvert, data->data);
2577 }
2578
2579 break;
2580 }
2581 default:
2582 break;
2583 }
2584 }
2585 }
2586}
2587
2589 ListBaseT<FModifier> *fmodifiers,
2590 FCurve *curve)
2591{
2592 for (FModifier &fcm : *fmodifiers) {
2593 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(&fcm);
2594
2595 /* relink general data */
2596 if (fmi) {
2597 fcm.data = BLO_read_struct_by_name_array(reader, fmi->struct_name, 1, fcm.data);
2598 }
2599 else {
2600 /* This can happen when the blend file has data for a modifier that doesn't exist in this
2601 * Blender version (when the blend file is newer). */
2604 RPT_("F-Curve modifier lost on '%s[%d]' because it has an unknown type"),
2605 curve->rna_path().c_str(),
2606 curve->array_index);
2607 fcm.data = nullptr;
2608 }
2609 fcm.curve = curve;
2610
2611 /* do relinking of data for specific types */
2612 switch (fcm.type) {
2614 FMod_Generator *data = static_cast<FMod_Generator *>(fcm.data);
2615 BLO_read_array_and_validate_size(reader, &data->coefficients, &data->arraysize);
2616 break;
2617 }
2619 FMod_Envelope *data = static_cast<FMod_Envelope *>(fcm.data);
2620
2622
2623 break;
2624 }
2625 default:
2626 break;
2627 }
2628 }
2629}
2630
2632{
2633 /* curve data */
2634 if (fcu->bezt) {
2635 writer->write_struct_array(fcu->totvert, fcu->bezt);
2636 }
2637 if (fcu->fpt) {
2638 writer->write_struct_array(fcu->totvert, fcu->fpt);
2639 }
2640
2641 if (fcu->rna_path_ptr) {
2642 writer->write_string(fcu->rna_path_ptr);
2643 }
2644
2645 /* driver data */
2646 if (fcu->driver) {
2647 ChannelDriver *driver = fcu->driver;
2648
2649 writer->write_struct(driver);
2650
2651 /* variables */
2652 writer->write_struct_list(&driver->variables);
2653 for (DriverVar &dvar : driver->variables) {
2655 if (dtar->rna_path) {
2656 writer->write_string(dtar->rna_path);
2657 }
2658 }
2660 }
2661 }
2662
2663 /* write F-Modifiers */
2665}
2666
2667void BKE_fcurve_blend_write_listbase(BlendWriter *writer, ListBaseT<FCurve> *fcurves)
2668{
2669 writer->write_struct_list(fcurves, [](BlendStructWriter<FCurve> &struct_writer) {
2670 struct_writer.shallow_data.runtime = nullptr;
2671 });
2672 for (FCurve &fcu : *fcurves) {
2673 BKE_fcurve_blend_write_data(writer, &fcu);
2674 }
2675}
2676
2678{
2679 /* Curve data: only one of `bezt`/`fpt` is set, so guard validation to avoid clobbering
2680 * `totvert` when reading the unset pointer. */
2681 fcu->runtime = MEM_new<bke::FCurveRuntime>(__func__);
2682 if (fcu->bezt) {
2683 BLO_read_array_and_validate_size(reader, &fcu->bezt, &fcu->totvert);
2684 }
2685 if (fcu->fpt) {
2686 BLO_read_array_and_validate_size(reader, &fcu->fpt, &fcu->totvert);
2687 }
2688
2689 /* rna path */
2690 BLO_read_string(reader, &fcu->rna_path_ptr);
2691 if (fcu->rna_path_ptr) {
2693 }
2694
2695 /* group */
2696 BLO_read_struct(reader, bActionGroup, &fcu->grp);
2697
2698 /* clear disabled flag - allows disabled drivers to be tried again (#32155),
2699 * but also means that another method for "reviving disabled F-Curves" exists
2700 */
2701 fcu->flag &= ~FCURVE_DISABLED;
2702
2703 /* driver */
2704 BLO_read_struct(reader, ChannelDriver, &fcu->driver);
2705 if (fcu->driver) {
2706 ChannelDriver *driver = fcu->driver;
2707
2708 /* Compiled expression data will need to be regenerated
2709 * (old pointer may still be set here). */
2710 driver->expr_comp = nullptr;
2711 driver->expr_simple = nullptr;
2712
2713 /* Give the driver a fresh chance - the operating environment may be different now
2714 * (addons, etc. may be different) so the driver namespace may be sane now #32155. */
2716
2717 /* relink variables, targets and their paths */
2718 BLO_read_struct_list(reader, DriverVar, &driver->variables);
2719 for (DriverVar &dvar : driver->variables) {
2721 /* only relink the targets being used */
2722 if (tarIndex < dvar.num_targets) {
2723 BLO_read_string(reader, &dtar->rna_path);
2724 }
2725 else {
2726 dtar->rna_path = nullptr;
2727 dtar->id = nullptr;
2728 }
2729 }
2731 }
2732 }
2733
2734 /* modifiers */
2736 BKE_fmodifiers_blend_read_data(reader, &fcu->modifiers, fcu);
2737}
2738
2739void BKE_fcurve_blend_read_data_listbase(BlendDataReader *reader, ListBaseT<FCurve> *fcurves)
2740{
2741 /* Link F-Curve data to F-Curve again (non ID-libraries). */
2742 for (FCurve &fcu : *fcurves) {
2743 BKE_fcurve_blend_read_data(reader, &fcu);
2744 }
2745}
2746
2748
2749} // namespace blender
Functions and classes to work with Actions.
#define BEZT_BINARYSEARCH_THRESH
#define DRIVER_TARGETS_LOOPER_BEGIN(dvar)
#define DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
#define DRIVER_TARGETS_LOOPER_END
#define BKE_LIB_FOREACHID_PROCESS_ID(data_, id_, cb_flag_)
#define SELECT
Definition BKE_mask.hh:19
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_ACTION
#define RPT_(msgid)
#define CTX_DATA_(context, msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define FCURVE_ACTIVE_KEYFRAME_NONE
#define BEZT_IS_AUTOH(bezt)
#define BEZT_ISSEL_ANY(bezt)
#define BEZT_DESEL_ALL(bezt)
Read Blender memory allocation and freeing.
#define SMALL
#define CLAMP_MAX(a, c)
#define BLI_assert_unreachable()
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define IS_EQT(a, b, c)
#define STREQ(a, b)
#define CLAMP_MIN(a, b)
#define MEM_realloc_uninitialized(vmemh, len)
btScalar max
const T * data() const
Definition BLI_array.hh:323
constexpr IndexRange()=default
static std::optional< ParsedRNAPath > from_string(StringRefNull path)
Definition rna_path.cc:194
constexpr StringRefNull()
bool add(const Key &key)
int64_t size() const
IndexRange index_range() const
#define offsetof(t, d)
#define INT_MAX
#define FLT_EPSILON
#define storage(slot, qualifiers)
#define round
#define cos
#define abs
#define floor
#define acos
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
FCurve * fcurve_find_in_action_slot(bAction *act, slot_handle_t slot_handle, const FCurveDescriptor &fcurve_descriptor)
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:69
BMesh Iterators.
MINLINE float min_ff(float a, float b)
VecBase< uint32_t, 2 > uint2
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
float BLI_easing_elastic_ease_in(float time, float begin, float change, float duration, float amplitude, float period)
Definition easing.cc:148
float BLI_easing_quint_ease_out(float time, float begin, float change, float duration)
Definition easing.cc:336
static BezTriple * cycle_offset_triple(bool cycle, BezTriple *out, const BezTriple *in, const BezTriple *from, const BezTriple *to)
bool BKE_fcurve_calc_range(const FCurve *fcu, float *r_min, float *r_max, bool selected_keys_only)
char * STRNCPY(char(&dst)[N], const char *src)
@ DRIVER_FLAG_PYTHON_BLOCKED
@ DRIVER_FLAG_INVALID
MINLINE int compare_ff_relative(float a, float b, float max_diff, int max_ulps)
void BKE_fcurve_keyframe_move_value_with_handles(BezTriple *keyframe, float new_value)
@ HD_AUTOTYPE_LOCKED_FINAL
@ RPT_WARNING
Definition BKE_report.hh:40
@ FMI_TYPE_GENERATE_CURVE
bool BKE_fcurve_delete_keys_selected(FCurve &fcu)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void testhandles_fcurve(FCurve *fcu, eBezTriple_Flag sel_flag, bool use_handle)
BezTriple * BKE_bezier_array_merge(const BezTriple *a, int size_a, const BezTriple *b, int size_b, int *r_merged_size)
float BLI_easing_sine_ease_in(float time, float begin, float change, float duration)
Definition easing.cc:350
float BLI_easing_back_ease_in_out(float time, float begin, float change, float duration, float overshoot)
Definition easing.cc:34
unsigned int uint
Definition BKE_text.h:55
float BLI_easing_expo_ease_in_out(float time, float begin, float change, float duration)
Definition easing.cc:273
BlendFileReadReport * BLO_read_data_reports(BlendDataReader *reader)
Definition readfile.cc:6834
float(*) FcuSampleFunc(FCurve *fcu, void *data, float evaltime)
void * BLO_read_struct_by_name_array(BlendDataReader *reader, StringRef struct_name, int64_t items_num, const void *old_address)
Definition readfile.cc:6521
static int BKE_fcurve_bezt_binarysearch_index_ex(const BezTriple array[], const float frame, const int arraylen, const float threshold, bool *r_replace)
float calculate_fcurve(PathResolvedRNA *anim_rna, FCurve *fcu, const AnimationEvalContext *anim_eval_context)
MINLINE void add_v2_v2v2(float r[2], const float a[2], const float b[2])
FCurve * BKE_fcurve_iter_step(FCurve *fcu_iter, const char rna_path[])
bool BKE_fcurve_is_keyframable(const FCurve &fcu)
void BKE_nurb_bezt_handle_test(BezTriple *bezt, eBezTriple_Flag__Alias sel_flag, const eNurbHandleTest_Mode handle_mode, bool use_around_local)
Definition curve.cc:4083
float BLI_easing_quad_ease_out(float time, float begin, float change, float duration)
Definition easing.cc:295
std::optional< std::string > RNA_path_from_ID_to_property(const PointerRNA *ptr, PropertyRNA *prop)
Definition rna_path.cc:1237
float evaluate_driver(struct PathResolvedRNA *anim_rna, struct ChannelDriver *driver, struct ChannelDriver *driver_orig, const struct AnimationEvalContext *anim_eval_context)
void BKE_fcurves_free(ListBaseT< FCurve > *list)
static bool get_bounding_bezt_indices(const FCurve *fcu, const bool selected_keys_only, const float frame_range[2], int *r_first, int *r_last)
uint evaluate_fmodifiers_storage_size_per_modifier(const ListBaseT< FModifier > *modifiers)
const char * RNA_property_identifier(const PropertyRNA *prop)
void BKE_fcurve_delete_keys(FCurve &fcu, uint2 index_range)
void BKE_fcurve_update_handle_flag_from_opposite(BezTriple &key, HandleSide source_side)
MINLINE void copy_v2_v2(float r[2], const float a[2])
@ FCU_CYCLE_OFFSET
@ FCU_CYCLE_NONE
@ FCU_CYCLE_PERFECT
void BKE_fmodifier_ensure_flag(ListBaseT< FModifier > *modifiers)
void BKE_fmodifier_name_set(FModifier *fcm, const char *name)
void copy_fmodifiers(ListBaseT< FModifier > *dst, const ListBaseT< FModifier > *src)
struct ChannelDriver * fcurve_copy_driver(const struct ChannelDriver *driver)
bool BKE_fcurve_bezt_subdivide_handles(BezTriple *bezt, BezTriple *prev, BezTriple *next, float *r_pdelta)
void BKE_fcurve_delete_key(FCurve *fcu, int index)
FCurve * BKE_fcurve_find_by_rna(PointerRNA *ptr, PropertyRNA *prop, int rnaindex, AnimData **r_adt, bAction **r_action, bool *r_driven, bool *r_special)
void BKE_fcurve_handles_recalc_ex(FCurve &fcu, eBezTriple_Flag handle_sel_flag)
void BKE_fcurve_blend_read_data_listbase(BlendDataReader *reader, ListBaseT< FCurve > *fcurves)
void BKE_fcurve_foreach_id(FCurve *fcu, LibraryForeachIDData *data)
const FModifierTypeInfo * fmodifier_get_typeinfo(const FModifier *fcm)
MINLINE void swap_v2_v2(float a[2], float b[2])
float BLI_easing_cubic_ease_in_out(float time, float begin, float change, float duration)
Definition easing.cc:110
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
float evaluate_fcurve_unmodified(const FCurve *fcu, float evaltime)
@ NURB_HANDLE_TEST_EACH
Definition BKE_curve.hh:65
@ NURB_HANDLE_TEST_KNOT_ONLY
Definition BKE_curve.hh:75
bool RNA_property_animateable(const PointerRNA *ptr, PropertyRNA *prop_orig)
Span< rna_path::Item > ParsedRNAPathRef
Definition RNA_path.hh:113
float BLI_easing_quart_ease_out(float time, float begin, float change, float duration)
Definition easing.cc:316
float BLI_easing_cubic_ease_out(float time, float begin, float change, float duration)
Definition easing.cc:104
void BKE_fcurve_blend_write_data(BlendWriter *writer, FCurve *fcu)
float BLI_easing_sine_ease_out(float time, float begin, float change, float duration)
Definition easing.cc:355
static void calculate_bezt_bounds_y(BezTriple *bezt_array, const int index_range[2], const bool selected_keys_only, const bool include_handles, float *r_min, float *r_max)
float BLI_easing_back_ease_out(float time, float begin, float change, float duration, float overshoot)
Definition easing.cc:27
void BKE_fcurve_blend_read_data(BlendDataReader *reader, FCurve *fcu)
void BKE_fcurve_blend_write_listbase(BlendWriter *writer, ListBaseT< FCurve > *fcurves)
bool list_has_suitable_fmodifier(const ListBaseT< FModifier > *modifiers, int mtype, short acttype)
float BLI_easing_linear_ease(float time, float begin, float change, float duration)
Definition easing.cc:284
void BKE_fmodifiers_blend_write(BlendWriter *writer, ListBaseT< FModifier > *fmodifiers)
float BLI_easing_expo_ease_in(float time, float begin, float change, float duration)
Definition easing.cc:257
static void init_unbaked_bezt_data(BezTriple *bezt)
float BLI_easing_bounce_ease_out(float time, float begin, float change, float duration)
Definition easing.cc:45
static void calculate_bezt_bounds_x(BezTriple *bezt_array, const int index_range[2], const bool include_handles, float *r_min, float *r_max)
void BKE_fcurve_merge_duplicate_keys(FCurve *fcu, const int sel_flag, const bool use_handle)
float BLI_easing_circ_ease_out(float time, float begin, float change, float duration)
Definition easing.cc:83
Global G
Definition blender.cc:57
float BLI_easing_circ_ease_in_out(float time, float begin, float change, float duration)
Definition easing.cc:89
@ FMODIFIER_TYPE_ENVELOPE
@ FMODIFIER_TYPE_STEPPED
@ FMODIFIER_TYPE_FN_GENERATOR
@ FMODIFIER_TYPE_CYCLES
@ FMODIFIER_TYPE_GENERATOR
@ FMODIFIER_TYPE_NOISE
static void fcurve_bezt_free(FCurve &fcu)
bool BLI_rctf_is_valid(const struct rctf *rect)
float BLI_easing_quint_ease_in(float time, float begin, float change, float duration)
Definition easing.cc:331
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
int BLI_sortutil_cmp_float(const void *a_, const void *b_)
Definition sort_utils.cc:27
void BKE_fcurve_keyframe_move_time_with_handles(BezTriple *keyframe, const float new_time)
void BLI_uniquename(const ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
bool BKE_nlastrip_has_curves_for_property(const PointerRNA *ptr, const PropertyRNA *prop)
@ FCM_EXTRAPOLATE_CYCLIC_OFFSET
@ FCM_EXTRAPOLATE_CYCLIC
bool BKE_fcurve_is_protected(const FCurve &fcu)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BKE_fcurves_copy(ListBaseT< FCurve > *dst, ListBaseT< FCurve > *src)
void evaluate_value_fmodifiers(FModifiersStackStorage *storage, const ListBaseT< FModifier > *modifiers, const FCurve *fcu, float *cvalue, float evaltime)
void BKE_fcurve_handles_recalc(FCurve &fcu)
bool BKE_fcurve_calc_bounds(const FCurve *fcu, bool selected_keys_only, bool include_handles, const float frame_range[2], rctf *r_bounds)
BMesh const char void * data
void BKE_fcurve_deselect_all_keys(FCurve &fcu)
@ FCURVE_DISCRETE_VALUES
float BLI_easing_quad_ease_in_out(float time, float begin, float change, float duration)
Definition easing.cc:301
FCurve * BKE_animadata_fcurve_find_by_rna_path(AnimData *animdata, const char *rna_path, const int rna_index, bAction **r_action, bool *r_driven)
FCurve * BKE_fcurve_copy(const FCurve *fcu)
sqrt(x)+1/max(0
static float fcurve_eval_keyframes_extrapolate(const FCurve *fcu, const BezTriple *bezts, float evaltime, int endpoint_offset, int direction_to_neighbor)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
eFCU_Cycle_Type BKE_fcurve_get_cycle_type(const FCurve &fcu)
float evaluate_fcurve_driver(PathResolvedRNA *anim_rna, FCurve *fcu, ChannelDriver *driver_orig, const AnimationEvalContext *anim_eval_context)
float BLI_easing_back_ease_in(float time, float begin, float change, float duration, float overshoot)
Definition easing.cc:20
static bool calculate_fpt_bounds(const FCurve *fcu, const float frame_range[2], rctf *r_bounds)
void BLO_reportf_wrap(BlendFileReadReport *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition readfile.cc:237
void free_fmodifiers(ListBaseT< FModifier > *modifiers)
bool BKE_fcurve_is_cyclic(const FCurve &fcu)
float BLI_easing_quart_ease_in_out(float time, float begin, float change, float duration)
Definition easing.cc:322
float BLI_easing_bounce_ease_in(float time, float begin, float change, float duration)
Definition easing.cc:63
MINLINE double sqrt3d(double d)
void BKE_nurb_handle_smooth_fcurve(BezTriple *bezt, int total, bool cyclic)
Definition curve.cc:3926
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
float BLI_easing_expo_ease_out(float time, float begin, float change, float duration)
Definition easing.cc:265
MINLINE void add_v3_v3(float r[3], const float a[3])
static float fcurve_eval_keyframes_interpolate(const FCurve *fcu, const BezTriple *bezts, float evaltime)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
static void berekeny(float f1, float f2, float f3, float f4, float *o, int b)
static float evaluate_fcurve_ex(const FCurve *fcu, float evaltime, float cvalue)
void BKE_fcurve_free(FCurve *fcu)
void BKE_fcurve_correct_bezpart(const float v1[2], float v2[2], float v3[2], const float v4[2])
Array< float > BKE_fcurves_calc_keyed_frames_ex(Span< FCurve * > fcurve_array, float interval)
float evaluate_fcurve_only_curve(const FCurve *fcu, float evaltime)
float BLI_easing_bounce_ease_in_out(float time, float begin, float change, float duration)
Definition easing.cc:68
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_fcurve_active_keyframe_set(FCurve *fcu, const BezTriple *active_bezt)
int BKE_fcurve_active_keyframe_index(const FCurve *fcu)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
float BLI_easing_quint_ease_in_out(float time, float begin, float change, float duration)
Definition easing.cc:341
void interp_v2_v2v2(float r[2], const float a[2], const float b[2], float t)
iter begin(iter)
MINLINE float max_ff(float a, float b)
Array< float > BKE_fcurves_calc_keyed_frames(Span< FCurve * > fcurve_array)
float BLI_easing_circ_ease_in(float time, float begin, float change, float duration)
Definition easing.cc:77
bool BKE_fcurve_are_keyframes_usable(const FCurve &fcu)
FCurve * id_data_find_fcurve(ID *id, void *data, StructRNA *type, const char *prop_name, int index, bool *r_driven)
void BKE_fmodifiers_blend_read_data(BlendDataReader *reader, ListBaseT< FModifier > *fmodifiers, FCurve *curve)
void BKE_fcurve_delete_keys_all(FCurve &fcu)
static CLG_LogRef LOG
Definition logging.cpp:12
MINLINE float interpf(float target, float origin, float t)
@ FCM_GENERATOR_ADDITIVE
float BLI_easing_elastic_ease_in_out(float time, float begin, float change, float duration, float amplitude, float period)
Definition easing.cc:213
bool test_time_fcurve(FCurve &fcu)
int BKE_fcurve_bezt_binarysearch_index(const BezTriple array[], float frame, int arraylen, bool *r_replace)
void fcurve_free_driver(struct FCurve *fcu)
void BKE_fcurve_bezt_resize(FCurve &fcu, int new_totvert)
bool BKE_fcurve_is_empty(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)
FCurve * BKE_fcurve_find_by_rna_context_ui(bContext *C, const PointerRNA *ptr, PropertyRNA *prop, int rnaindex, AnimData **r_animdata, bAction **r_action, bool *r_driven, bool *r_special)
float BLI_easing_quad_ease_in(float time, float begin, float change, float duration)
Definition easing.cc:289
FCurve * BKE_fcurve_find(ListBaseT< FCurve > *list, const char rna_path[], int array_index)
float fcurve_samplingcb_evalcurve(FCurve *fcu, void *data, float evaltime)
static int solve_cubic(double c0, double c1, double c2, double c3, float *o)
static float fcurve_eval_samples(const FCurve *fcu, const FPoint *fpts, float evaltime)
float BLI_easing_quart_ease_in(float time, float begin, float change, float duration)
Definition easing.cc:310
ListBase BLI_listbase_from_link(Link *some_link)
Definition listbase.cc:814
bool BKE_fcurve_has_selected_control_points(const FCurve &fcu)
MINLINE int clamp_i(int value, int min, int max)
@ FCURVE_EXTRAPOLATE_CONSTANT
static float fcurve_eval_keyframes(const FCurve *fcu, const BezTriple *bezts, float evaltime)
static int findzero(float x, float q0, float q1, float q2, float q3, float *o)
@ FMI_REQUIRES_ORIGINAL_DATA
float BLI_easing_sine_ease_in_out(float time, float begin, float change, float duration)
Definition easing.cc:360
const FModifierTypeInfo * get_fmodifier_typeinfo(int type)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
float BLI_easing_cubic_ease_in(float time, float begin, float change, float duration)
Definition easing.cc:98
float evaluate_time_fmodifiers(FModifiersStackStorage *storage, const ListBaseT< FModifier > *modifiers, const FCurve *fcu, float cvalue, float evaltime)
void fcurve_samples_to_keyframes(FCurve *fcu, int start, int end)
float BLI_easing_elastic_ease_out(float time, float begin, float change, float duration, float amplitude, float period)
Definition easing.cc:181
FCurve * BKE_fcurve_create()
void sort_time_fcurve(FCurve &fcu)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
float evaluate_fcurve(const FCurve *fcu, float evaltime)
static bool calculate_bezt_bounds(const FCurve *fcu, const bool selected_keys_only, const bool include_handles, const float frame_range[2], rctf *r_bounds)
@ FMODIFIER_FLAG_MUTED
@ FMODIFIER_FLAG_DISABLED
@ FMODIFIER_FLAG_RANGERESTRICT
@ FMODIFIER_FLAG_USEINFLUENCE
const btScalar eps
Definition poly34.cpp:11
#define floorf
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#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
void write_string(const char *data)
void write_struct_by_name(const char *struct_name, const void *data, BlendStructWriterFn fn=nullptr)
void write_float_array(int64_t num, const float *data)
void write_struct_list(const ListBaseT< T > *list, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
ListBaseT< DriverVar > variables
struct ExprPyLike_Parsed * expr_simple
bActionGroup * grp
eFCurve_Flags flag
eFCurve_Extend extend
struct FCurve * prev
struct FCurve * next
ListBaseT< FModifier > modifiers
ChannelDriver * driver
bke::FCurveRuntime * runtime
unsigned int totvert
eFModifier_Flags flag
eFModifier_Types type
ListBaseT< FCurve > fcurves
std::optional< ParsedRNAPath<> > parsed_rna_path
Definition BKE_fcurve.hh:48