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