Visual Servoing Platform  version 3.5.1 under development (2023-05-31)
vpBSpline.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  * This class implements the B-Spline
33  *
34  * Authors:
35  * Nicolas Melchior
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpBSpline.h>
40 #include <visp3/core/vpDebug.h>
41 
49  : controlPoints(), knots(), p(3), // By default : p=3 for clubic spline
50  crossingPoints()
51 {
52 }
53 
59  : controlPoints(bspline.controlPoints), knots(bspline.knots), p(bspline.p), // By default : p=3 for clubic spline
60  crossingPoints(bspline.crossingPoints)
61 {
62 }
67 
84 unsigned int vpBSpline::findSpan(double l_u, unsigned int l_p, std::vector<double> &l_knots)
85 {
86  unsigned int m = (unsigned int)l_knots.size() - 1;
87 
88  if (l_u > l_knots.back()) {
89  // vpTRACE("l_u higher than the maximum value in the knot vector :
90  // %lf",l_u);
91  return ((unsigned int)(m - l_p - 1));
92  }
93 
94  // if (l_u == l_knots.back())
95  if (std::fabs(l_u - l_knots.back()) <=
96  std::fabs(vpMath::maximum(l_u, l_knots.back())) * std::numeric_limits<double>::epsilon())
97  return ((unsigned int)(m - l_p - 1));
98 
99  double low = l_p;
100  double high = m - l_p;
101  double middle = (low + high) / 2.0;
102 
103  while (l_u < l_knots[(unsigned int)middle] || l_u >= l_knots[(unsigned int)middle + 1]) {
104  if (l_u < l_knots[(unsigned int)vpMath::round(middle)])
105  high = middle;
106  else
107  low = middle;
108  middle = (low + high) / 2.0;
109  }
110 
111  return (unsigned int)middle;
112 }
113 
128 unsigned int vpBSpline::findSpan(double u) { return findSpan(u, p, knots); }
129 
147 vpBasisFunction *vpBSpline::computeBasisFuns(double l_u, unsigned int l_i, unsigned int l_p,
148  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  left[j] = l_u - l_knots[l_i + 1 - j];
160  right[j] = l_knots[l_i + j] - l_u;
161  double saved = 0.0;
162 
163  for (unsigned int r = 0; r < j; r++) {
164  temp = N[r].value / (right[r + 1] + left[j - r]);
165  N[r].value = saved + right[r + 1] * temp;
166  saved = left[j - r] * temp;
167  }
168  N[j].value = saved;
169  }
170  for (unsigned int j = 0; j < l_p + 1; j++) {
171  N[j].i = l_i - l_p + j;
172  N[j].p = l_p;
173  N[j].u = l_u;
174  N[j].k = 0;
175  }
176 
177  delete[] left;
178  delete[] right;
179 
180  return N;
181 }
182 
198 vpBasisFunction *vpBSpline::computeBasisFuns(double u)
199 {
200  unsigned int i = findSpan(u);
201  return computeBasisFuns(u, i, p, knots);
202 }
203 
233 vpBasisFunction **vpBSpline::computeDersBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, unsigned int l_der,
234  std::vector<double> &l_knots)
235 {
236  vpBasisFunction **N;
237  N = new vpBasisFunction *[l_der + 1];
238  for (unsigned int j = 0; j <= l_der; j++)
239  N[j] = new vpBasisFunction[l_p + 1];
240 
241  vpMatrix a(2, l_p + 1);
242  vpMatrix ndu(l_p + 1, l_p + 1);
243  ndu[0][0] = 1.0;
244 
245  double *left = new double[l_p + 1];
246  double *right = new double[l_p + 1];
247  double temp = 0.0;
248 
249  for (unsigned int j = 1; j <= l_p; j++) {
250  left[j] = l_u - l_knots[l_i + 1 - j];
251  right[j] = l_knots[l_i + j] - l_u;
252  double saved = 0.0;
253 
254  for (unsigned int r = 0; r < j; r++) {
255  ndu[j][r] = right[r + 1] + left[j - r];
256  temp = ndu[r][j - 1] / ndu[j][r];
257  ndu[r][j] = saved + right[r + 1] * temp;
258  saved = left[j - r] * temp;
259  }
260  ndu[j][j] = saved;
261  }
262 
263  for (unsigned int j = 0; j <= l_p; j++) {
264  N[0][j].value = ndu[j][l_p];
265  N[0][j].i = l_i - l_p + j;
266  N[0][j].p = l_p;
267  N[0][j].u = l_u;
268  N[0][j].k = 0;
269  }
270 
271  if (l_der > l_p) {
272  vpTRACE("l_der must be under or equal to l_p");
273  l_der = l_p;
274  }
275 
276  double d;
277  int rk;
278  unsigned int pk;
279  unsigned int j1, j2;
280 
281  for (unsigned int r = 0; r <= l_p; r++) {
282  unsigned int s1 = 0;
283  unsigned int s2 = 1;
284  a[0][0] = 1.0;
285  for (unsigned int k = 1; k <= l_der; k++) {
286  d = 0.0;
287  rk = (int)(r - k);
288  pk = l_p - k;
289  if (r >= k) {
290  a[s2][0] = a[s1][0] / ndu[pk + 1][rk];
291  d = a[s2][0] * ndu[(unsigned int)rk][pk];
292  }
293 
294  if (rk >= -1)
295  j1 = 1;
296  else
297  j1 = (unsigned int)(-rk);
298 
299  if (r - 1 <= pk)
300  j2 = k - 1;
301  else
302  j2 = l_p - r;
303 
304  for (unsigned int j = j1; j <= j2; j++) {
305  a[s2][j] = (a[s1][j] - a[s1][j - 1]) / ndu[pk + 1][(unsigned int)rk + j];
306  d += a[s2][j] * ndu[(unsigned int)rk + j][pk];
307  }
308 
309  if (r <= pk) {
310  a[s2][k] = -a[s1][k - 1] / ndu[pk + 1][r];
311  d += a[s2][k] * ndu[r][pk];
312  }
313  N[k][r].value = d;
314  N[k][r].i = l_i - l_p + r;
315  N[k][r].p = l_p;
316  N[k][r].u = l_u;
317  N[k][r].k = k;
318 
319  s1 = (s1 + 1) % 2;
320  s2 = (s2 + 1) % 2;
321  }
322  }
323 
324  double r = l_p;
325  for (unsigned int k = 1; k <= l_der; k++) {
326  for (unsigned int j = 0; j <= l_p; j++)
327  N[k][j].value *= r;
328  r *= (l_p - k);
329  }
330 
331  delete[] left;
332  delete[] right;
333 
334  return N;
335 }
336 
363 vpBasisFunction **vpBSpline::computeDersBasisFuns(double u, unsigned int der)
364 {
365  unsigned int i = findSpan(u);
366  return computeDersBasisFuns(u, i, p, der, knots);
367 }
368 
380 vpImagePoint vpBSpline::computeCurvePoint(double l_u, unsigned int l_i, unsigned int l_p, std::vector<double> &l_knots,
381  std::vector<vpImagePoint> &l_controlPoints)
382 {
383  vpBasisFunction *N = computeBasisFuns(l_u, l_i, l_p, l_knots);
384  vpImagePoint pt;
385 
386  double ic = 0;
387  double jc = 0;
388  for (unsigned int j = 0; j <= l_p; j++) {
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 
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  ic = ic + N[j].value * (controlPoints[N[0].i + j]).get_i();
418  jc = jc + N[j].value * (controlPoints[N[0].i + j]).get_j();
419  }
420 
421  pt.set_i(ic);
422  pt.set_j(jc);
423 
424  delete[] N;
425 
426  return pt;
427 }
428 
450 vpImagePoint *vpBSpline::computeCurveDers(double l_u, unsigned int l_i, unsigned int l_p, unsigned int l_der,
451  std::vector<double> &l_knots, std::vector<vpImagePoint> &l_controlPoints)
452 {
453  vpImagePoint *derivate = new vpImagePoint[l_der + 1];
454  vpBasisFunction **N;
455  N = computeDersBasisFuns(l_u, l_i, l_p, l_der, l_knots);
456 
457  unsigned int du;
458  if (l_p < l_der) {
459  vpTRACE("l_der must be under or equal to l_p");
460  du = l_p;
461  } else
462  du = l_der;
463 
464  for (unsigned int k = 0; k <= du; k++) {
465  derivate[k].set_ij(0.0, 0.0);
466  for (unsigned int j = 0; j <= l_p; j++) {
467  derivate[k].set_i(derivate[k].get_i() + N[k][j].value * (l_controlPoints[l_i - l_p + j]).get_i());
468  derivate[k].set_j(derivate[k].get_j() + N[k][j].value * (l_controlPoints[l_i - l_p + j]).get_j());
469  }
470  }
471 
472  for (unsigned int j = 0; j <= l_der; j++)
473  delete[] N[j];
474  delete[] N;
475 
476  return derivate;
477 }
478 
496 vpImagePoint *vpBSpline::computeCurveDers(double u, unsigned int der)
497 {
498  vpImagePoint *derivate = new vpImagePoint[der + 1];
499  vpBasisFunction **N;
500  N = computeDersBasisFuns(u, der);
501 
502  unsigned int du;
503  if (p < der) {
504  vpTRACE("der must be under or equal to p");
505  du = p;
506  } else
507  du = der;
508 
509  for (unsigned int k = 0; k <= du; k++) {
510  derivate[k].set_ij(0.0, 0.0);
511  for (unsigned int j = 0; j <= p; j++) {
512  derivate[k].set_i(derivate[k].get_i() + N[k][j].value * (controlPoints[N[0][0].i - p + j]).get_i());
513  derivate[k].set_j(derivate[k].get_j() + N[k][j].value * (controlPoints[N[0][0].i - p + j]).get_j());
514  }
515  }
516 
517  for (unsigned int j = 0; j <= der; j++)
518  delete[] N[j];
519  delete[] N;
520 
521  return derivate;
522 }
Class that provides tools to compute and manipulate a B-Spline curve.
Definition: vpBSpline.h:111
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:233
static vpBasisFunction * computeBasisFuns(double l_u, unsigned int l_i, unsigned int l_p, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:147
virtual ~vpBSpline()
Definition: vpBSpline.cpp:66
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
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:450
static unsigned int findSpan(double l_u, unsigned int l_p, std::vector< double > &l_knots)
Definition: vpBSpline.cpp:84
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:89
void set_j(double jj)
Definition: vpImagePoint.h:309
void set_ij(double ii, double jj)
Definition: vpImagePoint.h:320
void set_i(double ii)
Definition: vpImagePoint.h:298
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:170
static int round(double x)
Definition: vpMath.h:321
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
#define vpTRACE
Definition: vpDebug.h:416