Skip to content

Commit

Permalink
By George, I think she's got it!
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Dec 7, 2023
1 parent 80c84eb commit 9ae1e57
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
48 changes: 21 additions & 27 deletions earcut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,28 @@ drawvec reinforce(drawvec const &pts, std::vector<std::vector<Point>> polygon, d
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));
}
out2.push_back(draw(VT_MOVETO, pts[indices[v1]].x, pts[indices[v1]].y));
out2.push_back(draw(VT_LINETO, 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, pts[indices[v1]].x, pts[indices[v1]].y));
#endif

double px, py;
double d = distance_from_line_noclamp(pts[indices[v1]].x, pts[indices[v1]].y,
pts[indices[v2]].x, pts[indices[v2]].y,
pts[indices[v3]].x, pts[indices[v3]].y,
&px, &py);

if (d < scale) {
double ang = atan2(py - pts[indices[v1]].y, px - pts[indices[v1]].x);
double dist = scale - d;

out2.push_back(draw(VT_MOVETO, pts[indices[v1]].x, pts[indices[v1]].y));
out2.push_back(draw(VT_LINETO, pts[indices[v2]].x + dist * cos(ang), pts[indices[v2]].y + dist * sin(ang)));
out2.push_back(draw(VT_LINETO, pts[indices[v3]].x + dist * cos(ang), pts[indices[v3]].y + dist * sin(ang)));
out2.push_back(draw(VT_LINETO, pts[indices[v1]].x, pts[indices[v1]].y));
}
}
}

Expand Down Expand Up @@ -95,6 +88,7 @@ drawvec fix_by_triangulation(drawvec const &dv, int z, int detail) {
for (auto const &d : additional) {
out2.push_back(d);
}

for (auto const &d : dv) {
out2.push_back(d);
}
Expand Down
23 changes: 23 additions & 0 deletions geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,29 @@ double distance_from_line(long long point_x, long long point_y, long long segA_x
return out;
}

double distance_from_line_noclamp(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;
double u = (0 == something) ? 0 : ((point_x - segA_x) * p2x + (point_y - segA_y) * p2y) / (something);

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;

double out = std::round(sqrt(dx * dx + dy * dy) * 16.0) / 16.0;
return out;
}

// https://github.com/Project-OSRM/osrm-backend/blob/733d1384a40f/Algorithms/DouglasePeucker.cpp
static void douglas_peucker(drawvec &geom, int start, int n, double e, size_t kept, size_t retain) {
std::stack<int> recursion_stack;
Expand Down
1 change: 1 addition & 0 deletions geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ drawvec clip_point(drawvec &geom, long long x1, long long y1, long long x2, long
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 *px, double *py);
double distance_from_line_noclamp(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<std::string> const &keep, bool do_compress,
Expand Down
7 changes: 6 additions & 1 deletion tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,16 @@ void *partial_feature_worker(void *v) {
drawvec geom = (*partials)[i].geoms[0];

if (t == VT_POLYGON) {
geom = remove_noop(geom, VT_POLYGON, 0);
drawvec cleaned = clean_or_clip_poly(geom, 0, 0, false, false);
drawvec fixed = fix_by_triangulation(geom, z, out_detail);
drawvec fixed = fix_by_triangulation(cleaned, z, out_detail);
geom = clean_or_clip_poly(fixed, 0, 0, false, false);
#if 0
if (fixed != cleaned) {
geom = fixed;
}
(*partials)[i].t = t = VT_LINE;
#endif
}

to_tile_scale(geom, z, out_detail);
Expand Down

0 comments on commit 9ae1e57

Please sign in to comment.