lane_route_core plans on a precomputed coarse graph (Voronoi ridges and direction-zone lanes) and, when enabled, falls back to a weighted A* search over an SE(2) lattice that enforces forward motion, the minimum turn radius, end-only spins, the footprint gap and direction zones. - R0: route_planner.cpp split into graph search, path building and the graph tier; regress7 output identical to the previous build. - R1: lattice tier (lazy packed C-space tiles, direction-aware heuristic field, analytic Dubins and point connectors, end-gap floors counting unknown cells). Off by default (lattice_enabled = false). - lane_route_eval takes an optional tier mode and LANE_ROUTE_EVAL_SET overrides for offline tuning. Local snapshot for history only; not pushed.
251 lines
9.3 KiB
C++
251 lines
9.3 KiB
C++
#include <lane_route_core/path_verifier.h>
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstddef>
|
|
|
|
#include <lane_route_core/grid_checks.h>
|
|
|
|
namespace lane_route_core
|
|
{
|
|
namespace
|
|
{
|
|
|
|
constexpr double kRadToDeg = 180.0 / kPi;
|
|
constexpr double kBendSpacingM = 0.10;
|
|
constexpr double kBendLimitDeg = 8.0; // A 1.06 m arc turns 5.4 deg over 0.1 m, so a legal curve stays below this.
|
|
|
|
/** Segment against a unit cell, slab test in grid coordinates. */
|
|
bool segmentHitsCell(double x0, double y0, double x1, double y1, int32_t ix, int32_t iy)
|
|
{
|
|
double low = 0.0, high = 1.0;
|
|
const double points[2] = { x0, y0 };
|
|
const double deltas[2] = { x1 - x0, y1 - y0 };
|
|
const double bounds[2] = { static_cast<double>(ix), static_cast<double>(iy) };
|
|
for (int axis = 0; axis < 2; ++axis)
|
|
{
|
|
if (std::abs(deltas[axis]) < 1e-14)
|
|
{
|
|
if (points[axis] < bounds[axis] || points[axis] > bounds[axis] + 1.0)
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
const double a = (bounds[axis] - points[axis]) / deltas[axis];
|
|
const double b = (bounds[axis] + 1.0 - points[axis]) / deltas[axis];
|
|
low = std::max(low, std::min(a, b));
|
|
high = std::min(high, std::max(a, b));
|
|
if (low > high + 1e-12)
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int zoneViolations(const PathPoint& a, const PathPoint& b, const GridRef& grid, double deviation_deg)
|
|
{
|
|
if (grid.direction == nullptr)
|
|
return 0;
|
|
const double x0 = (a.x - grid.origin_x) / grid.resolution;
|
|
const double y0 = (a.y - grid.origin_y) / grid.resolution;
|
|
const double x1 = (b.x - grid.origin_x) / grid.resolution;
|
|
const double y1 = (b.y - grid.origin_y) / grid.resolution;
|
|
if (std::hypot(x1 - x0, y1 - y0) < 1e-12)
|
|
return 0;
|
|
const double angle = std::atan2(b.y - a.y, b.x - a.x);
|
|
int count = 0;
|
|
const int32_t first_y = std::max<int32_t>(0, static_cast<int32_t>(std::floor(std::min(y0, y1) - 1e-10)));
|
|
const int32_t last_y = std::min<int32_t>(grid.height - 1, static_cast<int32_t>(std::floor(std::max(y0, y1) + 1e-10)));
|
|
const int32_t first_x = std::max<int32_t>(0, static_cast<int32_t>(std::floor(std::min(x0, x1) - 1e-10)));
|
|
const int32_t last_x = std::min<int32_t>(grid.width - 1, static_cast<int32_t>(std::floor(std::max(x0, x1) + 1e-10)));
|
|
for (int32_t y = first_y; y <= last_y; ++y)
|
|
{
|
|
for (int32_t x = first_x; x <= last_x; ++x)
|
|
{
|
|
const uint8_t code = grid.direction[y * grid.width + x];
|
|
if (code == 0 || !segmentHitsCell(x0, y0, x1, y1, x, y))
|
|
continue;
|
|
if (std::abs(wrapAngle(angle - (code - 1) * kPi / 4.0)) * kRadToDeg > deviation_deg + 1e-9)
|
|
++count;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
bool kinkMetrics(const Path& path, int* kinks, double* max_bend_deg,
|
|
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline)
|
|
{
|
|
std::vector<double> along(path.size(), 0.0);
|
|
for (std::size_t i = 0; i + 1 < path.size(); ++i)
|
|
{
|
|
if ((i & 1023u) == 0 && ((cancel && cancel->load(std::memory_order_relaxed)) ||
|
|
(deadline && std::chrono::steady_clock::now() >= *deadline))) return false;
|
|
along[i + 1] = along[i] + std::hypot(path[i + 1].x - path[i].x, path[i + 1].y - path[i].y);
|
|
}
|
|
const double total = along.back();
|
|
|
|
int spins = 0;
|
|
std::size_t i = 0;
|
|
const std::size_t segments = path.size() - 1;
|
|
while (i < segments)
|
|
{
|
|
const double step = along[i + 1] - along[i];
|
|
if (step < 1e-4 && std::abs(wrapAngle(path[i + 1].yaw - path[i].yaw)) > 1e-3)
|
|
{
|
|
std::size_t j = i;
|
|
while (j < segments && along[j + 1] - along[j] < 1e-4)
|
|
++j;
|
|
if (along[i] > 0.02 && total - along[i] > 0.02)
|
|
++spins;
|
|
i = j;
|
|
}
|
|
else
|
|
{
|
|
++i;
|
|
}
|
|
}
|
|
|
|
std::vector<std::size_t> keep(1, 0);
|
|
for (std::size_t k = 1; k < path.size(); ++k)
|
|
if (along[k] - along[keep.back()] >= kBendSpacingM)
|
|
keep.push_back(k);
|
|
int over = 0;
|
|
double largest = 0.0;
|
|
for (std::size_t k = 0; k + 2 < keep.size(); ++k)
|
|
{
|
|
const PathPoint& a = path[keep[k]];
|
|
const PathPoint& b = path[keep[k + 1]];
|
|
const PathPoint& c = path[keep[k + 2]];
|
|
const double bend = std::abs(wrapAngle(std::atan2(c.y - b.y, c.x - b.x) - std::atan2(b.y - a.y, b.x - a.x))) * kRadToDeg;
|
|
largest = std::max(largest, bend);
|
|
if (bend > kBendLimitDeg)
|
|
++over;
|
|
}
|
|
*kinks = spins + over;
|
|
*max_bend_deg = largest;
|
|
return true;
|
|
}
|
|
|
|
int selfCrossings(const Path& path, const std::atomic<bool>* cancel,
|
|
const std::chrono::steady_clock::time_point* deadline)
|
|
{
|
|
constexpr double kStep = 0.1;
|
|
constexpr std::size_t kIgnore = 3;
|
|
std::vector<Point2> distinct;
|
|
std::vector<double> along;
|
|
for (const PathPoint& point : path)
|
|
{
|
|
if ((cancel && cancel->load(std::memory_order_relaxed)) || (deadline && std::chrono::steady_clock::now() >= *deadline)) return -1;
|
|
const Point2 here{ point.x, point.y };
|
|
if (distinct.empty())
|
|
{
|
|
distinct.push_back(here);
|
|
along.push_back(0.0);
|
|
}
|
|
else if (distanceOf(distinct.back(), here) > 1e-6)
|
|
{
|
|
along.push_back(along.back() + distanceOf(distinct.back(), here));
|
|
distinct.push_back(here);
|
|
}
|
|
}
|
|
if (distinct.size() < 3)
|
|
return 0;
|
|
|
|
std::vector<Point2> samples;
|
|
std::size_t segment = 0;
|
|
const double total = along.back();
|
|
for (std::size_t k = 0; k * kStep < total + kStep / 2.0; ++k)
|
|
{
|
|
if ((k & 1023u) == 0 && ((cancel && cancel->load(std::memory_order_relaxed)) ||
|
|
(deadline && std::chrono::steady_clock::now() >= *deadline))) return -1;
|
|
const double s = std::min(k * kStep, total);
|
|
while (segment + 2 < distinct.size() && along[segment + 1] < s)
|
|
++segment;
|
|
const double span = along[segment + 1] - along[segment];
|
|
const double t = span > 0.0 ? (s - along[segment]) / span : 0.0;
|
|
samples.push_back({ distinct[segment].x + t * (distinct[segment + 1].x - distinct[segment].x),
|
|
distinct[segment].y + t * (distinct[segment + 1].y - distinct[segment].y) });
|
|
}
|
|
if (samples.size() < kIgnore + 3)
|
|
return 0;
|
|
|
|
int count = 0;
|
|
const std::size_t lines = samples.size() - 1;
|
|
for (std::size_t i = 0; i + kIgnore + 1 < lines; ++i)
|
|
{
|
|
if ((cancel && cancel->load(std::memory_order_relaxed)) || (deadline && std::chrono::steady_clock::now() >= *deadline)) return -1;
|
|
const Point2& a = samples[i];
|
|
const Point2& b = samples[i + 1];
|
|
const double wx = b.x - a.x;
|
|
const double wy = b.y - a.y;
|
|
for (std::size_t j = i + kIgnore + 1; j < lines; ++j)
|
|
{
|
|
if ((j & 1023u) == 0 && ((cancel && cancel->load(std::memory_order_relaxed)) ||
|
|
(deadline && std::chrono::steady_clock::now() >= *deadline))) return -1;
|
|
const Point2& c = samples[j];
|
|
const Point2& e = samples[j + 1];
|
|
const double vx = e.x - c.x;
|
|
const double vy = e.y - c.y;
|
|
const double o1 = wx * (c.y - a.y) - wy * (c.x - a.x);
|
|
const double o2 = wx * (e.y - a.y) - wy * (e.x - a.x);
|
|
const double o3 = vx * (a.y - c.y) - vy * (a.x - c.x);
|
|
const double o4 = vx * (b.y - c.y) - vy * (b.x - c.x);
|
|
if (o1 * o2 < 0.0 && o3 * o4 < 0.0)
|
|
++count;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
PathCheck verifyPath(const Path& path, const GridRef& grid, double deviation_deg, const PlannerConfig& config,
|
|
const std::atomic<bool>* cancel)
|
|
{
|
|
return verifyPath(path, grid, deviation_deg, config, cancel, nullptr);
|
|
}
|
|
|
|
PathCheck verifyPath(const Path& path, const GridRef& grid, double deviation_deg, const PlannerConfig& config,
|
|
const std::atomic<bool>* cancel, const std::chrono::steady_clock::time_point* deadline)
|
|
{
|
|
PathCheck check;
|
|
if (path.size() < 2)
|
|
return check;
|
|
for (const PathPoint& point : path)
|
|
if (!std::isfinite(point.x) || !std::isfinite(point.y) || !std::isfinite(point.yaw))
|
|
return check;
|
|
|
|
if (!kinkMetrics(path, &check.kinks, &check.max_bend_deg, cancel, deadline)) return check;
|
|
for (std::size_t i = 0; i + 1 < path.size(); ++i)
|
|
{
|
|
if ((cancel && cancel->load(std::memory_order_relaxed)) || (deadline && std::chrono::steady_clock::now() >= *deadline)) return check;
|
|
const PathPoint& a = path[i];
|
|
const PathPoint& b = path[i + 1];
|
|
const double dx = b.x - a.x;
|
|
const double dy = b.y - a.y;
|
|
const double distance = std::hypot(dx, dy);
|
|
const double turn = wrapAngle(b.yaw - a.yaw);
|
|
if (distance > 1e-7)
|
|
{
|
|
// The displacement is a chord: recover arc length before estimating curvature.
|
|
const double arc = std::abs(turn) < 1e-9 ? distance : distance * std::abs(turn) / (2.0 * std::abs(std::sin(turn / 2.0)));
|
|
check.max_curvature = std::max(check.max_curvature, std::abs(turn) / arc);
|
|
}
|
|
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)) });
|
|
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))
|
|
++check.footprint_collisions;
|
|
}
|
|
}
|
|
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 &&
|
|
check.self_crossings == 0 && check.max_curvature <= 1.0 / config.min_turn_radius_m + 1e-6;
|
|
return check;
|
|
}
|
|
|
|
} // namespace lane_route_core
|