mirror of
https://github.com/felt/tippecanoe.git
synced 2026-12-05 06:41:41 +01:00
Don't snap polygons a second time when removing overlaps
Removing the area that a polygon shares with an earlier polygon ran wagyu on geometry that had already been snapped to tile coordinates, and snapping it again could change it. In Dane County tile 8/64/93, consistent snapping bent one block's edge into a 45-degree diagonal that left it a half-pixel triangle, and when that block's overlap with another block was removed, the diagonal passed through the corner of the hot pixel of the vertex beside it, so it was snapped again and the triangle collapsed, leaving a hole between it and its neighbor. Elsewhere in the same tile, two half-pixel overlaps were left behind the same way. The polygons' edges no longer cross after consistent snapping, so the difference needs no new vertices. So compute it at 16 times the tile resolution, where the vertices are far from each other's hot pixels, and use the result if all of its vertices are the original ones. Only if they aren't, fall back to snapping at tile resolution to the layer's hot pixels. For Dane County at -D8 -d8 -pt -pn -zg, this reduces the overlapping pixels at z8, z9, and z10 from 7, 8, and 16 to 5, 3, and 3, and removes the hole and both overlaps in 8/64/93 at -z8. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
This commit is contained in:
+50
-29
@@ -826,41 +826,62 @@ size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const
|
||||
continue;
|
||||
}
|
||||
|
||||
mapbox::geometry::multi_polygon<long long> result;
|
||||
try {
|
||||
mapbox::geometry::wagyu::wagyu<long long> wagyu;
|
||||
add_rings(wagyu, *geoms[i], mapbox::geometry::wagyu::polygon_type_subject, 1);
|
||||
drawvec all = *geoms[i];
|
||||
for (size_t earlier : overlaps[i]) {
|
||||
add_rings(wagyu, *geoms[earlier], mapbox::geometry::wagyu::polygon_type_clip, 1);
|
||||
all.insert(all.end(), geoms[earlier]->begin(), geoms[earlier]->end());
|
||||
// The polygons have already been snapped, and their edges don't cross,
|
||||
// so the difference doesn't need any new vertices. Snapping them again at
|
||||
// tile resolution could still bend their edges (for example, a 45-degree
|
||||
// edge through the corner of the hot pixel of the vertex beside it) and
|
||||
// collapse parts of them, so do it at a higher resolution, where the
|
||||
// vertices are far from each other's hot pixels, and use the result if
|
||||
// it does only have the original vertices. Otherwise, snap at tile
|
||||
// resolution to the layer's hot pixels, consistently with the rest.
|
||||
drawvec out;
|
||||
bool done = false;
|
||||
for (long long scale : {16LL, 1LL}) {
|
||||
mapbox::geometry::multi_polygon<long long> result;
|
||||
try {
|
||||
mapbox::geometry::wagyu::wagyu<long long> wagyu;
|
||||
add_rings(wagyu, *geoms[i], mapbox::geometry::wagyu::polygon_type_subject, scale);
|
||||
drawvec all = *geoms[i];
|
||||
for (size_t earlier : overlaps[i]) {
|
||||
add_rings(wagyu, *geoms[earlier], mapbox::geometry::wagyu::polygon_type_clip, scale);
|
||||
all.insert(all.end(), geoms[earlier]->begin(), geoms[earlier]->end());
|
||||
}
|
||||
if (scale == 1 && hot != NULL) {
|
||||
std::vector<mapbox::geometry::point<long long>> near = hot_pixels_near(all, *hot);
|
||||
if (near.size() > 0) {
|
||||
wagyu.add_hot_pixels(near);
|
||||
}
|
||||
}
|
||||
wagyu.execute(mapbox::geometry::wagyu::clip_type_difference, result, mapbox::geometry::wagyu::fill_type_positive, mapbox::geometry::wagyu::fill_type_positive);
|
||||
} catch (std::runtime_error &e) {
|
||||
continue;
|
||||
}
|
||||
// Snap the same way as the polygons were cleaned, so the result
|
||||
// stays consistent with them.
|
||||
if (hot != NULL) {
|
||||
std::vector<mapbox::geometry::point<long long>> near = hot_pixels_near(all, *hot);
|
||||
if (near.size() > 0) {
|
||||
wagyu.add_hot_pixels(near);
|
||||
|
||||
out.clear();
|
||||
bool exact = true;
|
||||
for (auto const &poly : result) {
|
||||
for (auto const &ring : poly) {
|
||||
for (size_t k = 0; k < ring.size(); k++) {
|
||||
if (ring[k].x % scale != 0 || ring[k].y % scale != 0) {
|
||||
exact = false;
|
||||
}
|
||||
out.emplace_back(k == 0 ? VT_MOVETO : VT_LINETO, ring[k].x / scale, ring[k].y / scale);
|
||||
}
|
||||
if (ring.size() > 0 && (ring.front().x != ring.back().x || ring.front().y != ring.back().y)) {
|
||||
out.emplace_back(VT_LINETO, ring.front().x / scale, ring.front().y / scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
wagyu.execute(mapbox::geometry::wagyu::clip_type_difference, result, mapbox::geometry::wagyu::fill_type_positive, mapbox::geometry::wagyu::fill_type_positive);
|
||||
} catch (std::runtime_error &e) {
|
||||
continue;
|
||||
if (exact) {
|
||||
done = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
drawvec out;
|
||||
for (auto const &poly : result) {
|
||||
for (auto const &ring : poly) {
|
||||
for (size_t k = 0; k < ring.size(); k++) {
|
||||
out.emplace_back(k == 0 ? VT_MOVETO : VT_LINETO, ring[k].x, ring[k].y);
|
||||
}
|
||||
if (ring.size() > 0 && (ring.front().x != ring.back().x || ring.front().y != ring.back().y)) {
|
||||
out.emplace_back(VT_LINETO, ring.front().x, ring.front().y);
|
||||
}
|
||||
}
|
||||
if (done) {
|
||||
*geoms[i] = std::move(out);
|
||||
changed++;
|
||||
}
|
||||
*geoms[i] = std::move(out);
|
||||
changed++;
|
||||
}
|
||||
|
||||
return changed;
|
||||
|
||||
Reference in New Issue
Block a user