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

How to get the length of a curve #127

Closed AlbertoGarciaRasco closed 5 years ago

AlbertoGarciaRasco commented 5 years ago

Hello, I'm thinking about using TinySpline in my OpenGL program in order to create camera routes. But in order to edit the route I need to draw the curve generated by TinySpline, then I need to know the length of the curve in order to subdivide it in small lines that will represent the curve in OpenGL.

I have seen in the examples that you use GLUT for the curve drawing, but it uses old OpenGL code which is not compatible with my project.

Is it possible to obtain the length of a curve using the functions that TinySpline currently provides?

Thanks in advance.

msteinbeck commented 5 years ago

Hi @AlbertoGarciaRasco,

instead of drawing a curve line by line, you can also utilize OpenGL's Bézier curve evaluators: https://users.cs.jmu.edu/bernstdh/web/common/lectures/summary_opengl-bezier-curves.php. You just need to subdivide your spline using ts_bspline_to_beziers and pass the resulting Bézier curves to the OpenGL evaluators mentioned above. You will find a Java example using this approach here (The resulting Bézier curves are rendered with Java's Graphics2D engine).

If you can't use OpenGL's Bézier curve evaluators, you need to split your spline with ts_bspline_split until the resulting sequence of splines approximates a sequence of lines. Another, and more efficient, approach is called forward differencing. I already though about adding forward differencing to TinySpline, but didn't find the time yet. An approach for arbitrary NURBS curves is described in this paper.

Anyhow, to answer your actual question, TinySpline does not provide a function to calculate the length of a spline.

msteinbeck commented 5 years ago

See s1240 in #125.