Apollo 11.0
自动驾驶开放平台
obstacle_clusters.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 <algorithm>
20#include <limits>
21
23
24namespace apollo {
25namespace prediction {
26
27using ::apollo::hdmap::LaneInfo;
28
30 const double start_s, const double length, const bool consider_lane_split,
31 std::shared_ptr<const LaneInfo> lane_info_ptr) {
32 std::string lane_id = lane_info_ptr->id().id();
33 RoadGraph road_graph(start_s, length, consider_lane_split, lane_info_ptr);
34 LaneGraph lane_graph;
35 road_graph.BuildLaneGraph(&lane_graph);
36 return lane_graph;
37}
38
40 const double start_s, const double length, bool is_on_lane,
41 std::shared_ptr<const LaneInfo> lane_info_ptr) {
42 RoadGraph road_graph(start_s, length, true, lane_info_ptr);
43 LaneGraph lane_graph;
44 road_graph.BuildLaneGraphBidirection(&lane_graph);
45 return lane_graph;
46}
47
48void ObstacleClusters::AddObstacle(const int obstacle_id,
49 const std::string& lane_id,
50 const double lane_s, const double lane_l) {
51 LaneObstacle lane_obstacle;
52 lane_obstacle.set_obstacle_id(obstacle_id);
53 lane_obstacle.set_lane_id(lane_id);
54 lane_obstacle.set_lane_s(lane_s);
55 lane_obstacle.set_lane_l(lane_l);
56 lane_obstacles_[lane_id].push_back(std::move(lane_obstacle));
57}
58
60 lane_obstacles_.clear();
61}
62
64 for (auto iter = lane_obstacles_.begin(); iter != lane_obstacles_.end();
65 ++iter) {
66 std::sort(iter->second.begin(), iter->second.end(),
67 [](const LaneObstacle& obs0, const LaneObstacle& obs1) -> bool {
68 return obs0.lane_s() < obs1.lane_s();
69 });
70 }
71}
72
74 const LaneSequence& lane_sequence, const int obstacle_id,
75 const double obstacle_s, const double obstacle_l,
76 NearbyObstacle* const nearby_obstacle_ptr) {
77 double accumulated_s = 0.0;
78 for (const LaneSegment& lane_segment : lane_sequence.lane_segment()) {
79 std::string lane_id = lane_segment.lane_id();
80 double lane_length = lane_segment.total_length();
81 if (lane_obstacles_.find(lane_id) == lane_obstacles_.end() ||
82 lane_obstacles_[lane_id].empty()) {
83 accumulated_s += lane_length;
84 continue;
85 }
86 for (const LaneObstacle& lane_obstacle : lane_obstacles_[lane_id]) {
87 if (lane_obstacle.obstacle_id() == obstacle_id) {
88 continue;
89 }
90 double relative_s = accumulated_s + lane_obstacle.lane_s() - obstacle_s;
91 double relative_l = lane_obstacle.lane_l() - obstacle_l;
92 if (relative_s > 0.0) {
93 nearby_obstacle_ptr->set_id(lane_obstacle.obstacle_id());
94 nearby_obstacle_ptr->set_s(relative_s);
95 nearby_obstacle_ptr->set_l(relative_l);
96 return true;
97 }
98 }
99 }
100 return false;
101}
102
104 const LaneSequence& lane_sequence, const int obstacle_id,
105 const double obstacle_s, const double obstacle_l,
106 NearbyObstacle* const nearby_obstacle_ptr) {
107 if (lane_sequence.lane_segment().empty()) {
108 AERROR << "Empty lane sequence found.";
109 return false;
110 }
111 const LaneSegment& lane_segment = lane_sequence.lane_segment(0);
112 std::string lane_id = lane_segment.lane_id();
113
114 // Search current lane
115 if (lane_obstacles_.find(lane_id) != lane_obstacles_.end() &&
116 !lane_obstacles_[lane_id].empty()) {
117 for (int i = static_cast<int>(lane_obstacles_[lane_id].size()) - 1; i >= 0;
118 --i) {
119 const LaneObstacle& lane_obstacle = lane_obstacles_[lane_id][i];
120 if (lane_obstacle.obstacle_id() == obstacle_id) {
121 continue;
122 }
123 double relative_s = lane_obstacle.lane_s() - obstacle_s;
124 double relative_l = lane_obstacle.lane_l() - obstacle_l;
125 if (relative_s < 0.0) {
126 nearby_obstacle_ptr->set_id(lane_obstacle.obstacle_id());
127 nearby_obstacle_ptr->set_s(relative_s);
128 nearby_obstacle_ptr->set_l(relative_l);
129 return true;
130 }
131 }
132 }
133
134 // Search backward lanes
135 std::shared_ptr<const LaneInfo> lane_info_ptr =
137 bool found_one_behind = false;
138 double relative_s = -std::numeric_limits<double>::infinity();
139 double relative_l = 0.0;
140 for (const auto& predecessor_lane_id :
141 lane_info_ptr->lane().predecessor_id()) {
142 std::string lane_id = predecessor_lane_id.id();
143 if (lane_obstacles_.find(lane_id) == lane_obstacles_.end() ||
144 lane_obstacles_[lane_id].empty()) {
145 continue;
146 }
147 std::shared_ptr<const LaneInfo> pred_lane_info_ptr =
148 PredictionMap::LaneById(predecessor_lane_id.id());
149 const LaneObstacle& backward_obs = lane_obstacles_[lane_id].back();
150 double delta_s = backward_obs.lane_s() -
151 (obstacle_s + pred_lane_info_ptr->total_length());
152 found_one_behind = true;
153 if (delta_s > relative_s) {
154 relative_s = delta_s;
155 relative_l = backward_obs.lane_l() - obstacle_l;
156 nearby_obstacle_ptr->set_id(backward_obs.obstacle_id());
157 nearby_obstacle_ptr->set_s(relative_s);
158 nearby_obstacle_ptr->set_l(relative_l);
159 }
160 }
161
162 return found_one_behind;
163}
164
166 StopSign stop_sign;
167 // Find the stop_sign by lane_id in the hashtable
168 if (lane_id_stop_sign_map_.find(lane_id) != lane_id_stop_sign_map_.end()) {
169 return lane_id_stop_sign_map_[lane_id];
170 }
171 std::shared_ptr<const LaneInfo> lane_info_ptr =
173 CHECK_NOTNULL(lane_info_ptr);
174 for (const auto& overlap_id : lane_info_ptr->lane().overlap_id()) {
175 auto overlap_info_ptr = PredictionMap::OverlapById(overlap_id.id());
176 if (overlap_info_ptr == nullptr) {
177 continue;
178 }
179 for (const auto& object : overlap_info_ptr->overlap().object()) {
180 // find the overlap with stop_sign
181 if (object.has_stop_sign_overlap_info()) {
182 for (const auto& obj : overlap_info_ptr->overlap().object()) {
183 // find the obj of in the overlap
184 if (obj.has_lane_overlap_info()) {
185 if (!stop_sign.has_lane_s() ||
186 stop_sign.lane_s() > obj.lane_overlap_info().start_s()) {
187 stop_sign.set_stop_sign_id(object.id().id());
188 stop_sign.set_lane_id(lane_id);
189 stop_sign.set_lane_s(obj.lane_overlap_info().start_s());
190 lane_id_stop_sign_map_[lane_id] = stop_sign;
191 }
192 }
193 }
194 }
195 }
196 }
197 return lane_id_stop_sign_map_[lane_id];
198}
199
200} // namespace prediction
201} // namespace apollo
LaneGraph GetLaneGraph(const double start_s, const double length, const bool consider_lane_split, std::shared_ptr< const apollo::hdmap::LaneInfo > lane_info_ptr)
Obtain a lane graph given a lane info and s
bool ForwardNearbyObstacle(const LaneSequence &lane_sequence, const double s, LaneObstacle *const lane_obstacle)
Get the nearest obstacle on lane sequence at s
void AddObstacle(const int obstacle_id, const std::string &lane_id, const double lane_s, const double lane_l)
Add an obstacle into clusters
StopSign QueryStopSignByLaneId(const std::string &lane_id)
Query stop sign by lane ID
LaneGraph GetLaneGraphWithoutMemorizing(const double start_s, const double length, const bool is_on_lane, std::shared_ptr< const apollo::hdmap::LaneInfo > lane_info_ptr)
Obtain a lane graph given a lane info and s, but don't memorize it.
bool BackwardNearbyObstacle(const LaneSequence &lane_sequence, const int obstacle_id, const double obstacle_s, const double obstacle_l, NearbyObstacle *const nearby_obstacle_ptr)
Get the backward nearest obstacle on lane sequence at s
void SortObstacles()
Sort lane obstacles by lane s
static std::shared_ptr< const hdmap::LaneInfo > LaneById(const std::string &id)
Get a shared pointer to a lane by lane ID.
static std::shared_ptr< const hdmap::OverlapInfo > OverlapById(const std::string &id)
Get a shared pointer to an overlap by overlap ID.
common::Status BuildLaneGraph(LaneGraph *const lane_graph)
Build the lane graph.
Definition road_graph.cc:94
common::Status BuildLaneGraphBidirection(LaneGraph *const lane_graph_ptr)
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
repeated LaneSegment lane_segment