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

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