Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the length of a cubic B-spline

Using scipy's interpolate.splprep function get a parametric spline on parameter u, but the domain of u is not the line integral of the spline, it is a piecewise linear connection of the input coordinates. I've tried integrate.splint, but that just gives the individual integrals over u. Obviously, I can numerically integrate a bunch of Cartesian differential distances, but I was wondering if there was closed-form method for getting the length of a spline or spline segment (using scipy or numpy) that I was overlooking.

Edit: I am looking for a closed-form solution or a very fast way to converge to a machine-precision answer. I have all but given up on the numerical root-finding methods and am now primarily after a closed-form answer. If anyone has any experience integrating elliptical functions or can point me to a good resource (other than Wolfram), That would be great.

I'm going to try Maxima to try to get the indefinite integral of what I believe is the function for one segment of the spline: I cross-posted this on MathOverflow

like image 729
Paul Avatar asked Feb 02 '10 01:02

Paul


People also ask

What is cubic B-spline?

Abstract. In the present paper, the cubic B-splines method is considered for solving one-dimensional heat and wave equations. A typical finite difference approach had been used to discretize the time derivative while the cubic B-spline is applied as an interpolation function in the space dimension.


2 Answers

Because both x & y are cubic parametric functions, there isn't a closed solution in terms of simple functions. Numerical integration is the way to go. Either integrating the arc length expression or simply adding line segment lengths - depends on the accuracy you are after and how much effort you want to exert.

An accurate and fast "Adding length of line segments" method:

Using recurvise subdivision (a form of de Casteljeau's algorithm) to generate points, can give you a highly accurate representation with minimal number of points. Only subdivide subdivisions if they fail to meet a criteria. Usually the criteria is based on the length joining the control points (the hull or cage). For cubic, usually comparing closeness of P0P1+P1P2+P2P3 to P0P3, where P0, P1, P2 & P3 are the control points that define your bezier.

You can find some Delphi code here: link text

It should be relatively easy to convert to Python. It will generate the points. The code already calculates the length of the segments in order to test the criteria. You can simply accumulate those length values along the way.

like image 110
symmetry Avatar answered Oct 18 '22 19:10

symmetry


You can integrate the function sqrt(x'(u)**2+y'(u)**2) over u, where you calculate the derivatives x' and y' of your coordinates with scipy.interpolate.splev. The integration can be done with one of the routines from scipy.integrate (quad is precise [Clenshaw-Curtis], romberg is generally faster). This should be more precise, and probably faster than adding up lots of small distances (which is equivalent to integrating with the rectangle rule).

like image 36
Eric O Lebigot Avatar answered Oct 18 '22 21:10

Eric O Lebigot