10 #ifndef EIGEN_SPLINE_FITTING_H
11 #define EIGEN_SPLINE_FITTING_H
23 #include "../../../../Eigen/LU"
24 #include "../../../../Eigen/QR"
46 template <
typename KnotVectorType>
48 knots.resize(parameters.size() +
degree + 1);
53 knots.segment(knots.size() -
degree - 1,
degree + 1) = KnotVectorType::Ones(
degree + 1);
77 template <
typename KnotVectorType,
typename ParameterVectorType,
typename IndexArray>
79 const IndexArray& derivativeIndices, KnotVectorType& knots) {
83 DenseIndex numDerivatives = derivativeIndices.size();
85 if (numDerivatives < 1) {
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;
116 averageKnots[++newKnotIndex] = parameters.segment(
i,
degree).mean();
117 averageKnots[++newKnotIndex] = parameters[numParameters - 1];
121 ParameterVectorType temporaryParameters(numParameters + 1);
122 KnotVectorType derivativeKnots(numInternalDerivatives);
124 temporaryParameters[0] = averageKnots[
i];
125 ParameterVectorType parameterIndices(numParameters);
126 int temporaryParameterIndex = 1;
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) {
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());
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;
170 template <
typename Po
intArrayType,
typename KnotVectorType>
171 void ChordLengths(
const PointArrayType& pts, KnotVectorType& chord_lengths) {
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);
194 template <
typename SplineType>
207 template <
typename Po
intArrayType>
219 template <
typename Po
intArrayType>
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,
265 template <
typename SplineType>
266 template <
typename Po
intArrayType>
270 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
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);
296 template <
typename SplineType>
297 template <
typename Po
intArrayType>
301 return Interpolate(pts,
degree, chord_lengths);
304 template <
typename SplineType>
305 template <
typename Po
intArrayType,
typename IndexArray>
307 const PointArrayType& derivatives,
308 const IndexArray& derivativeIndices,
309 const unsigned int degree,
312 typedef typename SplineType::ControlPointVectorType ControlPointVectorType;
316 const DenseIndex n = points.cols() + derivatives.cols();
332 if (derivativeIndices[0] == 0) {
333 A.template block<1, 2>(1, 0) << -1, 1;
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;
348 b.col(
b.cols() - 2) =
y * derivatives.col(derivatives.cols() - 1);
356 if (derivativeIndex < derivativeIndices.size() && derivativeIndices[derivativeIndex] ==
i) {
358 SplineType::BasisFunctionDerivatives(parameters[
i], 1,
degree, knots);
360 b.col(
row++) = points.col(
i);
361 b.col(
row++) = derivatives.col(derivativeIndex++);
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);
381 template <
typename SplineType>
382 template <
typename Po
intArrayType,
typename IndexArray>
384 const PointArrayType& derivatives,
385 const IndexArray& derivativeIndices,
386 const unsigned int degree) {
389 return InterpolateWithDerivatives(points, derivatives, derivativeIndices,
degree, parameters);
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
HouseholderQR< MatrixXf > qr(A)
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
LU decomposition of a matrix with complete pivoting, and related features.
Definition: FullPivLU.h:63
cout<< "Here is the matrix m:"<< endl<< m<< endl;Eigen::FullPivLU< Matrix5x3 > lu(m)
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
void ChordLengths(const PointArrayType &pts, KnotVectorType &chord_lengths)
Computes chord length parameters which are required for spline interpolation.
Definition: SplineFitting.h:171
void KnotAveraging(const KnotVectorType ¶meters, DenseIndex degree, KnotVectorType &knots)
Computes knot averages.
Definition: SplineFitting.h:47
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
Scalar * y
Definition: level1_cplx_impl.h:128
char char char int int * k
Definition: level2_impl.h:374
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:75
const Mdouble degree
Definition: ExtendedMath.h:32
double Zero
Definition: pseudosolid_node_update_elements.cc:35
Spline fitting methods.
Definition: SplineFitting.h:195
SplineType::KnotVectorType KnotVectorType
Definition: SplineFitting.h:196
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
static SplineType Interpolate(const PointArrayType &pts, DenseIndex degree)
Fits an interpolating Spline to the given data points.
Definition: SplineFitting.h:298
SplineType::ParameterVectorType ParameterVectorType
Definition: SplineFitting.h:197
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2