#ifndef LANE_ROUTE_CORE_LATTICE_INTERNAL_H_ #define LANE_ROUTE_CORE_LATTICE_INTERNAL_H_ #include #include #include #include #include #include #include #include #include #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 motion; double length; }; int latticeDubinsWords(const PathPoint& start, double x, double y, double yaw, double radius, std::array* 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(f) / width_m) : static_cast(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 samples; }; LatticeContext& context_; std::pmr::unordered_map tiles_; std::pmr::vector 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 values; std::pmr::vector 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_