Splines
Splines offers a standardized framework for interpolation of
numeric data. Currently, only "natural cubic splines" are implemented,
although many other forms exist.
template <typename T, std::size_t D>
class NaturalCubicSpline;
The behaviour is largely determined by template parameter D,
which specifies the dimensionality of the data.
D | Result |
0 | Compilation error; there cannot be any data. |
1 | Template parameter T should refer to a
basic type suitable for numeric computation. |
≥ 2 | Template parameter
T should refer to a container type holding at least
D values of a basic type suitable for numeric computation. |
If D >= 2 and T must be a container type holding the
vector data, that container must at least provide the following:
typename value_type |
The basic type of the numeric values in the vector. |
T::value_type &operator[] (int i) const |
Return a reference to the ith element in the container. |
Typical containers might be tr1/array or CVMLCPP's
Euclidean Points.
Interface
Multi-Dimensional Data
If the data is multi-dimensional, it is assumed that the data is
stored in containers.
template <typename It>
NaturalCubicSpline(It first, It last) |
Creates a new spline based on the vector data in the range first
to last. |
const T operator() (const typename T::value_type t) const |
Returns an interpolated data-point for a given t
between zero and the value returned by size(). |
std::size_t size() const |
The number of segments, which is also an upper limit on the
interpolation parameter t for operator(). |
One-Dimensional Data
If the data is one-dimensional, it is assumed that the data is not
stored in containers.
template <typename It>
NaturalCubicSpline(It first, It last) |
Creates a new spline based on the scalar data in the range first
to last. |
const T operator() (const T t) const |
Returns an interpolated data-point for a given t
between zero and the value returned by size() |
std::size_t size() const |
The number of segments, which is also an upper limit on the
interpolation parameter t for operator(). |
Examples
#include <cassert>
#include <iostream>
#include <cvmlcpp/math/Splines>
#include <cvmlcpp/math/Euclid>
int main()
{
using namespace cvmlcpp;
// 1-dimensional: template parameter T is a double
double data [] = {0, 1, 2, 3};
NaturalCubicSpline<double, 1> spline(data, data+4);
for (double d = 0.0; d <= spline.size()+0.05; d += 0.1)
std::cout << spline(d) << " ";
std::cout << std::endl;
for (int i = 0; i < 4; ++i)
assert(spline(i) == i);
// 2-dimensional: template parameter T is a vector holding doubles
std::vector<dPoint2D> data2;
for (int i = 0; i < 4; ++i)
data2.push_back( dPoint2D(i, 2*i) );
NaturalCubicSpline<dPoint2D, 2> spline2(data2.begin(), data2.end());
for (double d = 0.0; d <= spline.size()+0.05; d += 0.1)
std::cout << spline2(d) << " " << std::endl;
for (int i = 0; i < 4; ++i)
{
assert(spline2(i).x() == i);
assert(spline2(i).y() == 2*i);
}
return 0;
}
|