10 #ifndef EIGEN_SPLINE_H
11 #define EIGEN_SPLINE_H
13 #include "SplineFwd.h"
34 template <
typename _Scalar,
int _Dim,
int _Degree>
43 typedef typename SplineTraits<Spline>::PointType
PointType;
59 template <
typename OtherVectorType,
typename OtherArrayType>
60 Spline(
const OtherVectorType&
knots,
const OtherArrayType&
ctrls) : m_knots(knots), m_ctrls(ctrls) {}
66 template <
int OtherDegree>
68 m_knots(spline.
knots()), m_ctrls(spline.
ctrls()) {}
105 typename SplineTraits<Spline>::DerivativeType
113 template <
int DerivativeOrder>
114 typename SplineTraits<Spline,DerivativeOrder>::DerivativeType
133 typename SplineTraits<Spline>::BasisVectorType
149 typename SplineTraits<Spline>::BasisDerivativeType
157 template <
int DerivativeOrder>
158 typename SplineTraits<Spline,DerivativeOrder>::BasisDerivativeType
164 DenseIndex
degree()
const;
175 static DenseIndex
Span(
typename SplineTraits<Spline>::Scalar u, DenseIndex
degree,
const typename SplineTraits<Spline>::KnotVectorType&
knots);
197 template <
typename _Scalar,
int _Dim,
int _Degree>
204 if (u <= knots(0))
return degree;
205 const Scalar* pos = std::upper_bound(knots.data()+degree-1, knots.data()+knots.size()-degree-1, u);
206 return static_cast<DenseIndex
>( std::distance(knots.data(), pos) - 1 );
209 template <
typename _Scalar,
int _Dim,
int _Degree>
218 const DenseIndex p = degree;
223 BasisVectorType left(p+1); left(0) =
Scalar(0);
224 BasisVectorType right(p+1); right(0) =
Scalar(0);
229 BasisVectorType N(1,p+1);
231 for (DenseIndex j=1; j<=p; ++j)
234 for (DenseIndex r=0; r<j; r++)
236 const Scalar tmp = N(r)/(right(r+1)+left(j-r));
237 N[r] = saved + right(r+1)*tmp;
238 saved = left(j-r)*tmp;
245 template <
typename _Scalar,
int _Dim,
int _Degree>
249 return m_knots.size() - m_ctrls.cols() - 1;
254 template <
typename _Scalar,
int _Dim,
int _Degree>
260 template <
typename _Scalar,
int _Dim,
int _Degree>
263 enum { Order = SplineTraits<Spline>::OrderAtCompileTime };
265 const DenseIndex span = this->span(u);
266 const DenseIndex p = degree();
271 return (ctrl_weights * ctrl_pts).rowwise().sum();
276 template <
typename SplineType,
typename DerivativeType>
277 void derivativesImpl(
const SplineType& spline,
typename SplineType::Scalar u, DenseIndex order, DerivativeType& der)
279 enum { Dimension = SplineTraits<SplineType>::Dimension };
280 enum { Order = SplineTraits<SplineType>::OrderAtCompileTime };
281 enum { DerivativeOrder = DerivativeType::ColsAtCompileTime };
283 typedef typename SplineTraits<SplineType>::Scalar Scalar;
285 typedef typename SplineTraits<SplineType>::BasisVectorType BasisVectorType;
286 typedef typename SplineTraits<SplineType>::ControlPointVectorType ControlPointVectorType;
288 typedef typename SplineTraits<SplineType,DerivativeOrder>::BasisDerivativeType BasisDerivativeType;
289 typedef typename BasisDerivativeType::ConstRowXpr BasisDerivativeRowXpr;
291 const DenseIndex p = spline.degree();
292 const DenseIndex span = spline.span(u);
294 const DenseIndex n = (std::min)(p, order);
296 der.resize(Dimension,n+1);
299 const BasisDerivativeType basis_func_ders = spline.template basisFunctionDerivatives<DerivativeOrder>(u, n+1);
302 for (DenseIndex der_order=0; der_order<n+1; ++der_order)
304 const Replicate<BasisDerivativeRowXpr,Dimension,1> ctrl_weights( basis_func_ders.row(der_order) );
305 const Block<const ControlPointVectorType,Dimension,Order> ctrl_pts(spline.ctrls(),0,span-p,Dimension,p+1);
306 der.col(der_order) = (ctrl_weights * ctrl_pts).rowwise().sum();
310 template <
typename _Scalar,
int _Dim,
int _Degree>
311 typename SplineTraits< Spline<_Scalar, _Dim, _Degree> >::DerivativeType
314 typename SplineTraits< Spline >::DerivativeType res;
315 derivativesImpl(*
this, u, order, res);
319 template <
typename _Scalar,
int _Dim,
int _Degree>
320 template <
int DerivativeOrder>
321 typename SplineTraits< Spline<_Scalar, _Dim, _Degree>, DerivativeOrder >::DerivativeType
324 typename SplineTraits< Spline, DerivativeOrder >::DerivativeType res;
325 derivativesImpl(*
this, u, order, res);
329 template <
typename _Scalar,
int _Dim,
int _Degree>
330 typename SplineTraits< Spline<_Scalar, _Dim, _Degree> >::BasisVectorType
338 template <
typename SplineType,
typename DerivativeType>
339 void basisFunctionDerivativesImpl(
const SplineType& spline,
typename SplineType::Scalar u, DenseIndex order, DerivativeType& N_)
341 enum { Order = SplineTraits<SplineType>::OrderAtCompileTime };
343 typedef typename SplineTraits<SplineType>::Scalar Scalar;
344 typedef typename SplineTraits<SplineType>::BasisVectorType BasisVectorType;
345 typedef typename SplineTraits<SplineType>::KnotVectorType KnotVectorType;
346 typedef typename SplineTraits<SplineType>::ControlPointVectorType ControlPointVectorType;
348 const KnotVectorType& U = spline.knots();
350 const DenseIndex p = spline.degree();
351 const DenseIndex span = spline.span(u);
353 const DenseIndex n = (std::min)(p, order);
357 BasisVectorType left = BasisVectorType::Zero(p+1);
358 BasisVectorType right = BasisVectorType::Zero(p+1);
360 Matrix<Scalar,Order,Order> ndu(p+1,p+1);
369 left[j] = u-U[span+1-j];
370 right[j] = U[span+j]-u;
373 for (DenseIndex r=0; r<j; ++r)
376 ndu(j,r) = right[r+1]+left[j-r];
377 temp = ndu(r,j-1)/ndu(j,r);
379 ndu(r,j) =
static_cast<Scalar
>(saved+right[r+1] * temp);
380 saved = left[j-r] * temp;
383 ndu(j,j) =
static_cast<Scalar
>(saved);
386 for (j = p; j>=0; --j)
390 DerivativeType a(n+1,p+1);
399 for (DenseIndex k=1; k<=static_cast<DenseIndex>(n); ++k)
402 DenseIndex rk,pk,j1,j2;
407 a(s2,0) = a(s1,0)/ndu(pk+1,rk);
408 d = a(s2,0)*ndu(rk,pk);
414 if (r-1 <= pk) j2 = k-1;
417 for (j=j1; j<=j2; ++j)
419 a(s2,j) = (a(s1,j)-a(s1,j-1))/ndu(pk+1,rk+j);
420 d += a(s2,j)*ndu(rk+j,pk);
425 a(s2,k) = -a(s1,k-1)/ndu(pk+1,r);
426 d += a(s2,k)*ndu(r,pk);
429 N_(k,r) =
static_cast<Scalar
>(d);
430 j = s1; s1 = s2; s2 = j;
437 for (DenseIndex k=1; k<=static_cast<DenseIndex>(n); ++k)
439 for (DenseIndex j=p; j>=0; --j) N_(k,j) *= r;
444 template <
typename _Scalar,
int _Dim,
int _Degree>
445 typename SplineTraits< Spline<_Scalar, _Dim, _Degree> >::BasisDerivativeType
448 typename SplineTraits< Spline >::BasisDerivativeType der;
449 basisFunctionDerivativesImpl(*
this, u, order, der);
453 template <
typename _Scalar,
int _Dim,
int _Degree>
454 template <
int DerivativeOrder>
455 typename SplineTraits< Spline<_Scalar, _Dim, _Degree>, DerivativeOrder >::BasisDerivativeType
458 typename SplineTraits< Spline, DerivativeOrder >::BasisDerivativeType der;
459 basisFunctionDerivativesImpl(*
this, u, order, der);
464 #endif // EIGEN_SPLINE_H