#ifndef VORONOI_ROUTE_VORONOI_GRAPH_H_ #define VORONOI_ROUTE_VORONOI_GRAPH_H_ #include #include #include "voronoi_route/types.h" namespace voronoi_route { class VoronoiField; struct GraphNode { Cell cell; std::vector edges; ///< Incident edge indices. A loop edge (u == v) appears once. }; struct GraphEdge { int u = -1; int v = -1; Polyline cells; ///< cells.front() is node u's cell, cells.back() node v's. std::vector cum_cells; ///< cum_cells[i] = walked length from cells[0] to cells[i]. float length_cells = 0.0f; ///< == cum_cells.back(). float min_clearance_cells = 0.0f; ///< Narrowest point along the chain. }; /** * @brief Route graph extracted from a thinned GVD: junctions/dead-ends are nodes, the chains * between them are edges. Also answers "which edge does this GVD cell belong to" and * routes between two attached points with Dijkstra. */ class VoronoiGraph { public: /** * @param min_edge_length_cells Dead-end branches shorter than this are pruned (SLAM wall * noise grows stubs a few cells long at every convex corner). Pruning repeats until * nothing short is left, and nodes left with exactly two edges are merged away. */ struct ExtractParams { double min_edge_length_cells = 6.0; /** * A dead-end branch shorter than `ratio * clearance(root junction)` never leaves the empty * disc around its junction: it is the diagram reacting to a notch in a far wall, not a way * anywhere. Real dead-end corridors are longer than the hall they leave. 0 disables. */ double spur_clearance_ratio = 1.0; /** Connected components whose total edge length is below this are dropped (pockets). */ double min_component_length_cells = 0.0; /** * Edges whose narrowest point has less clearance than this are dropped before anything * else: a passage the robot cannot fit through is not a route, and on SLAM maps the ragged * free/unknown frontier grows dense little loops of exactly that kind. Set it to the robot's * inscribed radius in cells. 0 disables. */ double min_clearance_cells = 0.0; }; void extract(const VoronoiField& field, const ExtractParams& params); void extract(const VoronoiField& field, double min_edge_length_cells) { ExtractParams p; p.min_edge_length_cells = min_edge_length_cells; extract(field, p); } void clear(); bool empty() const { return edges_.empty(); } int sizeX() const { return size_x_; } int sizeY() const { return size_y_; } const std::vector& nodes() const { return nodes_; } const std::vector& edges() const { return edges_; } /** Edge index of a GVD cell, -1 if the cell is not on the graph. */ int edgeAt(int x, int y) const; /** Position of the cell inside edges()[edgeAt(x,y)].cells, -1 if not on the graph. */ int indexOnEdge(int x, int y) const; /** Number of connected components (nodes reachable from each other). */ int componentCount() const; /** * Shortest route along the graph between two attached points. Result runs from a's attach * cell to b's attach cell inclusive, 8-connected. Empty if they are on different components. */ Polyline route(const Attachment& a, const Attachment& b) const; private: struct RawEdge { int u, v; Polyline cells; }; void traceFromNodes(const VoronoiField& field, const std::vector& is_gvd, const std::vector& degree, std::vector& node_of_cell, std::vector& visited, std::vector& raw); void traceCycles(const VoronoiField& field, const std::vector& is_gvd, std::vector& node_of_cell, std::vector& visited, std::vector& raw); void pruneSpursAndMerge(const VoronoiField& field, const ExtractParams& params); void dropSmallComponents(double min_total_length_cells); void finalize(const VoronoiField& field); int size_x_ = 0; int size_y_ = 0; std::vector nodes_; std::vector edges_; std::vector cell_edge_; // size_x*size_y, -1 = not on graph std::vector cell_index_; // position within that edge's chain }; } // namespace voronoi_route #endif // VORONOI_ROUTE_VORONOI_GRAPH_H_