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:
+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++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user