CARLA
 
载入中...
搜索中...
未找到
client/Map.cpp
浏览该文件的文档.
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
12#include "carla/road/Map.h"
15
16#include <sstream>
17
18namespace carla {
19namespace 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
44 SharedPtr<Waypoint> Map::GetWaypoint(
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
59 SharedPtr<Waypoint> Map::GetWaypointXODR(
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
70 Map::TopologyList Map::GetTopology() const {
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
111 const geom::GeoLocation &Map::GetGeoReference() const {
112 return _map.GetGeoReference();
113 }
114
115 std::vector<geom::Location> Map::GetAllCrosswalkZones() const {
116 return _map.GetAllCrosswalkZones();
117 }
118
119 SharedPtr<Junction> Map::GetJunction(const Waypoint &waypoint) const {
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
Class containing a reference to RoadInfoSignal
Definition Landmark.h:22
const road::element::RoadInfoSignal * _signal
Definition Landmark.h:137
std::vector< std::pair< SharedPtr< Waypoint >, SharedPtr< Waypoint > > > TopologyList
Definition client/Map.h:64
std::string open_drive_file
Definition client/Map.h:102
Map(rpc::MapInfo description, std::string xodr_content)
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
road::JuncId GetJunctionId() const
static boost::optional< road::Map > Load(const std::string &opendrive)
LaneType
Can be used as flags
Definition Lane.h:29
const std::set< ContId > & GetControllers() const
Definition Signal.h:176
static void Cook(WorldMap world_map, const std::string &path)
static auto MakeMap(const std::string &opendrive_contents)
int32_t JuncId
Definition RoadTypes.h:17
int32_t LaneId
Definition RoadTypes.h:19
uint32_t RoadId
Definition RoadTypes.h:15
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
void throw_exception(const std::exception &e)
Definition Carla.cpp:135
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