Increase Delaunator orient function robustness #479
Merged
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.
I found a corner case where in debug mode where the Delaunator orient function return a different result depending on whether it is in debug or release mode. This causes issues in the release mode, which is basically wrong. The issue comes down to that
(qy - py) * (rx - qx) - (qx - px) * (ry - qy)
is exactly zero in debug mode (so(qy - py) * (rx - qx) - (qx - px) * (ry - qy) < 0.0
is false) and the release mode is slightly less accurate and results in -3.48086e-21, which then returns true.The solution I have gone for here is to check if the value is smaller that -epsilon. I guess it is not a perfect solution, but it seems to solve the problem.
For reference, these are the values of
(qy - py) * (rx - qx) - (qx - px) * (ry - qy)
and the results for debug mode left and release mode right.(0.80218-0.793353) * (-1.78205 - -1.7908) - (-1.7908 - -1.79954) * (0.811008 - 0.80218) = 0 | (0.80218-0.793353) * (-1.78205 - -1.7908) - (-1.7908 - -1.79954) * (0.811008 - 0.80218) = -3.48086e-21
Playing with some of the flag could potentially help, but I just want it to be more robust, which this pull request achieves.