From 0c4b5d089bbc4958f9d7aa32887cd89ae62b8b42 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Fri, 27 Oct 2023 14:47:39 -0700 Subject: [PATCH] Does floating point help? --- earcut.cpp | 49 +++++-------------------------------------------- geometry.cpp | 2 +- geometry.hpp | 2 +- tile.cpp | 7 ------- 4 files changed, 7 insertions(+), 53 deletions(-) diff --git a/earcut.cpp b/earcut.cpp index 277b3d7b..8ba8cf82 100644 --- a/earcut.cpp +++ b/earcut.cpp @@ -5,45 +5,6 @@ using Coord = long long; using N = size_t; using Point = std::array; -// Return false if the requested adjustment would give -// any of the polygons that contain vertex N a negative area -bool can_adjust(drawvec const &dv, std::vector const &indices, size_t n, double *dx, double *dy) { - printf("looking for %zu\n", n); - - bool again = true; - while (again) { - again = false; - for (size_t i = 0; i + 2 < indices.size(); i += 3) { - for (size_t j = 0; j < 3; j++) { - if (indices[i + j] == indices[n]) { - drawvec tri; - for (size_t k = 0; k < 3; k++) { - printf("found %zu %lld,%lld\n", i + j, dv[indices[i + k]].x, dv[indices[i + k]].y); - - tri.push_back(dv[indices[i + k]]); - tri[k].op = VT_LINETO; - - if (indices[i + k] == indices[n]) { - tri[k].x += *dx; - tri[k].y += *dy; - } - } - tri.push_back(tri[0]); - tri[0].op = VT_MOVETO; - printf("area %f\n", get_area(tri, 0, tri.size())); - if (get_area(tri, 0, tri.size()) < 0) { - *dx /= 2; - *dy /= 2; - again = true; - } - } - } - } - } - - return true; -} - drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) { std::vector> polygon; drawvec out; @@ -83,18 +44,18 @@ drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) { size_t v2 = i + ((j + 1) % 3); size_t v3 = i + ((j + 2) % 3); - long long px, py; + double px, py; if (distance_from_line(out[indices[v1]].x, out[indices[v1]].y, // the point out[indices[v2]].x, out[indices[v2]].y, // start of opposite side out[indices[v3]].x, out[indices[v3]].y, // end of opposite side - &px, &py) < scale) { + &px, &py) < 2 * scale) { + double ang = atan2(out[indices[v1]].y - py, out[indices[v1]].x - px); + // make a new triangle that is not so flat out2.push_back(draw(VT_MOVETO, out[indices[v2]].x, out[indices[v2]].y)); out2.push_back(draw(VT_LINETO, out[indices[v3]].x, out[indices[v3]].y)); - - double ang = atan2(out[indices[v1]].y - py, out[indices[v1]].x - px); - out2.push_back(draw(VT_LINETO, out[indices[v1]].x + scale * cos(ang), out[indices[v1]].y + scale * sin(ang))); + out2.push_back(draw(VT_LINETO, px + 2 * scale * cos(ang), py + 2 * scale * sin(ang))); out2.push_back(draw(VT_LINETO, out[indices[v2]].x, out[indices[v2]].y)); } } diff --git a/geometry.cpp b/geometry.cpp index 8a617ac5..117a986e 100644 --- a/geometry.cpp +++ b/geometry.cpp @@ -334,7 +334,7 @@ bool point_within_tile(long long x, long long y, int z) { return x >= 0 && y >= 0 && x < area && y < area; } -double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y, long long *px, long long *py) { +double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y, double *px, double *py) { long long p2x = segB_x - segA_x; long long p2y = segB_y - segA_y; double something = p2x * p2x + p2y * p2y; diff --git a/geometry.hpp b/geometry.hpp index 15aa7bd1..2974bb81 100644 --- a/geometry.hpp +++ b/geometry.hpp @@ -96,7 +96,7 @@ drawvec clip_lines(drawvec &geom, long long x1, long long y1, long long x2, long drawvec clip_point(drawvec &geom, long long x1, long long y1, long long x2, long long y2); void visvalingam(drawvec &ls, size_t start, size_t end, double threshold, size_t retain); int pnpoly(const drawvec &vert, size_t start, size_t nvert, long long testx, long long testy); -double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y, long long *px, long long *py); +double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y, double *px, double *py); std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int ny, int detail, int buffer, std::set const &keep, bool do_compress); diff --git a/tile.cpp b/tile.cpp index 924d7564..f9f74e6b 100644 --- a/tile.cpp +++ b/tile.cpp @@ -613,12 +613,6 @@ void *partial_feature_worker(void *v) { drawvec geom = (*partials)[i].geoms[0]; - if (t == VT_POLYGON) { - geom = clean_or_clip_poly(geom, 0, 0, false, false); - geom = fix_by_triangulation(geom, z, out_detail); - } - -#if 0 if (t == VT_POLYGON) { drawvec cleaned = clean_or_clip_poly(geom, 0, 0, false, false); drawvec fixed = fix_by_triangulation(geom, z, out_detail); @@ -626,7 +620,6 @@ void *partial_feature_worker(void *v) { geom = fixed; } } -#endif to_tile_scale(geom, z, out_detail);