Skip to content

Commit

Permalink
fix a polygon clipping edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Oct 22, 2018
1 parent e172e9c commit 20debb0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/mapbox/geojsonvt/clip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ class clipper {

slice = newSlice(line);

} else if (bk >= k1) { // ---|--> |
} else if (bk > k1) { // ---|--> |
t = calc_progress<I>(a, b, k1);
slice.push_back(intersect<I>(a, b, k1, t));
if (lineMetrics) slice.segStart = lineLen + segLen * t;

if (i == len - 2)
slice.push_back(b); // last point
}
} else if (ak >= k2) {
} else if (ak > k2) {
if (bk < k1) { // <--|-----|---
t = calc_progress<I>(a, b, k2);
slice.push_back(intersect<I>(a, b, k2, t));
Expand Down Expand Up @@ -202,7 +202,7 @@ class clipper {
const double bk = get<I>(b);

if (ak < k1) {
if (bk >= k1) {
if (bk > k1) {
// ---|--> |
slice.push_back(intersect<I>(a, b, k1, calc_progress<I>(a, b, k1)));
if (bk > k2)
Expand All @@ -211,7 +211,7 @@ class clipper {
else if (i == len - 2)
slice.push_back(b); // last point
}
} else if (ak >= k2) {
} else if (ak > k2) {
if (bk < k2) { // | <--|---
slice.push_back(intersect<I>(a, b, k2, calc_progress<I>(a, b, k2)));
if (bk < k1) // <--|-----|---
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/polygon-bug.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "Polygon",
"coordinates": [[
[42.1875, 57.32652122521708],
[47.8125, 57.32652122521708],
[47.8125, 54.16243396806781],
[42.1875, 54.16243396806781],
[42.1875, 57.32652122521708]
]]
}
21 changes: 21 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ TEST(GetTile, AntimeridianTriangle) {
}
}

TEST(GetTile, PolygonClippingBug) {
const auto geojson = mapbox::geojson::parse(loadFile("test/fixtures/polygon-bug.json"));

Options options;
options.buffer = 1024;

GeoJSONVT index{ geojson, options };

auto tile = index.getTile(5, 19, 9);
ASSERT_EQ(tile.features.size(), 1);
ASSERT_EQ(tile.num_points, 5);

const mapbox::geometry::polygon<int16_t> expected{
{ {3072, 3072}, {5120, 3072}, {5120, 5120}, {3072, 5120}, {3072, 3072} }
};

const auto actual = tile.features[0].geometry.get<mapbox::geometry::polygon<int16_t>>();

ASSERT_EQ(actual, expected);
}

TEST(GetTile, Projection) {
const auto geojson = mapbox::geojson::parse(loadFile("test/fixtures/linestring.json"));

Expand Down

0 comments on commit 20debb0

Please sign in to comment.