25 #ifndef EIGEN_SPLINE_FITTING_H
26 #define EIGEN_SPLINE_FITTING_H
30 #include "SplineFwd.h"
55 template <
typename KnotVectorType>
56 void KnotAveraging(
const KnotVectorType& parameters, DenseIndex degree, KnotVectorType& knots)
58 typedef typename KnotVectorType::Scalar Scalar;
60 knots.resize(parameters.size()+degree+1);
62 for (DenseIndex j=1; j<parameters.size()-degree; ++j)
63 knots(j+degree) = parameters.segment(j,degree).mean();
65 knots.segment(0,degree+1) = KnotVectorType::Zero(degree+1);
66 knots.segment(knots.size()-degree-1,degree+1) = KnotVectorType::Ones(degree+1);
78 template <
typename Po
intArrayType,
typename KnotVectorType>
79 void ChordLengths(
const PointArrayType& pts, KnotVectorType& chord_lengths)
81 typedef typename KnotVectorType::Scalar Scalar;
83 const DenseIndex n = pts.cols();
86 chord_lengths.resize(pts.cols());
88 chord_lengths.rightCols(n-1) = (pts.array().leftCols(n-1) - pts.array().rightCols(n-1)).matrix().colwise().norm();
91 std::partial_sum(chord_lengths.data(), chord_lengths.data()+n, chord_lengths.data());
94 chord_lengths /= chord_lengths(n-1);
95 chord_lengths(n-1) = Scalar(1);
102 template <
typename SplineType>
105 typedef typename SplineType::KnotVectorType KnotVectorType;
115 template <
typename Po
intArrayType>
116 static SplineType
Interpolate(
const PointArrayType& pts, DenseIndex degree);
127 template <
typename Po
intArrayType>
128 static SplineType
Interpolate(
const PointArrayType& pts, DenseIndex degree,
const KnotVectorType& knot_parameters);
131 template <
typename SplineType>
132 template <
typename Po
intArrayType>
135 typedef typename SplineType::KnotVectorType::Scalar Scalar;
136 typedef typename SplineType::BasisVectorType BasisVectorType;
137 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
139 typedef Matrix<Scalar,Dynamic,Dynamic> MatrixType;
141 KnotVectorType knots;
144 DenseIndex n = pts.cols();
145 MatrixType A = MatrixType::Zero(n,n);
146 for (DenseIndex i=1; i<n-1; ++i)
148 const DenseIndex span = SplineType::Span(knot_parameters[i], degree, knots);
151 A.row(i).segment(span-degree, degree+1) = SplineType::BasisFunctions(knot_parameters[i], degree, knots);
156 HouseholderQR<MatrixType> qr(A);
159 ControlPointVectorType ctrls = qr.solve(MatrixType(pts.transpose())).transpose();
161 return SplineType(knots, ctrls);
164 template <
typename SplineType>
165 template <
typename Po
intArrayType>
168 KnotVectorType chord_lengths;
170 return Interpolate(pts, degree, chord_lengths);
174 #endif // EIGEN_SPLINE_FITTING_H