Skip to content

Commit

Permalink
Factor out search for shared nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Oct 20, 2023
1 parent e1c665b commit a1ce734
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *still_needs_sim
// OR it is an inner ring and we haven't output an outer ring for it to be
// cut out of, so we are just subtracting its area from the tiny polygon
// rather than trying to deal with it geometrically
if ((area > 0 && area <= pixel * pixel) || (area < 0 && !included_last_outer)) {
if (((area > 0 && area <= pixel * pixel) || (area < 0 && !included_last_outer))) {
*accum_area += area;
*reduced_away = true;

Expand Down Expand Up @@ -485,6 +485,39 @@ drawvec impose_tile_boundaries(drawvec &geom, long long extent) {
return out;
}

bool is_shared_node(draw d, int z, int tx, int ty, drawvec const &shared_nodes, struct node *shared_nodes_map, size_t nodepos) {
// This is kind of weird, because we have two lists of shared nodes to look through:
// * the drawvec, which is nodes that were introduced during clipping to the tile edge,
// and which are in local tile coordinates
// * the shared_nodes_map, which was made globally before tiling began, and which
// is in global quadkey coordinates.
// To look through the latter, we need to offset and encode the coordinates
// of the feature we are simplifying.

auto pt = std::lower_bound(shared_nodes.begin(), shared_nodes.end(), d);
if (pt != shared_nodes.end() && *pt == d) {
return true;
}

if (nodepos > 0) {
// offset to global
if (z != 0) {
d.x += tx * (1LL << (32 - z));
d.y += ty * (1LL << (32 - z));
}

// to quadkey
struct node n;
n.index = encode_quadkey((unsigned) d.x, (unsigned) d.y);

if (bsearch(&n, shared_nodes_map, nodepos / sizeof(node), sizeof(node), nodecmp) != NULL) {
return true;
}
}

return false;
}

drawvec simplify_lines(drawvec &geom, int z, int tx, int ty, int detail, bool mark_tile_bounds, double simplification, size_t retain, drawvec const &shared_nodes, struct node *shared_nodes_map, size_t nodepos) {
int res = 1 << (32 - detail - z);
long long area = 1LL << (32 - z);
Expand All @@ -501,35 +534,9 @@ drawvec simplify_lines(drawvec &geom, int z, int tx, int ty, int detail, bool ma
}

if (prevent[P_SIMPLIFY_SHARED_NODES]) {
// This is kind of weird, because we have two lists of shared nodes to look through:
// * the drawvec, which is nodes that were introduced during clipping to the tile edge,
// and which are in local tile coordinates
// * the shared_nodes_map, which was made globally before tiling began, and which
// is in global quadkey coordinates.
// To look through the latter, we need to offset and encode the coordinates
// of the feature we are simplifying.

auto pt = std::lower_bound(shared_nodes.begin(), shared_nodes.end(), geom[i]);
if (pt != shared_nodes.end() && *pt == geom[i]) {
if (is_shared_node(geom[i], z, tx, ty, shared_nodes, shared_nodes_map, nodepos)) {
geom[i].necessary = true;
}

if (nodepos > 0) {
// offset to global
draw d = geom[i];
if (z != 0) {
d.x += tx * (1LL << (32 - z));
d.y += ty * (1LL << (32 - z));
}

// to quadkey
struct node n;
n.index = encode_quadkey((unsigned) d.x, (unsigned) d.y);

if (bsearch(&n, shared_nodes_map, nodepos / sizeof(node), sizeof(node), nodecmp) != NULL) {
geom[i].necessary = true;
}
}
}
}

Expand Down

0 comments on commit a1ce734

Please sign in to comment.