diff --git a/polygon.cpp b/polygon.cpp index 9de0f4ef..372a7fc3 100644 --- a/polygon.cpp +++ b/polygon.cpp @@ -421,14 +421,19 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail, double simpl return out; } -drawvec outline_strips(drawvec const &geom, int z, int detail) { +drawvec outline_strips(drawvec const &geom, int z, int detail, double simplification, long long ox, long long oy, bool *all_shared) { drawvec out; + *all_shared = false; int shift = 32 - detail - z; if (shift <= 0) { return out; } + // How far a simplified shared border may have strayed from the original shared edges + const double tolerance = (1 + std::max(simplification, 0.0)) * (1LL << shift); + size_t edges = 0, shared = 0; + for (size_t i = 0; i < geom.size(); i++) { if (geom[i].op == VT_MOVETO) { size_t j; @@ -441,6 +446,20 @@ drawvec outline_strips(drawvec const &geom, int z, int detail) { if (j - i >= 3) { for (size_t k = i; k < j; k++) { size_t n = (k + 1 < j) ? k + 1 : i; // close the ring if it isn't already + if (geom[k].x == geom[n].x && geom[k].y == geom[n].y) { + continue; + } + edges++; + + // Along a border shared with another feature, that feature + // already covers the space, so leave it to close over it. + if (shared_edges_count > 0 && + (shared_toward(geom[k].x + ox, geom[k].y + oy, geom[n].x + ox, geom[n].y + oy, tolerance) || + shared_toward(geom[n].x + ox, geom[n].y + oy, geom[k].x + ox, geom[k].y + oy, tolerance))) { + shared++; + continue; + } + add_strip(out, geom[k].x, geom[k].y, geom[n].x, geom[n].y, shift); } } @@ -449,6 +468,7 @@ drawvec outline_strips(drawvec const &geom, int z, int detail) { } } + *all_shared = edges > 0 && shared == edges; return out; } diff --git a/polygon.hpp b/polygon.hpp index 73fa0e99..1592f9b1 100644 --- a/polygon.hpp +++ b/polygon.hpp @@ -31,9 +31,12 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail, double simpl double unshared_perimeter(drawvec const &geom, size_t i, size_t j, long long ox, long long oy); // Strips (in tile coordinates) one pixel wide along every edge of a polygon -// (in world coordinates), which together trace its outline. This is used to -// stand in for a polygon that has collapsed away to nothing entirely. -drawvec outline_strips(drawvec const &geom, int z, int detail); +// (in world coordinates relative to a tile origin at (ox, oy)), which together +// trace its outline. This is used to stand in for a polygon that has collapsed +// away to nothing entirely. Edges shared with other features (with +// --no-simplification-of-shared-nodes) are left out, since those features +// already cover the space, and *all_shared is set if every edge was shared. +drawvec outline_strips(drawvec const &geom, int z, int detail, double simplification, long long ox, long long oy, bool *all_shared); // Union the strips from thin_polygon_strips() with a cleaned polygon // (both in tile coordinates) diff --git a/tile.cpp b/tile.cpp index bc4894ef..2b0c3ae8 100644 --- a/tile.cpp +++ b/tile.cpp @@ -718,13 +718,23 @@ static void *simplification_worker(void *v) { // which keeps its shape and location. Only if that too is empty, // because the whole polygon is within a pixel or so, fall back to // a rectangle with the polygon's area (but at least a pixel). + // + // But with --no-simplification-of-shared-nodes, a polygon whose + // borders are all shared with other features is left out instead, + // so that its neighbors close over the space without overlapping. geom.clear(); + bool all_shared = false; if (world.size() > 0) { - geom = union_polygon_strips(drawvec(), outline_strips(world, z, out_detail)); + geom = union_polygon_strips(drawvec(), outline_strips(world, z, out_detail, (*features)[i]->simplification, + (long long) (*features)[i]->tx << (32 - z), (long long) (*features)[i]->ty << (32 - z), &all_shared)); } if (geom.size() < 3) { - // area is in world coordinates, calculated before scaling down - geom = revive_polygon(before, area, z, out_detail); + if (all_shared) { + geom.clear(); + } else { + // area is in world coordinates, calculated before scaling down + geom = revive_polygon(before, area, z, out_detail); + } } } else { geom.clear(); diff --git a/unit.cpp b/unit.cpp index 7e7062fa..d1f96947 100644 --- a/unit.cpp +++ b/unit.cpp @@ -674,7 +674,9 @@ TEST_CASE("A polygon that collapses entirely can be replaced by its outline", "[ to_tile_scale(scaled, z, detail); REQUIRE(clean_or_clip_poly(scaled, 0, 0, false, true).size() == 0); - drawvec outline = union_polygon_strips(drawvec(), outline_strips(geom, z, detail)); + bool all_shared; + drawvec outline = union_polygon_strips(drawvec(), outline_strips(geom, z, detail, 1, 0, 0, &all_shared)); + REQUIRE(!all_shared); double area = get_mp_area(outline); // About a pixel wide along both arms, rather than a square