#ifndef LANE_ROUTE_CORE_ROUTE_PLANNER_INTERNAL_H_ #define LANE_ROUTE_CORE_ROUTE_PLANNER_INTERNAL_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace lane_route_core { namespace detail { using Clock = std::chrono::steady_clock; using TimePoint = Clock::time_point; constexpr double kInfinity = std::numeric_limits::infinity(); constexpr double kTurnWeight = 0.6; // Cost per radian of turn at a route vertex. constexpr double kVoronoiWeight = 1.6; // Free space costs more than an explicit lane. constexpr double kSharpness[5] = { 1.0, 2.0, 4.0, 8.0, 16.0 }; constexpr double kSpinStepRad = 0.04; constexpr int32_t kStartLinks = 600; // Candidates tried around start and goal. constexpr double kStartRadiusM = 15.0; constexpr int kAttachLimit = 120; constexpr int32_t kTemporaryLinks = 80; constexpr double kTemporaryRadiusM = 8.0; constexpr int32_t kTranslatedLinks = 100; // Reserved from the deadline while improving a path that is already legal, so that the legal path can still be returned. constexpr double kSwingReserveS = 1.0; inline double msSince(TimePoint begin) { return std::chrono::duration(Clock::now() - begin).count(); } /** Compiled graph plus the nodes and edges of one query, without copying the compiled part. */ class QueryGraph { public: explicit QueryGraph(const CompiledGraph& base) : base_(base), base_nodes_(static_cast(base.nodes.size())), base_edges_(static_cast(base.edges.size())), extra_outgoing_(base.nodes.size()) { } int32_t baseNodes() const { return base_nodes_; } int32_t baseEdges() const { return base_edges_; } int32_t nodeCount() const { return base_nodes_ + static_cast(extra_nodes_.size()); } int32_t edgeCount() const { return base_edges_ + static_cast(extra_edges_.size()); } const Point2& node(int32_t i) const { return i < base_nodes_ ? base_.nodes[static_cast(i)] : extra_nodes_[static_cast(i - base_nodes_)]; } const GraphEdge& edge(int32_t i) const { return i < base_edges_ ? base_.edges[static_cast(i)] : extra_edges_[static_cast(i - base_edges_)]; } const std::vector& extraEdges() const { return extra_edges_; } const CompiledGraph& base() const { return base_; } int32_t addNode(const Point2& point) { extra_nodes_.push_back(point); extra_outgoing_.emplace_back(); return nodeCount() - 1; } void addEdge(int32_t from, int32_t to, std::vector points, EdgeKind kind, double cost) { extra_outgoing_[static_cast(from)].push_back(edgeCount()); extra_edges_.push_back({ from, to, std::move(points), kind, cost }); } template void forOutgoing(int32_t node, Visit visit) const { if (node < base_nodes_) for (int32_t edge : base_.outgoing[static_cast(node)]) visit(edge); for (int32_t edge : extra_outgoing_[static_cast(node)]) visit(edge); } private: const CompiledGraph& base_; int32_t base_nodes_, base_edges_; std::vector extra_nodes_; std::vector extra_edges_; std::vector> extra_outgoing_; }; /** Everything one core pass needs. The corner cache lives across retries and the neutral pass. */ class Standability; struct ReferenceMemo; struct QueryContext { const GridRef& grid; const GridRef& verify_grid; const PlannerConfig& config; double deviation_deg; TimePoint deadline; int hard_mm, min_mm, preferred_mm, collision_mm; struct CornerOption { double trim; std::shared_ptr curve; // Null on a straight vertex. double radius; }; std::map, std::vector> corner_options; PlanInfo* info = nullptr; const std::atomic* cancel = nullptr; Standability* standability = nullptr; ReferenceMemo* reference = nullptr; // Shared route_too_long reference of this pass; set with standability. bool expired() const { return (cancel && cancel->load(std::memory_order_relaxed)) || Clock::now() >= deadline; } bool lineLegal(const Point2& a, const Point2& b) const { return lineOk(grid, a, b, deviation_deg, hard_mm); } }; struct BuildFailure { std::string reason; bool has_corner = false; // True for a failure tied to one vertex. int vertex = -1; Point2 corner; }; struct Candidate { bool ok = false; Path path; BuildFailure why; std::vector radii; std::vector vertices; std::vector>> provenance; }; void addQueryEdges(QueryGraph* query, const QueryContext& context, const Point2& start, const Point2& goal, int32_t* start_node, int32_t* goal_node); bool searchRoute(const QueryGraph& query, int32_t start, int32_t goal, const std::vector& blocked, const std::set>& blocked_pairs, double max_straight_m, double min_radius_m, const std::vector& factors, std::vector* route, const std::atomic* cancel); double pathLength(const Path& path); Candidate constructCandidate(const std::vector& route, const QueryGraph& query, QueryContext* context, const uint8_t* master, const Pose2& start, const Pose2& goal, double shortcut_minimum_m); struct GraphTierInput { const CompiledGraph& graph; const RouteRequest& request; const GridRef& live; TimePoint deadline; const PlannerConfig& config; const RidgeBackend& backend; GraphTrace* trace = nullptr; }; struct GraphTierOutput { Path& path; PathCheck& check; PlanInfo& info; }; bool planGraphTier(const GraphTierInput& input, GraphTierOutput output); } // namespace detail } // namespace lane_route_core #endif // LANE_ROUTE_CORE_ROUTE_PLANNER_INTERNAL_H_