Don't let wagyu snap edges to hot pixels whose corners they only touch

Wagyu's snap rounding finds the hot pixels (pixels containing a vertex or
intersection) that each edge passes through, row by row, by rounding the
x range that the edge covers within the row outward. A value exactly
halfway between two integers was rounded outward too, so a pixel whose
corner the edge merely touched counted as one it passed through.

For a 45-degree edge, the x range always ends exactly on pixel corners,
so two parallel diagonal edges one unit apart were each snapped to the
other's endpoints, and a perfectly valid diagonal polygon one unit wide
(for example, a narrow Census block between two others, after rounding to
tile coordinates) was collapsed to nothing by cleaning, leaving a diagonal
gap between the blocks on either side of it.

Round halfway values inward instead, for this purpose only (the rounding
of intersection points is unchanged). On Dane County census blocks at
-D8 -d8 -pn -pt, this removes about 10-20% of the gaps between blocks at
z9 and z10, without introducing any self-intersecting polygons (checked
independently with exact orientation tests at z6-z12). Upstream wagyu's
rounding is unchanged since 2020.

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-11-30 22:15:42 +00:00
parent 2468cdba25
commit 1b17becfa3
18 changed files with 107 additions and 52 deletions
+30 -4
View File
@@ -100,6 +100,32 @@ T round_towards_max(double val) {
}
}
// For finding the hot pixels that an edge passes through: unlike round_towards_min
// and round_towards_max, a value exactly halfway between two integers is rounded
// inward, so that an edge that only touches the corner of a pixel, without
// passing through it, does not count as passing through it. Otherwise two
// parallel diagonal edges one pixel apart are each snapped to the other's
// vertices, and a valid polygon one pixel wide collapses to nothing.
template <typename T>
T round_inward_min(double val) {
double half = std::floor(val) + 0.5;
if (values_are_equal(val, half)) {
return static_cast<T>(std::ceil(val));
} else {
return static_cast<T>(std::llround(val));
}
}
template <typename T>
T round_inward_max(double val) {
double half = std::floor(val) + 0.5;
if (values_are_equal(val, half)) {
return static_cast<T>(std::floor(val));
} else {
return static_cast<T>(std::llround(val));
}
}
template <typename T>
inline T get_edge_min_x(edge<T> const& edge, const T current_y) {
if (is_horizontal(edge)) {
@@ -114,7 +140,7 @@ inline T get_edge_min_x(edge<T> const& edge, const T current_y) {
} else {
double lower_range_y = static_cast<double>(current_y - edge.bot.y) - 0.5;
double return_val = static_cast<double>(edge.bot.x) + edge.dx * lower_range_y;
T value = round_towards_min<T>(return_val);
T value = round_inward_min<T>(return_val);
return value;
}
} else {
@@ -123,7 +149,7 @@ inline T get_edge_min_x(edge<T> const& edge, const T current_y) {
} else {
double return_val =
static_cast<double>(edge.bot.x) + edge.dx * (static_cast<double>(current_y - edge.bot.y) + 0.5);
T value = round_towards_min<T>(return_val);
T value = round_inward_min<T>(return_val);
return value;
}
}
@@ -143,7 +169,7 @@ inline T get_edge_max_x(edge<T> const& edge, const T current_y) {
} else {
double lower_range_y = static_cast<double>(current_y - edge.bot.y) - 0.5;
double return_val = static_cast<double>(edge.bot.x) + edge.dx * lower_range_y;
T value = round_towards_max<T>(return_val);
T value = round_inward_max<T>(return_val);
return value;
}
} else {
@@ -152,7 +178,7 @@ inline T get_edge_max_x(edge<T> const& edge, const T current_y) {
} else {
double return_val =
static_cast<double>(edge.bot.x) + edge.dx * (static_cast<double>(current_y - edge.bot.y) + 0.5);
T value = round_towards_max<T>(return_val);
T value = round_inward_max<T>(return_val);
return value;
}
}