Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
SplineFitting.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2010-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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_SPLINE_FITTING_H
12#define EIGEN_SPLINE_FITTING_H
13
14#include <algorithm>
15#include <functional>
16#include <numeric>
17#include <vector>
18
19// IWYU pragma: private
20#include "./InternalHeaderCheck.h"
21
22#include "SplineFwd.h"
23
24#include "../../../../Eigen/LU"
25#include "../../../../Eigen/QR"
26
27namespace Eigen {
48template <typename KnotVectorType>
49void KnotAveraging(const KnotVectorType& parameters, DenseIndex degree, KnotVectorType& knots) {
50 knots.resize(parameters.size() + degree + 1);
51
52 for (DenseIndex j = 1; j < parameters.size() - degree; ++j) knots(j + degree) = parameters.segment(j, degree).mean();
53
54 // The boundary knots replicate the first and last parameter so that the
55 // spline domain matches the parameter range, whatever interval it spans.
56 knots.head(degree + 1).setConstant(parameters(0));
57 knots.tail(degree + 1).setConstant(parameters(placeholders::last));
58}
59
81template <typename KnotVectorType, typename ParameterVectorType, typename IndexArray>
82void KnotAveragingWithDerivatives(const ParameterVectorType& parameters, const unsigned int degree,
83 const IndexArray& derivativeIndices, KnotVectorType& knots) {
84 typedef typename ParameterVectorType::Scalar Scalar;
85
86 DenseIndex numParameters = parameters.size();
87 DenseIndex numDerivatives = derivativeIndices.size();
88
89 if (numDerivatives < 1) {
90 KnotAveraging(parameters, degree, knots);
91 return;
92 }
93
94 DenseIndex startIndex;
95 DenseIndex endIndex;
96
97 DenseIndex numInternalDerivatives = numDerivatives;
98
99 if (derivativeIndices[0] == 0) {
100 startIndex = 0;
101 --numInternalDerivatives;
102 } else {
103 startIndex = 1;
104 }
105 if (derivativeIndices[numDerivatives - 1] == numParameters - 1) {
106 endIndex = numParameters - degree;
107 --numInternalDerivatives;
108 } else {
109 endIndex = numParameters - degree - 1;
110 }
111
112 // There are (endIndex - startIndex + 1) knots obtained from the averaging
113 // and 2 for the first and last parameters.
114 DenseIndex numAverageKnots = endIndex - startIndex + 3;
115 KnotVectorType averageKnots(numAverageKnots);
116 averageKnots[0] = parameters[0];
117
118 int newKnotIndex = 0;
119 for (DenseIndex i = startIndex; i <= endIndex; ++i)
120 averageKnots[++newKnotIndex] = parameters.segment(i, degree).mean();
121 averageKnots[++newKnotIndex] = parameters[numParameters - 1];
122
123 newKnotIndex = -1;
124
125 ParameterVectorType temporaryParameters(numParameters + 1);
126 KnotVectorType derivativeKnots(numInternalDerivatives);
127 for (DenseIndex i = 0; i < numAverageKnots - 1; ++i) {
128 temporaryParameters[0] = averageKnots[i];
129 ParameterVectorType parameterIndices(numParameters);
130 int temporaryParameterIndex = 1;
131 for (DenseIndex j = 0; j < numParameters; ++j) {
132 Scalar parameter = parameters[j];
133 if (parameter >= averageKnots[i] && parameter < averageKnots[i + 1]) {
134 parameterIndices[temporaryParameterIndex] = j;
135 temporaryParameters[temporaryParameterIndex++] = parameter;
136 }
137 }
138 temporaryParameters[temporaryParameterIndex] = averageKnots[i + 1];
139
140 for (int j = 0; j <= temporaryParameterIndex - 2; ++j) {
141 for (DenseIndex k = 0; k < derivativeIndices.size(); ++k) {
142 if (parameterIndices[j + 1] == derivativeIndices[k] && parameterIndices[j + 1] != 0 &&
143 parameterIndices[j + 1] != numParameters - 1) {
144 derivativeKnots[++newKnotIndex] = temporaryParameters.segment(j, 3).mean();
145 break;
146 }
147 }
148 }
149 }
150
151 KnotVectorType temporaryKnots(averageKnots.size() + derivativeKnots.size());
152
153 std::merge(averageKnots.data(), averageKnots.data() + averageKnots.size(), derivativeKnots.data(),
154 derivativeKnots.data() + derivativeKnots.size(), temporaryKnots.data());
155
156 // Number of knots (one for each point and derivative) plus spline order.
157 DenseIndex numKnots = numParameters + numDerivatives + degree + 1;
158 knots.resize(numKnots);
159
160 knots.head(degree).fill(temporaryKnots[0]);
161 knots.tail(degree).fill(temporaryKnots.template tail<1>()[0]);
162 knots.segment(degree, temporaryKnots.size()) = temporaryKnots;
163}
164
174template <typename PointArrayType, typename KnotVectorType>
175void ChordLengths(const PointArrayType& pts, KnotVectorType& chord_lengths) {
176 typedef typename KnotVectorType::Scalar Scalar;
177
178 const DenseIndex n = pts.cols();
179
180 // 1. compute the column-wise norms
181 chord_lengths.resize(pts.cols());
182 chord_lengths[0] = 0;
183 chord_lengths.rightCols(n - 1) =
184 (pts.array().leftCols(n - 1) - pts.array().rightCols(n - 1)).matrix().colwise().norm();
185
186 // 2. compute the partial sums
187 std::partial_sum(chord_lengths.data(), chord_lengths.data() + n, chord_lengths.data());
188
189 // 3. normalize the data
190 chord_lengths /= chord_lengths(n - 1);
191 chord_lengths(n - 1) = Scalar(1);
192}
193
198template <typename SplineType>
200 typedef typename SplineType::KnotVectorType KnotVectorType;
201 typedef typename SplineType::ParameterVectorType ParameterVectorType;
202
211 template <typename PointArrayType>
212 static SplineType Interpolate(const PointArrayType& pts, DenseIndex degree);
213
223 template <typename PointArrayType>
224 static SplineType Interpolate(const PointArrayType& pts, DenseIndex degree, const KnotVectorType& knot_parameters);
225
243 template <typename PointArrayType, typename IndexArray>
244 static SplineType InterpolateWithDerivatives(const PointArrayType& points, const PointArrayType& derivatives,
245 const IndexArray& derivativeIndices, const unsigned int degree);
246
263 template <typename PointArrayType, typename IndexArray>
264 static SplineType InterpolateWithDerivatives(const PointArrayType& points, const PointArrayType& derivatives,
265 const IndexArray& derivativeIndices, const unsigned int degree,
266 const ParameterVectorType& parameters);
267};
268
269template <typename SplineType>
270template <typename PointArrayType>
271SplineType SplineFitting<SplineType>::Interpolate(const PointArrayType& pts, DenseIndex degree,
272 const KnotVectorType& knot_parameters) {
273 typedef typename SplineType::KnotVectorType::Scalar Scalar;
274 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
275
276 typedef Matrix<Scalar, Dynamic, Dynamic> MatrixType;
277
278 KnotVectorType knots;
279 KnotAveraging(knot_parameters, degree, knots);
280
281 DenseIndex n = pts.cols();
282 MatrixType A = MatrixType::Zero(n, n);
283 for (DenseIndex i = 1; i < n - 1; ++i) {
284 const DenseIndex span = SplineType::Span(knot_parameters[i], degree, knots);
285
286 // The segment call should somehow be told the spline order at compile time.
287 A.row(i).segment(span - degree, degree + 1) = SplineType::BasisFunctions(knot_parameters[i], degree, knots);
288 }
289 A(0, 0) = 1.0;
290 A(n - 1, n - 1) = 1.0;
291
293
294 // Here, we are creating a temporary due to an Eigen issue.
295 ControlPointVectorType ctrls = qr.solve(MatrixType(pts.transpose())).transpose();
296
297 return SplineType(knots, ctrls);
298}
299
300template <typename SplineType>
301template <typename PointArrayType>
302SplineType SplineFitting<SplineType>::Interpolate(const PointArrayType& pts, DenseIndex degree) {
303 KnotVectorType chord_lengths; // knot parameters
304 ChordLengths(pts, chord_lengths);
305 return Interpolate(pts, degree, chord_lengths);
306}
307
308template <typename SplineType>
309template <typename PointArrayType, typename IndexArray>
310SplineType SplineFitting<SplineType>::InterpolateWithDerivatives(const PointArrayType& points,
311 const PointArrayType& derivatives,
312 const IndexArray& derivativeIndices,
313 const unsigned int degree,
314 const ParameterVectorType& parameters) {
315 typedef typename SplineType::KnotVectorType::Scalar Scalar;
316 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
317
318 typedef Matrix<Scalar, Dynamic, Dynamic> MatrixType;
319
320 const DenseIndex n = points.cols() + derivatives.cols();
321
322 KnotVectorType knots;
323
324 KnotAveragingWithDerivatives(parameters, degree, derivativeIndices, knots);
325
326 // fill matrix
327 MatrixType A = MatrixType::Zero(n, n);
328
329 // Use these dimensions for quicker populating, then transpose for solving.
330 MatrixType b(points.rows(), n);
331
332 DenseIndex startRow;
333 DenseIndex derivativeStart;
334
335 // End derivatives.
336 if (derivativeIndices[0] == 0) {
337 A.template block<1, 2>(1, 0) << -1, 1;
338
339 Scalar y = (knots(degree + 1) - knots(0)) / degree;
340 b.col(1) = y * derivatives.col(0);
341
342 startRow = 2;
343 derivativeStart = 1;
344 } else {
345 startRow = 1;
346 derivativeStart = 0;
347 }
348 if (derivativeIndices[derivatives.cols() - 1] == points.cols() - 1) {
349 A.template block<1, 2>(n - 2, n - 2) << -1, 1;
350
351 Scalar y = (knots(knots.size() - 1) - knots(knots.size() - (degree + 2))) / degree;
352 b.col(b.cols() - 2) = y * derivatives.col(derivatives.cols() - 1);
353 }
354
355 DenseIndex row = startRow;
356 DenseIndex derivativeIndex = derivativeStart;
357 for (DenseIndex i = 1; i < parameters.size() - 1; ++i) {
358 const DenseIndex span = SplineType::Span(parameters[i], degree, knots);
359
360 if (derivativeIndex < derivativeIndices.size() && derivativeIndices[derivativeIndex] == i) {
361 A.block(row, span - degree, 2, degree + 1) =
362 SplineType::BasisFunctionDerivatives(parameters[i], 1, degree, knots);
363
364 b.col(row++) = points.col(i);
365 b.col(row++) = derivatives.col(derivativeIndex++);
366 } else {
367 A.row(row).segment(span - degree, degree + 1) = SplineType::BasisFunctions(parameters[i], degree, knots);
368 b.col(row++) = points.col(i);
369 }
370 }
371 b.col(0) = points.col(0);
372 b.col(b.cols() - 1) = points.col(points.cols() - 1);
373 A(0, 0) = 1;
374 A(n - 1, n - 1) = 1;
375
376 // Solve
378 ControlPointVectorType controlPoints = lu.solve(MatrixType(b.transpose())).transpose();
379
380 SplineType spline(knots, controlPoints);
381
382 return spline;
383}
384
385template <typename SplineType>
386template <typename PointArrayType, typename IndexArray>
387SplineType SplineFitting<SplineType>::InterpolateWithDerivatives(const PointArrayType& points,
388 const PointArrayType& derivatives,
389 const IndexArray& derivativeIndices,
390 const unsigned int degree) {
391 ParameterVectorType parameters;
392 ChordLengths(points, parameters);
393 return InterpolateWithDerivatives(points, derivatives, derivativeIndices, degree, parameters);
394}
395} // namespace Eigen
396
397#endif // EIGEN_SPLINE_FITTING_H
Solve< FullPivLU, Rhs > solve(const MatrixBase< Rhs > &b) const
Solve< HouseholderQR, Rhs > solve(const MatrixBase< Rhs > &b) const
static constexpr const last_t last
void ChordLengths(const PointArrayType &pts, KnotVectorType &chord_lengths)
Computes chord length parameters which are required for spline interpolation.
Definition SplineFitting.h:175
void KnotAveraging(const KnotVectorType &parameters, DenseIndex degree, KnotVectorType &knots)
Computes knot averages.
Definition SplineFitting.h:49
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:82
Namespace containing all symbols from the Eigen library.
Spline fitting methods.
Definition SplineFitting.h:199
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:387
static SplineType Interpolate(const PointArrayType &pts, DenseIndex degree)
Fits an interpolating Spline to the given data points.
Definition SplineFitting.h:302