diff --git a/polygon.cpp b/polygon.cpp index 7bc2ed2a..cc3063eb 100644 --- a/polygon.cpp +++ b/polygon.cpp @@ -373,6 +373,42 @@ static void add_rings(mapbox::geometry::wagyu::wagyu &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 result; + + try { + mapbox::geometry::wagyu::wagyu 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 result; - mapbox::geometry::wagyu::wagyu 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; }