feat(lane_route): footprint-zone rule with entry/exit transition, longer lane-end links (R2.11)
- A footprint may overlap a direction zone against its heading only within zone_transition_m (default 1.0 m) of path where the centre is inside that same zone, or at a parked endpoint. Checked on whole paths by the verifier. - The lattice C-space keeps the strict per-pose rule so its paths always pass. - Lane-end junction links reach 8 m with up to 24 links (was 6 m / 12 tries). - lane_route_eval can dump the graph, attempts and routes per query.
This commit is contained in:
@@ -3,6 +3,11 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <memory_resource>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#include "footprint_zones.h"
|
||||
|
||||
#include <lane_route_core/grid_checks.h>
|
||||
|
||||
@@ -72,6 +77,118 @@ int zoneViolations(const PathPoint& a, const PathPoint& b, const GridRef& grid,
|
||||
return count;
|
||||
}
|
||||
|
||||
struct ZoneEndCell
|
||||
{
|
||||
uint32_t region;
|
||||
bool active = true;
|
||||
std::size_t suffix_begin = 0;
|
||||
};
|
||||
|
||||
std::vector<ZoneEndCell> wrongEndCells(const PathPoint& pose, const GridRef& grid, double deviation_deg,
|
||||
const PlannerConfig& config, detail::FootprintZoneIndex& index)
|
||||
{
|
||||
std::vector<ZoneEndCell> cells;
|
||||
detail::visitFootprintZoneCells(grid, pose.x, pose.y, pose.yaw, config.half_x_m, config.half_y_m,
|
||||
[&](const detail::FootprintZoneCell& cell) {
|
||||
if (!detail::headingAllowedInZone(pose.yaw, cell.code, deviation_deg))
|
||||
{
|
||||
const uint32_t region = index.component(cell.index);
|
||||
if (region != 0 && std::none_of(cells.begin(), cells.end(),
|
||||
[&](const ZoneEndCell& existing) { return existing.region == region; })) cells.push_back({region});
|
||||
}
|
||||
});
|
||||
return cells;
|
||||
}
|
||||
|
||||
bool touchesZoneRegion(const PathPoint& pose, uint32_t region, const GridRef& grid, const PlannerConfig& config,
|
||||
const detail::FootprintZoneIndex& index)
|
||||
{
|
||||
bool touches = false;
|
||||
detail::visitFootprintZoneCells(grid, pose.x, pose.y, pose.yaw, config.half_x_m, config.half_y_m,
|
||||
[&](const detail::FootprintZoneCell& cell) { if (index.knownComponent(cell.index) == region) touches = true; });
|
||||
return touches;
|
||||
}
|
||||
|
||||
// Collect wrong-way overlaps by connected region, then compare path distance with sampled centre entries into that
|
||||
// same region. Endpoint allowances are contiguous; transition allowances can occur before or after a centre entry.
|
||||
int footprintZoneViolations(const Path& samples, const GridRef& grid, double deviation_deg, const PlannerConfig& config,
|
||||
std::vector<ZoneEndCell> start_cells, std::vector<ZoneEndCell> goal_cells,
|
||||
detail::FootprintZoneIndex& index, const std::atomic<bool>* cancel,
|
||||
const std::chrono::steady_clock::time_point* deadline)
|
||||
{
|
||||
if (grid.direction == nullptr || samples.empty()) return 0;
|
||||
const auto stopped = [&]() {
|
||||
return (cancel && cancel->load(std::memory_order_relaxed)) ||
|
||||
(deadline && std::chrono::steady_clock::now() >= *deadline);
|
||||
};
|
||||
for (ZoneEndCell& cell : goal_cells)
|
||||
{
|
||||
std::size_t i = samples.size() - 1;
|
||||
while (i > 0 && touchesZoneRegion(samples[i - 1], cell.region, grid, config, index))
|
||||
{
|
||||
if (stopped()) return -1;
|
||||
--i;
|
||||
}
|
||||
cell.suffix_begin = i;
|
||||
}
|
||||
struct Pending { std::size_t sample; uint32_t region; };
|
||||
std::vector<Pending> pending;
|
||||
std::vector<double> along(samples.size(), 0.0);
|
||||
for (std::size_t i = 0; i < samples.size(); ++i)
|
||||
{
|
||||
if (stopped()) return -1;
|
||||
const PathPoint& pose = samples[i];
|
||||
if (i != 0) along[i] = along[i - 1] + std::hypot(pose.x - samples[i - 1].x, pose.y - samples[i - 1].y);
|
||||
for (ZoneEndCell& cell : start_cells)
|
||||
if (cell.active && !touchesZoneRegion(pose, cell.region, grid, config, index)) cell.active = false;
|
||||
if (!index.mayViolate(pose.x, pose.y, pose.yaw, config.half_x_m, config.half_y_m, deviation_deg)) continue;
|
||||
std::vector<uint32_t> rejected_regions;
|
||||
detail::visitFootprintZoneCells(grid, pose.x, pose.y, pose.yaw, config.half_x_m, config.half_y_m,
|
||||
[&](const detail::FootprintZoneCell& cell) {
|
||||
if (detail::headingAllowedInZone(pose.yaw, cell.code, deviation_deg)) return;
|
||||
uint32_t region = index.knownComponent(cell.index);
|
||||
if (region == 0) region = index.component(cell.index);
|
||||
if (region == 0) return;
|
||||
const bool allowed_start = std::any_of(start_cells.begin(), start_cells.end(),
|
||||
[&](const ZoneEndCell& allowed) { return allowed.active && allowed.region == region; });
|
||||
const bool allowed_goal = std::any_of(goal_cells.begin(), goal_cells.end(),
|
||||
[&](const ZoneEndCell& allowed) { return i >= allowed.suffix_begin && allowed.region == region; });
|
||||
if (!allowed_start && !allowed_goal &&
|
||||
std::find(rejected_regions.begin(), rejected_regions.end(), region) == rejected_regions.end())
|
||||
rejected_regions.push_back(region);
|
||||
});
|
||||
if (index.failed()) return -1;
|
||||
for (uint32_t region : rejected_regions) pending.push_back({i, region});
|
||||
}
|
||||
if (pending.empty()) return 0;
|
||||
if (config.zone_transition_m <= 0.0) return static_cast<int>(pending.size());
|
||||
|
||||
// Components that appear here were already flood-filled above; no whole-map label array is needed. Arc distances
|
||||
// are monotone, so each violation needs one binary search in the centre-entry distances of its own region.
|
||||
std::unordered_map<uint32_t, std::vector<double>> centre_distances;
|
||||
for (const Pending& entry : pending) centre_distances.try_emplace(entry.region);
|
||||
for (std::size_t i = 0; i < samples.size(); ++i)
|
||||
{
|
||||
if (stopped()) return -1;
|
||||
const double fx = std::floor((samples[i].x - grid.origin_x) / grid.resolution);
|
||||
const double fy = std::floor((samples[i].y - grid.origin_y) / grid.resolution);
|
||||
if (fx < 0.0 || fy < 0.0 || fx >= grid.width || fy >= grid.height) continue;
|
||||
const int32_t cell = static_cast<int32_t>(fy) * grid.width + static_cast<int32_t>(fx);
|
||||
const uint32_t region = index.knownComponent(cell);
|
||||
const auto found = centre_distances.find(region);
|
||||
if (found != centre_distances.end()) found->second.push_back(along[i]);
|
||||
}
|
||||
int violations = 0;
|
||||
for (const Pending& entry : pending)
|
||||
{
|
||||
const auto& distances = centre_distances.find(entry.region)->second;
|
||||
const double here = along[entry.sample];
|
||||
const auto found = std::lower_bound(distances.begin(), distances.end(), here - config.zone_transition_m);
|
||||
if (found == distances.end() || *found > here + config.zone_transition_m) ++violations;
|
||||
}
|
||||
return violations;
|
||||
}
|
||||
|
||||
bool kinkMetrics(const Path& path, double bend_limit_deg, int* kinks, double* max_bend_deg,
|
||||
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline)
|
||||
{
|
||||
@@ -215,6 +332,17 @@ PathCheck verifyPath(const Path& path, const GridRef& grid, double deviation_deg
|
||||
if (!std::isfinite(point.x) || !std::isfinite(point.y) || !std::isfinite(point.yaw))
|
||||
return check;
|
||||
|
||||
std::vector<ZoneEndCell> start_zone_cells, goal_zone_cells;
|
||||
detail::FootprintZoneIndex zone_index(grid, std::pmr::get_default_resource(), cancel, deadline);
|
||||
Path zone_samples;
|
||||
if (grid.direction != nullptr)
|
||||
{
|
||||
start_zone_cells = wrongEndCells(path.front(), grid, config.max_deviation_deg, config, zone_index);
|
||||
goal_zone_cells = wrongEndCells(path.back(), grid, config.max_deviation_deg, config, zone_index);
|
||||
if (zone_index.failed()) return check;
|
||||
zone_samples.reserve(path.size());
|
||||
}
|
||||
|
||||
// The bend limit follows the tightest legal curve (a 0.4 m floor turns 14.3 deg over 0.1 m), and a sharp corner may bend up
|
||||
// to max_kink_deg; the curvature test below still tells a sharp corner from a too-tight curve.
|
||||
const double kink_rad = config.max_kink_deg * kPi / 180.0;
|
||||
@@ -247,13 +375,36 @@ PathCheck verifyPath(const Path& path, const GridRef& grid, double deviation_deg
|
||||
}
|
||||
check.zone_violations += zoneViolations(a, b, grid, deviation_deg);
|
||||
const int samples = std::max({ 1, static_cast<int>(std::ceil(distance / 0.01)), static_cast<int>(std::ceil(std::abs(turn) / 0.03)) });
|
||||
if (corner && grid.direction != nullptr)
|
||||
{
|
||||
const int spin_samples = std::max(1, static_cast<int>(std::ceil(std::abs(turn) / 0.03)));
|
||||
for (int j = 1; j <= spin_samples; ++j)
|
||||
zone_samples.push_back({a.x, a.y, a.yaw + turn * j / spin_samples, 0.0});
|
||||
}
|
||||
if (grid.direction != nullptr)
|
||||
{
|
||||
// Match the footprint-gap sampling contract. The actual swept boundary between samples is not reconstructed.
|
||||
const int zone_steps = std::max({ 1, static_cast<int>(std::ceil(distance / 0.04)),
|
||||
static_cast<int>(std::ceil(std::abs(turn) / 0.03)) });
|
||||
for (int j = i == 0 ? 0 : 1; j <= zone_steps; ++j)
|
||||
{
|
||||
const double t = static_cast<double>(j) / zone_steps;
|
||||
zone_samples.push_back({a.x + t * dx, a.y + t * dy, corner ? b.yaw : a.yaw + t * turn, 0.0});
|
||||
}
|
||||
}
|
||||
for (int j = 0; j <= samples; ++j)
|
||||
{
|
||||
const double t = static_cast<double>(j) / samples;
|
||||
if (footprintCollides(grid, a.x + t * dx, a.y + t * dy, a.yaw + t * turn, config.half_x_m, config.half_y_m))
|
||||
const double x = a.x + t * dx, y = a.y + t * dy, yaw = a.yaw + t * turn;
|
||||
if (footprintCollides(grid, x, y, yaw, config.half_x_m, config.half_y_m))
|
||||
++check.footprint_collisions;
|
||||
}
|
||||
}
|
||||
const int body_zone_violations = footprintZoneViolations(zone_samples, grid, config.max_deviation_deg, config,
|
||||
std::move(start_zone_cells), std::move(goal_zone_cells),
|
||||
zone_index, cancel, deadline);
|
||||
if (body_zone_violations < 0) return check;
|
||||
check.zone_violations += body_zone_violations;
|
||||
check.self_crossings = selfCrossings(path, cancel, deadline);
|
||||
if (check.self_crossings < 0) return check;
|
||||
check.valid = check.zone_violations == 0 && check.footprint_collisions == 0 && check.kinks == 0 &&
|
||||
|
||||
Reference in New Issue
Block a user