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

Odd failure #141

Closed
madcowfpb opened this issue Mar 25, 2023 · 4 comments · Fixed by #142
Closed

Odd failure #141

madcowfpb opened this issue Mar 25, 2023 · 4 comments · Fixed by #142
Labels
bug Something isn't working

Comments

@madcowfpb
Copy link

madcowfpb commented Mar 25, 2023

I haven't tried to exactly isolate the bug yet, but the attached exhibits a failure case. The voronoi library at https://github.com/Dozed12/p5.voronoi seems to handle this set of points correctly. It looks like the circumcenters are correctly computed.

vfail2.txt

@mbostock mbostock added the bug Something isn't working label Mar 25, 2023
@mbostock
Copy link
Member

Here is a notebook to reproduce: https://observablehq.com/d/7624520d45e4b44f (In the future, if you’d be willing to share these reproductions as an Observable notebook, it would save us some time. Thank you!)

It renders fine when scaled and translated, as here with Plot:

untitled (32)

Plot.marks(Plot.voronoi(points), Plot.delaunayMesh(points)).plot()

But it looks like the there are some problems computing the Voronoi cell edges for points on the Delaunay hull.

image

@Fil
Copy link
Member

Fil commented Mar 25, 2023

Not that it helps much to understand why the bug happens, but this test case is correct under d3-delaunay@4.

@Fil
Copy link
Member

Fil commented Mar 25, 2023

In the collinear case (a "flat triangle" on the hull)

if (Math.abs(ab) < 1e-9) {
, we compute a to cast a long ray (1e9) towards the exterior in order to compute the circumcenter as the "point at infinity".

In this case a is sometimes 0, or going in the wrong direction, because the reference point r (point 36, marked as a green circle) happens to be exactly on the hull, and collinear with the (flat) triangle whose circumcenter we're trying to compute.

Capture d’écran 2023-03-25 à 10 01 57

So, any flat triangle on the collinear part of the hull has a 1/2 chance to have its (degenerate) circumcenter be projected in the wrong direction. This results in the weird polygonCell 180 (also in green).

A quick and dirty patch:

-        const r = triangles[0] * 2;
-        a *= Math.sign((points[r] - x1) * ey - (points[r + 1] - y1) * ex);
+        let i=0;
+        while (i < triangles.length && Math.abs(a) < 1e-3) {
+          const r = triangles[i++] * 2;
+          a =  ((points[r] - x1) * ey - (points[r + 1] - y1) * ex);
+        }
+        a = Math.sign(a) * 1e9;

There might be a better approach though, using the fact that the hull points are enumerated clockwise? Needs a bit more thought.

Fil added a commit that referenced this issue Mar 25, 2023
@Fil
Copy link
Member

Fil commented Mar 25, 2023

#142 fixes it, thanks for the test case!

mbostock added a commit that referenced this issue Apr 1, 2023
* fix #141

* bx, by

---------

Co-authored-by: Mike Bostock <mbostock@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

3 participants