Apollo 11.0
自动驾驶开放平台
vector_net.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2021 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 *****************************************************************************/
17
18#include <cmath>
19#include <limits>
20#include <unordered_set>
21#include <utility>
22
23#include "cyber/common/file.h"
24
25namespace apollo {
26namespace prediction {
27template <typename Points>
28void VectorNet::GetOnePolyline(
29 const Points& points, double* start_length,
30 const common::PointENU& center_point, const double obstacle_phi,
31 ATTRIBUTE_TYPE attr_type, BOUNDARY_TYPE bound_type, const int count,
32 std::vector<std::vector<double>>* const one_polyline,
33 std::vector<double>* const one_p_id) {
34 size_t size = points.size();
35 std::vector<double> s(size, 0);
36
37 for (size_t i = 1; i < size; ++i) {
38 s[i] = std::hypot(points.at(i).x() - points.at(i - 1).x(),
39 points.at(i).y() - points.at(i - 1).y()) +
40 s[i - 1];
41 }
42
43 std::vector<double> x;
44 std::vector<double> y;
45 double cur_length = *start_length;
46
47 auto it_lower = std::lower_bound(s.begin(), s.end(), cur_length);
48 while (it_lower != s.end()) {
49 if (it_lower == s.begin()) {
50 x.push_back(points.at(0).x());
51 y.push_back(points.at(0).y());
52 } else {
53 const auto distance = std::distance(s.begin(), it_lower);
54 x.push_back(common::math::lerp(points.at(distance - 1).x(),
55 s[distance - 1], points.at(distance).x(),
56 s[distance], cur_length));
57 y.push_back(common::math::lerp(points.at(distance - 1).y(),
58 s[distance - 1], points.at(distance).y(),
59 s[distance], cur_length));
60 }
61 cur_length += FLAGS_point_distance;
62 it_lower = std::lower_bound(s.begin(), s.end(), cur_length);
63 }
64 size_t point_size = x.size();
65
66 *start_length = cur_length - s[size - 1];
67 if (point_size == 0) return;
68 const double attr = attribute_map.at(attr_type);
69 const double bound = boundary_map.at(bound_type);
70 auto last_point_after_rotate = common::math::RotateVector2d(
71 {x[0] - center_point.x(), y[0] - center_point.y()},
72 M_PI_2 - obstacle_phi);
73
74 for (size_t i = 1; i < point_size; ++i) {
75 if (one_p_id->at(0) > last_point_after_rotate.x()) {
76 one_p_id->at(0) = last_point_after_rotate.x();
77 }
78 if (one_p_id->at(1) > last_point_after_rotate.y()) {
79 one_p_id->at(1) = last_point_after_rotate.y();
80 }
81
82 std::vector<double> one_vector;
83
84 // d_s, d_e
85 one_vector.push_back(last_point_after_rotate.x());
86 one_vector.push_back(last_point_after_rotate.y());
87
88 Eigen::Vector2d point_after_rotate = common::math::RotateVector2d(
89 {x[i] - center_point.x(), y[i] - center_point.y()},
90 M_PI_2 - obstacle_phi);
91
92 one_vector.push_back(point_after_rotate.x());
93 one_vector.push_back(point_after_rotate.y());
94 last_point_after_rotate = std::move(point_after_rotate);
95
96 // attribute
97 one_vector.insert(one_vector.end(), {0.0, 0.0, attr, bound});
98
99 one_vector.push_back(count);
100 one_polyline->push_back(std::move(one_vector));
101 }
102}
103
104bool VectorNet::query(const common::PointENU& center_point,
105 const double obstacle_phi,
106 FeatureVector* const feature_ptr,
107 PidVector* const p_id_ptr) {
108 CHECK_NOTNULL(feature_ptr);
109 count_ = 0;
110 GetRoads(center_point, obstacle_phi, feature_ptr, p_id_ptr);
111 GetLanes(center_point, obstacle_phi, feature_ptr, p_id_ptr);
112 GetJunctions(center_point, obstacle_phi, feature_ptr, p_id_ptr);
113 GetCrosswalks(center_point, obstacle_phi, feature_ptr, p_id_ptr);
114 return true;
115}
116
117bool VectorNet::offline_query(const double obstacle_x, const double obstacle_y,
118 const double obstacle_phi) {
119 return offline_query(obstacle_x,
120 obstacle_y,
121 obstacle_phi,
122 FLAGS_prediction_target_file);
123}
124
125bool VectorNet::offline_query(const double obstacle_x,
126 const double obstacle_y,
127 const double obstacle_phi,
128 const std::string file_name) {
129 FeatureVector offline_feature;
130 PidVector p_id;
131 common::PointENU center_point =
132 common::util::PointFactory::ToPointENU(obstacle_x, obstacle_y);
133 query(center_point, obstacle_phi, &offline_feature, &p_id);
134
136 vector_net_pb_.mutable_car_position()->set_x(obstacle_x);
137 vector_net_pb_.mutable_car_position()->set_y(obstacle_y);
138 vector_net_pb_.mutable_car_position()->set_phi(obstacle_phi);
139
140 size_t i = 0;
141 for (const auto& polyline : offline_feature) {
142 auto* polyline_pb = vector_net_pb_.add_polyline();
143 polyline_pb->set_p_id_x(p_id[i][0]);
144 polyline_pb->set_p_id_y(p_id[i][1]);
145 i++;
146 for (const auto& vector : polyline) {
147 auto* vector_pb = polyline_pb->add_vector();
148 for (const auto& element : vector) {
149 vector_pb->add_element(element);
150 }
151 }
152 }
154 file_name);
155
156 return true;
157}
158
159void VectorNet::GetRoads(const common::PointENU& center_point,
160 const double obstacle_phi,
161 FeatureVector* const feature_ptr,
162 PidVector* const p_id_ptr) {
163 std::vector<apollo::hdmap::RoadInfoConstPtr> roads;
165 FLAGS_road_distance, &roads);
166
167 for (const auto& road : roads) {
168 for (const auto& section : road->road().section()) {
169 for (const auto& edge : section.boundary().outer_polygon().edge()) {
170 std::vector<std::vector<double>> one_polyline;
171 std::vector<double> one_p_id{std::numeric_limits<float>::max(),
172 std::numeric_limits<float>::max()};
173 double start_length = 0;
174 BOUNDARY_TYPE bound_type = UNKNOW;
175 if (edge.type() == hdmap::BoundaryEdge::LEFT_BOUNDARY) {
176 bound_type = LEFT_BOUNDARY;
177 } else if (edge.type() == hdmap::BoundaryEdge::RIGHT_BOUNDARY) {
178 bound_type = RIGHT_BOUNDARY;
179 } else if (edge.type() == hdmap::BoundaryEdge::NORMAL) {
180 bound_type = NORMAL;
181 } else {
182 bound_type = UNKNOW;
183 }
184
185 for (const auto& segment : edge.curve().segment()) {
186 GetOnePolyline(segment.line_segment().point(), &start_length,
187 center_point, obstacle_phi, ROAD, bound_type, count_,
188 &one_polyline, &one_p_id);
189 }
190 if (one_polyline.size() == 0) continue;
191
192 feature_ptr->push_back(std::move(one_polyline));
193 p_id_ptr->push_back(std::move(one_p_id));
194 ++count_;
195 }
196 }
197 }
198}
199
200void VectorNet::GetLaneQueue(
201 const std::vector<hdmap::LaneInfoConstPtr>& lanes,
202 std::vector<std::deque<hdmap::LaneInfoConstPtr>>* const lane_deque_ptr) {
203 std::unordered_set<hdmap::LaneInfoConstPtr> lane_set(lanes.begin(),
204 lanes.end());
205
206 while (!lane_set.empty()) {
207 std::deque<apollo::hdmap::LaneInfoConstPtr> one_lane_deque;
208 auto cur_lane = *lane_set.begin();
209 lane_set.erase(lane_set.begin());
210 one_lane_deque.push_back(cur_lane);
211 while (cur_lane->lane().successor_id_size() > 0) {
212 auto id = cur_lane->lane().successor_id(0);
214 if (lane_set.find(cur_lane) != lane_set.end()) {
215 lane_set.erase(cur_lane);
216 one_lane_deque.push_back(cur_lane);
217 } else {
218 break;
219 }
220 }
221
222 cur_lane = one_lane_deque.front();
223 while (cur_lane->lane().predecessor_id_size() > 0) {
224 auto id = cur_lane->lane().predecessor_id(0);
226 if (lane_set.find(cur_lane) != lane_set.end()) {
227 lane_set.erase(cur_lane);
228 one_lane_deque.push_front(cur_lane);
229 } else {
230 break;
231 }
232 }
233
234 lane_deque_ptr->push_back(one_lane_deque);
235 }
236}
237
238void VectorNet::GetLanes(const common::PointENU& center_point,
239 const double obstacle_phi,
240 FeatureVector* const feature_ptr,
241 PidVector* const p_id_ptr) {
242 std::vector<apollo::hdmap::LaneInfoConstPtr> lanes;
244 FLAGS_road_distance, &lanes);
245
246 std::vector<std::deque<apollo::hdmap::LaneInfoConstPtr>> lane_deque_vector;
247 GetLaneQueue(lanes, &lane_deque_vector);
248
249 for (const auto& lane_deque : lane_deque_vector) {
250 // Draw lane's left_boundary
251 std::vector<std::vector<double>> left_polyline;
252 std::vector<double> left_p_id{std::numeric_limits<float>::max(),
253 std::numeric_limits<float>::max()};
254 double start_length = 0;
255 for (const auto& lane : lane_deque) {
256 std::cout << lane->lane().id().id() << " ";
257 // if (lane->lane().left_boundary().virtual_()) continue;
258 for (const auto& segment :
259 lane->lane().left_boundary().curve().segment()) {
260 auto bound_type =
261 lane->lane().left_boundary().boundary_type(0).types(0);
262 GetOnePolyline(segment.line_segment().point(), &start_length,
263 center_point, obstacle_phi, lane_attr_map.at(bound_type),
264 LEFT_BOUNDARY, count_, &left_polyline, &left_p_id);
265 }
266 }
267
268 if (left_polyline.size() < 2) continue;
269 feature_ptr->push_back(std::move(left_polyline));
270 p_id_ptr->push_back(std::move(left_p_id));
271 ++count_;
272
273 std::vector<std::vector<double>> right_polyline;
274 std::vector<double> right_p_id{std::numeric_limits<float>::max(),
275 std::numeric_limits<float>::max()};
276 start_length = 0;
277 // Draw lane's right_boundary
278 for (const auto& lane : lane_deque) {
279 // if (lane->lane().right_boundary().virtual_()) continue;
280 for (const auto& segment :
281 lane->lane().right_boundary().curve().segment()) {
282 auto bound_type =
283 lane->lane().left_boundary().boundary_type(0).types(0);
284 GetOnePolyline(segment.line_segment().point(), &start_length,
285 center_point, obstacle_phi, lane_attr_map.at(bound_type),
286 RIGHT_BOUNDARY, count_, &right_polyline, &right_p_id);
287 }
288 }
289
290 if (right_polyline.size() < 2) continue;
291 feature_ptr->push_back(std::move(right_polyline));
292 p_id_ptr->push_back(std::move(right_p_id));
293 ++count_;
294 }
295}
296
297void VectorNet::GetJunctions(const common::PointENU& center_point,
298 const double obstacle_phi,
299 FeatureVector* const feature_ptr,
300 PidVector* const p_id_ptr) {
301 std::vector<apollo::hdmap::JunctionInfoConstPtr> junctions;
303 center_point, FLAGS_road_distance, &junctions);
304 for (const auto& junction : junctions) {
305 std::vector<std::vector<double>> one_polyline;
306 std::vector<double> one_p_id{std::numeric_limits<float>::max(),
307 std::numeric_limits<float>::max()};
308 double start_length = 0;
309 GetOnePolyline(junction->junction().polygon().point(), &start_length,
310 center_point, obstacle_phi, JUNCTION, UNKNOW, count_,
311 &one_polyline, &one_p_id);
312
313 feature_ptr->push_back(std::move(one_polyline));
314 p_id_ptr->push_back(std::move(one_p_id));
315 ++count_;
316 }
317}
318
319void VectorNet::GetCrosswalks(const common::PointENU& center_point,
320 const double obstacle_phi,
321 FeatureVector* const feature_ptr,
322 PidVector* const p_id_ptr) {
323 std::vector<apollo::hdmap::CrosswalkInfoConstPtr> crosswalks;
325 center_point, FLAGS_road_distance, &crosswalks);
326 for (const auto& crosswalk : crosswalks) {
327 std::vector<std::vector<double>> one_polyline;
328 std::vector<double> one_p_id{std::numeric_limits<float>::max(),
329 std::numeric_limits<float>::max()};
330 double start_length = 0;
331 GetOnePolyline(crosswalk->crosswalk().polygon().point(), &start_length,
332 center_point, obstacle_phi, CROSSWALK, UNKNOW, count_,
333 &one_polyline, &one_p_id);
334
335 feature_ptr->push_back(std::move(one_polyline));
336 p_id_ptr->push_back(std::move(one_p_id));
337 ++count_;
338 }
339}
340} // namespace prediction
341} // namespace apollo
static PointENU ToPointENU(const double x, const double y, const double z=0)
static const HDMap & BaseMap()
int GetCrosswalks(const apollo::common::PointENU &point, double distance, std::vector< CrosswalkInfoConstPtr > *crosswalks) const
get all crosswalks in certain range
Definition hdmap.cc:116
int GetLanes(const apollo::common::PointENU &point, double distance, std::vector< LaneInfoConstPtr > *lanes) const
get all lanes in certain range
Definition hdmap.cc:90
LaneInfoConstPtr GetLaneById(const Id &id) const
Definition hdmap.cc:34
int GetRoads(const apollo::common::PointENU &point, double distance, std::vector< RoadInfoConstPtr > *roads) const
get all roads in certain range
Definition hdmap.cc:144
int GetJunctions(const apollo::common::PointENU &point, double distance, std::vector< JunctionInfoConstPtr > *junctions) const
get all junctions in certain range
Definition hdmap.cc:95
bool offline_query(const double obstacle_x, const double obstacle_y, const double obstacle_phi)
bool query(const common::PointENU &center_point, const double obstacle_phi, FeatureVector *const feature_ptr, PidVector *const p_id_ptr)
T lerp(const T &x0, const double t0, const T &x1, const double t1, const double t)
Linear interpolation between two points of type T.
Eigen::Vector2d RotateVector2d(const Eigen::Vector2d &v_in, const double theta)
Definition math_utils.cc:82
bool SetProtoToASCIIFile(const google::protobuf::Message &message, int file_descriptor)
Definition file.cc:43
T bound(const T &x, const T &min, const T &max)
Get bound of value x
Definition misc.h:98
std::vector< std::vector< double > > PidVector
Definition vector_net.h:34
std::vector< std::vector< std::vector< double > > > FeatureVector
Definition vector_net.h:33
class register implement
Definition arena_queue.h:37