Apollo 11.0
自动驾驶开放平台
open_space_roi_util.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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
22
23#include <limits>
24#include <memory>
25#include <numeric>
26#include <utility>
27
29
30namespace apollo {
31namespace planning {
32
35
37 auto &obstacles_vertices_vec = *open_space_info->mutable_obstacles_vertices_vec();
38 auto &obstacles_edges_num_vec = *open_space_info->mutable_obstacles_edges_num();
39 size_t obstacles_vertices_vec_num = obstacles_vertices_vec.size();
40 obstacles_edges_num_vec.resize(obstacles_vertices_vec_num, 1);
41 for (size_t i = 0; i < obstacles_vertices_vec_num; i++) {
42 CHECK_GT(obstacles_vertices_vec[i].size(), 1U);
43 obstacles_edges_num_vec(i, 0) = static_cast<int>(obstacles_vertices_vec[i].size()) - 1;
44 }
45 open_space_info->set_obstacles_num(obstacles_vertices_vec.size());
46 // Transform vertices into the form of Ax>b
47 if (!LoadObstacleInHyperPlanes(open_space_info)) {
48 AERROR << "fail at LoadObstacleInHyperPlanes()";
49 return false;
50 }
51 return true;
52}
53
55 const std::vector<std::vector<common::math::Vec2d>> &roi_parking_boundary,
56 std::vector<double> *const XYBoundary) {
57 XYBoundary->clear();
58 XYBoundary->resize(4); // x_min x_max y_min y_max
59 *XYBoundary
60 = {std::numeric_limits<double>::max(),
61 std::numeric_limits<double>::lowest(),
62 std::numeric_limits<double>::max(),
63 std::numeric_limits<double>::lowest()};
64 for (auto &bound_vec : roi_parking_boundary) {
65 for (auto &pt : bound_vec) {
66 XYBoundary->at(0) = std::min<double>(XYBoundary->at(0), pt.x());
67 XYBoundary->at(1) = std::max<double>(XYBoundary->at(1), pt.x());
68 XYBoundary->at(2) = std::min<double>(XYBoundary->at(2), pt.y());
69 XYBoundary->at(3) = std::max<double>(XYBoundary->at(3), pt.y());
70 }
71 }
72}
73
75 const common::math::Vec2d &origin_point,
76 const double heading,
77 std::vector<std::vector<common::math::Vec2d>> *roi_parking_boundary) {
78 for (auto &bound_vec : *roi_parking_boundary) {
79 TransformByOriginPoint(origin_point, heading, &bound_vec);
80 }
81}
82
84 const common::math::Vec2d &origin_point,
85 const double heading,
86 std::vector<common::math::Vec2d> *roi_parking_boundary) {
87 for (auto &point : *roi_parking_boundary) {
88 TransformByOriginPoint(origin_point, heading, &point);
89 }
90}
91
93 const common::math::Vec2d &origin_point,
94 const double heading,
95 common::math::Vec2d *point) {
96 *point -= origin_point;
97 point->SelfRotate(-heading);
98}
99
101 const double filtering_distance,
102 const double perception_obstacle_buffer,
103 const std::vector<double> &xy_boundary,
104 Frame *const frame,
105 std::vector<std::vector<common::math::Vec2d>> *const roi_parking_boundary) {
106 auto obstacles_by_frame = frame->GetObstacleList();
107 size_t perception_obstacles_num = 0;
108 // load vertices for perception obstacles(repeat the first vertice at the
109 // last to form closed convex hull)
110 for (const auto &obstacle : obstacles_by_frame->Items()) {
111 if (FilterOutObstacle(filtering_distance, *frame, xy_boundary, *obstacle)) {
112 continue;
113 }
114 ++perception_obstacles_num;
115
116 Box2d original_box = obstacle->PerceptionBoundingBox();
117 original_box.LongitudinalExtend(perception_obstacle_buffer);
118 original_box.LateralExtend(perception_obstacle_buffer);
119
120 // TODO(Jinyun): Check correctness of ExpandByDistance() in polygon
121 // Polygon2d buffered_box(original_box);
122 // buffered_box = buffered_box.ExpandByDistance(
123 // config_.perception_obstacle_buffer());
124 // TODO(Runxin): Rotate from origin instead
125 // original_box.RotateFromCenter(-1.0 * origin_heading);
126 std::vector<Vec2d> vertices_ccw = original_box.GetAllCorners();
127 std::vector<Vec2d> vertices_cw;
128 while (!vertices_ccw.empty()) {
129 auto current_corner_pt = vertices_ccw.back();
130 vertices_cw.push_back(current_corner_pt);
131 vertices_ccw.pop_back();
132 }
133 // As the perception obstacle is a closed convex set, the first vertice
134 // is repeated at the end of the vector to help transform all four edges
135 // to inequality constraint
136 vertices_cw.push_back(vertices_cw.front());
137 roi_parking_boundary->push_back(vertices_cw);
138 }
139
140 return true;
141}
142
144 const double filtering_distance,
145 const Frame &frame,
146 const std::vector<double> &xy_boundary,
147 const Obstacle &obstacle) {
148 if (obstacle.IsVirtual() || !obstacle.IsStatic()) {
149 return true;
150 }
151
152 const auto &open_space_info = frame.open_space_info();
153 const auto &origin_point = open_space_info.origin_point();
154 const auto &origin_heading = open_space_info.origin_heading();
155 const auto &obstacle_box = obstacle.PerceptionBoundingBox();
156 auto obstacle_center_xy = obstacle_box.center();
157
158 // xy_boundary in xmin, xmax, ymin, ymax.
159 obstacle_center_xy -= origin_point;
160 obstacle_center_xy.SelfRotate(-origin_heading);
161 if (obstacle_center_xy.x() < xy_boundary[0] || obstacle_center_xy.x() > xy_boundary[1]
162 || obstacle_center_xy.y() < xy_boundary[2] || obstacle_center_xy.y() > xy_boundary[3]) {
163 return true;
164 }
165
166 // Translate the end pose back to world frame with endpose in x, y, phi, v
167 const auto &end_pose = open_space_info.open_space_end_pose();
168 Vec2d end_pose_x_y(end_pose[0], end_pose[1]);
169 end_pose_x_y.SelfRotate(origin_heading);
170 end_pose_x_y += origin_point;
171 const auto &vehicle_state = frame.vehicle_state();
172 // Get vehicle state
173 Vec2d vehicle_x_y(vehicle_state.x(), vehicle_state.y());
174
175 // Use vehicle position and end position to filter out obstacle
176 const double vehicle_center_to_obstacle = obstacle_box.DistanceTo(vehicle_x_y);
177 const double end_pose_center_to_obstacle = obstacle_box.DistanceTo(end_pose_x_y);
178 if (vehicle_center_to_obstacle > filtering_distance && end_pose_center_to_obstacle > filtering_distance) {
179 return true;
180 }
181 return false;
182}
183
185 *(open_space_info->mutable_obstacles_A()) = Eigen::MatrixXd::Zero(open_space_info->obstacles_edges_num().sum(), 2);
186 *(open_space_info->mutable_obstacles_b()) = Eigen::MatrixXd::Zero(open_space_info->obstacles_edges_num().sum(), 1);
187 // vertices using H-representation
188 if (!GetHyperPlanes(
189 open_space_info->obstacles_num(),
190 open_space_info->obstacles_edges_num(),
191 open_space_info->obstacles_vertices_vec(),
192 open_space_info->mutable_obstacles_A(),
193 open_space_info->mutable_obstacles_b())) {
194 AERROR << "Fail to present obstacle in hyperplane";
195 return false;
196 }
197 return true;
198}
199
201 const size_t &obstacles_num,
202 const Eigen::MatrixXi &obstacles_edges_num,
203 const std::vector<std::vector<Vec2d>> &obstacles_vertices_vec,
204 Eigen::MatrixXd *A_all,
205 Eigen::MatrixXd *b_all) {
206 if (obstacles_num != obstacles_vertices_vec.size()) {
207 AERROR << "obstacles_num != obstacles_vertices_vec.size()";
208 return false;
209 }
210
211 A_all->resize(obstacles_edges_num.sum(), 2);
212 b_all->resize(obstacles_edges_num.sum(), 1);
213
214 int counter = 0;
215 double kEpsilon = 1.0e-5;
216 // start building H representation
217 for (size_t i = 0; i < obstacles_num; ++i) {
218 size_t current_vertice_num = obstacles_edges_num(i, 0);
219 Eigen::MatrixXd A_i(current_vertice_num, 2);
220 Eigen::MatrixXd b_i(current_vertice_num, 1);
221
222 // take two subsequent vertices, and computer hyperplane
223 for (size_t j = 0; j < current_vertice_num; ++j) {
224 Vec2d v1 = obstacles_vertices_vec[i][j];
225 Vec2d v2 = obstacles_vertices_vec[i][j + 1];
226
227 Eigen::MatrixXd A_tmp(2, 1), b_tmp(1, 1), ab(2, 1);
228 // find hyperplane passing through v1 and v2
229 if (std::abs(v1.x() - v2.x()) < kEpsilon) {
230 if (v2.y() < v1.y()) {
231 A_tmp << 1, 0;
232 b_tmp << v1.x();
233 } else {
234 A_tmp << -1, 0;
235 b_tmp << -v1.x();
236 }
237 } else if (std::abs(v1.y() - v2.y()) < kEpsilon) {
238 if (v1.x() < v2.x()) {
239 A_tmp << 0, 1;
240 b_tmp << v1.y();
241 } else {
242 A_tmp << 0, -1;
243 b_tmp << -v1.y();
244 }
245 } else {
246 Eigen::MatrixXd tmp1(2, 2);
247 tmp1 << v1.x(), 1, v2.x(), 1;
248 Eigen::MatrixXd tmp2(2, 1);
249 tmp2 << v1.y(), v2.y();
250 ab = tmp1.inverse() * tmp2;
251 double a = ab(0, 0);
252 double b = ab(1, 0);
253
254 if (v1.x() < v2.x()) {
255 A_tmp << -a, 1;
256 b_tmp << b;
257 } else {
258 A_tmp << a, -1;
259 b_tmp << -b;
260 }
261 }
262
263 // store vertices
264 A_i.block(j, 0, 1, 2) = A_tmp.transpose();
265 b_i.block(j, 0, 1, 1) = b_tmp;
266 }
267
268 A_all->block(counter, 0, A_i.rows(), 2) = A_i;
269 b_all->block(counter, 0, b_i.rows(), 1) = b_i;
270 counter += static_cast<int>(current_vertice_num);
271 }
272 return true;
273}
274
276 const Vec2d &origin_point,
277 const double &origin_heading,
278 OpenSpaceInfo *open_space_info) {
279 open_space_info->set_origin_heading(origin_heading);
280 *open_space_info->mutable_origin_point() = origin_point;
281 auto obstacles_vertices_vec = open_space_info->mutable_obstacles_vertices_vec();
282 TransformByOriginPoint(origin_point, origin_heading, obstacles_vertices_vec);
283 auto end_pose_vec = open_space_info->mutable_open_space_end_pose();
284 Vec2d end_pose(end_pose_vec->at(0), end_pose_vec->at(1));
285 TransformByOriginPoint(origin_point, origin_heading, &end_pose);
286 end_pose_vec->at(0) = end_pose.x();
287 end_pose_vec->at(1) = end_pose.y();
288 end_pose_vec->at(2) -= origin_heading;
289 auto xy_boundary = open_space_info->mutable_ROI_xy_boundary();
290 GetRoiXYBoundary(*obstacles_vertices_vec, xy_boundary);
291}
292bool OpenSpaceRoiUtil::IsPolygonClockwise(const std::vector<Vec2d> &polygon) {
293 double s = 0;
294 for (size_t i = 0; i < polygon.size(); i++) {
295 if (i + 1 < polygon.size()) {
296 s += polygon.at(i).x() * polygon.at(i + 1).y() - polygon.at(i).y() * polygon.at(i + 1).x();
297 } else {
298 s += polygon.at(i).x() * polygon.at(0).y() - polygon.at(i).y() * polygon.at(0).x();
299 }
300 }
301 if (s < 0) {
302 return true;
303 } else {
304 return false;
305 }
306}
307
308bool OpenSpaceRoiUtil::AdjustPointsOrderToClockwise(std::vector<Vec2d> *polygon) {
309 if (!IsPolygonClockwise(*polygon)) {
310 // counter clockwise reverse it
311 ADEBUG << "point is anticlockwise,reverse";
312 std::reverse(polygon->begin(), polygon->end());
313 return true;
314 } else {
315 return false;
316 }
317}
318// make points order as left top ,right top, right bottom, left bottom
319bool OpenSpaceRoiUtil::UpdateParkingPointsOrder(const apollo::hdmap::Path &nearby_path, std::vector<Vec2d> *points) {
321 double min_dist = std::numeric_limits<double>::max();
322 size_t min_index = 0;
323 double sum_l = 0;
324 for (size_t i = 0; i < points->size(); i++) {
325 double s = 0.0, l = 0.0;
326 nearby_path.GetProjection(points->at(i), &s, &l);
327 sum_l += l;
328 ADEBUG << std::fixed << points->at(i).x() << "," << points->at(i).y() << "sl" << s << "," << l;
329 if (std::fabs(s) + 2.0 * std::fabs(l) < min_dist) {
330 min_dist = std::fabs(s) + 2.0 * std::fabs(l);
331 min_index = i;
332 }
333 }
334 std::vector<Vec2d> tmp_points(*points);
335 for (size_t i = 0; i < points->size(); i++) {
336 tmp_points[i] = points->at((i + min_index) % points->size());
337 }
338 if (sum_l < 0) {
339 for (size_t i = 0; i < points->size(); i++) {
340 points->at(i) = tmp_points[i];
341 }
342 } else {
343 points->at(0) = tmp_points[0];
344 for (size_t i = points->size() - 1; i > 0; i--) {
345 points->at(points->size() - i) = tmp_points[i];
346 }
347 }
348 return true;
349}
350
351} // namespace planning
352} // namespace apollo
Rectangular (undirected) bounding box in 2-D.
Definition box2d.h:52
void GetAllCorners(std::vector< Vec2d > *const corners) const
Getter of the corners of the box
Definition box2d.cc:140
void LateralExtend(const double extension_length)
Definition box2d.cc:412
const Vec2d & center() const
Getter of the center of the box
Definition box2d.h:106
void LongitudinalExtend(const double extension_length)
Extend the box longitudinally
Definition box2d.cc:406
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
void SelfRotate(const double angle)
rotate the vector itself by angle.
Definition vec2d.cc:70
double y() const
Getter for y component
Definition vec2d.h:57
double x() const
Getter for x component
Definition vec2d.h:54
bool GetProjection(const common::math::Vec2d &point, double *accumulate_s, double *lateral) const
Definition path.cc:739
Frame holds all data for one planning cycle.
Definition frame.h:62
const OpenSpaceInfo & open_space_info() const
Definition frame.h:167
const common::VehicleState & vehicle_state() const
Definition frame.cc:83
ThreadSafeIndexedObstacles * GetObstacleList()
Definition frame.h:165
This is the class that associates an Obstacle with its path properties.
Definition obstacle.h:62
const common::math::Box2d & PerceptionBoundingBox() const
Definition obstacle.h:90
std::vector< double > * mutable_ROI_xy_boundary()
Eigen::MatrixXd * mutable_obstacles_b()
const common::math::Vec2d & origin_point() const
Eigen::MatrixXd * mutable_obstacles_A()
std::vector< double > * mutable_open_space_end_pose()
const std::vector< std::vector< common::math::Vec2d > > & obstacles_vertices_vec() const
common::math::Vec2d * mutable_origin_point()
Eigen::MatrixXi * mutable_obstacles_edges_num()
std::vector< std::vector< common::math::Vec2d > > * mutable_obstacles_vertices_vec()
const Eigen::MatrixXi & obstacles_edges_num() const
void set_origin_heading(const double original_heading)
void set_obstacles_num(const size_t obstacles_num)
static bool AdjustPointsOrderToClockwise(std::vector< Vec2d > *polygon)
static bool LoadObstacleInHyperPlanes(OpenSpaceInfo *const open_space_info)
brief Transform the vertice presentation of the obstacles into linear inequality as Ax>b
static bool FormulateBoundaryConstraints(OpenSpaceInfo *const open_space_info)
main process to compute and load info needed by open space planner
static bool LoadObstacles(const double filtering_distance, const double perception_obstacle_buffer, const std::vector< double > &xy_boundary, Frame *const frame, std::vector< std::vector< common::math::Vec2d > > *const roi_parking_boundary)
Load obstacle polygon to roi_boundary in clock wise order
static bool UpdateParkingPointsOrder(const apollo::hdmap::Path &nearby_path, std::vector< Vec2d > *points)
static void TransformByOriginPoint(const common::math::Vec2d &origin_point, const double heading, std::vector< std::vector< common::math::Vec2d > > *roi_parking_boundary)
static bool IsPolygonClockwise(const std::vector< Vec2d > &polygon)
static void GetRoiXYBoundary(const std::vector< std::vector< common::math::Vec2d > > &roi_parking_boundary, std::vector< double > *const XYBoundary)
get XY boundary which is minmax x or y in roi boundary
static bool FilterOutObstacle(const double filtering_distance, const Frame &frame, const std::vector< double > &xy_boundary, const Obstacle &obstacle)
is obstacle can be filterd out, filtered by distance and roi XY bound
static bool GetHyperPlanes(const size_t &obstacles_num, const Eigen::MatrixXi &obstacles_edges_num, const std::vector< std::vector< Vec2d > > &obstacles_vertices_vec, Eigen::MatrixXd *A_all, Eigen::MatrixXd *b_all)
Planning module main class.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37