Skip to content

Commit

Permalink
fix infinite loops, simplify hole elimination
Browse files Browse the repository at this point in the history
Co-Authored-By: Juha Laukala <104564021+JuhaLaukala@users.noreply.github.com>
  • Loading branch information
mourner and JuhaLaukala committed Jul 4, 2022
1 parent a189518 commit ac18648
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/earcut.js
Expand Up @@ -281,7 +281,6 @@ function eliminateHoles(data, holeIndices, outerNode, dim) {
// process holes from left to right
for (i = 0; i < queue.length; i++) {
outerNode = eliminateHole(queue[i], outerNode);
outerNode = filterPoints(outerNode, outerNode.next);
}

return outerNode;
Expand All @@ -301,11 +300,8 @@ function eliminateHole(hole, outerNode) {
var bridgeReverse = splitPolygon(bridge, hole);

// filter collinear points around the cuts
var filteredBridge = filterPoints(bridge, bridge.next);
filterPoints(bridgeReverse, bridgeReverse.next);

// Check if input node was removed by the filtering
return outerNode === bridge ? filteredBridge : outerNode;
return filterPoints(bridge, bridge.next);
}

// David Eberly's algorithm for finding a bridge between hole and outer polygon
Expand Down
4 changes: 3 additions & 1 deletion test/expected.json
Expand Up @@ -41,7 +41,9 @@
"touching3": 15,
"touching4": 20,
"rain": 2681,
"issue131": 12
"issue131": 12,
"infinite-loop-jhl" : 0,
"filtered-bridge-jhl" : 25
},
"errors": {
"dude": 2e-15,
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/filtered-bridge-jhl.json
@@ -0,0 +1,7 @@
[
[[ 22.0, 14.0 ], [ 17.0, 12.0 ], [ 5.0, 12.0 ], [ 0.0, 12.0 ], [ 0.0, 0.0 ], [ 22.0, 0.0 ]],
[[ 9.0, 4.0 ], [ 10.0, 4.0 ], [ 10.0, 3.0 ], [ 9.0, 3.0 ]],
[[ 6.0, 9.0 ], [ 7.0, 9.0 ], [ 7.0, 8.0 ], [ 6.0, 8.0 ]],
[[ 7.0, 10.0 ], [ 17.0, 10.0 ], [ 17.0, 5.0 ], [ 8.0, 5.0 ]],
[[ 13.0, 4.0 ], [ 14.0, 4.0 ], [ 14.0, 3.0 ], [ 13.0, 3.0 ]]
]
4 changes: 4 additions & 0 deletions test/fixtures/infinite-loop-jhl.json
@@ -0,0 +1,4 @@
[
[[-1.0, 2.0], [0.0, 0.0], [2.0, -1.0]],
[[2.0, -1.0], [0.0, 1.0e-28], [-1.0, 2.0]]
]

0 comments on commit ac18648

Please sign in to comment.