#include #include #include #include #include #include namespace route_graph { namespace { constexpr float kInf = std::numeric_limits::infinity(); inline float stepLength(const Cell& a, const Cell& b) { return (a.x != b.x && a.y != b.y) ? 1.41421356f : 1.0f; } } // namespace void Graph::clear() { width_ = height_ = 0; nodes_.clear(); edges_.clear(); cells_.clear(); cum_.clear(); cell_index_.clear(); } bool Graph::fromMsg(const robot_sensor_msgs::RouteGraph& msg, std::string* err) { clear(); auto fail = [&](const std::string& why) { if (err) *err = why; clear(); return false; }; if (msg.version != kSupportedVersion) return fail("version " + std::to_string(msg.version) + " unsupported (want " + std::to_string(kSupportedVersion) + ")"); if (msg.width == 0 || msg.height == 0) return fail("width/height is 0"); if (msg.node_x.size() != msg.node_y.size()) return fail("node_x/node_y size mismatch"); const std::size_t n_nodes = msg.node_x.size(); const std::size_t n_edges = msg.edge_u.size(); if (msg.edge_v.size() != n_edges || msg.edge_length_cells.size() != n_edges || msg.edge_min_clearance_cells.size() != n_edges) return fail("edge arrays size mismatch"); if (msg.edge_cell_offset.size() != n_edges + 1) return fail("edge_cell_offset size != edges + 1"); if (msg.cell_x.size() != msg.cell_y.size()) return fail("cell_x/cell_y size mismatch"); if (msg.edge_cell_offset.front() != 0 || msg.edge_cell_offset.back() != msg.cell_x.size()) return fail("edge_cell_offset does not span cell arrays"); if (n_nodes == 0 || n_edges == 0) return fail("empty graph"); width_ = static_cast(msg.width); height_ = static_cast(msg.height); auto inGrid = [&](int x, int y) { return x >= 0 && y >= 0 && x < width_ && y < height_; }; nodes_.resize(n_nodes); for (std::size_t i = 0; i < n_nodes; ++i) { if (!inGrid(msg.node_x[i], msg.node_y[i])) return fail("node " + std::to_string(i) + " outside the grid"); nodes_[i].cell = Cell(msg.node_x[i], msg.node_y[i]); } cells_.resize(msg.cell_x.size()); for (std::size_t i = 0; i < cells_.size(); ++i) { if (!inGrid(msg.cell_x[i], msg.cell_y[i])) return fail("cell " + std::to_string(i) + " outside the grid"); cells_[i] = Cell(msg.cell_x[i], msg.cell_y[i]); } edges_.resize(n_edges); cum_.resize(cells_.size(), 0.0f); for (std::size_t e = 0; e < n_edges; ++e) { Edge& ed = edges_[e]; ed.u = msg.edge_u[e]; ed.v = msg.edge_v[e]; if (ed.u < 0 || ed.v < 0 || static_cast(ed.u) >= n_nodes || static_cast(ed.v) >= n_nodes) return fail("edge " + std::to_string(e) + " references a node out of range"); const uint32_t a = msg.edge_cell_offset[e], b = msg.edge_cell_offset[e + 1]; if (b < a) return fail("edge_cell_offset not monotonic at " + std::to_string(e)); ed.first = a; ed.count = b - a; if (ed.count < 2) return fail("edge " + std::to_string(e) + " has fewer than 2 cells"); if (cells_[ed.first] != nodes_[ed.u].cell || cells_[ed.first + ed.count - 1] != nodes_[ed.v].cell) return fail("edge " + std::to_string(e) + " does not start at u / end at v"); ed.length_cells = msg.edge_length_cells[e]; ed.min_clearance_cells = msg.edge_min_clearance_cells[e]; nodes_[ed.u].edges.push_back(static_cast(e)); if (ed.v != ed.u) nodes_[ed.v].edges.push_back(static_cast(e)); float acc = 0.0f; cum_[ed.first] = 0.0f; for (std::size_t i = 1; i < ed.count; ++i) { const std::size_t k = ed.first + i; const int dx = std::abs(cells_[k].x - cells_[k - 1].x); const int dy = std::abs(cells_[k].y - cells_[k - 1].y); if (dx > 1 || dy > 1) return fail("edge " + std::to_string(e) + " is not 8-connected"); acc += stepLength(cells_[k - 1], cells_[k]); cum_[k] = acc; } // Interior cells first, so a junction cell (shared by several edges) resolves to whichever // edge claimed it first — any of them is a valid attachment. for (std::size_t i = 0; i < ed.count; ++i) cell_index_.emplace(key(cells_[ed.first + i].x, cells_[ed.first + i].y, width_), std::make_pair(static_cast(e), static_cast(i))); } return true; } bool Graph::cellOnGraph(int x, int y, int* edge, std::size_t* index) const { if (x < 0 || y < 0 || x >= width_ || y >= height_) return false; const auto it = cell_index_.find(key(x, y, width_)); if (it == cell_index_.end()) return false; if (edge) *edge = it->second.first; if (index) *index = it->second.second; return true; } Attachment Graph::attach(Cell from, int max_radius_cells, const std::function& passable, float min_clearance_cells) const { Attachment out; if (empty() || from.x < 0 || from.y < 0 || from.x >= width_ || from.y >= height_) return out; const int r = std::max(1, max_radius_cells); const int side = 2 * r + 1; const int ox = from.x - r, oy = from.y - r; auto local = [&](int x, int y) { return (y - oy) * side + (x - ox); }; auto inWindow = [&](int x, int y) { return x >= ox && y >= oy && x < ox + side && y < oy + side && x >= 0 && y >= 0 && x < width_ && y < height_; }; std::vector parent(static_cast(side) * side, -2); // -2 unvisited, -1 root std::queue q; parent[local(from.x, from.y)] = -1; q.push(from); Cell found; bool hit = false; static const int kDx[8] = {1, -1, 0, 0, 1, 1, -1, -1}; static const int kDy[8] = {0, 0, 1, -1, 1, -1, 1, -1}; while (!q.empty() && !hit) { const Cell c = q.front(); q.pop(); if (cellOnGraph(c.x, c.y, &out.edge, &out.index) && edges_[out.edge].min_clearance_cells >= min_clearance_cells) { found = c; hit = true; break; } for (int k = 0; k < 8; ++k) { const int nx = c.x + kDx[k], ny = c.y + kDy[k]; if (!inWindow(nx, ny)) continue; int32_t& p = parent[local(nx, ny)]; if (p != -2) continue; if (!passable(nx, ny)) continue; p = local(c.x, c.y); q.push(Cell(nx, ny)); } } if (!hit) return out; // Walk parents back to the root, then reverse so the path runs query -> graph. std::vector rev; int cur = local(found.x, found.y); while (cur >= 0) { rev.push_back(Cell(ox + cur % side, oy + cur / side)); cur = parent[cur]; } out.path.assign(rev.rbegin(), rev.rend()); out.ok = true; return out; } void Graph::appendEdgeCells(int edge, std::size_t from_index, std::size_t to_index, Route& out) const { const Edge& e = edges_[edge]; const Cell* cells = edgeCells(edge); auto push = [&](std::size_t i) { const Cell& c = cells[i]; if (!out.cells.empty()) { if (out.cells.back() == c) return; out.length_cells += stepLength(out.cells.back(), c); } out.cells.push_back(c); out.clearance_cells.push_back(e.min_clearance_cells); }; if (from_index <= to_index) for (std::size_t i = from_index; i <= to_index; ++i) push(i); else for (std::size_t i = from_index + 1; i-- > to_index;) push(i); } Route Graph::route(const Attachment& a, const Attachment& b, float min_clearance_cells) const { Route out; if (empty() || !a.ok || !b.ok) return out; if (a.edge < 0 || b.edge < 0 || static_cast(a.edge) >= edges_.size() || static_cast(b.edge) >= edges_.size()) return out; if (a.index >= edges_[a.edge].count || b.index >= edges_[b.edge].count) return out; if (edges_[a.edge].min_clearance_cells < min_clearance_cells || edges_[b.edge].min_clearance_cells < min_clearance_cells) return out; const Edge& ea = edges_[a.edge]; const Edge& eb = edges_[b.edge]; const float a_to_u = cumAt(a.edge, a.index); const float a_to_v = ea.length_cells - a_to_u; const float b_to_u = cumAt(b.edge, b.index); const float b_to_v = eb.length_cells - b_to_u; // Same edge: the direct slice is a candidate; the detour through the rest of the graph is the // other one (it wins only on a loop edge, where the far side can be shorter). float best = kInf; enum class Kind { kNone, kDirect, kViaNodes } kind = Kind::kNone; if (a.edge == b.edge) { best = std::fabs(cumAt(a.edge, a.index) - cumAt(b.edge, b.index)); kind = Kind::kDirect; } // Dijkstra over nodes, seeded from both ends of a's edge. const std::size_t n = nodes_.size(); std::vector dist(n, kInf); std::vector prev_node(n, -1), prev_edge(n, -1); using Item = std::pair; std::priority_queue, std::greater> pq; auto seed = [&](int node, float d) { if (d < dist[node]) { dist[node] = d; pq.emplace(d, node); } }; seed(ea.u, a_to_u); seed(ea.v, a_to_v); while (!pq.empty()) { const auto [d, u] = pq.top(); pq.pop(); if (d > dist[u]) continue; for (int e : nodes_[u].edges) { const Edge& ed = edges_[e]; if (ed.min_clearance_cells < min_clearance_cells) continue; // too narrow for this robot const int w = ed.u == u ? ed.v : ed.u; const float nd = d + ed.length_cells; if (nd < dist[w]) { dist[w] = nd; prev_node[w] = u; prev_edge[w] = e; pq.emplace(nd, w); } } } int end_node = -1; const float via_u = dist[eb.u] + b_to_u; const float via_v = dist[eb.v] + b_to_v; if (via_u < best || via_v < best) { if (via_u <= via_v) { best = via_u; end_node = eb.u; } else { best = via_v; end_node = eb.v; } kind = Kind::kViaNodes; } if (kind == Kind::kNone || !std::isfinite(best)) return out; // Attach path of a: query cell -> graph cell. The graph cell itself is pushed again by the // edge walk and de-duplicated there. for (const Cell& c : a.path) { if (!out.cells.empty()) out.length_cells += stepLength(out.cells.back(), c); out.cells.push_back(c); out.clearance_cells.push_back(ea.min_clearance_cells); } if (kind == Kind::kDirect) { appendEdgeCells(a.edge, a.index, b.index, out); } else { // Unwind node chain from end_node back to whichever end of a's edge seeded it. std::vector chain_nodes, chain_edges; int cur = end_node; while (cur != -1 && prev_node[cur] != -1) { chain_nodes.push_back(cur); chain_edges.push_back(prev_edge[cur]); cur = prev_node[cur]; } const int first_node = cur; // ea.u or ea.v std::reverse(chain_nodes.begin(), chain_nodes.end()); std::reverse(chain_edges.begin(), chain_edges.end()); // a's edge: from a.index to the seeded end. appendEdgeCells(a.edge, a.index, first_node == ea.u ? 0 : ea.count - 1, out); // Node chain. int at = first_node; for (std::size_t i = 0; i < chain_edges.size(); ++i) { const Edge& ed = edges_[chain_edges[i]]; if (ed.u == at) appendEdgeCells(chain_edges[i], 0, ed.count - 1, out); else appendEdgeCells(chain_edges[i], ed.count - 1, 0, out); at = chain_nodes[i]; } // b's edge: from the reached end to b.index. appendEdgeCells(b.edge, end_node == eb.u ? 0 : eb.count - 1, b.index, out); } // Attach path of b, walked backwards: graph cell -> query cell. for (std::size_t i = b.path.size(); i-- > 0;) { const Cell& c = b.path[i]; if (!out.cells.empty()) { if (out.cells.back() == c) continue; out.length_cells += stepLength(out.cells.back(), c); } out.cells.push_back(c); out.clearance_cells.push_back(eb.min_clearance_cells); } return out; } int Graph::componentCount() const { const std::size_t n = nodes_.size(); std::vector seen(n, 0); int count = 0; std::vector stack; for (std::size_t s = 0; s < n; ++s) { if (seen[s]) continue; ++count; seen[s] = 1; stack.assign(1, static_cast(s)); while (!stack.empty()) { const int u = stack.back(); stack.pop_back(); for (int e : nodes_[u].edges) { const int w = edges_[e].u == u ? edges_[e].v : edges_[e].u; if (!seen[w]) { seen[w] = 1; stack.push_back(w); } } } } return count; } } // namespace route_graph