Skip to content
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

Issue #43 #51

Merged
merged 3 commits into from
Aug 30, 2019
Merged

Issue #43 #51

merged 3 commits into from
Aug 30, 2019

Conversation

Fil
Copy link
Collaborator

@Fil Fil commented Aug 26, 2019

This PR contains a fix and test for #43: the idea is that the cross-product ABxAC is not stable, so we have a majority vote on the sign of ABxAC, BCxBA, CAxCB. The formula for the majority vote is symmetric, so it always gives the same answer for a given triangle. (For performance, we don't call the vote if ABxAC is not too small.)

I've also included a fix and test files for related issues with the voronoi (https://observablehq.com/d/bd8a95abd9a01c79 & https://observablehq.com/d/b219d92fae85fc9b), but as these issues only show in voronoi (ie in d3-delaunay)—the tests don't actually fail. I'm not sure what to do with this part — I don't see a way of fixing that in d3-delaunay directly though, so I've stuck it here for discussion.

With these two, all the test notebooks are fixed.

@Fil Fil requested review from mbostock and mourner August 26, 2019 03:04
@Fil
Copy link
Collaborator Author

Fil commented Aug 26, 2019

Cleaner version. The fixes for voronoi will be in d3-delaunay (d3/d3-delaunay#86).

Copy link
Member

@mourner mourner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's such an awesome fix! It will probably still have some edge cases, especially considering that asymmetric orientation tests can occur within a set of points > 3 (where pairwise they're stable but inconsistent as a system), and that inCircle tests may be unstable too. But if it makes the library more robust without affecting performance much, I'm up for it!

index.js Outdated
@@ -379,7 +379,14 @@ function dist(ax, ay, bx, by) {
}

function orient(px, py, qx, qy, rx, ry) {
return (qy - py) * (rx - qx) - (qx - px) * (ry - qy) < 0;
const d = cross(px, py, qx, qy, rx, ry);
if (Math.abs(d) > 1e-6) return d < 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how much we could lower the 1e-6? That may be too high for the purpose.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all the examples I had either large numbers, or very small ones (for triangles that were definitely problematic). In those examples it looked like the small value was ~1e-11 or less, but I don't know when the precision stops being a problem.

(In any case the cost for such small triangles is just +2 area computations.)

@Fil
Copy link
Collaborator Author

Fil commented Aug 26, 2019

asymmetric orientation tests can occur within a set of points > 3 (where pairwise they're stable but inconsistent as a system)

An asymmetric (but consistent) method could be to start with the top-left point of the set — but for triangles it would be less elegant (and probably more code).

inCircle tests may be unstable too

🤯

@mourner
Copy link
Member

mourner commented Aug 29, 2019

Did some exploration of the topic and came up with an alternative approach ("More precise" in the notebook) — @Fil check it out and let me know what you think: https://observablehq.com/@mourner/non-robust-arithmetic-as-art

@Fil
Copy link
Collaborator Author

Fil commented Aug 30, 2019

👍 I like the fact that it separates l and r — hadn't thought of it that way.

And the notebook is ⚡️

@mourner
Copy link
Member

mourner commented Aug 30, 2019

It may be a little slower than the voting approach because of the extra operations for bounds checking, but the advantage is that it's a proven error bound — with arbitrary ones like 1e6, we can't be sure this won't break for some fringe cases.

@mourner
Copy link
Member

mourner commented Aug 30, 2019

Actually somehow it looks ~15-20% faster — here are my results if you change bench to generate points within 0..1 (to trigger collinearity more):

# with error bound
20000: 11.108ms
100000: 75.444ms
200000: 175.354ms
500000: 506.151ms
1000000: 989.714ms

# voting 
20000: 12.186ms
100000: 96.920ms
200000: 217.174ms
500000: 556.190ms
1000000: 1172.073ms

The tests failed at first but that turned out to be because of non-robust convex check. :) Updated to use the same formula there too and it passes now. Pushed my commit here.

@mourner mourner merged commit 696b299 into master Aug 30, 2019
@mourner mourner deleted the issue43 branch August 30, 2019 06:59
@mbostock
Copy link
Collaborator

Fantastic work! 👏👏👏

andreesteve added a commit to mourner/delaunator-rs that referenced this pull request Oct 11, 2021
Based on mapbox/delaunator#51
Along with the last missing test case "issue44".
mourner pushed a commit to mourner/delaunator-rs that referenced this pull request Oct 11, 2021
* Robust orient2d checks

* Add example to display triangulation in SVG

* Clean up svg.rs example

* Split fixture tests into separate test cases

* Add test case for #10

* Copy remaining robustness tests from js repo

* Add point label to svg example to easy investigations

* Add grid test fixture

* Add hull convex check to tests

Based on mapbox/delaunator#51
Along with the last missing test case "issue44".

* Remove invalid test code for connectivity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants