Now update the backtracking to check aliases when looking for adjacent ways.


Edit

    BacktrackState state{outer_ways, node_to_ways, used};

    for (size_t start_idx = 0; start_idx < outer_ways.size(); start_idx++) {
        if (used[start_idx]) continue;

        const auto* start_geom = outer_ways[start_idx].geom;
        int64_t ring_first_nid = start_geom->first_node_id;
        int64_t ring_last_nid = start_geom->last_node_id;

        // Single way already closed
        if (start_geom->coords.size() >= 4 && ring_first_nid == ring_last_nid) {
            used[start_idx] = true;
            rings.push_back(start_geom->coords);
            continue;
        }

        // Try to build a closed ring starting with this way
        used[start_idx] = true;
        std::vector<std::pair<size_t, bool>> path; // (way_index, reversed)
        if (state.try_close(ring_first_nid, ring_last_nid, path, 0)) {

    BacktrackState state{outer_ways, node_to_ways, used, nid_aliases};

    for (size_t start_idx = 0; start_idx < outer_ways.size(); start_idx++) {
        if (used[start_idx]) continue;

        const auto* start_geom = outer_ways[start_idx].geom;
        int64_t ring_first_nid = start_geom->first_node_id;
        int64_t ring_last_nid = start_geom->last_node_id;

        // Single way already closed (by node ID or coordinate alias)
        bool self_closed = (ring_first_nid == ring_last_nid);
        if (!self_closed) {
            auto ait = nid_aliases.find(ring_first_nid);
            if (ait != nid_aliases.end()) {
                for (auto alias : ait->second) {
                    if (alias == ring_last_nid) { self_closed = true; break; }
                }
            }
        }
        if (start_geom->coords.size() >= 4 && self_closed) {
            used[start_idx] = true;
            rings.push_back(start_geom->coords);
            continue;
        }

        // Try to build a closed ring starting with this way
        used[start_idx] = true;
        std::vector<std::pair<size_t, bool>> path; // (way_index, reversed)
        if (state.try_close(ring_first_nid, ring_last_nid, path, 0)) {