#include #include #include #include #include #include #include #include "footprint_zones.h" #include 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(ix), static_cast(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(0, static_cast(std::floor(std::min(y0, y1) - 1e-10))); const int32_t last_y = std::min(grid.height - 1, static_cast(std::floor(std::max(y0, y1) + 1e-10))); const int32_t first_x = std::max(0, static_cast(std::floor(std::min(x0, x1) - 1e-10))); const int32_t last_x = std::min(grid.width - 1, static_cast(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; } struct ZoneEndCell { uint32_t region; bool active = true; std::size_t suffix_begin = 0; }; std::vector wrongEndCells(const PathPoint& pose, const GridRef& grid, double deviation_deg, const PlannerConfig& config, detail::FootprintZoneIndex& index) { std::vector 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 start_cells, std::vector goal_cells, detail::FootprintZoneIndex& index, const std::atomic* cancel, const std::chrono::steady_clock::time_point* deadline, PathCheck* check) { 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; std::vector 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 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; const auto locate = [&](const Pending& entry) { if (!check->has_failure_location) { check->has_failure_location = true; check->failure_location = { samples[entry.sample].x, samples[entry.sample].y }; } }; if (config.zone_transition_m <= 0.0) { locate(pending.front()); return static_cast(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> 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(fy) * grid.width + static_cast(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) { locate(entry); ++violations; } } return violations; } bool kinkMetrics(const Path& path, double bend_limit_deg, int* kinks, double* max_bend_deg, const std::atomic* cancel, const std::chrono::steady_clock::time_point* deadline) { std::vector 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 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 > bend_limit_deg) ++over; } *kinks = spins + over; *max_bend_deg = largest; return true; } int selfCrossings(const Path& path, const std::atomic* cancel, const std::chrono::steady_clock::time_point* deadline) { constexpr double kStep = 0.1; constexpr std::size_t kIgnore = 3; std::vector distinct; std::vector 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 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* 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* 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; std::vector 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; const double bend_limit_deg = std::max({ kBendLimitDeg, 1.1 * kBendSpacingM / config.min_turn_radius_m * kRadToDeg, config.max_kink_deg > 0.0 ? config.max_kink_deg + 1e-6 : 0.0 }); if (!kinkMetrics(path, bend_limit_deg, &check.kinks, &check.max_bend_deg, cancel, deadline)) return check; const auto straightStep = [&path](std::size_t i) { return i + 1 < path.size() && std::hypot(path[i + 1].x - path[i].x, path[i + 1].y - path[i].y) > 1e-7 && std::abs(wrapAngle(path[i + 1].yaw - path[i].yaw)) < 1e-6; }; 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); // A sharp corner is one turning step between two straight steps; it is counted, not measured as a curve. const bool corner = kink_rad > 0.0 && distance > 1e-7 && std::abs(turn) > 1e-6 && std::abs(turn) <= kink_rad + 1e-9 && i > 0 && straightStep(i - 1) && straightStep(i + 1); if (corner) ++check.corners; else 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); } const int centre_violations = zoneViolations(a, b, grid, deviation_deg); check.zone_violations += centre_violations; if (centre_violations && !check.has_failure_location) { check.has_failure_location = true; check.failure_location = { (a.x + b.x) * 0.5, (a.y + b.y) * 0.5 }; } const int samples = std::max({ 1, static_cast(std::ceil(distance / 0.01)), static_cast(std::ceil(std::abs(turn) / 0.03)) }); if (corner && grid.direction != nullptr) { const int spin_samples = std::max(1, static_cast(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(std::ceil(distance / 0.04)), static_cast(std::ceil(std::abs(turn) / 0.03)) }); for (int j = i == 0 ? 0 : 1; j <= zone_steps; ++j) { const double t = static_cast(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(j) / samples; 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; if (!check.has_failure_location) { check.has_failure_location = true; check.failure_location = {x, y}; } } } } 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, &check); 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 && check.self_crossings == 0 && check.max_curvature <= 1.0 / config.min_turn_radius_m + 1e-6; return check; } } // namespace lane_route_core