Skip to content

Header-only cubic spline interpolator for C++

License

Notifications You must be signed in to change notification settings

snsinfu/cxx-spline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cubic splines for C++

C++11 Test status Boost License

Header-only cubic spline interpolator for C++11 and above.

Usage

Copy spline.hpp into your include directory. Use cubic_spline class like this:

#include <vector>
#include <spline.hpp>

int main()
{
    // Some points (t,y) on a curve y = f(t)
    std::vector<double> t = { 0, 1, 2, 3, 4, 5 };
    std::vector<double> y = { 1, 2, 3, 2, 1, 2 };

    // Spline interpolation (and extrapolation) of the points
    cubic_spline spline(t, y);

    spline(0.5); // 1.44976
    spline(1.5); // 2.65072
    spline(6.0); // 3
}

To interpolate a 2D (or higher dimensional) curve, just create splines for each coordinate values. Example:

#include <iostream>
#include <vector>
#include <spline.hpp>

int main()
{
    // Samples from a circle.
    std::vector<double> t = { 0, 0.25, 0.5, 0.75, 1 };
    std::vector<double> x = { 1, 0, -1, 0, 1 };
    std::vector<double> y = { 0, 1, 0, -1, 0 };

    // Spline interpolation of each coordinate.
    cubic_spline spline_x(t, x);
    cubic_spline spline_y(t, y);

    for (int i = 0; i <= 100; i++) {
        double sx = spline_x(i / 100.0);
        double sy = spline_y(i / 100.0);
        std::cout << sx << '\t' << sy << '\n';
    }
}

Boundary conditions

The cubic_spline class supports natural and not-a-knot boundary conditions. Pass cubic_spline::natural or cubic_spline::not_a_knot to the constructor to choose the boundary conditions. Natural is the default.

cubic_spline natural_spline(t, x, cubic_spline::natural);
cubic_spline notaknot_spline(t, x, cubic_spline::not_a_knot);

The natural (or free-end) spline gives the minimum-energy curve in a certain physical sense. The not-a-knot spline gives a smoother-looking curve when used for visualization.

Testing

git clone https://github.com/snsinfu/cxx-spline.git
cd cxx-spline/test
make

License

Boost Software License, Version 1.0.

Languages