83 const IndexArray& derivativeIndices, KnotVectorType& knots) {
84 typedef typename ParameterVectorType::Scalar Scalar;
86 DenseIndex numParameters = parameters.size();
87 DenseIndex numDerivatives = derivativeIndices.size();
89 if (numDerivatives < 1) {
94 DenseIndex startIndex;
97 DenseIndex numInternalDerivatives = numDerivatives;
99 if (derivativeIndices[0] == 0) {
101 --numInternalDerivatives;
105 if (derivativeIndices[numDerivatives - 1] == numParameters - 1) {
106 endIndex = numParameters - degree;
107 --numInternalDerivatives;
109 endIndex = numParameters - degree - 1;
114 DenseIndex numAverageKnots = endIndex - startIndex + 3;
115 KnotVectorType averageKnots(numAverageKnots);
116 averageKnots[0] = parameters[0];
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];
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;
138 temporaryParameters[temporaryParameterIndex] = averageKnots[i + 1];
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();
151 KnotVectorType temporaryKnots(averageKnots.size() + derivativeKnots.size());
153 std::merge(averageKnots.data(), averageKnots.data() + averageKnots.size(), derivativeKnots.data(),
154 derivativeKnots.data() + derivativeKnots.size(), temporaryKnots.data());
157 DenseIndex numKnots = numParameters + numDerivatives + degree + 1;
158 knots.resize(numKnots);
160 knots.head(degree).fill(temporaryKnots[0]);
161 knots.tail(degree).fill(temporaryKnots.template tail<1>()[0]);
162 knots.segment(degree, temporaryKnots.size()) = temporaryKnots;
175void ChordLengths(
const PointArrayType& pts, KnotVectorType& chord_lengths) {
176 typedef typename KnotVectorType::Scalar Scalar;
178 const DenseIndex n = pts.cols();
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();
187 std::partial_sum(chord_lengths.data(), chord_lengths.data() + n, chord_lengths.data());
190 chord_lengths /= chord_lengths(n - 1);
191 chord_lengths(n - 1) = Scalar(1);
200 typedef typename SplineType::KnotVectorType KnotVectorType;
201 typedef typename SplineType::ParameterVectorType ParameterVectorType;
211 template <
typename Po
intArrayType>
212 static SplineType
Interpolate(
const PointArrayType& pts, DenseIndex degree);
223 template <
typename Po
intArrayType>
224 static SplineType
Interpolate(
const PointArrayType& pts, DenseIndex degree,
const KnotVectorType& knot_parameters);
243 template <
typename Po
intArrayType,
typename IndexArray>
245 const IndexArray& derivativeIndices,
const unsigned int degree);
263 template <
typename Po
intArrayType,
typename IndexArray>
265 const IndexArray& derivativeIndices,
const unsigned int degree,
266 const ParameterVectorType& parameters);
272 const KnotVectorType& knot_parameters) {
273 typedef typename SplineType::KnotVectorType::Scalar Scalar;
274 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
278 KnotVectorType knots;
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);
287 A.row(i).segment(span - degree, degree + 1) = SplineType::BasisFunctions(knot_parameters[i], degree, knots);
290 A(n - 1, n - 1) = 1.0;
295 ControlPointVectorType ctrls = qr.
solve(MatrixType(pts.transpose())).transpose();
297 return SplineType(knots, ctrls);
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;
320 const DenseIndex n = points.cols() + derivatives.cols();
322 KnotVectorType knots;
327 MatrixType A = MatrixType::Zero(n, n);
330 MatrixType b(points.rows(), n);
333 DenseIndex derivativeStart;
336 if (derivativeIndices[0] == 0) {
337 A.template block<1, 2>(1, 0) << -1, 1;
339 Scalar y = (knots(degree + 1) - knots(0)) / degree;
340 b.col(1) = y * derivatives.col(0);
348 if (derivativeIndices[derivatives.cols() - 1] == points.cols() - 1) {
349 A.template block<1, 2>(n - 2, n - 2) << -1, 1;
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);
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);
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);
364 b.col(row++) = points.col(i);
365 b.col(row++) = derivatives.col(derivativeIndex++);
367 A.row(row).segment(span - degree, degree + 1) = SplineType::BasisFunctions(parameters[i], degree, knots);
368 b.col(row++) = points.col(i);
371 b.col(0) = points.col(0);
372 b.col(b.cols() - 1) = points.col(points.cols() - 1);
378 ControlPointVectorType controlPoints = lu.
solve(MatrixType(b.transpose())).transpose();
380 SplineType spline(knots, controlPoints);
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:82
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