Code Monkey home page Code Monkey logo

splines's Introduction

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.

splines's People

Contributors

vanyka avatar

Watchers

 avatar

splines's Issues

Improving the naming/usage of EvenDistributedSpline

The current naming is not fitting perfectly, because EvenDistributedSpline is not actually a Spline. The reason for that is EvenDistributedSpline is calculating from a given Spline and cannot take more control points. So possible fixes would be to change the naming of EvenDistributedSpline or change the usage of the class.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.