feat(lane_route): footprint standability, unknown cells in graph gap (R2.8)

- Standability: a cell is passable for the graph length reference and the
  lattice heuristic field only when the footprint plus the minimum gap fits
  at one of 16 headings; memoised per 32x32 tile, open space skips the test.
- Graph reference no longer cuts diagonal corners; route_too_long reports
  the reference length.
- Lattice field: coarse block passability decided lazily on first touch.
- ReferenceMemo: one route_too_long reference search per core pass instead
  of one per candidate.
- Graph tier counts unknown cells as obstacles for the footprint gap, like
  the lattice (user ruling 10/11).
- PlanInfo and eval JSON: standability counters, graph reference length.
This commit is contained in:
2026-11-10 10:01:39 +07:00
parent 527071827f
commit 4453f454a9
13 changed files with 516 additions and 23 deletions

View File

@@ -1,3 +1,5 @@
#include "standability.h"
#include <lane_route_core/grid_checks.h>
#include <algorithm>
@@ -393,8 +395,9 @@ bool enclosedByObstacles(const GridRef& grid, const Point2& start, const Point2&
return true;
}
bool shortestDirectedReference(const GridRef& grid, const Point2& start, const Point2& goal, double deviation_deg,
double bound_m, double* length_m, const std::atomic<bool>* cancel)
bool detail::shortestStandableReference(const GridRef& grid, const Point2& start, const Point2& goal, double deviation_deg,
double bound_m, double* length_m, Standability& standability,
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline)
{
int32_t from = 0, to = 0;
if (!cellOf(grid, start.x, start.y, &from) || !cellOf(grid, goal.x, goal.y, &to))
@@ -405,6 +408,7 @@ bool shortestDirectedReference(const GridRef& grid, const Point2& start, const P
const double resolution = grid.resolution;
const int32_t goal_x = to % width;
const int32_t goal_y = to / width;
const auto passable = [&](int32_t cell) { return cell == from || cell == to || standability.fits(cell); };
const auto heuristic = [&](int32_t x, int32_t y) {
return std::hypot(static_cast<double>(goal_x - x), static_cast<double>(goal_y - y)) * resolution;
};
@@ -442,7 +446,8 @@ bool shortestDirectedReference(const GridRef& grid, const Point2& start, const P
queue.push({ heuristic(from % width, from / width), { 0.0, from } });
while (!queue.empty())
{
if (cancel && cancel->load(std::memory_order_relaxed)) return false;
if ((cancel && cancel->load(std::memory_order_relaxed)) ||
(deadline && std::chrono::steady_clock::now() >= *deadline)) return false;
const Entry top = queue.top();
queue.pop();
if (top.first > bound_m + 1e-9)
@@ -465,7 +470,9 @@ bool shortestDirectedReference(const GridRef& grid, const Point2& start, const P
if (ny < 0 || nx < 0 || ny >= grid.height || nx >= width)
continue;
const int32_t other = ny * width + nx;
if (grid.cost[other] >= kInscribedCost)
if (!passable(other))
continue;
if (move.dx != 0 && move.dy != 0 && (!passable(y * width + nx) || !passable(ny * width + x)))
continue;
if (grid.direction != nullptr)
{
@@ -492,6 +499,40 @@ bool shortestDirectedReference(const GridRef& grid, const Point2& start, const P
return false;
}
bool detail::cachedStandableReference(const GridRef& grid, const Point2& start, const Point2& goal, double deviation_deg,
double bound_m, double* length_m, Standability& standability, ReferenceMemo& memo,
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline)
{
if (memo.length_m >= 0.0)
{
if (memo.length_m > bound_m + 1e-9)
return false;
*length_m = memo.length_m;
return true;
}
if (bound_m <= memo.empty_bound_m)
return false;
if (shortestStandableReference(grid, start, goal, deviation_deg, bound_m, length_m, standability, cancel, deadline))
{
memo.length_m = *length_m;
return true;
}
// A stop cuts the search short, so it proves nothing about this bound.
const bool stopped = (cancel && cancel->load(std::memory_order_relaxed)) ||
(deadline && std::chrono::steady_clock::now() >= *deadline);
if (!stopped)
memo.empty_bound_m = std::max(memo.empty_bound_m, bound_m);
return false;
}
bool shortestDirectedReference(const GridRef& grid, const Point2& start, const Point2& goal, double deviation_deg,
double bound_m, double* length_m, const std::atomic<bool>* cancel)
{
const PlannerConfig config;
detail::Standability standability(grid, config, std::pmr::get_default_resource(), nullptr, nullptr, cancel, nullptr);
return detail::shortestStandableReference(grid, start, goal, deviation_deg, bound_m, length_m, standability, cancel);
}
bool outsideZones(const Point2& a, const Point2& b, const GridRef& grid)
{
const double length = distanceOf(a, b);

View File

@@ -1,4 +1,5 @@
#include "lattice_internal.h"
#include "standability.h"
#include <algorithm>
#include <cmath>
@@ -82,18 +83,17 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
if (cells64 > std::numeric_limits<int32_t>::max()) return true;
const std::size_t cells = static_cast<std::size_t>(cells64);
// A cell counts as free only when the robot centre fits there: below kInscribedCost and with clearance to lethal cell centres of at
// least the narrow footprint half width plus the gap, less one cell for rounding (the prototype's rule). Cost alone is not enough:
// a grid without an inscribed ring has walls one or two cells thick, and a block rule on cost alone opened passages through them,
// so the heuristic pointed through walls and the search flooded (warehouse p0: 1.1 M states, no path; full resolution: 339 states).
// The extra field margin remains a separate heuristic filter; standability always uses the hard minimum gap.
const PlannerConfig& config = context.config;
// extra_clearance_m closes gaps that are only as wide as the robot: a long robot can stand in a gap between two posts but rarely
// turn in and out of it, and a field that runs through such gaps pulls the search there until it times out (sim 9/11, hook_150
// with a 0.196 m inscribed ring: four queries timed out that solve in about 1 s with the gaps closed).
const double fit_m = std::min(config.half_x_m, config.half_y_m) + config.footprint_min_gap_m - g.resolution +
std::max(0.0, extra_clearance_m);
const uint16_t fit_mm = static_cast<uint16_t>(std::max(0.0, std::min(65535.0, std::floor(fit_m * 1000.0))));
const auto fits = [&](std::size_t cell) { return g.cost[cell] < kInscribedCost && g.clearance_mm[cell] >= fit_mm; };
Standability standability(g, config, &context.memory, &context.info.lattice_standability_tests,
&context.info.lattice_standability_rejected, context.request.cancel, &field_deadline);
const auto fits = [&](std::size_t cell) {
return (extra_clearance_m <= 0.0 || g.clearance_mm[cell] >= fit_mm) &&
standability.fits(cell);
};
std::pmr::vector<uint8_t> passable(&context.memory), direction(&context.memory);
std::pmr::vector<uint16_t> clearance(&context.memory);
if (factor > 1)
@@ -123,7 +123,6 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
if ((x & 4095) == 0 && context.expired()) return false;
if ((x & 4095) == 0 && Clock::now() >= field_deadline) return true;
const std::size_t cell = static_cast<std::size_t>(y) * g.width + static_cast<std::size_t>(x);
if (fits(cell)) passable[block] = 1;
clearance[block] = std::max(clearance[block], g.clearance_mm[cell]);
}
}
@@ -135,10 +134,30 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
}
}
}
// A coarse block is passable when any of its cells is standable. It is decided lazily, when the Dijkstra first touches
// it: the search settles a few thousand blocks, while an eager scan ran the standability band test over the whole map
// (34 ms -> 250 ms field build on a 0.05 m sim map). 0 = untested, 1 = passable, 2 = blocked.
const auto blockPassable = [&](std::size_t block) {
uint8_t& state = passable[block];
if (state != 0) return state == 1;
const int64_t bx = static_cast<int64_t>(block % static_cast<std::size_t>(width));
const int64_t by = static_cast<int64_t>(block / static_cast<std::size_t>(width));
const int64_t y0 = by * factor, y1 = std::min<int64_t>(g.height, y0 + factor);
const int64_t x0 = bx * factor, x1 = std::min<int64_t>(g.width, x0 + factor);
state = 2;
for (int64_t y = y0; y < y1 && state == 2; ++y)
for (int64_t x = x0; x < x1; ++x)
if (fits(static_cast<std::size_t>(y) * g.width + static_cast<std::size_t>(x)))
{
state = 1;
break;
}
return state == 1;
};
// Start and goal are always free here: a robot parked closer than the fit rule must still get a field; the C-space decides the rest.
const auto isFree = [&](int32_t cell) {
if (cell == start || cell == goal) return true;
return factor == 1 ? fits(static_cast<std::size_t>(cell)) : passable[static_cast<std::size_t>(cell)] != 0;
return factor == 1 ? fits(static_cast<std::size_t>(cell)) : blockPassable(static_cast<std::size_t>(cell));
};
const auto zone = [&](int32_t cell) -> uint8_t {
return factor == 1 ? (g.direction == nullptr ? 0 : g.direction[cell]) : direction[static_cast<std::size_t>(cell)];

View File

@@ -1,4 +1,5 @@
#include "route_planner_internal.h"
#include "standability.h"
#include <algorithm>
#include <array>
@@ -599,7 +600,7 @@ bool candidateLegal(const Path& path, const QueryContext& context, const Pose2&
const PlannerConfig& config = context.config;
if (!verifyPath(path, grid, context.deviation_deg, config, context.cancel).valid || !endpointOk(path, start, goal) ||
!freeClearanceOk(path, grid, context.min_mm) || blockedEndpointTurn(path, grid, config.half_x_m, config.half_y_m) != nullptr ||
!checkFootprintGap(path, grid, config.footprint_min_gap_m, config.half_x_m, config.half_y_m, context.cancel).valid)
!checkFootprintGap(path, grid, config.footprint_min_gap_m, config.half_x_m, config.half_y_m, context.cancel, nullptr, true).valid)
return false;
const Point2 start_point{ start.x, start.y };
const Point2 goal_point{ goal.x, goal.y };
@@ -608,7 +609,8 @@ bool candidateLegal(const Path& path, const QueryContext& context, const Pose2&
{
const double bound = (length - config.route_too_long_margin_m) / config.route_too_long_ratio;
double reference = 0.0;
if (shortestDirectedReference(grid, start_point, goal_point, context.deviation_deg, bound, &reference, context.cancel) &&
if (cachedStandableReference(grid, start_point, goal_point, context.deviation_deg, bound, &reference, *context.standability,
*context.reference, context.cancel, &context.deadline) &&
length > config.route_too_long_ratio * reference + config.route_too_long_margin_m)
return false;
}

View File

@@ -1,4 +1,5 @@
#include "route_planner_internal.h"
#include "standability.h"
#include <algorithm>
#include <array>
@@ -43,6 +44,11 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
clearanceThresholdMm(config.collision_check_distance_m), {} };
context.info = info;
context.cancel = cancel;
Standability standability(verify_grid, config, std::pmr::get_default_resource(), &info->graph_standability_tests,
&info->graph_standability_rejected, cancel, &deadline);
context.standability = &standability;
ReferenceMemo reference_memo;
context.reference = &reference_memo;
const Point2 start_point{ start.x, start.y };
const Point2 goal_point{ goal.x, goal.y };
const double dev = graph.deviation_deg;
@@ -186,7 +192,8 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
if (path_check.valid && ends_ok && clear_ok)
{
timer = Clock::now();
gap = checkFootprintGap(candidate.path, verify_grid, config.footprint_min_gap_m, config.half_x_m, config.half_y_m, context.cancel);
gap = checkFootprintGap(candidate.path, verify_grid, config.footprint_min_gap_m, config.half_x_m, config.half_y_m, context.cancel,
nullptr, true);
info->verify_ms += msSince(timer);
gap_checked = true;
if (!gap.valid)
@@ -209,7 +216,8 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
blockedEndpointTurn(alternative.path, verify_grid, config.half_x_m, config.half_y_m) == nullptr;
GapCheck alternative_gap;
if (usable)
alternative_gap = checkFootprintGap(alternative.path, verify_grid, config.footprint_min_gap_m, config.half_x_m, config.half_y_m, context.cancel);
alternative_gap = checkFootprintGap(alternative.path, verify_grid, config.footprint_min_gap_m, config.half_x_m, config.half_y_m,
context.cancel, nullptr, true);
info->verify_ms += msSince(timer);
if (!usable)
continue;
@@ -223,7 +231,8 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
{
const double bound = (length - config.route_too_long_margin_m) / config.route_too_long_ratio;
double reference = 0.0;
if (shortestDirectedReference(verify_grid, start_point, goal_point, dev, bound, &reference, context.cancel) &&
if (cachedStandableReference(verify_grid, start_point, goal_point, dev, bound, &reference, standability, reference_memo,
context.cancel, &deadline) &&
length > config.route_too_long_ratio * reference + config.route_too_long_margin_m)
continue;
reference_checked = true;
@@ -252,10 +261,12 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
timer = Clock::now();
const double bound = (length - config.route_too_long_margin_m) / config.route_too_long_ratio;
double reference = 0.0;
const bool known = shortestDirectedReference(verify_grid, start_point, goal_point, dev, bound, &reference, context.cancel);
const bool known = cachedStandableReference(verify_grid, start_point, goal_point, dev, bound, &reference, standability,
reference_memo, context.cancel, &deadline);
info->verify_ms += msSince(timer);
if (known && length > config.route_too_long_ratio * reference + config.route_too_long_margin_m)
{
info->graph_reference_length_m = reference;
reason = "route_too_long";
break;
}
@@ -391,6 +402,10 @@ bool planRung(const CompiledGraph& graph, const RouteRequest& request, const Gri
}
info->retries += live_info.retries;
info->gap_failures += live_info.gap_failures;
info->graph_standability_tests += live_info.graph_standability_tests;
info->graph_standability_rejected += live_info.graph_standability_rejected;
if (live_info.graph_reference_length_m > 0.0)
info->graph_reference_length_m = live_info.graph_reference_length_m;
info->search_ms += live_info.search_ms;
info->build_ms += live_info.build_ms;
info->verify_ms += live_info.verify_ms;

View File

@@ -182,6 +182,13 @@ PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, c
{
result.info = tight_info;
}
else
{
// Preserve diagnostics from the tight attempt even when the regular path is kept.
result.info.graph_standability_tests = tight_info.graph_standability_tests;
result.info.graph_standability_rejected = tight_info.graph_standability_rejected;
result.info.graph_reference_length_m = tight_info.graph_reference_length_m;
}
}
if (graph_ok && result.info.graph_radius_pass.empty())
result.info.graph_radius_pass = "regular";

View File

@@ -98,6 +98,9 @@ private:
};
/** Everything one core pass needs. The corner cache lives across retries and the neutral pass. */
class Standability;
struct ReferenceMemo;
struct QueryContext
{
const GridRef& grid;
@@ -116,6 +119,8 @@ struct QueryContext
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); }

122
src/standability.cpp Normal file
View File

@@ -0,0 +1,122 @@
#include "standability.h"
#include <algorithm>
#include <cmath>
#include <lane_route_core/grid_checks.h>
namespace lane_route_core
{
namespace detail
{
Standability::Standability(const GridRef& grid, const PlannerConfig& config, std::pmr::memory_resource* memory,
uint64_t* tests, uint64_t* rejected, const std::atomic<bool>* cancel,
const std::chrono::steady_clock::time_point* deadline)
: grid_(grid), config_(config), tiles_(memory), tests_(tests), rejected_(rejected), cancel_(cancel), deadline_(deadline)
{
}
bool Standability::stopped() const
{
return (cancel_ && cancel_->load(std::memory_order_relaxed)) ||
(deadline_ && std::chrono::steady_clock::now() >= *deadline_);
}
bool Standability::tileHasBlocked(int32_t tx, int32_t ty) const
{
const double radius = std::hypot(config_.half_x_m + config_.footprint_min_gap_m,
config_.half_y_m + config_.footprint_min_gap_m);
const double max_pad = static_cast<double>(std::max(grid_.width, grid_.height)) + 2.0;
const int64_t pad = static_cast<int64_t>(std::min(max_pad, std::ceil(radius / grid_.resolution) + 2.0));
const int32_t x0 = static_cast<int32_t>(std::max<int64_t>(0, static_cast<int64_t>(tx) * 32 - pad));
const int32_t y0 = static_cast<int32_t>(std::max<int64_t>(0, static_cast<int64_t>(ty) * 32 - pad));
const int32_t x1 = static_cast<int32_t>(std::min<int64_t>(grid_.width, (static_cast<int64_t>(tx) + 1) * 32 + pad));
const int32_t y1 = static_cast<int32_t>(std::min<int64_t>(grid_.height, (static_cast<int64_t>(ty) + 1) * 32 + pad));
for (int32_t y = y0; y < y1; ++y)
{
if (stopped()) return true;
for (int32_t x = x0; x < x1; ++x)
if (grid_.cost[static_cast<std::size_t>(y) * grid_.width + x] >= kLethalCost) return true;
}
return false;
}
bool Standability::fits(std::size_t cell)
{
if (stopped()) return false;
const int32_t x = static_cast<int32_t>(cell % static_cast<std::size_t>(grid_.width));
const int32_t y = static_cast<int32_t>(cell / static_cast<std::size_t>(grid_.width));
const int32_t tx = x / 32, ty = y / 32;
const int64_t tile_id = static_cast<int64_t>(ty) * ((static_cast<int64_t>(grid_.width) + 31) / 32) + tx;
Tile& tile = tiles_[tile_id];
uint8_t& value = tile.cells[static_cast<std::size_t>(y % 32) * 32 + x % 32];
if (value != 0) return value == 1;
if (grid_.cost[cell] >= kInscribedCost)
{
value = 2;
if (rejected_) ++*rejected_;
return false;
}
const double hx = config_.half_x_m + config_.footprint_min_gap_m;
const double hy = config_.half_y_m + config_.footprint_min_gap_m;
const double radius = std::hypot(hx, hy);
const double wx = grid_.origin_x + (x + 0.5) * grid_.resolution;
const double wy = grid_.origin_y + (y + 0.5) * grid_.resolution;
const double edge = std::min(std::min((x + 0.5) * grid_.resolution, (grid_.width - x - 0.5) * grid_.resolution),
std::min((y + 0.5) * grid_.resolution, (grid_.height - y - 0.5) * grid_.resolution));
// Clearance is to lethal cell centres only. A live tile-halo scan covers lethal and unknown cells, including stale clearance; the edge test covers outside the map.
// In open space this costs one halo scan per 32x32 tile; in the band, at most 16 rectangle/cell scans per cell.
const double fast = radius + std::sqrt(2.0) * grid_.resolution / 2.0 + 0.001;
if (grid_.clearance_mm && grid_.clearance_mm[cell] / 1000.0 >= fast && edge > radius)
{
if (!tile.blocked_checked)
{
const bool blocked = tileHasBlocked(tx, ty);
if (stopped()) return false;
tile.blocked_near = blocked;
tile.blocked_checked = true;
}
if (!tile.blocked_near)
{
value = 1;
return true;
}
}
// A low cached clearance may describe a removed lethal cell. Confirm a live lethal centre inside the inscribed disc
// before rejecting without headings; otherwise the exact checks below use the current master grid.
const double inner = std::min(hx, hy) - grid_.resolution;
if (grid_.clearance_mm && inner > 0.0 && grid_.clearance_mm[cell] / 1000.0 < inner)
{
const int32_t cells = static_cast<int32_t>(std::min<double>(std::max(grid_.width, grid_.height),
std::ceil(inner / grid_.resolution)));
for (int32_t iy = std::max(0, y - cells); iy <= std::min(grid_.height - 1, y + cells); ++iy)
{
if (stopped()) return false;
for (int32_t ix = std::max(0, x - cells); ix <= std::min(grid_.width - 1, x + cells); ++ix)
{
const double distance = std::hypot(ix - x, iy - y) * grid_.resolution;
if (distance < inner && grid_.cost[static_cast<std::size_t>(iy) * grid_.width + ix] == kLethalCost)
{
value = 2;
if (rejected_) ++*rejected_;
return false;
}
}
}
}
if (tests_) ++*tests_;
for (int heading = 0; heading < 16; ++heading)
{
if (stopped()) return false;
if (!footprintCollides(grid_, wx, wy, heading * kPi / 16.0, hx, hy))
{
value = 1;
return true;
}
}
value = 2;
if (rejected_) ++*rejected_;
return false;
}
} // namespace detail
} // namespace lane_route_core

67
src/standability.h Normal file
View File

@@ -0,0 +1,67 @@
#ifndef LANE_ROUTE_CORE_STANDABILITY_H_
#define LANE_ROUTE_CORE_STANDABILITY_H_
#include <array>
#include <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <memory_resource>
#include <unordered_map>
#include <lane_route_core/types.h>
namespace lane_route_core
{
namespace detail
{
// A tile is allocated only when visited; 0 is untested, 1 is standable and 2 is blocked.
class Standability
{
public:
Standability(const GridRef& grid, const PlannerConfig& config, std::pmr::memory_resource* memory,
uint64_t* tests, uint64_t* rejected, const std::atomic<bool>* cancel,
const std::chrono::steady_clock::time_point* deadline);
bool fits(std::size_t cell);
private:
struct Tile
{
std::array<uint8_t, 1024> cells{};
bool blocked_checked = false;
bool blocked_near = true;
};
bool stopped() const;
bool tileHasBlocked(int32_t tx, int32_t ty) const;
const GridRef& grid_;
const PlannerConfig& config_;
std::pmr::unordered_map<int64_t, Tile> tiles_;
uint64_t* tests_;
uint64_t* rejected_;
const std::atomic<bool>* cancel_;
const std::chrono::steady_clock::time_point* deadline_;
};
bool shortestStandableReference(const GridRef& grid, const Point2& start, const Point2& goal, double deviation_deg,
double bound_m, double* length_m, Standability& standability,
const std::atomic<bool>* cancel = nullptr,
const std::chrono::steady_clock::time_point* deadline = nullptr);
/** Reference result shared by every candidate of one core pass. Start, goal, grid and deviation are fixed within a
* pass, so only the bound changes between calls: an exact length answers every later bound, and a bound searched
* without reaching the goal answers every smaller bound. Without it each candidate repeated the full A* (one kit
* query went from 0.35 s to 2.3 s of graph build once the reference had to respect the footprint). */
struct ReferenceMemo
{
double length_m = -1.0; // Exact shortest standable length, once found.
double empty_bound_m = -1.0; // Largest bound searched to the end without reaching the goal.
};
bool cachedStandableReference(const GridRef& grid, const Point2& start, const Point2& goal, double deviation_deg,
double bound_m, double* length_m, Standability& standability, ReferenceMemo& memo,
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline);
} // namespace detail
} // namespace lane_route_core
#endif // LANE_ROUTE_CORE_STANDABILITY_H_