Skip to content

Commit

Permalink
fix cases of bad diagonals in the split routine
Browse files Browse the repository at this point in the history
ports
mapbox/earcut@d13c8dc
47f27ce1
  • Loading branch information
mourner committed Mar 23, 2016
1 parent 6d5e318 commit db215c0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ Import the project from https://github.com/mapbox/earcut.hpp.git and you should

## Status

This is currently based on [earcut 2.0.9](https://github.com/mapbox/earcut#209-mar-10-2016).
This is currently based on [earcut 2.1.1](https://github.com/mapbox/earcut#211-mar-17-2016).
6 changes: 4 additions & 2 deletions include/earcut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,8 @@ bool Earcut<N>::pointInTriangle(double ax, double ay, double bx, double by, doub
// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
template <typename N>
bool Earcut<N>::isValidDiagonal(Node* a, Node* b) {
return equals(a, b) || (a->next->i != b->i && a->prev->i != b->i && !intersectsPolygon(a, b) &&
locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b));
return a->next->i != b->i && a->prev->i != b->i && !intersectsPolygon(a, b) &&
locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b);
}

// signed area of a triangle
Expand All @@ -664,6 +664,8 @@ bool Earcut<N>::equals(const Node* p1, const Node* p2) {
// check if two segments intersect
template <typename N>
bool Earcut<N>::intersects(const Node* p1, const Node* q1, const Node* p2, const Node* q2) {
if ((equals(p1, q1) && equals(p2, q2)) ||
(equals(p1, q2) && equals(p2, q1))) return true;
return area(p1, q1, p2) > 0 != area(p1, q1, q2) > 0 &&
area(p2, q2, p1) > 0 != area(p2, q2, q1) > 0;
}
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/bad_diagonals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is auto-generated, manual changes will be lost if the code is regenerated.

#include "geometries.hpp"

MAPBOX_MSVC_DISABLE_OPTIMIZATION()
namespace mapbox {
namespace fixtures {

const IntegerPolygon bad_diagonals = {
{{440,4152},{440,4208},{296,4192},{368,4192},{400,4200},{400,4176},{368,4192},{296,4192},{264,4200},{288,4160},{296,4192}},
};

}
}
MAPBOX_MSVC_ENABLE_OPTIMIZATION()
1 change: 1 addition & 0 deletions test/fixtures/geometries.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ using DoublePolygon = Polygon<DoublePoint>;


extern const ShortPolygon park;
extern const IntegerPolygon bad_diagonals;
extern const IntegerPolygon bad_hole;
extern const IntegerPolygon building;
extern const IntegerPolygon degenerate;
Expand Down
3 changes: 2 additions & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int main() {
areaTest<int>("water3", mapbox::fixtures::water3, 197);
areaTest<int>("water3b", mapbox::fixtures::water3b, 25);
areaTest<int>("water4", mapbox::fixtures::water4, 705);
areaTest<int>("water_huge", mapbox::fixtures::water_huge, 5173, 0.0011, 0.0002);
areaTest<int>("water_huge", mapbox::fixtures::water_huge, 5174, 0.0011, 0.0002);
areaTest<int>("water_huge2", mapbox::fixtures::water_huge2, 4461, 0.0028, 0.00015);
areaTest<int>("degenerate", mapbox::fixtures::degenerate, 0);
areaTest<int>("bad_hole", mapbox::fixtures::bad_hole, 42, 0.042, 0.0022);
Expand All @@ -144,6 +144,7 @@ int main() {
areaTest<double>("issue52", mapbox::fixtures::issue52, 109, 1e-13);
areaTest<double>("eberly3", mapbox::fixtures::eberly_3, 73, 1e-13);
areaTest<double>("shared-points", mapbox::fixtures::shared_points, 4);
areaTest<double>("bad-diagonals", mapbox::fixtures::bad_diagonals, 7);

return 0;
}

0 comments on commit db215c0

Please sign in to comment.