ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
BSpline.cpp
1 /****************************************************************************
2  *
3  * $Id: BSpline.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Exemple of a B-Spline curve.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
55 #include <visp/vpDebug.h>
56 
57 #include <visp/vpBSpline.h>
58 
59 #include <visp/vpImage.h>
60 #include <visp/vpImageIo.h>
61 #include <visp/vpImagePoint.h>
62 #include <visp/vpDisplayGTK.h>
63 #include <visp/vpDisplayGDI.h>
64 #include <visp/vpDisplayOpenCV.h>
65 #include <visp/vpDisplayD3D.h>
66 #include <visp/vpDisplayX.h> // Should be after #include <visp/vpDisplayOpenCV.h>
67 
68 #include <visp/vpParseArgv.h>
69 #include <visp/vpIoTools.h>
70 #include <cstdlib>
71 #include <stdlib.h>
72 
73 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_D3D9)
74 
75 // List of allowed command line options
76 #define GETOPTARGS "cdh"
77 
86 void usage(const char *name, const char *badparam)
87 {
88  fprintf(stdout, "\n\
89 Describe a curve thanks to a BSpline.\n\
90 \n\
91 SYNOPSIS\n\
92  %s [-c] [-d] [-h]\n", name);
93 
94  fprintf(stdout, "\n\
95 OPTIONS: Default\n\
96  -c\n\
97  Disable the mouse click. Useful to automaze the \n\
98  execution of this program without humain intervention.\n\
99 \n\
100  -d \n\
101  Turn off the display.\n\
102 \n\
103  -h\n\
104  Print the help.\n");
105 
106  if (badparam)
107  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
108 }
109 
110 
123 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
124 {
125  const char *optarg;
126  int c;
127  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
128 
129  switch (c) {
130  case 'c': click_allowed = false; break;
131  case 'd': display = false; break;
132  case 'h': usage(argv[0], NULL); return false; break;
133 
134  default:
135  usage(argv[0], optarg);
136  return false; break;
137  }
138  }
139 
140  if ((c == 1) || (c == -1)) {
141  // standalone param or error
142  usage(argv[0], NULL);
143  std::cerr << "ERROR: " << std::endl;
144  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
145  return false;
146  }
147 
148  return true;
149 }
150 
151 
152 int
153 main(int argc, const char ** argv)
154 {
155  bool opt_click_allowed = true;
156  bool opt_display = true;
157 
158  // Read the command line options
159  if (getOptions(argc, argv, opt_click_allowed,
160  opt_display) == false) {
161  exit (-1);
162  }
163 
164  // Declare an image, this is a gray level image (unsigned char)
165  // it size is not defined yet, it will be defined when the image will
166  // read on the disk
167  vpImage<unsigned char> I(540,480);
168 
169  // We open a window using either X11, GTK or GDI.
170 #if defined VISP_HAVE_X11
171  vpDisplayX display;
172 #elif defined VISP_HAVE_GTK
173  vpDisplayGTK display;
174 #elif defined VISP_HAVE_GDI
175  vpDisplayGDI display;
176 #elif defined VISP_HAVE_OPENCV
177  vpDisplayOpenCV display;
178 #elif defined VISP_HAVE_D3D9
179  vpDisplayD3D display;
180 #endif
181 
182  if (opt_display) {
183  try{
184  // Display size is automatically defined by the image (I) size
185  display.init(I, 100, 100,"Display image") ;
186  vpDisplay::display(I) ;
187  vpDisplay::flush(I) ;
188  }
189  catch(...)
190  {
191  vpERROR_TRACE("Error while displaying the image") ;
192  exit(-1);
193  }
194  }
195 
196  vpBSpline bSpline;
197  std::list<double> knots;
198  knots.push_back(0);
199  knots.push_back(0);
200  knots.push_back(0);
201  knots.push_back(1);
202  knots.push_back(2);
203  knots.push_back(3);
204  knots.push_back(4);
205  knots.push_back(4);
206  knots.push_back(5);
207  knots.push_back(5);
208  knots.push_back(5);
209 
210  std::list<vpImagePoint> controlPoints;
211  vpImagePoint pt;
212  pt.set_ij(50,300);
213  controlPoints.push_back(pt);
214  pt.set_ij(100,130);
215  controlPoints.push_back(pt);
216  pt.set_ij(150,400);
217  controlPoints.push_back(pt);
218  pt.set_ij(200,370);
219  controlPoints.push_back(pt);
220  pt.set_ij(250,120);
221  controlPoints.push_back(pt);
222  pt.set_ij(300,250);
223  controlPoints.push_back(pt);
224  pt.set_ij(350,200);
225  controlPoints.push_back(pt);
226  pt.set_ij(400,300);
227  controlPoints.push_back(pt);
228 
229  bSpline.set_p(2);
230  bSpline.set_knots(knots);
231  bSpline.set_controlPoints(controlPoints);
232 
233  std::cout << "The parameters are :" <<std::endl;
234  std::cout << "p : " << bSpline.get_p() <<std::endl;
235  std::cout << "" <<std::endl;
236  std::cout << "The knot vector :" <<std::endl;
237  std::list<double> knots_cur;
238  bSpline.get_knots(knots_cur);
239  unsigned int i_display=0;
240  for(std::list<double>::const_iterator it=knots_cur.begin(); it!=knots_cur.end(); ++it, ++i_display){
241  std::cout << i_display << " ---> " << *it << std::endl;
242  }
243  std::cout << "The control points are :" <<std::endl;
244  std::list<vpImagePoint> controlPoints_cur;
245  bSpline.get_controlPoints(controlPoints_cur);
246  i_display=0;
247  for(std::list<vpImagePoint>::const_iterator it=controlPoints_cur.begin(); it!=controlPoints_cur.end(); ++it, ++i_display){
248  std::cout << i_display << " ---> " << *it << std::endl;
249  }
250 
251  unsigned int i = bSpline.findSpan(5/2.0);
252  std::cout << "The knot interval number for the value u = 5/2 is : " << i <<std::endl;
253 
254  vpBasisFunction *N = NULL;
255  N = bSpline.computeBasisFuns(5/2.0);
256  std::cout << "The nonvanishing basis functions N(u=5/2) are :" << std::endl;
257  for (unsigned int j = 0; j < bSpline.get_p()+1; j++)
258  std::cout << N[j].value << std::endl;
259 
260  vpBasisFunction **N2 = NULL;
261  N2 = bSpline.computeDersBasisFuns(5/2.0, 2);
262  std::cout << "The first derivatives of the basis functions N'(u=5/2) are :" << std::endl;
263  for (unsigned int j = 0; j < bSpline.get_p()+1; j++)
264  std::cout << N2[1][j].value << std::endl;
265 
266  std::cout << "The second derivatives of the basis functions N''(u=5/2) are :" << std::endl;
267  for (unsigned int j = 0; j < bSpline.get_p()+1; j++)
268  std::cout << N2[2][j].value << std::endl;
269 
270  if (opt_display && opt_click_allowed)
271  {
272  double u = 0.0;
273  vpImagePoint pt;
274  while (u <= 5)
275  {
276  pt = bSpline.computeCurvePoint(u);
278  u+=0.01;
279  }
280  for(std::list<vpImagePoint>::const_iterator it=controlPoints.begin(); it!= controlPoints.end(); ++it){
282  }
283  vpDisplay::flush(I) ;
285  }
286 
287  if (N != NULL) delete[] N;
288  if (N2 != NULL)
289  {
290  for (unsigned int j = 0; j <= 2; j++)
291  delete[] N2[j];
292  delete[] N2;
293  }
294 
295  return 0;
296 }
297 
298 #else
299 int main()
300 {
301  std::cout << "This example requires a video device. "
302  << std::endl
303  << "You should install X11, GTK, OpenCV, GDI or Direct3D"
304  << std::endl
305  << "to be able to execute this example."
306  << std::endl;
307  return 0;
308 }
309 #endif
void get_controlPoints(std::list< vpImagePoint > &list) const
Definition: vpBSpline.h:139
#define vpERROR_TRACE
Definition: vpDebug.h:379
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void set_p(unsigned int p)
Definition: vpBSpline.h:173
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static const vpColor red
Definition: vpColor.h:167
static vpBasisFunction ** computeDersBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, unsigned int l_der, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:230
static unsigned int findSpan(double l_u, unsigned int l_p, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:89
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
void set_ij(const double i, const double j)
Definition: vpImagePoint.h:167
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
static vpBasisFunction * computeBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:149
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
unsigned int get_p() const
Definition: vpBSpline.h:132
void set_controlPoints(const std::list< vpImagePoint > &list)
Definition: vpBSpline.h:181
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
Class that provides tools to compute and manipulate a B-Spline curve.
Definition: vpBSpline.h:109
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
void get_knots(std::list< double > &list) const
Definition: vpBSpline.h:150
void set_knots(const std::list< double > &list)
Definition: vpBSpline.h:193
static vpImagePoint computeCurvePoint(double l_u, unsigned int l_i, unsigned int l_p, std::vector< double > &l_knots, std::vector< vpImagePoint > &l_controlPoints)
Definition: vpBSpline.cpp:384