CARLA
InMemoryMap.h
Go to the documentation of this file.
1 // Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include <chrono>
10 #include <memory>
11 #include <unordered_map>
12 #include <unordered_set>
13 
14 #if defined(__clang__)
15 # pragma clang diagnostic push
16 # pragma clang diagnostic ignored "-Wshadow"
17 #endif
18 #include "boost/geometry.hpp"
19 #include "boost/geometry/geometries/point.hpp"
20 #include "boost/geometry/index/rtree.hpp"
21 #if defined(__clang__)
22 # pragma clang diagnostic pop
23 #endif
24 
25 #include "carla/client/Map.h"
26 #include "carla/client/Waypoint.h"
27 #include "carla/geom/Location.h"
28 #include "carla/geom/Math.h"
29 #include "carla/Memory.h"
30 #include "carla/road/RoadTypes.h"
31 
35 
36 namespace carla {
37 namespace traffic_manager {
38 
39 namespace cg = carla::geom;
40 namespace cc = carla::client;
41 namespace crd = carla::road;
42 namespace bg = boost::geometry;
43 namespace bgi = boost::geometry::index;
44 
46  using SimpleWaypointPtr = std::shared_ptr<SimpleWaypoint>;
47  using NodeList = std::vector<SimpleWaypointPtr>;
50 
51  using Point3D = bg::model::point<float, 3, bg::cs::cartesian>;
52  using Box = bg::model::box<Point3D>;
53  using SpatialTreeEntry = std::pair<Point3D, SimpleWaypointPtr>;
54 
55  using SegmentId = std::tuple<crd::RoadId, crd::LaneId, crd::SectionId>;
56  using SegmentTopology = std::map<SegmentId, std::pair<std::vector<SegmentId>, std::vector<SegmentId>>>;
57  using SegmentMap = std::map<SegmentId, std::vector<SimpleWaypointPtr>>;
58  using Rtree = bgi::rtree<SpatialTreeEntry, bgi::rstar<16>>;
59 
60  /// This class builds a discretized local map-cache.
61  /// Instantiate the class with the world and run SetUp() to construct the
62  /// local map.
63  class InMemoryMap {
64 
65  private:
66 
67  /// Object to hold the world map received by the constructor.
69  /// Structure to hold all custom waypoint objects after interpolation of
70  /// sparse topology.
72  /// Spatial quadratic R-tree for indexing and querying waypoints.
74 
75  public:
76 
77  InMemoryMap(WorldMap world_map);
78  ~InMemoryMap();
79 
80  static void Cook(WorldMap world_map, const std::string& path);
81 
82  //bool Load(const std::string& filename);
83  bool Load(const std::vector<uint8_t>& content);
84 
85  /// This method constructs the local map with a resolution of sampling_resolution.
86  void SetUp();
87 
88  /// This method returns the closest waypoint to a given location on the map.
90 
91  /// This method returns n waypoints in an delta area with a certain distance from the ego vehicle.
92  NodeList GetWaypointsInDelta(const cg::Location loc, const uint16_t n_points, const float random_sample) const;
93 
94  /// This method returns the full list of discrete samples of the map in the local cache.
95  NodeList GetDenseTopology() const;
96 
97  std::string GetMapName();
98 
99  const cc::Map& GetMap() const;
100 
101  private:
102  void Save(const std::string& path);
103 
104  void SetUpDenseTopology();
105  void SetUpSpatialTree();
106  void SetUpRoadOption();
107 
108  /// This method is used to find and place lane change links.
109  void FindAndLinkLaneChange(SimpleWaypointPtr reference_waypoint);
110 
111  NodeList GetSuccessors(const SegmentId segment_id,
112  const SegmentTopology &segment_topology,
113  const SegmentMap &segment_map);
114  NodeList GetPredecessors(const SegmentId segment_id,
115  const SegmentTopology &segment_topology,
116  const SegmentMap &segment_map);
117 
118  /// Computes the segment id of a given waypoint.
119  /// The Id takes into account OpenDrive's road Id, lane Id and Section Id.
120  SegmentId GetSegmentId(const WaypointPtr &wp) const;
121  SegmentId GetSegmentId(const SimpleWaypointPtr &swp) const;
122  };
123 
124 } // namespace traffic_manager
125 } // namespace carla
SegmentId GetSegmentId(const WaypointPtr &wp) const
Computes the segment id of a given waypoint.
Definition: InMemoryMap.cpp:25
NodeList GetPredecessors(const SegmentId segment_id, const SegmentTopology &segment_topology, const SegmentMap &segment_map)
Definition: InMemoryMap.cpp:52
NodeList GetDenseTopology() const
This method returns the full list of discrete samples of the map in the local cache.
Rtree rtree
Spatial quadratic R-tree for indexing and querying waypoints.
Definition: InMemoryMap.h:73
bg::model::point< float, 3, bg::cs::cartesian > Point3D
Definition: InMemoryMap.h:51
bgi::rtree< SpatialTreeEntry, bgi::rstar< 16 > > Rtree
Definition: InMemoryMap.h:58
SimpleWaypointPtr GetWaypoint(const cg::Location loc) const
This method returns the closest waypoint to a given location on the map.
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
static void Cook(WorldMap world_map, const std::string &path)
Definition: InMemoryMap.cpp:71
NodeList GetSuccessors(const SegmentId segment_id, const SegmentTopology &segment_topology, const SegmentMap &segment_map)
Definition: InMemoryMap.cpp:33
void FindAndLinkLaneChange(SimpleWaypointPtr reference_waypoint)
This method is used to find and place lane change links.
bg::model::box< Point3D > Box
Definition: InMemoryMap.h:52
void Save(const std::string &path)
Definition: InMemoryMap.cpp:77
std::map< SegmentId, std::vector< SimpleWaypointPtr > > SegmentMap
Definition: InMemoryMap.h:57
std::map< SegmentId, std::pair< std::vector< SegmentId >, std::vector< SegmentId > >> SegmentTopology
Definition: InMemoryMap.h:56
carla::SharedPtr< cc::Waypoint > WaypointPtr
Definition: InMemoryMap.h:45
NodeList GetWaypointsInDelta(const cg::Location loc, const uint16_t n_points, const float random_sample) const
This method returns n waypoints in an delta area with a certain distance from the ego vehicle...
std::vector< SimpleWaypointPtr > NodeList
Definition: InMemoryMap.h:47
crd::JuncId GeoGridId
Definition: InMemoryMap.h:48
bool Load(const std::vector< uint8_t > &content)
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
std::pair< Point3D, SimpleWaypointPtr > SpatialTreeEntry
Definition: InMemoryMap.h:53
NodeList dense_topology
Structure to hold all custom waypoint objects after interpolation of sparse topology.
Definition: InMemoryMap.h:71
const cc::Map & GetMap() const
carla::SharedPtr< const cc::Map > WorldMap
Definition: InMemoryMap.h:49
This class builds a discretized local map-cache.
Definition: InMemoryMap.h:63
void SetUp()
This method constructs the local map with a resolution of sampling_resolution.
WorldMap _world_map
Object to hold the world map received by the constructor.
Definition: InMemoryMap.h:68
std::tuple< crd::RoadId, crd::LaneId, crd::SectionId > SegmentId
Definition: InMemoryMap.h:55