Prevent refine() from looping forever on near-cocircular points#205
Open
spokodev wants to merge 1 commit into
Open
Prevent refine() from looping forever on near-cocircular points#205spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
refine()'s Lawson edge-flip cascade uses an inexact inCircle test, so on near-cocircular vertices roundoff can make an edge and its flip both test as illegal and the cascade cycles forever, hanging the caller. Bound the number of flips to a generous multiple of the half-edge count; a converging cascade needs far fewer, and on hitting the cap the output is still a valid mesh with at worst a few not-quite-Delaunay edges, as the docstring already allows.
a55eb0d to
24a61c5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
refine()can loop forever on a class of valid, simple polygons, hanging the calling thread.Cause
refine()runs a Lawson edge-flip cascade whose flip decision uses a an inexact floating-pointinCircle. On near-cocircular points the test is not transitive: an edge and its flipped counterpart can both test as illegal, so a legalized edge gets re-queued and flipped back, and the cascade cycles forever. The loop has no bound. Measured incidence is about 0.5 percent of random convex float polygons (roughly 1 in 200), and exact-tie cocircular inputs do not trigger it, so it has gone unnoticed.refineis a documented public API whose docstring says the worst case is a not-quite-Delaunay edge, so a hang is the one outcome it should never produce.Fix
Bound the cascade with a flip budget of
25 * n(n is the half-edge count, 3 times the triangle count). A converging Delaunay cascade needs O(triangles) flips in practice, so this never truncates legitimate work, while it always breaks a float-induced cycle. On hitting the cap the output is exactly what the docstring already allows: a valid manifold mesh with at worst a few not-quite-Delaunay edges (the flip itself is unchanged and convexity-guarded).Testing
Added a test with the reproducer polygon above. On the current code the test never returns (a synchronous infinite loop freezes the whole test runner). With the fix it passes: the mesh is unchanged in size and its deviation is 0. Full suite stays green (247 tests), eslint and tsc clean. The existing refine quality tests, including the mvt-fixture perimeter-reduction check, are unaffected.