msteinbeck / tinyspline

ANSI C library for NURBS, B-Splines, and Bézier curves with interfaces for C++, C#, D, Go, Java, Javascript, Lua, Octave, PHP, Python, R, and Ruby.
MIT License
1.2k stars 208 forks source link

Is there any way to make curve pass control points? #130

Closed xu2555 closed 5 years ago

xu2555 commented 5 years ago

Hello,

This works very well, thank you!

How to make result curve passes control points?

Regards,

sirop commented 5 years ago

Catmull-Rom-Spline?

msteinbeck commented 5 years ago

Hi @xu2555,

what you need is known as cubic spline interpolation. TinySpline provides the following function: BSpline::interpolateCubic

xu2555 commented 5 years ago

Hi @msteinbeck ,

I am using c, and assume this is the function to call. tsError ts_bspline_interpolate_cubic(const tsReal points, size_t n, size_t dim, tsBSpline spline, tsStatus *status);

Could you provide some example codes to use it?

Thank you!

msteinbeck commented 5 years ago

Let's say you have 5 points in 3D. You can create a cubic spline passing these points with:

tsBSpline spline;

tsReal points[15];
points[0]  =  1;
points[1]  = -1;
points[2]  =  0;
points[3]  = -1;
points[4]  =  2;
points[5]  =  0;
points[6]  =  1;
points[7]  =  4;
points[8]  =  0;
points[9]  =  4;
points[10] =  3;
points[11] =  0;
points[12] =  7;
points[13] =  5;
points[14] =  0;

ts_bspline_interpolate_cubic(points, 5, 3, &spline, NULL);

The resulting spline (spline) is a sequence of cubic bezier curves connecting each point in points. There is an example using the C interface: https://github.com/msteinbeck/tinyspline/blob/master/examples/c/interpolation.c To run this example:

  1. Install glut (freeglut).
  2. Run CMake with TINYSPLINE_FLOAT_PRECISION=Yes.

You may also have a look at the Java example: https://github.com/msteinbeck/tinyspline/blob/master/examples/java/Swing.java In this example, I use TinySpline to interpolate a spline from a given list of points and render it's sequence (bezier curves) one after another using Swing (https://github.com/msteinbeck/tinyspline/blob/master/examples/java/Swing.java#L78).

xu2555 commented 5 years ago

This works very well! Thank you very much!