47void KnotAveraging(
const KnotVectorType& parameters, DenseIndex degree, KnotVectorType& knots) {
48 knots.resize(parameters.size() + degree + 1);
50 for (DenseIndex j = 1; j < parameters.size() - degree; ++j) knots(j + degree) = parameters.segment(j, degree).mean();
52 knots.segment(0, degree + 1) = KnotVectorType::Zero(degree + 1);
53 knots.segment(knots.size() - degree - 1, degree + 1) = KnotVectorType::Ones(degree + 1);
79 const IndexArray& derivativeIndices, KnotVectorType& knots) {
80 typedef typename ParameterVectorType::Scalar
Scalar;
82 DenseIndex numParameters = parameters.size();
83 DenseIndex numDerivatives = derivativeIndices.size();
85 if (numDerivatives < 1) {
90 DenseIndex startIndex;
93 DenseIndex numInternalDerivatives = numDerivatives;
95 if (derivativeIndices[0] == 0) {
97 --numInternalDerivatives;
101 if (derivativeIndices[numDerivatives - 1] == numParameters - 1) {
102 endIndex = numParameters - degree;
103 --numInternalDerivatives;
105 endIndex = numParameters - degree - 1;
110 DenseIndex numAverageKnots = endIndex - startIndex + 3;
111 KnotVectorType averageKnots(numAverageKnots);
112 averageKnots[0] = parameters[0];
114 int newKnotIndex = 0;
115 for (DenseIndex i = startIndex; i <= endIndex; ++i)
116 averageKnots[++newKnotIndex] = parameters.segment(i, degree).mean();
117 averageKnots[++newKnotIndex] = parameters[numParameters - 1];
121 ParameterVectorType temporaryParameters(numParameters + 1);
122 KnotVectorType derivativeKnots(numInternalDerivatives);
123 for (DenseIndex i = 0; i < numAverageKnots - 1; ++i) {
124 temporaryParameters[0] = averageKnots[i];
125 ParameterVectorType parameterIndices(numParameters);
126 int temporaryParameterIndex = 1;
127 for (DenseIndex j = 0; j < numParameters; ++j) {
128 Scalar parameter = parameters[j];
129 if (parameter >= averageKnots[i] && parameter < averageKnots[i + 1]) {
130 parameterIndices[temporaryParameterIndex] = j;
131 temporaryParameters[temporaryParameterIndex++] = parameter;
134 temporaryParameters[temporaryParameterIndex] = averageKnots[i + 1];
136 for (
int j = 0; j <= temporaryParameterIndex - 2; ++j) {
137 for (DenseIndex k = 0; k < derivativeIndices.size(); ++k) {
138 if (parameterIndices[j + 1] == derivativeIndices[k] && parameterIndices[j + 1] != 0 &&
139 parameterIndices[j + 1] != numParameters - 1) {
140 derivativeKnots[++newKnotIndex] = temporaryParameters.segment(j, 3).mean();
147 KnotVectorType temporaryKnots(averageKnots.size() + derivativeKnots.size());
149 std::merge(averageKnots.data(), averageKnots.data() + averageKnots.size(), derivativeKnots.data(),
150 derivativeKnots.data() + derivativeKnots.size(), temporaryKnots.data());
153 DenseIndex numKnots = numParameters + numDerivatives + degree + 1;
154 knots.resize(numKnots);
156 knots.head(degree).fill(temporaryKnots[0]);
157 knots.tail(degree).fill(temporaryKnots.template tail<1>()[0]);
158 knots.segment(degree, temporaryKnots.size()) = temporaryKnots;
171void ChordLengths(
const PointArrayType& pts, KnotVectorType& chord_lengths) {
172 typedef typename KnotVectorType::Scalar
Scalar;
174 const DenseIndex n = pts.cols();
177 chord_lengths.resize(pts.cols());
178 chord_lengths[0] = 0;
179 chord_lengths.rightCols(n - 1) =
180 (pts.array().leftCols(n - 1) - pts.array().rightCols(n - 1)).matrix().colwise().norm();
183 std::partial_sum(chord_lengths.data(), chord_lengths.data() + n, chord_lengths.data());
186 chord_lengths /= chord_lengths(n - 1);
187 chord_lengths(n - 1) =
Scalar(1);
196 typedef typename SplineType::KnotVectorType KnotVectorType;
197 typedef typename SplineType::ParameterVectorType ParameterVectorType;
207 template <
typename Po
intArrayType>
208 static SplineType
Interpolate(
const PointArrayType& pts, DenseIndex degree);
219 template <
typename Po
intArrayType>
220 static SplineType
Interpolate(
const PointArrayType& pts, DenseIndex degree,
const KnotVectorType& knot_parameters);
239 template <
typename Po
intArrayType,
typename IndexArray>
241 const IndexArray& derivativeIndices,
const unsigned int degree);
259 template <
typename Po
intArrayType,
typename IndexArray>
261 const IndexArray& derivativeIndices,
const unsigned int degree,
262 const ParameterVectorType& parameters);
268 const KnotVectorType& knot_parameters) {
269 typedef typename SplineType::KnotVectorType::Scalar
Scalar;
270 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
274 KnotVectorType knots;
277 DenseIndex n = pts.cols();
278 MatrixType A = MatrixType::Zero(n, n);
279 for (DenseIndex i = 1; i < n - 1; ++i) {
280 const DenseIndex span = SplineType::Span(knot_parameters[i], degree, knots);
283 A.row(i).segment(span - degree, degree + 1) = SplineType::BasisFunctions(knot_parameters[i], degree, knots);
286 A(n - 1, n - 1) = 1.0;
291 ControlPointVectorType ctrls = qr.
solve(MatrixType(pts.transpose())).transpose();
293 return SplineType(knots, ctrls);
307 const PointArrayType& derivatives,
308 const IndexArray& derivativeIndices,
309 const unsigned int degree,
310 const ParameterVectorType& parameters) {
311 typedef typename SplineType::KnotVectorType::Scalar
Scalar;
312 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
316 const DenseIndex n = points.cols() + derivatives.cols();
318 KnotVectorType knots;
323 MatrixType A = MatrixType::Zero(n, n);
326 MatrixType b(points.rows(), n);
329 DenseIndex derivativeStart;
332 if (derivativeIndices[0] == 0) {
333 A.template block<1, 2>(1, 0) << -1, 1;
335 Scalar y = (knots(degree + 1) - knots(0)) / degree;
336 b.col(1) = y * derivatives.col(0);
344 if (derivativeIndices[derivatives.cols() - 1] == points.cols() - 1) {
345 A.template block<1, 2>(n - 2, n - 2) << -1, 1;
347 Scalar y = (knots(knots.size() - 1) - knots(knots.size() - (degree + 2))) / degree;
348 b.col(b.cols() - 2) = y * derivatives.col(derivatives.cols() - 1);
351 DenseIndex row = startRow;
352 DenseIndex derivativeIndex = derivativeStart;
353 for (DenseIndex i = 1; i < parameters.size() - 1; ++i) {
354 const DenseIndex span = SplineType::Span(parameters[i], degree, knots);
356 if (derivativeIndex < derivativeIndices.size() && derivativeIndices[derivativeIndex] == i) {
357 A.block(row, span - degree, 2, degree + 1) =
358 SplineType::BasisFunctionDerivatives(parameters[i], 1, degree, knots);
360 b.col(row++) = points.col(i);
361 b.col(row++) = derivatives.col(derivativeIndex++);
363 A.row(row).segment(span - degree, degree + 1) = SplineType::BasisFunctions(parameters[i], degree, knots);
364 b.col(row++) = points.col(i);
367 b.col(0) = points.col(0);
368 b.col(b.cols() - 1) = points.col(points.cols() - 1);
374 ControlPointVectorType controlPoints = lu.
solve(MatrixType(b.transpose())).transpose();
376 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:78
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:383