Skip to content

Commit db215c0

Browse files
committed
fix cases of bad diagonals in the split routine
ports mapbox/earcut@d13c8dc 47f27ce1
1 parent 6d5e318 commit db215c0

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ Import the project from https://github.com/mapbox/earcut.hpp.git and you should
106106

107107
## Status
108108

109-
This is currently based on [earcut 2.0.9](https://github.com/mapbox/earcut#209-mar-10-2016).
109+
This is currently based on [earcut 2.1.1](https://github.com/mapbox/earcut#211-mar-17-2016).

include/earcut.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,8 @@ bool Earcut<N>::pointInTriangle(double ax, double ay, double bx, double by, doub
645645
// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
646646
template <typename N>
647647
bool Earcut<N>::isValidDiagonal(Node* a, Node* b) {
648-
return equals(a, b) || (a->next->i != b->i && a->prev->i != b->i && !intersectsPolygon(a, b) &&
649-
locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b));
648+
return a->next->i != b->i && a->prev->i != b->i && !intersectsPolygon(a, b) &&
649+
locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b);
650650
}
651651

652652
// signed area of a triangle
@@ -664,6 +664,8 @@ bool Earcut<N>::equals(const Node* p1, const Node* p2) {
664664
// check if two segments intersect
665665
template <typename N>
666666
bool Earcut<N>::intersects(const Node* p1, const Node* q1, const Node* p2, const Node* q2) {
667+
if ((equals(p1, q1) && equals(p2, q2)) ||
668+
(equals(p1, q2) && equals(p2, q1))) return true;
667669
return area(p1, q1, p2) > 0 != area(p1, q1, q2) > 0 &&
668670
area(p2, q2, p1) > 0 != area(p2, q2, q1) > 0;
669671
}

test/fixtures/bad_diagonals.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file is auto-generated, manual changes will be lost if the code is regenerated.
2+
3+
#include "geometries.hpp"
4+
5+
MAPBOX_MSVC_DISABLE_OPTIMIZATION()
6+
namespace mapbox {
7+
namespace fixtures {
8+
9+
const IntegerPolygon bad_diagonals = {
10+
{{440,4152},{440,4208},{296,4192},{368,4192},{400,4200},{400,4176},{368,4192},{296,4192},{264,4200},{288,4160},{296,4192}},
11+
};
12+
13+
}
14+
}
15+
MAPBOX_MSVC_ENABLE_OPTIMIZATION()

test/fixtures/geometries.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using DoublePolygon = Polygon<DoublePoint>;
3535

3636

3737
extern const ShortPolygon park;
38+
extern const IntegerPolygon bad_diagonals;
3839
extern const IntegerPolygon bad_hole;
3940
extern const IntegerPolygon building;
4041
extern const IntegerPolygon degenerate;

test/test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int main() {
120120
areaTest<int>("water3", mapbox::fixtures::water3, 197);
121121
areaTest<int>("water3b", mapbox::fixtures::water3b, 25);
122122
areaTest<int>("water4", mapbox::fixtures::water4, 705);
123-
areaTest<int>("water_huge", mapbox::fixtures::water_huge, 5173, 0.0011, 0.0002);
123+
areaTest<int>("water_huge", mapbox::fixtures::water_huge, 5174, 0.0011, 0.0002);
124124
areaTest<int>("water_huge2", mapbox::fixtures::water_huge2, 4461, 0.0028, 0.00015);
125125
areaTest<int>("degenerate", mapbox::fixtures::degenerate, 0);
126126
areaTest<int>("bad_hole", mapbox::fixtures::bad_hole, 42, 0.042, 0.0022);
@@ -144,6 +144,7 @@ int main() {
144144
areaTest<double>("issue52", mapbox::fixtures::issue52, 109, 1e-13);
145145
areaTest<double>("eberly3", mapbox::fixtures::eberly_3, 73, 1e-13);
146146
areaTest<double>("shared-points", mapbox::fixtures::shared_points, 4);
147+
areaTest<double>("bad-diagonals", mapbox::fixtures::bad_diagonals, 7);
147148

148149
return 0;
149150
}

0 commit comments

Comments
 (0)