10 #ifndef EIGEN_SPLINE_FITTING_H
11 #define EIGEN_SPLINE_FITTING_H
18 #include "./InternalHeaderCheck.h"
20 #include "SplineFwd.h"
22 #include "../../../../Eigen/LU"
23 #include "../../../../Eigen/QR"
47 template <
typename KnotVectorType>
48 void KnotAveraging(
const KnotVectorType& parameters, DenseIndex degree, KnotVectorType& knots)
50 knots.resize(parameters.size()+degree+1);
52 for (DenseIndex j=1; j<parameters.size()-degree; ++j)
53 knots(j+degree) = parameters.segment(j,degree).mean();
55 knots.segment(0,degree+1) = KnotVectorType::Zero(degree+1);
56 knots.segment(knots.size()-degree-1,degree+1) = KnotVectorType::Ones(degree+1);
80 template <
typename KnotVectorType,
typename ParameterVectorType,
typename IndexArray>
82 const unsigned int degree,
83 const IndexArray& derivativeIndices,
84 KnotVectorType& knots)
86 typedef typename ParameterVectorType::Scalar Scalar;
88 DenseIndex numParameters = parameters.size();
89 DenseIndex numDerivatives = derivativeIndices.size();
91 if (numDerivatives < 1)
97 DenseIndex startIndex;
100 DenseIndex numInternalDerivatives = numDerivatives;
102 if (derivativeIndices[0] == 0)
105 --numInternalDerivatives;
111 if (derivativeIndices[numDerivatives - 1] == numParameters - 1)
113 endIndex = numParameters - degree;
114 --numInternalDerivatives;
118 endIndex = numParameters - degree - 1;
123 DenseIndex numAverageKnots = endIndex - startIndex + 3;
124 KnotVectorType averageKnots(numAverageKnots);
125 averageKnots[0] = parameters[0];
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];
134 ParameterVectorType temporaryParameters(numParameters + 1);
135 KnotVectorType derivativeKnots(numInternalDerivatives);
136 for (DenseIndex i = 0; i < numAverageKnots - 1; ++i)
138 temporaryParameters[0] = averageKnots[i];
139 ParameterVectorType parameterIndices(numParameters);
140 int temporaryParameterIndex = 1;
141 for (DenseIndex j = 0; j < numParameters; ++j)
143 Scalar parameter = parameters[j];
144 if (parameter >= averageKnots[i] && parameter < averageKnots[i + 1])
146 parameterIndices[temporaryParameterIndex] = j;
147 temporaryParameters[temporaryParameterIndex++] = parameter;
150 temporaryParameters[temporaryParameterIndex] = averageKnots[i + 1];
152 for (
int j = 0; j <= temporaryParameterIndex - 2; ++j)
154 for (DenseIndex k = 0; k < derivativeIndices.size(); ++k)
156 if (parameterIndices[j + 1] == derivativeIndices[k]
157 && parameterIndices[j + 1] != 0
158 && parameterIndices[j + 1] != numParameters - 1)
160 derivativeKnots[++newKnotIndex] = temporaryParameters.segment(j, 3).mean();
167 KnotVectorType temporaryKnots(averageKnots.size() + derivativeKnots.size());
169 std::merge(averageKnots.data(), averageKnots.data() + averageKnots.size(),
170 derivativeKnots.data(), derivativeKnots.data() + derivativeKnots.size(),
171 temporaryKnots.data());
174 DenseIndex numKnots = numParameters + numDerivatives + degree + 1;
175 knots.resize(numKnots);
177 knots.head(degree).fill(temporaryKnots[0]);
178 knots.tail(degree).fill(temporaryKnots.template tail<1>()[0]);
179 knots.segment(degree, temporaryKnots.size()) = temporaryKnots;
191 template <
typename Po
intArrayType,
typename KnotVectorType>
192 void ChordLengths(
const PointArrayType& pts, KnotVectorType& chord_lengths)
194 typedef typename KnotVectorType::Scalar Scalar;
196 const DenseIndex n = pts.cols();
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();
204 std::partial_sum(chord_lengths.data(), chord_lengths.data()+n, chord_lengths.data());
207 chord_lengths /= chord_lengths(n-1);
208 chord_lengths(n-1) = Scalar(1);
215 template <
typename SplineType>
218 typedef typename SplineType::KnotVectorType KnotVectorType;
219 typedef typename SplineType::ParameterVectorType ParameterVectorType;
229 template <
typename Po
intArrayType>
230 static SplineType
Interpolate(
const PointArrayType& pts, DenseIndex degree);
241 template <
typename Po
intArrayType>
242 static SplineType
Interpolate(
const PointArrayType& pts, DenseIndex degree,
const KnotVectorType& knot_parameters);
261 template <
typename Po
intArrayType,
typename IndexArray>
263 const PointArrayType& derivatives,
264 const IndexArray& derivativeIndices,
265 const unsigned int degree);
283 template <
typename Po
intArrayType,
typename IndexArray>
285 const PointArrayType& derivatives,
286 const IndexArray& derivativeIndices,
287 const unsigned int degree,
288 const ParameterVectorType& parameters);
291 template <
typename SplineType>
292 template <
typename Po
intArrayType>
295 typedef typename SplineType::KnotVectorType::Scalar Scalar;
296 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
300 KnotVectorType knots;
303 DenseIndex n = pts.cols();
304 MatrixType A = MatrixType::Zero(n,n);
305 for (DenseIndex i=1; i<n-1; ++i)
307 const DenseIndex span = SplineType::Span(knot_parameters[i], degree, knots);
310 A.row(i).segment(span-degree, degree+1) = SplineType::BasisFunctions(knot_parameters[i], degree, knots);
318 ControlPointVectorType ctrls = qr.
solve(MatrixType(pts.transpose())).transpose();
320 return SplineType(knots, ctrls);
323 template <
typename SplineType>
324 template <
typename Po
intArrayType>
327 KnotVectorType chord_lengths;
329 return Interpolate(pts, degree, chord_lengths);
332 template <
typename SplineType>
333 template <
typename Po
intArrayType,
typename IndexArray>
336 const PointArrayType& derivatives,
337 const IndexArray& derivativeIndices,
338 const unsigned int degree,
339 const ParameterVectorType& parameters)
341 typedef typename SplineType::KnotVectorType::Scalar Scalar;
342 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
346 const DenseIndex n = points.cols() + derivatives.cols();
348 KnotVectorType knots;
353 MatrixType A = MatrixType::Zero(n, n);
356 MatrixType b(points.rows(), n);
359 DenseIndex derivativeStart;
362 if (derivativeIndices[0] == 0)
364 A.template block<1, 2>(1, 0) << -1, 1;
366 Scalar y = (knots(degree + 1) - knots(0)) / degree;
367 b.col(1) = y*derivatives.col(0);
377 if (derivativeIndices[derivatives.cols() - 1] == points.cols() - 1)
379 A.template block<1, 2>(n - 2, n - 2) << -1, 1;
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);
385 DenseIndex row = startRow;
386 DenseIndex derivativeIndex = derivativeStart;
387 for (DenseIndex i = 1; i < parameters.size() - 1; ++i)
389 const DenseIndex span = SplineType::Span(parameters[i], degree, knots);
391 if (derivativeIndex < derivativeIndices.size() && derivativeIndices[derivativeIndex] == i)
393 A.block(row, span - degree, 2, degree + 1)
394 = SplineType::BasisFunctionDerivatives(parameters[i], 1, degree, knots);
396 b.col(row++) = points.col(i);
397 b.col(row++) = derivatives.col(derivativeIndex++);
401 A.row(row).segment(span - degree, degree + 1)
402 = SplineType::BasisFunctions(parameters[i], degree, knots);
403 b.col(row++) = points.col(i);
406 b.col(0) = points.col(0);
407 b.col(b.cols() - 1) = points.col(points.cols() - 1);
413 ControlPointVectorType controlPoints = lu.
solve(MatrixType(b.transpose())).transpose();
415 SplineType spline(knots, controlPoints);
420 template <
typename SplineType>
421 template <
typename Po
intArrayType,
typename IndexArray>
424 const PointArrayType& derivatives,
425 const IndexArray& derivativeIndices,
426 const unsigned int degree)
428 ParameterVectorType parameters;
430 return InterpolateWithDerivatives(points, derivatives, derivativeIndices, degree, parameters);
const Solve< FullPivLU, Rhs > solve(const MatrixBase< Rhs > &b) const
const Solve< HouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
void ChordLengths(const PointArrayType &pts, KnotVectorType &chord_lengths)
Computes chord length parameters which are required for spline interpolation.
Definition: SplineFitting.h:192
void KnotAveraging(const KnotVectorType ¶meters, DenseIndex degree, KnotVectorType &knots)
Computes knot averages.
Definition: SplineFitting.h:48
void KnotAveragingWithDerivatives(const ParameterVectorType ¶meters, 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
Namespace containing all symbols from the Eigen library.
Spline fitting methods.
Definition: SplineFitting.h:217
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.
Definition: SplineFitting.h:423
static SplineType Interpolate(const PointArrayType &pts, DenseIndex degree)
Fits an interpolating Spline to the given data points.
Definition: SplineFitting.h:325