Treat hot pixels as half-open when snapping to a layer's common set

With consistent snapping, edges are snapped to every hot pixel they touch,
including those whose corners they only touch. But with closed pixels, an
edge passing diagonally through a lattice point touches the corners of the
two pixels beside it as well, and is snapped into a zigzag through all four.
Two edges from different polygons that cross at a pixel corner zigzag
through the same pixels in different orders, and the zigzags cross each
other. (In Dane County tile 10/258/376, this left a quarter-pixel gap and a
quarter-pixel overlap between two blocks on either side of a sliver.)

Hobby's snap rounding, whose guarantee that snapped edges don't cross is
what makes snapping to a common set of hot pixels work, uses half-open
pixels, [x - 0.5, x + 0.5) by [y - 0.5, y + 0.5), consistent with rounding
halves up, which is how wagyu rounds intersection points. So when snapping
to a common set of hot pixels, find the range of pixels an edge passes
through in each row that way, so that a point on a pixel boundary belongs
to only one pixel.

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 00:58:29 +00:00
parent 162cb1c1b0
commit b8e8542dfa
6 changed files with 141 additions and 11 deletions
+70
View File
@@ -12,6 +12,7 @@
#include <limits.h>
#include <tuple>
#include <algorithm>
#include <sstream>
TEST_CASE("UTF-8 enforcement", "[utf8]") {
REQUIRE(check_utf8("") == std::string(""));
@@ -756,3 +757,72 @@ TEST_CASE("Snapping to a layer's hot pixels keeps shared borders consistent", "[
REQUIRE(gaps == 0);
REQUIRE(overlaps == 0);
}
TEST_CASE("Consistent snapping doesn't make neighboring polygons' edges cross", "[wagyu]") {
// Five Census blocks around a sliver whose sides crossed each other when
// rounded to tile coordinates. With closed hot pixels, edges passing
// diagonally through pixel corners were snapped into zigzags that visited
// the same pixels in different orders, and edges of two neighboring blocks
// crossed each other. With half-open hot pixels, they only meet at vertices.
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("M196,147 196,147 196,149 199,147 198,146 199,146 200,147 200,148 201,149 200,150 203,150 203,155 204,154 204,155 207,158 207,158 201,152 203,153 203,150 200,150 201,149 198,146 199,148 197,148 198,149 196,147"),
parse("M196,149 196,147 199,145 200,147 199,146 198,146 199,147 196,149"),
parse("M198,146 201,149 200,150 203,150 203,153 201,152 207,158 206,162 192,162 192,148 196,147 198,149 197,148 199,148 198,146"),
parse("M200,148 207,148 207,158 204,155 204,154 203,155 203,150 200,150 201,149 200,148"),
parse("M199,145 207,144 207,148 200,148 200,147 199,145"),
};
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));
REQUIRE(ok);
}
auto orient = [](draw const &a, draw const &b, draw const &c) {
long long v = ((long long) b.x - a.x) * ((long long) c.y - a.y) - ((long long) b.y - a.y) * ((long long) c.x - a.x);
return (v > 0) - (v < 0);
};
size_t crossings = 0;
for (size_t a = 0; a < out.size(); a++) {
for (size_t b = a + 1; b < out.size(); b++) {
for (size_t i = 0; i + 1 < out[a].size(); i++) {
if (out[a][i + 1].op != VT_LINETO) {
continue;
}
for (size_t j = 0; j + 1 < out[b].size(); j++) {
if (out[b][j + 1].op != VT_LINETO) {
continue;
}
draw const &p1 = out[a][i], &p2 = out[a][i + 1], &q1 = out[b][j], &q2 = out[b][j + 1];
if (orient(p1, p2, q1) * orient(p1, p2, q2) < 0 && orient(q1, q2, p1) * orient(q1, q2, p2) < 0) {
crossings++;
}
}
}
}
}
REQUIRE(crossings == 0);
}