diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e848d99..094365a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,10 +53,15 @@ area it covered is covered by both of its neighbors, that area is removed from all but the earliest of them. These overlaps are found from edges that two polygons share in the same direction, which neighbors that tile exactly - never do, and from vertices of one polygon strictly inside another. The + never do, and from vertices and edge midpoints of one polygon strictly + inside another, after splitting each polygon's edges wherever another + polygon's vertex lies on them. The overlapping area is removed at a higher resolution than the tile, so that the polygon isn't snapped to tile coordinates a second time, which could - collapse parts of it that the first snapping had left, and leave holes. For Dane County Census blocks at -D8 -d8 -pt -pn, this + collapse parts of it that the first snapping had left, and leave holes. + Where spindle strips made two polygons' edges cross, removing the overlap + has to snap the crossing to tile coordinates, which can leave a smaller + overlap, so this is repeated until no overlaps are left (at most 4 times). For Dane County Census blocks at -D8 -d8 -pt -pn, this reduces the gaps between blocks at z9 and z10 from 55 and 155 pixels to 13 and 23, and the overlaps from 113 and 175 to 56 and 59, and takes about 7% longer for Natural Earth countries at -z8 -pn. diff --git a/polygon.cpp b/polygon.cpp index 732f2705..547ee345 100644 --- a/polygon.cpp +++ b/polygon.cpp @@ -710,9 +710,10 @@ void for_each_edge(drawvec const &geom, F f) { } } // namespace -// Whether the point (px, py) is strictly inside the polygon `geom` by the -// nonzero winding rule, with exact integer arithmetic. A point exactly on the -// polygon's boundary does not count as inside. +// Whether the point (px / 2, py / 2) is strictly inside the polygon `geom` by +// the nonzero winding rule, with exact integer arithmetic. (The point is given +// in doubled coordinates so that the midpoints of edges can be tested exactly.) +// A point exactly on the polygon's boundary does not count as inside. static bool strictly_inside(drawvec const &geom, long long px, long long py) { int wn = 0; for (size_t i = 0; i < geom.size(); i++) { @@ -724,7 +725,8 @@ static bool strictly_inside(drawvec const &geom, long long px, long long py) { } } for (size_t k = i; k + 1 < j; k++) { - long long x1 = geom[k].x, y1 = geom[k].y, x2 = geom[k + 1].x, y2 = geom[k + 1].y; + long long x1 = 2 * (long long) geom[k].x, y1 = 2 * (long long) geom[k].y; + long long x2 = 2 * (long long) geom[k + 1].x, y2 = 2 * (long long) geom[k + 1].y; long long cross = (x2 - x1) * (py - y1) - (px - x1) * (y2 - y1); if (cross == 0 && std::min(x1, x2) <= px && px <= std::max(x1, x2) && std::min(y1, y2) <= py && py <= std::max(y1, y2)) { return false; // on the boundary @@ -745,7 +747,87 @@ static bool strictly_inside(drawvec const &geom, long long px, long long py) { return wn != 0; } -size_t remove_overlaps(std::vector const &geoms, hot_pixel_list const *hot) { +// Add a vertex to each polygon's edges wherever a vertex of any of the polygons +// lies exactly on the edge, between its ends. This doesn't change any polygon's +// shape, but it means that wherever two polygons' borders run along each other, +// they have the same edges, which overlap detection depends on. +static void split_at_vertices(std::vector &polys) { + const long long cell = 16; + auto key = [](long long cx, long long cy) { + return (cx << 32) ^ (cy & 0xFFFFFFFF); + }; + auto cell_of = [&](long long v) { + return (long long) std::floor(v / (double) cell); + }; + + std::unordered_map>> grid; + for (auto const &g : polys) { + for (auto const &d : g) { + grid[key(cell_of(d.x), cell_of(d.y))].emplace_back(d.x, d.y); + } + } + for (auto &c : grid) { + std::sort(c.second.begin(), c.second.end()); + c.second.erase(std::unique(c.second.begin(), c.second.end()), c.second.end()); + } + + for (auto &g : polys) { + drawvec out; + for (size_t k = 0; k < g.size(); k++) { + out.push_back(g[k]); + if (k + 1 >= g.size() || g[k + 1].op != VT_LINETO) { + continue; + } + + long long ax = g[k].x, ay = g[k].y, bx = g[k + 1].x, by = g[k + 1].y; + long long dx = bx - ax, dy = by - ay; + long long len_sq = dx * dx + dy * dy; + if (len_sq == 0) { + continue; + } + + std::vector>> on; + for (long long cx = cell_of(std::min(ax, bx)); cx <= cell_of(std::max(ax, bx)); cx++) { + for (long long cy = cell_of(std::min(ay, by)); cy <= cell_of(std::max(ay, by)); cy++) { + auto found = grid.find(key(cx, cy)); + if (found == grid.end()) { + continue; + } + for (auto const &v : found->second) { + long long cross = dx * (v.second - ay) - dy * (v.first - ax); + if (cross != 0) { + continue; + } + long long along = dx * (v.first - ax) + dy * (v.second - ay); + if (along > 0 && along < len_sq) { + on.emplace_back(along, v); + } + } + } + } + + std::sort(on.begin(), on.end()); + for (auto const &v : on) { + out.emplace_back(VT_LINETO, v.second.first, v.second.second); + } + } + g = std::move(out); + } +} + +size_t remove_overlaps(std::vector const &geoms_in, hot_pixel_list const *hot) { + // Work on copies whose edges are split wherever another polygon's vertex is on + // them, and only copy back the polygons whose overlapping area is removed. + std::vector split; + for (auto const *g : geoms_in) { + split.push_back(*g); + } + split_at_vertices(split); + std::vector geoms; + for (auto &g : split) { + geoms.push_back(&g); + } + // Which polygon (the earliest) each directed edge is part of. Cleaned rings // all have their polygon's interior on the same side, so two polygons that // tile exactly traverse the border they share in opposite directions, and @@ -769,8 +851,10 @@ size_t remove_overlaps(std::vector const &geoms, hot_pixel_list const }); } - // Where the polygons' boundaries don't share any edges where they overlap, - // one of them has a vertex that is strictly inside the other. + // Where the polygons' boundaries don't share any edges in the same direction + // where they overlap, the overlap is bounded by an edge of one of them that + // isn't on the other's boundary, so the other covers both sides of it, and the + // edge's midpoint (or one of its ends) is strictly inside the other. { const long long cell = 16; std::vector minx(geoms.size()), miny(geoms.size()), maxx(geoms.size()), maxy(geoms.size()); @@ -798,13 +882,24 @@ size_t remove_overlaps(std::vector const &geoms, hot_pixel_list const } for (size_t i = 0; i < geoms.size(); i++) { - for (auto const &d : *geoms[i]) { - auto found = grid.find(key(std::floor(d.x / (double) cell), std::floor(d.y / (double) cell))); + // The vertices and the midpoints of the edges, in doubled coordinates + std::vector> points; + drawvec const &g = *geoms[i]; + for (size_t k = 0; k < g.size(); k++) { + points.emplace_back(2 * (long long) g[k].x, 2 * (long long) g[k].y); + if (k + 1 < g.size() && g[k + 1].op == VT_LINETO) { + points.emplace_back((long long) g[k].x + g[k + 1].x, (long long) g[k].y + g[k + 1].y); + } + } + + for (auto const &pt : points) { + double x = pt.first / 2.0, y = pt.second / 2.0; + auto found = grid.find(key(std::floor(x / cell), std::floor(y / cell))); if (found == grid.end()) { continue; } for (size_t j : found->second) { - if (j == i || d.x < minx[j] || d.x > maxx[j] || d.y < miny[j] || d.y > maxy[j]) { + if (j == i || x < minx[j] || x > maxx[j] || y < miny[j] || y > maxy[j]) { continue; } size_t later = std::max(i, j), earlier = std::min(i, j); @@ -812,7 +907,7 @@ size_t remove_overlaps(std::vector const &geoms, hot_pixel_list const if (std::find(o.begin(), o.end(), earlier) != o.end()) { continue; } - if (strictly_inside(*geoms[j], d.x, d.y)) { + if (strictly_inside(*geoms[j], pt.first, pt.second)) { o.push_back(earlier); } } @@ -880,6 +975,7 @@ size_t remove_overlaps(std::vector const &geoms, hot_pixel_list const if (done) { *geoms[i] = std::move(out); + *geoms_in[i] = *geoms[i]; changed++; } } diff --git a/polygon.hpp b/polygon.hpp index bf54746c..bca75ee2 100644 --- a/polygon.hpp +++ b/polygon.hpp @@ -64,6 +64,6 @@ drawvec clean_polygon_with_hot_pixels(drawvec const &geom, hot_pixel_list const // from all but the first of the polygons that cover it. Returns how many // polygons were changed. // `hot` is the hot pixels of the layer, from layer_hot_pixels(), if any. -size_t remove_overlaps(std::vector const &geoms, hot_pixel_list const *hot); +size_t remove_overlaps(std::vector const &geoms_in, hot_pixel_list const *hot); #endif diff --git a/tile.cpp b/tile.cpp index 8035dc5e..97d93623 100644 --- a/tile.cpp +++ b/tile.cpp @@ -2648,7 +2648,15 @@ long long write_tile(decompressor *geoms, std::atomic *geompos_in, ch layer_geoms.push_back(g.second); } auto hot = hot_pixels_by_layer.find(lp.first); - remove_overlaps(layer_geoms, hot != hot_pixels_by_layer.end() ? &hot->second : NULL); + // Removing an overlap between polygons whose edges cross (which + // spindle strips can make them do) has to snap the new vertices + // where they cross, which can leave a smaller overlap, so repeat + // until there are none left, or a few times at most. + for (int attempt = 0; attempt < 4; attempt++) { + if (remove_overlaps(layer_geoms, hot != hot_pixels_by_layer.end() ? &hot->second : NULL) == 0) { + break; + } + } } } diff --git a/unit.cpp b/unit.cpp index d2487585..dad5c6ec 100644 --- a/unit.cpp +++ b/unit.cpp @@ -976,3 +976,46 @@ TEST_CASE("Removing overlaps doesn't snap the polygons again", "[wagyu]") { } REQUIRE(n == 1); } + +TEST_CASE("Overlaps are found where one polygon's edge runs past another's vertices", "[wagyu]") { + auto ring = [](std::vector> pts) { + drawvec g; + for (size_t i = 0; i < pts.size(); i++) { + g.push_back(draw(i == 0 ? VT_MOVETO : VT_LINETO, pts[i].first, pts[i].second)); + } + g.push_back(draw(VT_LINETO, pts[0].first, pts[0].second)); + if (get_area(g, 0, g.size()) < 0) { + std::reverse(g.begin(), g.end()); + g[0].op = VT_MOVETO; + g[g.size() - 1].op = VT_LINETO; + } + return g; + }; + + // A sliver whose long top edge runs along the top of a square, through its + // corners but without vertices there, and which covers part of the square. + // None of the vertices or edge midpoints of either is strictly inside the + // other, and they share no edges, until the sliver's edge is split at the + // square's corners. + drawvec square = ring({{99, 235}, {100, 235}, {100, 236}, {99, 236}}); + drawvec sliver = ring({{93, 235}, {104, 235}, {93, 236}}); + std::vector geoms = {&square, &sliver}; + + REQUIRE(remove_overlaps(geoms, NULL) == 1); + REQUIRE(get_mp_area(square) == 1); + + // No point is covered by both + auto x4 = [](drawvec g) { + for (auto &d : g) { + d.x *= 4; + d.y *= 4; + } + return g; + }; + drawvec s4 = x4(square), l4 = x4(sliver); + for (long long y = 235 * 4 + 1; y < 236 * 4; y += 2) { + for (long long x = 93 * 4 + 1; x < 104 * 4; x += 2) { + REQUIRE(pnpoly_mp(s4, x, y) + pnpoly_mp(l4, x, y) <= 1); + } + } +}