Visual Servoing Platform  version 3.6.1 under development (2024-05-08)
BSpline.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  * Example of a B-Spline curve.
33  *
34 *****************************************************************************/
47 #include <visp3/core/vpDebug.h>
48 
49 #include <visp3/core/vpBSpline.h>
50 
51 #include <visp3/core/vpDisplay.h>
52 #include <visp3/core/vpImage.h>
53 #include <visp3/core/vpImagePoint.h>
54 #include <visp3/io/vpImageIo.h>
55 #ifdef VISP_HAVE_MODULE_GUI
56 #include <visp3/gui/vpDisplayD3D.h>
57 #include <visp3/gui/vpDisplayGDI.h>
58 #include <visp3/gui/vpDisplayGTK.h>
59 #include <visp3/gui/vpDisplayOpenCV.h>
60 #include <visp3/gui/vpDisplayX.h> // Should be after #include <visp3/gui/vpDisplayOpenCV.h>
61 #endif
62 
63 #include <cstdlib>
64 #include <stdlib.h>
65 #include <visp3/core/vpIoTools.h>
66 #include <visp3/io/vpParseArgv.h>
67 
68 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || \
69  defined(VISP_HAVE_D3D9)
70 
71  // List of allowed command line options
72 #define GETOPTARGS "cdh"
73 
74 void usage(const char *name, const char *badparam);
75 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
76 
85 void usage(const char *name, const char *badparam)
86 {
87  fprintf(stdout, "\n\
88 Describe a curve thanks to a BSpline.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-c] [-d] [-h]\n",
92  name);
93 
94  fprintf(stdout, "\n\
95 OPTIONS: Default\n\
96  -c\n\
97  Disable the mouse click. Useful to automate the \n\
98  execution of this program without human intervention.\n\
99 \n\
100  -d \n\
101  Turn off the display.\n\
102 \n\
103  -h\n\
104  Print the help.\n");
105 
106  if (badparam)
107  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
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':
130  click_allowed = false;
131  break;
132  case 'd':
133  display = false;
134  break;
135  case 'h':
136  usage(argv[0], nullptr);
137  return false;
138  break;
139 
140  default:
141  usage(argv[0], optarg_);
142  return false;
143  break;
144  }
145  }
146 
147  if ((c == 1) || (c == -1)) {
148  // standalone param or error
149  usage(argv[0], nullptr);
150  std::cerr << "ERROR: " << std::endl;
151  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
152  return false;
153  }
154 
155  return true;
156 }
157 
158 int main(int argc, const char **argv)
159 {
160  try {
161  bool opt_click_allowed = true;
162  bool opt_display = true;
163 
164  // Read the command line options
165  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
166  return EXIT_FAILURE;
167  }
168 
169  // Declare an image, this is a gray level image (unsigned char)
170  // it size is not defined yet, it will be defined when the image will
171  // read on the disk
172  vpImage<unsigned char> I(540, 480);
173 
174  // We open a window using either X11, GTK or GDI.
175 
176 #ifdef VISP_HAVE_MODULE_GUI
177 #if defined(VISP_HAVE_X11)
179 #elif defined(VISP_HAVE_GTK)
181 #elif defined(VISP_HAVE_GDI)
183 #elif defined(HAVE_OPENCV_HIGHGUI)
185 #elif defined(VISP_HAVE_D3D9)
187 #endif
188 
189  if (opt_display) {
190  // Display size is automatically defined by the image (I) size
191  display.init(I, 100, 100, "Display image");
193  vpDisplay::flush(I);
194  }
195 #endif
196 
197  vpBSpline bSpline;
198  std::list<double> knots;
199  knots.push_back(0);
200  knots.push_back(0);
201  knots.push_back(0);
202  knots.push_back(1);
203  knots.push_back(2);
204  knots.push_back(3);
205  knots.push_back(4);
206  knots.push_back(4);
207  knots.push_back(5);
208  knots.push_back(5);
209  knots.push_back(5);
210 
211  std::list<vpImagePoint> controlPoints;
212  vpImagePoint pt;
213  pt.set_ij(50, 300);
214  controlPoints.push_back(pt);
215  pt.set_ij(100, 130);
216  controlPoints.push_back(pt);
217  pt.set_ij(150, 400);
218  controlPoints.push_back(pt);
219  pt.set_ij(200, 370);
220  controlPoints.push_back(pt);
221  pt.set_ij(250, 120);
222  controlPoints.push_back(pt);
223  pt.set_ij(300, 250);
224  controlPoints.push_back(pt);
225  pt.set_ij(350, 200);
226  controlPoints.push_back(pt);
227  pt.set_ij(400, 300);
228  controlPoints.push_back(pt);
229 
230  bSpline.set_p(2);
231  bSpline.set_knots(knots);
232  bSpline.set_controlPoints(controlPoints);
233 
234  std::cout << "The parameters are :" << std::endl;
235  std::cout << "p : " << bSpline.get_p() << std::endl;
236  std::cout << "" << std::endl;
237  std::cout << "The knot vector :" << std::endl;
238  std::list<double> knots_cur;
239  bSpline.get_knots(knots_cur);
240  unsigned int i_display = 0;
241  for (std::list<double>::const_iterator it = knots_cur.begin(); it != knots_cur.end(); ++it, ++i_display) {
242  std::cout << i_display << " ---> " << *it << std::endl;
243  }
244  std::cout << "The control points are :" << std::endl;
245  std::list<vpImagePoint> controlPoints_cur;
246  bSpline.get_controlPoints(controlPoints_cur);
247  i_display = 0;
248  for (std::list<vpImagePoint>::const_iterator it = controlPoints_cur.begin(); it != controlPoints_cur.end();
249  ++it, ++i_display) {
250  std::cout << i_display << " ---> " << *it << std::endl;
251  }
252 
253  unsigned int i = bSpline.findSpan(5 / 2.0);
254  std::cout << "The knot interval number for the value u = 5/2 is : " << i << std::endl;
255 
256  vpBasisFunction *N = nullptr;
257  N = bSpline.computeBasisFuns(5 / 2.0);
258  std::cout << "The nonvanishing basis functions N(u=5/2) are :" << std::endl;
259  for (unsigned int j = 0; j < bSpline.get_p() + 1; j++)
260  std::cout << N[j].value << std::endl;
261 
262  vpBasisFunction **N2 = nullptr;
263  N2 = bSpline.computeDersBasisFuns(5 / 2.0, 2);
264  std::cout << "The first derivatives of the basis functions N'(u=5/2) are :" << std::endl;
265  for (unsigned int j = 0; j < bSpline.get_p() + 1; j++)
266  std::cout << N2[1][j].value << std::endl;
267 
268  std::cout << "The second derivatives of the basis functions N''(u=5/2) are :" << std::endl;
269  for (unsigned int j = 0; j < bSpline.get_p() + 1; j++)
270  std::cout << N2[2][j].value << std::endl;
271 
272  if (opt_display && opt_click_allowed) {
273  double u = 0.0;
274  while (u <= 5) {
275  pt = bSpline.computeCurvePoint(u);
277  u += 0.01;
278  }
279  for (std::list<vpImagePoint>::const_iterator it = controlPoints.begin(); it != controlPoints.end(); ++it) {
281  }
282  vpDisplay::flush(I);
284  }
285 
286  if (N != nullptr)
287  delete [] N;
288  if (N2 != nullptr) {
289  for (unsigned int j = 0; j <= 2; j++)
290  delete [] N2[j];
291  delete [] N2;
292  }
293 
294  return EXIT_SUCCESS;
295  }
296  catch (const vpException &e) {
297  std::cout << "Catch an exception: " << e << std::endl;
298  return EXIT_FAILURE;
299  }
300 }
301 
302 #else
303 int main()
304 {
305  std::cout
306  << "You do not have X11, GTK, or OpenCV, or GDI (Graphical Device Interface) functionalities to display images..."
307  << std::endl;
308  std::cout << "Tip if you are on a unix-like system:" << std::endl;
309  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
310  std::cout << "Tip if you are on a windows-like system:" << std::endl;
311  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
312  return EXIT_SUCCESS;
313 }
314 #endif
Class that provides tools to compute and manipulate a B-Spline curve.
Definition: vpBSpline.h:107
static vpImagePoint computeCurvePoint(double l_u, unsigned int l_i, unsigned int l_p, const std::vector< double > &l_knots, const std::vector< vpImagePoint > &l_controlPoints)
Definition: vpBSpline.cpp:375
void get_controlPoints(std::list< vpImagePoint > &list) const
Definition: vpBSpline.h:136
void set_p(unsigned int degree)
Definition: vpBSpline.h:176
static unsigned int findSpan(double l_u, unsigned int l_p, const std::vector< double > &l_knots)
Definition: vpBSpline.cpp:79
unsigned int get_p() const
Definition: vpBSpline.h:128
static vpBasisFunction ** computeDersBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, unsigned int l_der, const std::vector< double > &l_knots)
Definition: vpBSpline.cpp:228
void set_controlPoints(const std::list< vpImagePoint > &list)
Definition: vpBSpline.h:183
void get_knots(std::list< double > &list) const
Definition: vpBSpline.h:149
void set_knots(const std::list< double > &list)
Definition: vpBSpline.h:196
static vpBasisFunction * computeBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, const std::vector< double > &l_knots)
Definition: vpBSpline.cpp:142
static const vpColor red
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:214
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:101
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_ij(double ii, double jj)
Definition: vpImagePoint.h:315
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.