mirror of
https://github.com/felt/tippecanoe.git
synced 2026-12-05 06:41:41 +01:00
Fall back instead of exiting when wagyu fails on a spindle strip union
On arm64, clang contracts floating point multiply-adds into FMA instructions, which changes wagyu's intersection arithmetic enough that it can fail on some strip unions with "Trying to find intersection of lines that do not intersect" (seen with ne_10m_admin_0_countries at -D10 -d10 -zg; reproducible on x86 with -mfma -ffp-contract=fast, where 2 of 4426 unions failed). The strips are only an improvement on an already-valid cleaned polygon, so if the union fails at 16x resolution, retry it at tile resolution, and if that fails too, keep the cleaned polygon without strips. The re-cleaning of the union after rounding it back to tile resolution also no longer goes through clean_or_clip_poly(), which exits on failure, but does the same thing (16x if the result is exact, otherwise 1x) with the same fallbacks. Output where wagyu doesn't fail is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
This commit is contained in:
+61
-24
@@ -373,6 +373,42 @@ static void add_rings(mapbox::geometry::wagyu::wagyu<long long> &wagyu, drawvec
|
||||
}
|
||||
}
|
||||
|
||||
// Run wagyu on `subject` (positive fill) and optionally `clip` (non-zero fill),
|
||||
// at `scale` times tile resolution, and round the result back to tile resolution.
|
||||
// Returns false, rather than exiting, if wagyu fails, since the callers have
|
||||
// a valid polygon to fall back on.
|
||||
static bool wagyu_union(drawvec const &subject, drawvec const *clip, long long scale, drawvec &out, bool require_exact) {
|
||||
mapbox::geometry::multi_polygon<long long> result;
|
||||
|
||||
try {
|
||||
mapbox::geometry::wagyu::wagyu<long long> wagyu;
|
||||
add_rings(wagyu, subject, mapbox::geometry::wagyu::polygon_type_subject, scale);
|
||||
if (clip != NULL) {
|
||||
add_rings(wagyu, *clip, mapbox::geometry::wagyu::polygon_type_clip, scale);
|
||||
}
|
||||
wagyu.execute(mapbox::geometry::wagyu::clip_type_union, result, mapbox::geometry::wagyu::fill_type_positive, mapbox::geometry::wagyu::fill_type_non_zero);
|
||||
} catch (std::runtime_error &e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
out.clear();
|
||||
for (auto const &poly : result) {
|
||||
for (auto const &ring : poly) {
|
||||
for (size_t k = 0; k < ring.size(); k++) {
|
||||
if (require_exact && (ring[k].x % scale != 0 || ring[k].y % scale != 0)) {
|
||||
return false;
|
||||
}
|
||||
out.emplace_back(k == 0 ? VT_MOVETO : VT_LINETO, std::llround(ring[k].x / (double) scale), std::llround(ring[k].y / (double) scale));
|
||||
}
|
||||
if (ring.size() > 0 && (ring.front().x != ring.back().x || ring.front().y != ring.back().y)) {
|
||||
out.emplace_back(VT_LINETO, std::llround(ring.front().x / (double) scale), std::llround(ring.front().y / (double) scale));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
drawvec union_polygon_strips(drawvec const &geom, drawvec const &strips) {
|
||||
if (strips.size() == 0) {
|
||||
return geom;
|
||||
@@ -384,34 +420,35 @@ drawvec union_polygon_strips(drawvec const &geom, drawvec const &strips) {
|
||||
// would make the strips collapse in places, which is the very problem
|
||||
// they are trying to solve. So do the union at a higher resolution,
|
||||
// and then clean the result again at tile resolution.
|
||||
const long long scale = 16;
|
||||
|
||||
mapbox::geometry::multi_polygon<long long> result;
|
||||
mapbox::geometry::wagyu::wagyu<long long> wagyu;
|
||||
|
||||
//
|
||||
// The cleaned polygon uses the positive fill rule as usual, but the strips
|
||||
// use non-zero so that overlapping strips can never cancel each other out.
|
||||
add_rings(wagyu, geom, mapbox::geometry::wagyu::polygon_type_subject, scale);
|
||||
add_rings(wagyu, strips, mapbox::geometry::wagyu::polygon_type_clip, scale);
|
||||
//
|
||||
// Wagyu can fail on some of these unions ("Trying to find intersection
|
||||
// of lines that do not intersect"), depending on platform floating point
|
||||
// details like fused multiply-add. The strips are only an improvement on
|
||||
// an already-valid polygon, so if it fails, try again at tile resolution,
|
||||
// and if that fails too, give up on the strips for this feature.
|
||||
|
||||
try {
|
||||
wagyu.execute(mapbox::geometry::wagyu::clip_type_union, result, mapbox::geometry::wagyu::fill_type_positive, mapbox::geometry::wagyu::fill_type_non_zero);
|
||||
} catch (std::runtime_error &e) {
|
||||
fprintf(stderr, "Internal error: Polygon spindle union failed: %s\n", e.what());
|
||||
exit(EXIT_IMPOSSIBLE);
|
||||
}
|
||||
|
||||
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, std::llround(ring[k].x / (double) scale), std::llround(ring[k].y / (double) scale));
|
||||
}
|
||||
if (ring.size() > 0 && (ring.front().x != ring.back().x || ring.front().y != ring.back().y)) {
|
||||
out.emplace_back(VT_LINETO, std::llround(ring.front().x / (double) scale), std::llround(ring.front().y / (double) scale));
|
||||
}
|
||||
drawvec unioned;
|
||||
if (!wagyu_union(geom, &strips, 16, unioned, false)) {
|
||||
if (!wagyu_union(geom, &strips, 1, unioned, false)) {
|
||||
return geom;
|
||||
}
|
||||
return unioned;
|
||||
}
|
||||
|
||||
return clean_or_clip_poly(out, 0, 0, false, true);
|
||||
// Clean the rounded union at tile resolution, the same way
|
||||
// clean_or_clip_poly() would, but without exiting on failure.
|
||||
drawvec out;
|
||||
if (wagyu_union(unioned, NULL, 16, out, true)) {
|
||||
return out;
|
||||
}
|
||||
if (wagyu_union(unioned, NULL, 1, out, false)) {
|
||||
return out;
|
||||
}
|
||||
if (wagyu_union(geom, &strips, 1, out, false)) {
|
||||
return out;
|
||||
}
|
||||
return geom;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user