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.18k stars 204 forks source link

Spline Curvature? #207

Closed sopgenorth closed 2 years ago

sopgenorth commented 2 years ago

Is it possible to get a continuous function with a spline curve representing its curvature?

Or must this be evaluated with some strategy on a point by point basis, similar to: https://stackoverflow.com/questions/56586756/find-the-radius-or-curvature-at-a-point-on-a-bspline-using-geomdl-nurbs

Or are there better ways to evaluate the curvature of a spline that anyone here might be aware of?

msteinbeck commented 2 years ago

Hi @sopgenorth,

TinySpline has no built-in function to express a spline's curvature as another spline function. However, TinySpline provides all functions needed to calculate the curvature of a spline at certain knots. Based on your link, the following code should give you the curvature of spline s at knot t:

BSpline d1 = s.derive(); // first derivative
BSpline d2 = d1.derive(); // second derivative
Vec3 CP = d1(t).resultVec3(); // C' as Vec3
Vec3 CPP = d2(t).resultVec3(); // C'' as Vec3
double val = std::pow(CP.magnitude(), 3) / CP.cross(CPP).magnitude();

You can use this to build your own spline curvature function.

msteinbeck commented 2 years ago

Note: Always consider division by 0! You should handle the case:

CP.cross(CPP).magnitude() < eps // eps > 0
msteinbeck commented 2 years ago

Could you solve your issue? If yes, please close this issue.

msteinbeck commented 2 years ago

I'm closing this issue. Feel free to reopen it if necessary.