CARLA
client/Map.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 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 #include "carla/client/Map.h"
8 
10 #include "carla/client/Waypoint.h"
12 #include "carla/road/Map.h"
13 #include "carla/road/RoadTypes.h"
15 
16 #include <sstream>
17 
18 namespace carla {
19 namespace client {
20 
21  static auto MakeMap(const std::string &opendrive_contents) {
22  auto stream = std::istringstream(opendrive_contents);
23  auto map = opendrive::OpenDriveParser::Load(stream.str());
24  if (!map.has_value()) {
25  throw_exception(std::runtime_error("failed to generate map"));
26  }
27  return std::move(*map);
28  }
29 
30  Map::Map(rpc::MapInfo description, std::string xodr_content)
31  : _description(std::move(description)),
32  _map(MakeMap(xodr_content)){
33  open_drive_file = xodr_content;
34  }
35  Map::Map(std::string name, std::string xodr_content)
36  : Map(rpc::MapInfo{
37  std::move(name),
38  std::vector<geom::Transform>{}}, xodr_content) {
39  open_drive_file = xodr_content;
40  }
41 
42  Map::~Map() = default;
43 
45  const geom::Location &location,
46  bool project_to_road,
47  int32_t lane_type) const {
48  boost::optional<road::element::Waypoint> waypoint;
49  if (project_to_road) {
50  waypoint = _map.GetClosestWaypointOnRoad(location, lane_type);
51  } else {
52  waypoint = _map.GetWaypoint(location, lane_type);
53  }
54  return waypoint.has_value() ?
55  SharedPtr<Waypoint>(new Waypoint{shared_from_this(), *waypoint}) :
56  nullptr;
57  }
58 
60  carla::road::RoadId road_id,
61  carla::road::LaneId lane_id,
62  float s) const {
63  boost::optional<road::element::Waypoint> waypoint;
64  waypoint = _map.GetWaypoint(road_id, lane_id, s);
65  return waypoint.has_value() ?
66  SharedPtr<Waypoint>(new Waypoint{shared_from_this(), *waypoint}) :
67  nullptr;
68  }
69 
71  namespace re = carla::road::element;
72  std::unordered_map<re::Waypoint, SharedPtr<Waypoint>> waypoints;
73 
74  auto get_or_make_waypoint = [&](const auto &waypoint) {
75  auto it = waypoints.find(waypoint);
76  if (it == waypoints.end()) {
77  it = waypoints.emplace(
78  waypoint,
79  SharedPtr<Waypoint>(new Waypoint{shared_from_this(), waypoint})).first;
80  }
81  return it->second;
82  };
83 
84  TopologyList result;
85  auto topology = _map.GenerateTopology();
86  result.reserve(topology.size());
87  for (const auto &pair : topology) {
88  result.emplace_back(
89  get_or_make_waypoint(pair.first),
90  get_or_make_waypoint(pair.second));
91  }
92  return result;
93  }
94 
95  std::vector<SharedPtr<Waypoint>> Map::GenerateWaypoints(double distance) const {
96  std::vector<SharedPtr<Waypoint>> result;
97  const auto waypoints = _map.GenerateWaypoints(distance);
98  result.reserve(waypoints.size());
99  for (const auto &waypoint : waypoints) {
100  result.emplace_back(SharedPtr<Waypoint>(new Waypoint{shared_from_this(), waypoint}));
101  }
102  return result;
103  }
104 
105  std::vector<road::element::LaneMarking> Map::CalculateCrossedLanes(
106  const geom::Location &origin,
107  const geom::Location &destination) const {
108  return _map.CalculateCrossedLanes(origin, destination);
109  }
110 
112  return _map.GetGeoReference();
113  }
114 
115  std::vector<geom::Location> Map::GetAllCrosswalkZones() const {
116  return _map.GetAllCrosswalkZones();
117  }
118 
120  const road::Junction *juncptr = GetMap().GetJunction(waypoint.GetJunctionId());
121  auto junction = SharedPtr<Junction>(new Junction(shared_from_this(), juncptr));
122  return junction;
123  }
124 
125  std::vector<std::pair<SharedPtr<Waypoint>, SharedPtr<Waypoint>>> Map::GetJunctionWaypoints(
126  road::JuncId id,
127  road::Lane::LaneType lane_type) const {
128  std::vector<std::pair<SharedPtr<Waypoint>, SharedPtr<Waypoint>>> result;
129  auto junction_waypoints = GetMap().GetJunctionWaypoints(id, lane_type);
130  for (auto &waypoint_pair : junction_waypoints) {
131  result.emplace_back(
132  std::make_pair(SharedPtr<Waypoint>(new Waypoint(shared_from_this(), waypoint_pair.first)),
133  SharedPtr<Waypoint>(new Waypoint(shared_from_this(), waypoint_pair.second))));
134  }
135  return result;
136  }
137 
138  std::vector<SharedPtr<Landmark>> Map::GetAllLandmarks() const {
139  std::vector<SharedPtr<Landmark>> result;
140  auto signal_references = _map.GetAllSignalReferences();
141  for(auto* signal_reference : signal_references) {
142  result.emplace_back(
143  new Landmark(nullptr, shared_from_this(), signal_reference, 0));
144  }
145  return result;
146  }
147 
148  std::vector<SharedPtr<Landmark>> Map::GetLandmarksFromId(std::string id) const {
149  std::vector<SharedPtr<Landmark>> result;
150  auto signal_references = _map.GetAllSignalReferences();
151  for(auto* signal_reference : signal_references) {
152  if(signal_reference->GetSignalId() == id) {
153  result.emplace_back(
154  new Landmark(nullptr, shared_from_this(), signal_reference, 0));
155  }
156  }
157  return result;
158  }
159 
160  std::vector<SharedPtr<Landmark>> Map::GetAllLandmarksOfType(std::string type) const {
161  std::vector<SharedPtr<Landmark>> result;
162  auto signal_references = _map.GetAllSignalReferences();
163  for(auto* signal_reference : signal_references) {
164  if(signal_reference->GetSignal()->GetType() == type) {
165  result.emplace_back(
166  new Landmark(nullptr, shared_from_this(), signal_reference, 0));
167  }
168  }
169  return result;
170  }
171 
172  std::vector<SharedPtr<Landmark>>
173  Map::GetLandmarkGroup(const Landmark &landmark) const {
174  std::vector<SharedPtr<Landmark>> result;
175  auto &controllers = landmark._signal->GetSignal()->GetControllers();
176  for (auto& controller_id : controllers) {
177  const auto &controller = _map.GetControllers().at(controller_id);
178  for(auto& signal_id : controller->GetSignals()) {
179  auto& signal = _map.GetSignals().at(signal_id);
180  auto new_landmarks = GetLandmarksFromId(signal->GetSignalId());
181  result.insert(result.end(), new_landmarks.begin(), new_landmarks.end());
182  }
183  }
184  return result;
185  }
186 
187  void Map::CookInMemoryMap(const std::string& path) const {
188  traffic_manager::InMemoryMap::Cook(shared_from_this(), path);
189  }
190 
191 } // namespace client
192 } // namespace carla
std::vector< element::LaneMarking > CalculateCrossedLanes(const geom::Location &origin, const geom::Location &destination) const
Definition: road/Map.cpp:449
std::vector< std::pair< SharedPtr< Waypoint >, SharedPtr< Waypoint > > > GetJunctionWaypoints(road::JuncId id, road::Lane::LaneType type) const
Returns a pair of waypoints (start and end) for each lane in the junction.
Definition: client/Map.cpp:125
uint32_t RoadId
Definition: RoadTypes.h:15
const geom::GeoLocation & GetGeoReference() const
Definition: client/Map.cpp:111
std::vector< std::pair< Waypoint, Waypoint > > GenerateTopology() const
Generate the minimum set of waypoints that define the topology of map.
Definition: road/Map.cpp:724
boost::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
Definition: road/Map.cpp:212
void CookInMemoryMap(const std::string &path) const
Cooks InMemoryMap used by the traffic manager.
Definition: client/Map.cpp:187
SharedPtr< Waypoint > GetWaypoint(const geom::Location &location, bool project_to_road=true, int32_t lane_type=static_cast< uint32_t >(road::Lane::LaneType::Driving)) const
Definition: client/Map.cpp:44
void throw_exception(const std::exception &e)
Definition: Carla.cpp:135
std::vector< SharedPtr< Waypoint > > GenerateWaypoints(double distance) const
Definition: client/Map.cpp:95
std::vector< geom::Location > GetAllCrosswalkZones() const
Returns a list of locations defining 2d areas, when a location is repeated an area is finished...
Definition: road/Map.cpp:455
const std::unordered_map< ContId, std::unique_ptr< Controller > > & GetControllers() const
Definition: road/Map.h:192
const road::Map & GetMap() const
Definition: client/Map.h:42
Junction * GetJunction(JuncId id)
Definition: road/Map.cpp:997
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
std::vector< const element::RoadInfoSignal * > GetAllSignalReferences() const
Return all RoadInfoSignal in the map.
Definition: road/Map.cpp:437
Class containing a reference to RoadInfoSignal.
Definition: Landmark.h:22
std::vector< std::pair< SharedPtr< Waypoint >, SharedPtr< Waypoint > >> TopologyList
Definition: client/Map.h:64
int32_t JuncId
Definition: RoadTypes.h:17
SharedPtr< Waypoint > GetWaypointXODR(carla::road::RoadId road_id, carla::road::LaneId lane_id, float s) const
Definition: client/Map.cpp:59
static boost::optional< road::Map > Load(const std::string &opendrive)
boost::optional< element::Waypoint > GetClosestWaypointOnRoad(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
======================================================================== – Geometry ----------------...
Definition: road/Map.cpp:165
std::vector< road::element::LaneMarking > CalculateCrossedLanes(const geom::Location &origin, const geom::Location &destination) const
Definition: client/Map.cpp:105
road::JuncId GetJunctionId() const
const Signal * GetSignal() const
const std::set< ContId > & GetControllers() const
Definition: Signal.h:176
LaneType
Can be used as flags.
Definition: Lane.h:29
int32_t LaneId
Definition: RoadTypes.h:19
std::vector< SharedPtr< Landmark > > GetAllLandmarksOfType(std::string type) const
Returns all the landmarks in the map of a specific type.
Definition: client/Map.cpp:160
std::vector< geom::Location > GetAllCrosswalkZones() const
Definition: client/Map.cpp:115
std::vector< SharedPtr< Landmark > > GetLandmarkGroup(const Landmark &landmark) const
Returns all the landmarks in the same group including this one.
Definition: client/Map.cpp:173
std::vector< SharedPtr< Landmark > > GetAllLandmarks() const
Returns all the larndmarks in the map.
Definition: client/Map.cpp:138
SharedPtr< Junction > GetJunction(const Waypoint &waypoint) const
Definition: client/Map.cpp:119
static auto MakeMap(const std::string &opendrive_contents)
Definition: client/Map.cpp:21
std::string open_drive_file
Definition: client/Map.h:102
const geom::GeoLocation & GetGeoReference() const
======================================================================== – Georeference ------------...
Definition: road/Map.h:46
TopologyList GetTopology() const
Definition: client/Map.cpp:70
const road::Map _map
Definition: client/Map.h:106
std::vector< Waypoint > GenerateWaypoints(double approx_distance) const
Generate all the waypoints in map separated by approx_distance.
Definition: road/Map.cpp:648
const road::element::RoadInfoSignal * _signal
Definition: Landmark.h:137
const std::unordered_map< SignId, std::unique_ptr< Signal > > & GetSignals() const
Definition: road/Map.h:188
std::vector< SharedPtr< Landmark > > GetLandmarksFromId(std::string id) const
Returns all the larndmarks in the map with a specific OpenDRIVE id.
Definition: client/Map.cpp:148
std::vector< std::pair< Waypoint, Waypoint > > GetJunctionWaypoints(JuncId id, Lane::LaneType lane_type) const
Generate waypoints of the junction.
Definition: road/Map.cpp:747
Map(rpc::MapInfo description, std::string xodr_content)
Definition: client/Map.cpp:30
carla::SharedPtr< carla::client::Junction > Junction