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