feat(lane_route): add lane_route_core and lane_route_planner (R0 + R1)
lane_route_core plans on a precomputed coarse graph (Voronoi ridges and direction-zone lanes) and, when enabled, falls back to a weighted A* search over an SE(2) lattice that enforces forward motion, the minimum turn radius, end-only spins, the footprint gap and direction zones. - R0: route_planner.cpp split into graph search, path building and the graph tier; regress7 output identical to the previous build. - R1: lattice tier (lazy packed C-space tiles, direction-aware heuristic field, analytic Dubins and point connectors, end-gap floors counting unknown cells). Off by default (lattice_enabled = false). - lane_route_eval takes an optional tier mode and LANE_ROUTE_EVAL_SET overrides for offline tuning. Local snapshot for history only; not pushed.
This commit is contained in:
173
src/route_planner_internal.h
Normal file
173
src/route_planner_internal.h
Normal file
@@ -0,0 +1,173 @@
|
||||
#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. */
|
||||
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;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
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_
|
||||
Reference in New Issue
Block a user