Files
voronoi_route/include/voronoi_route/voronoi_route.h
2026-09-22 15:44:53 +07:00

99 lines
3.7 KiB
C++

#ifndef VORONOI_ROUTE_VORONOI_ROUTE_H_
#define VORONOI_ROUTE_VORONOI_ROUTE_H_
#include <cstddef>
#include <cstdint>
#include <vector>
#include "voronoi_route/attachment.h"
#include "voronoi_route/corridor_mask.h"
#include "voronoi_route/types.h"
#include "voronoi_route/voronoi_field.h"
#include "voronoi_route/voronoi_graph.h"
namespace voronoi_route {
/**
* @brief FNV-1a 64 over the bit-packed obstacle mask, row-major. The one hash both the producer
* (offline compile) and the consumer (planner on the live static layer) use; if they ever
* computed it differently, a stale graph could be accepted in silence.
*/
uint64_t hashObstacleMask(const std::vector<uint8_t>& occupied);
/**
* @brief Facade: occupancy in, route polylines and corridor masks out.
*
* Holds the distance field, the extracted graph and the config. Nothing here knows about
* costmaps, messages or threads; the caller decides what "occupied" means and serialises access.
*/
class VoronoiRoute {
public:
struct Config {
double min_edge_length_cells = 6.0; ///< Spur filter, absolute (0.3 m at 5 cm).
double spur_clearance_ratio = 1.2; ///< Spur filter, relative to clearance at root.
double min_component_length_cells = 40.0; ///< Drop pocket components (2 m at 5 cm).
double min_clearance_cells = 0.0; ///< Drop edges narrower than the robot (0 = off).
int attach_max_radius_cells = 200; ///< BFS bound for attach() (10 m at 5 cm).
size_t graph_rebuild_min_changed_cells = 200; ///< Below: distance update only, graph kept.
double full_rebuild_fraction = 0.02; ///< Above this share of cells: rebuild, not incremental.
};
struct Stats {
int nodes = 0;
int edges = 0;
int components = 0;
size_t gvd_cells = 0;
size_t obstacle_cells = 0;
double field_ms = 0.0; ///< DynamicVoronoi build/update + prune.
double extract_ms = 0.0; ///< Graph extraction.
size_t last_sync_changed = 0;
bool last_sync_full_rebuild = false;
};
VoronoiRoute();
explicit VoronoiRoute(const Config& cfg);
/** Build everything from scratch. `occupied` row-major, size_x*size_y, nonzero = obstacle. */
void rebuild(const std::vector<uint8_t>& occupied, int size_x, int size_y);
/**
* Compare `occupied` (same size as the current field) with what the field holds and bring it up
* to date: nothing, incremental occupy/clear + refresh, or a full rebuild when the change is
* wide (a whole-floor swap is faster from scratch than by propagation). The graph is
* re-extracted when the number of changed cells exceeds `graph_rebuild_min_changed_cells`.
* @return number of cells whose obstacle state differed. Size mismatch -> full rebuild.
*/
size_t syncOccupancy(const std::vector<uint8_t>& occupied, int size_x, int size_y);
bool ready() const { return field_.ready() && !graph_.empty(); }
Attachment attach(Cell c) const;
/** start -> ... -> goal, including both attach paths. Empty if no route on the graph. */
Polyline query(const Attachment& start, const Attachment& goal) const;
std::vector<uint8_t> corridorMask(const Polyline& route, const CorridorParams& p,
size_t* cells_in_mask) const;
const VoronoiField& field() const { return field_; }
const VoronoiGraph& graph() const { return graph_; }
const Stats& stats() const { return stats_; }
const Config& config() const { return cfg_; }
/** Hash of the occupancy the field was built from. */
uint64_t occupancyHash() const { return occupancy_hash_; }
private:
void extractGraph();
Config cfg_;
VoronoiField field_;
VoronoiGraph graph_;
Stats stats_;
uint64_t occupancy_hash_ = 0;
};
} // namespace voronoi_route
#endif // VORONOI_ROUTE_VORONOI_ROUTE_H_