#include "geometry.hpp" #include "mapbox/geometry/earcut.hpp" using Coord = long long; using N = size_t; using Point = std::array; drawvec reinforce(drawvec const &pts, std::vector> polygon, double scale) { std::vector indices = mapbox::earcut(polygon); drawvec out2; for (size_t i = 0; i + 2 < indices.size(); i += 3) { std::vector lengths; for (size_t j = 0; j < 3; j++) { size_t v1 = i + j; size_t v2 = i + ((j + 1) % 3); size_t v3 = i + ((j + 2) % 3); double px, py; if (distance_from_line(pts[indices[v1]].x, pts[indices[v1]].y, // the point pts[indices[v2]].x, pts[indices[v2]].y, // start of opposite side pts[indices[v3]].x, pts[indices[v3]].y, // end of opposite side &px, &py) < 2 * scale) { double ang = atan2(pts[indices[v1]].y - py, pts[indices[v1]].x - px); // make a new triangle that is not so flat out2.push_back(draw(VT_MOVETO, pts[indices[v2]].x, pts[indices[v2]].y)); out2.push_back(draw(VT_LINETO, pts[indices[v3]].x, pts[indices[v3]].y)); out2.push_back(draw(VT_LINETO, px + scale * 2 * cos(ang), py + scale * 2 * sin(ang))); out2.push_back(draw(VT_LINETO, pts[indices[v2]].x, pts[indices[v2]].y)); } #if 0 long long dx = pts[indices[v2]].x - pts[indices[v1]].x; long long dy = pts[indices[v2]].y - pts[indices[v1]].y; double d = sqrt(dx * dx + dy * dy); if (d < sqrt(2) * scale) { // make a new triangle with a longer side double ang = atan2(dy, dx); out2.push_back(draw(VT_MOVETO, pts[indices[v3]].x, pts[indices[v3]].y)); out2.push_back(draw(VT_LINETO, pts[indices[v1]].x - sqrt(2) * scale * cos(ang), pts[indices[v1]].y - sqrt(2) * scale * sin(ang))); out2.push_back(draw(VT_LINETO, pts[indices[v2]].x + sqrt(2) * scale * cos(ang), pts[indices[v2]].y + sqrt(2) * scale * sin(ang))); out2.push_back(draw(VT_LINETO, pts[indices[v3]].x, pts[indices[v3]].y)); } #endif } } return out2; } drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) { std::vector> polygon; drawvec out, out2; double scale = 1LL << (32 - z - detail); for (size_t i = 0; i < dv.size(); i++) { if (dv[i].op == VT_MOVETO) { size_t j; for (j = i + 1; j < dv.size(); j++) { if (dv[j].op != VT_LINETO) { break; } } if (get_area(dv, i, j) > 0) { // outer ring, so process whatever we have so far drawvec additional = reinforce(out, polygon, scale); for (auto const &d : additional) { out2.push_back(d); } polygon.clear(); out.clear(); } std::vector ring; // j - 1 because earcut docs indicate that it doesn't expect // a duplicate last point in each ring for (size_t k = i; k < j - 1; k++) { Point p = {(long long) dv[k].x, (long long) dv[k].y}; ring.push_back(p); out.push_back(dv[k]); } polygon.push_back(ring); i = j - 1; } } drawvec additional = reinforce(out, polygon, scale); for (auto const &d : additional) { out2.push_back(d); } for (auto const &d : dv) { out2.push_back(d); } return out2; }