Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpBSpline.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * This class implements the B-Spline
32  *
33  * Authors:
34  * Nicolas Melchior
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpBSpline.h>
39 #include <visp3/core/vpDebug.h>
40 
48  : controlPoints(), knots(), p(3), // By default : p=3 for clubic spline
49  crossingPoints()
50 {
51 }
52 
58  : controlPoints(), knots(), p(3), // By default : p=3 for clubic spline
59  crossingPoints()
60 {
61  controlPoints = bspline.controlPoints;
62  knots = bspline.knots;
63  p = bspline.p;
64  crossingPoints = bspline.crossingPoints;
65 }
70 {
71 }
72 
87 unsigned int
88 vpBSpline::findSpan(double l_u, unsigned int l_p, std::vector<double> &l_knots)
89 {
90  unsigned int m = (unsigned int)l_knots.size()-1;
91 
92  if(l_u > l_knots.back())
93  {
94  //vpTRACE("l_u higher than the maximum value in the knot vector : %lf",l_u);
95  return((unsigned int)(m-l_p-1));
96  }
97 
98  //if (l_u == l_knots.back())
99  if (std::fabs(l_u - l_knots.back()) <= std::fabs(vpMath::maximum(l_u, l_knots.back())) * std::numeric_limits<double>::epsilon())
100  return((unsigned int)(m-l_p-1));
101 
102  double low = l_p;
103  double high = m-l_p;
104  double middle = (low+high)/2.0;
105 
106  while (l_u < l_knots[(unsigned int)vpMath::round(middle)] || l_u >= l_knots[(unsigned int)vpMath::round(middle+1)])
107  {
108  if(l_u < l_knots[(unsigned int)vpMath::round(middle)]) high = middle;
109  else low = middle;
110  middle = (low+high)/2.0;
111  }
112 
113  return (unsigned int)middle;
114 }
115 
116 
129 unsigned int
131 {
132  return findSpan( u, p, knots);
133 }
134 
135 
148 vpBasisFunction* vpBSpline::computeBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, std::vector<double> &l_knots)
149 {
150  vpBasisFunction* N = new vpBasisFunction[l_p+1];
151 
152  N[0].value = 1.0;
153 
154  double *left = new double[l_p+1];
155  double *right = new double[l_p+1];
156  double temp = 0.0;
157 
158  for(unsigned int j = 1; j <= l_p; j++)
159  {
160  left[j] = l_u - l_knots[l_i+1-j];
161  right[j] = l_knots[l_i+j] - l_u;
162  double saved = 0.0;
163 
164  for (unsigned int r = 0; r < j; r++)
165  {
166  temp = N[r].value / (right[r+1]+left[j-r]);
167  N[r].value = saved +right[r+1]*temp;
168  saved = left[j-r]*temp;
169  }
170  N[j].value = saved;
171  }
172  for(unsigned int j = 0; j < l_p+1; j++)
173  {
174  N[j].i = l_i-l_p+j;
175  N[j].p = l_p;
176  N[j].u = l_u;
177  N[j].k = 0;
178  }
179 
180  delete[] left;
181  delete[] right;
182 
183  return N;
184 }
185 
186 
198 vpBasisFunction* vpBSpline::computeBasisFuns(double u)
199 {
200  unsigned int i = findSpan(u);
201  return computeBasisFuns(u, i, p ,knots);
202 }
203 
204 
228 vpBasisFunction** vpBSpline::computeDersBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, unsigned int l_der, std::vector<double> &l_knots)
229 {
230  vpBasisFunction** N;
231  N = new vpBasisFunction*[l_der+1];
232  for(unsigned int j = 0; j <= l_der; j++)
233  N[j] = new vpBasisFunction[l_p+1];
234 
235  vpMatrix a(2,l_p+1);
236  vpMatrix ndu(l_p+1,l_p+1);
237  ndu[0][0] = 1.0;
238 
239  double *left = new double[l_p+1];
240  double *right = new double[l_p+1];
241  double temp = 0.0;
242 
243  for(unsigned int j = 1; j <= l_p; j++)
244  {
245  left[j] = l_u - l_knots[l_i+1-j];
246  right[j] = l_knots[l_i+j] - l_u;
247  double saved = 0.0;
248 
249  for (unsigned int r = 0; r < j; r++)
250  {
251  ndu[j][r] = right[r+1]+left[j-r];
252  temp = ndu[r][j-1]/ndu[j][r];
253  ndu[r][j] = saved + right[r+1]*temp;
254  saved = left[j-r]*temp;
255  }
256  ndu[j][j] = saved;
257  }
258 
259  for(unsigned int j = 0; j <= l_p; j++)
260  {
261  N[0][j].value = ndu[j][l_p];
262  N[0][j].i = l_i-l_p+j;
263  N[0][j].p = l_p;
264  N[0][j].u = l_u;
265  N[0][j].k = 0;
266  }
267 
268  if( l_der > l_p)
269  {
270  vpTRACE("l_der must be under or equal to l_p");
271  l_der = l_p;
272  }
273 
274  double d;
275  int rk;
276  unsigned int pk;
277  unsigned int j1,j2;
278 
279  for (unsigned int r = 0; r <= l_p; r++)
280  {
281  unsigned int s1 = 0;
282  unsigned int s2 = 1;
283  a[0][0] = 1.0;
284  for(unsigned int k = 1; k <= l_der; k++)
285  {
286  d = 0.0;
287  rk = (int)(r-k);
288  pk = l_p-k;
289  if(r >= k)
290  {
291  a[s2][0] = a[s1][0]/ndu[pk+1][rk];
292  d = a[s2][0]*ndu[(unsigned int)rk][pk];
293  }
294 
295  if(rk >= -1)
296  j1 = 1;
297  else
298  j1 = (unsigned int)(-rk);
299 
300  if(r-1 <= pk)
301  j2 = k-1;
302  else
303  j2 = l_p-r;
304 
305  for(unsigned int j =j1; j<= j2; j++)
306  {
307  a[s2][j] = (a[s1][j]-a[s1][j-1])/ndu[pk+1][(unsigned int)rk+j];
308  d += a[s2][j]*ndu[(unsigned int)rk+j][pk];
309  }
310 
311  if(r <= pk)
312  {
313  a[s2][k] = -a[s1][k-1]/ndu[pk+1][r];
314  d += a[s2][k]*ndu[r][pk];
315  }
316  N[k][r].value = d;
317  N[k][r].i = l_i-l_p+r;
318  N[k][r].p = l_p;
319  N[k][r].u = l_u;
320  N[k][r].k = k;
321 
322  s1 = (s1+1)%2;
323  s2 = (s2+1)%2;
324  }
325  }
326 
327  double r = l_p;
328  for ( unsigned int k = 1; k <= l_der; k++ )
329  {
330  for (unsigned int j = 0; j <= l_p; j++)
331  N[k][j].value *= r;
332  r *= (l_p-k);
333  }
334 
335  delete[] left;
336  delete[] right;
337 
338  return N;
339 }
340 
341 
362 vpBasisFunction** vpBSpline::computeDersBasisFuns(double u, unsigned int der)
363 {
364  unsigned int i = findSpan(u);
365  return computeDersBasisFuns(u, i, p , der, knots);
366 }
367 
368 
380 vpImagePoint vpBSpline::computeCurvePoint(double l_u, unsigned int l_i, unsigned int l_p, std::vector<double> &l_knots, std::vector<vpImagePoint> &l_controlPoints)
381 {
382  vpBasisFunction* N = computeBasisFuns(l_u, l_i, l_p, l_knots);
383  vpImagePoint pt;
384 
385  double ic = 0;
386  double jc = 0;
387  for(unsigned int j = 0; j <= l_p; j++)
388  {
389  ic = ic + N[j].value * (l_controlPoints[l_i-l_p+j]).get_i();
390  jc = jc + N[j].value * (l_controlPoints[l_i-l_p+j]).get_j();
391  }
392 
393  pt.set_i(ic);
394  pt.set_j(jc);
395 
396  delete[] N;
397 
398  return pt;
399 }
400 
401 
410 {
411  vpBasisFunction* N = computeBasisFuns(u);
412  vpImagePoint pt;
413 
414  double ic = 0;
415  double jc = 0;
416  for(unsigned int j = 0; j <= p; j++)
417  {
418  ic = ic + N[j].value * (controlPoints[N[0].i+j]).get_i();
419  jc = jc + N[j].value * (controlPoints[N[0].i+j]).get_j();
420  }
421 
422  pt.set_i(ic);
423  pt.set_j(jc);
424 
425  delete[] N;
426 
427  return pt;
428 }
429 
430 
449 vpImagePoint* vpBSpline::computeCurveDers(double l_u, unsigned int l_i, unsigned int l_p, unsigned int l_der, std::vector<double> &l_knots, std::vector<vpImagePoint> &l_controlPoints)
450 {
451  vpImagePoint *derivate = new vpImagePoint[l_der+1];
452  vpBasisFunction** N;
453  N = computeDersBasisFuns(l_u, l_i, l_p, l_der, l_knots);
454 
455  unsigned int du;
456  if (l_p < l_der)
457  {
458  vpTRACE("l_der must be under or equal to l_p");
459  du = l_p;
460  }
461  else du = l_der;
462 
463  for(unsigned int k = 0; k <= du; k++)
464  {
465  derivate[k].set_ij(0.0,0.0);
466  for(unsigned int j = 0; j<= l_p; j++)
467  {
468  derivate[k].set_i( derivate[k].get_i() + N[k][j].value*(l_controlPoints[l_i-l_p+j]).get_i());
469  derivate[k].set_j( derivate[k].get_j() + N[k][j].value*(l_controlPoints[l_i-l_p+j]).get_j());
470  }
471  }
472 
473 
474  for(unsigned int j = 0; j <= l_der; j++)
475  delete[] N[j];
476  delete[] N;
477 
478 
479  return derivate;
480 }
481 
482 
497 vpImagePoint* vpBSpline::computeCurveDers(double u, unsigned int der)
498 {
499  vpImagePoint *derivate = new vpImagePoint[der+1];
500  vpBasisFunction** N;
501  N = computeDersBasisFuns(u, der);
502 
503  unsigned int du;
504  if (p < der)
505  {
506  vpTRACE("der must be under or equal to p");
507  du = p;
508  }
509  else du = der;
510 
511  for(unsigned int k = 0; k <= du; k++)
512  {
513  derivate[k].set_ij(0.0,0.0);
514  for(unsigned int j = 0; j<= p; j++)
515  {
516  derivate[k].set_i( derivate[k].get_i() + N[k][j].value*(controlPoints[N[0][0].i-p+j]).get_i());
517  derivate[k].set_j( derivate[k].get_j() + N[k][j].value*(controlPoints[N[0][0].i-p+j]).get_j());
518  }
519  }
520 
521  for(unsigned int j = 0; j <= der; j++)
522  delete[] N[j];
523  delete[] N;
524 
525  return derivate;
526 }
static vpImagePoint * computeCurveDers(double l_u, unsigned int l_i, unsigned int l_p, unsigned int l_der, std::vector< double > &l_knots, std::vector< vpImagePoint > &l_controlPoints)
Definition: vpBSpline.cpp:449
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
virtual ~vpBSpline()
Definition: vpBSpline.cpp:69
static int round(const double x)
Definition: vpMath.h:249
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:228
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:140
static unsigned int findSpan(double l_u, unsigned int l_p, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:88
void set_i(const double ii)
Definition: vpImagePoint.h:163
#define vpTRACE
Definition: vpDebug.h:414
static vpBasisFunction * computeBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:148
void set_j(const double jj)
Definition: vpImagePoint.h:174
Class that provides tools to compute and manipulate a B-Spline curve.
Definition: vpBSpline.h:100
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(const double ii, const double jj)
Definition: vpImagePoint.h:185
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:380