Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleInterpolation.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 
27 /** @example ExampleInterpolation.cpp
28  @relates Interpolator
29  @ingroup g_examples
30  @brief Example for Interpolator
31  @author MIP
32 */
33 
34 #include <Base/Common/BIASpragma.hh>
35 
36 #include <stdlib.h>
37 #include <vector>
38 
39 // use MIPMath library
40 #include "MathAlgo/Interpolator.hh"
41 #include "Base/Image/Image.hh"
42 #include "Base/ImageUtils/ImageDraw.hh"
43 #include "Gui/GuiWX.hh"
44 
45 using namespace BIAS;
46 using namespace std;
47 
48 
49 Interpolator interpolator;
50 std::vector<double> listT;
51 std::vector<double> listX;
52 std::vector<double> listY;
53 std::vector<Vector2<double> > listControlPoints;
54 void initDim2()
55 {
56  double t;
57  Vector2<double> vec;
58  t=0; vec[0]=1; vec[1]=599;
59  listT.push_back(t); listControlPoints.push_back(vec);
60  t=50; vec[0]=100; vec[1]=450;
61  listT.push_back(t); listControlPoints.push_back(vec);
62  t=100;vec[0]=200; vec[1]=400;
63  listT.push_back(t); listControlPoints.push_back(vec);
64  t=200; vec[0]=300;vec[1]=400;
65  listT.push_back(t); listControlPoints.push_back(vec);
66  t=300;vec[0]=400;vec[1]=400;
67  listT.push_back(t); listControlPoints.push_back(vec);
68  t=400;vec[0]=500;vec[1]=200;
69  listT.push_back(t); listControlPoints.push_back(vec);
70  t=500;vec[0]=600;vec[1]=100;
71  listT.push_back(t); listControlPoints.push_back(vec);
72  t=600;vec[0]=700; vec[1]=100;
73  listT.push_back(t); listControlPoints.push_back(vec);
74  t=700;vec[0]=600;vec[1]=200;
75  listT.push_back(t); listControlPoints.push_back(vec);
76  t=800;vec[0]=500;vec[1]=400;
77  listT.push_back(t); listControlPoints.push_back(vec);
78  t=900;vec[0]=200;vec[1]=200;
79  listT.push_back(t); listControlPoints.push_back(vec);
80 
81  interpolator.SetKnotPoints(listT);
82  interpolator.SetControlPoints(listControlPoints);
83  interpolator.InitSpline();
84  interpolator.InitBezier(2);
85 }
86 
87 void initDim1()
88 {
89  double t,y;
90  t=0; y=100;
91  listT.push_back(t); listY.push_back(y);
92  t=50; y=100;
93  listT.push_back(t); listY.push_back(y);
94  t=100; y=100;
95  listT.push_back(t); listY.push_back(y);
96  t=150; y=500;
97  listT.push_back(t); listY.push_back(y);
98  t=200;y=500;
99  listT.push_back(t); listY.push_back(y);
100  t=300;y=450;
101  listT.push_back(t); listY.push_back(y);
102  t=400;y=410;
103  listT.push_back(t); listY.push_back(y);
104  t=500;y=100;
105  listT.push_back(t); listY.push_back(y);
106  t=600;y=200;
107  listT.push_back(t); listY.push_back(y);
108  t=650;y=100;
109  listT.push_back(t); listY.push_back(y);
110  t=700;y=350;
111  listT.push_back(t); listY.push_back(y);
112 
113  /*double x,y;
114  x=300.0*0.000; y=1.000*x;
115  listT.push_back(x); listY.push_back(y);
116  x=300.0*0.138; y=1.000*x;
117  listT.push_back(x); listY.push_back(y);
118  x=300.0*0.424; y=0.993*x;
119  listT.push_back(x); listY.push_back(y);
120  x=300.0*0.757; y=0.974*x;
121  listT.push_back(x); listY.push_back(y);
122  x=300.0*1.045; y=0.941*x;
123  listT.push_back(x); listY.push_back(y);
124  x=300.0*1.342; y=0.899*x;
125  listT.push_back(x); listY.push_back(y);
126  x=300.0*1.436; y=0.881*x;
127  listT.push_back(x); listY.push_back(y);
128  x=300.0*1.652; y=0.828*x;
129  listT.push_back(x); listY.push_back(y);
130  */
131  /*
132  double x,y;
133  x=350*0.005; y=350*1.000;
134  listT.push_back(x); listY.push_back(y);
135  x=350*0.092; y=350*1.000;
136  listT.push_back(x); listY.push_back(y);
137  x=350*0.179; y=350*1.000;
138  listT.push_back(x); listY.push_back(y);
139  x=350*0.266; y=350*1.003;
140  listT.push_back(x); listY.push_back(y);
141  x=350*0.354; y=350*1.003;
142  listT.push_back(x); listY.push_back(y);
143  x=350*0.439; y=350*1.003;
144  listT.push_back(x); listY.push_back(y);
145  x=350*0.527; y=350*1.003;
146  listT.push_back(x); listY.push_back(y);
147  x=350*0.615; y=350*1.005;
148  listT.push_back(x); listY.push_back(y);
149  x=350*0.701; y=350*1.005;
150  listT.push_back(x); listY.push_back(y);
151  x=350*0.788; y=350*1.005;
152  listT.push_back(x); listY.push_back(y);
153  x=350*0.875; y=350*1.005;
154  listT.push_back(x); listY.push_back(y);
155  x=350*0.962; y=350*1.003;
156  listT.push_back(x); listY.push_back(y);
157  x=350*1.049; y=350*0.998;
158  listT.push_back(x); listY.push_back(y);
159  x=350*1.135; y=350*0.993;
160  listT.push_back(x); listY.push_back(y);
161  x=350*1.223; y=350*0.983;
162  listT.push_back(x); listY.push_back(y);
163  x=350*1.310; y=350*0.966;
164  listT.push_back(x); listY.push_back(y);
165  x=350*1.398; y=350*0.941;
166  listT.push_back(x); listY.push_back(y);
167  x=350*1.485; y=350*0.897;
168  listT.push_back(x); listY.push_back(y);
169  x=350*1.572; y=350*0.828;
170  listT.push_back(x); listY.push_back(y);
171  x=350*1.660; y=350*0.716;
172  listT.push_back(x); listY.push_back(y);
173  */
174  interpolator.SetKnotPoints(listT);
175  interpolator.SetControlPoints(listY);
176  interpolator.InitSpline();
177  interpolator.InitBezier(1);
178 }
179 
180 
181 int main (int argc, char **argv)
182 {
183  double dt;
184 
185  GuiWX window;
186  char key = '+';
187  initDim1();
189  img.Init(800,600);
190  img.FillImageWithConstValue((unsigned char)0);
191  for (unsigned int i=0;i<listT.size()-1;i++){
192  //img.DrawLine(listT[i],600-listY[i],listT[i],600-listY[i],255);
193  if(listT[i]>=0 && 600-listY[i]>=0 && listT[i+1]<img.GetWidth() && 600-listY[i+1]<img.GetHeight())
195  (unsigned int)listT[i],
196  600-(unsigned int)listY[i],
197  (unsigned int)listT[i+1],
198  600-(unsigned int)listY[i+1],
199  255);
200  }
201  window.SetTitle(string("Interpolation Test"));
202  window.ShowImage(img);
203  int x0=0, y0=0, x1=0, y1=0, k=3;
204  double pos;
205  bool useSpline=true;
206  // Vector2<double> startTang(5,-5),endTang(0,0);
207  double startTang=-10000,endTang=-10000;
208  cerr<<"Press h for help."<<endl;
209 
210  while (key!='q') {
211  window.ShowImage(img);
212  key=window.WaitForKeyEvent();
213  if (key=='s') {
214  if (startTang==-10000) {
215  startTang=0;
216  }
217  startTang-=0.1;
218  cout<<"startTang:"<<startTang<<endl;
219  cout<<"endTang:"<<endTang<<endl;
220  }
221  if (key=='w') {
222  if (startTang==-10000) {
223  startTang=0;
224  }
225  startTang+=0.1;
226  cout<<"startTang:"<<startTang<<endl;
227  cout<<"endTang:"<<endTang<<endl;
228  }
229  if (key=='a') {
230  startTang=-10000;
231  cout<<"startTang:"<<startTang<<endl;
232  cout<<"endTang:"<<endTang<<endl;
233  }
234  if (key=='i') {
235  if (endTang==-10000) {
236  endTang=0;
237  }
238  endTang+=0.1;
239  cout<<"startTang:"<<startTang<<endl;
240  cout<<"endTang:"<<endTang<<endl;
241  }
242  if (key=='k') {
243  if (endTang==-10000) {
244  endTang=0;
245  }
246  endTang-=0.1;
247  cout<<"startTang:"<<startTang<<endl;
248  cout<<"endTang:"<<endTang<<endl;
249  }
250  if (key=='j') {
251  endTang=-10000;
252  cout<<"startTang:"<<startTang<<endl;
253  cout<<"endTang:"<<endTang<<endl;
254  }
255  if (key=='1') k=1;
256  if (key=='2') k=2;
257  if (key=='3') k=3;
258  if (key=='4') k=4;
259  if (key=='b') {
260  if (useSpline) {
261  cout << "showing Bezier" << endl;
262  useSpline=false;
263  } else {
264  cout << "showing Spline" << endl;
265  useSpline=true;
266  }
267  }
268  if (key=='h') {
269  cerr<<"keys: "<<endl;
270  cerr<<"w,s: change startTangent"<<endl;
271  cerr<<"a: relaxed start condition"<<endl;
272  cerr<<"i,k: change endTangent"<<endl;
273  cerr<<"j: relaxed end condition"<<endl;
274  cerr<<"1,2,3: change degree of interpolation"<<endl;
275  cerr<<"4: use akima-spline"<<endl;
276  cerr<<"b : switch Spline and Bezier"<<endl;
277  cerr<<"q : quits"<<endl;
278  }
279 
280  img.FillImageWithConstValue((unsigned char)0);
281  int deg=3;
282  interpolator.SetStartTangent(startTang);
283  interpolator.SetEndTangent(endTang);
284  for (int j=1;j<=2;j++) {
285  if (j==1) deg=1;
286  else deg=k;
287  dt=0;
288  for (int i=0;i<700;i++) {
289  if (useSpline || j==1) {
290  interpolator.Spline(pos,dt,deg);
291  } else {
292  interpolator.Bezier3(pos,dt);
293  }
294  x1=(unsigned int)dt;y1=600-(unsigned int)pos;
295  // interpolator.Spline(y,dt,k);
296  //cerr<<"dt:"<<dt<<" pos:"<<pos<<endl;
297  //y1=600-(int)y;
298  // window.ShowImage(img);
299  //key=window.WaitForKeyEvent();
300  if (i>0){
301 
302  if(x0>=0 && y0>=0 && (unsigned int)x1<img.GetWidth() && (unsigned int)y1<img.GetHeight()&&
303  (unsigned int)x0<img.GetWidth() && (unsigned int)y0<img.GetHeight() && x1>0 && y1 >0)
304  ImageDraw<unsigned char>::Line(img, x0,y0,x1,y1,j*126);
305  }
306  x0=x1;
307  y0=y1;
308  dt+=1;
309  }
310  }
311 
312  }
313 }
314 
315 
316 
317 
318 
int Bezier3(double &res, double t)
these functions do the bezier interpolation as described in David Salomon 4...
virtual char WaitForKeyEvent()
waits until a key is pressed and returns it tested
Definition: GuiBase.cpp:352
void InitSpline()
call this for restart at t= first knot point initiates recalculation of all polynom coefficients at f...
virtual void SetTitle(std::string const &Title)
Definition: GuiWX.cpp:46
simple wxWindow to display an image.
Definition: GuiWX.hh:49
void SetKnotPoints(const std::vector< double > &kPnt)
set the additional knot points, if you want nonuniform interpolation.
void InitBezier(int dim_of_CP)
here the calculation of all coefficients for the bezier interpolation is done and the nmatrix is set ...
void SetStartTangent(double startTangent)
unsigned int GetWidth() const
Definition: ImageBase.hh:312
this class interpolates a function y=f(t) between given control points (the y-values) ...
Definition: Interpolator.hh:71
int Spline(double &res, double t, unsigned int k=3)
these functions do the Spline interpolation which reaches each control point.
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void FillImageWithConstValue(StorageType Value)
fill grey images
Definition: Image.cpp:456
void SetControlPoints(const std::vector< double > &cPnt1)
set the control points, which control the interpolating curve
Definition: Interpolator.hh:87
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
drawing simple entities into the image like rectangles or lines As all functions are static they have...
Definition: ImageDraw.hh:72
virtual int ShowImage(ImageBase &image, float min=0.0, float max=255.0)
shows/updates the image shown in window.
Definition: GuiBase.cpp:137
void SetEndTangent(double endTangent)