Distance of a point to a spline / Binary Search

BlitzMax Forums/BlitzMax Programming/Distance of a point to a spline / Binary Search

beanage(Posted 2010) [#1]
Hey,

this is a solution I had a hard time to find myself, so I thought you might be interested.

My Key Problem was, that I had to figure out the distance of an arbitrary point (aka arbitrary distance) to a cubic spline segment (not the whole function!). I then stumbled across all sorts of issues I never had dealt with before, like unsolvable fifth order polynomials. You aint get taught at highschool that there are unsolvable equations... frustration!

Then there was all the trouble with the Newton-Raphson iteration. At that point I was first reminded, that a spline is an indiscrete function going on beyound the zero and one interpolation values.. also there was trouble with getting a good initial guess for the newton raphson iteration. And finally, it didnt work out for arbitrary distances..

So I developed a pretty much brute force binary search "workaround". The performance is not stellar - about 0.3 ms [0.03 ms actually :)] on my desktop machine for a single search [with e-3 precision] - but good enough for now. And after all it works - which makes it better than any other solution I tried regardless of performance.

IF YOU HAVE A MORE ANALYTICAL SOLUTION, PLEASE SHARE IT WITH ME!!

The beanage.bsearch Module, which has been developed only for this problem, will be required for the test to run.
Screenshot:




ImaginaryHuman(Posted 2010) [#2]
Wow, your grasp of all those formulas and stuff is impressive. I have no idea how it all works.

I do know how to generate a cubic bezier pretty fast, though. See my fairly simple tutorial here: http://gurumeditations.wordpress.com/articles/calculating-and-drawing-bezier-curves-with-blitzmax/

It can be used to find any point along the curve pretty quickly... ie if you want the mid-point of the curve you just find the mid point of the lines between the control handles, make some new lines, find the midpoints of those, make some new lines, then find the final midpoint. I am sure that is faster than 0.3ms.

Maybe if mine is faster you can adapt it to do a binary search much more quickly.

Also I wonder if you can get a quick estimate of which portion of the curve the intersection will likely be in, based on the orientation of the line, or some bounding boxes or something. A 3-point quadratic bezier would be easier to deal with since it can't loop back on itself. Maybe for a cubic you can test each `half` of the spline separately and then dismiss the unlikely half? You could create two bounding triangles using the control points and the `mid point` of the curve, then see if the line intersects either triangle and proceed from there?


beanage(Posted 2010) [#3]
Hey IH, I actually hoped you'd show up here :) Your tut on gurumeditations really helped me getting into splines..

Wow, your grasp of all those formulas and stuff is impressive.

Thanks, but Matlab did most of the hard work tho :)

A 3-point quadratic bezier would be easier to deal with

Yea. Totally agree with you on that one - not only would it tremendously simplify determinig the binary search interval, you could propably even use an analytic solution, since no 5th order polynomial would be to solve. But falling back to quadratic beziers just because of this procedural issue was (and is?) a little beyound my honor.. i guess.. i may change my mind on that :)

Maybe for a cubic you can test each `half` of the spline separately and then dismiss the unlikely half?

Well.. thats what binary search is all about, isnt it?

You could create two bounding triangles using the control points and the `mid point` of the curve, then see if the line intersects either triangle and proceed from there?

See if what line intersects?

The problem with all these guessing methods is, that there is such a big - if not infinite - number of them. And with growing complexity, each one tends to have a growing bunch of border cases, that are pretty hard to deal with, and finally make a simple-seeming approximation method more complicated than your current mathematical one.

One last thing I want to try tho- that is, converting the cubic spline into two quadratics and back. The biggest problem with cubics is also theire biggest strength - namely the complexity of shapes you can achieve with them, which is partly why I'm so addicted to them ;)