From ebcc8bf799c761e8a462342187d8804d0d504405 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Mon, 10 Feb 2025 13:02:22 -0800 Subject: [PATCH] Progress toward eliminating decodes of feature indices --- geometry.cpp | 6 ++---- geometry.hpp | 2 +- main.cpp | 1 + serial.cpp | 26 ++++++++++++++++++++------ serial.hpp | 3 ++- tile.cpp | 6 ++++-- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/geometry.cpp b/geometry.cpp index 58d4f163..324fa7cd 100644 --- a/geometry.cpp +++ b/geometry.cpp @@ -944,12 +944,10 @@ drawvec polygon_to_anchor(const drawvec &geom) { return drawvec(); } -drawvec checkerboard_anchors(drawvec const &geom, int tx, int ty, int z, unsigned long long label_point) { +drawvec checkerboard_anchors(drawvec const &geom, int tx, int ty, int z, unsigned wx, unsigned wy) { drawvec out; - // anchor point in world coordinates - unsigned wx, wy; - decode_index(label_point, &wx, &wy); + // wx,wy is anchor point in world coordinates // upper left of tile in world coordinates long long tx1 = 0, ty1 = 0; diff --git a/geometry.hpp b/geometry.hpp index db96a2cd..a8c2283f 100644 --- a/geometry.hpp +++ b/geometry.hpp @@ -89,7 +89,7 @@ void check_polygon(drawvec &geom); double get_area(const drawvec &geom, size_t i, size_t j); double get_mp_area(drawvec &geom); drawvec polygon_to_anchor(const drawvec &geom); -drawvec checkerboard_anchors(drawvec const &geom, int tx, int ty, int z, unsigned long long label_point); +drawvec checkerboard_anchors(drawvec const &geom, int tx, int ty, int z, unsigned label_x, unsigned label_y); drawvec simple_clip_poly(drawvec &geom, int z, int buffer, drawvec &shared_nodes, bool prevent_simplify_shared_nodes); drawvec simple_clip_poly(drawvec &geom, long long x1, long long y1, long long x2, long long y2, bool prevent_simplify_shared_nodes); diff --git a/main.cpp b/main.cpp index 531bdd86..0446c098 100644 --- a/main.cpp +++ b/main.cpp @@ -2499,6 +2499,7 @@ std::pair read_input(std::vector &sources, char *fname, i long long ip; for (ip = 0; ip < indices; ip++) { unsigned xx, yy; + // XXX this will behave oddly if the index is actually H3 decode_index(map[ip].ix, &xx, &yy); long long nprogress = 100 * ip / indices; diff --git a/serial.cpp b/serial.cpp index cd779024..5cbafec9 100644 --- a/serial.cpp +++ b/serial.cpp @@ -186,7 +186,7 @@ std::string serialize_feature(serial_feature *sf, long long wx, long long wy) { long long layer = 0; layer |= sf->layer << FLAG_LAYER; - layer |= (sf->label_point != 0) << FLAG_LABEL_POINT; + layer |= ((sf->label_x | sf->label_y) != 0) << FLAG_LABEL_POINT; layer |= (sf->index != 0) << FLAG_INDEX; layer |= (sf->extent != 0) << FLAG_EXTENT; layer |= sf->has_id << FLAG_ID; @@ -211,10 +211,13 @@ std::string serialize_feature(serial_feature *sf, long long wx, long long wy) { if (sf->index != 0) { serialize_ulong_long(s, sf->index); + serialize_ulong_long(s, sf->wx); + serialize_ulong_long(s, sf->wy); serialize_ulong_long(s, sf->gap); } - if (sf->label_point != 0) { - serialize_ulong_long(s, sf->label_point); + if ((sf->label_x | sf->label_y) != 0) { + serialize_ulong_long(s, sf->label_x); + serialize_ulong_long(s, sf->label_y); } if (sf->extent != 0) { serialize_long_long(s, sf->extent); @@ -261,17 +264,27 @@ serial_feature deserialize_feature(std::string const &geoms, unsigned z, unsigne sf.index = 0; sf.gap = 0; - sf.label_point = 0; + sf.label_x = 0; + sf.label_y = 0; sf.extent = 0; sf.geometry = decode_geometry(&cp, z, tx, ty, sf.bbox, initial_x[sf.segment], initial_y[sf.segment]); if (sf.layer & (1 << FLAG_INDEX)) { + unsigned long long wx, wy; deserialize_ulong_long(&cp, &sf.index); + deserialize_ulong_long(&cp, &wx); + deserialize_ulong_long(&cp, &wy); + sf.wx = wx; + sf.wy = wy; deserialize_ulong_long(&cp, &sf.gap); } if (sf.layer & (1 << FLAG_LABEL_POINT)) { - deserialize_ulong_long(&cp, &sf.label_point); + unsigned long long wx, wy; + deserialize_ulong_long(&cp, &wx); + deserialize_ulong_long(&cp, &wy); + sf.label_x = wx; + sf.label_y = wy; } if (sf.layer & (1 << FLAG_EXTENT)) { deserialize_long_long(&cp, &sf.extent); @@ -741,7 +754,8 @@ int serialize_feature(struct serialization_state *sst, serial_feature &sf, std:: if (dv.size() > 0) { dv[0].x = SHIFT_LEFT(dv[0].x) & ((1LL << 32) - 1); dv[0].y = SHIFT_LEFT(dv[0].y) & ((1LL << 32) - 1); - sf.label_point = encode_index(dv[0].x, dv[0].y); + sf.label_x = dv[0].x; + sf.label_y = dv[0].y; } } diff --git a/serial.hpp b/serial.hpp index a351f21d..80f83e91 100644 --- a/serial.hpp +++ b/serial.hpp @@ -118,8 +118,9 @@ struct serial_feature { drawvec geometry = drawvec(); unsigned long long index = 0; + unsigned wx = 0, wy = 0; unsigned long long gap = 0; // filled in during z0. square of planar distance - unsigned long long label_point = 0; + unsigned label_x = 0, label_y = 0; long long extent = 0; // These fields are not directly serialized, but are used diff --git a/tile.cpp b/tile.cpp index 40ef416c..1d531e58 100644 --- a/tile.cpp +++ b/tile.cpp @@ -714,7 +714,7 @@ static void *simplification_worker(void *v) { if (t == VT_POLYGON && additional[A_GENERATE_POLYGON_LABEL_POINTS]) { t = (*features)[i]->t = VT_POINT; - geom = checkerboard_anchors(from_tile_scale(geom, z, out_detail), (*features)[i]->tx, (*features)[i]->ty, z, (*features)[i]->label_point); + geom = checkerboard_anchors(from_tile_scale(geom, z, out_detail), (*features)[i]->tx, (*features)[i]->ty, z, (*features)[i]->label_x, (*features)[i]->label_y); to_tile_scale(geom, z, out_detail); } @@ -1131,6 +1131,8 @@ static serial_feature next_feature(decompressor *geoms, std::atomic * unsigned wx1, wy1; decode_index(next_feature_state.previndex, &wx1, &wy1); + // find the furthest distance of a vertex in this feature + // from the representative point of the previous feature for (auto const &g : sf.geometry) { long long dx = (long long) wx1 - (g.x + ox); long long dy = (long long) wy1 - (g.y + oy); @@ -2819,7 +2821,7 @@ long long write_tile(decompressor *geoms, std::atomic *geompos_in, ch } if (skipped > 0 || too_many_bytes || too_many_features) { - fprintf(stderr, "Can't happen: writing tile even though we skipped\n"); + fprintf(stderr, "Can't happen: writing tile even though we skipped (%zu %d %d)\n", skipped, too_many_bytes, too_many_features); exit(EXIT_IMPOSSIBLE); }