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