CARLA
Loading...
Searching...
No Matches
GlobalRoutePlanner.h
Go to the documentation of this file.
1// Copyright (c) 2018-2020 CVC.
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#pragma once
7
8#include "carla/Memory.h"
10#include "carla/geom/Location.h"
11#include "carla/geom/Vector3D.h"
12
13#include <array>
14#include <cstdint>
15#include <map>
16#include <optional>
17#include <tuple>
18#include <unordered_map>
19#include <utility>
20#include <vector>
21
22namespace carla {
23
24namespace client {
25 class Map;
26 class Waypoint;
27} // namespace client
28
29namespace agents {
30namespace navigation {
31
32 /// C++ port of agents.navigation.global_route_planner.GlobalRoutePlanner.
33 /// Builds a directed graph from carla::client::Map::GetTopology() and runs
34 /// A* search (euclidean heuristic, edge weight = `length`) to produce a
35 /// (waypoint, RoadOption) plan between two world locations.
37 public:
38
39 using RouteItem = std::pair<SharedPtr<client::Waypoint>, RoadOption>;
40
41 GlobalRoutePlanner(SharedPtr<client::Map> map, double sampling_resolution);
42
43 /// Returns the list of (waypoint, RoadOption) pairs from `origin` to
44 /// `destination`. Mirrors Python `trace_route`.
45 std::vector<RouteItem> TraceRoute(const geom::Location &origin,
46 const geom::Location &destination);
47
48 private:
49
50 using NodeId = int;
51 using XYZKey = std::tuple<double, double, double>;
52
53 struct Edge {
56 double length = 0.0; // matches Python `length`
57 std::vector<SharedPtr<client::Waypoint>> path;
60 std::optional<geom::Vector3D> entry_vector;
61 std::optional<geom::Vector3D> exit_vector;
62 std::optional<geom::Vector3D> net_vector;
63 bool intersection = false;
66 };
67
68 struct Node {
69 std::array<double, 3> vertex{{0.0, 0.0, 0.0}};
70 std::vector<NodeId> successors; // outgoing neighbors
71 };
72
80
81 // ---- pipeline stages (mirror Python `_build_*` / `_find_loose_ends` / `_lane_change_link`)
82 void BuildTopology();
83 void BuildGraph();
84 void FindLooseEnds();
85 void LaneChangeLink();
86
87 // ---- helpers (mirror Python `_localize`, `_path_search`, etc.)
88 std::optional<std::pair<NodeId, NodeId>> Localize(const geom::Location &loc) const;
89 std::vector<NodeId> PathSearch(const geom::Location &origin,
90 const geom::Location &destination);
91 double DistanceHeuristic(NodeId a, NodeId b) const;
92 std::pair<std::optional<NodeId>, const Edge *>
93 SuccessiveLastIntersectionEdge(std::size_t index,
94 const std::vector<NodeId> &route) const;
95 RoadOption TurnDecision(std::size_t index,
96 const std::vector<NodeId> &route);
97 static std::size_t FindClosestInList(
98 const SharedPtr<client::Waypoint> &current,
99 const std::vector<SharedPtr<client::Waypoint>> &list);
100
101 // ---- graph mutators
102 NodeId GetOrCreateNode(const XYZKey &key);
103 Edge &AddEdge(NodeId n1, NodeId n2);
104 const Edge *FindEdge(NodeId n1, NodeId n2) const;
105 Edge *FindEdge(NodeId n1, NodeId n2);
106
107 // ---- members
110 std::vector<TopologySegment> _topology;
111 std::unordered_map<NodeId, Node> _nodes;
112 std::map<std::pair<NodeId, NodeId>, Edge> _edges;
113 std::map<XYZKey, NodeId> _id_map;
114 // road_id -> section_id -> lane_id -> (n1, n2)
115 std::map<uint32_t,
116 std::map<uint32_t,
117 std::map<int32_t, std::pair<NodeId, NodeId>>>> _road_id_to_edge;
118
119 // Mutable per-call-sequence state — Python keeps these as instance attrs
120 // and never resets them between trace_route() calls. Preserve that.
123 };
124
125} // namespace navigation
126} // namespace agents
127} // namespace carla
C++ port of agents.navigation.global_route_planner.GlobalRoutePlanner.
std::tuple< double, double, double > XYZKey
double DistanceHeuristic(NodeId a, NodeId b) const
std::pair< SharedPtr< client::Waypoint >, RoadOption > RouteItem
RoadOption TurnDecision(std::size_t index, const std::vector< NodeId > &route)
std::map< std::pair< NodeId, NodeId >, Edge > _edges
std::vector< RouteItem > TraceRoute(const geom::Location &origin, const geom::Location &destination)
Returns the list of (waypoint, RoadOption) pairs from origin to destination.
std::unordered_map< NodeId, Node > _nodes
static std::size_t FindClosestInList(const SharedPtr< client::Waypoint > &current, const std::vector< SharedPtr< client::Waypoint > > &list)
const Edge * FindEdge(NodeId n1, NodeId n2) const
std::map< uint32_t, std::map< uint32_t, std::map< int32_t, std::pair< NodeId, NodeId > > > > _road_id_to_edge
std::pair< std::optional< NodeId >, const Edge * > SuccessiveLastIntersectionEdge(std::size_t index, const std::vector< NodeId > &route) const
std::optional< std::pair< NodeId, NodeId > > Localize(const geom::Location &loc) const
std::vector< NodeId > PathSearch(const geom::Location &origin, const geom::Location &destination)
RoadOption
Topological direction emitted by the planners.
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15
std::vector< SharedPtr< client::Waypoint > > path