Cleanup: Animation, split FCurve interpolation into separate function

This commit is contained in:
Sybren A. Stüvel
2020-05-01 15:49:59 +02:00
parent 9c2c697011
commit 5b6ee80351

View File

@@ -1462,11 +1462,10 @@ static float fcurve_eval_keyframes_extrapolate(
return endpoint_bezt->vec[1][1] - (fac * dx); return endpoint_bezt->vec[1][1] - (fac * dx);
} }
/* Calculate F-Curve value for 'evaltime' using BezTriple keyframes */ static float fcurve_eval_keyframes_interpolate(FCurve *fcu, BezTriple *bezts, float evaltime)
static float fcurve_eval_keyframes(FCurve *fcu, BezTriple *bezts, float evaltime)
{ {
const float eps = 1.e-8f; const float eps = 1.e-8f;
BezTriple *bezt, *prevbezt, *lastbezt; BezTriple *bezt, *prevbezt;
float v1[2], v2[2], v3[2], v4[2], opl[32]; float v1[2], v2[2], v3[2], v4[2], opl[32];
unsigned int a; unsigned int a;
int b; int b;
@@ -1476,329 +1475,332 @@ static float fcurve_eval_keyframes(FCurve *fcu, BezTriple *bezts, float evaltime
a = fcu->totvert - 1; a = fcu->totvert - 1;
prevbezt = bezts; prevbezt = bezts;
bezt = prevbezt + 1; bezt = prevbezt + 1;
lastbezt = prevbezt + a;
/* evaluation time at or past endpoints? */ /* evaltime occurs somewhere in the middle of the curve */
if (prevbezt->vec[1][0] >= evaltime) { bool exact = false;
cvalue = fcurve_eval_keyframes_extrapolate(fcu, bezts, evaltime, 0, +1);
} /* Use binary search to find appropriate keyframes...
else if (lastbezt->vec[1][0] <= evaltime) { *
cvalue = fcurve_eval_keyframes_extrapolate(fcu, bezts, evaltime, fcu->totvert - 1, -1); * The threshold here has the following constraints:
* - 0.001 is too coarse:
* We get artifacts with 2cm driver movements at 1BU = 1m (see T40332)
*
* - 0.00001 is too fine:
* Weird errors, like selecting the wrong keyframe range (see T39207), occur.
* This lower bound was established in b888a32eee8147b028464336ad2404d8155c64dd.
*/
a = binarysearch_bezt_index_ex(bezts, evaltime, fcu->totvert, 0.0001, &exact);
if (exact) {
/* index returned must be interpreted differently when it sits on top of an existing keyframe
* - that keyframe is the start of the segment we need (see action_bug_2.blend in T39207)
*/
prevbezt = bezts + a;
bezt = (a < fcu->totvert - 1) ? (prevbezt + 1) : prevbezt;
} }
else { else {
/* evaltime occurs somewhere in the middle of the curve */ /* index returned refers to the keyframe that the eval-time occurs *before*
bool exact = false; * - hence, that keyframe marks the start of the segment we're dealing with
/* Use binary search to find appropriate keyframes...
*
* The threshold here has the following constraints:
* - 0.001 is too coarse:
* We get artifacts with 2cm driver movements at 1BU = 1m (see T40332)
*
* - 0.00001 is too fine:
* Weird errors, like selecting the wrong keyframe range (see T39207), occur.
* This lower bound was established in b888a32eee8147b028464336ad2404d8155c64dd.
*/ */
a = binarysearch_bezt_index_ex(bezts, evaltime, fcu->totvert, 0.0001, &exact); bezt = bezts + a;
prevbezt = (a > 0) ? (bezt - 1) : bezt;
}
if (exact) { /* use if the key is directly on the frame,
/* index returned must be interpreted differently when it sits on top of an existing keyframe * rare cases this is needed else we get 0.0 instead. */
* - that keyframe is the start of the segment we need (see action_bug_2.blend in T39207) /* XXX: consult T39207 for examples of files where failure of these checks can cause issues */
*/ if (exact) {
prevbezt = bezts + a; cvalue = prevbezt->vec[1][1];
bezt = (a < fcu->totvert - 1) ? (prevbezt + 1) : prevbezt; }
} else if (fabsf(bezt->vec[1][0] - evaltime) < eps) {
else { cvalue = bezt->vec[1][1];
/* index returned refers to the keyframe that the eval-time occurs *before* }
* - hence, that keyframe marks the start of the segment we're dealing with /* evaltime occurs within the interval defined by these two keyframes */
*/ else if ((prevbezt->vec[1][0] <= evaltime) && (bezt->vec[1][0] >= evaltime)) {
bezt = bezts + a; const float begin = prevbezt->vec[1][1];
prevbezt = (a > 0) ? (bezt - 1) : bezt; const float change = bezt->vec[1][1] - prevbezt->vec[1][1];
} const float duration = bezt->vec[1][0] - prevbezt->vec[1][0];
const float time = evaltime - prevbezt->vec[1][0];
const float amplitude = prevbezt->amplitude;
const float period = prevbezt->period;
/* use if the key is directly on the frame, /* value depends on interpolation mode */
* rare cases this is needed else we get 0.0 instead. */ if ((prevbezt->ipo == BEZT_IPO_CONST) || (fcu->flag & FCURVE_DISCRETE_VALUES) ||
/* XXX: consult T39207 for examples of files where failure of these checks can cause issues */ (duration == 0)) {
if (exact) { /* constant (evaltime not relevant, so no interpolation needed) */
cvalue = prevbezt->vec[1][1]; cvalue = prevbezt->vec[1][1];
} }
else if (fabsf(bezt->vec[1][0] - evaltime) < eps) { else {
cvalue = bezt->vec[1][1]; switch (prevbezt->ipo) {
} /* interpolation ...................................... */
/* evaltime occurs within the interval defined by these two keyframes */ case BEZT_IPO_BEZ:
else if ((prevbezt->vec[1][0] <= evaltime) && (bezt->vec[1][0] >= evaltime)) { /* bezier interpolation */
const float begin = prevbezt->vec[1][1]; /* (v1, v2) are the first keyframe and its 2nd handle */
const float change = bezt->vec[1][1] - prevbezt->vec[1][1]; v1[0] = prevbezt->vec[1][0];
const float duration = bezt->vec[1][0] - prevbezt->vec[1][0]; v1[1] = prevbezt->vec[1][1];
const float time = evaltime - prevbezt->vec[1][0]; v2[0] = prevbezt->vec[2][0];
const float amplitude = prevbezt->amplitude; v2[1] = prevbezt->vec[2][1];
const float period = prevbezt->period; /* (v3, v4) are the last keyframe's 1st handle + the last keyframe */
v3[0] = bezt->vec[0][0];
v3[1] = bezt->vec[0][1];
v4[0] = bezt->vec[1][0];
v4[1] = bezt->vec[1][1];
/* value depends on interpolation mode */ if (fabsf(v1[1] - v4[1]) < FLT_EPSILON && fabsf(v2[1] - v3[1]) < FLT_EPSILON &&
if ((prevbezt->ipo == BEZT_IPO_CONST) || (fcu->flag & FCURVE_DISCRETE_VALUES) || fabsf(v3[1] - v4[1]) < FLT_EPSILON) {
(duration == 0)) { /* Optimization: If all the handles are flat/at the same values,
/* constant (evaltime not relevant, so no interpolation needed) */ * the value is simply the shared value (see T40372 -> F91346)
cvalue = prevbezt->vec[1][1]; */
} cvalue = v1[1];
else { }
switch (prevbezt->ipo) { else {
/* interpolation ...................................... */ /* adjust handles so that they don't overlap (forming a loop) */
case BEZT_IPO_BEZ: correct_bezpart(v1, v2, v3, v4);
/* bezier interpolation */
/* (v1, v2) are the first keyframe and its 2nd handle */
v1[0] = prevbezt->vec[1][0];
v1[1] = prevbezt->vec[1][1];
v2[0] = prevbezt->vec[2][0];
v2[1] = prevbezt->vec[2][1];
/* (v3, v4) are the last keyframe's 1st handle + the last keyframe */
v3[0] = bezt->vec[0][0];
v3[1] = bezt->vec[0][1];
v4[0] = bezt->vec[1][0];
v4[1] = bezt->vec[1][1];
if (fabsf(v1[1] - v4[1]) < FLT_EPSILON && fabsf(v2[1] - v3[1]) < FLT_EPSILON && /* try to get a value for this position - if failure, try another set of points */
fabsf(v3[1] - v4[1]) < FLT_EPSILON) { b = findzero(evaltime, v1[0], v2[0], v3[0], v4[0], opl);
/* Optimization: If all the handles are flat/at the same values, if (b) {
* the value is simply the shared value (see T40372 -> F91346) berekeny(v1[1], v2[1], v3[1], v4[1], opl, 1);
*/ cvalue = opl[0];
cvalue = v1[1]; /* break; */
} }
else { else {
/* adjust handles so that they don't overlap (forming a loop) */ if (G.debug & G_DEBUG) {
correct_bezpart(v1, v2, v3, v4); printf(" ERROR: findzero() failed at %f with %f %f %f %f\n",
evaltime,
/* try to get a value for this position - if failure, try another set of points */ v1[0],
b = findzero(evaltime, v1[0], v2[0], v3[0], v4[0], opl); v2[0],
if (b) { v3[0],
berekeny(v1[1], v2[1], v3[1], v4[1], opl, 1); v4[0]);
cvalue = opl[0];
/* break; */
}
else {
if (G.debug & G_DEBUG) {
printf(" ERROR: findzero() failed at %f with %f %f %f %f\n",
evaltime,
v1[0],
v2[0],
v3[0],
v4[0]);
}
} }
} }
break; }
break;
case BEZT_IPO_LIN: case BEZT_IPO_LIN:
/* linear - simply linearly interpolate between values of the two keyframes */ /* linear - simply linearly interpolate between values of the two keyframes */
cvalue = BLI_easing_linear_ease(time, begin, change, duration); cvalue = BLI_easing_linear_ease(time, begin, change, duration);
break; break;
/* easing ............................................ */ /* easing ............................................ */
case BEZT_IPO_BACK: case BEZT_IPO_BACK:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_back_ease_in(time, begin, change, duration, prevbezt->back); cvalue = BLI_easing_back_ease_in(time, begin, change, duration, prevbezt->back);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_back_ease_out(time, begin, change, duration, prevbezt->back); cvalue = BLI_easing_back_ease_out(time, begin, change, duration, prevbezt->back);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_back_ease_in_out( cvalue = BLI_easing_back_ease_in_out(time, begin, change, duration, prevbezt->back);
time, begin, change, duration, prevbezt->back); break;
break;
default: /* default/auto: same as ease out */ default: /* default/auto: same as ease out */
cvalue = BLI_easing_back_ease_out(time, begin, change, duration, prevbezt->back); cvalue = BLI_easing_back_ease_out(time, begin, change, duration, prevbezt->back);
break; break;
} }
break; break;
case BEZT_IPO_BOUNCE: case BEZT_IPO_BOUNCE:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_bounce_ease_in(time, begin, change, duration); cvalue = BLI_easing_bounce_ease_in(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_bounce_ease_out(time, begin, change, duration); cvalue = BLI_easing_bounce_ease_out(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_bounce_ease_in_out(time, begin, change, duration); cvalue = BLI_easing_bounce_ease_in_out(time, begin, change, duration);
break; break;
default: /* default/auto: same as ease out */ default: /* default/auto: same as ease out */
cvalue = BLI_easing_bounce_ease_out(time, begin, change, duration); cvalue = BLI_easing_bounce_ease_out(time, begin, change, duration);
break; break;
} }
break; break;
case BEZT_IPO_CIRC: case BEZT_IPO_CIRC:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_circ_ease_in(time, begin, change, duration); cvalue = BLI_easing_circ_ease_in(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_circ_ease_out(time, begin, change, duration); cvalue = BLI_easing_circ_ease_out(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_circ_ease_in_out(time, begin, change, duration); cvalue = BLI_easing_circ_ease_in_out(time, begin, change, duration);
break; break;
default: /* default/auto: same as ease in */ default: /* default/auto: same as ease in */
cvalue = BLI_easing_circ_ease_in(time, begin, change, duration); cvalue = BLI_easing_circ_ease_in(time, begin, change, duration);
break; break;
} }
break; break;
case BEZT_IPO_CUBIC: case BEZT_IPO_CUBIC:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_cubic_ease_in(time, begin, change, duration); cvalue = BLI_easing_cubic_ease_in(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_cubic_ease_out(time, begin, change, duration); cvalue = BLI_easing_cubic_ease_out(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_cubic_ease_in_out(time, begin, change, duration); cvalue = BLI_easing_cubic_ease_in_out(time, begin, change, duration);
break; break;
default: /* default/auto: same as ease in */ default: /* default/auto: same as ease in */
cvalue = BLI_easing_cubic_ease_in(time, begin, change, duration); cvalue = BLI_easing_cubic_ease_in(time, begin, change, duration);
break; break;
} }
break; break;
case BEZT_IPO_ELASTIC: case BEZT_IPO_ELASTIC:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_elastic_ease_in( cvalue = BLI_easing_elastic_ease_in(
time, begin, change, duration, amplitude, period); time, begin, change, duration, amplitude, period);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_elastic_ease_out( cvalue = BLI_easing_elastic_ease_out(
time, begin, change, duration, amplitude, period); time, begin, change, duration, amplitude, period);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_elastic_ease_in_out( cvalue = BLI_easing_elastic_ease_in_out(
time, begin, change, duration, amplitude, period); time, begin, change, duration, amplitude, period);
break; break;
default: /* default/auto: same as ease out */ default: /* default/auto: same as ease out */
cvalue = BLI_easing_elastic_ease_out( cvalue = BLI_easing_elastic_ease_out(
time, begin, change, duration, amplitude, period); time, begin, change, duration, amplitude, period);
break; break;
} }
break; break;
case BEZT_IPO_EXPO: case BEZT_IPO_EXPO:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_expo_ease_in(time, begin, change, duration); cvalue = BLI_easing_expo_ease_in(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_expo_ease_out(time, begin, change, duration); cvalue = BLI_easing_expo_ease_out(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_expo_ease_in_out(time, begin, change, duration); cvalue = BLI_easing_expo_ease_in_out(time, begin, change, duration);
break; break;
default: /* default/auto: same as ease in */ default: /* default/auto: same as ease in */
cvalue = BLI_easing_expo_ease_in(time, begin, change, duration); cvalue = BLI_easing_expo_ease_in(time, begin, change, duration);
break; break;
} }
break; break;
case BEZT_IPO_QUAD: case BEZT_IPO_QUAD:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_quad_ease_in(time, begin, change, duration); cvalue = BLI_easing_quad_ease_in(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_quad_ease_out(time, begin, change, duration); cvalue = BLI_easing_quad_ease_out(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_quad_ease_in_out(time, begin, change, duration); cvalue = BLI_easing_quad_ease_in_out(time, begin, change, duration);
break; break;
default: /* default/auto: same as ease in */ default: /* default/auto: same as ease in */
cvalue = BLI_easing_quad_ease_in(time, begin, change, duration); cvalue = BLI_easing_quad_ease_in(time, begin, change, duration);
break; break;
} }
break; break;
case BEZT_IPO_QUART: case BEZT_IPO_QUART:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_quart_ease_in(time, begin, change, duration); cvalue = BLI_easing_quart_ease_in(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_quart_ease_out(time, begin, change, duration); cvalue = BLI_easing_quart_ease_out(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_quart_ease_in_out(time, begin, change, duration); cvalue = BLI_easing_quart_ease_in_out(time, begin, change, duration);
break; break;
default: /* default/auto: same as ease in */ default: /* default/auto: same as ease in */
cvalue = BLI_easing_quart_ease_in(time, begin, change, duration); cvalue = BLI_easing_quart_ease_in(time, begin, change, duration);
break; break;
} }
break; break;
case BEZT_IPO_QUINT: case BEZT_IPO_QUINT:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_quint_ease_in(time, begin, change, duration); cvalue = BLI_easing_quint_ease_in(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_quint_ease_out(time, begin, change, duration); cvalue = BLI_easing_quint_ease_out(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_quint_ease_in_out(time, begin, change, duration); cvalue = BLI_easing_quint_ease_in_out(time, begin, change, duration);
break; break;
default: /* default/auto: same as ease in */ default: /* default/auto: same as ease in */
cvalue = BLI_easing_quint_ease_in(time, begin, change, duration); cvalue = BLI_easing_quint_ease_in(time, begin, change, duration);
break; break;
} }
break; break;
case BEZT_IPO_SINE: case BEZT_IPO_SINE:
switch (prevbezt->easing) { switch (prevbezt->easing) {
case BEZT_IPO_EASE_IN: case BEZT_IPO_EASE_IN:
cvalue = BLI_easing_sine_ease_in(time, begin, change, duration); cvalue = BLI_easing_sine_ease_in(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_OUT: case BEZT_IPO_EASE_OUT:
cvalue = BLI_easing_sine_ease_out(time, begin, change, duration); cvalue = BLI_easing_sine_ease_out(time, begin, change, duration);
break; break;
case BEZT_IPO_EASE_IN_OUT: case BEZT_IPO_EASE_IN_OUT:
cvalue = BLI_easing_sine_ease_in_out(time, begin, change, duration); cvalue = BLI_easing_sine_ease_in_out(time, begin, change, duration);
break; break;
default: /* default/auto: same as ease in */ default: /* default/auto: same as ease in */
cvalue = BLI_easing_sine_ease_in(time, begin, change, duration); cvalue = BLI_easing_sine_ease_in(time, begin, change, duration);
break; break;
} }
break; break;
default: default:
cvalue = prevbezt->vec[1][1]; cvalue = prevbezt->vec[1][1];
break; break;
}
}
}
else {
if (G.debug & G_DEBUG) {
printf(" ERROR: failed eval - p=%f b=%f, t=%f (%f)\n",
prevbezt->vec[1][0],
bezt->vec[1][0],
evaltime,
fabsf(bezt->vec[1][0] - evaltime));
} }
} }
} }
else {
if (G.debug & G_DEBUG) {
printf(" ERROR: failed eval - p=%f b=%f, t=%f (%f)\n",
prevbezt->vec[1][0],
bezt->vec[1][0],
evaltime,
fabsf(bezt->vec[1][0] - evaltime));
}
}
/* return value */
return cvalue; return cvalue;
} }
/* Calculate F-Curve value for 'evaltime' using BezTriple keyframes */
static float fcurve_eval_keyframes(FCurve *fcu, BezTriple *bezts, float evaltime)
{
if (evaltime <= bezts->vec[1][0]) {
return fcurve_eval_keyframes_extrapolate(fcu, bezts, evaltime, 0, +1);
}
BezTriple *lastbezt = bezts + fcu->totvert - 1;
if (lastbezt->vec[1][0] <= evaltime) {
return fcurve_eval_keyframes_extrapolate(fcu, bezts, evaltime, fcu->totvert - 1, -1);
}
return fcurve_eval_keyframes_interpolate(fcu, bezts, evaltime);
}
/* Calculate F-Curve value for 'evaltime' using FPoint samples */ /* Calculate F-Curve value for 'evaltime' using FPoint samples */
static float fcurve_eval_samples(FCurve *fcu, FPoint *fpts, float evaltime) static float fcurve_eval_samples(FCurve *fcu, FPoint *fpts, float evaltime)
{ {