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// 命名空间 carla
18namespace carla {
19// 命名空间 client
20namespace client {
21// 静态函数 MakeMap,根据输入的 opendrive 内容生成地图
22static auto MakeMap(const std::string &opendrive_contents) {
23// 创建输入字符串流
24 auto stream = std::istringstream(opendrive_contents);
25 // 调用 OpenDriveParser 类的 Load 函数加载地图,返回 boost::optional<carla::road::Map>
26 auto map = opendrive::OpenDriveParser::Load(stream.str());
27 // 如果 map 为空,抛出运行时异常
28 if (!map.has_value()) {
29 throw_exception(std::runtime_error("failed to generate map"));
30 }
31// 移动 map 的值
32 return std::move(*map);
33 }
34 // Map 类的构造函数,接受 rpc::MapInfo 和 xodr 内容
35 Map::Map(rpc::MapInfo description, std::string xodr_content)
36 : _description(std::move(description)),
37 _map(MakeMap(xodr_content)){
38// 存储 xodr 内容
39 open_drive_file = xodr_content;
40 }
41// 另一个 Map 类的构造函数,接受名称和 xodr 内容
42 Map::Map(std::string name, std::string xodr_content)
43 : Map(rpc::MapInfo{
44 std::move(name),
45 std::vector<geom::Transform>{}}, xodr_content) {
46 open_drive_file = xodr_content;
47 }
48// Map 类的析构函数,使用默认析构函数
49 Map::~Map() = default;
50// 获取与给定位置相关的 Waypoint 的函数
51 SharedPtr<Waypoint> Map::GetWaypoint(
52 const geom::Location &location,
53 bool project_to_road,
54 int32_t lane_type) const {
55// 定义一个可选的 road::element::Waypoint 变量
56 boost::optional<road::element::Waypoint> waypoint;
57// 根据是否投影到道路选择不同的获取方式
58 if (project_to_road) {
59 waypoint = _map.GetClosestWaypointOnRoad(location, lane_type);
60 } else {
61 waypoint = _map.GetWaypoint(location, lane_type);
62 }
63 // 如果存在 waypoint,创建一个新的 Waypoint 并返回,否则返回 nullptr
64 return waypoint.has_value() ?
65 SharedPtr<Waypoint>(new Waypoint{shared_from_this(), *waypoint}) :
66 nullptr;
67 }
68// 根据道路 ID、车道 ID 和 s 坐标获取 Waypoint 的函数
69 SharedPtr<Waypoint> Map::GetWaypointXODR(
70 carla::road::RoadId road_id,
71 carla::road::LaneId lane_id,
72 float s) const {
73 // 定义一个可选的 road::element::Waypoint 变量
74 boost::optional<road::element::Waypoint> waypoint;
75// 调用 _map 的 GetWaypoint 函数获取 waypoint
76 waypoint = _map.GetWaypoint(road_id, lane_id, s);
77 // 如果存在 waypoint,创建一个新的 Waypoint 并返回,否则返回 nullptr
78 return waypoint.has_value() ?
79 SharedPtr<Waypoint>(new Waypoint{shared_from_this(), *waypoint}) :
80 nullptr;
81 }
82// 获取地图拓扑结构的函数
83 Map::TopologyList Map::GetTopology() const {
84// 为简洁使用 re 作为 carla::road::element 的别名
85 namespace re = carla::road::element;
86// 为简洁使用 re 作为 carla::road::element 的别名
87 std::unordered_map<re::Waypoint, SharedPtr<Waypoint>> waypoints;
88 // 定义一个 lambda 函数,用于获取或创建 Waypoint
89 auto get_or_make_waypoint = [&](const auto &waypoint) {
90 auto it = waypoints.find(waypoint);
91 if (it == waypoints.end()) {
92 it = waypoints.emplace(
93 waypoint,
94 SharedPtr<Waypoint>(new Waypoint{shared_from_this(), waypoint})).first;
95 }
96 return it->second;
97 };
98 // 存储拓扑结构的列表
99 TopologyList result;
100 // 生成拓扑结构
101 auto topology = _map.GenerateTopology();
102 // 为结果预留空间
103 result.reserve(topology.size());
104 // 遍历拓扑结构中的元素,创建 Waypoint 并添加到结果中
105 for (const auto &pair : topology) {
106 result.emplace_back(
107 get_or_make_waypoint(pair.first),
108 get_or_make_waypoint(pair.second));
109 }
110 return result;
111 }
112// 生成间隔一定距离的 Waypoint 列表的函数
113 std::vector<SharedPtr<Waypoint>> Map::GenerateWaypoints(double distance) const {
114 // 存储结果的 Waypoint 向量
115 std::vector<SharedPtr<Waypoint>> result;
116// 生成 Waypoint 列表
117 const auto waypoints = _map.GenerateWaypoints(distance);
118// 为结果预留空间
119 result.reserve(waypoints.size());
120 // 遍历生成的 Waypoint,创建并添加到结果中
121 for (const auto &waypoint : waypoints) {
122 result.emplace_back(SharedPtr<Waypoint>(new Waypoint{shared_from_this(), waypoint}));
123 }
124 return result;
125 }
126// 计算穿越车道的函数
127 std::vector<road::element::LaneMarking> Map::CalculateCrossedLanes(
128 const geom::Location &origin,
129 const geom::Location &destination) const {
130 // 调用 _map 的 CalculateCrossedLanes 函数
131 return _map.CalculateCrossedLanes(origin, destination);
132 }
133 // 获取地理参考的函数
134 const geom::GeoLocation &Map::GetGeoReference() const {
135// 调用 _map 的 GetGeoReference 函数
136 return _map.GetGeoReference();
137 }
138 // 获取所有人行横道区域的函数
139 std::vector<geom::Location> Map::GetAllCrosswalkZones() const {
140// 调用 _map 的 GetAllCrosswalkZones 函数
141 return _map.GetAllCrosswalkZones();
142 }
143// 获取与给定 Waypoint 相关的 Junction 的函数
144 SharedPtr<Junction> Map::GetJunction(const Waypoint &waypoint) const {
145// 获取 Junction 指针
146 const road::Junction *juncptr = GetMap().GetJunction(waypoint.GetJunctionId());
147// 创建新的 Junction 并返回
148 auto junction = SharedPtr<Junction>(new Junction(shared_from_this(), juncptr));
149 return junction;
150 }
151 // 获取 Junction 的 Waypoint 对的函数
152 std::vector<std::pair<SharedPtr<Waypoint>, SharedPtr<Waypoint>>> Map::GetJunctionWaypoints(
153 road::JuncId id,
154 road::Lane::LaneType lane_type) const {
155 // 存储结果的向量
156 std::vector<std::pair<SharedPtr<Waypoint>, SharedPtr<Waypoint>>> result;
157 // 获取 Junction 的 Waypoint 对
158 auto junction_waypoints = GetMap().GetJunctionWaypoints(id, lane_type);
159 // 遍历并创建新的 Waypoint 对添加到结果中
160 for (auto &waypoint_pair : junction_waypoints) {
161 result.emplace_back(
162 std::make_pair(SharedPtr<Waypoint>(new Waypoint(shared_from_this(), waypoint_pair.first)),
163 SharedPtr<Waypoint>(new Waypoint(shared_from_this(), waypoint_pair.second))));
164 }
165 return result;
166 }
167 // 获取所有地标的函数
168 std::vector<SharedPtr<Landmark>> Map::GetAllLandmarks() const {
169 // 存储结果的向量
170 std::vector<SharedPtr<Landmark>> result;
171 // 获取所有信号引用
172 auto signal_references = _map.GetAllSignalReferences();
173// 遍历信号引用,创建新的 Landmark 并添加到结果中
174 for(auto* signal_reference : signal_references) {
175 result.emplace_back(
176 new Landmark(nullptr, shared_from_this(), signal_reference, 0));
177 }
178 return result;
179 }
180// 从 ID 获取地标的函数
181 std::vector<SharedPtr<Landmark>> Map::GetLandmarksFromId(std::string id) const {
182// 存储结果的向量
183 std::vector<SharedPtr<Landmark>> result;
184 // 获取所有信号引用
185 auto signal_references = _map.GetAllSignalReferences();
186// 遍历信号引用,找到符合 ID 的创建新的 Landmark 并添加到结果中
187 for(auto* signal_reference : signal_references) {
188 if(signal_reference->GetSignalId() == id) {
189 result.emplace_back(
190 new Landmark(nullptr, shared_from_this(), signal_reference, 0));
191 }
192 }
193 return result;
194 }
195 // 从类型获取地标的函数
196 std::vector<SharedPtr<Landmark>> Map::GetAllLandmarksOfType(std::string type) const {
197 // 存储结果的向量
198 std::vector<SharedPtr<Landmark>> result;
199 // 获取所有信号引用
200 auto signal_references = _map.GetAllSignalReferences();
201 // 遍历信号引用,找到符合类型的创建新的 Landmark 并添加到结果中
202 for(auto* signal_reference : signal_references) {
203 if(signal_reference->GetSignal()->GetType() == type) {
204 result.emplace_back(
205 new Landmark(nullptr, shared_from_this(), signal_reference, 0));
206 }
207 }
208 return result;
209 }
210// 获取地标组的函数
211 std::vector<SharedPtr<Landmark>>
212 Map::GetLandmarkGroup(const Landmark &landmark) const {
213 // 存储结果的向量
214 std::vector<SharedPtr<Landmark>> result;
215 // 获取信号的控制器
216 auto &controllers = landmark._signal->GetSignal()->GetControllers();
217// 遍历控制器和控制器中的信号,添加新的 Landmark 到结果中
218 for (auto& controller_id : controllers) {
219 const auto &controller = _map.GetControllers().at(controller_id);
220 for(auto& signal_id : controller->GetSignals()) {
221 auto& signal = _map.GetSignals().at(signal_id);
222 auto new_landmarks = GetLandmarksFromId(signal->GetSignalId());
223 result.insert(result.end(), new_landmarks.begin(), new_landmarks.end());
224 }
225 }
226 return result;
227 }
228 // 烹饪内存中地图的函数
229 void Map::CookInMemoryMap(const std::string& path) const {
230// 调用 InMemoryMap 的 Cook 函数
231 traffic_manager::InMemoryMap::Cook(shared_from_this(), path);
232 }
233
234} // 结束 client 命名空间
235} // 结束 carla 命名空间
地图类的前向声明,用于在LaneInvasionSensor类中可能的引用。
static boost::optional< road::Map > Load(const std::string &opendrive)
LaneType
可以作为标志使用
Definition Lane.h:29
static auto MakeMap(const std::string &opendrive_contents)
int32_t JuncId
Definition RoadTypes.h:23
int32_t LaneId
Definition RoadTypes.h:26
uint32_t RoadId
Definition RoadTypes.h:20
std::vector< std::pair< WaypointPtr, WaypointPtr > > TopologyList
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
void throw_exception(const std::exception &e)
Definition Carla.cpp:142
boost::shared_ptr< T > SharedPtr
使用这个SharedPtr(boost::shared_ptr)以保持与boost::python的兼容性, 但未来如果可能的话,我们希望能为std::shared_ptr制作一个Python适配器。
Definition Memory.h:19
包含CARLA客户端相关类和函数的命名空间。