Don't snap polygons a second time when removing overlaps

Removing the area that a polygon shares with an earlier polygon ran wagyu on
geometry that had already been snapped to tile coordinates, and snapping it
again could change it. In Dane County tile 8/64/93, consistent snapping bent
one block's edge into a 45-degree diagonal that left it a half-pixel
triangle, and when that block's overlap with another block was removed, the
diagonal passed through the corner of the hot pixel of the vertex beside it,
so it was snapped again and the triangle collapsed, leaving a hole between
it and its neighbor. Elsewhere in the same tile, two half-pixel overlaps
were left behind the same way.

The polygons' edges no longer cross after consistent snapping, so the
difference needs no new vertices. So compute it at 16 times the tile
resolution, where the vertices are far from each other's hot pixels, and use
the result if all of its vertices are the original ones. Only if they aren't,
fall back to snapping at tile resolution to the layer's hot pixels.

For Dane County at -D8 -d8 -pt -pn -zg, this reduces the overlapping
pixels at z8, z9, and z10 from 7, 8, and 16 to 5, 3, and 3, and removes the
hole and both overlaps in 8/64/93 at -z8.

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:13:36 +00:00
parent 082487b594
commit 4d761d4307
4 changed files with 168 additions and 31 deletions
+113
View File
@@ -863,3 +863,116 @@ TEST_CASE("Overlaps between polygons go to the earlier polygon", "[wagyu]") {
REQUIRE(get_mp_area(second) == 100);
REQUIRE(get_mp_area(third) == 0);
}
TEST_CASE("Removing overlaps doesn't snap the polygons again", "[wagyu]") {
// Census blocks around a half-pixel triangle (in Dane County tile 8/64/93),
// as rounded to tile coordinates, in order of sequence number. Consistent
// snapping leaves the triangle in one of them, whose diagonal edge then
// passes through the corner of the hot pixel of the vertex beside it.
// Removing that block's overlap with another must not snap it again,
// which collapsed the triangle and left a hole.
auto parse = [](const char *s) {
drawvec g;
std::istringstream in(s);
std::string tok;
while (in >> tok) {
bool m = tok[0] == 'M';
if (m) {
tok = tok.substr(1);
}
long long x, y;
sscanf(tok.c_str(), "%lld,%lld", &x, &y);
g.push_back(draw(m ? VT_MOVETO : VT_LINETO, x, y));
}
g.push_back(draw(VT_LINETO, (long long) g[0].x, (long long) g[0].y));
return g;
};
std::vector<drawvec> in = {
parse("M65,245 65,244 66,244 66,244 66,245 65,245"),
parse("M66,244 66,244 65,244 65,244 66,244"),
parse("M67,244 67,244 67,244 67,244 67,243 67,243 67,243 67,243 67,243 67,244"),
parse("M66,244 66,244 66,244 67,245 67,244 67,245 67,245 66,245 66,245 66,245 66,244"),
parse("M66,244 66,244 66,244 66,244 67,244 67,244 67,244 67,244 66,244 66,244"),
parse("M68,242 67,243 67,243 66,243 66,242 67,242 68,242 67,243 68,243"),
parse("M65,244 65,245 65,245 65,245 65,245 65,244"),
parse("M64,244 65,245 64,245 64,244 64,244"),
parse("M65,244 65,244 65,244 64,244 64,244 65,244"),
parse("M65,244 65,244 66,244 66,244 66,244 66,244 65,244"),
parse("M66,244 67,244 67,244 67,244 67,244 67,244 66,244 66,244 66,244"),
parse("M69,240 71,238 73,237 73,241 69,240"),
parse("M66,243 66,243 66,243 66,243 66,244 66,244 65,244 65,244 66,243"),
parse("M67,244 67,243 68,243 68,243 68,243 68,244 67,244"),
parse("M67,243 67,243 67,243 68,243 68,243 65,244"),
parse("M61,244 63,239 66,235 69,234 69,236 67,236 68,241 68,242 67,242 67,241 65,242 65,243 65,243 65,244 64,244 64,243 62,244 62,244 61,244"),
parse("M65,245 65,245 65,244 65,244 65,244 65,244 65,244 65,244 65,244 65,244 65,244 65,243 65,243 65,243 65,244 65,244 65,244 65,244 65,245"),
parse("M67,244 67,244 68,244 68,244 68,244 68,244 68,244 68,244 73,245 73,245 73,249 72,249 71,249 71,249 69,249 66,247 66,247 66,247 66,245 67,245 67,245 67,245 67,244 67,244"),
parse("M64,244 64,245 64,245 64,244 64,244"),
parse("M66,244 67,244 67,245 67,244 67,245 66,244"),
parse("M69,243 68,244 68,244 68,243 68,243 68,243 68,243 69,243"),
parse("M68,241 67,236 69,236 69,240 68,241"),
parse("M73,241 73,243 73,244 68,244 68,244 68,244 69,243 68,243 67,243 68,242 68,241 69,240 73,241"),
parse("M67,243 67,244 67,244 67,243 67,243"),
parse("M65,244 65,243 66,243 66,242 66,243 65,244"),
parse("M66,242 66,242 66,242 66,242 66,243 66,242"),
parse("M63,244 64,244 64,245 63,245 63,244"),
parse("M64,245 64,244 64,245 64,245 64,245"),
parse("M65,244 65,244 65,244 65,244 65,245 65,244 65,244"),
parse("M66,244 66,243 66,243 66,243 66,243 67,242 66,242 66,243 67,243 67,243 67,243 67,244 66,244 66,244 66,244 66,244"),
parse("M67,244 67,244 66,244 66,244 67,244"),
parse("M67,245 67,244 67,245 67,245"),
parse("M73,245 68,244 68,244 73,244 73,245"),
parse("M64,244 64,244 64,244 64,244 64,244"),
parse("M65,243 65,243 65,242 67,241 67,242 66,243 66,242 66,242 66,242 66,242 66,243 65,243"),
parse("M69,236 71,238 69,240 69,236"),
parse("M64,245 64,245 64,244 64,244 64,245"),
parse("M66,244 66,244 66,244 66,244 66,244 66,245 66,245 66,245 66,245 66,245 65,245 66,245 66,244 66,244 63,244 63,244 64,244"),
parse("M65,244 65,245 64,244 64,244 63,244 63,244 63,245 63,244 63,244"),
parse("M65,245 67,243 67,243")
};
std::vector<drawvec const *> ptrs;
for (auto const &g : in) {
ptrs.push_back(&g);
}
hot_pixel_list hot = layer_hot_pixels(ptrs);
std::vector<drawvec> out;
for (auto const &g : in) {
bool ok;
out.push_back(clean_polygon_with_hot_pixels(g, hot, &ok));
}
std::vector<drawvec *> geoms;
for (auto &g : out) {
geoms.push_back(&g);
}
remove_overlaps(geoms, &hot);
// Sample the area (with coordinates doubled, so the samples are at
// quarter-pixel offsets that are never on a boundary)
for (auto &g : out) {
for (auto &d : g) {
d.x *= 4;
d.y *= 4;
}
}
size_t overlaps = 0;
for (long long y = 238 * 4 + 1; y < 245 * 4; y += 2) {
for (long long x = 62 * 4 + 1; x < 70 * 4; x += 2) {
size_t n = 0;
for (auto const &g : out) {
n += pnpoly_mp(g, x, y);
}
if (n > 1) {
overlaps++;
}
}
}
REQUIRE(overlaps == 0);
// The triangle between (67, 241), (67, 242), and (66, 242) is covered once
size_t n = 0;
for (auto const &g : out) {
n += pnpoly_mp(g, 4 * 67 - 1, 4 * 242 - 1);
}
REQUIRE(n == 1);
}