Visual Servoing Platform version 3.5.0
testNurbs.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 Nurbs curve.
33 *
34 * Authors:
35 * Nicolas Melchior
36 *
37 *****************************************************************************/
44#include <visp3/core/vpDebug.h>
45
46#include <visp3/me/vpNurbs.h>
47
48#include <visp3/core/vpImage.h>
49#include <visp3/core/vpImagePoint.h>
50#include <visp3/io/vpImageIo.h>
51#ifdef VISP_HAVE_MODULE_GUI
52#include <visp3/gui/vpDisplayD3D.h>
53#include <visp3/gui/vpDisplayGDI.h>
54#include <visp3/gui/vpDisplayGTK.h>
55#include <visp3/gui/vpDisplayOpenCV.h>
56#include <visp3/gui/vpDisplayX.h>
57#endif
58
59#include <cstdlib>
60#include <visp3/core/vpIoTools.h>
61#include <visp3/io/vpParseArgv.h>
62#if defined(VISP_HAVE_DISPLAY) \
63 && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
64
65// List of allowed command line options
66#define GETOPTARGS "cdh"
67
68void usage(const char *name, const char *badparam);
69bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
70
79void usage(const char *name, const char *badparam)
80{
81 fprintf(stdout, "\n\
82Describe a curve thanks to a Nurbs.\n\
83\n\
84SYNOPSIS\n\
85 %s [-c] [-d] [-h]\n", name);
86
87 fprintf(stdout, "\n\
88OPTIONS: Default\n\
89 -c\n\
90 Disable the mouse click. Useful to automaze the \n\
91 execution of this program without humain intervention.\n\
92\n\
93 -d \n\
94 Turn off the display.\n\
95\n\
96 -h\n\
97 Print the help.\n");
98
99 if (badparam)
100 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
101}
102
115bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
116{
117 const char *optarg_;
118 int c;
119 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
120
121 switch (c) {
122 case 'c':
123 click_allowed = false;
124 break;
125 case 'd':
126 display = false;
127 break;
128 case 'h':
129 usage(argv[0], NULL);
130 return false;
131 break;
132
133 default:
134 usage(argv[0], optarg_);
135 return false;
136 break;
137 }
138 }
139
140 if ((c == 1) || (c == -1)) {
141 // standalone param or error
142 usage(argv[0], NULL);
143 std::cerr << "ERROR: " << std::endl;
144 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
145 return false;
146 }
147
148 return true;
149}
150
151int main(int argc, const char **argv)
152{
153 try {
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, opt_display) == false) {
159 exit(-1);
160 }
161
162 // Declare an image, this is a gray level image (unsigned char)
163 // it size is not defined yet, it will be defined when the image will
164 // read on the disk
165 vpImage<unsigned char> I(540, 480);
166 vpImage<unsigned char> I2(540, 480);
167 vpImage<unsigned char> I3(540, 480);
168
169// We open a window using either X11, GTK or GDI.
170#if defined VISP_HAVE_X11
171 vpDisplayX display[3];
172#elif defined VISP_HAVE_GDI
173 vpDisplayGDI display[3];
174#elif defined VISP_HAVE_GTK
175 vpDisplayGTK display[3];
176#elif defined VISP_HAVE_OPENCV
177 vpDisplayOpenCV display[3];
178#endif
179
180 if (opt_display) {
181 // Display size is automatically defined by the image (I) size
182 display[0].init(I, 100, 100, "Points as control points");
185 }
186
187 vpNurbs Nurbs;
188 std::list<double> knots;
189 knots.push_back(0);
190 knots.push_back(0);
191 knots.push_back(0);
192 knots.push_back(1);
193 knots.push_back(2);
194 knots.push_back(3);
195 knots.push_back(4);
196 knots.push_back(4);
197 knots.push_back(5);
198 knots.push_back(5);
199 knots.push_back(5);
200
201 std::list<vpImagePoint> controlPoints;
202 std::list<double> weights;
203 vpImagePoint pt;
204 pt.set_ij(50, 300);
205 controlPoints.push_back(pt);
206 weights.push_back(1);
207 pt.set_ij(100, 130);
208 controlPoints.push_back(pt);
209 weights.push_back(5);
210 pt.set_ij(150, 400);
211 controlPoints.push_back(pt);
212 weights.push_back(0.2);
213 pt.set_ij(200, 370);
214 controlPoints.push_back(pt);
215 weights.push_back(10);
216 pt.set_ij(250, 120);
217 controlPoints.push_back(pt);
218 weights.push_back(1);
219 pt.set_ij(300, 250);
220 controlPoints.push_back(pt);
221 weights.push_back(2);
222 pt.set_ij(350, 200);
223 controlPoints.push_back(pt);
224 weights.push_back(3);
225 pt.set_ij(400, 300);
226 controlPoints.push_back(pt);
227 weights.push_back(1);
228
229 Nurbs.set_p(2);
230 Nurbs.set_knots(knots);
231 Nurbs.set_controlPoints(controlPoints);
232 Nurbs.set_weights(weights);
233
234 std::cout << "The parameters are :" << std::endl;
235 std::cout << "p : " << Nurbs.get_p() << std::endl;
236 std::cout << "" << std::endl;
237 std::cout << "The knot vector :" << std::endl;
238 std::list<double> knots_cur;
239 Nurbs.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 Nurbs.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 std::cout << "The associated weights are :" << std::endl;
253 std::list<double> weights_cur;
254 Nurbs.get_weights(weights_cur);
255 i_display = 0;
256 for (std::list<double>::const_iterator it = weights_cur.begin(); it != weights_cur.end(); ++it, ++i_display) {
257 std::cout << i_display << " ---> " << *it << std::endl;
258 }
259
260 unsigned int i = Nurbs.findSpan(5 / 2.0);
261 std::cout << "The knot interval number for the value u = 5/2 is : " << i << std::endl;
262
263 vpBasisFunction *N = NULL;
264 N = Nurbs.computeBasisFuns(5 / 2.0);
265 std::cout << "The nonvanishing basis functions N(u=5/2) are :" << std::endl;
266 for (unsigned int j = 0; j < Nurbs.get_p() + 1; j++)
267 std::cout << N[j].value << std::endl;
268
269 vpBasisFunction **N2 = NULL;
270 N2 = Nurbs.computeDersBasisFuns(5 / 2.0, 2);
271 std::cout << "The first derivatives of the basis functions N'(u=5/2) are :" << std::endl;
272 for (unsigned int j = 0; j < Nurbs.get_p() + 1; j++)
273 std::cout << N2[1][j].value << std::endl;
274
275 std::cout << "The second derivatives of the basis functions N''(u=5/2) are :" << std::endl;
276 for (unsigned int j = 0; j < Nurbs.get_p() + 1; j++)
277 std::cout << N2[2][j].value << std::endl;
278
279 if (opt_display && opt_click_allowed) {
280 double u = 0.0;
281 while (u <= 5) {
282 pt = Nurbs.computeCurvePoint(u);
284 u += 0.01;
285 }
286 for (std::list<vpImagePoint>::const_iterator it = controlPoints.begin(); it != controlPoints.end(); ++it) {
288 }
289
292 }
293
294 if (opt_display) {
295 try {
296 // Display size is automatically defined by the image (I) size
297 display[1].init(I2, 100, 100, "Points interpolation");
300 } catch (...) {
301 vpERROR_TRACE("Error while displaying the image");
302 exit(-1);
303 }
304 }
305
306 Nurbs.globalCurveInterp(controlPoints);
307
308 if (opt_display && opt_click_allowed) {
309 double u = 0.0;
310 while (u <= 1) {
311 pt = Nurbs.computeCurvePoint(u);
313 u += 0.01;
314 }
315
316 for (std::list<vpImagePoint>::const_iterator it = controlPoints.begin(); it != controlPoints.end(); ++it) {
318 }
321 }
322
323 if (opt_display) {
324 try {
325 // Display size is automatically defined by the image (I) size
326 display[2].init(I3, 100, 100, "Points approximation");
329 } catch (...) {
330 vpERROR_TRACE("Error while displaying the image");
331 exit(-1);
332 }
333 }
334
335 Nurbs.globalCurveApprox(controlPoints, 5);
336
337 if (opt_display && opt_click_allowed) {
338 double u = 0.0;
339 while (u <= 1) {
340 pt = Nurbs.computeCurvePoint(u);
342 u += 0.01;
343 }
344
345 for (std::list<vpImagePoint>::const_iterator it = controlPoints.begin(); it != controlPoints.end(); ++it) {
347 }
348
351 }
352
353 if (N != NULL)
354 delete[] N;
355 if (N2 != NULL) {
356 for (int j = 0; j <= 2; j++)
357 delete[] N2[j];
358 delete[] N2;
359 }
360
361 return 0;
362 } catch (const vpException &e) {
363 std::cout << "Catch an exception: " << e << std::endl;
364 return 1;
365 }
366}
367
368#elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
369int main()
370{
371 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
372 return EXIT_SUCCESS;
373}
374#else
375int main()
376{
377 std::cout << "This example requires a video device. " << std::endl
378 << "You should install X11, GTK, OpenCV, GDI or Direct3D" << std::endl
379 << "to be able to execute this example." << std::endl;
380 return 0;
381}
382#endif
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 vpBasisFunction * computeBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:148
void get_controlPoints(std::list< vpImagePoint > &list) const
Definition: vpBSpline.h:140
void set_p(unsigned int degree)
Definition: vpBSpline.h:180
unsigned int get_p() const
Definition: vpBSpline.h:132
void set_controlPoints(const std::list< vpImagePoint > &list)
Definition: vpBSpline.h:187
void get_knots(std::list< double > &list) const
Definition: vpBSpline.h:153
void set_knots(const std::list< double > &list)
Definition: vpBSpline.h:200
static unsigned int findSpan(double l_u, unsigned int l_p, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:84
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
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:135
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 emited by ViSP classes.
Definition: vpException.h:72
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void set_ij(double ii, double jj)
Definition: vpImagePoint.h:188
Class that provides tools to compute and manipulate a Non Uniform Rational B-Spline curve.
Definition: vpNurbs.h:98
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:685
void get_weights(std::list< double > &list) const
Definition: vpNurbs.h:120
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:85
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:866
void set_weights(const std::list< double > &list)
Definition: vpNurbs.h:132
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
#define vpERROR_TRACE
Definition: vpDebug.h:393