ViSP  2.9.0
BSpline.cpp
1 /****************************************************************************
2  *
3  * $Id: BSpline.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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 
78 void usage(const char *name, const char *badparam);
79 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
80 
89 void usage(const char *name, const char *badparam)
90 {
91  fprintf(stdout, "\n\
92 Describe a curve thanks to a BSpline.\n\
93 \n\
94 SYNOPSIS\n\
95  %s [-c] [-d] [-h]\n", name);
96 
97  fprintf(stdout, "\n\
98 OPTIONS: Default\n\
99  -c\n\
100  Disable the mouse click. Useful to automaze the \n\
101  execution of this program without humain intervention.\n\
102 \n\
103  -d \n\
104  Turn off the display.\n\
105 \n\
106  -h\n\
107  Print the help.\n");
108 
109  if (badparam)
110  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
111 }
112 
113 
126 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
127 {
128  const char *optarg_;
129  int c;
130  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
131 
132  switch (c) {
133  case 'c': click_allowed = false; break;
134  case 'd': display = false; break;
135  case 'h': usage(argv[0], NULL); return false; break;
136 
137  default:
138  usage(argv[0], optarg_);
139  return false; break;
140  }
141  }
142 
143  if ((c == 1) || (c == -1)) {
144  // standalone param or error
145  usage(argv[0], NULL);
146  std::cerr << "ERROR: " << std::endl;
147  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
148  return false;
149  }
150 
151  return true;
152 }
153 
154 
155 int
156 main(int argc, const char ** argv)
157 {
158  try {
159  bool opt_click_allowed = true;
160  bool opt_display = true;
161 
162  // Read the command line options
163  if (getOptions(argc, argv, opt_click_allowed,
164  opt_display) == false) {
165  exit (-1);
166  }
167 
168  // Declare an image, this is a gray level image (unsigned char)
169  // it size is not defined yet, it will be defined when the image will
170  // read on the disk
171  vpImage<unsigned char> I(540,480);
172 
173  // We open a window using either X11, GTK or GDI.
174 #if defined VISP_HAVE_X11
175  vpDisplayX display;
176 #elif defined VISP_HAVE_GTK
177  vpDisplayGTK display;
178 #elif defined VISP_HAVE_GDI
179  vpDisplayGDI display;
180 #elif defined VISP_HAVE_OPENCV
181  vpDisplayOpenCV display;
182 #elif defined VISP_HAVE_D3D9
183  vpDisplayD3D display;
184 #endif
185 
186  if (opt_display) {
187  // Display size is automatically defined by the image (I) size
188  display.init(I, 100, 100,"Display image") ;
189  vpDisplay::display(I) ;
190  vpDisplay::flush(I) ;
191  }
192 
193  vpBSpline bSpline;
194  std::list<double> knots;
195  knots.push_back(0);
196  knots.push_back(0);
197  knots.push_back(0);
198  knots.push_back(1);
199  knots.push_back(2);
200  knots.push_back(3);
201  knots.push_back(4);
202  knots.push_back(4);
203  knots.push_back(5);
204  knots.push_back(5);
205  knots.push_back(5);
206 
207  std::list<vpImagePoint> controlPoints;
208  vpImagePoint pt;
209  pt.set_ij(50,300);
210  controlPoints.push_back(pt);
211  pt.set_ij(100,130);
212  controlPoints.push_back(pt);
213  pt.set_ij(150,400);
214  controlPoints.push_back(pt);
215  pt.set_ij(200,370);
216  controlPoints.push_back(pt);
217  pt.set_ij(250,120);
218  controlPoints.push_back(pt);
219  pt.set_ij(300,250);
220  controlPoints.push_back(pt);
221  pt.set_ij(350,200);
222  controlPoints.push_back(pt);
223  pt.set_ij(400,300);
224  controlPoints.push_back(pt);
225 
226  bSpline.set_p(2);
227  bSpline.set_knots(knots);
228  bSpline.set_controlPoints(controlPoints);
229 
230  std::cout << "The parameters are :" <<std::endl;
231  std::cout << "p : " << bSpline.get_p() <<std::endl;
232  std::cout << "" <<std::endl;
233  std::cout << "The knot vector :" <<std::endl;
234  std::list<double> knots_cur;
235  bSpline.get_knots(knots_cur);
236  unsigned int i_display=0;
237  for(std::list<double>::const_iterator it=knots_cur.begin(); it!=knots_cur.end(); ++it, ++i_display){
238  std::cout << i_display << " ---> " << *it << std::endl;
239  }
240  std::cout << "The control points are :" <<std::endl;
241  std::list<vpImagePoint> controlPoints_cur;
242  bSpline.get_controlPoints(controlPoints_cur);
243  i_display=0;
244  for(std::list<vpImagePoint>::const_iterator it=controlPoints_cur.begin(); it!=controlPoints_cur.end(); ++it, ++i_display){
245  std::cout << i_display << " ---> " << *it << std::endl;
246  }
247 
248  unsigned int i = bSpline.findSpan(5/2.0);
249  std::cout << "The knot interval number for the value u = 5/2 is : " << i <<std::endl;
250 
251  vpBasisFunction *N = NULL;
252  N = bSpline.computeBasisFuns(5/2.0);
253  std::cout << "The nonvanishing basis functions N(u=5/2) are :" << std::endl;
254  for (unsigned int j = 0; j < bSpline.get_p()+1; j++)
255  std::cout << N[j].value << std::endl;
256 
257  vpBasisFunction **N2 = NULL;
258  N2 = bSpline.computeDersBasisFuns(5/2.0, 2);
259  std::cout << "The first derivatives of the basis functions N'(u=5/2) are :" << std::endl;
260  for (unsigned int j = 0; j < bSpline.get_p()+1; j++)
261  std::cout << N2[1][j].value << std::endl;
262 
263  std::cout << "The second derivatives of the basis functions N''(u=5/2) are :" << std::endl;
264  for (unsigned int j = 0; j < bSpline.get_p()+1; j++)
265  std::cout << N2[2][j].value << std::endl;
266 
267  if (opt_display && opt_click_allowed)
268  {
269  double u = 0.0;
270  while (u <= 5)
271  {
272  pt = bSpline.computeCurvePoint(u);
274  u+=0.01;
275  }
276  for(std::list<vpImagePoint>::const_iterator it=controlPoints.begin(); it!= controlPoints.end(); ++it){
278  }
279  vpDisplay::flush(I) ;
281  }
282 
283  if (N != NULL) delete[] N;
284  if (N2 != NULL)
285  {
286  for (unsigned int j = 0; j <= 2; j++)
287  delete[] N2[j];
288  delete[] N2;
289  }
290 
291  return 0;
292  }
293  catch(vpException e) {
294  std::cout << "Catch an exception: " << e << std::endl;
295  return 1;
296  }
297 }
298 
299 #else
300 int main()
301 {
302  std::cout << "This example requires a video device. "
303  << std::endl
304  << "You should install X11, GTK, OpenCV, GDI or Direct3D"
305  << std::endl
306  << "to be able to execute this example."
307  << std::endl;
308  return 0;
309 }
310 #endif
void get_controlPoints(std::list< vpImagePoint > &list) const
Definition: vpBSpline.h:139
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
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:233
static unsigned int findSpan(double l_u, unsigned int l_p, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:92
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
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:152
The vpDisplayOpenCV allows to display image using the opencv library.
void set_p(unsigned int degree)
Definition: vpBSpline.h:173
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
void set_ij(const double ii, const double jj)
Definition: vpImagePoint.h:180
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:387