ANIMA  4.0
animaCubicInterpolation.hxx
Go to the documentation of this file.
1 #pragma once
3 
4 namespace anima
5 {
6 
7 template <class T>
8 T
9 Cubic (T x0, T x1, T x2, T x3, T y0, T y1, T y2, T y3, T x)
10 {
11  double p0 = y1;
12  double p1 = y2;
13 
14  double m0 = (y2-y0)/(x2-x0);
15  double m1 = (y3-y1)/(x3-x1);
16 
17  double t = (x - x1)/(x2 - x1);
18  double t2 = t*t;
19  double t3 = t2*t;
20  double h00 = 2*t3 -3*t2 +1;
21  double h10 = t3 -2*t2 +t;
22  double h01 = -2*t3 + 3*t2;
23  double h11 = t3 - t2;
24 
25  return h00*p0 + h10*m0*(x2 - x1) + h01*p1 + h11*m1*(x2 - x1);
26 }
27 
28 template <class T>
29 void
30 InverseCubicInterpolator(std::vector <T> &inputVect, std::vector <T> &outputVect, T step)
31 {
32  double x0, x1, x2, x3, y0, y1, y2, y3;
33  int i=0;
34  outputVect.clear();
35  outputVect.push_back(0);
36  double value = step + inputVect.front();
37 
38  while (value < inputVect.back())
39  {
40  while(inputVect[i] <= value)
41  {
42  i++;
43  }
44  x1 = i-1;
45  x2 = i;
46  x3 = i+1;
47 
48  y1 = inputVect[i-1];
49  y2 = inputVect[i];
50 
51  if (i == 1)
52  {
53  x0 = x1;;
54  y0 = y1;
55  }
56  else
57  {
58  y0 = inputVect[i-2];
59  x0 = i-2;
60  }
61 
62  if (i == inputVect.size() - 1)
63  {
64  y3 = y2 ;
65  x3 = x2;
66  }
67  else
68  {
69  y3 = inputVect[i+1];
70  x3 = i+1;
71  }
72 
73  outputVect.push_back(Cubic(-y3, -y2, -y1, -y0, x3, x2, x1, x0, -value));
74  value += step;
75  }
76  outputVect.push_back(inputVect.size() - 1);
77 }
78 
79 template <class T>
80 void
81 CubicInterpolator(std::vector <T> &transfVect, std::vector <T> &scale, std::vector <T> &outputVect, T LengthLine)
82 {
83  double x0, x1, x2, x3, y0, y1, y2, y3;
84  int i = 0;
85  int it = 0;
86  outputVect.clear();
87  outputVect.push_back(transfVect[0]);
88  i++;
89  while(i < LengthLine -1)
90  {
91  while (scale[it] < i)
92  {
93  it++;
94  }
95 
96  x1 = scale[it-1];
97  x2 = scale[it];
98  y1 = transfVect[it-1];
99  y2 = transfVect[it];
100 
101  if(it ==1)
102  {
103  x0 = x1 ;
104  y0 = y1;
105  }
106  else
107  {
108  x0 = scale[it-2];
109  y0 = transfVect[it-2];
110  }
111 
112  if (it == scale.size() -1)
113  {
114  x3 = x2;
115  y3 = y2;
116  }
117  else
118  {
119  x3 = scale[it+1];
120  y3 = transfVect[it+1];
121  }
122 
123  outputVect.push_back(Cubic<double>(x0, x1, x2, x3, y0, y1, y2, y3, i));
124  i++;
125  }
126 
127  outputVect.push_back(transfVect.back());
128 }
129 
130 } // end namespace anima
void CubicInterpolator(std::vector< T > &transfVect, std::vector< T > &scale, std::vector< T > &outputVect, T LengthLine)
void InverseCubicInterpolator(std::vector< T > &inputVect, std::vector< T > &outputVect, T step)
T Cubic(T x0, T x1, T x2, T x3, T y0, T y1, T y2, T y3, T x)