Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
BSpline.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Example of a B-Spline curve.
32 */
33
39
45
46#include <visp3/core/vpConfig.h>
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/vpDisplayFactory.h>
57#endif
58
59#include <cstdlib>
60#include <stdlib.h>
61#include <visp3/core/vpIoTools.h>
62#include <visp3/io/vpParseArgv.h>
63
64#if defined(VISP_HAVE_DISPLAY)
65
66 // List of allowed command line options
67#define GETOPTARGS "cdh"
68
69#ifdef ENABLE_VISP_NAMESPACE
70using namespace VISP_NAMESPACE_NAME;
71#endif
72
73void usage(const char *name, const char *badparam);
74bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
75
84void usage(const char *name, const char *badparam)
85{
86 fprintf(stdout, "\n\
87Describe a curve thanks to a BSpline.\n\
88\n\
89SYNOPSIS\n\
90 %s [-c] [-d] [-h]\n",
91 name);
92
93 fprintf(stdout, "\n\
94OPTIONS: Default\n\
95 -c\n\
96 Disable the mouse click. Useful to automate the \n\
97 execution of this program without human 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
121bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
122{
123 const char *optarg_;
124 int c;
125 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
126
127 switch (c) {
128 case 'c':
129 click_allowed = false;
130 break;
131 case 'd':
132 display = false;
133 break;
134 case 'h':
135 usage(argv[0], nullptr);
136 return false;
137
138 default:
139 usage(argv[0], optarg_);
140 return false;
141 }
142 }
143
144 if ((c == 1) || (c == -1)) {
145 // standalone param or error
146 usage(argv[0], nullptr);
147 std::cerr << "ERROR: " << std::endl;
148 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
149 return false;
150 }
151
152 return true;
153}
154
155int main(int argc, const char **argv)
156{
157 // We declare the display variable to be able to free it in the catch block if needed.
158#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
159 std::shared_ptr<vpDisplay> display;
160#else
161 vpDisplay *display = nullptr;
162#endif
163 try {
164 bool opt_click_allowed = true;
165 bool opt_display = true;
166
167 // Read the command line options
168 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
169 return EXIT_FAILURE;
170 }
171
172 // Declare an image, this is a gray level image (unsigned char)
173 // it size is not defined yet, it will be defined when the image will
174 // read on the disk
175 vpImage<unsigned char> I(540, 480);
176
177 // We open a window using either X11, GTK or GDI.
178#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
180#else
182#endif
183
184 if (opt_display) {
185 // Display size is automatically defined by the image (I) size
186 display->init(I, 100, 100, "Display image");
189 }
190
191 vpBSpline bSpline;
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 vpImagePoint pt;
207 pt.set_ij(50, 300);
208 controlPoints.push_back(pt);
209 pt.set_ij(100, 130);
210 controlPoints.push_back(pt);
211 pt.set_ij(150, 400);
212 controlPoints.push_back(pt);
213 pt.set_ij(200, 370);
214 controlPoints.push_back(pt);
215 pt.set_ij(250, 120);
216 controlPoints.push_back(pt);
217 pt.set_ij(300, 250);
218 controlPoints.push_back(pt);
219 pt.set_ij(350, 200);
220 controlPoints.push_back(pt);
221 pt.set_ij(400, 300);
222 controlPoints.push_back(pt);
223
224 bSpline.set_p(2);
225 bSpline.set_knots(knots);
226 bSpline.set_controlPoints(controlPoints);
227
228 std::cout << "The parameters are :" << std::endl;
229 std::cout << "p : " << bSpline.get_p() << std::endl;
230 std::cout << "" << std::endl;
231 std::cout << "The knot vector :" << std::endl;
232 std::list<double> knots_cur;
233 bSpline.get_knots(knots_cur);
234 unsigned int i_display = 0;
235 for (std::list<double>::const_iterator it = knots_cur.begin(); it != knots_cur.end(); ++it, ++i_display) {
236 std::cout << i_display << " ---> " << *it << std::endl;
237 }
238 std::cout << "The control points are :" << std::endl;
239 std::list<vpImagePoint> controlPoints_cur;
240 bSpline.get_controlPoints(controlPoints_cur);
241 i_display = 0;
242 for (std::list<vpImagePoint>::const_iterator it = controlPoints_cur.begin(); it != controlPoints_cur.end();
243 ++it, ++i_display) {
244 std::cout << i_display << " ---> " << *it << std::endl;
245 }
246
247 unsigned int i = bSpline.findSpan(5 / 2.0);
248 std::cout << "The knot interval number for the value u = 5/2 is : " << i << std::endl;
249
250 vpBasisFunction *N = nullptr;
251 N = bSpline.computeBasisFuns(5 / 2.0);
252 std::cout << "The nonvanishing basis functions N(u=5/2) are :" << std::endl;
253 for (unsigned int j = 0; j < bSpline.get_p() + 1; j++)
254 std::cout << N[j].value << std::endl;
255
256 vpBasisFunction **N2 = nullptr;
257 N2 = bSpline.computeDersBasisFuns(5 / 2.0, 2);
258 std::cout << "The first derivatives of the basis functions N'(u=5/2) are :" << std::endl;
259 for (unsigned int j = 0; j < bSpline.get_p() + 1; j++)
260 std::cout << N2[1][j].value << std::endl;
261
262 std::cout << "The second derivatives of the basis functions N''(u=5/2) are :" << std::endl;
263 for (unsigned int j = 0; j < bSpline.get_p() + 1; j++)
264 std::cout << N2[2][j].value << std::endl;
265
266 if (opt_display && opt_click_allowed) {
267 double u = 0.0;
268 while (u <= 5) {
269 pt = bSpline.computeCurvePoint(u);
271 u += 0.01;
272 }
273 for (std::list<vpImagePoint>::const_iterator it = controlPoints.begin(); it != controlPoints.end(); ++it) {
275 }
278 }
279
280 if (N != nullptr)
281 delete[] N;
282 if (N2 != nullptr) {
283 for (unsigned int j = 0; j <= 2; j++)
284 delete[] N2[j];
285 delete[] N2;
286 }
287
288#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
289 if (display != nullptr) {
290 delete display;
291 }
292#endif
293 return EXIT_SUCCESS;
294 }
295 catch (const vpException &e) {
296 std::cout << "Catch an exception: " << e << std::endl;
297#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
298 if (display != nullptr) {
299 delete display;
300 }
301#endif
302 return EXIT_FAILURE;
303 }
304}
305
306#else
307int main()
308{
309 std::cout
310 << "You do not have X11, GTK, or OpenCV, or GDI (Graphical Device Interface) functionalities to display images..."
311 << std::endl;
312 std::cout << "Tip if you are on a unix-like system:" << std::endl;
313 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
314 std::cout << "Tip if you are on a windows-like system:" << std::endl;
315 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
316 return EXIT_SUCCESS;
317}
318#endif
Class that provides tools to compute and manipulate a B-Spline curve.
Definition vpBSpline.h:108
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)
void get_controlPoints(std::list< vpImagePoint > &list) const
Definition vpBSpline.h:135
void set_p(unsigned int degree)
Definition vpBSpline.h:175
static unsigned int findSpan(double l_u, unsigned int l_p, const std::vector< double > &l_knots)
Definition vpBSpline.cpp:63
unsigned int get_p() const
Definition vpBSpline.h:127
static vpBasisFunction ** computeDersBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, unsigned int l_der, const std::vector< double > &l_knots)
void set_controlPoints(const std::list< vpImagePoint > &list)
Definition vpBSpline.h:182
void get_knots(std::list< double > &list) const
Definition vpBSpline.h:148
void set_knots(const std::list< double > &list)
Definition vpBSpline.h:195
static vpBasisFunction * computeBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, const std::vector< double > &l_knots)
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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:60
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void set_ij(double ii, double jj)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.