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
+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) {