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:
262
src/route_planner.cpp
Normal file
262
src/route_planner.cpp
Normal file
@@ -0,0 +1,262 @@
|
||||
#include <lane_route_core/route_planner.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
|
||||
#include <lane_route_core/clothoid.h>
|
||||
#include <lane_route_core/grid_checks.h>
|
||||
#include <lane_route_core/swing_chain.h>
|
||||
|
||||
#include "lattice_internal.h"
|
||||
#include "route_planner_internal.h"
|
||||
|
||||
namespace lane_route_core
|
||||
{
|
||||
|
||||
using detail::Clock;
|
||||
using detail::TimePoint;
|
||||
using detail::msSince;
|
||||
|
||||
PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, const GridRef& live, const PlannerConfig& config,
|
||||
const RidgeBackend& backend)
|
||||
{
|
||||
PlanResult result;
|
||||
const TimePoint began = Clock::now();
|
||||
const auto finish = [&]() {
|
||||
result.info.query_ms = msSince(began);
|
||||
if (request.cancel && request.cancel->load(std::memory_order_relaxed))
|
||||
{
|
||||
result.ok = false;
|
||||
result.path.clear();
|
||||
result.info.reason = "cancelled";
|
||||
}
|
||||
return result;
|
||||
};
|
||||
const std::string config_error = config.validate();
|
||||
if (!config_error.empty())
|
||||
{
|
||||
result.info.reason = "invalid_config: " + config_error;
|
||||
return finish();
|
||||
}
|
||||
if (live.cost == nullptr || live.clearance_mm == nullptr || live.width <= 0 || live.height <= 0 || !(live.resolution > 0.0) ||
|
||||
(!graphs && !config.lattice_enabled) || !std::isfinite(request.start.x) || !std::isfinite(request.start.y) || !std::isfinite(request.goal.x) ||
|
||||
!std::isfinite(request.goal.y) || (config.lattice_enabled && (!std::isfinite(live.resolution) ||
|
||||
!std::isfinite(live.origin_x) || !std::isfinite(live.origin_y))) || (request.start.has_yaw && !std::isfinite(request.start.yaw)) ||
|
||||
(request.goal.has_yaw && !std::isfinite(request.goal.yaw)))
|
||||
{
|
||||
result.info.reason = "invalid_request";
|
||||
return finish();
|
||||
}
|
||||
|
||||
const TimePoint deadline = began + std::chrono::duration_cast<Clock::duration>(std::chrono::duration<double>(config.time_limit_s));
|
||||
|
||||
// Stricter limits first. With no zones, or a strict configured limit, one rung is all there is.
|
||||
const std::size_t cell_count = static_cast<std::size_t>(live.width) * static_cast<std::size_t>(live.height);
|
||||
bool has_zones = false;
|
||||
if (live.direction != nullptr)
|
||||
has_zones = std::any_of(live.direction, live.direction + cell_count, [](uint8_t code) { return code != 0; });
|
||||
std::vector<double> rungs;
|
||||
if (config.max_deviation_deg > 0.0 && has_zones)
|
||||
{
|
||||
std::set<double> strict;
|
||||
for (double value : config.deviation_ladder_deg)
|
||||
if (value < config.max_deviation_deg)
|
||||
strict.insert(value);
|
||||
rungs.assign(strict.begin(), strict.end());
|
||||
}
|
||||
rungs.push_back(config.max_deviation_deg);
|
||||
|
||||
// R2 smoothing must share this fallback deadline instead of starting a new budget.
|
||||
TimePoint fallback_deadline = deadline;
|
||||
bool fallback_started = false;
|
||||
for (std::size_t rung = 0; rung < rungs.size(); ++rung)
|
||||
{
|
||||
const double deviation = rungs[rung];
|
||||
if ((request.cancel && request.cancel->load(std::memory_order_relaxed)) || Clock::now() >= deadline)
|
||||
{
|
||||
result.info.reason = "timeout";
|
||||
break;
|
||||
}
|
||||
bool graph_ok = false;
|
||||
if (!(config.lattice_enabled && request.lattice_only))
|
||||
{
|
||||
const TimePoint compile_began = Clock::now();
|
||||
const std::shared_ptr<const CompiledGraph> graph = graphs ? graphs(deviation) : std::shared_ptr<const CompiledGraph>();
|
||||
result.info.compile_ms += msSince(compile_began);
|
||||
if (request.cancel && request.cancel->load(std::memory_order_relaxed)) break;
|
||||
if (graph == nullptr || std::abs(graph->deviation_deg - deviation) > 1e-9)
|
||||
result.info.reason = "graph_unavailable";
|
||||
else
|
||||
{
|
||||
result.info.live_ridge.clear();
|
||||
graph_ok = detail::planGraphTier({ *graph, request, live, deadline, config, backend },
|
||||
{ result.path, result.check, result.info });
|
||||
}
|
||||
result.info.graph_reason = result.info.reason;
|
||||
result.info.graph_rung_reasons.push_back(result.info.graph_reason);
|
||||
if (graph_ok)
|
||||
{
|
||||
result.ok = true;
|
||||
result.info.tier_used = "graph";
|
||||
result.info.rung_used_deg = deviation;
|
||||
result.info.rung_reasons.push_back(result.info.reason);
|
||||
return finish();
|
||||
}
|
||||
// Preserve R0 early exits exactly while the new tier is disabled.
|
||||
if (!config.lattice_enabled)
|
||||
{
|
||||
if (result.info.reason == "graph_unavailable") break;
|
||||
result.info.rung_reasons.push_back(result.info.reason);
|
||||
if (result.info.reason == "no_route_enclosed" || result.info.reason == "timeout")
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
if (result.info.reason == "no_route_enclosed")
|
||||
{
|
||||
result.info.rung_reasons.push_back(result.info.reason);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ((request.cancel && request.cancel->load(std::memory_order_relaxed)) || Clock::now() >= deadline || result.info.reason == "timeout")
|
||||
break;
|
||||
const TimePoint now = Clock::now();
|
||||
if (!fallback_started)
|
||||
{
|
||||
fallback_started = true;
|
||||
fallback_deadline = std::min(deadline, now + std::chrono::duration_cast<Clock::duration>(std::chrono::duration<double>(config.fallback_time_limit_s)));
|
||||
}
|
||||
if (now >= fallback_deadline)
|
||||
{
|
||||
result.info.lattice_reason = "lattice_timeout";
|
||||
result.info.lattice_rung_reasons.push_back(result.info.lattice_reason);
|
||||
result.info.reason = result.info.lattice_reason;
|
||||
result.info.rung_reasons.push_back(result.info.reason);
|
||||
break;
|
||||
}
|
||||
const TimePoint rung_deadline = now + (fallback_deadline - now) / static_cast<Clock::duration::rep>(rungs.size() - rung);
|
||||
if (detail::planLatticeTier({ request, live, config, deviation, rung_deadline },
|
||||
{ result.path, result.check, result.info }))
|
||||
{
|
||||
result.ok = true;
|
||||
result.info.tier_used = "lattice";
|
||||
result.info.lattice_rung_reasons.push_back(result.info.lattice_reason);
|
||||
result.info.rung_used_deg = deviation;
|
||||
result.info.rung_reasons.push_back(result.info.reason);
|
||||
return finish();
|
||||
}
|
||||
result.info.reason = result.info.lattice_reason;
|
||||
result.info.lattice_rung_reasons.push_back(result.info.lattice_reason);
|
||||
result.info.rung_reasons.push_back(result.info.reason);
|
||||
if (result.info.reason == "cancelled" || result.info.reason == "lattice_resource_limit" || Clock::now() >= deadline)
|
||||
break;
|
||||
}
|
||||
return finish();
|
||||
}
|
||||
|
||||
std::vector<WorldBox> changedBoxes(const uint8_t* compiled_cost, const GridRef& live, const std::atomic<bool>* cancel,
|
||||
std::vector<uint8_t>* scratch, std::vector<int32_t>* stack_scratch)
|
||||
{
|
||||
// Lethal and unknown only. The compiled grid comes from the static layer, so the inflated ring the master
|
||||
// holds around every static obstacle would otherwise count as a change, and so would any soft value.
|
||||
const auto state = [](uint8_t value) { return value < kLethalCost ? 0 : value - kLethalCost + 1; };
|
||||
const std::size_t cell_count = static_cast<std::size_t>(live.width) * static_cast<std::size_t>(live.height);
|
||||
std::vector<uint8_t> local_scratch;
|
||||
std::vector<uint8_t>& differs = scratch ? *scratch : local_scratch;
|
||||
differs.assign(cell_count, 0);
|
||||
for (std::size_t i = 0; i < cell_count; ++i)
|
||||
{
|
||||
if ((i & 4095u) == 0 && cancel && cancel->load(std::memory_order_relaxed)) return {};
|
||||
differs[i] = state(compiled_cost[i]) != state(live.cost[i]) ? 1 : 0;
|
||||
}
|
||||
|
||||
std::vector<WorldBox> boxes;
|
||||
std::vector<int32_t> local_stack;
|
||||
std::vector<int32_t>& stack = stack_scratch ? *stack_scratch : local_stack;
|
||||
for (std::size_t seed = 0; seed < cell_count; ++seed)
|
||||
{
|
||||
if ((seed & 4095u) == 0 && cancel && cancel->load(std::memory_order_relaxed)) return {};
|
||||
if (differs[seed] == 0)
|
||||
continue;
|
||||
differs[seed] = 0;
|
||||
stack.assign(1, static_cast<int32_t>(seed));
|
||||
int32_t x_min = live.width, x_max = -1, y_min = live.height, y_max = -1;
|
||||
while (!stack.empty())
|
||||
{
|
||||
if (cancel && cancel->load(std::memory_order_relaxed)) return {};
|
||||
const int32_t cell = stack.back();
|
||||
stack.pop_back();
|
||||
const int32_t x = cell % live.width;
|
||||
const int32_t y = cell / live.width;
|
||||
x_min = std::min(x_min, x);
|
||||
x_max = std::max(x_max, x);
|
||||
y_min = std::min(y_min, y);
|
||||
y_max = std::max(y_max, y);
|
||||
const auto visit = [&](int32_t other) {
|
||||
if (differs[static_cast<std::size_t>(other)] != 0)
|
||||
{
|
||||
differs[static_cast<std::size_t>(other)] = 0;
|
||||
stack.push_back(other);
|
||||
}
|
||||
};
|
||||
if (x > 0)
|
||||
visit(cell - 1);
|
||||
if (x + 1 < live.width)
|
||||
visit(cell + 1);
|
||||
if (y > 0)
|
||||
visit(cell - live.width);
|
||||
if (y + 1 < live.height)
|
||||
visit(cell + live.width);
|
||||
}
|
||||
boxes.push_back({ live.origin_x + x_min * live.resolution, live.origin_y + y_min * live.resolution,
|
||||
live.origin_x + (x_max + 1) * live.resolution, live.origin_y + (y_max + 1) * live.resolution });
|
||||
}
|
||||
return boxes;
|
||||
}
|
||||
|
||||
void patchClearance(const uint8_t* compiled_cost, const uint16_t* compiled_clearance_mm, const GridRef& live, double radius_m,
|
||||
std::vector<uint16_t>* clearance_mm, const std::atomic<bool>* cancel)
|
||||
{
|
||||
const std::size_t cell_count = static_cast<std::size_t>(live.width) * static_cast<std::size_t>(live.height);
|
||||
clearance_mm->assign(compiled_clearance_mm, compiled_clearance_mm + cell_count);
|
||||
const int32_t radius_cells = static_cast<int32_t>(std::ceil(radius_m / live.resolution));
|
||||
for (int32_t y = 0; y < live.height; ++y)
|
||||
{
|
||||
if (cancel && cancel->load(std::memory_order_relaxed)) return;
|
||||
for (int32_t x = 0; x < live.width; ++x)
|
||||
{
|
||||
const int32_t cell = y * live.width + x;
|
||||
if (live.cost[cell] != kLethalCost || compiled_cost[cell] == kLethalCost)
|
||||
continue;
|
||||
(*clearance_mm)[static_cast<std::size_t>(cell)] = 0;
|
||||
// Only cells on the rim of a new region set the distance for anything outside it.
|
||||
const bool inner = x > 0 && y > 0 && x + 1 < live.width && y + 1 < live.height && live.cost[cell - 1] == kLethalCost &&
|
||||
live.cost[cell + 1] == kLethalCost && live.cost[cell - live.width] == kLethalCost &&
|
||||
live.cost[cell + live.width] == kLethalCost;
|
||||
if (inner)
|
||||
continue;
|
||||
for (int32_t ny = std::max(0, y - radius_cells); ny <= std::min(live.height - 1, y + radius_cells); ++ny)
|
||||
{
|
||||
for (int32_t nx = std::max(0, x - radius_cells); nx <= std::min(live.width - 1, x + radius_cells); ++nx)
|
||||
{
|
||||
const double metres = std::hypot(static_cast<double>(ny - y), static_cast<double>(nx - x)) * live.resolution;
|
||||
const double millimetres = std::floor(metres * 1000.0);
|
||||
uint16_t& stored = (*clearance_mm)[static_cast<std::size_t>(ny * live.width + nx)];
|
||||
if (millimetres < stored)
|
||||
stored = static_cast<uint16_t>(millimetres);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace lane_route_core
|
||||
Reference in New Issue
Block a user