10 #ifndef EIGEN_SPLINE_FITTING_H
11 #define EIGEN_SPLINE_FITTING_H
22 #include "../../../../Eigen/LU"
23 #include "../../../../Eigen/QR"
47 template <
typename KnotVectorType>
50 knots.resize(parameters.size()+degree+1);
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;
89 DenseIndex numDerivatives = derivativeIndices.size();
91 if (numDerivatives < 1)
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;
129 averageKnots[++newKnotIndex] = parameters.segment(
i, degree).mean();
130 averageKnots[++newKnotIndex] = parameters[numParameters - 1];
134 ParameterVectorType temporaryParameters(numParameters + 1);
135 KnotVectorType derivativeKnots(numInternalDerivatives);
138 temporaryParameters[0] = averageKnots[
i];
139 ParameterVectorType parameterIndices(numParameters);
140 int temporaryParameterIndex = 1;
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;
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>
229 template <
typename Po
intArrayType>
241 template <
typename Po
intArrayType>
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,
291 template <
typename SplineType>
292 template <
typename Po
intArrayType>
295 typedef typename SplineType::KnotVectorType::Scalar Scalar;
296 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
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>
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,
341 typedef typename SplineType::KnotVectorType::Scalar Scalar;
342 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
346 const DenseIndex n = points.cols() + derivatives.cols();
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);
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)
430 return InterpolateWithDerivatives(points, derivatives, derivativeIndices, degree, parameters);
SparseMatrix< double > A(n, n)
RowXpr row(Index i) const
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 ¶meters, DenseIndex degree, KnotVectorType &knots)
Computes knot averages.
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...
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
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