10 #ifndef EIGEN_SPLINE_FITTING_H
11 #define EIGEN_SPLINE_FITTING_H
20 #include "../../../../Eigen/LU"
21 #include "../../../../Eigen/QR"
44 template <
typename KnotVectorType>
52 knots.segment(0,
degree+1) = KnotVectorType::Zero(
degree+1);
77 template <
typename KnotVectorType,
typename ParameterVectorType,
typename IndexArray>
80 const IndexArray& derivativeIndices,
81 KnotVectorType& knots)
86 DenseIndex numDerivatives = derivativeIndices.size();
88 if (numDerivatives < 1)
97 DenseIndex numInternalDerivatives = numDerivatives;
99 if (derivativeIndices[0] == 0)
102 --numInternalDerivatives;
108 if (derivativeIndices[numDerivatives - 1] == numParameters - 1)
110 endIndex = numParameters -
degree;
111 --numInternalDerivatives;
115 endIndex = numParameters -
degree - 1;
120 DenseIndex numAverageKnots = endIndex - startIndex + 3;
121 KnotVectorType averageKnots(numAverageKnots);
124 int newKnotIndex = 0;
127 averageKnots[++newKnotIndex] =
parameters[numParameters - 1];
131 ParameterVectorType temporaryParameters(numParameters + 1);
132 KnotVectorType derivativeKnots(numInternalDerivatives);
135 temporaryParameters[0] = averageKnots[
i];
136 ParameterVectorType parameterIndices(numParameters);
137 int temporaryParameterIndex = 1;
141 if (parameter >= averageKnots[
i] && parameter < averageKnots[
i + 1])
143 parameterIndices[temporaryParameterIndex] =
j;
144 temporaryParameters[temporaryParameterIndex++] = parameter;
147 temporaryParameters[temporaryParameterIndex] = averageKnots[
i + 1];
149 for (
int j = 0;
j <= temporaryParameterIndex - 2; ++
j)
151 for (
DenseIndex k = 0; k < derivativeIndices.size(); ++k)
153 if (parameterIndices[
j + 1] == derivativeIndices[k]
154 && parameterIndices[
j + 1] != 0
155 && parameterIndices[
j + 1] != numParameters - 1)
157 derivativeKnots[++newKnotIndex] = temporaryParameters.segment(
j, 3).mean();
164 KnotVectorType temporaryKnots(averageKnots.size() + derivativeKnots.size());
166 std::merge(averageKnots.data(), averageKnots.data() + averageKnots.size(),
167 derivativeKnots.data(), derivativeKnots.data() + derivativeKnots.size(),
168 temporaryKnots.data());
172 knots.resize(numKnots);
174 knots.head(
degree).fill(temporaryKnots[0]);
175 knots.tail(
degree).fill(temporaryKnots.template tail<1>()[0]);
176 knots.segment(
degree, temporaryKnots.size()) = temporaryKnots;
188 template <
typename Po
intArrayType,
typename KnotVectorType>
189 void ChordLengths(
const PointArrayType& pts, KnotVectorType& chord_lengths)
196 chord_lengths.resize(pts.cols());
197 chord_lengths[0] = 0;
198 chord_lengths.rightCols(
n-1) = (pts.array().leftCols(
n-1) - pts.array().rightCols(
n-1)).
matrix().colwise().norm();
201 std::partial_sum(chord_lengths.data(), chord_lengths.data()+
n, chord_lengths.data());
204 chord_lengths /= chord_lengths(
n-1);
205 chord_lengths(
n-1) =
Scalar(1);
212 template <
typename SplineType>
226 template <
typename Po
intArrayType>
238 template <
typename Po
intArrayType>
258 template <
typename Po
intArrayType,
typename IndexArray>
260 const PointArrayType& derivatives,
261 const IndexArray& derivativeIndices,
262 const unsigned int degree);
280 template <
typename Po
intArrayType,
typename IndexArray>
282 const PointArrayType& derivatives,
283 const IndexArray& derivativeIndices,
284 const unsigned int degree,
288 template <
typename SplineType>
289 template <
typename Po
intArrayType>
293 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
307 A.row(
i).segment(span-
degree,
degree+1) = SplineType::BasisFunctions(knot_parameters[
i],
degree, knots);
315 ControlPointVectorType ctrls =
qr.solve(
MatrixType(pts.transpose())).transpose();
317 return SplineType(knots, ctrls);
320 template <
typename SplineType>
321 template <
typename Po
intArrayType>
326 return Interpolate(pts,
degree, chord_lengths);
329 template <
typename SplineType>
330 template <
typename Po
intArrayType,
typename IndexArray>
333 const PointArrayType& derivatives,
334 const IndexArray& derivativeIndices,
335 const unsigned int degree,
339 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
343 const DenseIndex n = points.cols() + derivatives.cols();
359 if (derivativeIndices[0] == 0)
361 A.template block<1, 2>(1, 0) << -1, 1;
364 b.col(1) =
y*derivatives.col(0);
374 if (derivativeIndices[derivatives.cols() - 1] == points.cols() - 1)
376 A.template block<1, 2>(
n - 2,
n - 2) << -1, 1;
379 b.col(
b.cols() - 2) =
y*derivatives.col(derivatives.cols() - 1);
388 if (derivativeIndex < derivativeIndices.size() && derivativeIndices[derivativeIndex] ==
i)
393 b.col(
row++) = points.col(
i);
394 b.col(
row++) = derivatives.col(derivativeIndex++);
400 b.col(
row++) = points.col(
i);
403 b.col(0) = points.col(0);
404 b.col(
b.cols() - 1) = points.col(points.cols() - 1);
410 ControlPointVectorType controlPoints =
lu.solve(
MatrixType(
b.transpose())).transpose();
412 SplineType spline(knots, controlPoints);
417 template <
typename SplineType>
418 template <
typename Po
intArrayType,
typename IndexArray>
421 const PointArrayType& derivatives,
422 const IndexArray& derivativeIndices,
423 const unsigned int degree)
427 return InterpolateWithDerivatives(points, derivatives, derivativeIndices,
degree,
parameters);
431 #endif // EIGEN_SPLINE_FITTING_H