Files
lane_route_core/src/route_planner_internal.h
duongtd 447ce46c29 feat(lane_route): footprint-zone rule with entry/exit transition, longer lane-end links (R2.11)
- A footprint may overlap a direction zone against its heading only within
  zone_transition_m (default 1.0 m) of path where the centre is inside that
  same zone, or at a parked endpoint. Checked on whole paths by the verifier.
- The lattice C-space keeps the strict per-pose rule so its paths always pass.
- Lane-end junction links reach 8 m with up to 24 links (was 6 m / 12 tries).
- lane_route_eval can dump the graph, attempts and routes per query.
2026-11-10 16:32:02 +07:00

180 lines
6.0 KiB
C++

#ifndef LANE_ROUTE_CORE_ROUTE_PLANNER_INTERNAL_H_
#define LANE_ROUTE_CORE_ROUTE_PLANNER_INTERNAL_H_
#include <array>
#include <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <lane_route_core/grid_checks.h>
#include <lane_route_core/route_planner.h>
namespace lane_route_core
{
namespace detail
{
using Clock = std::chrono::steady_clock;
using TimePoint = Clock::time_point;
constexpr double kInfinity = std::numeric_limits<double>::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<double, std::milli>(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<int32_t>(base.nodes.size())), base_edges_(static_cast<int32_t>(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<int32_t>(extra_nodes_.size()); }
int32_t edgeCount() const { return base_edges_ + static_cast<int32_t>(extra_edges_.size()); }
const Point2& node(int32_t i) const
{
return i < base_nodes_ ? base_.nodes[static_cast<std::size_t>(i)] : extra_nodes_[static_cast<std::size_t>(i - base_nodes_)];
}
const GraphEdge& edge(int32_t i) const
{
return i < base_edges_ ? base_.edges[static_cast<std::size_t>(i)] : extra_edges_[static_cast<std::size_t>(i - base_edges_)];
}
const std::vector<GraphEdge>& 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<Point2> points, EdgeKind kind, double cost)
{
extra_outgoing_[static_cast<std::size_t>(from)].push_back(edgeCount());
extra_edges_.push_back({ from, to, std::move(points), kind, cost });
}
template <typename Visit>
void forOutgoing(int32_t node, Visit visit) const
{
if (node < base_nodes_)
for (int32_t edge : base_.outgoing[static_cast<std::size_t>(node)])
visit(edge);
for (int32_t edge : extra_outgoing_[static_cast<std::size_t>(node)])
visit(edge);
}
private:
const CompiledGraph& base_;
int32_t base_nodes_, base_edges_;
std::vector<Point2> extra_nodes_;
std::vector<GraphEdge> extra_edges_;
std::vector<std::vector<int32_t>> 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<const Path> curve; // Null on a straight vertex.
double radius;
};
std::map<std::array<double, 6>, std::vector<CornerOption>> corner_options;
PlanInfo* info = nullptr;
const std::atomic<bool>* 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<double> radii;
std::vector<Point2> vertices;
std::vector<std::vector<std::pair<int32_t, int32_t>>> 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<uint8_t>& blocked,
const std::set<std::pair<int32_t, int32_t>>& blocked_pairs, double max_straight_m, double min_radius_m,
const std::vector<double>& factors, std::vector<int32_t>* route, const std::atomic<bool>* cancel);
double pathLength(const Path& path);
Candidate constructCandidate(const std::vector<int32_t>& 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_