Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleBSplineCurve.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009, 2005 (see file CONTACT 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 /** @example ExampleBSplineCurve.cpp
26  @relates BSplineCurve
27  @ingroup g_examples
28  @brief Example draws:
29  - regular open quadratic bspline into ExampleBSplineCurve0.mip
30  - regular closed quadratic bspline into ExampleBSplineCurve1.mip
31  - regular open cubic bspline into ExampleBSplineCurve2.mip
32  - regular closed cubic bspline into ExampleBSplineCurve3.mip
33  @author MIP
34 */
35 
36 
37 #include <Base/ImageUtils/ImageDraw.hh>
38 #include <Base/Image/ImageIO.hh>
39 #include <Base/Math/Matrix.hh>
40 #include <Base/Math/Vector.hh>
41 #include <Base/Math/Vector2.hh>
42 #include <MathAlgo/BSplineCurve.hh>
43 
44 using namespace BIAS;
45 using namespace std;
46 
47 
49 unsigned int imgCounter=0;
50 unsigned char value[1];
51 
52 
53 void drawQuadratic(BIAS::BSplineCurve bspline){
54  img.FillImageWithConstValue(value);
55  double t=0;
56  double d=0.01;//stepsize
57  Vector2<double> tmp;//remember last result
58  Vector2<double> res;
59 
60  bspline.QuadraticUniformBSpline(res,t);//first result
61  tmp=res;
62  t+=d;
63  while(t<1.+d){
64  bspline.QuadraticUniformBSpline(res,t);
65  ImageDraw<unsigned char>::Line(img,(unsigned int)tmp[0],
66  (unsigned int)tmp[1],
67  (unsigned int)res[0],
68  (unsigned int)res[1],
69  255);
70  tmp=res;
71  t+=d;
72  }
73  char title[255];
74  sprintf(title, "ExampleBSplineCurve%01i.mip",imgCounter++);
75 
76  BIAS::ImageIO::Save(title, img);
77 }
78 
79 void drawCubic(BIAS::BSplineCurve bspline){
80  img.FillImageWithConstValue(value);
81  double t=0;
82  double d=0.01;//stepsize
83  Vector2<double> tmp;//remember last result
84  Vector2<double> res;
85 
86  bspline.CubicUniformBSpline(res,t);//first result
87  tmp=res;
88  t+=d;
89  while(t<1.+d){
90  bspline.CubicUniformBSpline(res,t);
91  ImageDraw<unsigned char>::Line(img,(unsigned int)tmp[0],
92  (unsigned int)tmp[1],
93  (unsigned int)res[0],
94  (unsigned int)res[1],
95  255);
96  tmp=res;
97  t+=d;
98  }
99  char title[255];
100  sprintf(title, "ExampleBSplineCurve%01i.mip",imgCounter++);
101 
102  BIAS::ImageIO::Save(title, img);
103 }
104 
105 
106 //int main(int args, char * argv[])
107 int main()
108 {
109  //prepage image for dawing
110  value[0]=0;
111  img.Init(800,800);
112  img.FillImageWithConstValue(value);
113 
114  //bspline constructor
115  BSplineCurve bsplineQuadraticOpen,bsplineQuadraticClosed,
116  bsplineCubicOpen,bsplineCubicClosed;
117 
118  //set controlpoints for simple open bspline (cubic and quadratic)
119  std::vector<BIAS::Vector2<double> > cPnt;
121  item[0]=0.;
122  item[1]=0.;
123  cPnt.push_back(item);
124  item[0]=1345.996845;
125  item[1]=1263.704834;
126  cPnt.push_back(item);
127  item[0]=1911.155259;
128  item[1]=1785.161187;
129  cPnt.push_back(item);
130  item[0]=2385.32832;
131  item[1]=2306.61754;
132  cPnt.push_back(item);
133 
134  item[0]=2850.048898;
135  item[1]=2828.073893;
136  cPnt.push_back(item);
137 
138  item[0]=3375.122;
139  item[1]=3349.530246;
140  cPnt.push_back(item);
141 
142  item[0]=3934.636532;
143  item[1]=3870.986599;
144  cPnt.push_back(item);
145 
146  item[0]=4446.42884;
147  item[1]=4392.442952;
148  cPnt.push_back(item);
149 
150  item[0]=4990.218638;
151  item[1]=4913.899306;
152  cPnt.push_back(item);
153 
154  item[0]=5604.887952;
155  item[1]=5435.355659;
156  cPnt.push_back(item);
157 
158  item[0]=6120.227702;
159  item[1]=5956.812012;
160  cPnt.push_back(item);
161 
162  item[0]=8000;
163  item[1]=8000;
164  cPnt.push_back(item);
165 
166 
167  for(unsigned int i=0;i<cPnt.size();i++)
168  cPnt[i].DivideIP(10);
169 
170 // item[0]=200.;
171 // item[1]=150.;
172 // cPnt.push_back(item);
173 // item[0]=100.;
174 // item[1]=100.;
175 // cPnt.push_back(item);
176 // item[0]=100.;
177 // item[1]=200.;
178 // cPnt.push_back(item);
179 // item[0]=200.;
180 // item[1]=300.;
181 // cPnt.push_back(item);
182 // item[0]=200.;
183 // item[1]=300.;
184 // cPnt.push_back(item);
185 // item[0]=300.;
186 // item[1]=200.;
187 // cPnt.push_back(item);
188 // item[0]=300.;
189 // item[1]=100.;
190 // cPnt.push_back(item);
191 // item[0]=200.;
192 // item[1]=150.;
193 // cPnt.push_back(item);
194 // item[0]=200.;
195 // item[1]=200.;
196 // cPnt.push_back(item);
197 
198 
199  bsplineQuadraticOpen.SetControlPoints(cPnt);
200  bsplineCubicOpen.SetControlPoints(cPnt);
201 
202 
203  //set controlpoints for simple closed bspline
204  //cPnt[n],cPnt[0],...,cPnt[n],cPnt[0] //quadratic case
205  //cPnt[n],cPnt[0],...,cPnt[n],cPnt[0],cPnt[1] //cubic case
206  cPnt.clear();
207  item[0]=200.;
208  item[1]=200.;
209  cPnt.push_back(item);
210  item[0]=200.;
211  item[1]=150.;
212  cPnt.push_back(item);
213  item[0]=100.;
214  item[1]=100.;
215  cPnt.push_back(item);
216  item[0]=100.;
217  item[1]=200.;
218  cPnt.push_back(item);
219  item[0]=200.;
220  item[1]=300.;
221  cPnt.push_back(item);
222  item[0]=200.;
223  item[1]=300.;
224  cPnt.push_back(item);
225  item[0]=300.;
226  item[1]=200.;
227  cPnt.push_back(item);
228  item[0]=300.;
229  item[1]=100.;
230  cPnt.push_back(item);
231  item[0]=200.;
232  item[1]=150.;
233  cPnt.push_back(item);
234  item[0]=200.;
235  item[1]=200.;
236  cPnt.push_back(item);
237  item[0]=200.;
238  item[1]=150.;
239  cPnt.push_back(item);
240  bsplineQuadraticClosed.SetControlPoints(cPnt);
241  item[0]=100.;
242  item[1]=100.;
243  cPnt.push_back(item);
244  bsplineCubicClosed.SetControlPoints(cPnt);
245 
246  //init bsplines (compute coefficient matrices for segments)
247  bsplineQuadraticOpen.InitQuadraticUniformBSpline();
248  bsplineQuadraticClosed.InitQuadraticUniformBSpline();
249  bsplineCubicOpen.InitCubicUniformBSpline();
250  bsplineCubicClosed.InitCubicUniformBSpline();
251 
252 
253  //draw open quadratic bspline into ExampleBSplineCurve0.mip
254  drawQuadratic(bsplineQuadraticOpen);
255 
256  //draw closed quadratic bspline into ExampleBSplineCurve1.mip
257  drawQuadratic(bsplineQuadraticClosed);
258 
259  //draw open cubic bspline into ExampleBSplineCurve2.mip
260  drawCubic(bsplineCubicOpen);
261 
262  //draw closed cubic bspline into ExampleBSplineCurve3.mip
263  drawCubic(bsplineCubicClosed);
264  return 0;
265 }
void InitQuadraticUniformBSpline()
computes coefficients for all segments of a quadratic uniform B-spline
void CubicUniformBSpline(Vector2< double > &res, double t)
computes the point on a cubic uniform B-spline curve at time t
static int Line(Image< StorageType > &im, const unsigned int start[2], const unsigned int end[2], const StorageType value[])
lines
Definition: ImageDraw.cpp:404
this class is intended for drawing of quadratic and cubic regular B-Splines from given control points...
Definition: BSplineCurve.hh:70
void FillImageWithConstValue(StorageType Value)
fill grey images
Definition: Image.cpp:456
void InitCubicUniformBSpline()
computes coefficients for all segments of a cubic uniform B-spline
void SetControlPoints(std::vector< BIAS::Vector2< double > > &cPnt)
set the control points, which control the interpolating curve
Definition: BSplineCurve.hh:80
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
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
void QuadraticUniformBSpline(Vector2< double > &res, double t)
computes the point on a quadratic uniform B-spline curve at time t