1 #ifndef MSP_INTERPOLATE_SPLINE_H_
2 #define MSP_INTERPOLATE_SPLINE_H_
6 #include "polynomial.h"
9 namespace Interpolate {
11 template<typename T, unsigned N>
14 typedef LinAl::Vector<T, N> Type;
15 static T get(const Type &v, unsigned i) { return v[i]; }
16 static Type make(const T *v) { return Type(v); }
20 struct SplineValue<T, 1>
23 static T get(const Type &v, unsigned) { return v; }
24 static Type make(const T *v) { return *v; }
27 template<typename T, unsigned N>
30 typedef typename SplineValue<T, N>::Type Value;
34 SplineKnot(): x(T()) { }
35 SplineKnot(T x_, const Value &y_): x(x_), y(y_) { }
39 Stores a spline of degree D. It is a piecewise polynomial function with value
40 continuity. Derivatives are not guaranteed to be continuous.
42 The template parameter N controls the dimensionality of the spline's values.
43 Multi-dimensional splines can be used to implement parametric curves.
45 While this class contains everything needed to store and evaluate splines, it
46 cannot be used to construct non-empty splines. See also HermiteSpline.
48 template<typename T, unsigned D, unsigned N = 1>
52 typedef typename SplineValue<T, N>::Type Value;
53 typedef SplineKnot<T, N> Knot;
58 Polynomial<T, D> polynomials[N];
61 Value operator()(T) const;
65 enum { STRIDE = sizeof(Segment)-sizeof(Knot) };
67 unsigned short n_segments;
68 unsigned short capacity;
76 Spline(const Spline &);
77 Spline &operator=(const Spline &);
81 static unsigned data_size(unsigned n) { return sizeof(Knot)+n*STRIDE; }
83 void reserve(unsigned);
84 void add_segment(const Polynomial<T, D> [N], T);
87 unsigned degree() const { return D; }
88 unsigned size() const { return n_segments; }
89 const Segment &segment(unsigned i) const;
90 const Knot &knot(unsigned i) const { return i==0 ? segment(0).start_knot : segment(i-1).end_knot; }
91 const Polynomial<T, D> &polynomial(unsigned i, unsigned j = 0) const { return segment(i).polynomials[j]; }
93 Value operator()(T) const;
96 template<typename T, unsigned D, unsigned N>
97 inline Spline<T, D, N>::Spline():
103 template<typename T, unsigned D, unsigned N>
104 inline Spline<T, D, N>::Spline(const Knot &k):
107 segments(new char[sizeof(Knot)])
109 new(segments) Knot(k);
112 template<typename T, unsigned D, unsigned N>
113 inline Spline<T, D, N>::Spline(const Spline &s):
121 template<typename T, unsigned D, unsigned N>
122 inline Spline<T, D, N> &Spline<T, D, N>::operator=(const Spline &s)
126 reserve(s.n_segments);
127 std::copy(s.segments, s.segments+data_size(s.n_segments), segments);
128 n_segments = s.n_segments;
141 template<typename T, unsigned D, unsigned N>
142 inline Spline<T, D, N>::~Spline()
147 template<typename T, unsigned D, unsigned N>
148 inline void Spline<T, D, N>::reserve(unsigned n)
153 char *new_segs = new char[data_size(n)];
156 std::copy(segments, segments+data_size(n_segments), new_segs);
163 template<typename T, unsigned D, unsigned N>
164 inline void Spline<T, D, N>::add_segment(const Polynomial<T, D> polys[N], T end_x)
167 throw std::logic_error("Spline::add_segment");
169 if(n_segments==capacity)
170 reserve(n_segments+1);
173 Segment &seg = *reinterpret_cast<Segment *>(segments+(n_segments-1)*STRIDE);
174 std::copy(polys, polys+N, seg.polynomials);
175 seg.end_knot.x = end_x;
176 seg.end_knot.y = seg(end_x);
179 template<typename T, unsigned D, unsigned N>
180 inline const typename Spline<T, D, N>::Segment &Spline<T, D, N>::segment(unsigned i) const
182 return *reinterpret_cast<const Segment *>(segments+i*STRIDE);
185 template<typename T, unsigned D, unsigned N>
186 inline typename Spline<T, D, N>::Value Spline<T, D, N>::operator()(T x) const
191 const Segment *seg = &segment(0);
192 for(unsigned i=1; (i<n_segments && x>seg->end_knot.x); ++i)
198 template<typename T, unsigned D, unsigned N>
199 inline typename Spline<T, D, N>::Value Spline<T, D, N>::Segment::operator()(T x) const
202 for(unsigned i=0; i<N; ++i)
203 v[i] = polynomials[i](x);
205 return SplineValue<T, N>::make(v);
208 } // namespace Interpolate