Files
lane_route_core/src/lattice_internal.h
duongtd d27cff23bd feat(lane_route): keep a preferred footprint gap on lattice paths (R2.5)
The lattice tier first finds a path with the minimum gap, as before, so
coverage cannot drop. If that path is not legal under
lattice_preferred_gap_m (0.15 m) away from the ends, a bounded upgrade
search (lattice_preferred_time_fraction of the rung slice) tries a wider
path and replaces it on success. Smoothing keeps the margin of the
returned path. PlanInfo, eval JSON and the plugin debug/fallback line
report the gap pass, the minimum gap and the upgrade time. The plugin
loads the preferred-gap and smoother keys.

regress7 (3 s): coverage equal to R2c (hook_150 1718, kit 1709),
graph-only output identical, middle gap >= 0.15 m on 133/153 and
128/190 lattice paths (R2c 88 and 89), Python recheck 0 bad.
2026-11-09 17:42:48 +07:00

172 lines
5.9 KiB
C++

#ifndef LANE_ROUTE_CORE_LATTICE_INTERNAL_H_
#define LANE_ROUTE_CORE_LATTICE_INTERNAL_H_
#include <array>
#include <chrono>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <memory_resource>
#include <new>
#include <unordered_map>
#include <vector>
#include "route_planner_internal.h"
namespace lane_route_core
{
namespace detail
{
// All persistent search containers use this resource, including temporary growth during reallocation.
class LatticeMemory final : public std::pmr::memory_resource
{
public:
explicit LatticeMemory(std::size_t limit) : limit_(limit) {}
std::size_t peak() const { return peak_; }
private:
void* do_allocate(std::size_t bytes, std::size_t alignment) override
{
if (bytes > limit_ - used_)
throw std::bad_alloc();
void* p = std::pmr::new_delete_resource()->allocate(bytes, alignment);
used_ += bytes;
if (used_ > peak_)
peak_ = used_;
return p;
}
void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override
{
std::pmr::new_delete_resource()->deallocate(p, bytes, alignment);
used_ -= bytes;
}
bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override { return this == &other; }
std::size_t limit_, used_ = 0, peak_ = 0;
};
struct LatticeContext
{
const RouteRequest& request;
const GridRef& grid;
const PlannerConfig& config;
double deviation_deg;
TimePoint deadline;
PlanInfo& info;
LatticeMemory& memory;
uint64_t* smooth_checks = nullptr;
uint64_t smooth_limit = 0;
double gap_scan_margin_m = 0.0; ///< [m] Additional gap reporting range; minimum search keeps the original scan.
bool cancelled() const { return request.cancel && request.cancel->load(std::memory_order_relaxed); }
bool expired() const { return cancelled() || Clock::now() >= deadline || (smooth_checks && *smooth_checks >= smooth_limit); }
};
struct LatticeTierInput
{
const RouteRequest& request;
const GridRef& grid;
const PlannerConfig& config;
double deviation_deg;
TimePoint deadline;
TimePoint smooth_deadline;
};
class LatticeCSpace;
struct LatticeMotion { double curvature; double length; };
struct LatticeDubinsWord { std::array<LatticeMotion, 3> motion; double length; };
int latticeDubinsWords(const PathPoint& start, double x, double y, double yaw, double radius,
std::array<LatticeDubinsWord, 6>* words);
bool latticeAppendMotion(LatticeContext& context, LatticeCSpace& space, const LatticeMotion& motion, Path* path, bool check = true);
bool latticeAppendSpin(LatticeContext& context, LatticeCSpace& space, double yaw, Path* path);
struct LatticeStateKey
{
int32_t x, y, heading;
bool operator==(const LatticeStateKey& other) const { return x == other.x && y == other.y && heading == other.heading; }
};
struct LatticeOpenEntry
{
long double priority;
uint64_t serial;
std::size_t node;
};
inline long double latticeBucketPriority(double f, double width_m)
{
return width_m > 0.0 ? std::floor(static_cast<long double>(f) / width_m) : static_cast<long double>(f);
}
struct LatticeOpenOrder
{
bool operator()(const LatticeOpenEntry& a, const LatticeOpenEntry& b) const
{
return a.priority != b.priority ? a.priority > b.priority : a.serial > b.serial;
}
};
double latticeMotionCost(double length, double clearance, int steer, int last_steer, int multiple, const PlannerConfig& config);
struct LatticePointConnector
{
double curvature, arc_length_m, straight_length_m, arrival_yaw;
};
bool latticePointConnector(const PathPoint& start, const Point2& goal, double radius, int turn,
LatticePointConnector* connector);
// One tile stores lazy C-space samples, not a cropped search window. A zero entry is unknown,
// one is conservatively free and two requires an exact check at the actual pose.
class LatticeCSpace
{
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 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); }
std::pmr::vector<uint8_t> samples;
};
LatticeContext& context_;
std::pmr::unordered_map<uint64_t, Tile> tiles_;
std::pmr::vector<uint32_t> blocked_prefix_;
bool prefix_ready_ = false;
double start_floor_ = 0.0; // [m] Gap margin allowed near the start, see endFloor().
double goal_floor_ = 0.0; // [m] Same near the goal.
double gap_margin_; // [m] Away from endpoints; changing it invalidates tile samples.
double endFloor(const Pose2& pose) const;
bool buildBlockedPrefix();
uint32_t blockedCount(int32_t x0, int32_t y0, int32_t x1, int32_t y1) const;
};
struct LatticeField
{
explicit LatticeField(std::pmr::memory_resource* resource) : values(resource), settled(resource) {}
std::pmr::vector<float> values;
std::pmr::vector<uint8_t> settled;
int32_t width = 0, height = 0;
int factor = 1;
double frontier = 0.0;
bool complete = false;
double at(double x, double y, const LatticeContext& context) const;
};
bool rejectLatticeCrossing(LatticeContext& context, const Path& path);
bool verifyLatticeCandidate(LatticeContext& context, const Path& path, PathCheck* check);
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline);
bool searchLattice(LatticeContext& context, LatticeCSpace& space, const LatticeField& field, Path* path, PathCheck* check);
void smoothLatticePath(LatticeContext& context, LatticeCSpace& space, Path* path, PathCheck* check);
bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output);
} // namespace detail
} // namespace lane_route_core
#endif // LANE_ROUTE_CORE_LATTICE_INTERNAL_H_