OpenWareLaboratory
Interpolator.h
Go to the documentation of this file.
1 #ifndef _Interpolator_h_
2 #define _Interpolator_h_
3 
4 #include <stdint.h>
5 
6 class Interpolator {
7 public:
8  static float linear(float y1, float y2, float mu){
9  return y1 + (y2 - y1) * mu;
10  }
11  static float cosine(float y1, float y2, float mu){
12  float mu2 = (1-cosf(mu*M_PI))/2;
13  return y1*(1-mu2)+y2*mu2;
14  }
19  static float cubic(float y0, float y1, float y2, float mu){
20  float d1 = 0.5*(y2-y0);
21  float d2 = y2-y1+y0-y1;
22  float dx = mu;
23  return y1 + dx*(d1+0.5*d2*dx);
24  }
30  static float cubic(float y0, float y1, float y2, float y3, float mu) {
31  float mu2 = mu*mu;
32  float a0 = y3 - y2 - y0 + y1;
33  float a1 = y0 - y1 - a0;
34  float a2 = y2 - y0;
35  float a3 = y1;
36  return a0*mu*mu2+a1*mu2+a2*mu+a3;
37  }
38  static float cubicSmooth(float y0, float y1, float y2, float y3, float mu) {
39  // use Catmull-Rom splines:
40  // take the slope between the previous point and the next as the derivative at the current point
41  float mu2 = mu*mu;
42  float a0 = -0.5*y0 + 1.5*y1 - 1.5*y2 + 0.5*y3;
43  float a1 = y0 - 2.5*y1 + 2*y2 - 0.5*y3;
44  float a2 = -0.5*y0 + 0.5*y2;
45  float a3 = y1;
46  return a0*mu*mu2+a1*mu2+a2*mu+a3;
47  }
48  /*
49  Tension: 1 is high, 0 normal, -1 is low
50  Bias: 0 is even,
51  positive is towards first segment,
52  negative towards the other
53  */
54  static float hermite(float y0, float y1, float y2, float y3, float mu, float tension=0, float bias=0){
55  float mu2 = mu * mu;
56  float mu3 = mu2 * mu;
57  float m0 = (y1-y0)*(1+bias)*(1-tension)/2;
58  m0 += (y2-y1)*(1-bias)*(1-tension)/2;
59  float m1 = (y2-y1)*(1+bias)*(1-tension)/2;
60  m1 += (y3-y2)*(1-bias)*(1-tension)/2;
61  float a0 = 2*mu3 - 3*mu2 + 1;
62  float a1 = mu3 - 2*mu2 + mu;
63  float a2 = mu3 - mu2;
64  float a3 = -2*mu3 + 3*mu2;
65  return(a0*y1+a1*m0+a2*m1+a3*y2);
66  }
67 };
68 
77 };
78 
79 #endif /* _Interpolator_h_ */
InterpolationMethod
Definition: Interpolator.h:69
@ LINEAR_INTERPOLATION
Definition: Interpolator.h:71
@ HERMITE_INTERPOLATION
Definition: Interpolator.h:76
@ NO_INTERPOLATION
Definition: Interpolator.h:70
@ CUBIC_3P_INTERPOLATION
Definition: Interpolator.h:73
@ CUBIC_4P_INTERPOLATION
Definition: Interpolator.h:74
@ CUBIC_4P_SMOOTH_INTERPOLATION
Definition: Interpolator.h:75
@ COSINE_INTERPOLATION
Definition: Interpolator.h:72
#define M_PI
Definition: basicmaths.h:52
static float cubic(float y0, float y1, float y2, float mu)
Three-point cubic interpolation of point between y1 and y2 ref: http://www.ebyte.it/library/codesnipp...
Definition: Interpolator.h:19
static float hermite(float y0, float y1, float y2, float y3, float mu, float tension=0, float bias=0)
Definition: Interpolator.h:54
static float linear(float y1, float y2, float mu)
Definition: Interpolator.h:8
static float cosine(float y1, float y2, float mu)
Definition: Interpolator.h:11
static float cubic(float y0, float y1, float y2, float y3, float mu)
Four-point cubic interpolation Get the interpolated value of a curve described by y0 to y4,...
Definition: Interpolator.h:30
static float cubicSmooth(float y0, float y1, float y2, float y3, float mu)
Definition: Interpolator.h:38