forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpolation.cpp
1116 lines (996 loc) · 41.6 KB
/
Interpolation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Natron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "Interpolation.h"
#include <cassert>
#include <cmath>
#include <stdexcept>
#include <vector>
#include <algorithm> // min, max
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_OFF
GCC_DIAG_OFF(unused-parameter)
#include <boost/math/special_functions/fpclassify.hpp>
// boost/optional/optional.hpp:1254:53: warning: unused parameter 'out' [-Wunused-parameter]
#include <boost/math/special_functions/cbrt.hpp>
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_ON
GCC_DIAG_ON(unused-parameter)
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288 /* pi */
#endif
NATRON_NAMESPACE_ENTER
using boost::math::cbrt;
using std::sqrt;
using std::cos;
using std::acos;
using std::sqrt;
using std::fabs;
static void
hermiteToCubicCoeffs(double P0,
double P0pr,
double P3pl,
double P3,
double *c0,
double *c1,
double *c2,
double *c3)
{
*c0 = P0;
*c1 = P0pr;
*c2 = 3 * (P3 - P0) - 2 * P0pr - P3pl;
*c3 = -2 * (P3 - P0) + P0pr + P3pl;
assert(P0 == P0 && P0pr == P0pr && P3pl == P3pl && P3 == P3 && *c0 == *c0 && *c1 == *c1 && *c2 == *c2 && *c3 == *c3);
}
// evaluate at t
static double
cubicEval(double c0,
double c1,
double c2,
double c3,
double t)
{
const double t2 = t * t;
const double t3 = t2 * t;
assert(t == t && t2 == t2 && t3 == t3 && c0 == c0 && c1 == c1 && c2 == c2 && c3 == c3);
return c0 + (c1 ? c1 * t : 0.) + (c2 ? c2 * t2 : 0.) + (c3 ? c3 * t3 : 0.);
}
// integrate from 0 to t
static double
cubicIntegrate(double c0,
double c1,
double c2,
double c3,
double t)
{
const double t2 = t * t;
const double t3 = t2 * t;
const double t4 = t3 * t;
assert(t == t && t2 == t2 && t3 == t3 && t4 == t4 && c0 == c0 && c1 == c1 && c2 == c2 && c3 == c3);
return (c0 ? c0 * t : 0.) + (c1 ? c1 * t2 / 2. : 0.) + (c2 ? c2 * t3 / 3 : 0.) + (c3 ? c3 * t4 / 4 : 0.);
}
// derive at t
static double
cubicDerive(double /*c0*/,
double c1,
double c2,
double c3,
double t)
{
const double t2 = t * t;
assert(t == t && t2 == t2 && c1 == c1 && c2 == c2 && c3 == c3);
return c1 + (c2 ? 2 * c2 * t : 0.) + (c3 ? 3 * c3 * t2 : 0.);
}
#define EQN_EPS 1e-9
/********************************************************
* *
* This function determines if a double is small enough *
* to be zero. The purpose of the subroutine is to try *
* to overcome precision problems in math routines. *
* *
********************************************************/
static int
isZero(double x)
{
assert(x == x);
return (x > -EQN_EPS && x < EQN_EPS);
}
/// solve linear equation c0 + c1*x = 0.
/// @returns the number of solutions.
/// solutions an and their order are put in s and o
int
Interpolation::solveLinear(double c0,
double c1,
double s[1],
int o[1])
{
assert(c0 == c0 && c1 == c1);
if ( (c1 == 0.) || isZero(c1) ) {
// it's a constant equation
// there may be an infinity of solutions (if b=0) , but we always return none
return 0; // no solution
} else {
const double a = c1;
const double b = c0;
// solve ax+b = 0
s[0] = -b / a;
o[0] = 1;
return 1;
}
}
/// solve quadric c0 + c1*x + c2*x2 = 0.
/// @returns the number of solutions.
/// solutions an and their order are put in s and o
int
Interpolation::solveQuadric(double c0,
double c1,
double c2,
double s[2],
int o[2])
{
assert(c0 == c0 && c1 == c1 && c2 == c2);
if ( (c2 == 0.) || isZero(c2) ) {
// it's at most a linear equation
return Interpolation::solveLinear(c0, c1, s, o);
}
// normal for: x^2 + px + q
double p = c1 / (2.0 * c2);
double q = c0 / c2;
double D = p * p - q;
if ( (D == 0.) || isZero(D) ) {
// one double root
s[0] = -p;
o[0] = 2;
return 1;
} else if (D < 0.0) {
// no real root
return 0;
} else {
// two real roots
double sqrt_D = sqrt(D);
s[0] = sqrt_D - p;
o[0] = 1;
s[1] = -sqrt_D - p;
o[1] = 1;
return 2;
}
}
/// solve cubic c0 + c1*x + c2*x2 + c3*x3 = 0.
/// @returns the number of solutions.
/// solutions an and their order are put in s and o
int
Interpolation::solveCubic(double c0,
double c1,
double c2,
double c3,
double s[3],
int o[3])
{
assert(c0 == c0 && c1 == c1 && c2 == c2 && c3 == c3);
if ( (c3 == 0.) || isZero(c3) ) {
// it's at most a second-degree polynomial
return Interpolation::solveQuadric(c0, c1, c2, s, o);
}
// normalize the equation:x ^ 3 + Ax ^ 2 + Bx + C = 0
double A = c2 / c3;
double B = c1 / c3;
double C = c0 / c3;
// substitute x = y - A / 3 to eliminate the quadric term: x^3 + px + q = 0
double sq_A = A * A;
double p = 1.0 / 3.0 * (-1.0 / 3.0 * sq_A + B);
double q = 1.0 / 2.0 * (2.0 / 27.0 * A * sq_A - 1.0 / 3.0 * A * B + C);
// use Cardano's formula
double cb_p = p * p * p;
double D = q * q + cb_p;
int num;
if ( (D == 0.) || isZero(D) ) {
if ( (q == 0.) || isZero(q) ) {
// one triple solution
s[0] = 0.;
o[0] = 3;
num = 1;
} else {
// one single and one double solution
double u = cbrt(-q);
s[0] = 2.0 * u;
o[0] = 1;
s[1] = -u;
o[1] = 2;
num = 2;
}
} else if (D < 0.0) {
// casus irreductibilis: three real solutions
double phi = 1.0 / 3.0 * acos( -q / sqrt(-cb_p) );
double t = 2.0 * sqrt(-p);
s[0] = t * cos(phi);
o[0] = 1;
s[1] = -t * cos(phi + M_PI / 3.0);
o[1] = 1;
s[2] = -t * cos(phi - M_PI / 3.0);
o[2] = 1;
num = 3;
} else { // D > 0.0
// one real solution
double sqrt_D = sqrt(D);
double u = cbrt( sqrt_D + fabs(q) );
if (q > 0.0) {
s[0] = -u + p / u;
} else {
s[0] = u - p / u;
}
o[0] = 1;
num = 1;
}
// resubstitute
double sub = 1.0 / 3.0 * A;
for (int i = 0; i < num; ++i) {
s[i] -= sub;
}
return num;
} // solveCubic
/// compute one root from the cubic c0 + c1*x + c2*x2 + c3*x3 = 0, with c3 != 0.
/// @returns one solution.
static double
getOneCubicRoot(double c0,
double c1,
double c2,
double c3)
{
assert(c0 == c0 && c1 == c1 && c2 == c2 && c3 == c3);
assert (c3 != 0.);
// normalize the equation:x ^ 3 + Ax ^ 2 + Bx + C = 0
double A = c2 / c3;
double B = c1 / c3;
double C = c0 / c3;
// substitute x = y - A / 3 to eliminate the quadric term: x^3 + px + q = 0
double sq_A = A * A;
double p = 1.0 / 3.0 * (-1.0 / 3.0 * sq_A + B);
double q = 1.0 / 2.0 * (2.0 / 27.0 * A * sq_A - 1.0 / 3.0 * A * B + C);
double s;
// use Cardano's formula
double cb_p = p * p * p;
double D = q * q + cb_p;
if ( (D == 0.) || isZero(D) ) {
if ( (q == 0.) || isZero(q) ) {
// one triple solution
s = 0.;
} else {
// one single and one double solution
double u = cbrt(-q);
s = 2.0 * u;
}
} else if (D < 0.0) {
// casus irreductibilis: three real solutions
double phi = 1.0 / 3.0 * acos( -q / sqrt(-cb_p) );
double t = 2.0 * sqrt(-p);
s = t * cos(phi);
} else { // D > 0.0
// one real solution
double sqrt_D = sqrt(D);
double u = cbrt( sqrt_D + fabs(q) );
if (q > 0.0) {
s = -u + p / u;
} else {
s = u - p / u;
}
}
// resubstitute
return s - 1.0 / 3.0 * A;
}
/// solve quartic c0 + c1*x + c2*x2 + c3*x3 +c4*x4 = 0.
/// @returns the number of solutions.
/// solutions an and their order are put in s and o
int
Interpolation::solveQuartic(double c0,
double c1,
double c2,
double c3,
double c4,
double s[4],
int o[4])
{
assert(c0 == c0 && c1 == c1 && c2 == c2 && c3 == c3 && c4 == c4);
if ( (c4 == 0.) || isZero(c4) ) {
// it's at most a third-degree polynomial
return Interpolation::solveCubic(c0, c1, c2, c3, s, o);
}
// normalize the equation:x ^ 4 + Ax ^ 3 + Bx ^ 2 + Cx + D = 0
double A = c3 / c4;
double B = c2 / c4;
double C = c1 / c4;
double D = c0 / c4;
// substitute x = y - A / 4 to eliminate the cubic term: x^4 + px^2 + qx + r = 0
double sq_A = A * A;
double p = -3.0 / 8.0 * sq_A + B;
double q = 1.0 / 8.0 * sq_A * A - 1.0 / 2.0 * A * B + C;
double r = -3.0 / 256.0 * sq_A * sq_A + 1.0 / 16.0 * sq_A * B - 1.0 / 4.0 * A * C + D;
int num;
if ( (r == 0.) || isZero(r) ) {
// no absolute term:y(y ^ 3 + py + q) = 0
num = Interpolation::solveCubic(q, p, 0., 1., s, o);
// if q = 0, this should be within the previously computed solutions,
// but we just add another solution with order 1
s[num] = 0.,
o[num] = 1;
++num;
} else {
// solve the resolvent cubic...
// ...and take one real solution... (there may be more)
double z = getOneCubicRoot(1.0 / 2.0 * r * p - 1.0 / 8.0 * q * q, -r, -1.0 / 2.0 * p, -1);
// ...to build two quadratic equations
double u = z * z - r;
double v = 2.0 * z - p;
if ( (u == 0.) || isZero(u) ) {
u = 0.0;
} else if (u > 0.0) {
u = sqrt(u);
} else {
return 0;
}
if ( isZero(v) ) {
v = 0;
} else if (v > 0.0) {
v = sqrt(v);
} else {
return 0;
}
num = Interpolation::solveQuadric(z - u, q < 0 ? -v : v, 1.0, s, o);
num += Interpolation::solveQuadric(z + u, q < 0 ? v : -v, 1.0, s + num, o + num);
}
// resubstitute
double sub = 1.0 / 4 * A;
for (int i = 0; i < num; i++) {
s[i] -= sub;
}
return num;
} // solveQuartic
/**
* @brief Interpolates using the control points P0(t0,v0) , P3(t3,v3)
* and the derivatives P1(t1,v1) (being the derivative at P0 with respect to
* t \in [t1,t2]) and P2(t2,v2) (being the derivative at P3 with respect to
* t \in [t1,t2]) the value at 'currentTime' using the
* interpolation method "interp".
* Note that for CATMULL-ROM you must use the function interpolate_catmullRom
* which will compute the derivatives for you.
**/
double
Interpolation::interpolate(double tcur,
const double vcur, //start control point
const double vcurDerivRight, //being the derivative dv/dt at tcur
const double vnextDerivLeft, //being the derivative dv/dt at tnext
double tnext,
const double vnext, //end control point
double currentTime,
KeyframeTypeEnum interp,
KeyframeTypeEnum interpNext)
{
double P0 = vcur;
double P3 = vnext;
// Hermite coefficients P0' and P3' are the derivatives with respect to x \in [0,1]
double P0pr = vcurDerivRight * (tnext - tcur); // normalize for x \in [0,1]
double P3pl = vnextDerivLeft * (tnext - tcur); // normalize for x \in [0,1]
// if the following is true, this makes the special case for eKeyframeTypeConstant at tnext useless, and we can always use a cubic - the strict "currentTime < tnext" is the key
// commented-out: the following assert is not true for periodic curves and passing the flag to interpolate would only be required in NDEBUG
//assert( ( (interp == eKeyframeTypeNone) || (tcur <= currentTime) ) && ( (currentTime < tnext) || (interpNext == eKeyframeTypeNone) ) );
// after the last / before the first keyframe, derivatives are wrt currentTime (i.e. non-normalized)
if (interp == eKeyframeTypeNone) {
// virtual previous frame at t-1
P0 = P3 - P3pl;
P0pr = P3pl;
tcur = tnext - 1.;
} else if (interp == eKeyframeTypeConstant) {
P0pr = 0.;
P3pl = 0.;
P3 = P0;
}
if (interpNext == eKeyframeTypeNone) {
// virtual next frame at t+1
P3pl = P0pr;
P3 = P0 + P0pr;
tnext = tcur + 1;
}
double c0, c1, c2, c3;
hermiteToCubicCoeffs(P0, P0pr, P3pl, P3, &c0, &c1, &c2, &c3);
const double t = (currentTime - tcur) / (tnext - tcur);
double ret = cubicEval(c0, c1, c2, c3, t);
// cubicDerive: divide the result by (tnext-tcur)
// cubicIntegrate: multiply the result by (tnext-tcur)
return ret;
}
/// derive at currentTime. The derivative is with respect to currentTime
double
Interpolation::derive(double tcur,
const double vcur, //start control point
const double vcurDerivRight, //being the derivative dv/dt at tcur
const double vnextDerivLeft, //being the derivative dv/dt at tnext
double tnext,
const double vnext, //end control point
double currentTime,
KeyframeTypeEnum interp,
KeyframeTypeEnum interpNext)
{
double P0 = vcur;
double P3 = vnext;
// Hermite coefficients P0' and P3' are the derivatives with respect to x \in [0,1]
double P0pr = vcurDerivRight * (tnext - tcur); // normalize for x \in [0,1]
double P3pl = vnextDerivLeft * (tnext - tcur); // normalize for x \in [0,1]
// if the following is true, this makes the special case for eKeyframeTypeConstant at tnext useless, and we can always use a cubic - the strict "currentTime < tnext" is the key
assert( ( (interp == eKeyframeTypeNone) || (tcur <= currentTime) ) && ( (currentTime < tnext) || (interpNext == eKeyframeTypeNone) ) );
// after the last / before the first keyframe, derivatives are wrt currentTime (i.e. non-normalized)
if (interp == eKeyframeTypeNone) {
// virtual previous frame at t-1
P0 = P3 - P3pl;
P0pr = P3pl;
tcur = tnext - 1.;
} else if (interp == eKeyframeTypeConstant) {
P0pr = 0.;
P3pl = 0.;
P3 = P0;
}
if (interpNext == eKeyframeTypeNone) {
// virtual next frame at t+1
P3pl = P0pr;
P3 = P0 + P0pr;
tnext = tcur + 1;
}
double c0, c1, c2, c3;
hermiteToCubicCoeffs(P0, P0pr, P3pl, P3, &c0, &c1, &c2, &c3);
const double t = (currentTime - tcur) / (tnext - tcur);
double ret = cubicDerive(c0, c1, c2, c3, t);
// cubicDerive: divide the result by (tnext-tcur)
// cubicIntegrate: multiply the result by (tnext-tcur)
return ret / (tnext - tcur);
}
/// interpolate and derive at currentTime. The derivative is with respect to currentTime
double
Interpolation::derive_clamp(double tcur,
const double vcur, //start control point
const double vcurDerivRight, //being the derivative dv/dt at tcur
const double vnextDerivLeft, //being the derivative dv/dt at tnext
double tnext,
const double vnext, //end control point
double currentTime,
double vmin,
double vmax,
KeyframeTypeEnum interp,
KeyframeTypeEnum interpNext)
{
double P0 = vcur;
double P3 = vnext;
// Hermite coefficients P0' and P3' are the derivatives with respect to x \in [0,1]
double P0pr = vcurDerivRight * (tnext - tcur); // normalize for x \in [0,1]
double P3pl = vnextDerivLeft * (tnext - tcur); // normalize for x \in [0,1]
// if the following is true, this makes the special case for eKeyframeTypeConstant at tnext useless, and we can always use a cubic - the strict "currentTime < tnext" is the key
assert( ( (interp == eKeyframeTypeNone) || (tcur <= currentTime) ) && ( (currentTime < tnext) || (interpNext == eKeyframeTypeNone) ) );
// after the last / before the first keyframe, derivatives are wrt currentTime (i.e. non-normalized)
if (interp == eKeyframeTypeNone) {
// virtual previous frame at t-1
P0 = P3 - P3pl;
P0pr = P3pl;
tcur = tnext - 1.;
} else if (interp == eKeyframeTypeConstant) {
P0pr = 0.;
P3pl = 0.;
P3 = P0;
}
if (interpNext == eKeyframeTypeNone) {
// virtual next frame at t+1
P3pl = P0pr;
P3 = P0 + P0pr;
tnext = tcur + 1;
}
double c0, c1, c2, c3;
hermiteToCubicCoeffs(P0, P0pr, P3pl, P3, &c0, &c1, &c2, &c3);
const double t = (currentTime - tcur) / (tnext - tcur);
double v = cubicEval(c0, c1, c2, c3, t);
if ( (vmin < v) && (v < vmax) ) {
// cubicDerive: divide the result by (tnext-tcur)
return cubicDerive(c0, c1, c2, c3, t) / (tnext - tcur);
}
// function is clamped at t, derivative is 0.
return 0.;
}
// integrate from time1 to time2
double
Interpolation::integrate(double tcur,
const double vcur, //start control point
const double vcurDerivRight, //being the derivative dv/dt at tcur
const double vnextDerivLeft, //being the derivative dv/dt at tnext
double tnext,
const double vnext, //end control point
double time1,
double time2,
KeyframeTypeEnum interp,
KeyframeTypeEnum interpNext)
{
double P0 = vcur;
double P3 = vnext;
// Hermite coefficients P0' and P3' are the derivatives with respect to x \in [0,1]
double P0pr = vcurDerivRight * (tnext - tcur); // normalize for x \in [0,1]
double P3pl = vnextDerivLeft * (tnext - tcur); // normalize for x \in [0,1]
// in the next expression, the correct test is t2 <= tnext (not <), in order to integrate from tcur to tnext
assert( ( (interp == eKeyframeTypeNone) || (tcur <= time1) ) && (time1 <= time2) && ( (time2 <= tnext) || (interpNext == eKeyframeTypeNone) ) );
// after the last / before the first keyframe, derivatives are wrt currentTime (i.e. non-normalized)
if (interp == eKeyframeTypeNone) {
// virtual previous frame at t-1
P0 = P3 - P3pl;
P0pr = P3pl;
tcur = tnext - 1.;
} else if (interp == eKeyframeTypeConstant) {
P0pr = 0.;
P3pl = 0.;
P3 = P0;
}
if (interpNext == eKeyframeTypeNone) {
// virtual next frame at t+1
P3pl = P0pr;
P3 = P0 + P0pr;
tnext = tcur + 1;
}
double c0, c1, c2, c3;
hermiteToCubicCoeffs(P0, P0pr, P3pl, P3, &c0, &c1, &c2, &c3);
const double t2 = (time2 - tcur) / (tnext - tcur);
double ret = cubicIntegrate(c0, c1, c2, c3, t2);
if (time1 != tcur) {
const double t1 = (time1 - tcur) / (tnext - tcur);
ret -= cubicIntegrate(c0, c1, c2, c3, t1);
}
// cubicDerive: divide the result by (tnext-tcur)
// cubicIntegrate: multiply the result by (tnext-tcur)
return ret * (tnext - tcur);
}
NATRON_NAMESPACE_ANONYMOUS_ENTER
enum SolTypeEnum
{
eSolTypeMin,
eSolTypeMax
};
enum FuncTypeEnum
{
eFuncTypeClampMin,
eFuncTypeClampMax,
eFuncTypeCubic
};
struct Sol
{
Sol(SolTypeEnum _type,
double _t,
int _order,
double _deriv)
: type(_type), t(_t), order(_order), deriv(_deriv)
{
}
SolTypeEnum type;
double t;
int order;
double deriv;
};
struct Sol_less_than_t
{
inline bool operator() (const Sol & struct1,
const Sol & struct2)
{
return struct1.t < struct2.t;
}
};
NATRON_NAMESPACE_ANONYMOUS_EXIT
// comptute the function type after sol, from the function type before sol
static FuncTypeEnum
statusUpdate(FuncTypeEnum status,
const Sol & sol)
{
switch (status) {
case eFuncTypeClampMin:
assert(sol.type == eSolTypeMin);
assert(sol.deriv >= /*0*/ -EQN_EPS);
if (sol.order % 2) {
// only odd solution orders may change the status
return eFuncTypeCubic;
}
break;
case eFuncTypeClampMax:
assert(sol.type == eSolTypeMax);
assert(sol.deriv <= /*0*/ EQN_EPS);
if (sol.order % 2) {
// only odd solution orders may change the status
return eFuncTypeCubic;
}
break;
case eFuncTypeCubic:
if (sol.type == eSolTypeMin) {
assert(sol.deriv <= /*0*/ EQN_EPS);
if (sol.order % 2) {
// only odd solution orders may change the status
return eFuncTypeClampMin;
}
} else {
assert(sol.deriv >= /*0*/ -EQN_EPS);
if (sol.order % 2) {
// only odd solution orders may change the status
return eFuncTypeClampMax;
}
}
break;
}
// status is unchanged
assert( (sol.order % 2) == 0 );
return status;
}
// integrate from time1 to time2 with clamping of the function values in [vmin,vmax]
double
Interpolation::integrate_clamp(double tcur,
const double vcur, //start control point
const double vcurDerivRight, //being the derivative dv/dt at tcur
const double vnextDerivLeft, //being the derivative dv/dt at tnext
double tnext,
const double vnext, //end control point
double time1,
double time2,
double vmin,
double vmax,
KeyframeTypeEnum interp,
KeyframeTypeEnum interpNext)
{
if ( vmin == -std::numeric_limits<double>::infinity() &&
vmax == +std::numeric_limits<double>::infinity() ) {
return integrate(tcur,
vcur, //start control point
vcurDerivRight, //being the derivative dv/dt at tcur
vnextDerivLeft, //being the derivative dv/dt at tnext
tnext,
vnext, //end control point
time1,
time2,
interp,
interpNext);
}
double P0 = vcur;
double P3 = vnext;
// Hermite coefficients P0' and P3' are the derivatives with respect to x \in [0,1]
double P0pr = vcurDerivRight * (tnext - tcur); // normalize for x \in [0,1]
double P3pl = vnextDerivLeft * (tnext - tcur); // normalize for x \in [0,1]
// in the next expression, the correct test is t2 <= tnext (not <), in order to integrate from tcur to tnext
assert( ( (interp == eKeyframeTypeNone) || (tcur <= time1) ) && (time1 <= time2) && ( (time2 <= tnext) || (interpNext == eKeyframeTypeNone) ) );
// after the last / before the first keyframe, derivatives are wrt currentTime (i.e. non-normalized)
if (interp == eKeyframeTypeNone) {
// virtual previous frame at t-1
P0 = P3 - P3pl;
P0pr = P3pl;
tcur = tnext - 1.;
} else if (interp == eKeyframeTypeConstant) {
P0pr = 0.;
P3pl = 0.;
P3 = P0;
}
if (interpNext == eKeyframeTypeNone) {
// virtual next frame at t+1
P3pl = P0pr;
P3 = P0 + P0pr;
tnext = tcur + 1;
}
double c0, c1, c2, c3;
hermiteToCubicCoeffs(P0, P0pr, P3pl, P3, &c0, &c1, &c2, &c3);
// solve cubic = vmax
double tmax[3];
int omax[3];
int nmax = (vmax < DBL_MAX) ? Interpolation::solveCubic(c0 - vmax, c1, c2, c3, tmax, omax) : 0;
// solve cubic = vmin
double tmin[3];
int omin[3];
int nmin = (vmin > -DBL_MAX) ? Interpolation::solveCubic(c0 - vmin, c1, c2, c3, tmin, omin) : 0;
// now, find out on which intervals the function is constant/clamped, and on which intervals it is a cubic.
// ignore the solutions with an order of 2 (which means the tangent is horizontal and the polynomial doesn't change sign)
// algorithm: order the solutions, sort them wrt time. The cubic sections are where there are transitions between min and max solutions.
std::vector<Sol> sols;
for (int i = 0; i < nmax; ++i) {
double deriv = cubicDerive(c0, c1, c2, c3, tmax[i]);
assert(deriv == deriv);
sols.push_back( Sol(eSolTypeMax, tmax[i], omax[i], deriv) );
}
for (int i = 0; i < nmin; ++i) {
double deriv = cubicDerive(c0, c1, c2, c3, tmin[i]);
assert(deriv == deriv);
sols.push_back( Sol(eSolTypeMin, tmin[i], omin[i], deriv) );
}
const double t2 = (time2 - tcur) / (tnext - tcur);
const double t1 = (time1 - tcur) / (tnext - tcur);
// special case: no solution, do the same as Interpolation::integrate()
if ( sols.empty() ) {
// no solution.
// function never crosses vmin or vmax:
// - either it's entirely below vmin or above vmax
// - or it' constant
double ret = cubicIntegrate(c0, c1, c2, c3, t2);
if (time1 != tcur) {
ret -= cubicIntegrate(c0, c1, c2, c3, t1);
}
// cubicDerive: divide the result by (tnext-tcur)
// cubicIntegrate: multiply the result by (tnext-tcur)
return ret * (tnext - tcur);
}
// sort the solutions wrt time
std::sort( sols.begin(), sols.end(), Sol_less_than_t() );
// find out the status before the first solution
FuncTypeEnum status;
if (sols[0].type == eSolTypeMax) {
// a non-constant cubic cannot remain within [vmin,vmax] at -infinity
assert(sols[0].deriv < /*0*/ EQN_EPS);
status = eFuncTypeClampMax;
} else {
// a non-constant cubic cannot remain within [vmin,vmax] at -infinity
assert(sols[0].deriv > /*0*/ -EQN_EPS);
status = eFuncTypeClampMin;
}
// find out the status at t1
std::vector<Sol>::const_iterator it = sols.begin();
while (it != sols.end() && it->t <= t1) {
status = statusUpdate(status, *it);
++it;
}
double t = t1;
double ret = 0.;
// it is now pointing to the first solution after t1, or end()
while (it != sols.end() && it->t < t2) {
// integrate from t to it->t
switch (status) {
case eFuncTypeClampMax:
ret += (it->t - t) * vmax;
break;
case eFuncTypeClampMin:
ret += (it->t - t) * vmin;
break;
case eFuncTypeCubic:
ret += cubicIntegrate(c0, c1, c2, c3, it->t) - cubicIntegrate(c0, c1, c2, c3, t);
break;
}
status = statusUpdate(status, *it);
t = it->t;
++it;
}
// integrate from t to t2
switch (status) {
case eFuncTypeClampMax:
ret += (t2 - t) * vmax;
break;
case eFuncTypeClampMin:
ret += (t2 - t) * vmin;
break;
case eFuncTypeCubic:
ret += cubicIntegrate(c0, c1, c2, c3, t2) - cubicIntegrate(c0, c1, c2, c3, t);
break;
}
// cubicIntegrate: multiply the result by (tnext-tcur)
return ret * (tnext - tcur);
} // integrate_clamp
/**
* @brief This function will set the left and right derivative of 'cur', depending on the interpolation method 'interp' and the
* previous and next key frames.
* ----------------------------------------------------------------------------
* Using the Bezier cubic equation, its 2nd derivative can be expressed as such:
* B''(t) = 6(1-t)(P2 - 2P1 + P0) + 6t(P3 - 2P2 + P1)
* We have P1 = P0 + P0'_r / 3
* and Q2 = Q3 - Q3'_l / 3
* We can insert it in the 2nd derivative form, which yields:
* B''(t) = 6(1-t)(P3 - P3'_l/3 - P0 - 2P0'_r/3) + 6t(P0 - P3 + 2P3'_l/3 + P0'_r/3)
*
* So for t = 0, we have:
* B''(0) = 6(P3 - P0 - P3'_l / 3 - 2P0'_r / 3)
* and for t = 1 , we have:
* Q''(1) = 6(Q0 - Q3 + 2Q3'_l / 3 + Q0'_r / 3)
*
* We also know that the 1st derivative of B(t) at 0 is the derivative to P0
* and the 1st derivative of B(t) at 1 is the derivative to P3, i.e:
* B'(0) = P0'_r
* B'(1) = P3'_l
**/
/*
Maple code to compute the values for each case:
with(CodeGeneration):
P := t -> (1-t)**3 * P0 + 3 * (1-t)**2 * t * P1 + 3 * (1-t) * t**2 * P2 + t**3 * P3:
Q := t -> (1-t)**3 * Q0 + 3 * (1-t)**2 * t * Q1 + 3 * (1-t) * t**2 * Q2 + t**3 * Q3:
dP := D(P):
dP2 := D(dP):
dQ := D(Q):
dQ2 := D(dQ):
P1 := P0 + P0pr / 3:
Q2 := Q3 - Q3pl / 3:
Q1 := Q0 + Q0pr / 3:
P2 := P3 - P3pl / 3:
Q3 := P0:
derivativeAtCurRight := dP(0)/(tnext-tcur):
curvatureAtCurRight := dP2(0)/(tnext-tcur)**2:
curvatureAtNextLeft:= dP2(1)/(tnext - tcur)**2:
derivativeAtCurLeft := dQ(1)/(tcur-tprev):
curvatureAtCurLeft:= dQ2(1)/(tcur - tprev)**2:
curvatureAtPrevRight:= dQ2(0)/(tcur - tprev)**2:
printf("linear, general case:"):
solve( {curvatureAtCurRight = 0, curvatureAtCurLeft = 0}, { P0pr, Q3pl });
map(C,%):
printf("linear, prev is linear:"):
solve({curvatureAtCurRight = 0, curvatureAtCurLeft = 0, curvatureAtPrevRight = 0}, { P0pr, Q3pl, Q0pr});
map(C,%):
printf("linear, next is linear:"):
solve({curvatureAtCurRight = 0, curvatureAtCurLeft = 0, curvatureAtNextLeft = 0}, {P0pr, Q3pl, P3pl});
map(C,%):
printf("linear, prev and next are linear:"):
solve({curvatureAtCurRight = 0, curvatureAtCurLeft = 0, curvatureAtPrevRight = 0, curvatureAtNextLeft = 0}, {P0pr, Q3pl, Q0pr, P3pl});
map(C,%):
printf("cubic, general case:"):
solve({curvatureAtCurRight = curvatureAtCurLeft, derivativeAtCurRight = derivativeAtCurLeft}, {P0pr, Q3pl});
map(C,%):
printf("cubic, prev is linear:"):
solve({curvatureAtCurRight = curvatureAtCurLeft, derivativeAtCurRight = derivativeAtCurLeft, curvatureAtPrevRight = 0},{P0pr, Q3pl, Q0pr});
map(C,%):
printf("cubic, next is linear:"):
solve({curvatureAtCurRight = curvatureAtCurLeft, derivativeAtCurRight = derivativeAtCurLeft, curvatureAtNextLeft = 0}, {P0pr, Q3pl, P3pl});
map(C,%):
printf("cubic, prev and next are linear"):
solve({curvatureAtCurRight = curvatureAtCurLeft, derivativeAtCurRight = derivativeAtCurLeft, curvatureAtPrevRight = 0, curvatureAtNextLeft = 0},{P0pr, Q3pl, Q0pr, P3pl});
map(C,%):
*/
void
Interpolation::autoComputeDerivatives(KeyframeTypeEnum interpPrev,
KeyframeTypeEnum interp,
KeyframeTypeEnum interpNext,
double tprev,
const double vprev, // vprev = Q0
double tcur,
const double vcur, // vcur = Q3 = P0
double tnext,
const double vnext, // vnext = P3
const double vprevDerivRight, // Q0'_r
const double vnextDerivLeft, // P3'_l
double *vcurDerivLeft, // Q3'_l
double *vcurDerivRight) // P0'_r
{
const double Q0 = vprev;
const double Q3 = vcur;
const double P0 = vcur;
const double P3 = vnext;
// Hermite coefficients P0' and P3' are the derivatives with respect to x \in [0,1]
if (interpPrev == eKeyframeTypeNone) {
tprev = tcur - 1.;
}
if (interpNext == eKeyframeTypeNone) {
tnext = tcur + 1.;
}
const double Q0pr = vprevDerivRight * (tcur - tprev); // normalize for x \in [0,1]
const double P3pl = vnextDerivLeft * (tnext - tcur); // normalize for x \in [0,1]
double P0pr = double();
double Q3pl = double();
// if there are no keyframes before and after, the derivatives are zero
if ( (interpPrev == eKeyframeTypeNone) && (interpNext == eKeyframeTypeNone) ) {
*vcurDerivRight = 0.;
*vcurDerivLeft = 0.;
}
// If there is no next/previous keyframe, should there be a continuous derivative?
bool keyframe_none_same_derivative = false;
// if there is no next/previous keyframe, use LINEAR interpolation (except for horizontal, see bug #1050), and set keyframe_none_same_derivative
if ( (interp != eKeyframeTypeHorizontal) && ( (interpPrev == eKeyframeTypeNone) || (interpNext == eKeyframeTypeNone) ) ) {
// Do this before modifying interp (next line)
keyframe_none_same_derivative = (interp == eKeyframeTypeCatmullRom || interp == eKeyframeTypeCubic);
interp = eKeyframeTypeLinear;
}
switch (interp) {
case eKeyframeTypeLinear:
/* Linear means the the 2nd derivative of the cubic curve at the point 'cur' is zero. */
if (interpNext == eKeyframeTypeNone) {
P0pr = 0.;