Apollo 10.0
自动驾驶开放平台
topo_graph.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <utility>
20
21namespace apollo {
22namespace routing {
23
24void TopoGraph::Clear() {
25 topo_nodes_.clear();
26 topo_edges_.clear();
27 node_index_map_.clear();
28}
29
30bool TopoGraph::LoadNodes(const Graph& graph) {
31 if (graph.node().empty()) {
32 AERROR << "No nodes found in topology graph.";
33 return false;
34 }
35 for (const auto& node : graph.node()) {
36 node_index_map_[node.lane_id()] = static_cast<int>(topo_nodes_.size());
37 std::shared_ptr<TopoNode> topo_node;
38 topo_node.reset(new TopoNode(node));
39 road_node_map_[node.road_id()].insert(topo_node.get());
40 topo_nodes_.push_back(std::move(topo_node));
41 }
42 return true;
43}
44
45// Need to execute load_nodes() firstly
46bool TopoGraph::LoadEdges(const Graph& graph) {
47 if (graph.edge().empty()) {
48 AINFO << "0 edges found in topology graph, but it's fine";
49 return true;
50 }
51 for (const auto& edge : graph.edge()) {
52 const std::string& from_lane_id = edge.from_lane_id();
53 const std::string& to_lane_id = edge.to_lane_id();
54 if (node_index_map_.count(from_lane_id) != 1 ||
55 node_index_map_.count(to_lane_id) != 1) {
56 return false;
57 }
58 std::shared_ptr<TopoEdge> topo_edge;
59 TopoNode* from_node = topo_nodes_[node_index_map_[from_lane_id]].get();
60 TopoNode* to_node = topo_nodes_[node_index_map_[to_lane_id]].get();
61 topo_edge.reset(new TopoEdge(edge, from_node, to_node));
62 from_node->AddOutEdge(topo_edge.get());
63 to_node->AddInEdge(topo_edge.get());
64 topo_edges_.push_back(std::move(topo_edge));
65 }
66 return true;
67}
68
69bool TopoGraph::LoadGraph(const Graph& graph) {
70 Clear();
71
72 map_version_ = graph.hdmap_version();
73 map_district_ = graph.hdmap_district();
74
75 if (!LoadNodes(graph)) {
76 AERROR << "Failed to load nodes from topology graph.";
77 return false;
78 }
79 if (!LoadEdges(graph)) {
80 AERROR << "Failed to load edges from topology graph.";
81 return false;
82 }
83 AINFO << "Load Topo data successful.";
84 return true;
85}
86
87const std::string& TopoGraph::MapVersion() const { return map_version_; }
88
89const std::string& TopoGraph::MapDistrict() const { return map_district_; }
90
91const TopoNode* TopoGraph::GetNode(const std::string& id) const {
92 const auto& iter = node_index_map_.find(id);
93 if (iter == node_index_map_.end()) {
94 return nullptr;
95 }
96 return topo_nodes_[iter->second].get();
97}
98
100 const std::string& road_id,
101 std::unordered_set<const TopoNode*>* const node_in_road) const {
102 const auto& iter = road_node_map_.find(road_id);
103 if (iter != road_node_map_.end()) {
104 node_in_road->insert(iter->second.begin(), iter->second.end());
105 }
106}
107
108} // namespace routing
109} // namespace apollo
const TopoNode * topo_node
const std::string & MapVersion() const
Definition topo_graph.cc:87
void GetNodesByRoadId(const std::string &road_id, std::unordered_set< const TopoNode * > *const node_in_road) const
Definition topo_graph.cc:99
const std::string & MapDistrict() const
Definition topo_graph.cc:89
bool LoadGraph(const Graph &filename)
Definition topo_graph.cc:69
const TopoNode * GetNode(const std::string &id) const
Definition topo_graph.cc:91
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
optional string hdmap_district
optional string hdmap_version