Blender V5.3
fmodifier.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 <algorithm> /* For `min/max`. */
10#include <cfloat>
11#include <cmath>
12#include <cstdio>
13#include <cstring>
14
15#include "MEM_guardedalloc.h"
16
17#include "CLG_log.h"
18
19#include "DNA_anim_types.h"
20#include "DNA_screen_types.h"
21
22#include "BLT_translation.hh"
23
24#include "BLI_listbase.hh"
25#include "BLI_math_base_c.hh"
26#include "BLI_noise.hh"
27#include "BLI_noise_c.hh"
28#include "BLI_utildefines.hh"
29
30#include "BKE_fcurve.hh"
31
32namespace blender {
33
34static CLG_LogRef LOG = {"anim.fmodifier"};
35
36/* -------------------------------------------------------------------- */
39
40/* Info ------------------------------- */
41
42/* F-Modifiers are modifiers which operate on F-Curves. However, they can also be defined
43 * on NLA-Strips to affect all of the F-Curves referenced by the NLA-Strip.
44 */
45
46/* Template --------------------------- */
47
48/* Each modifier defines a set of functions, which will be called at the appropriate
49 * times. In addition to this, each modifier should have a type-info struct, where
50 * its functions are attached for use.
51 */
52
53/* Template for type-info data:
54 * - make a copy of this when creating new modifiers, and just change the functions
55 * pointed to as necessary
56 * - although the naming of functions doesn't matter, it would help for code
57 * readability, to follow the same naming convention as is presented here
58 * - any functions that a constraint doesn't need to define, don't define
59 * for such cases, just use nullptr
60 * - these should be defined after all the functions have been defined, so that
61 * forward-definitions/prototypes don't need to be used!
62 * - keep this copy #if-def'd so that future modifier can get based off this
63 */
64#if 0
65static FModifierTypeInfo FMI_MODNAME = {
66 /*type*/ FMODIFIER_TYPE_MODNAME,
67 /*size*/ sizeof(FMod_ModName),
68 /*acttype*/ FMI_TYPE_SOME_ACTION,
69 /*requires_flag*/ FMI_REQUIRES_SOME_REQUIREMENT,
70 /*name*/ "Modifier Name",
71 /*struct_name*/ "FMod_ModName",
72 /*storage_size*/ 0,
73 /*free_data*/ fcm_modname_free,
74 /*copy_data*/ fcm_modname_copy,
75 /*new_data*/ fcm_modname_new_data,
76 /*verify_data*/ fcm_modname_verify,
77 /*evaluate_modifier_time*/ fcm_modname_time,
78 /*evaluate_modifier*/ fcm_modname_evaluate,
79};
80#endif
81
82/* Generator F-Curve Modifier --------------------------- */
83
84/* Generators available:
85 * 1) simple polynomial generator:
86 * - Expanded form:
87 * (y = C[0]*(x^(n)) + C[1]*(x^(n-1)) + ... + C[n])
88 * - Factorized form:
89 * (y = (C[0][0]*x + C[0][1]) * (C[1][0]*x + C[1][1]) * ... * (C[n][0]*x + C[n][1]))
90 */
91
93{
94 FMod_Generator *data = static_cast<FMod_Generator *>(fcm->data);
95
96 /* free polynomial coefficients array */
97 if (data->coefficients) {
98 MEM_delete(data->coefficients);
99 }
100}
101
102static void fcm_generator_copy(FModifier *fcm, const FModifier *src)
103{
104 FMod_Generator *gen = static_cast<FMod_Generator *>(fcm->data);
105 FMod_Generator *ogen = static_cast<FMod_Generator *>(src->data);
106
107 /* copy coefficients array? */
108 if (ogen->coefficients) {
109 gen->coefficients = MEM_dupalloc(ogen->coefficients);
110 }
111}
112
113static void fcm_generator_new_data(void *mdata)
114{
115 FMod_Generator *data = static_cast<FMod_Generator *>(mdata);
116 float *cp;
117
118 /* set default generator to be linear 0-1 (gradient = 1, y-offset = 0) */
119 data->poly_order = 1;
120 data->arraysize = 2;
121 cp = data->coefficients = MEM_new_array_zeroed<float>(2, "FMod_Generator_Coefs");
122 cp[0] = 0; /* y-offset */
123 cp[1] = 1; /* gradient */
124}
125
127{
128 FMod_Generator *data = static_cast<FMod_Generator *>(fcm->data);
129
130 /* requirements depend on mode */
131 switch (data->mode) {
132 case FCM_GENERATOR_POLYNOMIAL: /* expanded polynomial expression */
133 {
134 const int arraysize_new = data->poly_order + 1;
135 /* arraysize needs to be order+1, so resize if not */
136 if (data->arraysize != arraysize_new) {
137 data->coefficients = static_cast<float *>(
138 MEM_realloc_zeroed(data->coefficients, sizeof(float) * arraysize_new));
139 data->arraysize = arraysize_new;
140 }
141 break;
142 }
143 case FCM_GENERATOR_POLYNOMIAL_FACTORISED: /* expanded polynomial expression */
144 {
145 const int arraysize_new = data->poly_order * 2;
146 /* arraysize needs to be (2 * order), so resize if not */
147 if (data->arraysize != arraysize_new) {
148 data->coefficients = static_cast<float *>(
149 MEM_realloc_zeroed(data->coefficients, sizeof(float) * arraysize_new));
150 data->arraysize = arraysize_new;
151 }
152 break;
153 }
154 }
155}
156
157static void fcm_generator_evaluate(const FCurve * /*fcu*/,
158 const FModifier *fcm,
159 float *cvalue,
160 float evaltime,
161 void * /*storage*/)
162{
163 FMod_Generator *data = static_cast<FMod_Generator *>(fcm->data);
164
165 /* behavior depends on mode
166 * NOTE: the data in its default state is fine too
167 */
168 switch (data->mode) {
169 case FCM_GENERATOR_POLYNOMIAL: /* expanded polynomial expression */
170 {
171 /* we overwrite cvalue with the sum of the polynomial */
172 float *powers = MEM_new_array_zeroed<float>(data->arraysize, "Poly Powers");
173 float value = 0.0f;
174
175 /* for each x^n, precalculate value based on previous one first... this should be
176 * faster that calling pow() for each entry
177 */
178 for (uint i = 0; i < data->arraysize; i++) {
179 /* first entry is x^0 = 1, otherwise, calculate based on previous */
180 if (i) {
181 powers[i] = powers[i - 1] * evaltime;
182 }
183 else {
184 powers[0] = 1;
185 }
186 }
187
188 /* for each coefficient, add to value, which we'll write to *cvalue in one go */
189 for (uint i = 0; i < data->arraysize; i++) {
190 value += data->coefficients[i] * powers[i];
191 }
192
193 /* only if something changed, write *cvalue in one go */
194 if (data->poly_order) {
195 if (data->flag & FCM_GENERATOR_ADDITIVE) {
196 *cvalue += value;
197 }
198 else {
199 *cvalue = value;
200 }
201 }
202
203 /* cleanup */
204 if (powers) {
205 MEM_delete(powers);
206 }
207 break;
208 }
209 case FCM_GENERATOR_POLYNOMIAL_FACTORISED: /* Factorized polynomial */
210 {
211 float value = 1.0f, *cp = nullptr;
212 uint i;
213
214 /* For each coefficient pair,
215 * solve for that bracket before accumulating in value by multiplying. */
216 for (cp = data->coefficients, i = 0; (cp) && (i < uint(data->poly_order)); cp += 2, i++) {
217 value *= (cp[0] * evaltime + cp[1]);
218 }
219
220 /* only if something changed, write *cvalue in one go */
221 if (data->poly_order) {
222 if (data->flag & FCM_GENERATOR_ADDITIVE) {
223 *cvalue += value;
224 }
225 else {
226 *cvalue = value;
227 }
228 }
229 break;
230 }
231 }
232}
233
236 /*size*/ sizeof(FMod_Generator),
237 /*acttype*/ FMI_TYPE_GENERATE_CURVE,
238 /*requires_flag*/ FMI_REQUIRES_NOTHING,
239 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_ACTION, "Generator"),
240 /*struct_name*/ "FMod_Generator",
241 /*storage_size*/ 0,
242 /*free_data*/ fcm_generator_free,
243 /*copy_data*/ fcm_generator_copy,
244 /*new_data*/ fcm_generator_new_data,
245 /*verify_data*/ fcm_generator_verify,
246 /*evaluate_modifier_time*/ nullptr,
247 /*evaluate_modifier*/ fcm_generator_evaluate,
248};
249
250/* Built-In Function Generator F-Curve Modifier --------------------------- */
251
252/* This uses the general equation for equations:
253 * y = amplitude * fn(phase_multiplier * x + phase_offset) + y_offset
254 *
255 * where amplitude, phase_multiplier/offset, y_offset are user-defined coefficients,
256 * x is the evaluation 'time', and 'y' is the resultant value
257 *
258 * Functions available are
259 * sin, cos, tan, sinc (normalized sin), natural log, square root
260 */
261
262static void fcm_fn_generator_new_data(void *mdata)
263{
264 FMod_FunctionGenerator *data = static_cast<FMod_FunctionGenerator *>(mdata);
265
266 /* set amplitude and phase multiplier to 1.0f so that something is generated */
267 data->amplitude = 1.0f;
268 data->phase_multiplier = 1.0f;
269}
270
271/* Unary 'normalized sine' function
272 * y = sin(PI + x) / (PI * x),
273 * except for x = 0 when y = 1.
274 */
275static double sinc(double x)
276{
277 if (fabs(x) < 0.0001) {
278 return 1.0;
279 }
280
281 return sin(M_PI * x) / (M_PI * x);
282}
283
284static void fcm_fn_generator_evaluate(const FCurve * /*fcu*/,
285 const FModifier *fcm,
286 float *cvalue,
287 float evaltime,
288 void * /*storage*/)
289{
291 double arg = data->phase_multiplier * evaltime + data->phase_offset;
292 double (*fn)(double v) = nullptr;
293
294 /* get function pointer to the func to use:
295 * WARNING: must perform special argument validation hereto guard against crashes
296 */
297 switch (data->type) {
298 /* simple ones */
299 case FCM_GENERATOR_FN_SIN: /* sine wave */
300 fn = sin;
301 break;
302 case FCM_GENERATOR_FN_COS: /* cosine wave */
303 fn = cos;
304 break;
305 case FCM_GENERATOR_FN_SINC: /* normalized sine wave */
306 fn = sinc;
307 break;
308
309 /* validation required */
310 case FCM_GENERATOR_FN_TAN: /* tangent wave */
311 {
312 /* check that argument is not on one of the discontinuities (i.e. 90deg, 270 deg, etc) */
313 if (IS_EQ(fmod((arg - M_PI_2), M_PI), 0.0)) {
314 if ((data->flag & FCM_GENERATOR_ADDITIVE) == 0) {
315 *cvalue = 0.0f; /* no value possible here */
316 }
317 }
318 else {
319 fn = tan;
320 }
321 break;
322 }
323 case FCM_GENERATOR_FN_LN: /* natural log */
324 {
325 /* check that value is greater than 1? */
326 if (arg > 1.0) {
327 fn = log;
328 }
329 else {
330 if ((data->flag & FCM_GENERATOR_ADDITIVE) == 0) {
331 *cvalue = 0.0f; /* no value possible here */
332 }
333 }
334 break;
335 }
336 case FCM_GENERATOR_FN_SQRT: /* square root */
337 {
338 /* no negative numbers */
339 if (arg > 0.0) {
340 fn = sqrt;
341 }
342 else {
343 if ((data->flag & FCM_GENERATOR_ADDITIVE) == 0) {
344 *cvalue = 0.0f; /* no value possible here */
345 }
346 }
347 break;
348 }
349 default:
350 CLOG_ERROR(&LOG, "Invalid Function-Generator for F-Modifier - %d", data->type);
351 break;
352 }
353
354 /* execute function callback to set value if appropriate */
355 if (fn) {
356 float value = (data->amplitude * float(fn(arg)) + data->value_offset);
357
358 if (data->flag & FCM_GENERATOR_ADDITIVE) {
359 *cvalue += value;
360 }
361 else {
362 *cvalue = value;
363 }
364 }
365}
366
369 /*size*/ sizeof(FMod_FunctionGenerator),
370 /*acttype*/ FMI_TYPE_GENERATE_CURVE,
371 /*requires_flag*/ FMI_REQUIRES_NOTHING,
372 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_ACTION, "Built-In Function"),
373 /*struct_name*/ "FMod_FunctionGenerator",
374 /*storage_size*/ 0,
375 /*free_data*/ nullptr,
376 /*copy_data*/ nullptr,
377 /*new_data*/ fcm_fn_generator_new_data,
378 /*verify_data*/ nullptr,
379 /*evaluate_modifier_time*/ nullptr,
380 /*evaluate_modifier*/ fcm_fn_generator_evaluate,
381};
382
383/* Envelope F-Curve Modifier --------------------------- */
384
386{
387 FMod_Envelope *env = static_cast<FMod_Envelope *>(fcm->data);
388
389 /* free envelope data array */
390 if (env->data) {
391 MEM_delete(env->data);
392 }
393}
394
395static void fcm_envelope_copy(FModifier *fcm, const FModifier *src)
396{
397 FMod_Envelope *env = static_cast<FMod_Envelope *>(fcm->data);
398 FMod_Envelope *oenv = static_cast<FMod_Envelope *>(src->data);
399
400 /* copy envelope data array */
401 if (oenv->data) {
402 env->data = MEM_dupalloc(oenv->data);
403 }
404}
405
406static void fcm_envelope_new_data(void *mdata)
407{
408 FMod_Envelope *env = static_cast<FMod_Envelope *>(mdata);
409
410 /* set default min/max ranges */
411 env->min = -1.0f;
412 env->max = 1.0f;
413}
414
416{
417 FMod_Envelope *env = static_cast<FMod_Envelope *>(fcm->data);
418
419 /* if the are points, perform bubble-sort on them, as user may have changed the order */
420 if (env->data) {
421 /* XXX todo... */
422 }
423}
424
425static void fcm_envelope_evaluate(const FCurve * /*fcu*/,
426 const FModifier *fcm,
427 float *cvalue,
428 float evaltime,
429 void * /*storage*/)
430{
431 FMod_Envelope *env = static_cast<FMod_Envelope *>(fcm->data);
432 FCM_EnvelopeData *fed, *prevfed, *lastfed;
433 float min = 0.0f, max = 0.0f, fac = 0.0f;
434 int a;
435
436 /* get pointers */
437 if (env->data == nullptr) {
438 return;
439 }
440 prevfed = env->data;
441 fed = prevfed + 1;
442 lastfed = prevfed + (env->totvert - 1);
443
444 /* get min/max values for envelope at evaluation time (relative to mid-value) */
445 if (prevfed->time >= evaltime) {
446 /* before or on first sample, so just extend value */
447 min = prevfed->min;
448 max = prevfed->max;
449 }
450 else if (lastfed->time <= evaltime) {
451 /* after or on last sample, so just extend value */
452 min = lastfed->min;
453 max = lastfed->max;
454 }
455 else {
456 /* evaltime occurs somewhere between segments */
457 /* TODO: implement binary search for this to make it faster? */
458 for (a = 0; prevfed && fed && (a < env->totvert - 1); a++, prevfed = fed, fed++) {
459 /* evaltime occurs within the interval defined by these two envelope points */
460 if ((prevfed->time <= evaltime) && (fed->time >= evaltime)) {
461 float afac, bfac, diff;
462
463 diff = fed->time - prevfed->time;
464 afac = (evaltime - prevfed->time) / diff;
465 bfac = (fed->time - evaltime) / diff;
466
467 min = bfac * prevfed->min + afac * fed->min;
468 max = bfac * prevfed->max + afac * fed->max;
469
470 break;
471 }
472 }
473 }
474
475 /* adjust *cvalue
476 * - fac is the ratio of how the current y-value corresponds to the reference range
477 * - thus, the new value is found by mapping the old range to the new!
478 */
479 fac = (*cvalue - (env->midval + env->min)) / (env->max - env->min);
480 *cvalue = min + fac * (max - min);
481}
482
485 /*size*/ sizeof(FMod_Envelope),
486 /*acttype*/ FMI_TYPE_REPLACE_VALUES,
487 /*requires_flag*/ 0,
488 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_ACTION, "Envelope"),
489 /*struct_name*/ "FMod_Envelope",
490 /*storage_size*/ 0,
491 /*free_data*/ fcm_envelope_free,
492 /*copy_data*/ fcm_envelope_copy,
493 /*new_data*/ fcm_envelope_new_data,
494 /*verify_data*/ fcm_envelope_verify,
495 /*evaluate_modifier_time*/ nullptr,
496 /*evaluate_modifier*/ fcm_envelope_evaluate,
497};
498
499/* exported function for finding points */
500
501/* Binary search algorithm for finding where to insert Envelope Data Point.
502 * Returns the index to insert at (data already at that index will be offset if replace is 0)
503 */
504#define BINARYSEARCH_FRAMEEQ_THRESH 0.0001f
505
507 float frame,
508 int arraylen,
509 bool *r_exists)
510{
511 int start = 0, end = arraylen;
512 int loopbreaker = 0, maxloop = arraylen * 2;
513
514 /* initialize exists-flag first */
515 *r_exists = false;
516
517 /* sneaky optimizations (don't go through searching process if...):
518 * - keyframe to be added is to be added out of current bounds
519 * - keyframe to be added would replace one of the existing ones on bounds
520 */
521 if ((arraylen <= 0) || (array == nullptr)) {
522 CLOG_WARN(&LOG, "encountered invalid array");
523 return 0;
524 }
525
526 /* check whether to add before/after/on */
527 float framenum;
528
529 /* 'First' Point (when only one point, this case is used) */
530 framenum = array[0].time;
531 if (IS_EQT(frame, framenum, BINARYSEARCH_FRAMEEQ_THRESH)) {
532 *r_exists = true;
533 return 0;
534 }
535 if (frame < framenum) {
536 return 0;
537 }
538
539 /* 'Last' Point */
540 framenum = array[(arraylen - 1)].time;
541 if (IS_EQT(frame, framenum, BINARYSEARCH_FRAMEEQ_THRESH)) {
542 *r_exists = true;
543 return (arraylen - 1);
544 }
545 if (frame > framenum) {
546 return arraylen;
547 }
548
549 /* most of the time, this loop is just to find where to put it
550 * - 'loopbreaker' is just here to prevent infinite loops
551 */
552 for (loopbreaker = 0; (start <= end) && (loopbreaker < maxloop); loopbreaker++) {
553 /* compute and get midpoint */
554
555 /* we calculate the midpoint this way to avoid int overflows... */
556 int mid = start + ((end - start) / 2);
557
558 float midfra = array[mid].time;
559
560 /* check if exactly equal to midpoint */
561 if (IS_EQT(frame, midfra, BINARYSEARCH_FRAMEEQ_THRESH)) {
562 *r_exists = true;
563 return mid;
564 }
565
566 /* repeat in upper/lower half */
567 if (frame > midfra) {
568 start = mid + 1;
569 }
570 else if (frame < midfra) {
571 end = mid - 1;
572 }
573 }
574
575 /* print error if loop-limit exceeded */
576 if (loopbreaker == (maxloop - 1)) {
577 CLOG_ERROR(&LOG, "binary search was taking too long");
578
579 /* Include debug info. */
581 "\tround = %d: start = %d, end = %d, arraylen = %d",
582 loopbreaker,
583 start,
584 end,
585 arraylen);
586 }
587
588 /* not found, so return where to place it */
589 return start;
590}
591#undef BINARYSEARCH_FRAMEEQ_THRESH
592
593/* Cycles F-Curve Modifier --------------------------- */
594
595/* This modifier changes evaltime to something that exists within the curve's frame-range,
596 * then re-evaluates modifier stack up to this point using the new time. This re-entrant behavior
597 * is very likely to be more time-consuming than the original approach...
598 * (which was tightly integrated into the calculation code...).
599 *
600 * NOTE: this needs to be at the start of the stack to be of use,
601 * as it needs to know the extents of the keyframes/sample-data.
602 *
603 * Possible TODO: store length of cycle information that can be initialized from the extents of
604 * the keyframes/sample-data, and adjusted as appropriate.
605 */
606
607/* temp data used during evaluation */
609 float cycyofs; /* y-offset to apply */
610};
611
612static void fcm_cycles_new_data(void *mdata)
613{
614 FMod_Cycles *data = static_cast<FMod_Cycles *>(mdata);
615
616 /* turn on cycles by default */
617 data->before_mode = data->after_mode = FCM_EXTRAPOLATE_CYCLIC;
618}
619
620static float fcm_cycles_time(
621 const FCurve *fcu, const FModifier *fcm, float /*cvalue*/, float evaltime, void *storage_)
622{
623 const FMod_Cycles *data = static_cast<FMod_Cycles *>(fcm->data);
624 tFCMED_Cycles *storage = static_cast<tFCMED_Cycles *>(storage_);
625 float firstkey[2], lastkey[2], cycyofs = 0.0f;
626 short side = 0, mode = 0;
627 int cycles = 0;
628 float ofs = 0;
629
630 /* Initialize storage. */
631 storage->cycyofs = 0;
632
633 /* It shouldn't be possible for this modifier type to be anywhere other than
634 * the top of the stack. If it is, something's wrong. */
635 BLI_assert(fcm->prev == nullptr);
636
637 if (fcu == nullptr || (fcu->bezt == nullptr && fcu->fpt == nullptr)) {
638 return evaltime;
639 }
640
641 /* calculate new evaltime due to cyclic interpolation */
642 if (fcu->bezt) {
643 const BezTriple *firstbezt = &fcu->bezt[0];
644 const BezTriple *lastbezt = &fcu->bezt[fcu->totvert - 1];
645
646 firstkey[0] = firstbezt->vec[1][0];
647 firstkey[1] = firstbezt->vec[1][1];
648
649 lastkey[0] = lastbezt->vec[1][0];
650 lastkey[1] = lastbezt->vec[1][1];
651 }
652 else {
653 BLI_assert(fcu->fpt != nullptr);
654 const FPoint *prevfpt = fcu->fpt;
655 const FPoint *lastfpt = prevfpt + fcu->totvert - 1;
656
657 firstkey[0] = prevfpt->vec[0];
658 firstkey[1] = prevfpt->vec[1];
659
660 lastkey[0] = lastfpt->vec[0];
661 lastkey[1] = lastfpt->vec[1];
662 }
663
664 /* check if modifier will do anything
665 * 1) if in data range, definitely don't do anything
666 * 2) if before first frame or after last frame, make sure some cycling is in use
667 */
668 if (evaltime < firstkey[0]) {
669 if (data->before_mode) {
670 side = -1;
671 mode = data->before_mode;
672 cycles = data->before_cycles;
673 ofs = firstkey[0];
674 }
675 }
676 else if (evaltime > lastkey[0]) {
677 if (data->after_mode) {
678 side = 1;
679 mode = data->after_mode;
680 cycles = data->after_cycles;
681 ofs = lastkey[0];
682 }
683 }
684 if (ELEM(0, side, mode)) {
685 return evaltime;
686 }
687
688 /* find relative place within a cycle */
689 {
690 /* calculate period and amplitude (total height) of a cycle */
691 const float cycdx = lastkey[0] - firstkey[0];
692 const float cycdy = lastkey[1] - firstkey[1];
693
694 /* check if cycle is infinitely small, to be point of being impossible to use */
695 if (cycdx == 0) {
696 return evaltime;
697 }
698
699 /* Calculate the 'number' of the cycle. Needs to be a double to combat precision issues like
700 * #119360. With floats it can happen that the `cycle` jumps to the next full number, while
701 * `cyct` below is still behind. */
702 const double cycle = side * (double(evaltime) - double(ofs)) / double(cycdx);
703 /* calculate the time inside the cycle */
704 const float cyct = fmod(evaltime - ofs, cycdx);
705
706 /* check that cyclic is still enabled for the specified time */
707 if (cycles == 0) {
708 /* catch this case so that we don't exit when we have (cycles = 0)
709 * as this indicates infinite cycles...
710 */
711 }
712 else if (cycle > cycles) {
713 /* we are too far away from range to evaluate
714 * TODO: but we should still hold last value...
715 */
716 return evaltime;
717 }
718
719 /* check if 'cyclic extrapolation', and thus calculate y-offset for this cycle */
720 if (mode == FCM_EXTRAPOLATE_CYCLIC_OFFSET) {
721 if (side < 0) {
722 cycyofs = floor((evaltime - ofs) / cycdx);
723 }
724 else {
725 cycyofs = ceil((evaltime - ofs) / cycdx);
726 }
727 cycyofs *= cycdy;
728 }
729
730 /* special case for cycle start/end */
731 if (cyct == 0.0f) {
732 evaltime = (side == 1 ? lastkey[0] : firstkey[0]);
733
734 if ((mode == FCM_EXTRAPOLATE_MIRROR) && (int(cycle) % 2)) {
735 evaltime = (side == 1 ? firstkey[0] : lastkey[0]);
736 }
737 }
738 /* calculate where in the cycle we are (overwrite evaltime to reflect this) */
739 else if ((mode == FCM_EXTRAPOLATE_MIRROR) && (int(cycle + 1) % 2)) {
740 /* When 'mirror' option is used and cycle number is odd, this cycle is played in reverse
741 * - for 'before' extrapolation, we need to flip in a different way, otherwise values past
742 * then end of the curve get referenced
743 * (result of fmod will be negative, and with different phase).
744 */
745 if (side < 0) {
746 evaltime = firstkey[0] - cyct;
747 }
748 else {
749 evaltime = lastkey[0] - cyct;
750 }
751 }
752 else {
753 /* the cycle is played normally... */
754 evaltime = firstkey[0] + cyct;
755 }
756 if (evaltime < firstkey[0]) {
757 evaltime += cycdx;
758 }
759 }
760
761 /* store temp data if needed */
762 if (mode == FCM_EXTRAPOLATE_CYCLIC_OFFSET) {
763 storage->cycyofs = cycyofs;
764 }
765
766 /* return the new frame to evaluate */
767 return evaltime;
768}
769
770static void fcm_cycles_evaluate(const FCurve * /*fcu*/,
771 const FModifier * /*fcm*/,
772 float *cvalue,
773 float /*evaltime*/,
774 void *storage_)
775{
776 tFCMED_Cycles *storage = static_cast<tFCMED_Cycles *>(storage_);
777 *cvalue += storage->cycyofs;
778}
779
781 /*type*/ FMODIFIER_TYPE_CYCLES,
782 /*size*/ sizeof(FMod_Cycles),
783 /*acttype*/ FMI_TYPE_EXTRAPOLATION,
784 /*requires_flag*/ FMI_REQUIRES_ORIGINAL_DATA,
785 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_ACTION, "Cycles"),
786 /*struct_name*/ "FMod_Cycles",
787 /*storage_size*/ sizeof(tFCMED_Cycles),
788 /*free_data*/ nullptr,
789 /*copy_data*/ nullptr,
790 /*new_data*/ fcm_cycles_new_data,
791 /*verify_data*/ nullptr /*fcm_cycles_verify*/,
792 /*evaluate_modifier_time*/ fcm_cycles_time,
793 /*evaluate_modifier*/ fcm_cycles_evaluate,
794};
795
796/* Noise F-Curve Modifier --------------------------- */
797
798static void fcm_noise_new_data(void *mdata)
799{
800 FMod_Noise *data = static_cast<FMod_Noise *>(mdata);
801
802 /* defaults */
803 data->size = 1.0f;
804 data->strength = 1.0f;
805 data->phase = 1.0f;
806 data->offset = 0.0f;
807 data->depth = 0;
808 data->modification = FCM_NOISE_MODIF_REPLACE;
809 data->lacunarity = 2.0f;
810 data->roughness = 0.5f;
811 data->legacy_noise = 0;
812}
813
814static void fcm_noise_evaluate(const FCurve * /*fcu*/,
815 const FModifier *fcm,
816 float *cvalue,
817 float evaltime,
818 void * /*storage*/)
819{
820 FMod_Noise *data = static_cast<FMod_Noise *>(fcm->data);
821 float noise;
822 if (data->legacy_noise) {
823 /* Generate legacy noise. This is deprecated, see #123875.
824 * - 0.1 is passed as the 'z' value, otherwise evaluation fails for size = phase = 1
825 * with evaltime being an integer (which happens when evaluating on frame by frame basis)
826 */
828 data->size, evaltime - data->offset, data->phase, 0.1f, data->depth);
829 }
830 else {
831 float scale;
832 if (data->size == 0.0f) {
833 scale = 0.0;
834 }
835 else {
836 scale = 1.0 / data->size;
837 }
838 /* Adding an offset so the 0 positions are unlikely to be on full frames. The user can counter
839 * that, which is why the value is chosen to be quite obscure. */
840 const float offset = 0.61803398874;
841 /* Using float2 to generate a phase offset. Offsetting the evaltime by `offset` to ensure that
842 * the noise at full frames isn't always at 0. */
844 float2((evaltime - data->offset) * scale + offset, data->phase),
845 data->depth,
846 data->roughness,
847 data->lacunarity,
848 true);
849 }
850
851 /* combine the noise with existing motion data */
852 switch (data->modification) {
854 *cvalue = *cvalue + noise * data->strength;
855 break;
857 *cvalue = *cvalue - noise * data->strength;
858 break;
860 *cvalue = *cvalue * noise * data->strength;
861 break;
863 default:
864 *cvalue = *cvalue + (noise - 0.5f) * data->strength;
865 break;
866 }
867}
868
870 /*type*/ FMODIFIER_TYPE_NOISE,
871 /*size*/ sizeof(FMod_Noise),
872 /*acttype*/ FMI_TYPE_REPLACE_VALUES,
873 /*requires_flag*/ 0,
874 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_ACTION, "Noise"),
875 /*struct_name*/ "FMod_Noise",
876 /*storage_size*/ 0,
877 /*free_data*/ nullptr,
878 /*copy_data*/ nullptr,
879 /*new_data*/ fcm_noise_new_data,
880 /*verify_data*/ nullptr /*fcm_noise_verify*/,
881 /*evaluate_modifier_time*/ nullptr,
882 /*evaluate_modifier*/ fcm_noise_evaluate,
883};
884
885/* Limits F-Curve Modifier --------------------------- */
886
887static float fcm_limits_time(const FCurve * /*fcu*/,
888 const FModifier *fcm,
889 float /*cvalue*/,
890 float evaltime,
891 void * /*storage*/)
892{
893 FMod_Limits *data = static_cast<FMod_Limits *>(fcm->data);
894
895 /* check for the time limits */
896 if ((data->flag & FCM_LIMIT_XMIN) && (evaltime < data->rect.xmin)) {
897 return data->rect.xmin;
898 }
899 if ((data->flag & FCM_LIMIT_XMAX) && (evaltime > data->rect.xmax)) {
900 return data->rect.xmax;
901 }
902
903 /* modifier doesn't change time */
904 return evaltime;
905}
906
907static void fcm_limits_evaluate(const FCurve * /*fcu*/,
908 const FModifier *fcm,
909 float *cvalue,
910 float /*evaltime*/,
911 void * /*storage*/)
912{
913 FMod_Limits *data = static_cast<FMod_Limits *>(fcm->data);
914
915 /* value limits now */
916 if ((data->flag & FCM_LIMIT_YMIN) && (*cvalue < data->rect.ymin)) {
917 *cvalue = data->rect.ymin;
918 }
919 if ((data->flag & FCM_LIMIT_YMAX) && (*cvalue > data->rect.ymax)) {
920 *cvalue = data->rect.ymax;
921 }
922}
923
925 /*type*/ FMODIFIER_TYPE_LIMITS,
926 /*size*/ sizeof(FMod_Limits),
927 /*acttype*/ FMI_TYPE_GENERATE_CURVE,
928 /*requires_flag*/ FMI_REQUIRES_RUNTIME_CHECK, /* XXX... err... */
929 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_ACTION, "Limits"),
930 /*struct_name*/ "FMod_Limits",
931 /*storage_size*/ 0,
932 /*free_data*/ nullptr,
933 /*copy_data*/ nullptr,
934 /*new_data*/ nullptr,
935 /*verify_data*/ nullptr,
936 /*evaluate_modifier_time*/ fcm_limits_time,
937 /*evaluate_modifier*/ fcm_limits_evaluate,
938};
939
940/* Stepped F-Curve Modifier --------------------------- */
941
942static void fcm_stepped_new_data(void *mdata)
943{
944 FMod_Stepped *data = static_cast<FMod_Stepped *>(mdata);
945
946 /* just need to set the step-size to 2-frames by default */
947 /* XXX: or would 5 be more normal? */
948 data->step_size = 2.0f;
949}
950
951static float fcm_stepped_time(const FCurve * /*fcu*/,
952 const FModifier *fcm,
953 float /*cvalue*/,
954 float evaltime,
955 void * /*storage*/)
956{
957 FMod_Stepped *data = static_cast<FMod_Stepped *>(fcm->data);
958 int snapblock;
959
960 /* check range clamping to see if we should alter the timing to achieve the desired results */
961 if (data->flag & FCM_STEPPED_NO_BEFORE) {
962 if (evaltime < data->start_frame) {
963 return evaltime;
964 }
965 }
966 if (data->flag & FCM_STEPPED_NO_AFTER) {
967 if (evaltime > data->end_frame) {
968 return evaltime;
969 }
970 }
971
972 /* we snap to the start of the previous closest block of 'step_size' frames
973 * after the start offset has been discarded
974 * - i.e. round down
975 */
976 snapblock = int((evaltime - data->offset) / data->step_size);
977
978 /* reapply the offset, and multiple the snapblock by the size of the steps to get
979 * the new time to evaluate at
980 */
981 return (float(snapblock) * data->step_size) + data->offset;
982}
983
985 /*type*/ FMODIFIER_TYPE_STEPPED,
986 /*size*/ sizeof(FMod_Limits),
987 /*acttype*/ FMI_TYPE_GENERATE_CURVE,
988 /*requires_flag*/ FMI_REQUIRES_RUNTIME_CHECK, /* XXX... err... */
989 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_ACTION, "Stepped"),
990 /*struct_name*/ "FMod_Stepped",
991 /*storage_size*/ 0,
992 /*free_data*/ nullptr,
993 /*copy_data*/ nullptr,
994 /*new_data*/ fcm_stepped_new_data,
995 /*verify_data*/ nullptr,
996 /*evaluate_modifier_time*/ fcm_stepped_time,
997 /*evaluate_modifier*/ nullptr,
998};
999
1000/* Smooth F-Curve Modifier --------------------------- */
1001
1002static void fcm_smooth_new_data(void *mdata)
1003{
1004 FMod_Smooth *data = (FMod_Smooth *)mdata;
1005
1006 data->sigma = 0.33f;
1007 data->filter_width = 6;
1008}
1009
1011static float fcm_smooth_frame(const FCurve *fcu,
1012 const FModifier *fcm,
1013 const int evaltime,
1014 const float default_value)
1015{
1016 FMod_Smooth *data = (FMod_Smooth *)fcm->data;
1017
1018 const float sigma = data->sigma;
1019 const int filter_width = data->filter_width;
1020
1021 /* If filter_width is too small, the smoothing weight will become zero. */
1022 BLI_assert(filter_width >= 0.1);
1023
1024 /* Hold variables for weight, so we can compensate for the influence of the modifier. */
1025 float total_weighted_value = 0.0f;
1026 float total_weight = 0.0f;
1027
1028 /* Define sampling window around the frame using the filter width. */
1029 const int start_frame = floorf(evaltime - filter_width);
1030 const int end_frame = ceilf(evaltime + filter_width);
1031
1032 const float two_sigma_sq = 2.0f * sigma * sigma;
1033
1034 /* Sampling loop. */
1035 for (float sample_time = start_frame; sample_time <= end_frame; ++sample_time) {
1036 const float sample_distance = sample_time - evaltime;
1037
1038 /* Normalize sigma to filter width.
1039 * This makes it consistent with the behavior in GRAPH_OT_gaussian_smooth. */
1040 const float sample_dis_norm = sample_distance / filter_width;
1041 const float weight = expf(-(sample_dis_norm * sample_dis_norm) / two_sigma_sq);
1042
1043 const float sample_value = evaluate_fcurve_unmodified(fcu, sample_time);
1044
1045 total_weighted_value += sample_value * weight;
1046 total_weight += weight;
1047 }
1048
1049 if (total_weight <= 0.0f) {
1051 return default_value;
1052 }
1053
1054 return total_weighted_value / total_weight;
1055}
1056
1058 const FCurve *fcu, const FModifier *fcm, float *cvalue, float evaltime, void * /*storage*/)
1059{
1060 /* Check if evaltime is an integer, with FLT_EPSILON tolerance. */
1061 const bool is_integer_frame = (fabs(roundf(evaltime) - evaltime) <= FLT_EPSILON);
1062
1063 /* If the evaltime is an integer frame, we directly calculate the value. */
1064 if (is_integer_frame) {
1065 *cvalue = fcm_smooth_frame(fcu, fcm, evaltime, *cvalue);
1066 return;
1067 }
1068
1069 /* Otherwise, we linearly interpolate.
1070 * The Gaussian function requires knowing the distance from a sample to its neighboring frames.
1071 * However, F-Curve modifiers work as continuous functions, so we cannot access discrete keyframe
1072 * positions. Instead, we sample each integer frame, then linearly interpolate to find the value
1073 * at evaltime. This means that sub-frames won't contribute to the smoothing, but it is not
1074 * possible to know their positions.
1075 * The F-Curve is sampled using a fixed-size window of at least one frame, to prevent aliasing
1076 * that can occur when there is high frequency data (on sub-frames).
1077 */
1078 const float prev_frame = floorf(evaltime);
1079 const float next_frame = ceilf(evaltime);
1080
1081 float prev_value = evaluate_fcurve_unmodified(fcu, prev_frame);
1082 float next_value = evaluate_fcurve_unmodified(fcu, next_frame);
1083
1084 prev_value = fcm_smooth_frame(fcu, fcm, prev_frame, prev_value);
1085 next_value = fcm_smooth_frame(fcu, fcm, next_frame, prev_value);
1086
1087 *cvalue = interpf(next_value, prev_value, evaltime - prev_frame);
1088}
1089
1091 /*type*/ FMODIFIER_TYPE_SMOOTH,
1092 /*size*/ sizeof(FMod_Smooth),
1093 /*acttype*/ FMI_TYPE_REPLACE_VALUES,
1094 /*requires_flag*/ FMI_REQUIRES_ORIGINAL_DATA,
1095 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_ACTION, "Smooth"),
1096 /*struct_name*/ "FMod_Smooth",
1097 /*storage_size*/ 0,
1098 /*free_data*/ nullptr,
1099 /*copy_data*/ nullptr,
1100 /*new_data*/ fcm_smooth_new_data,
1101 /*verify_data*/ nullptr /*fcm_noise_verify*/,
1102 /*evaluate_modifier_time*/ nullptr,
1103 /*evaluate_modifier*/ fcm_smooth_evaluate,
1104};
1105
1107
1108/* -------------------------------------------------------------------- */
1114
1115/* These globals only ever get directly accessed in this file */
1117static short FMI_INIT = 1; /* when non-zero, the list needs to be updated */
1118
1121{
1133
1134#ifndef NDEBUG
1135 /* Check that the array indices are correct. */
1136 for (int i = 0; i < FMODIFIER_NUM_TYPES; i++) {
1137 if (!fmodifiersTypeInfo[i]) {
1138 continue;
1139 }
1140 BLI_assert_msg(i == fmodifiersTypeInfo[i]->type,
1141 "fmodifiersTypeInfo should be indexed by the modifier type number");
1142 }
1143#endif
1144}
1145
1147{
1148 /* initialize the type-info list? */
1149 if (FMI_INIT) {
1151 FMI_INIT = 0;
1152 }
1153
1154 /* only return for valid types */
1155 if ((type >= FMODIFIER_TYPE_NULL) && (type < FMODIFIER_NUM_TYPES)) {
1156 /* there shouldn't be any segfaults here... */
1157 return fmodifiersTypeInfo[type];
1158 }
1159
1160 CLOG_ERROR(&LOG, "No valid F-Curve Modifier type-info data available. Type = %i", type);
1161
1162 return nullptr;
1163}
1164
1166{
1167 /* only return typeinfo for valid modifiers */
1168 if (fcm) {
1169 return get_fmodifier_typeinfo(fcm->type);
1170 }
1171
1172 return nullptr;
1173}
1174
1176
1177/* -------------------------------------------------------------------- */
1180
1181FModifier *add_fmodifier(ListBaseT<FModifier> *modifiers, int type, FCurve *owner_fcu)
1182{
1183 const FModifierTypeInfo *fmi = get_fmodifier_typeinfo(type);
1184 FModifier *fcm;
1185
1186 /* sanity checks */
1187 if (ELEM(nullptr, modifiers, fmi)) {
1188 return nullptr;
1189 }
1190
1191 /* add modifier itself */
1192 fcm = MEM_new<FModifier>("F-Curve Modifier");
1193 fcm->type = eFModifier_Types(type);
1194 fcm->ui_expand_flag = UI_PANEL_DATA_EXPAND_ROOT; /* Expand the main panel, not the sub-panels. */
1195 fcm->curve = owner_fcu;
1196 fcm->influence = 1.0f;
1197 BLI_addtail(modifiers, fcm);
1198
1199 BKE_fmodifier_ensure_flag(modifiers);
1200
1201 /* Set modifier name and make sure it is unique. */
1202 BKE_fmodifier_name_set(fcm, "");
1203
1204 /* tag modifier as "active" if no other modifiers exist in the stack yet */
1205 if (modifiers->is_single()) {
1207 }
1208
1209 /* add modifier's data */
1210 fcm->data = MEM_new_zeroed(fmi->size, fmi->struct_name);
1211
1212 /* init custom settings if necessary */
1213 if (fmi->new_data) {
1214 fmi->new_data(fcm->data);
1215 }
1216
1217 /* update the fcurve if the Cycles modifier is added */
1218 if ((owner_fcu) && (type == FMODIFIER_TYPE_CYCLES)) {
1219 BKE_fcurve_handles_recalc(*owner_fcu);
1220 }
1221
1222 /* return modifier for further editing */
1223 return fcm;
1224}
1225
1227{
1228 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(src);
1229 FModifier *dst;
1230
1231 /* sanity check */
1232 if (src == nullptr) {
1233 return nullptr;
1234 }
1235
1236 /* copy the base data, clearing the links */
1237 dst = MEM_dupalloc(src);
1238 dst->next = dst->prev = nullptr;
1239 dst->curve = nullptr;
1240
1241 /* make a new copy of the F-Modifier's data */
1242 dst->data = MEM_dupalloc_void(src->data);
1243
1244 /* only do specific constraints if required */
1245 if (fmi && fmi->copy_data) {
1246 fmi->copy_data(dst, src);
1247 }
1248
1249 /* return the new modifier */
1250 return dst;
1251}
1252
1253void copy_fmodifiers(ListBaseT<FModifier> *dst, const ListBaseT<FModifier> *src)
1254{
1255 FModifier *fcm, *srcfcm;
1256
1257 if (ELEM(nullptr, dst, src)) {
1258 return;
1259 }
1260
1261 dst->clear_no_delete();
1262 BLI_duplicatelist(dst, src);
1263
1264 for (fcm = dst->first(), srcfcm = src->first(); fcm && srcfcm;
1265 srcfcm = srcfcm->next, fcm = fcm->next)
1266 {
1267 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(fcm);
1268
1269 /* make a new copy of the F-Modifier's data */
1270 fcm->data = MEM_dupalloc_void(fcm->data);
1271 fcm->curve = nullptr;
1272
1273 /* only do specific constraints if required */
1274 if (fmi && fmi->copy_data) {
1275 fmi->copy_data(fcm, srcfcm);
1276 }
1277 }
1278}
1279
1280bool remove_fmodifier(ListBaseT<FModifier> *modifiers, FModifier *fcm)
1281{
1282 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(fcm);
1283
1284 /* sanity check */
1285 if (fcm == nullptr) {
1286 return false;
1287 }
1288
1289 /* removing the cycles modifier requires a handle update */
1290 FCurve *update_fcu = (fcm->type == FMODIFIER_TYPE_CYCLES) ? fcm->curve : nullptr;
1291
1292 /* free modifier's special data (stored inside fcm->data) */
1293 if (fcm->data) {
1294 if (fmi && fmi->free_data) {
1295 fmi->free_data(fcm);
1296 }
1297
1298 /* free modifier's data (fcm->data) */
1299 MEM_delete_void(fcm->data);
1300 }
1301
1302 /* remove modifier from stack */
1303 if (modifiers) {
1304 BLI_freelinkN(modifiers, fcm);
1305
1306 /* update the fcurve if the Cycles modifier is removed */
1307 if (update_fcu) {
1308 BKE_fcurve_handles_recalc(*update_fcu);
1309 }
1310
1311 BKE_fmodifier_ensure_flag(modifiers);
1312 return true;
1313 }
1314
1315 /* XXX this case can probably be removed some day, as it shouldn't happen... */
1316 CLOG_STR_ERROR(&LOG, "no modifier stack given");
1317 MEM_delete(fcm);
1318 return false;
1319}
1320
1321void free_fmodifiers(ListBaseT<FModifier> *modifiers)
1322{
1323 FModifier *fcm, *fmn;
1324
1325 /* sanity check */
1326 if (modifiers == nullptr) {
1327 return;
1328 }
1329
1330 /* free each modifier in order - modifier is unlinked from list and freed */
1331 for (fcm = modifiers->first(); fcm; fcm = fmn) {
1332 fmn = fcm->next;
1333 remove_fmodifier(modifiers, fcm);
1334 }
1335}
1336
1337FModifier *find_active_fmodifier(ListBaseT<FModifier> *modifiers)
1338{
1339 /* sanity checks */
1340 if (ELEM(nullptr, modifiers, modifiers->first())) {
1341 return nullptr;
1342 }
1343
1344 /* loop over modifiers until 'active' one is found */
1345 for (FModifier &fcm : *modifiers) {
1346 if (fcm.flag & FMODIFIER_FLAG_ACTIVE) {
1347 return &fcm;
1348 }
1349 }
1350
1351 /* no modifier is active */
1352 return nullptr;
1353}
1354
1355void set_active_fmodifier(ListBaseT<FModifier> *modifiers, FModifier *fcm)
1356{
1357 /* sanity checks */
1358 if (ELEM(nullptr, modifiers, modifiers->first())) {
1359 return;
1360 }
1361
1362 /* deactivate all, and set current one active */
1363 for (FModifier &fm : *modifiers) {
1364 fm.flag &= ~FMODIFIER_FLAG_ACTIVE;
1365 }
1366
1367 /* make given modifier active */
1368 if (fcm) {
1370 }
1371}
1372
1373bool list_has_suitable_fmodifier(const ListBaseT<FModifier> *modifiers, int mtype, short acttype)
1374{
1375 /* if there are no specific filtering criteria, just skip */
1376 if ((mtype == 0) && (acttype == 0)) {
1377 return (modifiers && modifiers->first());
1378 }
1379
1380 /* sanity checks */
1381 if (ELEM(nullptr, modifiers, modifiers->first())) {
1382 return false;
1383 }
1384
1385 /* Find the first modifier fitting these criteria. */
1386 for (FModifier &fcm : *modifiers) {
1387 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(&fcm);
1388 short mOk = 1, aOk = 1; /* by default 1, so that when only one test, won't fail */
1389
1390 /* check if applicable ones are fulfilled */
1391 if (mtype) {
1392 mOk = (fcm.type == mtype);
1393 }
1394 if (acttype > -1) {
1395 aOk = (fmi->acttype == acttype);
1396 }
1397
1398 /* if both are ok, we've found a hit */
1399 if (mOk && aOk) {
1400 return true;
1401 }
1402 }
1403
1404 /* no matches */
1405 return false;
1406}
1407
1408/* Evaluation API --------------------------- */
1409
1410uint evaluate_fmodifiers_storage_size_per_modifier(const ListBaseT<FModifier> *modifiers)
1411{
1412 /* Sanity checks. */
1413 if (ELEM(nullptr, modifiers, modifiers->first())) {
1414 return 0;
1415 }
1416
1417 uint max_size = 0;
1418
1419 for (FModifier &fcm : *modifiers) {
1420 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(&fcm);
1421
1422 if (fmi == nullptr) {
1423 continue;
1424 }
1425
1426 max_size = std::max(max_size, fmi->storage_size);
1427 }
1428
1429 return max_size;
1430}
1431
1435static float eval_fmodifier_influence(FModifier *fcm, float evaltime)
1436{
1437 float influence;
1438
1439 /* sanity check */
1440 if (fcm == nullptr) {
1441 return 0.0f;
1442 }
1443
1444 /* should we use influence stored in modifier or not
1445 * NOTE: this is really just a hack so that we don't need to version patch old files ;)
1446 */
1447 if (fcm->flag & FMODIFIER_FLAG_USEINFLUENCE) {
1448 influence = fcm->influence;
1449 }
1450 else {
1451 influence = 1.0f;
1452 }
1453
1454 /* restricted range or full range? */
1456 if ((evaltime < fcm->sfra) || (evaltime > fcm->efra)) {
1457 /* out of range */
1458 return 0.0f;
1459 }
1460 if ((fcm->blendin != 0.0f) && (evaltime >= fcm->sfra) &&
1461 (evaltime <= fcm->sfra + fcm->blendin))
1462 {
1463 /* blend in range */
1464 float a = fcm->sfra;
1465 float b = fcm->sfra + fcm->blendin;
1466 return influence * (evaltime - a) / (b - a);
1467 }
1468 if ((fcm->blendout != 0.0f) && (evaltime <= fcm->efra) &&
1469 (evaltime >= fcm->efra - fcm->blendout))
1470 {
1471 /* blend out range */
1472 float a = fcm->efra;
1473 float b = fcm->efra - fcm->blendout;
1474 return influence * (evaltime - a) / (b - a);
1475 }
1476 }
1477
1478 /* just return the influence of the modifier */
1479 return influence;
1480}
1481
1483 const ListBaseT<FModifier> *modifiers,
1484 const FCurve *fcu,
1485 float cvalue,
1486 float evaltime)
1487{
1488 /* sanity checks */
1489 if (ELEM(nullptr, modifiers, modifiers->last())) {
1490 return evaltime;
1491 }
1492
1493 if (fcu && fcu->flag & FCURVE_MOD_OFF) {
1494 return evaltime;
1495 }
1496
1497 /* Starting from the end of the stack, calculate the time effects of various stacked modifiers
1498 * on the time the F-Curve should be evaluated at.
1499 *
1500 * This is done in reverse order to standard evaluation, as when this is done in standard
1501 * order, each modifier would cause jumps to other points in the curve, forcing all
1502 * previous ones to be evaluated again for them to be correct. However, if we did in the
1503 * reverse order as we have here, we can consider them a macro to micro type of waterfall
1504 * effect, which should get us the desired effects when using layered time manipulations
1505 * (such as multiple 'stepped' modifiers in sequence, causing different stepping rates)
1506 */
1507 uint fcm_index = storage->modifier_count - 1;
1508 for (FModifier *fcm = modifiers->last(); fcm; fcm = fcm->prev, fcm_index--) {
1509 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(fcm);
1510
1511 if (fmi == nullptr) {
1512 continue;
1513 }
1514
1515 /* If modifier cannot be applied on this frame
1516 * (whatever scale it is on, it won't affect the results)
1517 * hence we shouldn't bother seeing what it would do given the chance. */
1518 if ((fcm->flag & FMODIFIER_FLAG_RANGERESTRICT) == 0 ||
1519 ((fcm->sfra <= evaltime) && (fcm->efra >= evaltime)))
1520 {
1521 /* only evaluate if there's a callback for this */
1522 if (fmi->evaluate_modifier_time) {
1523 if ((fcm->flag & (FMODIFIER_FLAG_DISABLED | FMODIFIER_FLAG_MUTED)) == 0) {
1524 void *storage_ptr = POINTER_OFFSET(storage->buffer,
1525 fcm_index * storage->size_per_modifier);
1526
1527 float nval = fmi->evaluate_modifier_time(fcu, fcm, cvalue, evaltime, storage_ptr);
1528
1529 float influence = eval_fmodifier_influence(fcm, evaltime);
1530 evaltime = interpf(nval, evaltime, influence);
1531 }
1532 }
1533 }
1534 }
1535
1536 /* return the modified evaltime */
1537 return evaltime;
1538}
1539
1541 const ListBaseT<FModifier> *modifiers,
1542 const FCurve *fcu,
1543 float *cvalue,
1544 float evaltime)
1545{
1546 FModifier *fcm;
1547
1548 /* sanity checks */
1549 if (ELEM(nullptr, modifiers, modifiers->first())) {
1550 return;
1551 }
1552
1553 if (fcu->flag & FCURVE_MOD_OFF) {
1554 return;
1555 }
1556
1557 /* evaluate modifiers */
1558 uint fcm_index = 0;
1559 for (fcm = modifiers->first(); fcm; fcm = fcm->next, fcm_index++) {
1560 const FModifierTypeInfo *fmi = fmodifier_get_typeinfo(fcm);
1561
1562 if (fmi == nullptr) {
1563 continue;
1564 }
1565
1566 /* Only evaluate if there's a callback for this,
1567 * and if F-Modifier can be evaluated on this frame. */
1568 if ((fcm->flag & FMODIFIER_FLAG_RANGERESTRICT) == 0 ||
1569 ((fcm->sfra <= evaltime) && (fcm->efra >= evaltime)))
1570 {
1571 if (fmi->evaluate_modifier) {
1572 if ((fcm->flag & (FMODIFIER_FLAG_DISABLED | FMODIFIER_FLAG_MUTED)) == 0) {
1573 void *storage_ptr = POINTER_OFFSET(storage->buffer,
1574 fcm_index * storage->size_per_modifier);
1575
1576 float nval = *cvalue;
1577 fmi->evaluate_modifier(fcu, fcm, &nval, evaltime, storage_ptr);
1578
1579 float influence = eval_fmodifier_influence(fcm, evaltime);
1580 *cvalue = interpf(nval, *cvalue, influence);
1581 }
1582 }
1583 }
1584 }
1585}
1586
1587/* ---------- */
1588
1589void fcurve_bake_modifiers(FCurve *fcu, int start, int end)
1590{
1591 ChannelDriver *driver;
1592
1593 /* sanity checks */
1594 /* TODO: make these tests report errors using reports not CLOG's */
1595 if (ELEM(nullptr, fcu, fcu->modifiers.first())) {
1596 CLOG_ERROR(&LOG, "No F-Curve with F-Curve Modifiers to Bake");
1597 return;
1598 }
1599
1600 /* temporarily, disable driver while we sample, so that they don't influence the outcome */
1601 driver = fcu->driver;
1602 fcu->driver = nullptr;
1603
1604 /* bake the modifiers, by sampling the curve at each frame */
1605 fcurve_store_samples(fcu, nullptr, start, end, fcurve_samplingcb_evalcurve);
1606
1607 /* free the modifiers now */
1609
1610 /* restore driver */
1611 fcu->driver = driver;
1612}
1613
1615
1616} // namespace blender
#define M_PI_2
#define M_PI
#define BLT_I18NCONTEXT_ID_ACTION
#define CTX_N_(context, msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_STR_ERROR(clg_ref, str)
Definition CLG_log.h:196
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define IS_EQ(a, b)
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
#define BLI_assert_msg(a, msg)
#define IS_EQT(a, b, c)
#define MEM_realloc_zeroed(vmemh, len)
btScalar max
#define expf(x)
#define roundf(x)
#define BINARYSEARCH_FRAMEEQ_THRESH
Definition fmodifier.cc:504
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define FLT_EPSILON
#define storage(slot, qualifiers)
#define tan
#define log
#define sin
#define cos
#define floor
#define ceil
float3x3 scale(float3x3 mat, float2 scale)
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
ccl_device_inline float2 fmod(const float2 a, const float b)
template float perlin_fbm< float2 >(float2 p, const float detail, const float roughness, const float lacunarity, const bool normalize)
BMesh Iterators.
static void fcm_fn_generator_evaluate(const FCurve *, const FModifier *fcm, float *cvalue, float evaltime, void *)
Definition fmodifier.cc:284
static void fmods_init_typeinfo()
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ UI_PANEL_DATA_EXPAND_ROOT
@ FMI_TYPE_GENERATE_CURVE
@ FMI_TYPE_EXTRAPOLATION
@ FMI_TYPE_REPLACE_VALUES
unsigned int uint
Definition BKE_text.h:55
float BLI_noise_turbulence(float noisesize, float x, float y, float z, int nr)
Definition noise_c.cc:439
static void fcm_generator_verify(FModifier *fcm)
Definition fmodifier.cc:126
static FModifierTypeInfo FMI_SMOOTH
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
@ FCM_GENERATOR_POLYNOMIAL
@ FCM_GENERATOR_POLYNOMIAL_FACTORISED
uint evaluate_fmodifiers_storage_size_per_modifier(const ListBaseT< FModifier > *modifiers)
void BKE_fmodifier_ensure_flag(ListBaseT< FModifier > *modifiers)
void BKE_fmodifier_name_set(FModifier *fcm, const char *name)
static FModifierTypeInfo * fmodifiersTypeInfo[FMODIFIER_NUM_TYPES]
void copy_fmodifiers(ListBaseT< FModifier > *dst, const ListBaseT< FModifier > *src)
static FModifierTypeInfo FMI_ENVELOPE
Definition fmodifier.cc:483
static void fcm_smooth_evaluate(const FCurve *fcu, const FModifier *fcm, float *cvalue, float evaltime, void *)
const FModifierTypeInfo * fmodifier_get_typeinfo(const FModifier *fcm)
static void fcm_stepped_new_data(void *mdata)
Definition fmodifier.cc:942
static void fcm_envelope_copy(FModifier *fcm, const FModifier *src)
Definition fmodifier.cc:395
static float fcm_stepped_time(const FCurve *, const FModifier *fcm, float, float evaltime, void *)
Definition fmodifier.cc:951
FModifier * add_fmodifier(ListBaseT< FModifier > *modifiers, int type, FCurve *owner_fcu)
float evaluate_fcurve_unmodified(const FCurve *fcu, float evaltime)
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool list_has_suitable_fmodifier(const ListBaseT< FModifier > *modifiers, int mtype, short acttype)
void MEM_delete_void(void *vmemh)
bool remove_fmodifier(ListBaseT< FModifier > *modifiers, FModifier *fcm)
@ FCM_NOISE_MODIF_REPLACE
@ FCM_NOISE_MODIF_SUBTRACT
@ FCM_NOISE_MODIF_ADD
@ FCM_NOISE_MODIF_MULTIPLY
@ FMODIFIER_TYPE_ENVELOPE
@ FMODIFIER_TYPE_NULL
@ FMODIFIER_TYPE_STEPPED
@ FMODIFIER_TYPE_FN_GENERATOR
@ FMODIFIER_TYPE_LIMITS
@ FMODIFIER_TYPE_CYCLES
@ FMODIFIER_TYPE_FILTER
@ FMODIFIER_TYPE_SMOOTH
@ FMODIFIER_TYPE_GENERATOR
@ FMODIFIER_TYPE_NOISE
@ FMODIFIER_TYPE_PYTHON
@ FMODIFIER_NUM_TYPES
@ FCM_EXTRAPOLATE_MIRROR
@ FCM_EXTRAPOLATE_CYCLIC_OFFSET
@ FCM_EXTRAPOLATE_CYCLIC
static void fcm_generator_new_data(void *mdata)
Definition fmodifier.cc:113
void * MEM_dupalloc_void(const void *vmemh) ATTR_WARN_UNUSED_RESULT
VecBase< float, 2 > float2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void evaluate_value_fmodifiers(FModifiersStackStorage *storage, const ListBaseT< FModifier > *modifiers, const FCurve *fcu, float *cvalue, float evaltime)
static void fcm_cycles_evaluate(const FCurve *, const FModifier *, float *cvalue, float, void *storage_)
Definition fmodifier.cc:770
void BKE_fcurve_handles_recalc(FCurve &fcu)
static short FMI_INIT
BMesh const char void * data
static void fcm_generator_free(FModifier *fcm)
Definition fmodifier.cc:92
@ FCM_STEPPED_NO_AFTER
@ FCM_STEPPED_NO_BEFORE
static void fcm_generator_copy(FModifier *fcm, const FModifier *src)
Definition fmodifier.cc:102
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
static FModifierTypeInfo FMI_STEPPED
Definition fmodifier.cc:984
sqrt(x)+1/max(0
FModifier * find_active_fmodifier(ListBaseT< FModifier > *modifiers)
static float eval_fmodifier_influence(FModifier *fcm, float evaltime)
@ FCM_GENERATOR_FN_SIN
@ FCM_GENERATOR_FN_SINC
@ FCM_GENERATOR_FN_LN
@ FCM_GENERATOR_FN_TAN
@ FCM_GENERATOR_FN_COS
@ FCM_GENERATOR_FN_SQRT
static void fcm_noise_new_data(void *mdata)
Definition fmodifier.cc:798
static double sinc(double x)
Definition fmodifier.cc:275
void free_fmodifiers(ListBaseT< FModifier > *modifiers)
static void fcm_noise_evaluate(const FCurve *, const FModifier *fcm, float *cvalue, float evaltime, void *)
Definition fmodifier.cc:814
static FModifierTypeInfo FMI_CYCLES
Definition fmodifier.cc:780
static float fcm_limits_time(const FCurve *, const FModifier *fcm, float, float evaltime, void *)
Definition fmodifier.cc:887
static void fcm_cycles_new_data(void *mdata)
Definition fmodifier.cc:612
static void fcm_envelope_free(FModifier *fcm)
Definition fmodifier.cc:385
static void fcm_limits_evaluate(const FCurve *, const FModifier *fcm, float *cvalue, float, void *)
Definition fmodifier.cc:907
static void fcm_envelope_new_data(void *mdata)
Definition fmodifier.cc:406
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void fcurve_bake_modifiers(FCurve *fcu, int start, int end)
static void fcm_envelope_evaluate(const FCurve *, const FModifier *fcm, float *cvalue, float evaltime, void *)
Definition fmodifier.cc:425
static void fcm_generator_evaluate(const FCurve *, const FModifier *fcm, float *cvalue, float evaltime, void *)
Definition fmodifier.cc:157
FModifier * copy_fmodifier(const FModifier *src)
int BKE_fcm_envelope_find_index(FCM_EnvelopeData *array, float frame, int arraylen, bool *r_exists)
static void fcm_envelope_verify(FModifier *fcm)
Definition fmodifier.cc:415
static float fcm_smooth_frame(const FCurve *fcu, const FModifier *fcm, const int evaltime, const float default_value)
static CLG_LogRef LOG
Definition logging.cpp:12
MINLINE float interpf(float target, float origin, float t)
@ FCM_GENERATOR_ADDITIVE
static void fcm_fn_generator_new_data(void *mdata)
Definition fmodifier.cc:262
void fcurve_store_samples(FCurve *fcu, void *data, int start, int end, FcuSampleFunc sample_cb)
float fcurve_samplingcb_evalcurve(FCurve *fcu, void *data, float evaltime)
static void fcm_smooth_new_data(void *mdata)
static FModifierTypeInfo FMI_NOISE
Definition fmodifier.cc:869
static FModifierTypeInfo FMI_FN_GENERATOR
Definition fmodifier.cc:367
static FModifierTypeInfo FMI_LIMITS
Definition fmodifier.cc:924
void set_active_fmodifier(ListBaseT< FModifier > *modifiers, FModifier *fcm)
@ FMI_REQUIRES_ORIGINAL_DATA
@ FMI_REQUIRES_NOTHING
@ FMI_REQUIRES_RUNTIME_CHECK
const FModifierTypeInfo * get_fmodifier_typeinfo(int type)
static FModifierTypeInfo FMI_GENERATOR
Definition fmodifier.cc:234
float evaluate_time_fmodifiers(FModifiersStackStorage *storage, const ListBaseT< FModifier > *modifiers, const FCurve *fcu, float cvalue, float evaltime)
static float fcm_cycles_time(const FCurve *fcu, const FModifier *fcm, float, float evaltime, void *storage_)
Definition fmodifier.cc:620
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ FMODIFIER_FLAG_MUTED
@ FMODIFIER_FLAG_DISABLED
@ FMODIFIER_FLAG_RANGERESTRICT
@ FMODIFIER_FLAG_USEINFLUENCE
@ FMODIFIER_FLAG_ACTIVE
#define floorf
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
eFCurve_Flags flag
ListBaseT< FModifier > modifiers
ChannelDriver * driver
unsigned int totvert
FCM_EnvelopeData * data
void(*) free_data(FModifier *fcm)
Definition BKE_fcurve.hh:85
float(*) evaluate_modifier_time(const FCurve *fcu, const FModifier *fcm, float cvalue, float evaltime, void *storage)
Definition BKE_fcurve.hh:98
void(*) evaluate_modifier(const FCurve *fcu, const FModifier *fcm, float *cvalue, float evaltime, void *storage)
void(*) new_data(void *mdata)
Definition BKE_fcurve.hh:92
void(*) copy_data(FModifier *fcm, const FModifier *src)
Definition BKE_fcurve.hh:87
struct FModifier * prev
eFModifier_Flags flag
struct FModifier * next
eFModifier_Types type
struct FCurve * curve