mirror of
https://github.com/felt/tippecanoe.git
synced 2026-12-05 06:41:41 +01:00
Find all the overlaps between polygons when removing them
Overlap removal could miss overlapping polygons in three ways: - An overlap can be bounded entirely by vertices that both polygons share (for example, a half-pixel triangle cut off one block's square by another block's diagonal), so no vertex is strictly inside the other polygon. But the overlap is then bounded by an edge of one polygon that isn't on the other's boundary, so the edge's midpoint is strictly inside the other. So test the midpoints of edges as well as the vertices, in doubled coordinates so the test stays exact. - A polygon whose edges weren't snapped to the layer's hot pixels (an outline replacing a collapsed polygon, or one with spindle strips) can run along another polygon's border without having vertices where the other does, so neither the shared-edge test nor the vertex and midpoint tests see the overlap. So first split each polygon's edges exactly wherever another polygon's vertex lies on them, which doesn't change any shape. Polygons whose overlaps aren't removed are left as they were. - Where spindle strips made two polygons' edges cross, the difference has to snap the crossing to tile coordinates, which can leave a smaller overlap. So repeat overlap removal until nothing changes, at most 4 times. For Dane County Census blocks at -D8 -d8 -pt -pn, there are now no points covered by more than one polygon at any zoom level (sampled at four points per pixel), with -z8 and with -zg up to z12, and no self-intersecting polygons. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
This commit is contained in:
+7
-2
@@ -53,10 +53,15 @@
|
||||
area it covered is covered by both of its neighbors, that area is removed
|
||||
from all but the earliest of them. These overlaps are found from edges that
|
||||
two polygons share in the same direction, which neighbors that tile exactly
|
||||
never do, and from vertices of one polygon strictly inside another. The
|
||||
never do, and from vertices and edge midpoints of one polygon strictly
|
||||
inside another, after splitting each polygon's edges wherever another
|
||||
polygon's vertex lies on them. The
|
||||
overlapping area is removed at a higher resolution than the tile, so that
|
||||
the polygon isn't snapped to tile coordinates a second time, which could
|
||||
collapse parts of it that the first snapping had left, and leave holes. For Dane County Census blocks at -D8 -d8 -pt -pn, this
|
||||
collapse parts of it that the first snapping had left, and leave holes.
|
||||
Where spindle strips made two polygons' edges cross, removing the overlap
|
||||
has to snap the crossing to tile coordinates, which can leave a smaller
|
||||
overlap, so this is repeated until no overlaps are left (at most 4 times). 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.
|
||||
|
||||
+107
-11
@@ -710,9 +710,10 @@ void for_each_edge(drawvec const &geom, F f) {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Whether the point (px, py) is strictly inside the polygon `geom` by the
|
||||
// nonzero winding rule, with exact integer arithmetic. A point exactly on the
|
||||
// polygon's boundary does not count as inside.
|
||||
// Whether the point (px / 2, py / 2) is strictly inside the polygon `geom` by
|
||||
// the nonzero winding rule, with exact integer arithmetic. (The point is given
|
||||
// in doubled coordinates so that the midpoints of edges can be tested exactly.)
|
||||
// A point exactly on the polygon's boundary does not count as inside.
|
||||
static bool strictly_inside(drawvec const &geom, long long px, long long py) {
|
||||
int wn = 0;
|
||||
for (size_t i = 0; i < geom.size(); i++) {
|
||||
@@ -724,7 +725,8 @@ static bool strictly_inside(drawvec const &geom, long long px, long long py) {
|
||||
}
|
||||
}
|
||||
for (size_t k = i; k + 1 < j; k++) {
|
||||
long long x1 = geom[k].x, y1 = geom[k].y, x2 = geom[k + 1].x, y2 = geom[k + 1].y;
|
||||
long long x1 = 2 * (long long) geom[k].x, y1 = 2 * (long long) geom[k].y;
|
||||
long long x2 = 2 * (long long) geom[k + 1].x, y2 = 2 * (long long) geom[k + 1].y;
|
||||
long long cross = (x2 - x1) * (py - y1) - (px - x1) * (y2 - y1);
|
||||
if (cross == 0 && std::min(x1, x2) <= px && px <= std::max(x1, x2) && std::min(y1, y2) <= py && py <= std::max(y1, y2)) {
|
||||
return false; // on the boundary
|
||||
@@ -745,7 +747,87 @@ static bool strictly_inside(drawvec const &geom, long long px, long long py) {
|
||||
return wn != 0;
|
||||
}
|
||||
|
||||
size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const *hot) {
|
||||
// Add a vertex to each polygon's edges wherever a vertex of any of the polygons
|
||||
// lies exactly on the edge, between its ends. This doesn't change any polygon's
|
||||
// shape, but it means that wherever two polygons' borders run along each other,
|
||||
// they have the same edges, which overlap detection depends on.
|
||||
static void split_at_vertices(std::vector<drawvec> &polys) {
|
||||
const long long cell = 16;
|
||||
auto key = [](long long cx, long long cy) {
|
||||
return (cx << 32) ^ (cy & 0xFFFFFFFF);
|
||||
};
|
||||
auto cell_of = [&](long long v) {
|
||||
return (long long) std::floor(v / (double) cell);
|
||||
};
|
||||
|
||||
std::unordered_map<long long, std::vector<std::pair<long long, long long>>> grid;
|
||||
for (auto const &g : polys) {
|
||||
for (auto const &d : g) {
|
||||
grid[key(cell_of(d.x), cell_of(d.y))].emplace_back(d.x, d.y);
|
||||
}
|
||||
}
|
||||
for (auto &c : grid) {
|
||||
std::sort(c.second.begin(), c.second.end());
|
||||
c.second.erase(std::unique(c.second.begin(), c.second.end()), c.second.end());
|
||||
}
|
||||
|
||||
for (auto &g : polys) {
|
||||
drawvec out;
|
||||
for (size_t k = 0; k < g.size(); k++) {
|
||||
out.push_back(g[k]);
|
||||
if (k + 1 >= g.size() || g[k + 1].op != VT_LINETO) {
|
||||
continue;
|
||||
}
|
||||
|
||||
long long ax = g[k].x, ay = g[k].y, bx = g[k + 1].x, by = g[k + 1].y;
|
||||
long long dx = bx - ax, dy = by - ay;
|
||||
long long len_sq = dx * dx + dy * dy;
|
||||
if (len_sq == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<std::pair<long long, std::pair<long long, long long>>> on;
|
||||
for (long long cx = cell_of(std::min(ax, bx)); cx <= cell_of(std::max(ax, bx)); cx++) {
|
||||
for (long long cy = cell_of(std::min(ay, by)); cy <= cell_of(std::max(ay, by)); cy++) {
|
||||
auto found = grid.find(key(cx, cy));
|
||||
if (found == grid.end()) {
|
||||
continue;
|
||||
}
|
||||
for (auto const &v : found->second) {
|
||||
long long cross = dx * (v.second - ay) - dy * (v.first - ax);
|
||||
if (cross != 0) {
|
||||
continue;
|
||||
}
|
||||
long long along = dx * (v.first - ax) + dy * (v.second - ay);
|
||||
if (along > 0 && along < len_sq) {
|
||||
on.emplace_back(along, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(on.begin(), on.end());
|
||||
for (auto const &v : on) {
|
||||
out.emplace_back(VT_LINETO, v.second.first, v.second.second);
|
||||
}
|
||||
}
|
||||
g = std::move(out);
|
||||
}
|
||||
}
|
||||
|
||||
size_t remove_overlaps(std::vector<drawvec *> const &geoms_in, hot_pixel_list const *hot) {
|
||||
// Work on copies whose edges are split wherever another polygon's vertex is on
|
||||
// them, and only copy back the polygons whose overlapping area is removed.
|
||||
std::vector<drawvec> split;
|
||||
for (auto const *g : geoms_in) {
|
||||
split.push_back(*g);
|
||||
}
|
||||
split_at_vertices(split);
|
||||
std::vector<drawvec *> geoms;
|
||||
for (auto &g : split) {
|
||||
geoms.push_back(&g);
|
||||
}
|
||||
|
||||
// Which polygon (the earliest) each directed edge is part of. Cleaned rings
|
||||
// all have their polygon's interior on the same side, so two polygons that
|
||||
// tile exactly traverse the border they share in opposite directions, and
|
||||
@@ -769,8 +851,10 @@ size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const
|
||||
});
|
||||
}
|
||||
|
||||
// Where the polygons' boundaries don't share any edges where they overlap,
|
||||
// one of them has a vertex that is strictly inside the other.
|
||||
// Where the polygons' boundaries don't share any edges in the same direction
|
||||
// where they overlap, the overlap is bounded by an edge of one of them that
|
||||
// isn't on the other's boundary, so the other covers both sides of it, and the
|
||||
// edge's midpoint (or one of its ends) is strictly inside the other.
|
||||
{
|
||||
const long long cell = 16;
|
||||
std::vector<long long> minx(geoms.size()), miny(geoms.size()), maxx(geoms.size()), maxy(geoms.size());
|
||||
@@ -798,13 +882,24 @@ size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < geoms.size(); i++) {
|
||||
for (auto const &d : *geoms[i]) {
|
||||
auto found = grid.find(key(std::floor(d.x / (double) cell), std::floor(d.y / (double) cell)));
|
||||
// The vertices and the midpoints of the edges, in doubled coordinates
|
||||
std::vector<std::pair<long long, long long>> points;
|
||||
drawvec const &g = *geoms[i];
|
||||
for (size_t k = 0; k < g.size(); k++) {
|
||||
points.emplace_back(2 * (long long) g[k].x, 2 * (long long) g[k].y);
|
||||
if (k + 1 < g.size() && g[k + 1].op == VT_LINETO) {
|
||||
points.emplace_back((long long) g[k].x + g[k + 1].x, (long long) g[k].y + g[k + 1].y);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto const &pt : points) {
|
||||
double x = pt.first / 2.0, y = pt.second / 2.0;
|
||||
auto found = grid.find(key(std::floor(x / cell), std::floor(y / cell)));
|
||||
if (found == grid.end()) {
|
||||
continue;
|
||||
}
|
||||
for (size_t j : found->second) {
|
||||
if (j == i || d.x < minx[j] || d.x > maxx[j] || d.y < miny[j] || d.y > maxy[j]) {
|
||||
if (j == i || x < minx[j] || x > maxx[j] || y < miny[j] || y > maxy[j]) {
|
||||
continue;
|
||||
}
|
||||
size_t later = std::max(i, j), earlier = std::min(i, j);
|
||||
@@ -812,7 +907,7 @@ size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const
|
||||
if (std::find(o.begin(), o.end(), earlier) != o.end()) {
|
||||
continue;
|
||||
}
|
||||
if (strictly_inside(*geoms[j], d.x, d.y)) {
|
||||
if (strictly_inside(*geoms[j], pt.first, pt.second)) {
|
||||
o.push_back(earlier);
|
||||
}
|
||||
}
|
||||
@@ -880,6 +975,7 @@ size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const
|
||||
|
||||
if (done) {
|
||||
*geoms[i] = std::move(out);
|
||||
*geoms_in[i] = *geoms[i];
|
||||
changed++;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -64,6 +64,6 @@ drawvec clean_polygon_with_hot_pixels(drawvec const &geom, hot_pixel_list const
|
||||
// from all but the first of the polygons that cover it. Returns how many
|
||||
// polygons were changed.
|
||||
// `hot` is the hot pixels of the layer, from layer_hot_pixels(), if any.
|
||||
size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const *hot);
|
||||
size_t remove_overlaps(std::vector<drawvec *> const &geoms_in, hot_pixel_list const *hot);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2648,7 +2648,15 @@ long long write_tile(decompressor *geoms, std::atomic<long long> *geompos_in, ch
|
||||
layer_geoms.push_back(g.second);
|
||||
}
|
||||
auto hot = hot_pixels_by_layer.find(lp.first);
|
||||
remove_overlaps(layer_geoms, hot != hot_pixels_by_layer.end() ? &hot->second : NULL);
|
||||
// Removing an overlap between polygons whose edges cross (which
|
||||
// spindle strips can make them do) has to snap the new vertices
|
||||
// where they cross, which can leave a smaller overlap, so repeat
|
||||
// until there are none left, or a few times at most.
|
||||
for (int attempt = 0; attempt < 4; attempt++) {
|
||||
if (remove_overlaps(layer_geoms, hot != hot_pixels_by_layer.end() ? &hot->second : NULL) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -976,3 +976,46 @@ TEST_CASE("Removing overlaps doesn't snap the polygons again", "[wagyu]") {
|
||||
}
|
||||
REQUIRE(n == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("Overlaps are found where one polygon's edge runs past another's vertices", "[wagyu]") {
|
||||
auto ring = [](std::vector<std::pair<long long, long long>> pts) {
|
||||
drawvec g;
|
||||
for (size_t i = 0; i < pts.size(); i++) {
|
||||
g.push_back(draw(i == 0 ? VT_MOVETO : VT_LINETO, pts[i].first, pts[i].second));
|
||||
}
|
||||
g.push_back(draw(VT_LINETO, pts[0].first, pts[0].second));
|
||||
if (get_area(g, 0, g.size()) < 0) {
|
||||
std::reverse(g.begin(), g.end());
|
||||
g[0].op = VT_MOVETO;
|
||||
g[g.size() - 1].op = VT_LINETO;
|
||||
}
|
||||
return g;
|
||||
};
|
||||
|
||||
// A sliver whose long top edge runs along the top of a square, through its
|
||||
// corners but without vertices there, and which covers part of the square.
|
||||
// None of the vertices or edge midpoints of either is strictly inside the
|
||||
// other, and they share no edges, until the sliver's edge is split at the
|
||||
// square's corners.
|
||||
drawvec square = ring({{99, 235}, {100, 235}, {100, 236}, {99, 236}});
|
||||
drawvec sliver = ring({{93, 235}, {104, 235}, {93, 236}});
|
||||
std::vector<drawvec *> geoms = {&square, &sliver};
|
||||
|
||||
REQUIRE(remove_overlaps(geoms, NULL) == 1);
|
||||
REQUIRE(get_mp_area(square) == 1);
|
||||
|
||||
// No point is covered by both
|
||||
auto x4 = [](drawvec g) {
|
||||
for (auto &d : g) {
|
||||
d.x *= 4;
|
||||
d.y *= 4;
|
||||
}
|
||||
return g;
|
||||
};
|
||||
drawvec s4 = x4(square), l4 = x4(sliver);
|
||||
for (long long y = 235 * 4 + 1; y < 236 * 4; y += 2) {
|
||||
for (long long x = 93 * 4 + 1; x < 104 * 4; x += 2) {
|
||||
REQUIRE(pnpoly_mp(s4, x, y) + pnpoly_mp(l4, x, y) <= 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user