Find all the overlaps between polygons when removing them

Overlap removal could miss overlapping polygons in three ways:

- An overlap can be bounded entirely by vertices that both polygons share
  (for example, a half-pixel triangle cut off one block's square by another
  block's diagonal), so no vertex is strictly inside the other polygon. But
  the overlap is then bounded by an edge of one polygon that isn't on the
  other's boundary, so the edge's midpoint is strictly inside the other.
  So test the midpoints of edges as well as the vertices, in doubled
  coordinates so the test stays exact.

- A polygon whose edges weren't snapped to the layer's hot pixels (an
  outline replacing a collapsed polygon, or one with spindle strips) can run
  along another polygon's border without having vertices where the other
  does, so neither the shared-edge test nor the vertex and midpoint tests see
  the overlap. So first split each polygon's edges exactly wherever another
  polygon's vertex lies on them, which doesn't change any shape. Polygons
  whose overlaps aren't removed are left as they were.

- Where spindle strips made two polygons' edges cross, the difference has to
  snap the crossing to tile coordinates, which can leave a smaller overlap.
  So repeat overlap removal until nothing changes, at most 4 times.

For Dane County Census blocks at -D8 -d8 -pt -pn, there are now no points
covered by more than one polygon at any zoom level (sampled at four points
per pixel), with -z8 and with -zg up to z12, and no self-intersecting
polygons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
This commit is contained in:
Claude
2026-12-01 02:28:58 +00:00
parent 4d761d4307
commit c7f45ee81d
5 changed files with 167 additions and 15 deletions
+43
View File
@@ -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<std::pair<long long, long long>> 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<drawvec *> 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);
}
}
}