From c062c4a240a87488dbab48d6f49da0a407442ef6 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 21:38:40 +0000 Subject: [PATCH] Identify features for shared-edge detection by layer, segment, and seq Features don't necessarily have IDs, and sequence numbers are only unique within a layer and a reading segment: the flatgeobuf, geobuf, and CSV readers start every segment's sequence at the same offset and parse in parallel, so two different features can have the same seq. Record all three in each vertex record, and only count an edge as shared if it is part of polygons that differ in any of them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ --- main.cpp | 20 ++++++++++---------- serial.cpp | 2 +- serial.hpp | 39 +++++++++++++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/main.cpp b/main.cpp index 49104e97..9d7782c9 100644 --- a/main.cpp +++ b/main.cpp @@ -1231,10 +1231,10 @@ int vertexcmp(const void *void1, const void *void2) { return 1; } - if (v1->feature < v2->feature) { + if (v1->feature_less(*v2)) { return -1; } - if (v1->feature > v2->feature) { + if (v2->feature_less(*v1)) { return 1; } @@ -2070,26 +2070,26 @@ std::pair read_input(std::vector &sources, char *fname, i // All the vertices with the same central point, which are adjacent after sorting std::vector group; - // The polygon neighbors of the central point, and which feature each is from - std::vector> neighbors; + // The polygon neighbors of the central point, and which feature (vertex) each is from + std::vector> neighbors; auto finish_group = [&]() { // A neighbor that is connected to the central point by more than one // polygon feature is the other end of a shared edge neighbors.clear(); for (auto const &g : group) { - if (g.feature >= 0) { - neighbors.emplace_back(g.p1, g.feature); + if (g.is_polygon()) { + neighbors.emplace_back(g.p1, &g); if (g.p2 != g.p1) { - neighbors.emplace_back(g.p2, g.feature); + neighbors.emplace_back(g.p2, &g); } } } - std::sort(neighbors.begin(), neighbors.end(), [](std::pair const &a, std::pair const &b) { + std::sort(neighbors.begin(), neighbors.end(), [](std::pair const &a, std::pair const &b) { if (a.first < b.first) { return true; } else if (a.first == b.first) { - return a.second < b.second; + return a.second->feature_less(*b.second); } return false; }); @@ -2097,7 +2097,7 @@ std::pair read_input(std::vector &sources, char *fname, i size_t l; bool multiple = false; for (l = k + 1; l < neighbors.size() && neighbors[l].first == neighbors[k].first; l++) { - if (neighbors[l].second != neighbors[k].second) { + if (!neighbors[l].second->same_feature(*neighbors[k].second)) { multiple = true; } } diff --git a/serial.cpp b/serial.cpp index 58d7cb5a..4376ef34 100644 --- a/serial.cpp +++ b/serial.cpp @@ -526,7 +526,7 @@ int serialize_feature(struct serialization_state *sst, serial_feature &sf, std:: scaled_geometry[(k - i + 0) % (j - i - 1) + i], scaled_geometry[(k - i + 1) % (j - i - 1) + i], scaled_geometry[(k - i + 2) % (j - i - 1) + i], - *(sst->layer_seq)); + *(sst->layer_seq), sf.layer, sf.segment); fwrite_check((char *) &v, sizeof(struct vertex), 1, r->vertexfile, &r->vertexpos, sst->fname); } diff --git a/serial.hpp b/serial.hpp index 2e8b39cf..3adf307d 100644 --- a/serial.hpp +++ b/serial.hpp @@ -284,12 +284,19 @@ struct vertex { draw mid; draw p2; - // Which polygon feature this vertex is part of, or -1 if it is part of a line, - // so that edges shared between different polygon features can be identified - long long feature = -1; + // Which polygon feature this vertex is part of, so that edges shared + // between different polygon features can be identified. Features do not + // necessarily have IDs, and sequence numbers are only unique within a layer + // and a reading segment, so a feature is identified by all three. + // `seq` is -1 if the vertex is part of a line rather than a polygon. + long long seq = -1; + int layer = -1; + int segment = -1; - vertex(draw one, draw joint, draw two, long long feature_ = -1) { - feature = feature_; + vertex(draw one, draw joint, draw two, long long seq_ = -1, int layer_ = -1, int segment_ = -1) { + seq = seq_; + layer = layer_; + segment = segment_; if (one < two) { p1 = one; @@ -302,6 +309,24 @@ struct vertex { mid = joint; } + bool is_polygon() const { + return seq >= 0; + } + + bool same_feature(const vertex &v) const { + return seq == v.seq && layer == v.layer && segment == v.segment; + } + + bool feature_less(const vertex &v) const { + if (layer != v.layer) { + return layer < v.layer; + } + if (segment != v.segment) { + return segment < v.segment; + } + return seq < v.seq; + } + bool operator<(const vertex &v) const { if (mid < v.mid) { return true; @@ -312,9 +337,7 @@ struct vertex { if (p2 < v.p2) { return true; } else if (p2 == v.p2) { - if (feature < v.feature) { - return true; - } + return feature_less(v); } } }