#include "standability.h" #include #include #include 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* 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(std::max(grid_.width, grid_.height)) + 2.0; const int64_t pad = static_cast(std::min(max_pad, std::ceil(radius / grid_.resolution) + 2.0)); const int32_t x0 = static_cast(std::max(0, static_cast(tx) * 32 - pad)); const int32_t y0 = static_cast(std::max(0, static_cast(ty) * 32 - pad)); const int32_t x1 = static_cast(std::min(grid_.width, (static_cast(tx) + 1) * 32 + pad)); const int32_t y1 = static_cast(std::min(grid_.height, (static_cast(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(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(cell % static_cast(grid_.width)); const int32_t y = static_cast(cell / static_cast(grid_.width)); const int32_t tx = x / 32, ty = y / 32; const int64_t tile_id = static_cast(ty) * ((static_cast(grid_.width) + 31) / 32) + tx; Tile& tile = tiles_[tile_id]; uint8_t& value = tile.cells[static_cast(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(std::min(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(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