Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
testNurbs.cpp

Describe a curve thanks to a Nurbs.

/*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2023 by Inria. All rights reserved.
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
*
* See https://visp.inria.fr for more information.
*
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
*
* If you have questions regarding the use of this file, please contact
* Inria at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Description:
* Example of a Nurbs curve.
*/
#include <visp3/core/vpDebug.h>
#include <visp3/me/vpNurbs.h>
#include <visp3/core/vpImage.h>
#include <visp3/core/vpImagePoint.h>
#include <visp3/io/vpImageIo.h>
#ifdef VISP_HAVE_MODULE_GUI
#include <visp3/gui/vpDisplayD3D.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#endif
#include <cstdlib>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpParseArgv.h>
#if defined(VISP_HAVE_DISPLAY) && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
// List of allowed command line options
#define GETOPTARGS "cdh"
void usage(const char *name, const char *badparam);
bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
void usage(const char *name, const char *badparam)
{
fprintf(stdout, "\n\
Describe a curve thanks to a Nurbs.\n\
\n\
SYNOPSIS\n\
%s [-c] [-d] [-h]\n",
name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-c\n\
Disable the mouse click. Useful to automate the \n\
execution of this program without human intervention.\n\
\n\
-d \n\
Turn off the display.\n\
\n\
-h\n\
Print the help.\n");
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
}
bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
{
const char *optarg_;
int c;
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
switch (c) {
case 'c':
click_allowed = false;
break;
case 'd':
display = false;
break;
case 'h':
usage(argv[0], nullptr);
return false;
break;
default:
usage(argv[0], optarg_);
return false;
break;
}
}
if ((c == 1) || (c == -1)) {
// standalone param or error
usage(argv[0], nullptr);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
}
return true;
}
int main(int argc, const char **argv)
{
try {
bool opt_click_allowed = true;
bool opt_display = true;
// Read the command line options
if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
return EXIT_FAILURE;
}
// Declare an image, this is a gray level image (unsigned char)
// it size is not defined yet, it will be defined when the image will
// read on the disk
vpImage<unsigned char> I2(540, 480);
vpImage<unsigned char> I3(540, 480);
// We open a window using either X11, GTK or GDI.
#if defined(VISP_HAVE_X11)
#elif defined(VISP_HAVE_GDI)
#elif defined(VISP_HAVE_GTK)
#elif defined(HAVE_OPENCV_HIGHGUI)
#endif
if (opt_display) {
// Display size is automatically defined by the image (I) size
display[0].init(I, 100, 100, "Points as control points");
}
vpNurbs Nurbs;
std::list<double> knots;
knots.push_back(0);
knots.push_back(0);
knots.push_back(0);
knots.push_back(1);
knots.push_back(2);
knots.push_back(3);
knots.push_back(4);
knots.push_back(4);
knots.push_back(5);
knots.push_back(5);
knots.push_back(5);
std::list<vpImagePoint> controlPoints;
std::list<double> weights;
pt.set_ij(50, 300);
controlPoints.push_back(pt);
weights.push_back(1);
pt.set_ij(100, 130);
controlPoints.push_back(pt);
weights.push_back(5);
pt.set_ij(150, 400);
controlPoints.push_back(pt);
weights.push_back(0.2);
pt.set_ij(200, 370);
controlPoints.push_back(pt);
weights.push_back(10);
pt.set_ij(250, 120);
controlPoints.push_back(pt);
weights.push_back(1);
pt.set_ij(300, 250);
controlPoints.push_back(pt);
weights.push_back(2);
pt.set_ij(350, 200);
controlPoints.push_back(pt);
weights.push_back(3);
pt.set_ij(400, 300);
controlPoints.push_back(pt);
weights.push_back(1);
Nurbs.set_p(2);
Nurbs.set_knots(knots);
Nurbs.set_controlPoints(controlPoints);
Nurbs.set_weights(weights);
std::cout << "The parameters are :" << std::endl;
std::cout << "p : " << Nurbs.get_p() << std::endl;
std::cout << "" << std::endl;
std::cout << "The knot vector :" << std::endl;
std::list<double> knots_cur;
Nurbs.get_knots(knots_cur);
unsigned int i_display = 0;
for (std::list<double>::const_iterator it = knots_cur.begin(); it != knots_cur.end(); ++it, ++i_display) {
std::cout << i_display << " ---> " << *it << std::endl;
}
std::cout << "The control points are :" << std::endl;
std::list<vpImagePoint> controlPoints_cur;
Nurbs.get_controlPoints(controlPoints_cur);
i_display = 0;
for (std::list<vpImagePoint>::const_iterator it = controlPoints_cur.begin(); it != controlPoints_cur.end();
++it, ++i_display) {
std::cout << i_display << " ---> " << *it << std::endl;
}
std::cout << "The associated weights are :" << std::endl;
std::list<double> weights_cur;
Nurbs.get_weights(weights_cur);
i_display = 0;
for (std::list<double>::const_iterator it = weights_cur.begin(); it != weights_cur.end(); ++it, ++i_display) {
std::cout << i_display << " ---> " << *it << std::endl;
}
unsigned int i = Nurbs.findSpan(5 / 2.0);
std::cout << "The knot interval number for the value u = 5/2 is : " << i << std::endl;
vpBasisFunction *N = nullptr;
N = Nurbs.computeBasisFuns(5 / 2.0);
std::cout << "The nonvanishing basis functions N(u=5/2) are :" << std::endl;
for (unsigned int j = 0; j < Nurbs.get_p() + 1; j++)
std::cout << N[j].value << std::endl;
vpBasisFunction **N2 = nullptr;
N2 = Nurbs.computeDersBasisFuns(5 / 2.0, 2);
std::cout << "The first derivatives of the basis functions N'(u=5/2) are :" << std::endl;
for (unsigned int j = 0; j < Nurbs.get_p() + 1; j++)
std::cout << N2[1][j].value << std::endl;
std::cout << "The second derivatives of the basis functions N''(u=5/2) are :" << std::endl;
for (unsigned int j = 0; j < Nurbs.get_p() + 1; j++)
std::cout << N2[2][j].value << std::endl;
if (opt_display && opt_click_allowed) {
double u = 0.0;
while (u <= 5) {
pt = Nurbs.computeCurvePoint(u);
u += 0.01;
}
for (std::list<vpImagePoint>::const_iterator it = controlPoints.begin(); it != controlPoints.end(); ++it) {
}
}
if (opt_display) {
try {
// Display size is automatically defined by the image (I) size
display[1].init(I2, 100, 100, "Points interpolation");
} catch (...) {
vpERROR_TRACE("Error while displaying the image");
return EXIT_FAILURE;
}
}
Nurbs.globalCurveInterp(controlPoints);
if (opt_display && opt_click_allowed) {
double u = 0.0;
while (u <= 1) {
pt = Nurbs.computeCurvePoint(u);
u += 0.01;
}
for (std::list<vpImagePoint>::const_iterator it = controlPoints.begin(); it != controlPoints.end(); ++it) {
}
}
if (opt_display) {
try {
// Display size is automatically defined by the image (I) size
display[2].init(I3, 100, 100, "Points approximation");
} catch (...) {
vpERROR_TRACE("Error while displaying the image");
return EXIT_FAILURE;
}
}
Nurbs.globalCurveApprox(controlPoints, 5);
if (opt_display && opt_click_allowed) {
double u = 0.0;
while (u <= 1) {
pt = Nurbs.computeCurvePoint(u);
u += 0.01;
}
for (std::list<vpImagePoint>::const_iterator it = controlPoints.begin(); it != controlPoints.end(); ++it) {
}
}
if (N != nullptr)
delete[] N;
if (N2 != nullptr) {
for (int j = 0; j <= 2; j++)
delete[] N2[j];
delete[] N2;
}
return EXIT_SUCCESS;
} catch (const vpException &e) {
std::cout << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
}
#elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
int main()
{
std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
return EXIT_SUCCESS;
}
#else
int main()
{
std::cout << "This example requires a video device. " << std::endl
<< "You should install X11, GTK, OpenCV, GDI or Direct3D" << std::endl
<< "to be able to execute this example." << std::endl;
return EXIT_SUCCESS;
}
#endif
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 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
Class that provides tools to compute and manipulate a Non Uniform Rational B-Spline curve.
Definition: vpNurbs.h:92
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:463
void get_weights(std::list< double > &list) const
Definition: vpNurbs.h:177
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:53
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:592
void set_weights(const std::list< double > &list)
Definition: vpNurbs.h:189
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
#define vpERROR_TRACE
Definition: vpDebug.h:382
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.