'Spline curevature in python using splprep and splev

I'm trying to evaluate the curvature of a spline: equation for K

I'm using Python3.8. The spline was generated using splprep. I cannot use other functions (splrep, bspline, cubic_spline,...) because my data cannot be represented as y=f(x), and in the future I will have 3D curvature.

I tried to calculate it this way:

def spline_curvature(self, tck):
    u_new = np.linspace(0, 1, int(1e7))
    # x, y = interpolate.splev(u_new, tck)
    dx, dy = interpolate.splev(u_new, tck, der=1)
    ddx, ddy = interpolate.splev(u_new, tck, der=2)
    K = abs(dx * ddy - dy * ddx) / ((dx ** 2 + dy ** 2) ** (3 / 2))
    return K

When generating the spline I used a very discrete 'u' parameter (1,1e7) In the next figure you can see the spline: spline

In the next figure, you can see the curvature parameter along the spline. the x-axis is u parameter, the y-axis is the K (curvature) value. curvature

For some reason, the K parameter is very noisy and does not make sense. For example the"testing points" the spline seems to be "smooth" but the curvature is about K=1.7, meaning the radius is R=1/K=0.59. testing points, curvature tesing points

I assume this is because 'splev' calculates the derivative numerically, rather than analyticly?

interpolate.splev(u_new, tck, der=1)
interpolate.splev(u_new, tck, der=2)

Any ideas on how can I calculate the curvature of a spline?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source