Repository navigation
Closed
Labels
Description
In rare cases the triangulation fails and appears to originate from the precision of the data sent to the earcut function.
Below is data for a simple ring with a hole which triangulates correctly:
[[143.129527283745121, 61.240160826593642, 12.157953862848911],
[147.399527283763751, 74.780160826630892, 12.157953862848911],
[154.049527283757931, 90.260160827077932, 12.157953862848911],
[174.429527283762581, 81.710160826332872, 12.157953862848911],
[168.03952728374861, 67.040160826407372, 12.157953862848911],
[159.099527283746281, 53.590160826221112, 12.157953862848911]],
[[156.85952728375561, 67.430160827003422, 12.157953862848911],
[157.489527283760251, 67.160160826519132, 12.157953862848911],
[159.969527283741631, 68.350160826928912, 12.157953862848911],
[161.339527283766071, 67.640160826966172, 12.157953862848911],
[159.649527283763751, 63.310160826891662, 12.157953862848911],
[155.759527283749781, 64.880160826258362, 12.157953862848911]]
]
Modifying the y-coordinate of the first ring point slightly (61.240160826593642 -> 61.240160826593640) produces incorrect triangles (tested with the triangulation test of earcut):
[[143.129527283745121, 61.240160826593640, 12.157953862848911],
[147.399527283763751, 74.780160826630892, 12.157953862848911],
[154.049527283757931, 90.260160827077932, 12.157953862848911],
[174.429527283762581, 81.710160826332872, 12.157953862848911],
[168.03952728374861, 67.040160826407372, 12.157953862848911],
[159.099527283746281, 53.590160826221112, 12.157953862848911]],
[[156.85952728375561, 67.430160827003422, 12.157953862848911],
[157.489527283760251, 67.160160826519132, 12.157953862848911],
[159.969527283741631, 68.350160826928912, 12.157953862848911],
[161.339527283766071, 67.640160826966172, 12.157953862848911],
[159.649527283763751, 63.310160826891662, 12.157953862848911],
[155.759527283749781, 64.880160826258362, 12.157953862848911]]
]
In the above case, the triangulation appears to deviate in the isEar function, around line 250, where in one case we have a value of t at -1.xxxe-13 and in the other at +1.xxxe-13 leading to different test results on (t >= 0).
Tested with version 1.4.0 of earcut.
Activity
mourner commentedon Mar 17, 2015
Interesting! I'll take a look. Perhaps we should be less rigid in ear tests and compare things according to some Epsilon error value.
mourner commentedon Mar 17, 2015
Wow, the original sample doesn't triangulate correctly for me...
mourner commentedon Mar 17, 2015
Nevermind, it was a regressed viz page not working correctly... So, epsilon in that particular test seems to help with this example but breaks tests for other shapes. So looks like it'll be pretty tricky to solve...
mourner commentedon Mar 17, 2015
@sctf4 check out how it's done the tests... Basically I'm calculating sum of triangle areas and comparing it to the area of the original polygon. If it doesn't match within a tolerance — it's broken.
mourner commentedon Mar 17, 2015
@sctf4 actually I just figured out that it fails because of the
cureLocalIntersectionsroutine; if we disable that and go straight tosplitEarcut, it works!mourner commentedon Mar 17, 2015
BTW just wondering, where are you using the library?
mourner commentedon Mar 17, 2015
OK seems to be fixed, without much of a perf hit. Added your test case so that it doesn't regress in future. Let me know if you find any other failing cases.
mourner commentedon Mar 17, 2015
Awesome! F4 is such an amazing map. Bummer that it's only 20-30% though, I'd expect a bigger difference. :) Or is it overall processing speed, not just triangulation?
mourner commentedon Mar 17, 2015
I'll look at it, thanks!
mourner commentedon Mar 18, 2015
@sctf4 fixed! Committing a patch now.
Port fix for mapbox/earcut#16
Port fix for mapbox/earcut#16
Port fix for mapbox/earcut#16
Port fix for mapbox/earcut#16