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