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; }
28 Stores a spline of degree D. It is a piecewise polynomial function with value
29 continuity. Derivatives are not guaranteed to be continuous.
31 The template parameter N controls the dimensionality of the spline's values.
32 Multi-dimensional splines can be used to implement parametric curves.
34 While this class contains everything needed to store and evaluate splines, it
35 cannot be used to construct non-empty splines. See also HermiteSpline.
37 template<typename T, unsigned D, unsigned N = 1>
41 typedef typename SplineValue<T, N>::Type Value;
49 Knot(T x_, const Value &y_): x(x_), y(y_) { }
55 Polynomial<T, D> polynomials[N];
58 Value operator()(T) const;
62 enum { STRIDE = sizeof(Segment)-sizeof(Knot) };
64 unsigned short n_segments;
65 unsigned short capacity;
73 Spline(const Spline &);
74 Spline &operator=(const Spline &);
78 static unsigned data_size(unsigned n) { return sizeof(Knot)+n*STRIDE; }
80 void reserve(unsigned);
81 void add_segment(const Polynomial<T, D> [N], T);
84 unsigned degree() const { return D; }
85 unsigned size() const { return n_segments; }
86 const Segment &segment(unsigned i) const;
87 const Knot &knot(unsigned i) const { return i==0 ? segment(0).start_knot : segment(i-1).end_knot; }
88 const Polynomial<T, D> &polynomial(unsigned i, unsigned j = 0) const { return segment(i).polynomials[j]; }
90 Value operator()(T) const;
93 template<typename T, unsigned D, unsigned N>
94 inline Spline<T, D, N>::Spline():
100 template<typename T, unsigned D, unsigned N>
101 inline Spline<T, D, N>::Spline(const Knot &k):
104 segments(new char[sizeof(Knot)])
106 new(segments) Knot(k);
109 template<typename T, unsigned D, unsigned N>
110 inline Spline<T, D, N>::Spline(const Spline &s):
118 template<typename T, unsigned D, unsigned N>
119 inline Spline<T, D, N> &Spline<T, D, N>::operator=(const Spline &s)
123 reserve(s.n_segments);
124 std::copy(s.segments, s.segments+data_size(s.n_segments), segments);
125 n_segments = s.n_segments;
136 template<typename T, unsigned D, unsigned N>
137 inline Spline<T, D, N>::~Spline()
142 template<typename T, unsigned D, unsigned N>
143 inline void Spline<T, D, N>::reserve(unsigned n)
148 char *new_segs = new char[data_size(n)];
151 std::copy(segments, segments+data_size(n_segments), new_segs);
158 template<typename T, unsigned D, unsigned N>
159 inline void Spline<T, D, N>::add_segment(const Polynomial<T, D> polys[N], T end_x)
162 throw std::logic_error("Spline::add_segment");
164 if(n_segments==capacity)
165 reserve(n_segments+1);
168 Segment &seg = *reinterpret_cast<Segment *>(segments+(n_segments-1)*STRIDE);
169 std::copy(polys, polys+N, seg.polynomials);
170 seg.end_knot.x = end_x;
171 seg.end_knot.y = seg(end_x);
174 template<typename T, unsigned D, unsigned N>
175 inline const typename Spline<T, D, N>::Segment &Spline<T, D, N>::segment(unsigned i) const
177 return *reinterpret_cast<const Segment *>(segments+i*STRIDE);
180 template<typename T, unsigned D, unsigned N>
181 inline typename Spline<T, D, N>::Value Spline<T, D, N>::operator()(T x) const
186 const Segment *seg = &segment(0);
187 for(unsigned i=1; (i<n_segments && x>seg->end_knot.x); ++i)
193 template<typename T, unsigned D, unsigned N>
194 inline typename Spline<T, D, N>::Value Spline<T, D, N>::Segment::operator()(T x) const
197 for(unsigned i=0; i<N; ++i)
198 v[i] = polynomials[i](x);
200 return SplineValue<T, N>::make(v);
203 } // namespace Interpolate