-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
distance_to/nearest_t returning unexpected results #25
Comments
(This does seem to show the main issue; there's also a problem with some points returning very odd results included in this issue; hopefully they're the same)
Ah, the algorithm uses Newton-Raphson to find the closest point, and the 'gaps' effect you're seeing here is what happens if it starts from a bad guess. The algorithm only tries to optimise a single guess so occasionally it will choose poorly and wind up finding the start or end of the curve, creating a section with a much larger distance measurement than it should have. I've added a change to the head of the v0.7 branch that starts to address this by trying to optimise all of the points instead of just the initial closest point - this works for a much wider variety of points but I suspect there are still cases where it can fail (the current approach divides the curve into arcs and uses the mid-points as the initial guesses). I also spotted a slightly more subtle bug where the optimisation stops early if it moves outside the range 0<=t<=1 which could produce incorrect distances if the closest point was close to but not exactly on the start or end of the curve. |
It's working great so far -- thanks so much! |
Hi,
is returning |
Ah, yes, I thought there might still be cases where the algorithm fails. I've gone for a new approach and implemented the algorithm from Graphics Gems: this should be much more solid as it actually solves for the roots of the quintic function (vs my old approach which took initial guesses based on the geometry of the curve) I've added your example as a test case and it does find your example point correctly. However, I'm still working on this as I think there are still some cases for this particular curve that aren't resolving correctly - I think it already handles a much wider variety of cases though. |
Think I fixed the issues I found with the new algorithm, so I believe the latest version on the 0.7 branch should be very much more reliable than earlier versions. Issues were a problem with the actual test, and the need to subdivide until control points were ordered, which was causing an issue sometimes when many possible solutions were close together. |
Hi,
The stacktrace:
Is this intended behavior? If it is, would it be possible to return an option/result if the nearest point cannot be determined, instead of a panic? |
Ah: this was actually only two iterations away from succeeding. I've replaced the solver with one that doesn't produce an error even where it doesn't converge and increased the number of iterations, so this shouldn't happen any more. |
I'm trying to use the distance_to function to check whether a certain point is on a line, but have been receiving unexpected results. I wrote a basic test to showcase this issue (using the image library for displaying). Here is a simple example I've wrote to demonstrate:
Some curves work as expected, others run into issues. I've provided a couple of images using the curve
Coord2(259, 322) Coord2(272, 329) Coord2(297, 341) Coord2(350, 397)
(start, c1, c2, end) that hopefully help display this issue: https://imgur.com/a/N0A4BUJQuick image descriptions (in order of appearance):
The images also have a single pixel set to a turquoise color where there is a start/end/control point.
I hope this is helpful -- let me know if there's anything I can do to clarify.
Thanks for the great library!
The text was updated successfully, but these errors were encountered: