Fit 2D points to closed curve

Hi, I use a CAD package called Rhino which lets users write python scripts that run in the cad environment. I have a closed curve that is a design surface border and I have a sparse data set of 2D surveyed points taken around that border, so some error is included in the 2D data set. I would like to know what python function I could use to do a 2D best fit of the points to the curve. Plenty of examples of fitting curves to points but not of best fitting points to a closed curve of which I do not know the formula. Any help or advice greatly appreciated.

On Thu, Jan 20, 2022 at 1:15 PM <roger.davies@geo-spatial.co.uk> wrote:
Hi, I use a CAD package called Rhino which lets users write python scripts that run in the cad environment. I have a closed curve that is a design surface border and I have a sparse data set of 2D surveyed points taken around that border, so some error is included in the 2D data set. I would like to know what python function I could use to do a 2D best fit of the points to the curve. Plenty of examples of fitting curves to points but not of best fitting points to a closed curve of which I do not know the formula.
You can use a parametric spline (where X and Y are each functions of a "time" coordinate `t`) with periodic boundary conditions. See this StackOverflow answer for an example using scipy.interpolate: https://stackoverflow.com/a/31466013/163633 Whether our spline representation can be converted to Rhino NURBS or if Rhino itself can do a periodic parametric NURBS and do the fitting itself, I don't know. -- Robert Kern

On Thu, Jan 20, 2022 at 11:28 AM Robert Kern <robert.kern@gmail.com> wrote:
On Thu, Jan 20, 2022 at 1:15 PM <roger.davies@geo-spatial.co.uk> wrote:
Hi, I use a CAD package called Rhino which lets users write python scripts that run in the cad environment. I have a closed curve that is a design surface border and I have a sparse data set of 2D surveyed points taken around that border, so some error is included in the 2D data set. I would like to know what python function I could use to do a 2D best fit of the points to the curve. Plenty of examples of fitting curves to points but not of best fitting points to a closed curve of which I do not know the formula.
You can use a parametric spline (where X and Y are each functions of a "time" coordinate `t`) with periodic boundary conditions. See this StackOverflow answer for an example using scipy.interpolate:
https://stackoverflow.com/a/31466013/163633
Whether our spline representation can be converted to Rhino NURBS or if Rhino itself can do a periodic parametric NURBS and do the fitting itself, I don't know.
-- Robert Kern
Fitting curve is choosing a family of parametrized curves and picking the best one in the family. The number of parameters should not exceed the number of data points. Picking that family is a tradeoff between expectation (theory) and convenience. The splines Robert mentions are one family of curves which have nice properties and are good when there is little theory. If you know what the curve *should* be, you can try adding perturbations, i.e., effectively fitting the deviations. It is a bit of an art. Chuck

Hi Roger, On Thu, Jan 20, 2022, at 07:13, roger.davies@geo-spatial.co.uk wrote:
I use a CAD package called Rhino which lets users write python scripts that run in the cad environment. I have a closed curve that is a design surface border and I have a sparse data set of 2D surveyed points taken around that border, so some error is included in the 2D data set. I would like to know what python function I could use to do a 2D best fit of the points to the curve.
This isn't exactly what you asked for, but in case you are looking to refine these points into a smoother, I've found subdivision splines very handy. To get the boundary conditions right is usually just a matter of repeating the start and end points enough times to cover the subdivision mask. Here is an example implementation of Catmul-Clark subdivision: https://github.com/matplotlib/viscm/blob/master/viscm/bezierbuilder.py#L323 You can choose subdivision coefficients so that the resulting curve goes exactly through your data points or not. Best regards, Stéfan
participants (4)
-
Charles R Harris
-
Robert Kern
-
roger.davies@geo-spatial.co.uk
-
Stefan van der Walt