From 1fbc926d038ebfb449789bdb71ef4613952cef2b Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Fri, 27 Oct 2023 14:21:07 -0700 Subject: [PATCH] A little glitchy, but close --- earcut.cpp | 60 +++++++++++++++++----------------------------------- geometry.cpp | 15 +++++++++---- geometry.hpp | 2 +- serial.cpp | 2 +- 4 files changed, 32 insertions(+), 47 deletions(-) diff --git a/earcut.cpp b/earcut.cpp index ae577c70..277b3d7b 100644 --- a/earcut.cpp +++ b/earcut.cpp @@ -75,54 +75,33 @@ drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) { std::vector indices = mapbox::earcut(polygon); drawvec out2; - bool again = true; - while (again) { - again = false; - for (size_t i = 0; i + 2 < indices.size(); i += 3) { - std::vector lengths; + 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); + 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 dx = out[indices[v1]].x - out[indices[v2]].x; - double dy = out[indices[v1]].y - out[indices[v2]].y; - double d = sqrt(dx * dx + dy * dy); + long long px, py; - lengths.push_back(d); - } + 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) { + // 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)); - std::sort(lengths.begin(), lengths.end()); - printf("%f %f\n", lengths[2], lengths[0]); - - if (lengths[2] > 5 * lengths[0]) { - 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); - - if (std::llabs(std::llround(out[indices[v1]].x / scale) - std::llround(out[indices[v2]].x / scale)) < 2 && - std::llabs(std::llround(out[indices[v1]].y / scale) - std::llround(out[indices[v2]].y / scale)) < 2) { - double ang = atan2(out[indices[v2]].y - out[indices[v1]].y, out[indices[v2]].x - out[indices[v1]].x); - - double stretch = 1.5; - - drawvec tri; - tri.emplace_back(VT_MOVETO, (long long) out[indices[v3]].x, (long long) out[indices[v3]].y); - tri.emplace_back(VT_LINETO, out[indices[v1]].x - scale * cos(ang) * stretch, out[indices[v1]].y - scale * sin(ang) * stretch); - tri.emplace_back(VT_LINETO, out[indices[v2]].x + scale * cos(ang) * stretch, out[indices[v2]].y + scale * sin(ang) * stretch); - tri.emplace_back(VT_LINETO, (long long) out[indices[v3]].x, (long long) out[indices[v3]].y); - printf("%f\n", get_area(tri, 0, tri.size())); - - for (auto const &d : tri) { - out2.push_back(d); - } - } - } + 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, out[indices[v2]].x, out[indices[v2]].y)); } } } + // re-close the rings from which we removed the last points earlier + for (size_t i = 0; i < out.size(); i++) { if (out[i].op == VT_MOVETO) { size_t j; @@ -136,7 +115,6 @@ drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) { out2.push_back(out[k]); } - // re-close the ring out2.push_back(draw(VT_LINETO, out[i].x, out[i].y)); i = j - 1; } diff --git a/geometry.cpp b/geometry.cpp index 07ff1529..8a617ac5 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) { +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) { long long p2x = segB_x - segA_x; long long p2y = segB_y - segA_y; double something = p2x * p2x + p2y * p2y; @@ -349,6 +349,13 @@ double distance_from_line(long long point_x, long long point_y, long long segA_x double x = segA_x + u * p2x; double y = segA_y + u * p2y; + if (px != NULL) { + *px = std::round(x); + } + if (py != NULL) { + *py = std::round(y); + } + double dx = x - point_x; double dy = y - point_y; @@ -400,7 +407,7 @@ static void douglas_peucker(drawvec &geom, int start, int n, double e, size_t ke if (geom[start + first] < geom[start + second]) { farthest_element_index = first; for (i = first + 1; i < second; i++) { - double temp_dist = distance_from_line(geom[start + i].x, geom[start + i].y, geom[start + first].x, geom[start + first].y, geom[start + second].x, geom[start + second].y); + double temp_dist = distance_from_line(geom[start + i].x, geom[start + i].y, geom[start + first].x, geom[start + first].y, geom[start + second].x, geom[start + second].y, NULL, NULL); double distance = std::fabs(temp_dist); @@ -412,7 +419,7 @@ static void douglas_peucker(drawvec &geom, int start, int n, double e, size_t ke } else { farthest_element_index = second; for (i = second - 1; i > first; i--) { - double temp_dist = distance_from_line(geom[start + i].x, geom[start + i].y, geom[start + second].x, geom[start + second].y, geom[start + first].x, geom[start + first].y); + double temp_dist = distance_from_line(geom[start + i].x, geom[start + i].y, geom[start + second].x, geom[start + second].y, geom[start + first].x, geom[start + first].y, NULL, NULL); double distance = std::fabs(temp_dist); @@ -1046,7 +1053,7 @@ double label_goodness(const drawvec &dv, long long x, long long y) { } if (i > 0 && dv[i].op == VT_LINETO) { - dist = distance_from_line(x, y, dv[i - 1].x, dv[i - 1].y, dv[i].x, dv[i].y); + dist = distance_from_line(x, y, dv[i - 1].x, dv[i - 1].y, dv[i].x, dv[i].y, NULL, NULL); if (dist < closest) { closest = dist; } diff --git a/geometry.hpp b/geometry.hpp index 41d86ee3..15aa7bd1 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); +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); 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/serial.cpp b/serial.cpp index a40cc462..c02df19f 100644 --- a/serial.cpp +++ b/serial.cpp @@ -586,7 +586,7 @@ int serialize_feature(struct serialization_state *sst, serial_feature &sf) { for (size_t k = i + 1; k < j - 1; k++) { double d = distance_from_line(scaled_geometry[k].x, scaled_geometry[k].y, scaled_geometry[i].x, scaled_geometry[i].y, - scaled_geometry[which].x, scaled_geometry[which].y); + scaled_geometry[which].x, scaled_geometry[which].y, NULL, NULL); if ((d > far) || ((d == far) && (scaled_geometry[k] < scaled_geometry[which2]))) { far = d;