ViSP  2.8.0
Nurbs.cpp
1 /****************************************************************************
2  *
3  * $Id: Nurbs.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 Nurbs curve.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
55 #include <visp/vpDebug.h>
56 
57 #include <visp/vpNurbs.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> // Should be after #include <visp/vpDisplayOpenCV.h>
65 #include <visp/vpDisplayD3D.h>
66 #include <visp/vpDisplayX.h>
67 
68 #include <visp/vpParseArgv.h>
69 #include <visp/vpIoTools.h>
70 #include <cstdlib>
71 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_D3D9)
72 
73 // List of allowed command line options
74 #define GETOPTARGS "cdh"
75 
76 
85 void usage(const char *name, const char *badparam)
86 {
87  fprintf(stdout, "\n\
88 Describe a curve thanks to a Nurbs.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-c] [-d] [-h]\n", name);
92 
93  fprintf(stdout, "\n\
94 OPTIONS: Default\n\
95  -c\n\
96  Disable the mouse click. Useful to automaze the \n\
97  execution of this program without humain intervention.\n\
98 \n\
99  -d \n\
100  Turn off the display.\n\
101 \n\
102  -h\n\
103  Print the help.\n");
104 
105  if (badparam)
106  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
107 }
108 
109 
122 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
123 {
124  const char *optarg;
125  int c;
126  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
127 
128  switch (c) {
129  case 'c': click_allowed = false; break;
130  case 'd': display = false; break;
131  case 'h': usage(argv[0], NULL); return false; break;
132 
133  default:
134  usage(argv[0], optarg);
135  return false; break;
136  }
137  }
138 
139  if ((c == 1) || (c == -1)) {
140  // standalone param or error
141  usage(argv[0], NULL);
142  std::cerr << "ERROR: " << std::endl;
143  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
144  return false;
145  }
146 
147  return true;
148 }
149 
150 
151 int
152 main(int argc, const char ** argv)
153 {
154  bool opt_click_allowed = true;
155  bool opt_display = true;
156 
157  // Read the command line options
158  if (getOptions(argc, argv, opt_click_allowed,
159  opt_display) == false) {
160  exit (-1);
161  }
162 
163  // Declare an image, this is a gray level image (unsigned char)
164  // it size is not defined yet, it will be defined when the image will
165  // read on the disk
166  vpImage<unsigned char> I(540,480);
167  vpImage<unsigned char> I2(540,480);
168  vpImage<unsigned char> I3(540,480);
169 
170  // We open a window using either X11, GTK or GDI.
171 #if defined VISP_HAVE_X11
172  vpDisplayX display[3];
173 #elif defined VISP_HAVE_GDI
174  vpDisplayGDI display[3];
175 #elif defined VISP_HAVE_GTK
176  vpDisplayGTK display[3];
177 #elif defined VISP_HAVE_OPENCV
178  vpDisplayOpenCV display[3];
179 #endif
180 
181  if (opt_display) {
182  try{
183  // Display size is automatically defined by the image (I) size
184  display[0].init(I, 100, 100,"Points as control points") ;
185  vpDisplay::display(I) ;
186  vpDisplay::flush(I) ;
187 
188  }
189  catch(...)
190  {
191  vpERROR_TRACE("Error while displaying the image") ;
192  exit(-1);
193  }
194  }
195 
196  vpNurbs Nurbs;
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  std::list<double> weights;
212  vpImagePoint pt;
213  pt.set_ij(50,300);
214  controlPoints.push_back(pt);
215  weights.push_back(1);
216  pt.set_ij(100,130);
217  controlPoints.push_back(pt);
218  weights.push_back(5);
219  pt.set_ij(150,400);
220  controlPoints.push_back(pt);
221  weights.push_back(0.2);
222  pt.set_ij(200,370);
223  controlPoints.push_back(pt);
224  weights.push_back(10);
225  pt.set_ij(250,120);
226  controlPoints.push_back(pt);
227  weights.push_back(1);
228  pt.set_ij(300,250);
229  controlPoints.push_back(pt);
230  weights.push_back(2);
231  pt.set_ij(350,200);
232  controlPoints.push_back(pt);
233  weights.push_back(3);
234  pt.set_ij(400,300);
235  controlPoints.push_back(pt);
236  weights.push_back(1);
237 
238  Nurbs.set_p(2);
239  Nurbs.set_knots(knots);
240  Nurbs.set_controlPoints(controlPoints);
241  Nurbs.set_weights(weights);
242 
243  std::cout << "The parameters are :" <<std::endl;
244  std::cout << "p : " << Nurbs.get_p() <<std::endl;
245  std::cout << "" <<std::endl;
246  std::cout << "The knot vector :" <<std::endl;
247  std::list<double> knots_cur;
248  Nurbs.get_knots(knots_cur);
249  unsigned int i_display=0;
250  for(std::list<double>::const_iterator it=knots_cur.begin(); it!=knots_cur.end(); ++it, ++i_display){
251  std::cout << i_display << " ---> " << *it << std::endl;
252  }
253  std::cout << "The control points are :" <<std::endl;
254  std::list<vpImagePoint> controlPoints_cur;
255  Nurbs.get_controlPoints(controlPoints_cur);
256  i_display=0;
257  for(std::list<vpImagePoint>::const_iterator it=controlPoints_cur.begin(); it!=controlPoints_cur.end(); ++it, ++i_display){
258  std::cout << i_display << " ---> " << *it << std::endl;
259  }
260  std::cout << "The associated weights are :" <<std::endl;
261  std::list<double> weights_cur;
262  Nurbs.get_weights(weights_cur);
263  i_display=0;
264  for(std::list<double>::const_iterator it=weights_cur.begin(); it!=weights_cur.end(); ++it, ++i_display){
265  std::cout << i_display << " ---> " << *it << std::endl;
266  }
267 
268  unsigned int i = Nurbs.findSpan(5/2.0);
269  std::cout << "The knot interval number for the value u = 5/2 is : " << i <<std::endl;
270 
271  vpBasisFunction *N = NULL;
272  N = Nurbs.computeBasisFuns(5/2.0);
273  std::cout << "The nonvanishing basis functions N(u=5/2) are :" << std::endl;
274  for (unsigned int j = 0; j < Nurbs.get_p()+1; j++)
275  std::cout << N[j].value << std::endl;
276 
277  vpBasisFunction **N2 = NULL;
278  N2 = Nurbs.computeDersBasisFuns(5/2.0, 2);
279  std::cout << "The first derivatives of the basis functions N'(u=5/2) are :" << std::endl;
280  for (unsigned int j = 0; j < Nurbs.get_p()+1; j++)
281  std::cout << N2[1][j].value << std::endl;
282 
283  std::cout << "The second derivatives of the basis functions N''(u=5/2) are :" << std::endl;
284  for (unsigned int j = 0; j < Nurbs.get_p()+1; j++)
285  std::cout << N2[2][j].value << std::endl;
286 
287  if (opt_display && opt_click_allowed)
288  {
289  double u = 0.0;
290  vpImagePoint pt;
291  while (u <= 5)
292  {
293  pt = Nurbs.computeCurvePoint(u);
295  u+=0.01;
296  }
297  for(std::list<vpImagePoint>::const_iterator it=controlPoints.begin(); it!=controlPoints.end(); ++it){
299  }
300 
301  vpDisplay::flush(I) ;
303  }
304 
305  if (opt_display) {
306  try{
307  // Display size is automatically defined by the image (I) size
308  display[1].init(I2, 100, 100,"Points interpolation") ;
309  vpDisplay::display(I2) ;
310  vpDisplay::flush(I2) ;
311  }
312  catch(...)
313  {
314  vpERROR_TRACE("Error while displaying the image") ;
315  exit(-1);
316  }
317  }
318 
319  Nurbs.globalCurveInterp(controlPoints);
320 
321  if (opt_display && opt_click_allowed)
322  {
323  double u = 0.0;
324  vpImagePoint pt;
325  while (u <= 1)
326  {
327  pt = Nurbs.computeCurvePoint(u);
329  u+=0.01;
330  }
331 
332  for(std::list<vpImagePoint>::const_iterator it=controlPoints.begin(); it!=controlPoints.end(); ++it){
334  }
335  vpDisplay::flush(I2) ;
337  }
338 
339 
340  if (opt_display) {
341  try{
342  // Display size is automatically defined by the image (I) size
343  display[2].init(I3, 100, 100,"Points approximation") ;
344  vpDisplay::display(I3) ;
345  vpDisplay::flush(I3) ;
346  }
347  catch(...)
348  {
349  vpERROR_TRACE("Error while displaying the image") ;
350  exit(-1);
351  }
352  }
353 
354  Nurbs.globalCurveApprox(controlPoints,5);
355 
356  if (opt_display && opt_click_allowed)
357  {
358  double u = 0.0;
359  vpImagePoint pt;
360  while (u <= 1)
361  {
362  pt = Nurbs.computeCurvePoint(u);
364  u+=0.01;
365  }
366 
367  for(std::list<vpImagePoint>::const_iterator it=controlPoints.begin(); it!=controlPoints.end(); ++it){
369  }
370 
371  vpDisplay::flush(I3) ;
373  }
374 
375  if (N != NULL) delete[] N;
376  if (N2 != NULL)
377  {
378  for (int j = 0; j <= 2; j++)
379  delete[] N2[j];
380  delete[] N2;
381  }
382 
383  return 0;
384 }
385 
386 #else
387 int main()
388 {
389  std::cout << "This example requires a video device. "
390  << std::endl
391  << "You should install X11, GTK, OpenCV, GDI or Direct3D"
392  << std::endl
393  << "to be able to execute this example."
394  << std::endl;
395  return 0;
396 }
397 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void get_controlPoints(std::list< vpImagePoint > &list) const
Definition: vpBSpline.h:139
static vpImagePoint computeCurvePoint(double l_u, unsigned int l_i, unsigned int l_p, std::vector< double > &l_knots, std::vector< vpImagePoint > &l_controlPoints, std::vector< double > &l_weights)
Definition: vpNurbs.cpp:99
#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
static void globalCurveInterp(std::vector< vpImagePoint > &l_crossingPoints, unsigned int l_p, std::vector< double > &l_knots, std::vector< vpImagePoint > &l_controlPoints, std::vector< double > &l_weights)
Definition: vpNurbs.cpp:699
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
void get_weights(std::list< double > &list) const
Definition: vpNurbs.h:110
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
static void globalCurveApprox(std::vector< vpImagePoint > &l_crossingPoints, unsigned int l_p, unsigned int l_n, std::vector< double > &l_knots, std::vector< vpImagePoint > &l_controlPoints, std::vector< double > &l_weights)
Definition: vpNurbs.cpp:869
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 set_weights(const std::list< double > &list)
Definition: vpNurbs.h:121
void get_knots(std::list< double > &list) const
Definition: vpBSpline.h:150
void set_knots(const std::list< double > &list)
Definition: vpBSpline.h:193
Class that provides tools to compute and manipulate a Non Uniform Rational B-Spline curve...
Definition: vpNurbs.h:89