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:
234
src/footprint_zones.h
Normal file
234
src/footprint_zones.h
Normal file
@@ -0,0 +1,234 @@
|
||||
#ifndef LANE_ROUTE_CORE_FOOTPRINT_ZONES_H_
|
||||
#define LANE_ROUTE_CORE_FOOTPRINT_ZONES_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory_resource>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <lane_route_core/types.h>
|
||||
|
||||
namespace lane_route_core
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
struct FootprintZoneCell
|
||||
{
|
||||
int32_t index;
|
||||
uint8_t code;
|
||||
};
|
||||
|
||||
// Rectangle/square separating-axis test. Cell-edge contact counts, as in the footprint collision check.
|
||||
inline bool footprintIntersectsZoneCell(const GridRef& grid, double x, double y, double yaw,
|
||||
double half_x, double half_y, int32_t ix, int32_t iy)
|
||||
{
|
||||
const double c = std::cos(yaw), s = std::sin(yaw);
|
||||
const double ac = std::abs(c), as = std::abs(s);
|
||||
const double half_cell = grid.resolution * 0.5;
|
||||
const double dx = grid.origin_x + (ix + 0.5) * grid.resolution - x;
|
||||
const double dy = grid.origin_y + (iy + 0.5) * grid.resolution - y;
|
||||
const double projected = half_cell * (ac + as);
|
||||
constexpr double kEpsilon = 1e-10;
|
||||
return std::abs(dx) <= half_x * ac + half_y * as + half_cell + kEpsilon &&
|
||||
std::abs(dy) <= half_x * as + half_y * ac + half_cell + kEpsilon &&
|
||||
std::abs(c * dx + s * dy) <= half_x + projected + kEpsilon &&
|
||||
std::abs(-s * dx + c * dy) <= half_y + projected + kEpsilon;
|
||||
}
|
||||
|
||||
// Visit only direction cells in the rectangle's axis-aligned bounding box; the grid, rather than map polygons,
|
||||
// defines overlapping zones. Callers can use a tile mask first to avoid this scan away from incompatible zones.
|
||||
template <typename Visitor>
|
||||
inline void visitFootprintZoneCells(const GridRef& grid, double x, double y, double yaw,
|
||||
double half_x, double half_y, Visitor visitor)
|
||||
{
|
||||
if (grid.direction == nullptr || grid.width <= 0 || grid.height <= 0 || grid.resolution <= 0.0)
|
||||
return;
|
||||
const double c = std::cos(yaw), s = std::sin(yaw);
|
||||
const double ac = std::abs(c), as = std::abs(s);
|
||||
const double rx = half_x * ac + half_y * as, ry = half_x * as + half_y * ac;
|
||||
const double half_cell = grid.resolution * 0.5;
|
||||
const double projected = half_cell * (ac + as);
|
||||
constexpr double kEpsilon = 1e-10;
|
||||
constexpr double kCellEpsilon = 1e-9;
|
||||
const double xmin = std::floor((x - rx - grid.origin_x) / grid.resolution - kCellEpsilon);
|
||||
const double xmax = std::floor((x + rx - grid.origin_x) / grid.resolution + kCellEpsilon);
|
||||
const double ymin = std::floor((y - ry - grid.origin_y) / grid.resolution - kCellEpsilon);
|
||||
const double ymax = std::floor((y + ry - grid.origin_y) / grid.resolution + kCellEpsilon);
|
||||
if (xmax < 0.0 || ymax < 0.0 || xmin >= grid.width || ymin >= grid.height)
|
||||
return;
|
||||
const int32_t x0 = static_cast<int32_t>(std::max(0.0, xmin));
|
||||
const int32_t x1 = static_cast<int32_t>(std::min(static_cast<double>(grid.width - 1), xmax));
|
||||
const int32_t y0 = static_cast<int32_t>(std::max(0.0, ymin));
|
||||
const int32_t y1 = static_cast<int32_t>(std::min(static_cast<double>(grid.height - 1), ymax));
|
||||
for (int32_t iy = y0; iy <= y1; ++iy)
|
||||
for (int32_t ix = x0; ix <= x1; ++ix)
|
||||
{
|
||||
const int32_t index = iy * grid.width + ix;
|
||||
const uint8_t code = grid.direction[index];
|
||||
if (code == 0) continue;
|
||||
const double dx = grid.origin_x + (ix + 0.5) * grid.resolution - x;
|
||||
const double dy = grid.origin_y + (iy + 0.5) * grid.resolution - y;
|
||||
if (std::abs(dx) <= rx + half_cell + kEpsilon && std::abs(dy) <= ry + half_cell + kEpsilon &&
|
||||
std::abs(c * dx + s * dy) <= half_x + projected + kEpsilon &&
|
||||
std::abs(-s * dx + c * dy) <= half_y + projected + kEpsilon)
|
||||
visitor(FootprintZoneCell{index, code});
|
||||
}
|
||||
}
|
||||
|
||||
inline bool headingAllowedInZone(double yaw, uint8_t code, double deviation_deg)
|
||||
{
|
||||
if (code == 0)
|
||||
return true;
|
||||
const double delta = std::abs(std::remainder(yaw - (static_cast<int>(code) - 1) * kPi / 4.0, 2.0 * kPi));
|
||||
return delta * 180.0 / kPi <= deviation_deg + 1e-9;
|
||||
}
|
||||
|
||||
// One byte of direction bits per touched 32x32 tile. Most poses only inspect one or two tile masks; a rectangle/cell
|
||||
// scan is needed only when a nearby tile contains an incompatible direction. The index lives for one grid snapshot.
|
||||
class FootprintZoneIndex
|
||||
{
|
||||
public:
|
||||
explicit FootprintZoneIndex(const GridRef& grid, std::pmr::memory_resource* memory = std::pmr::get_default_resource(),
|
||||
const std::atomic<bool>* cancel = nullptr,
|
||||
const std::chrono::steady_clock::time_point* deadline = nullptr)
|
||||
: grid_(grid), masks_(memory), regions_(memory), memory_(memory), cancel_(cancel), deadline_(deadline) {}
|
||||
|
||||
// Match the reference graph's 8-connected equal-code regions. The grid has no polygon IDs, so two touching zones
|
||||
// with the same code are indistinguishable. Only regions needed for endpoint or wrong-overlap checks are flood-filled.
|
||||
// Labels use 4 KiB per visited tile; the temporary stack is charged to the lattice memory resource.
|
||||
uint32_t component(int32_t cell)
|
||||
{
|
||||
if (failed_ || grid_.direction == nullptr || cell < 0 || grid_.width <= 0 || grid_.height <= 0 ||
|
||||
static_cast<int64_t>(cell) >= static_cast<int64_t>(grid_.width) * grid_.height) return 0;
|
||||
const uint8_t code = grid_.direction[cell];
|
||||
if (code == 0) return 0;
|
||||
const uint32_t known = knownComponent(cell);
|
||||
if (known != 0) return known;
|
||||
const uint32_t id = static_cast<uint32_t>(cell) + 1u;
|
||||
labelCell(cell, id);
|
||||
std::pmr::vector<int32_t> stack(memory_);
|
||||
stack.push_back(cell);
|
||||
std::size_t visited = 0;
|
||||
while (!stack.empty())
|
||||
{
|
||||
if ((++visited & 1023u) == 0 && stopped()) { failed_ = true; return 0; }
|
||||
const int32_t here = stack.back(); stack.pop_back();
|
||||
const int32_t x = here % grid_.width, y = here / grid_.width;
|
||||
for (int32_t ny = std::max(0, y - 1); ny <= std::min(grid_.height - 1, y + 1); ++ny)
|
||||
for (int32_t nx = std::max(0, x - 1); nx <= std::min(grid_.width - 1, x + 1); ++nx)
|
||||
{
|
||||
const int32_t next = ny * grid_.width + nx;
|
||||
if (grid_.direction[next] != code || knownComponent(next) != 0) continue;
|
||||
labelCell(next, id);
|
||||
stack.push_back(next);
|
||||
}
|
||||
}
|
||||
if (stopped()) { failed_ = true; return 0; }
|
||||
return id;
|
||||
}
|
||||
|
||||
uint32_t knownComponent(int32_t cell) const
|
||||
{
|
||||
const int32_t x = cell % grid_.width, y = cell / grid_.width;
|
||||
const auto found = regions_.find(tileKey(x / 32, y / 32));
|
||||
return found == regions_.end() ? 0u : found->second.labels[static_cast<std::size_t>(y % 32) * 32 + x % 32];
|
||||
}
|
||||
|
||||
bool failed() const { return failed_; }
|
||||
|
||||
bool mayViolate(double x, double y, double yaw, double half_x, double half_y, double deviation_deg)
|
||||
{
|
||||
if (grid_.direction == nullptr) return false;
|
||||
const double ac = std::abs(std::cos(yaw)), as = std::abs(std::sin(yaw));
|
||||
const double rx = half_x * ac + half_y * as, ry = half_x * as + half_y * ac;
|
||||
const auto tileOf = [&](double coordinate, double origin, int32_t limit) {
|
||||
const double cell = std::floor((coordinate - origin) / grid_.resolution);
|
||||
return static_cast<int32_t>(std::max(0.0, std::min(static_cast<double>(limit - 1), cell))) / 32;
|
||||
};
|
||||
const int32_t x0 = tileOf(x - rx - 1e-9, grid_.origin_x, grid_.width);
|
||||
const int32_t x1 = tileOf(x + rx + 1e-9, grid_.origin_x, grid_.width);
|
||||
const int32_t y0 = tileOf(y - ry - 1e-9, grid_.origin_y, grid_.height);
|
||||
const int32_t y1 = tileOf(y + ry + 1e-9, grid_.origin_y, grid_.height);
|
||||
uint8_t directions = 0;
|
||||
for (int32_t ty = y0; ty <= y1; ++ty)
|
||||
for (int32_t tx = x0; tx <= x1; ++tx)
|
||||
{
|
||||
if (stopped()) { failed_ = true; return true; }
|
||||
directions |= tileMask(tx, ty);
|
||||
}
|
||||
for (int bit = 0; bit < 8; ++bit)
|
||||
if ((directions & (1u << bit)) && !headingAllowedInZone(yaw, static_cast<uint8_t>(bit + 1), deviation_deg))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// A legal transition must have a centre in this region within L of path length, and thus within L in x and y.
|
||||
// This deliberately accepts any same-code cell in every intersecting tile, even a different component, an obstacle,
|
||||
// or a cell outside the square. It is only a necessary-condition filter; the path verifier decides the real rule.
|
||||
bool nearbyDirection(double x, double y, uint8_t code, double distance_m)
|
||||
{
|
||||
if (grid_.direction == nullptr || code == 0 || distance_m <= 0.0) return false;
|
||||
const auto tileOf = [&](double coordinate, double origin, int32_t limit) {
|
||||
const double cell = std::floor((coordinate - origin) / grid_.resolution);
|
||||
return static_cast<int32_t>(std::max(0.0, std::min(static_cast<double>(limit - 1), cell))) / 32;
|
||||
};
|
||||
const int32_t x0 = tileOf(x - distance_m - 1e-9, grid_.origin_x, grid_.width);
|
||||
const int32_t x1 = tileOf(x + distance_m + 1e-9, grid_.origin_x, grid_.width);
|
||||
const int32_t y0 = tileOf(y - distance_m - 1e-9, grid_.origin_y, grid_.height);
|
||||
const int32_t y1 = tileOf(y + distance_m + 1e-9, grid_.origin_y, grid_.height);
|
||||
const uint8_t wanted = static_cast<uint8_t>(1u << ((code - 1) % 8));
|
||||
for (int32_t ty = y0; ty <= y1; ++ty)
|
||||
for (int32_t tx = x0; tx <= x1; ++tx)
|
||||
{
|
||||
if (stopped()) { failed_ = true; return false; }
|
||||
if ((tileMask(tx, ty) & wanted) != 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
struct RegionTile { std::array<uint32_t, 1024> labels{}; };
|
||||
int64_t tileKey(int32_t x, int32_t y) const
|
||||
{ return static_cast<int64_t>(y) * ((static_cast<int64_t>(grid_.width) + 31) / 32) + x; }
|
||||
uint8_t tileMask(int32_t tx, int32_t ty)
|
||||
{
|
||||
const int64_t key = tileKey(tx, ty);
|
||||
const auto found = masks_.find(key);
|
||||
if (found != masks_.end()) return found->second;
|
||||
uint8_t mask = 0;
|
||||
for (int32_t iy = ty * 32; iy < std::min(grid_.height, (ty + 1) * 32); ++iy)
|
||||
for (int32_t ix = tx * 32; ix < std::min(grid_.width, (tx + 1) * 32); ++ix)
|
||||
{
|
||||
const uint8_t code = grid_.direction[static_cast<std::size_t>(iy) * grid_.width + ix];
|
||||
if (code != 0) mask |= static_cast<uint8_t>(1u << ((code - 1) % 8));
|
||||
}
|
||||
masks_.emplace(key, mask);
|
||||
return mask;
|
||||
}
|
||||
void labelCell(int32_t cell, uint32_t id)
|
||||
{
|
||||
const int32_t x = cell % grid_.width, y = cell / grid_.width;
|
||||
regions_[tileKey(x / 32, y / 32)].labels[static_cast<std::size_t>(y % 32) * 32 + x % 32] = id;
|
||||
}
|
||||
bool stopped() const
|
||||
{ return (cancel_ && cancel_->load(std::memory_order_relaxed)) ||
|
||||
(deadline_ && std::chrono::steady_clock::now() >= *deadline_); }
|
||||
const GridRef& grid_;
|
||||
std::pmr::unordered_map<int64_t, uint8_t> masks_;
|
||||
std::pmr::unordered_map<int64_t, RegionTile> regions_;
|
||||
std::pmr::memory_resource* memory_;
|
||||
const std::atomic<bool>* cancel_;
|
||||
const std::chrono::steady_clock::time_point* deadline_;
|
||||
bool failed_ = false;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace lane_route_core
|
||||
#endif // LANE_ROUTE_CORE_FOOTPRINT_ZONES_H_
|
||||
@@ -19,9 +19,13 @@ constexpr int kLeadSteps = 10; // Straight lead of at most 2.5 m
|
||||
constexpr double kMinCompiledLaneM = 0.3;
|
||||
constexpr double kLaneOffsetPenalty = 2.8; // Per metre off the zone's main lane, so routes stay on it when they can.
|
||||
constexpr double kShortLeads[4] = { 0.25, 0.5, 1.0, 1.5 };
|
||||
constexpr double kJunctionRadiusM = 6.0;
|
||||
constexpr int32_t kJunctionCandidates = 50;
|
||||
constexpr int kJunctionLinks = 12;
|
||||
// Lane-end links reach as far as the query-time links of a temporary lane (kTemporaryRadiusM, kTemporaryLinks). With
|
||||
// 6 m and 12 tries a lane end next to a block of parallel lane ends spent every try on them: on SEHC the west ends of
|
||||
// zone_7 never reached the zone_1/zone_3 lanes 7 m away across open floor (no ridge there), so a start outside the zone
|
||||
// detoured 116 m through zone_2 and zone_1 where a start inside it went straight in 79 m (sim 10/11).
|
||||
constexpr double kJunctionRadiusM = 8.0;
|
||||
constexpr int32_t kJunctionCandidates = 80;
|
||||
constexpr int kJunctionLinks = 24; // Counts created links only.
|
||||
constexpr double kPreferredAttachRadiusM = 6.0;
|
||||
constexpr int32_t kPreferredAttachCandidates = 30;
|
||||
constexpr double kMinLinkM = 0.05;
|
||||
@@ -230,11 +234,18 @@ CompiledGraph compileGraph(const CoarseGraph& coarse, const GridRef& grid, const
|
||||
if (v == u || found.first < kMinLinkM)
|
||||
continue;
|
||||
const Point2 there = graph.nodes[static_cast<std::size_t>(v)];
|
||||
bool linked = false;
|
||||
if (line_ok(here, there))
|
||||
{
|
||||
graph.edges.push_back({ u, v, { here, there }, EdgeKind::kJunction, found.first });
|
||||
linked = true;
|
||||
}
|
||||
if (line_ok(there, here))
|
||||
{
|
||||
graph.edges.push_back({ v, u, { there, here }, EdgeKind::kJunction, found.first });
|
||||
if (++accepted >= kJunctionLinks)
|
||||
linked = true;
|
||||
}
|
||||
if (linked && ++accepted >= kJunctionLinks)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +93,8 @@ std::string PlannerConfig::validate() const
|
||||
error << "corner_radii_m must be finite and not below min_turn_radius_m";
|
||||
else if (!not_negative(footprint_min_gap_m))
|
||||
error << "footprint_min_gap_m must be finite and >= 0";
|
||||
else if (!not_negative(zone_transition_m))
|
||||
error << "zone_transition_m must be finite and >= 0";
|
||||
else if (std::any_of(footprint_gap_shortcut_raise_m.begin(), footprint_gap_shortcut_raise_m.end(), [&](double v) { return !positive(v); }))
|
||||
error << "footprint_gap_shortcut_raise_m values must be finite and > 0";
|
||||
else if (!std::isfinite(max_deviation_deg) || max_deviation_deg < 0.0 || max_deviation_deg >= 180.0)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
#include <limits>
|
||||
|
||||
#include <lane_route_core/grid_checks.h>
|
||||
@@ -45,10 +46,111 @@ bool hitsCell(double x0, double y0, double x1, double y1, int32_t x, int32_t y,
|
||||
} // namespace
|
||||
|
||||
LatticeCSpace::LatticeCSpace(LatticeContext& context)
|
||||
: context_(context), tiles_(&context.memory), blocked_prefix_(&context.memory), gap_margin_(context.config.footprint_min_gap_m)
|
||||
: context_(context), zone_index_(context.grid, &context.memory, context.request.cancel, &context.deadline),
|
||||
endpoint_zone_regions_(&context.memory),
|
||||
tiles_(&context.memory), blocked_prefix_(&context.memory), gap_margin_(context.config.footprint_min_gap_m)
|
||||
{
|
||||
start_floor_ = endFloor(context.request.start);
|
||||
goal_floor_ = endFloor(context.request.goal);
|
||||
for (const Pose2* end : { &context.request.start, &context.request.goal })
|
||||
{
|
||||
// A free end heading is chosen later. Its circumradius square conservatively includes every possible end overlap;
|
||||
// the final check computes allowances from the actual selected yaw and rejects any extra region.
|
||||
const double radius = std::hypot(context.config.half_x_m, context.config.half_y_m);
|
||||
const double hx = end->has_yaw ? context.config.half_x_m : radius;
|
||||
const double hy = end->has_yaw ? context.config.half_y_m : radius;
|
||||
visitFootprintZoneCells(context.grid, end->x, end->y, end->has_yaw ? end->yaw : 0.0, hx, hy,
|
||||
[&](const FootprintZoneCell& cell) {
|
||||
if (!end->has_yaw || !headingAllowedInZone(end->yaw, cell.code, context.config.max_deviation_deg))
|
||||
{
|
||||
const uint32_t region = zone_index_.component(cell.index);
|
||||
if (region != 0) endpoint_zone_regions_.push_back(region);
|
||||
}
|
||||
});
|
||||
}
|
||||
std::sort(endpoint_zone_regions_.begin(), endpoint_zone_regions_.end());
|
||||
endpoint_zone_regions_.erase(std::unique(endpoint_zone_regions_.begin(), endpoint_zone_regions_.end()), endpoint_zone_regions_.end());
|
||||
}
|
||||
|
||||
bool LatticeCSpace::zonePoseFree(double x, double y, double yaw, double sweep_margin, double yaw_sweep, bool near_end)
|
||||
{
|
||||
if (zone_index_.failed()) return false;
|
||||
if (context_.grid.direction == nullptr) return true;
|
||||
// This is a necessary-condition filter at the actual pose, not a swept-footprint rejection: an expanded rectangle
|
||||
// could reject a legal transition. The whole-path verifier decides same-region membership and path-distance timing.
|
||||
const double pad = near_end ? 0.0 : std::max(0.0, sweep_margin);
|
||||
const double limit = context_.config.max_deviation_deg;
|
||||
const double hx = context_.config.half_x_m, hy = context_.config.half_y_m;
|
||||
const bool may_violate = zone_index_.mayViolate(x, y, yaw, hx, hy, limit);
|
||||
if (zone_index_.failed()) return false;
|
||||
if (!may_violate) return true;
|
||||
if (!near_end)
|
||||
{
|
||||
// Cache only proofs of safety: inflate by the entire cell/heading quantisation and allow every yaw in the bin.
|
||||
// An inconclusive entry is rechecked at the actual pose; endpoint exemptions are never cached as globally safe.
|
||||
const GridRef& g = context_.grid;
|
||||
const int32_t ix = static_cast<int32_t>(std::floor((x - g.origin_x) / g.resolution));
|
||||
const int32_t iy = static_cast<int32_t>(std::floor((y - g.origin_y) / g.resolution));
|
||||
const int bins = context_.config.lattice_heading_bins;
|
||||
const uint64_t key = (static_cast<uint64_t>(static_cast<uint32_t>(iy / 32)) << 32) | static_cast<uint32_t>(ix / 32);
|
||||
auto inserted = tiles_.try_emplace(key, &context_.memory, static_cast<std::size_t>(32) * 32 * bins);
|
||||
if (inserted.second) ++context_.info.lattice_tiles_built;
|
||||
Tile& tile = inserted.first->second;
|
||||
if (tile.zone_samples.empty()) tile.zone_samples.resize((static_cast<std::size_t>(32) * 32 * bins + 3) / 4, 0);
|
||||
double positive_yaw = std::fmod(yaw, 2.0 * kPi);
|
||||
if (positive_yaw < 0.0) positive_yaw += 2.0 * kPi;
|
||||
const int heading = static_cast<int>(std::floor(positive_yaw * bins / (2.0 * kPi) + 0.5)) % bins;
|
||||
const std::size_t local = (static_cast<std::size_t>(iy % 32) * 32 + static_cast<std::size_t>(ix % 32)) * bins + heading;
|
||||
uint8_t& packed = tile.zone_samples[local / 4];
|
||||
const unsigned shift = static_cast<unsigned>((local % 4) * 2);
|
||||
uint8_t cached = static_cast<uint8_t>((packed >> shift) & 3u);
|
||||
const double cx = g.origin_x + (ix + 0.5) * g.resolution;
|
||||
const double cy = g.origin_y + (iy + 0.5) * g.resolution;
|
||||
const double bin_yaw = heading * 2.0 * kPi / bins;
|
||||
const double radius = std::hypot(context_.config.half_x_m, context_.config.half_y_m);
|
||||
const double quantization = g.resolution / std::sqrt(2.0) + 2.0 * radius * std::sin(kPi / (2.0 * bins));
|
||||
const double bin_limit = limit - 180.0 / bins;
|
||||
if (cached == 0)
|
||||
{
|
||||
bool safe = true;
|
||||
visitFootprintZoneCells(g, cx, cy, bin_yaw, context_.config.half_x_m + quantization,
|
||||
context_.config.half_y_m + quantization, [&](const FootprintZoneCell& cell) {
|
||||
if (!headingAllowedInZone(bin_yaw, cell.code, bin_limit)) safe = false;
|
||||
});
|
||||
cached = safe ? 1 : 2;
|
||||
packed = static_cast<uint8_t>(packed | (cached << shift));
|
||||
}
|
||||
const double yaw_error = std::abs(wrapAngle(yaw - bin_yaw));
|
||||
const double error = std::hypot(x - cx, y - cy) + 2.0 * radius * std::sin(yaw_error / 2.0);
|
||||
if (cached == 1 && error + pad <= quantization && yaw_error + yaw_sweep <= kPi / bins) return true;
|
||||
}
|
||||
// The lattice keeps the strict per-pose rule: a wrong-way overlap is legal only in an endpoint region. The path rule
|
||||
// allows an overlap within zone_transition_m of a centre entry, but a single pose cannot know whether the centre will
|
||||
// enter: an optimistic filter let every pose that turns toward a zone through, the search reached the goal along the
|
||||
// zone edge, verification rejected it and the search repeated until the budget ran out (test
|
||||
// DirectionZoneRequiresDetour: 3 s timeout, 21 ms with the strict filter). Lattice paths therefore always satisfy the
|
||||
// path rule; the graph tier, which checks whole candidates, keeps the transition allowance.
|
||||
// With transition = 0 the code below reduces to: required_turn must be zero, i.e. the heading is within the limit.
|
||||
const double transition = 0.0;
|
||||
const bool endpoint_spin_possible = transition > 0.0 &&
|
||||
(std::hypot(x - context_.request.start.x, y - context_.request.start.y) <= transition + 1e-9 ||
|
||||
std::hypot(x - context_.request.goal.x, y - context_.request.goal.y) <= transition + 1e-9);
|
||||
const int32_t centre_x = static_cast<int32_t>(std::floor((x - context_.grid.origin_x) / context_.grid.resolution));
|
||||
const int32_t centre_y = static_cast<int32_t>(std::floor((y - context_.grid.origin_y) / context_.grid.resolution));
|
||||
const uint8_t centre_code = context_.grid.direction[static_cast<std::size_t>(centre_y) * context_.grid.width + centre_x];
|
||||
bool legal = true;
|
||||
visitFootprintZoneCells(context_.grid, x, y, yaw, hx, hy, [&](const FootprintZoneCell& cell) {
|
||||
if (headingAllowedInZone(yaw, cell.code, limit) ||
|
||||
std::binary_search(endpoint_zone_regions_.begin(), endpoint_zone_regions_.end(),
|
||||
zone_index_.knownComponent(cell.index))) return;
|
||||
if (transition > 0.0 && centre_code == cell.code) return; // Possibly a different component; final verification decides.
|
||||
const double angle = (static_cast<int>(cell.code) - 1) * kPi / 4.0;
|
||||
const double required_turn = std::max(0.0, std::abs(wrapAngle(yaw - angle)) - limit * kPi / 180.0 - 0.03);
|
||||
const bool can_turn = endpoint_spin_possible ||
|
||||
(2.0 / kPi) * context_.config.min_turn_radius_m * required_turn <= transition + 1e-9;
|
||||
if (!can_turn || !zone_index_.nearbyDirection(x, y, cell.code, transition)) legal = false;
|
||||
});
|
||||
return legal;
|
||||
}
|
||||
|
||||
void LatticeCSpace::setGapMargin(double margin)
|
||||
@@ -110,7 +212,7 @@ uint32_t LatticeCSpace::blockedCount(int32_t x0, int32_t y0, int32_t x1, int32_t
|
||||
return at(x1, y1) - at(x0, y1) - at(x1, y0) + at(x0, y0);
|
||||
}
|
||||
|
||||
bool LatticeCSpace::poseFree(double x, double y, double yaw, double sweep_margin, double centre_sweep)
|
||||
bool LatticeCSpace::poseFree(double x, double y, double yaw, double sweep_margin, double centre_sweep, double yaw_sweep)
|
||||
{
|
||||
if (context_.expired()) return false;
|
||||
if (context_.smooth_checks) ++*context_.smooth_checks;
|
||||
@@ -130,6 +232,7 @@ bool LatticeCSpace::poseFree(double x, double y, double yaw, double sweep_margin
|
||||
const double relax = c.lattice_end_relax_radius_m;
|
||||
const bool near_start = std::hypot(x - start.x, y - start.y) + centre_sweep <= relax;
|
||||
const bool near_goal = std::hypot(x - goal.x, y - goal.y) + centre_sweep <= relax;
|
||||
if (!zonePoseFree(x, y, yaw, sweep_margin, yaw_sweep, near_start || near_goal)) return false;
|
||||
// Away from the ends: full gap plus the sweep bound, conservative. Near an end: the floor of that end without the sweep bound,
|
||||
// because the final check is the authority there and adding the sweep could lock in a robot parked closer than gap plus sweep.
|
||||
double margin = gap_margin_ + std::max(0.0, sweep_margin);
|
||||
@@ -258,7 +361,8 @@ bool LatticeCSpace::segmentLegal(const PathPoint& a, const PathPoint& b)
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
const double t = (static_cast<double>(i) + 0.5) / count;
|
||||
if (!poseFree(a.x + t * dx, a.y + t * dy, a.yaw + t * yaw_delta, sweep, centre_sweep)) return false;
|
||||
if (!poseFree(a.x + t * dx, a.y + t * dy, a.yaw + t * yaw_delta, sweep, centre_sweep,
|
||||
std::abs(yaw_delta) / (2.0 * count))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "route_planner_internal.h"
|
||||
#include "footprint_zones.h"
|
||||
|
||||
namespace lane_route_core
|
||||
{
|
||||
@@ -124,17 +125,22 @@ public:
|
||||
explicit LatticeCSpace(LatticeContext& context);
|
||||
void setGapMargin(double margin);
|
||||
double gapMargin() const { return gap_margin_; }
|
||||
bool poseFree(double x, double y, double yaw, double sweep_margin = 0.0, double centre_sweep = 0.0);
|
||||
bool poseFree(double x, double y, double yaw, double sweep_margin = 0.0, double centre_sweep = 0.0,
|
||||
double yaw_sweep = 0.0);
|
||||
bool segmentLegal(const PathPoint& a, const PathPoint& b);
|
||||
bool spinLegal(double x, double y, double from, double to);
|
||||
|
||||
private:
|
||||
struct Tile
|
||||
{
|
||||
Tile(std::pmr::memory_resource* resource, std::size_t size) : samples(resource) { samples.resize((size + 3) / 4, 0); }
|
||||
Tile(std::pmr::memory_resource* resource, std::size_t size) : samples(resource), zone_samples(resource)
|
||||
{ samples.resize((size + 3) / 4, 0); }
|
||||
std::pmr::vector<uint8_t> samples;
|
||||
std::pmr::vector<uint8_t> zone_samples; // Lazy 2-bit conservative footprint-zone checks per cell/heading.
|
||||
};
|
||||
LatticeContext& context_;
|
||||
FootprintZoneIndex zone_index_;
|
||||
std::pmr::vector<uint32_t> endpoint_zone_regions_;
|
||||
std::pmr::unordered_map<uint64_t, Tile> tiles_;
|
||||
std::pmr::vector<uint32_t> blocked_prefix_;
|
||||
bool prefix_ready_ = false;
|
||||
@@ -143,6 +149,7 @@ private:
|
||||
double gap_margin_; // [m] Away from endpoints; changing it invalidates tile samples.
|
||||
double endFloor(const Pose2& pose) const;
|
||||
bool buildBlockedPrefix();
|
||||
bool zonePoseFree(double x, double y, double yaw, double sweep_margin, double yaw_sweep, bool near_end);
|
||||
uint32_t blockedCount(int32_t x0, int32_t y0, int32_t x1, int32_t y1) const;
|
||||
};
|
||||
|
||||
|
||||
@@ -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 &&
|
||||
|
||||
@@ -184,8 +184,11 @@ bool kinkLegal(const Point2& vertex, double from, double to, const QueryContext&
|
||||
const double delta = wrapAngle(to - from);
|
||||
const int samples = std::max(1, static_cast<int>(std::ceil(std::abs(delta) / 0.03)));
|
||||
for (int i = 0; i <= samples; ++i)
|
||||
if (footprintCollides(grid, vertex.x, vertex.y, from + delta * i / samples, context.config.half_x_m, context.config.half_y_m))
|
||||
{
|
||||
const double yaw = from + delta * i / samples;
|
||||
if (footprintCollides(grid, vertex.x, vertex.y, yaw, context.config.half_x_m, context.config.half_y_m))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -524,7 +527,7 @@ Candidate constructGreedyCandidate(const std::vector<int32_t>& route, const Quer
|
||||
BuildFailure failure;
|
||||
std::vector<double> radii;
|
||||
if (buildPath(merged, start, goal, context, &path, &failure, &radii) &&
|
||||
verifyPath(path, context->verify_grid, context->deviation_deg, context->config, context->cancel).valid &&
|
||||
verifyPath(path, context->verify_grid, context->deviation_deg, context->config, context->cancel, &context->deadline).valid &&
|
||||
freeClearanceOk(path, context->verify_grid, context->min_mm))
|
||||
{
|
||||
result.ok = true;
|
||||
@@ -598,7 +601,7 @@ bool candidateLegal(const Path& path, const QueryContext& context, const Pose2&
|
||||
{
|
||||
const GridRef& grid = context.verify_grid;
|
||||
const PlannerConfig& config = context.config;
|
||||
if (!verifyPath(path, grid, context.deviation_deg, config, context.cancel).valid || !endpointOk(path, start, goal) ||
|
||||
if (!verifyPath(path, grid, context.deviation_deg, config, context.cancel, &context.deadline).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, nullptr, true).valid)
|
||||
return false;
|
||||
|
||||
@@ -37,7 +37,7 @@ bool hasSoftCost(const uint8_t* master, std::size_t count)
|
||||
/** Route graph, then the same without soft cost values. Hard rules hold in both passes. */
|
||||
bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal, const GridRef& grid, const std::vector<WorldBox>* changed,
|
||||
TimePoint deadline, bool allow_soft_fallback, const GridRef& verify_grid, const PlannerConfig& config, Path* path,
|
||||
PathCheck* check, PlanInfo* info, const std::atomic<bool>* cancel)
|
||||
PathCheck* check, PlanInfo* info, const std::atomic<bool>* cancel, GraphTrace* trace, const char* trace_source)
|
||||
{
|
||||
QueryContext context{ grid, verify_grid, config, graph.deviation_deg, deadline, clearanceThresholdMm(config.clearance_hard_m),
|
||||
clearanceThresholdMm(config.clearance_min_m), clearanceThresholdMm(config.clearance_preferred_m),
|
||||
@@ -92,6 +92,30 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
}
|
||||
}
|
||||
|
||||
GraphTraceRun* trace_run = nullptr;
|
||||
if (trace != nullptr)
|
||||
{
|
||||
trace->runs.emplace_back();
|
||||
trace_run = &trace->runs.back();
|
||||
trace_run->deviation_deg = dev;
|
||||
trace_run->min_turn_radius_m = config.min_turn_radius_m;
|
||||
trace_run->max_kink_deg = config.max_kink_deg;
|
||||
trace_run->voronoi_weight = kVoronoiWeight;
|
||||
trace_run->corner_radii_m = config.corner_radii_m;
|
||||
trace_run->source = trace_source;
|
||||
trace_run->base_nodes = query.baseNodes();
|
||||
trace_run->base_edges = query.baseEdges();
|
||||
trace_run->start_node = s;
|
||||
trace_run->goal_node = g;
|
||||
trace_run->nodes.reserve(static_cast<std::size_t>(query.nodeCount()));
|
||||
trace_run->edges.reserve(static_cast<std::size_t>(query.edgeCount()));
|
||||
for (int32_t i = 0; i < query.nodeCount(); ++i) trace_run->nodes.push_back(query.node(i));
|
||||
for (int32_t i = 0; i < query.edgeCount(); ++i) trace_run->edges.push_back(query.edge(i));
|
||||
trace_run->factors = factors;
|
||||
for (int32_t i = 0; i < query.edgeCount(); ++i)
|
||||
if (blocked[static_cast<std::size_t>(i)] != 0) trace_run->initially_blocked_edges.push_back(i);
|
||||
}
|
||||
|
||||
std::string reason;
|
||||
std::vector<int32_t> route;
|
||||
for (int pass = 0; pass < 2; ++pass)
|
||||
@@ -116,28 +140,48 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
compiledEdgeFactors(graph, master, &factors);
|
||||
for (const GraphEdge& edge : query.extraEdges())
|
||||
factors.push_back(edgeFactor(edge.points, master, grid));
|
||||
if (trace_run != nullptr) trace_run->neutral_factors = factors;
|
||||
reason.clear();
|
||||
}
|
||||
|
||||
std::set<std::pair<int32_t, int32_t>> blocked_pairs;
|
||||
for (int attempt = 0; attempt < config.max_route_attempts; ++attempt)
|
||||
{
|
||||
GraphTraceAttempt* trace_attempt = nullptr;
|
||||
if (trace_run != nullptr)
|
||||
{
|
||||
trace_run->attempts.emplace_back();
|
||||
trace_attempt = &trace_run->attempts.back();
|
||||
trace_attempt->pass = pass;
|
||||
trace_attempt->attempt = attempt;
|
||||
}
|
||||
const auto trace_reason = [&](const auto& value) {
|
||||
if (trace_attempt != nullptr)
|
||||
{
|
||||
trace_attempt->reason = value;
|
||||
if (trace_attempt->reason.empty()) trace_attempt->reason = "unspecified_rejection";
|
||||
}
|
||||
};
|
||||
if (context.expired())
|
||||
{
|
||||
reason = "timeout";
|
||||
if (trace_attempt != nullptr)
|
||||
trace_reason(context.cancel && context.cancel->load(std::memory_order_relaxed) ? "cancelled" : reason);
|
||||
break;
|
||||
}
|
||||
timer = Clock::now();
|
||||
const bool found = searchRoute(query, s, g, blocked, blocked_pairs, max_straight_m, config.min_turn_radius_m, factors, &route, context.cancel);
|
||||
info->search_ms += msSince(timer);
|
||||
if (trace_attempt != nullptr && found) trace_attempt->route = route;
|
||||
if (!found)
|
||||
{
|
||||
if (context.cancel && context.cancel->load(std::memory_order_relaxed)) break;
|
||||
if (context.cancel && context.cancel->load(std::memory_order_relaxed)) { trace_reason("cancelled"); break; }
|
||||
// The graph is not complete, so only a component test on the grid may call this no-route.
|
||||
if (enclosedByObstacles(grid, start_point, goal_point, context.cancel))
|
||||
reason = "no_route_enclosed";
|
||||
else if (attempt == 0)
|
||||
reason = "graph_disconnected";
|
||||
if (trace_attempt != nullptr) trace_reason(reason.empty() ? "search_exhausted" : "search_exhausted: " + reason);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -153,9 +197,11 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
}
|
||||
if (bad >= 0)
|
||||
{
|
||||
if (trace_attempt != nullptr) trace_attempt->rejected_edge = bad;
|
||||
blocked[static_cast<std::size_t>(bad)] = 1;
|
||||
++info->retries;
|
||||
reason = "edge_blocked";
|
||||
trace_reason(reason);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -165,6 +211,7 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
if (context.expired())
|
||||
{
|
||||
reason = "timeout";
|
||||
trace_reason(reason);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -173,8 +220,14 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
const Point2 corner = candidate.why.corner;
|
||||
if (candidate.ok)
|
||||
{
|
||||
if (trace_attempt != nullptr) trace_attempt->candidate_length_m = pathLength(candidate.path);
|
||||
timer = Clock::now();
|
||||
PathCheck path_check = verifyPath(candidate.path, verify_grid, dev, config, context.cancel);
|
||||
PathCheck path_check = verifyPath(candidate.path, verify_grid, dev, config, context.cancel, &deadline);
|
||||
if (trace_attempt != nullptr)
|
||||
{
|
||||
trace_attempt->zone_violations = path_check.zone_violations;
|
||||
trace_attempt->footprint_collisions = path_check.footprint_collisions;
|
||||
}
|
||||
bool ends_ok = endpointOk(candidate.path, start, goal);
|
||||
bool clear_ok = freeClearanceOk(candidate.path, verify_grid, context.min_mm);
|
||||
const char* turn_blocked = blockedEndpointTurn(candidate.path, verify_grid, config.half_x_m, config.half_y_m);
|
||||
@@ -182,6 +235,7 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
if (turn_blocked != nullptr)
|
||||
{
|
||||
reason = turn_blocked;
|
||||
trace_reason(reason);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -210,7 +264,7 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
if (!alternative.ok)
|
||||
continue;
|
||||
timer = Clock::now();
|
||||
const PathCheck alternative_check = verifyPath(alternative.path, verify_grid, dev, config, context.cancel);
|
||||
const PathCheck alternative_check = verifyPath(alternative.path, verify_grid, dev, config, context.cancel, &deadline);
|
||||
const bool usable = alternative_check.valid && endpointOk(alternative.path, start, goal) &&
|
||||
freeClearanceOk(alternative.path, verify_grid, context.min_mm) &&
|
||||
blockedEndpointTurn(alternative.path, verify_grid, config.half_x_m, config.half_y_m) == nullptr;
|
||||
@@ -238,7 +292,13 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
reference_checked = true;
|
||||
}
|
||||
candidate = std::move(alternative);
|
||||
if (trace_attempt != nullptr) trace_attempt->candidate_length_m = length;
|
||||
path_check = alternative_check;
|
||||
if (trace_attempt != nullptr)
|
||||
{
|
||||
trace_attempt->zone_violations = path_check.zone_violations;
|
||||
trace_attempt->footprint_collisions = path_check.footprint_collisions;
|
||||
}
|
||||
gap = alternative_gap;
|
||||
ends_ok = true;
|
||||
clear_ok = true;
|
||||
@@ -248,6 +308,7 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
if (context.expired())
|
||||
{
|
||||
reason = "timeout";
|
||||
trace_reason(reason);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -263,11 +324,13 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
double reference = 0.0;
|
||||
const bool known = cachedStandableReference(verify_grid, start_point, goal_point, dev, bound, &reference, standability,
|
||||
reference_memo, context.cancel, &deadline);
|
||||
if (trace_attempt != nullptr && known) trace_attempt->reference_length_m = reference;
|
||||
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";
|
||||
trace_reason(reason);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -282,6 +345,7 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
info->gap_shortcut_raise_m = raise_used;
|
||||
info->min_gap_m = gap.min_gap_m;
|
||||
info->gap_exemption_used = gap.exemption_used;
|
||||
trace_reason("verified");
|
||||
return true;
|
||||
}
|
||||
why = gap_checked && !gap.valid ? "footprint_gap_below_minimum" :
|
||||
@@ -292,6 +356,7 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
if (candidate.vertices.empty())
|
||||
{
|
||||
reason = why;
|
||||
trace_reason(reason);
|
||||
break;
|
||||
}
|
||||
std::vector<std::pair<int32_t, int32_t>> pairs;
|
||||
@@ -324,11 +389,14 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
if (pairs.empty())
|
||||
{
|
||||
reason = why;
|
||||
trace_reason(reason);
|
||||
break;
|
||||
}
|
||||
blocked_pairs.insert(pairs.begin(), pairs.end());
|
||||
if (trace_attempt != nullptr) trace_attempt->blocked_pairs_added = pairs;
|
||||
++info->retries;
|
||||
reason = why;
|
||||
trace_reason(reason);
|
||||
}
|
||||
}
|
||||
info->reason = reason.empty() ? "fallback" : reason;
|
||||
@@ -337,10 +405,11 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
|
||||
/** One rung: the normal core, then a ridge rebuilt from the live grid around start and goal when the map changed. */
|
||||
bool planRung(const CompiledGraph& graph, const RouteRequest& request, const GridRef& live, TimePoint deadline, const PlannerConfig& config,
|
||||
const RidgeBackend& backend, Path* path, PathCheck* check, PlanInfo* info)
|
||||
const RidgeBackend& backend, Path* path, PathCheck* check, PlanInfo* info, GraphTrace* trace)
|
||||
{
|
||||
const std::vector<WorldBox>* changed = request.changed_boxes.empty() ? nullptr : &request.changed_boxes;
|
||||
if (planCore(graph, request.start, request.goal, live, changed, deadline, true, live, config, path, check, info, request.cancel))
|
||||
if (planCore(graph, request.start, request.goal, live, changed, deadline, true, live, config, path, check, info, request.cancel,
|
||||
trace, "compiled"))
|
||||
return true;
|
||||
if ((request.cancel && request.cancel->load(std::memory_order_relaxed)) || changed == nullptr || !backend.build_coarse || info->reason == "no_route_enclosed" || info->reason == "timeout")
|
||||
return false;
|
||||
@@ -398,7 +467,8 @@ bool planRung(const CompiledGraph& graph, const RouteRequest& request, const Gri
|
||||
{
|
||||
const CompiledGraph live_graph = compileGraph(coarse, window, window.cost, graph.deviation_deg, config, backend, request.cancel);
|
||||
if (Clock::now() < deadline)
|
||||
ok = planCore(live_graph, request.start, request.goal, window, nullptr, deadline, false, live, config, path, check, &live_info, request.cancel);
|
||||
ok = planCore(live_graph, request.start, request.goal, window, nullptr, deadline, false, live, config, path, check,
|
||||
&live_info, request.cancel, trace, "live_ridge");
|
||||
}
|
||||
info->retries += live_info.retries;
|
||||
info->gap_failures += live_info.gap_failures;
|
||||
@@ -431,7 +501,7 @@ bool planRung(const CompiledGraph& graph, const RouteRequest& request, const Gri
|
||||
bool planGraphTier(const GraphTierInput& input, GraphTierOutput output)
|
||||
{
|
||||
return planRung(input.graph, input.request, input.live, input.deadline, input.config, input.backend,
|
||||
&output.path, &output.check, &output.info);
|
||||
&output.path, &output.check, &output.info, input.trace);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
@@ -44,6 +44,12 @@ double polylineLength(const Path& path)
|
||||
|
||||
PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, const GridRef& live, const PlannerConfig& config,
|
||||
const RidgeBackend& backend)
|
||||
{
|
||||
return planRoute(graphs, request, live, config, backend, nullptr);
|
||||
}
|
||||
|
||||
PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, const GridRef& live, const PlannerConfig& config,
|
||||
const RidgeBackend& backend, GraphTrace* trace)
|
||||
{
|
||||
PlanResult result;
|
||||
const TimePoint began = Clock::now();
|
||||
@@ -156,7 +162,7 @@ PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, c
|
||||
else
|
||||
{
|
||||
result.info.live_ridge.clear();
|
||||
graph_ok = detail::planGraphTier({ *graph, request, live, deadline, tight_corners ? regular : config, backend },
|
||||
graph_ok = detail::planGraphTier({ *graph, request, live, deadline, tight_corners ? regular : config, backend, trace },
|
||||
{ result.path, result.check, result.info });
|
||||
const bool detour = graph_ok && polylineLength(result.path) > config.suspicious_ratio * chord + config.suspicious_margin_m;
|
||||
const bool stopped = (request.cancel && request.cancel->load(std::memory_order_relaxed)) || Clock::now() >= deadline ||
|
||||
@@ -167,7 +173,7 @@ PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, c
|
||||
PathCheck tight_check;
|
||||
PlanInfo tight_info = result.info;
|
||||
tight_info.live_ridge.clear();
|
||||
const bool tight_ok = detail::planGraphTier({ *graph, request, live, deadline, config, backend },
|
||||
const bool tight_ok = detail::planGraphTier({ *graph, request, live, deadline, config, backend, trace },
|
||||
{ tight_path, tight_check, tight_info });
|
||||
// A tight corner must buy a clearly shorter path; otherwise the large-radius shape stays.
|
||||
if (tight_ok && (!graph_ok || polylineLength(tight_path) < (1.0 - kTightMinSaving) * polylineLength(result.path)))
|
||||
|
||||
@@ -161,6 +161,7 @@ struct GraphTierInput
|
||||
TimePoint deadline;
|
||||
const PlannerConfig& config;
|
||||
const RidgeBackend& backend;
|
||||
GraphTrace* trace = nullptr;
|
||||
};
|
||||
|
||||
struct GraphTierOutput
|
||||
|
||||
Reference in New Issue
Block a user