C++ Interface

The following listing gives an example of TinySpline's C++ API. Depending on its configuration, tinyspline::real reprsents either a float or a double:

#include <iostream>
#include "tinysplinecpp.h"

int main(int argc, char **argv)
{
    // Create a spline...
    tinyspline::BSpline spline(
        3, // ... of degree 3...
        2, // ... in 2D...
        7, // ... consisting of 7 control points...
        TS_CLAMPED // ... using a clamped knot vector.
    );

    // Setup control points.
    std::vector<tinyspline::real> ctrlp = spline.ctrlp();
    ctrlp[0]  = -1.75f; // x0
    ctrlp[1]  = -1.0f;  // y0
    ctrlp[2]  = -1.5f;  // x1
    ctrlp[3]  = -0.5f;  // y1
    ctrlp[4]  = -1.5f;  // x2
    ctrlp[5]  =  0.0f;  // y2
    ctrlp[6]  = -1.25f; // x3
    ctrlp[7]  =  0.5f;  // y3
    ctrlp[8]  = -0.75f; // x4
    ctrlp[9]  =  0.75f; // y4
    ctrlp[10] =  0.0f;  // x5
    ctrlp[11] =  0.5f;  // y5
    ctrlp[12] =  0.5f;  // x6
    ctrlp[13] =  0.0f;  // y6
    spline.setCtrlp(ctrlp);

    // Stores our evaluation results.
    std::vector<tinyspline::real> result;

    // Evaluate `spline` at u = 0.4 using 'evaluate'.
    result = spline.evaluate(0.4f).result();
    std::cout << "x = " << result[0] << ", y = " << result[1] << std::endl;

    // Derive `spline` and subdivide it into a sequence of Bezier curves.
    tinyspline::BSpline beziers = spline.derive().toBeziers();

    // Evaluate `beziers` at u = 0.3 using '()' instead of 'evaluate'.
    result = beziers(0.3f).result();
    std::cout << "x = " << result[0] << ", y = " << result[1] << std::endl;

    return 0;
}

You can download the full example here. The linked zip archive consists of the files tinyspline.h and tinyspline.c (the core library of TinySpline implemented in C), tinysplinecpp.h and tinysplinecpp.cpp (the object-oriented C++ wrapper), and quickstart.cpp (containing the source code listed above). Run the following command to compile the example using GCC:

g++ -std=c++11 tinyspline.c tinysplinecpp.cpp quickstart.cpp -o quickstart

If you prefer using MSVC on Windows, run the following command instead:

cl.exe tinyspline.c tinysplinecpp.cpp quickstart.cpp /out:quickstart.exe

Besides GCC and MSVC, Clang is officially supported, too. Most compiler suites should be compatible, though, as TinySpline has been implemented using a minimum set of dependencies.

You can use the preprocessor definition TINYSPLINE_DISABLE_CXX11_FEATURES to disable C++11 related features. That way compiler suites without proper support for C++11 are able to compile source code using TinySpline. The following command shows how to compile the example using GCC and C++98:

g++ -std=c++98 -DTINYSPLINE_DISABLE_CXX11_FEATURES tinyspline.c tinysplinecpp.cpp quickstart.cpp -o quickstart

Again, if you prefer using MSVC on Windows, run the following command instead:

cl.exe /DTINYSPLINE_DISABLE_CXX11_FEATURES tinyspline.c tinysplinecpp.cpp quickstart.cpp /out:quickstart.exe

As already indicated, TinySpline can be configured with single or double precision support for tinyspline::real. The default configuration is float. You can change this to double by supplying the preprocessor definition TINYSPLINE_DOUBLE_PRECISION. The command to compile the example with double precision support for tinyspline::real using GCC is as follows:

g++ -std=c++11 -DTINYSPLINE_DOUBLE_PRECISION tinyspline.c tinysplinecpp.cpp quickstart.cpp -o quickstart

MSVC is executed, accordingly:

cl.exe /DTINYSPLINE_DOUBLE_PRECISION tinyspline.c tinysplinecpp.cpp quickstart.cpp /out:quickstart.exe

It is, of course, possible to combine TINYSPLINE_DISABLE_CXX11_FEATURES and TINYSPLINE_DOUBLE_PRECISION to adjust TinySpline to your requirements.