From: Mikko Rasa Date: Fri, 7 Jun 2019 23:16:06 +0000 (+0300) Subject: Add a bezier spline type X-Git-Url: http://git.tdb.fi/?p=libs%2Fmath.git;a=commitdiff_plain;h=c854dd42c57b0430558baed5151449c83e24507f Add a bezier spline type --- diff --git a/source/interpolate/bezierspline.h b/source/interpolate/bezierspline.h new file mode 100644 index 0000000..c6e295a --- /dev/null +++ b/source/interpolate/bezierspline.h @@ -0,0 +1,88 @@ +#ifndef MSP_INTERPOLATE_BEZIERSPLINE_H_ +#define MSP_INTERPOLATE_BEZIERSPLINE_H_ + +#include +#include "spline.h" + +namespace Msp { +namespace Interpolate { + +template +struct Bernstein +{ + static Polynomial b(unsigned); +}; + +/** +A spline composed of bezier curves. Each segment has D-1 control points +between the knots which determine the shape of the curve. Bezier splines +with 2- or 3-dimensional values are commonly used in graphics. +*/ +template +class BezierSpline: public Spline +{ +public: + using typename Spline::Knot; + + BezierSpline(const std::vector &); +}; + +template +inline Polynomial Bernstein::b(unsigned k) +{ + if(k>D) + throw std::invalid_argument("Bernstein::b"); + + LinAl::Vector coeff; + + int c = ((D-k)%2 ? -1 : 1); + for(unsigned i=0; i<=D-k; ++i) + { + coeff[i] = c; + c = c*-1*static_cast(D-k-i)/static_cast(i+1); + } + + unsigned n = 1; + for(unsigned i=0; i(coeff); +} + +template +inline BezierSpline::BezierSpline(const std::vector &k): + Spline(k.front()) +{ + typedef SplineValue SV; + + if((k.size()-1)%D) + throw std::invalid_argument("BezierSpline::BezierSpline"); + + Polynomial b[D+1]; + for(unsigned i=0; i<=D; ++i) + b[i] = Bernstein::b(i); + + for(unsigned i=0; i+D t(LinAl::Vector(T(1)/dx, -k[i].x/dx)); + Polynomial p[N]; + for(unsigned j=0; jadd_segment(p, k[i+D].x); + } +} + +} // namespace Interpolate +} // namespace Msp + +#endif diff --git a/source/interpolate/dummy.cpp b/source/interpolate/dummy.cpp index ec10aec..cfa902e 100644 --- a/source/interpolate/dummy.cpp +++ b/source/interpolate/dummy.cpp @@ -1,3 +1,5 @@ +#include "bezierspline.h" #include "hermitespline.h" +#include "linearspline.h" #include "polynomial.h" #include "spline.h"