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 <numeric>
14 
15 #include "SplineFwd.h"
16 
17 #include <Eigen/QR>
18 
19 namespace Eigen
20 {
40  template <typename KnotVectorType>
41  void KnotAveraging(const KnotVectorType& parameters, DenseIndex degree, KnotVectorType& knots)
42  {
43  knots.resize(parameters.size()+degree+1);
44 
45  for (DenseIndex j=1; j<parameters.size()-degree; ++j)
46  knots(j+degree) = parameters.segment(j,degree).mean();
47 
48  knots.segment(0,degree+1) = KnotVectorType::Zero(degree+1);
49  knots.segment(knots.size()-degree-1,degree+1) = KnotVectorType::Ones(degree+1);
50  }
51 
61  template <typename PointArrayType, typename KnotVectorType>
62  void ChordLengths(const PointArrayType& pts, KnotVectorType& chord_lengths)
63  {
64  typedef typename KnotVectorType::Scalar Scalar;
65 
66  const DenseIndex n = pts.cols();
67 
68  // 1. compute the column-wise norms
69  chord_lengths.resize(pts.cols());
70  chord_lengths[0] = 0;
71  chord_lengths.rightCols(n-1) = (pts.array().leftCols(n-1) - pts.array().rightCols(n-1)).matrix().colwise().norm();
72 
73  // 2. compute the partial sums
74  std::partial_sum(chord_lengths.data(), chord_lengths.data()+n, chord_lengths.data());
75 
76  // 3. normalize the data
77  chord_lengths /= chord_lengths(n-1);
78  chord_lengths(n-1) = Scalar(1);
79  }
80 
85  template <typename SplineType>
87  {
88  typedef typename SplineType::KnotVectorType KnotVectorType;
89 
98  template <typename PointArrayType>
99  static SplineType Interpolate(const PointArrayType& pts, DenseIndex degree);
100 
110  template <typename PointArrayType>
111  static SplineType Interpolate(const PointArrayType& pts, DenseIndex degree, const KnotVectorType& knot_parameters);
112  };
113 
114  template <typename SplineType>
115  template <typename PointArrayType>
116  SplineType SplineFitting<SplineType>::Interpolate(const PointArrayType& pts, DenseIndex degree, const KnotVectorType& knot_parameters)
117  {
118  typedef typename SplineType::KnotVectorType::Scalar Scalar;
119  typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
120 
121  typedef Matrix<Scalar,Dynamic,Dynamic> MatrixType;
122 
123  KnotVectorType knots;
124  KnotAveraging(knot_parameters, degree, knots);
125 
126  DenseIndex n = pts.cols();
127  MatrixType A = MatrixType::Zero(n,n);
128  for (DenseIndex i=1; i<n-1; ++i)
129  {
130  const DenseIndex span = SplineType::Span(knot_parameters[i], degree, knots);
131 
132  // The segment call should somehow be told the spline order at compile time.
133  A.row(i).segment(span-degree, degree+1) = SplineType::BasisFunctions(knot_parameters[i], degree, knots);
134  }
135  A(0,0) = 1.0;
136  A(n-1,n-1) = 1.0;
137 
139 
140  // Here, we are creating a temporary due to an Eigen issue.
141  ControlPointVectorType ctrls = qr.solve(MatrixType(pts.transpose())).transpose();
142 
143  return SplineType(knots, ctrls);
144  }
145 
146  template <typename SplineType>
147  template <typename PointArrayType>
148  SplineType SplineFitting<SplineType>::Interpolate(const PointArrayType& pts, DenseIndex degree)
149  {
150  KnotVectorType chord_lengths; // knot parameters
151  ChordLengths(pts, chord_lengths);
152  return Interpolate(pts, degree, chord_lengths);
153  }
154 }
155 
156 #endif // EIGEN_SPLINE_FITTING_H
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
const internal::solve_retval< HouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
Spline fitting methods.
Definition: SplineFitting.h:86
void KnotAveraging(const KnotVectorType &parameters, DenseIndex degree, KnotVectorType &knots)
Computes knot averages.The knots are computed as where is the degree and the number knots of the d...
Definition: SplineFitting.h:41
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: XprHelper.h:27
static SplineType Interpolate(const PointArrayType &pts, DenseIndex degree)
Fits an interpolating Spline to the given data points.
void ChordLengths(const PointArrayType &pts, KnotVectorType &chord_lengths)
Computes chord length parameters which are required for spline interpolation.
Definition: SplineFitting.h:62
SplineType::KnotVectorType KnotVectorType
Definition: SplineFitting.h:88


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:10