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
+5 -1
View File
@@ -44,7 +44,11 @@
them and not the other, leaving a shard gap or overlap between them. Now the
hot pixels of all the polygons in the layer are found first, and each polygon
is snapped to the ones near it as well as its own, so shared borders are
snapped identically. For Dane County Census blocks at -D8 -d8 -pt -pn, this
snapped identically. In this mode the hot pixels are treated as half-open,
[x - 0.5, x + 0.5) by [y - 0.5, y + 0.5), as in Hobby's snap rounding,
rather than as closed squares, because edges passing diagonally through
pixel corners otherwise zigzagged through the same pixels in different
orders in different polygons, and could cross each other. 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.
+2 -1
View File
@@ -162,7 +162,8 @@ struct ring_manager {
// The hot pixels that are the rounded locations of intersections, sorted,
// which edges must be snapped to even if they only touch their corners
hot_pixel_vector<T> intersection_hot_pixels;
// Snap edges to every hot pixel they touch, even ones whose corners they only touch
// Snapping to a common set of hot pixels shared with other geometries:
// treat pixels as half-open, and don't skip any corner touches that remain
bool snap_all_corners = false;
hot_pixel_itr<T> current_hp_itr;
std::deque<point<T>> points;
+57 -2
View File
@@ -189,6 +189,55 @@ inline T get_edge_max_x(edge<T> const& edge, const T current_y, bool inward = fa
}
}
// The range of columns of hot pixels that an edge passes through within a row,
// treating each pixel as half-open, [x - 0.5, x + 0.5) by [y - 0.5, y + 0.5),
// which is consistent with the rounding of intersection points. Unlike the
// closed pixels used by get_edge_min_x and get_edge_max_x, a point on the
// boundary between two pixels belongs to only one of them, so an edge passing
// diagonally through a corner is not snapped to both of the pixels on either
// side of the corner. When several geometries are snapped to a common set of
// hot pixels, snapping to both made edges from different geometries zigzag
// through the same pixels in different orders and cross each other.
template <typename T>
inline void get_edge_half_open_x_range(edge<T> const& edge, const T current_y, T& x_min, T& x_max) {
if (is_horizontal(edge)) {
x_min = std::min(edge.bot.x, edge.top.x);
x_max = std::max(edge.bot.x, edge.top.x);
return;
}
// edge.top has the smaller y and edge.bot the larger
double y_lo = static_cast<double>(current_y) - 0.5;
double y_hi = static_cast<double>(current_y) + 0.5;
bool hi_included = false;
if (y_lo < static_cast<double>(edge.top.y)) {
y_lo = static_cast<double>(edge.top.y);
}
if (y_hi >= static_cast<double>(edge.bot.y)) {
y_hi = static_cast<double>(edge.bot.y);
hi_included = true;
}
double x_lo = static_cast<double>(edge.bot.x) + edge.dx * (y_lo - static_cast<double>(edge.bot.y));
double x_hi = static_cast<double>(edge.bot.x) + edge.dx * (y_hi - static_cast<double>(edge.bot.y));
if (x_lo <= x_hi) {
x_min = static_cast<T>(std::floor(x_lo + 0.5));
if (hi_included) {
x_max = static_cast<T>(std::floor(x_hi + 0.5));
} else {
x_max = static_cast<T>(std::ceil(x_hi + 0.5)) - 1;
}
} else {
x_max = static_cast<T>(std::floor(x_lo + 0.5));
if (hi_included) {
x_min = static_cast<T>(std::floor(x_hi + 0.5));
} else {
x_min = static_cast<T>(std::floor(x_hi - 0.5)) + 1;
}
}
}
template <typename T>
void hot_pixel_set_left_to_right(T y,
T start_x,
@@ -207,8 +256,11 @@ void hot_pixel_set_left_to_right(T y,
T x_min_inward = get_edge_min_x(*(bnd.current_edge), y, true);
T x_max_inward = get_edge_max_x(*(bnd.current_edge), y, true);
T x_min = get_edge_min_x(*(bnd.current_edge), y);
x_min = std::max(x_min, start_x);
T x_max = get_edge_max_x(*(bnd.current_edge), y);
if (rings.snap_all_corners) {
get_edge_half_open_x_range(*(bnd.current_edge), y, x_min, x_max);
}
x_min = std::max(x_min, start_x);
x_max = std::min(x_max, end_x);
for (; itr != end; ++itr) {
if (itr->x < x_min) {
@@ -257,8 +309,11 @@ void hot_pixel_set_right_to_left(T y,
T x_min_inward = get_edge_min_x(*(bnd.current_edge), y, true);
T x_max_inward = get_edge_max_x(*(bnd.current_edge), y, true);
T x_min = get_edge_min_x(*(bnd.current_edge), y);
x_min = std::max(x_min, end_x);
T x_max = get_edge_max_x(*(bnd.current_edge), y);
if (rings.snap_all_corners) {
get_edge_half_open_x_range(*(bnd.current_edge), y, x_min, x_max);
}
x_min = std::max(x_min, end_x);
x_max = std::min(x_max, start_x);
for (; itr != end; ++itr) {
if (itr->x > x_max) {
File diff suppressed because one or more lines are too long
+3 -3
View File
@@ -21,7 +21,7 @@
,
{ "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.492188, 39.977120 ], [ -100.019531, 39.977120 ], [ -100.019531, 40.044438 ], [ -99.931641, 40.044438 ], [ -99.755859, 40.044438 ], [ -99.492188, 39.977120 ] ] ] } }
,
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.755859, 40.044438 ], [ -99.492188, 39.977120 ], [ -99.316406, 39.977120 ], [ -99.316406, 39.909736 ], [ -100.019531, 39.909736 ], [ -100.019531, 39.977120 ], [ -100.019531, 40.044438 ], [ -99.931641, 40.044438 ], [ -99.755859, 40.044438 ] ] ] } }
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.755859, 40.044438 ], [ -99.492188, 39.977120 ], [ -99.316406, 39.977120 ], [ -99.316406, 39.909736 ], [ -100.019531, 39.909736 ], [ -100.019531, 39.977120 ], [ -99.931641, 40.044438 ], [ -99.755859, 40.044438 ] ] ] } }
] }
] }
,
@@ -91,11 +91,11 @@
,
{ "type": "Feature", "properties": { "name": "R" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.448242, 39.749434 ], [ -99.420776, 39.740986 ], [ -99.385071, 39.743098 ], [ -99.354858, 39.728313 ], [ -99.319153, 39.726201 ], [ -99.297180, 39.717751 ], [ -99.297180, 39.713525 ], [ -99.319153, 39.721976 ], [ -99.354858, 39.724089 ], [ -99.385071, 39.738874 ], [ -99.420776, 39.736762 ], [ -99.448242, 39.745210 ], [ -99.489441, 39.728313 ], [ -99.519653, 39.728313 ], [ -99.555359, 39.713525 ], [ -99.582825, 39.719863 ], [ -99.618530, 39.715638 ], [ -99.654236, 39.732538 ], [ -99.684448, 39.732538 ], [ -99.717407, 39.745210 ], [ -99.753113, 39.734650 ], [ -99.783325, 39.736762 ], [ -99.819031, 39.719863 ], [ -99.854736, 39.719863 ], [ -99.882202, 39.711413 ], [ -99.917908, 39.724089 ], [ -99.953613, 39.726201 ], [ -99.981079, 39.740986 ], [ -100.000305, 39.740986 ], [ -100.000305, 39.745210 ], [ -99.981079, 39.745210 ], [ -99.953613, 39.730426 ], [ -99.917908, 39.728313 ], [ -99.882202, 39.715638 ], [ -99.854736, 39.724089 ], [ -99.819031, 39.724089 ], [ -99.783325, 39.740986 ], [ -99.753113, 39.738874 ], [ -99.717407, 39.749434 ], [ -99.684448, 39.736762 ], [ -99.654236, 39.736762 ], [ -99.618530, 39.719863 ], [ -99.582825, 39.724089 ], [ -99.555359, 39.717751 ], [ -99.519653, 39.732538 ], [ -99.489441, 39.732538 ], [ -99.448242, 39.749434 ] ] ] } }
,
{ "type": "Feature", "properties": { "name": "P" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -99.717407, 40.000268 ], [ -99.747620, 39.985538 ], [ -99.753113, 39.985538 ], [ -99.717407, 40.000268 ] ] ], [ [ [ -99.783325, 39.989747 ], [ -99.813538, 39.985538 ], [ -99.819031, 39.987643 ], [ -99.783325, 39.989747 ] ] ], [ [ [ -99.582825, 40.010787 ], [ -99.555359, 39.996060 ], [ -99.519653, 39.993956 ], [ -99.555359, 39.993956 ], [ -99.582825, 40.010787 ] ] ] ] } }
{ "type": "Feature", "properties": { "name": "P" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -99.717407, 40.000268 ], [ -99.747620, 39.985538 ], [ -99.753113, 39.985538 ], [ -99.717407, 40.000268 ] ] ], [ [ [ -99.783325, 39.989747 ], [ -99.813538, 39.985538 ], [ -99.819031, 39.987643 ], [ -99.783325, 39.989747 ] ] ], [ [ [ -99.851990, 40.002372 ], [ -99.846497, 40.002372 ], [ -99.824524, 39.989747 ], [ -99.851990, 40.002372 ] ] ], [ [ [ -99.582825, 40.010787 ], [ -99.555359, 39.996060 ], [ -99.519653, 39.993956 ], [ -99.555359, 39.993956 ], [ -99.582825, 40.010787 ] ] ] ] } }
,
{ "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.297180, 40.071766 ], [ -99.297180, 40.006580 ], [ -99.319153, 40.017098 ], [ -99.354858, 40.008683 ], [ -99.385071, 40.010787 ], [ -99.420776, 39.993956 ], [ -99.453735, 39.993956 ], [ -99.483948, 39.983434 ], [ -99.519653, 39.993956 ], [ -99.555359, 39.996060 ], [ -99.582825, 40.010787 ], [ -99.618530, 40.008683 ], [ -99.648743, 40.017098 ], [ -99.684448, 40.002372 ], [ -99.717407, 40.000268 ], [ -99.753113, 39.985538 ], [ -99.758606, 39.985538 ], [ -99.783325, 39.989747 ], [ -99.819031, 39.987643 ], [ -99.824524, 39.989747 ], [ -99.846497, 40.002372 ], [ -99.851990, 40.002372 ], [ -99.882202, 40.004476 ], [ -99.917908, 40.017098 ], [ -99.953613, 40.006580 ], [ -99.981079, 40.008683 ], [ -100.000305, 40.000268 ], [ -100.000305, 40.071766 ], [ -99.297180, 40.071766 ] ] ] } }
,
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.912415, 40.017098 ], [ -99.882202, 40.004476 ], [ -99.854736, 40.004476 ], [ -99.851990, 40.002372 ], [ -99.846497, 40.002372 ], [ -99.824524, 39.989747 ], [ -99.819031, 39.987643 ], [ -99.813538, 39.985538 ], [ -99.783325, 39.989747 ], [ -99.758606, 39.985538 ], [ -99.753113, 39.985538 ], [ -99.747620, 39.985538 ], [ -99.717407, 40.000268 ], [ -99.684448, 40.002372 ], [ -99.648743, 40.017098 ], [ -99.618530, 40.008683 ], [ -99.582825, 40.010787 ], [ -99.555359, 39.993956 ], [ -99.519653, 39.993956 ], [ -99.483948, 39.983434 ], [ -99.453735, 39.993956 ], [ -99.420776, 39.993956 ], [ -99.385071, 40.010787 ], [ -99.354858, 40.008683 ], [ -99.319153, 40.017098 ], [ -99.297180, 40.006580 ], [ -99.297180, 39.928695 ], [ -100.000305, 39.928695 ], [ -100.000305, 40.000268 ], [ -99.981079, 40.008683 ], [ -99.953613, 40.006580 ], [ -99.923401, 40.017098 ], [ -99.917908, 40.017098 ], [ -99.912415, 40.017098 ] ] ] } }
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.912415, 40.017098 ], [ -99.882202, 40.004476 ], [ -99.854736, 40.004476 ], [ -99.851990, 40.002372 ], [ -99.824524, 39.989747 ], [ -99.819031, 39.987643 ], [ -99.813538, 39.985538 ], [ -99.783325, 39.989747 ], [ -99.758606, 39.985538 ], [ -99.753113, 39.985538 ], [ -99.747620, 39.985538 ], [ -99.717407, 40.000268 ], [ -99.684448, 40.002372 ], [ -99.648743, 40.017098 ], [ -99.618530, 40.008683 ], [ -99.582825, 40.010787 ], [ -99.555359, 39.993956 ], [ -99.519653, 39.993956 ], [ -99.483948, 39.983434 ], [ -99.453735, 39.993956 ], [ -99.420776, 39.993956 ], [ -99.385071, 40.010787 ], [ -99.354858, 40.008683 ], [ -99.319153, 40.017098 ], [ -99.297180, 40.006580 ], [ -99.297180, 39.928695 ], [ -100.000305, 39.928695 ], [ -100.000305, 40.000268 ], [ -99.981079, 40.008683 ], [ -99.953613, 40.006580 ], [ -99.923401, 40.017098 ], [ -99.917908, 40.017098 ], [ -99.912415, 40.017098 ] ] ] } }
] }
] }
,
+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);
}