SplineFitting.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 20010-2011 Hauke Heibel <hauke.heibel@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPLINE_FITTING_H
11 #define EIGEN_SPLINE_FITTING_H
12 
13 #include <algorithm>
14 #include <functional>
15 #include <numeric>
16 #include <vector>
17 
18 #include "./InternalHeaderCheck.h"
19 
20 #include "SplineFwd.h"
21 
22 #include "../../../../Eigen/LU"
23 #include "../../../../Eigen/QR"
24 
25 
26 namespace Eigen
27 {
47  template <typename KnotVectorType>
48  void KnotAveraging(const KnotVectorType& parameters, DenseIndex degree, KnotVectorType& knots)
49  {
50  knots.resize(parameters.size()+degree+1);
51 
52  for (DenseIndex j=1; j<parameters.size()-degree; ++j)
53  knots(j+degree) = parameters.segment(j,degree).mean();
54 
55  knots.segment(0,degree+1) = KnotVectorType::Zero(degree+1);
56  knots.segment(knots.size()-degree-1,degree+1) = KnotVectorType::Ones(degree+1);
57  }
58 
80  template <typename KnotVectorType, typename ParameterVectorType, typename IndexArray>
81  void KnotAveragingWithDerivatives(const ParameterVectorType& parameters,
82  const unsigned int degree,
83  const IndexArray& derivativeIndices,
84  KnotVectorType& knots)
85  {
86  typedef typename ParameterVectorType::Scalar Scalar;
87 
88  DenseIndex numParameters = parameters.size();
89  DenseIndex numDerivatives = derivativeIndices.size();
90 
91  if (numDerivatives < 1)
92  {
93  KnotAveraging(parameters, degree, knots);
94  return;
95  }
96 
97  DenseIndex startIndex;
98  DenseIndex endIndex;
99 
100  DenseIndex numInternalDerivatives = numDerivatives;
101 
102  if (derivativeIndices[0] == 0)
103  {
104  startIndex = 0;
105  --numInternalDerivatives;
106  }
107  else
108  {
109  startIndex = 1;
110  }
111  if (derivativeIndices[numDerivatives - 1] == numParameters - 1)
112  {
113  endIndex = numParameters - degree;
114  --numInternalDerivatives;
115  }
116  else
117  {
118  endIndex = numParameters - degree - 1;
119  }
120 
121  // There are (endIndex - startIndex + 1) knots obtained from the averaging
122  // and 2 for the first and last parameters.
123  DenseIndex numAverageKnots = endIndex - startIndex + 3;
124  KnotVectorType averageKnots(numAverageKnots);
125  averageKnots[0] = parameters[0];
126 
127  int newKnotIndex = 0;
128  for (DenseIndex i = startIndex; i <= endIndex; ++i)
129  averageKnots[++newKnotIndex] = parameters.segment(i, degree).mean();
130  averageKnots[++newKnotIndex] = parameters[numParameters - 1];
131 
132  newKnotIndex = -1;
133 
134  ParameterVectorType temporaryParameters(numParameters + 1);
135  KnotVectorType derivativeKnots(numInternalDerivatives);
136  for (DenseIndex i = 0; i < numAverageKnots - 1; ++i)
137  {
138  temporaryParameters[0] = averageKnots[i];
139  ParameterVectorType parameterIndices(numParameters);
140  int temporaryParameterIndex = 1;
141  for (DenseIndex j = 0; j < numParameters; ++j)
142  {
143  Scalar parameter = parameters[j];
144  if (parameter >= averageKnots[i] && parameter < averageKnots[i + 1])
145  {
146  parameterIndices[temporaryParameterIndex] = j;
147  temporaryParameters[temporaryParameterIndex++] = parameter;
148  }
149  }
150  temporaryParameters[temporaryParameterIndex] = averageKnots[i + 1];
151 
152  for (int j = 0; j <= temporaryParameterIndex - 2; ++j)
153  {
154  for (DenseIndex k = 0; k < derivativeIndices.size(); ++k)
155  {
156  if (parameterIndices[j + 1] == derivativeIndices[k]
157  && parameterIndices[j + 1] != 0
158  && parameterIndices[j + 1] != numParameters - 1)
159  {
160  derivativeKnots[++newKnotIndex] = temporaryParameters.segment(j, 3).mean();
161  break;
162  }
163  }
164  }
165  }
166 
167  KnotVectorType temporaryKnots(averageKnots.size() + derivativeKnots.size());
168 
169  std::merge(averageKnots.data(), averageKnots.data() + averageKnots.size(),
170  derivativeKnots.data(), derivativeKnots.data() + derivativeKnots.size(),
171  temporaryKnots.data());
172 
173  // Number of knots (one for each point and derivative) plus spline order.
174  DenseIndex numKnots = numParameters + numDerivatives + degree + 1;
175  knots.resize(numKnots);
176 
177  knots.head(degree).fill(temporaryKnots[0]);
178  knots.tail(degree).fill(temporaryKnots.template tail<1>()[0]);
179  knots.segment(degree, temporaryKnots.size()) = temporaryKnots;
180  }
181 
191  template <typename PointArrayType, typename KnotVectorType>
192  void ChordLengths(const PointArrayType& pts, KnotVectorType& chord_lengths)
193  {
194  typedef typename KnotVectorType::Scalar Scalar;
195 
196  const DenseIndex n = pts.cols();
197 
198  // 1. compute the column-wise norms
199  chord_lengths.resize(pts.cols());
200  chord_lengths[0] = 0;
201  chord_lengths.rightCols(n-1) = (pts.array().leftCols(n-1) - pts.array().rightCols(n-1)).matrix().colwise().norm();
202 
203  // 2. compute the partial sums
204  std::partial_sum(chord_lengths.data(), chord_lengths.data()+n, chord_lengths.data());
205 
206  // 3. normalize the data
207  chord_lengths /= chord_lengths(n-1);
208  chord_lengths(n-1) = Scalar(1);
209  }
210 
215  template <typename SplineType>
217  {
218  typedef typename SplineType::KnotVectorType KnotVectorType;
219  typedef typename SplineType::ParameterVectorType ParameterVectorType;
220 
229  template <typename PointArrayType>
230  static SplineType Interpolate(const PointArrayType& pts, DenseIndex degree);
231 
241  template <typename PointArrayType>
242  static SplineType Interpolate(const PointArrayType& pts, DenseIndex degree, const KnotVectorType& knot_parameters);
243 
261  template <typename PointArrayType, typename IndexArray>
262  static SplineType InterpolateWithDerivatives(const PointArrayType& points,
263  const PointArrayType& derivatives,
264  const IndexArray& derivativeIndices,
265  const unsigned int degree);
266 
283  template <typename PointArrayType, typename IndexArray>
284  static SplineType InterpolateWithDerivatives(const PointArrayType& points,
285  const PointArrayType& derivatives,
286  const IndexArray& derivativeIndices,
287  const unsigned int degree,
288  const ParameterVectorType& parameters);
289  };
290 
291  template <typename SplineType>
292  template <typename PointArrayType>
293  SplineType SplineFitting<SplineType>::Interpolate(const PointArrayType& pts, DenseIndex degree, const KnotVectorType& knot_parameters)
294  {
295  typedef typename SplineType::KnotVectorType::Scalar Scalar;
296  typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
297 
299 
300  KnotVectorType knots;
301  KnotAveraging(knot_parameters, degree, knots);
302 
303  DenseIndex n = pts.cols();
304  MatrixType A = MatrixType::Zero(n,n);
305  for (DenseIndex i=1; i<n-1; ++i)
306  {
307  const DenseIndex span = SplineType::Span(knot_parameters[i], degree, knots);
308 
309  // The segment call should somehow be told the spline order at compile time.
310  A.row(i).segment(span-degree, degree+1) = SplineType::BasisFunctions(knot_parameters[i], degree, knots);
311  }
312  A(0,0) = 1.0;
313  A(n-1,n-1) = 1.0;
314 
316 
317  // Here, we are creating a temporary due to an Eigen issue.
318  ControlPointVectorType ctrls = qr.solve(MatrixType(pts.transpose())).transpose();
319 
320  return SplineType(knots, ctrls);
321  }
322 
323  template <typename SplineType>
324  template <typename PointArrayType>
325  SplineType SplineFitting<SplineType>::Interpolate(const PointArrayType& pts, DenseIndex degree)
326  {
327  KnotVectorType chord_lengths; // knot parameters
328  ChordLengths(pts, chord_lengths);
329  return Interpolate(pts, degree, chord_lengths);
330  }
331 
332  template <typename SplineType>
333  template <typename PointArrayType, typename IndexArray>
334  SplineType
336  const PointArrayType& derivatives,
337  const IndexArray& derivativeIndices,
338  const unsigned int degree,
339  const ParameterVectorType& parameters)
340  {
341  typedef typename SplineType::KnotVectorType::Scalar Scalar;
342  typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
343 
345 
346  const DenseIndex n = points.cols() + derivatives.cols();
347 
348  KnotVectorType knots;
349 
350  KnotAveragingWithDerivatives(parameters, degree, derivativeIndices, knots);
351 
352  // fill matrix
353  MatrixType A = MatrixType::Zero(n, n);
354 
355  // Use these dimensions for quicker populating, then transpose for solving.
356  MatrixType b(points.rows(), n);
357 
358  DenseIndex startRow;
359  DenseIndex derivativeStart;
360 
361  // End derivatives.
362  if (derivativeIndices[0] == 0)
363  {
364  A.template block<1, 2>(1, 0) << -1, 1;
365 
366  Scalar y = (knots(degree + 1) - knots(0)) / degree;
367  b.col(1) = y*derivatives.col(0);
368 
369  startRow = 2;
370  derivativeStart = 1;
371  }
372  else
373  {
374  startRow = 1;
375  derivativeStart = 0;
376  }
377  if (derivativeIndices[derivatives.cols() - 1] == points.cols() - 1)
378  {
379  A.template block<1, 2>(n - 2, n - 2) << -1, 1;
380 
381  Scalar y = (knots(knots.size() - 1) - knots(knots.size() - (degree + 2))) / degree;
382  b.col(b.cols() - 2) = y*derivatives.col(derivatives.cols() - 1);
383  }
384 
385  DenseIndex row = startRow;
386  DenseIndex derivativeIndex = derivativeStart;
387  for (DenseIndex i = 1; i < parameters.size() - 1; ++i)
388  {
389  const DenseIndex span = SplineType::Span(parameters[i], degree, knots);
390 
391  if (derivativeIndex < derivativeIndices.size() && derivativeIndices[derivativeIndex] == i)
392  {
393  A.block(row, span - degree, 2, degree + 1)
394  = SplineType::BasisFunctionDerivatives(parameters[i], 1, degree, knots);
395 
396  b.col(row++) = points.col(i);
397  b.col(row++) = derivatives.col(derivativeIndex++);
398  }
399  else
400  {
401  A.row(row).segment(span - degree, degree + 1)
402  = SplineType::BasisFunctions(parameters[i], degree, knots);
403  b.col(row++) = points.col(i);
404  }
405  }
406  b.col(0) = points.col(0);
407  b.col(b.cols() - 1) = points.col(points.cols() - 1);
408  A(0,0) = 1;
409  A(n - 1, n - 1) = 1;
410 
411  // Solve
413  ControlPointVectorType controlPoints = lu.solve(MatrixType(b.transpose())).transpose();
414 
415  SplineType spline(knots, controlPoints);
416 
417  return spline;
418  }
419 
420  template <typename SplineType>
421  template <typename PointArrayType, typename IndexArray>
422  SplineType
424  const PointArrayType& derivatives,
425  const IndexArray& derivativeIndices,
426  const unsigned int degree)
427  {
428  ParameterVectorType parameters;
429  ChordLengths(points, parameters);
430  return InterpolateWithDerivatives(points, derivatives, derivativeIndices, degree, parameters);
431  }
432 }
433 
434 #endif // EIGEN_SPLINE_FITTING_H
int n
SparseMatrix< double > A(n, n)
int i
RowXpr row(Index i) const
Matrix3f y
HouseholderQR< MatrixXf > qr(A)
Matrix< float, 1, Dynamic > MatrixType
cout<< "Here is the matrix m:"<< endl<< m<< endl;Eigen::FullPivLU< Matrix5x3 > lu(m)
void ChordLengths(const PointArrayType &pts, KnotVectorType &chord_lengths)
Computes chord length parameters which are required for spline interpolation.
void KnotAveraging(const KnotVectorType &parameters, DenseIndex degree, KnotVectorType &knots)
Computes knot averages.
Definition: SplineFitting.h:48
void KnotAveragingWithDerivatives(const ParameterVectorType &parameters, const unsigned int degree, const IndexArray &derivativeIndices, KnotVectorType &knots)
Computes knot averages when derivative constraints are present. Note that this is a technical interpr...
Definition: SplineFitting.h:81
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Spline fitting methods.
SplineType::KnotVectorType KnotVectorType
static SplineType InterpolateWithDerivatives(const PointArrayType &points, const PointArrayType &derivatives, const IndexArray &derivativeIndices, const unsigned int degree)
Fits an interpolating spline to the given data points and derivatives.
static SplineType Interpolate(const PointArrayType &pts, DenseIndex degree)
Fits an interpolating Spline to the given data points.
SplineType::ParameterVectorType ParameterVectorType
std::ptrdiff_t j