Skip to content

vanyka/spline-lib-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A header-only C++ library that provides a variety of splines for curve interpolation, such as Catmull-Rom, and Bezier splines.

Features

  • Easy to use interface for using splines.
  • Implementation for Catmull-Rom and Bezier splines is included.
  • New spline types can be easily implemented by deriving the Spline interface.
  • templated implementation for using your already used Vector implementation. (An example implementation is provided in example/vector2.h)
  • Both using splines as a Functor for interpolating over or generating points with a given resolution is supported.
  • A transformation class to even distributed for any spline is provided by EvenDistributedSplineAdapter class.

Code examples

Creating a Catmull-Rom spline

#include "splines/spline.h"
#include "splines/catmull_rom.h"
#include "vector2.h"

using namespace vanyka::spline;
using vanyka::Vector2f;

Spline<Vector2f>* method1() {
    Spline<Vector2f>* spline = new CatmullRomSpline<Vector2f>();
    spline->AddSupportPoint({ 0.f, 0.f });
    spline->AddSupportPoint({ 1.f, .5f });
    spline->AddSupportPoint({ 2.f, .2f });
    spline->AddSupportPoint({ 1.f, .3f });

    return spline;
}

Spline<Vector2f>* method2() {
    Spline<Vector2f>* spline = new CatmullRomSpline<Vector2f>();
    spline->AddSupportPoints<4>(new Vector2f[]{
        { 0.f, 0.f },
        { 1.f, .5f },
        { 2.f, .2f },
        { 1.f, .3f }
    });

    return spline;
}

Spline<Vector2f>* method3() {
    Spline<Vector2f>* spline = new CatmullRomSpline<Vector2f>();
    std::vector<Vector2f> support_points = {
        { 0.f, 0.f },
        { 1.f, .5f },
        { 2.f, .2f },
        { 1.f, .3f }
    };

    spline->AddSupportPoints(support_points.begin(), support_points.end());

    return spline;
}

int main() {
    Spline<Vector2f>* spline =
        method1();
//      method2();
//      method3();

    // Generating Points over the spline
    std::vector<Vector2f> points = spline->GeneratePoints(100);

    // Interpolating over spline using the functor
    // Note: the interpolation value must be between 0 and 1
    Vector2f spline_center = (*spline)(0.5);

    delete spline;
}

Using the EvenDistributedSplineAdapter class

#include "splines/catmull_rom.h"
#include "splines/even_distributed_spline_adapter.h"
#include "vector2.h"

using namespace vanyka::spline;
using vanyka::Vector2f;

int main() {
    CatmullRomSpline<Vector2f> spline;
    spline->AddSupportPoints<4>(new Vector2f[]{
        { 0.f, 0.f },
        { 1.f, .5f },
        { 2.f, .2f },
        { 1.f, .3f }
    });

    EvenDistributedSplineAdapter<Vector2f> even_spline = EvenDistributedSplineAdapter(spline, 0.f, 10);

    // Generating Points over the even spline
    std::vector<Vector2f> points = even_spline.GeneratePoints(100);

    // Interpolating over even spline using the functor
    // Note: the interpolation value is now a distance from the start of the spline
    Vector2f point1 = even_spline(0.7f);
    Vector2f point2 = even_spline(1.4f);
}

Setup the Example project

You will need to have the following installed:

  • CMake (3.10 or newer)

Windows

By executing scripts/default.bat file, the detected IDE project will be generated by CMake.

Other OS

Generate it by using the following command at the root directory:

cmake -B build

Credits

This project is created by Ivan Alekseev.