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

@@ -39,6 +39,7 @@ add_library(lane_route_core SHARED
src/route_graph_search.cpp
src/route_graph_tier.cpp
src/route_planner.cpp
src/standability.cpp
src/swing_chain.cpp
)
target_include_directories(lane_route_core PUBLIC

View File

@@ -49,7 +49,8 @@ const char* blockedEndpointTurn(const Path& path, const GridRef& grid, double ha
bool enclosedByObstacles(const GridRef& grid, const Point2& start, const Point2& goal,
const std::atomic<bool>* cancel = nullptr);
/** @brief Bounded shortest 8-connected directed route on the grid.
/** @brief Bounded shortest 8-connected directed route through cells standable for the default PlannerConfig footprint.
* Diagonal moves require both side cells to be standable. The planner uses its own request footprint internally.
* @param length_m Output, set when found.
* @return False when no route shorter than `bound_m` was found. That never rejects a verified path. */
bool shortestDirectedReference(const GridRef& grid, const Point2& start, const Point2& goal, double deviation_deg,
@@ -73,7 +74,8 @@ struct GapCheck
* until the path first reaches it, the gap may not drop more than 1 mm below that end pose. */
GapCheck checkFootprintGap(const Path& path, const GridRef& grid, double minimum_m, double half_x, double half_y,
const std::atomic<bool>* cancel = nullptr);
/** @param count_unknown Also measure the gap to unknown cells. The graph tier keeps false; the lattice tier passes true. */
/** @param count_unknown Also measure the gap to unknown cells. Both planner tiers pass true (graph tier since R2.8, user ruling
* 10/11: an unknown cell is an obstacle for the 30 mm gap); false is kept for callers that only care about lethal cells. */
GapCheck checkFootprintGap(const Path& path, const GridRef& grid, double minimum_m, double half_x, double half_y,
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline,
bool count_unknown = false);

View File

@@ -53,6 +53,11 @@ struct PlanInfo
std::string lattice_reason; ///< Most recent lattice tier reason.
std::vector<std::string> graph_rung_reasons; ///< Graph reasons, in attempted-rung order.
std::vector<std::string> lattice_rung_reasons; ///< Lattice reasons, in attempted-rung order.
uint64_t graph_standability_tests = 0; ///< Cells requiring sampled footprint headings across graph attempts.
uint64_t graph_standability_rejected = 0; ///< Distinct cells rejected within each graph attempt cache.
uint64_t lattice_standability_tests = 0; ///< Cells requiring sampled footprint headings across field builds.
uint64_t lattice_standability_rejected = 0; ///< Distinct cells rejected within each field cache.
double graph_reference_length_m = 0.0; ///< [m] Most recent reference that caused route_too_long; 0 when absent.
uint64_t lattice_states_expanded = 0;
uint64_t lattice_pose_checks = 0;
uint64_t lattice_field_cells = 0;

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_

View File

@@ -7,6 +7,7 @@
#include <queue>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
@@ -20,6 +21,7 @@
#include <lane_route_core/costmap_backend.h>
#include "../src/lattice_internal.h"
#include "../src/standability.h"
using namespace lane_route_core;
@@ -644,6 +646,35 @@ TEST_F(PlanRouteTest, WallAcrossTheCorridorIsNoRoute)
EXPECT_EQ(result.info.reason, "no_route_enclosed");
}
TEST_F(PlanRouteTest, GraphFootprintGapCountsUnknownCells)
{
map_.finish(config_);
// Unknown cells in row 37 (y 1.85-1.90) for x 6-8 m: 20 mm above the footprint of a straight path at y = 1.53.
std::vector<uint8_t> live = map_.cost();
for (int x = 120; x < 160; ++x)
live[static_cast<std::size_t>(37 * 320 + x)] = kNoInformation;
const RouteRequest request{ pose(1.0, 1.53, 0.0), pose(14.0, 1.53, 0.0), {} };
GridRef grid = map_.compileGrid();
grid.cost = live.data();
const Path direct{ { 1.0, 1.53, 0.0, 0.0 }, { 14.0, 1.53, 0.0, 0.0 } };
ASSERT_TRUE(checkFootprintGap(direct, grid, config_.footprint_min_gap_m, config_.half_x_m, config_.half_y_m).valid);
ASSERT_FALSE(checkFootprintGap(direct, grid, config_.footprint_min_gap_m, config_.half_x_m, config_.half_y_m, nullptr,
nullptr, true).valid);
// Graph only: the candidate along the direct line is rejected. Its shortcut raise uses the lethal clearance field, so the
// graph cannot move the line away from unknown cells by itself; it fails instead of returning the 20 mm path.
const PlanResult graph_only = planWithObstacle(request, live);
EXPECT_FALSE(graph_only.ok);
EXPECT_EQ(graph_only.info.reason, "footprint_gap_below_minimum");
// With the fallback the lattice, which already counted unknown cells, returns a path that keeps the gap.
config_.lattice_enabled = true;
config_.lattice_preferred_gap_m = 0.0;
const PlanResult result = planWithObstacle(request, live);
expectLegal(result, request, config_);
EXPECT_EQ(result.info.tier_used, "lattice");
EXPECT_TRUE(checkFootprintGap(result.path, grid, config_.footprint_min_gap_m, config_.half_x_m, config_.half_y_m, nullptr,
nullptr, true).valid);
}
TEST_F(PlanRouteTest, GoesAroundAPartialObstacle)
{
map_.finish(config_);
@@ -1581,6 +1612,10 @@ TEST(LatticeField, BlocksUseAnyFreeLargestClearanceAndCentreDirection)
map.clearance[map.width + 3] = 5000; // Largest clearance is selected even in a blocked cell.
map.direction[map.width + 3] = 5; // Centre cell points west; other cells are unconstrained.
PlannerConfig config = latticeConfig();
// This synthetic two-row grid tests block aggregation, not a full-size robot at the map boundary.
config.half_x_m = 0.005;
config.half_y_m = 0.005;
config.footprint_min_gap_m = 0.0;
config.lattice_field_cell_factor = 2;
config.lattice_field_clearance_weight_m = 0.5; // The expected 0.26 below uses this weight.
RouteRequest request = latticeRequest(pose(0.225, 0.025, 0.0), pose(0.025, 0.025, 0.0));
@@ -2086,3 +2121,170 @@ TEST(LatticeFieldMargin, MarginThatClosesTheOnlyWayFallsBackToThePlainField)
ASSERT_TRUE(result.ok) << result.info.reason;
EXPECT_EQ(result.info.tier_used, "lattice");
}
namespace
{
void exactTestClearance(LatticeTestGrid* map)
{
std::vector<std::pair<int, int>> lethal;
for (int y = 0; y < map->height; ++y)
for (int x = 0; x < map->width; ++x)
if (map->cost[static_cast<std::size_t>(y) * map->width + x] == kLethalCost)
lethal.emplace_back(x, y);
for (int y = 0; y < map->height; ++y)
for (int x = 0; x < map->width; ++x)
{
double d = 10.0;
for (const auto& obstacle : lethal)
d = std::min(d, std::hypot(x - obstacle.first, y - obstacle.second) * kResolution);
map->clearance[static_cast<std::size_t>(y) * map->width + x] = static_cast<uint16_t>(std::floor(d * 1000.0));
}
}
} // namespace
TEST(Standability, PostsRejectEverySampledHeadingAndReferenceDetours)
{
LatticeTestGrid map(100, 100);
const int cx = 50, cy = 50;
for (const auto& offset : std::vector<std::pair<int, int>>{{-5, 0}, {5, 0}, {0, -5}, {0, 5}})
map.cost[static_cast<std::size_t>(cy + offset.second) * map.width + cx + offset.first] = kLethalCost;
exactTestClearance(&map);
PlannerConfig config = latticeConfig();
config.half_x_m = 0.5; config.half_y_m = 0.15; config.footprint_min_gap_m = 0.03;
GridRef grid = map.view();
uint64_t tests = 0, rejected = 0;
detail::LatticeMemory memory(16 * 1024 * 1024);
detail::Standability standability(grid, config, &memory, &tests, &rejected, nullptr, nullptr);
EXPECT_FALSE(standability.fits(cy * map.width + cx));
EXPECT_EQ(tests, 1u);
EXPECT_FALSE(standability.fits(cy * map.width + cx));
EXPECT_EQ(tests, 1u); // Memoised even for a rejected cell.
const Point2 start{1.025, 2.525}, goal{4.025, 2.525};
double reference = 0.0;
ASSERT_TRUE(detail::shortestStandableReference(grid, start, goal, 90.0, 100.0, &reference, standability));
EXPECT_GT(reference, 3.2); // The point-grid route through the four posts is about 3.04 m.
double bounded_reference = 0.0;
EXPECT_FALSE(detail::shortestStandableReference(grid, start, goal, 90.0, 3.2, &bounded_reference, standability));
RouteRequest request = latticeRequest(pose(start.x, start.y, 0.0), pose(goal.x, goal.y, 0.0));
PlanInfo info;
const auto deadline = detail::Clock::now() + std::chrono::seconds(10);
detail::LatticeContext context{request, grid, config, 90.0, deadline, info, memory};
detail::LatticeField field(&memory);
ASSERT_TRUE(detail::buildLatticeField(context, &field, deadline));
ASSERT_EQ(field.factor, 1);
EXPECT_EQ(field.settled[static_cast<std::size_t>(cy) * map.width + cx], 0);
EXPECT_GT(info.lattice_standability_rejected, 0u);
}
TEST(Standability, NarrowCorridorFitsLengthwise)
{
LatticeTestGrid map(100, 100);
for (int x = 25; x <= 75; ++x)
{
map.cost[static_cast<std::size_t>(45) * map.width + x] = kLethalCost;
map.cost[static_cast<std::size_t>(55) * map.width + x] = kLethalCost;
}
exactTestClearance(&map);
PlannerConfig config = latticeConfig();
config.half_x_m = 0.5; config.half_y_m = 0.15; config.footprint_min_gap_m = 0.03;
GridRef grid = map.view();
uint64_t tests = 0, rejected = 0;
detail::Standability standability(grid, config, std::pmr::get_default_resource(), &tests, &rejected, nullptr, nullptr);
const int cell = 50 * map.width + 50;
EXPECT_TRUE(standability.fits(cell));
EXPECT_GT(tests, 0u);
EXPECT_FALSE(footprintCollides(grid, 2.525, 2.525, 0.0, 0.53, 0.18));
EXPECT_TRUE(footprintCollides(grid, 2.525, 2.525, kPi / 2.0, 0.53, 0.18));
}
TEST(Standability, ReferenceDoesNotCutDiagonalBetweenObstacles)
{
LatticeTestGrid map(5, 5);
std::fill(map.cost.begin(), map.cost.end(), kLethalCost);
map.cost[1 * map.width + 1] = kFreeSpace;
map.cost[2 * map.width + 2] = kFreeSpace;
PlannerConfig config = latticeConfig();
config.half_x_m = config.half_y_m = 0.005; config.footprint_min_gap_m = 0.0;
GridRef grid = map.view();
detail::Standability standability(grid, config, std::pmr::get_default_resource(), nullptr, nullptr, nullptr, nullptr);
double reference = 0.0;
EXPECT_FALSE(detail::shortestStandableReference(grid, {0.075, 0.075}, {0.125, 0.125}, 90.0, 10.0,
&reference, standability));
}
TEST(Standability, ReferenceMemoMatchesDirectSearchForEveryBound)
{
LatticeTestGrid map(100, 100);
const int cx = 50, cy = 50;
for (const auto& offset : std::vector<std::pair<int, int>>{{-5, 0}, {5, 0}, {0, -5}, {0, 5}})
map.cost[static_cast<std::size_t>(cy + offset.second) * map.width + cx + offset.first] = kLethalCost;
exactTestClearance(&map);
PlannerConfig config = latticeConfig();
config.half_x_m = 0.5; config.half_y_m = 0.15; config.footprint_min_gap_m = 0.03;
GridRef grid = map.view();
detail::Standability standability(grid, config, std::pmr::get_default_resource(), nullptr, nullptr, nullptr, nullptr);
const Point2 start{1.025, 2.525}, goal{4.025, 2.525};
double exact = 0.0;
ASSERT_TRUE(detail::shortestStandableReference(grid, start, goal, 90.0, 100.0, &exact, standability));
// Empty bound first: the memo keeps it, a smaller bound stays empty, a larger one searches again.
detail::ReferenceMemo memo;
double reference = 0.0;
EXPECT_FALSE(detail::cachedStandableReference(grid, start, goal, 90.0, 3.2, &reference, standability, memo, nullptr, nullptr));
EXPECT_DOUBLE_EQ(memo.empty_bound_m, 3.2);
EXPECT_FALSE(detail::cachedStandableReference(grid, start, goal, 90.0, 3.1, &reference, standability, memo, nullptr, nullptr));
ASSERT_TRUE(detail::cachedStandableReference(grid, start, goal, 90.0, 100.0, &reference, standability, memo, nullptr, nullptr));
EXPECT_DOUBLE_EQ(reference, exact);
EXPECT_DOUBLE_EQ(memo.length_m, exact);
// Exact length known: any bound is answered from it, with the same result as a direct search.
reference = 0.0;
ASSERT_TRUE(detail::cachedStandableReference(grid, start, goal, 90.0, exact + 0.01, &reference, standability, memo, nullptr, nullptr));
EXPECT_DOUBLE_EQ(reference, exact);
EXPECT_FALSE(detail::cachedStandableReference(grid, start, goal, 90.0, exact - 0.01, &reference, standability, memo, nullptr, nullptr));
// A cancelled search proves nothing and is not memoised.
std::atomic<bool> cancel{true};
detail::ReferenceMemo cancelled;
EXPECT_FALSE(detail::cachedStandableReference(grid, start, goal, 90.0, 100.0, &reference, standability, cancelled, &cancel, nullptr));
EXPECT_LT(cancelled.empty_bound_m, 0.0);
EXPECT_LT(cancelled.length_m, 0.0);
}
TEST(Standability, OpenSpaceUsesNoSampledFootprintTests)
{
LatticeTestGrid map(200, 200);
PlannerConfig config = latticeConfig();
GridRef grid = map.view();
uint64_t tests = 0, rejected = 0;
detail::LatticeMemory memory(16 * 1024 * 1024);
detail::Standability standability(grid, config, &memory, &tests, &rejected, nullptr, nullptr);
double reference = 0.0;
ASSERT_TRUE(detail::shortestStandableReference(grid, {4.025, 5.025}, {6.025, 5.025}, 90.0, 10.0,
&reference, standability));
EXPECT_EQ(tests, 0u);
EXPECT_EQ(rejected, 0u);
RouteRequest request = latticeRequest(pose(4.025, 5.025, 0.0), pose(6.025, 5.025, 0.0));
PlanInfo info;
const auto deadline = detail::Clock::now() + std::chrono::seconds(10);
detail::LatticeContext context{request, grid, config, 90.0, deadline, info, memory};
detail::LatticeField field(&memory);
ASSERT_TRUE(detail::buildLatticeField(context, &field, deadline));
EXPECT_EQ(info.lattice_standability_tests, 0u);
}
TEST(Standability, UnknownCellsAreCheckedDespiteHighLethalClearance)
{
LatticeTestGrid map(100, 100);
const int cx = 50, cy = 50;
for (const auto& offset : std::vector<std::pair<int, int>>{{-5, 0}, {5, 0}, {0, -5}, {0, 5}})
map.cost[static_cast<std::size_t>(cy + offset.second) * map.width + cx + offset.first] = kNoInformation;
PlannerConfig config = latticeConfig();
config.half_x_m = 0.5; config.half_y_m = 0.15; config.footprint_min_gap_m = 0.03;
GridRef grid = map.view();
uint64_t tests = 0, rejected = 0;
detail::Standability standability(grid, config, std::pmr::get_default_resource(), &tests, &rejected, nullptr, nullptr);
EXPECT_FALSE(standability.fits(cy * map.width + cx));
EXPECT_EQ(tests, 1u);
EXPECT_EQ(rejected, 1u);
}

View File

@@ -384,7 +384,12 @@ int main(int argc, char** argv)
<< ",\"lattice_min_gap_m\":";
if (result.ok && info.tier_used == "lattice" && std::isfinite(info.min_gap_m)) out << info.min_gap_m;
else out << "null";
out << ",\"lattice_upgrade_ms\":" << info.lattice_upgrade_ms << "}\n";
out << ",\"lattice_upgrade_ms\":" << info.lattice_upgrade_ms
<< ",\"graph_standability_tests\":" << info.graph_standability_tests
<< ",\"graph_standability_rejected\":" << info.graph_standability_rejected
<< ",\"lattice_standability_tests\":" << info.lattice_standability_tests
<< ",\"lattice_standability_rejected\":" << info.lattice_standability_rejected
<< ",\"graph_reference_length_m\":" << info.graph_reference_length_m << "}\n";
std::cout << id << " " << deviation << " " << info.reason << " " << info.query_ms << " ms" << std::endl;
}
}