- lattice_field_standability_relax_m (default 0): shrinks the inflated footprint half sizes for the lattice heuristic field only; graph reference, C-space and verification stay strict. - Plugin loads it, prints it on the fallback boot line; README and eval override key. - Measured 10/11: with extra field clearance 0 any relax reopens the sim post gaps (dumps 1, 2, 4 time out); with extra clearance 0.1 a relax of 0.035 keeps them and gains one kit query.
124 lines
5.1 KiB
C++
124 lines
5.1 KiB
C++
#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, double relax_m)
|
|
: grid_(grid), half_x_m_(std::max(0.0, config.half_x_m + config.footprint_min_gap_m - relax_m)),
|
|
half_y_m_(std::max(0.0, config.half_y_m + config.footprint_min_gap_m - relax_m)),
|
|
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(half_x_m_, half_y_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 = half_x_m_;
|
|
const double hy = half_y_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
|