Apollo 9.0
自动驾驶开放平台
apollo::common::math::Polygon2d类 参考

The class of polygon in 2-D. 更多...

#include <polygon2d.h>

类 apollo::common::math::Polygon2d 继承关系图:
apollo::common::math::Polygon2d 的协作图:

Public 成员函数

 Polygon2d ()=default
 Empty constructor.
 
 Polygon2d (const Box2d &box)
 Constructor which takes a box.
 
 Polygon2d (std::vector< Vec2d > points, bool check_area=true)
 Constructor which takes a vector of points as its vertices.
 
const std::vector< Vec2d > & points () const
 Get the vertices of the polygon.
 
const std::vector< LineSegment2d > & line_segments () const
 Get the edges of the polygon.
 
int num_points () const
 Get the number of vertices of the polygon.
 
bool is_convex () const
 Check if the polygon is convex.
 
double area () const
 Get the area of the polygon.
 
double DistanceToBoundary (const Vec2d &point) const
 Compute the distance from a point to the boundary of the polygon.
 
double DistanceTo (const Vec2d &point) const
 Compute the distance from a point to the polygon.
 
double DistanceTo (const LineSegment2d &line_segment) const
 Compute the distance from a line segment to the polygon.
 
double DistanceTo (const Box2d &box) const
 Compute the distance from a box to the polygon.
 
double DistanceTo (const Polygon2d &polygon) const
 Compute the distance from another polygon to the polygon.
 
double DistanceSquareTo (const Vec2d &point) const
 Compute the square of distance from a point to the polygon.
 
bool IsPointIn (const Vec2d &point) const
 Check if a point is within the polygon.
 
bool IsPointOnBoundary (const Vec2d &point) const
 Check if a point is on the boundary of the polygon.
 
bool Contains (const LineSegment2d &line_segment) const
 Check if the polygon contains a line segment.
 
bool Contains (const Polygon2d &polygon) const
 Check if the polygon contains another polygon.
 
bool HasOverlap (const LineSegment2d &line_segment) const
 Check if a line segment has overlap with this polygon.
 
bool GetOverlap (const LineSegment2d &line_segment, Vec2d *const first, Vec2d *const last) const
 Get the overlap of a line segment and this polygon.
 
void GetAllVertices (std::vector< Vec2d > *const vertices) const
 Get all vertices of the polygon
 
std::vector< Vec2d > GetAllVertices () const
 Get all vertices of the polygon
 
std::vector< LineSegment2d > GetAllOverlaps (const LineSegment2d &line_segment) const
 Get all overlapped line segments of a line segment and this polygon.
 
bool HasOverlap (const Polygon2d &polygon) const
 Check if this polygon has overlap with another polygon.
 
bool ComputeOverlap (const Polygon2d &other_polygon, Polygon2d *const overlap_polygon) const
 Compute the overlap of this polygon and the other polygon if any.
 
double ComputeIoU (const Polygon2d &other_polygon) const
 Compute intersection over union ratio of this polygon and the other polygon.
 
AABox2d AABoundingBox () const
 Get the axis-aligned bound box of the polygon.
 
Box2d BoundingBoxWithHeading (const double heading) const
 Get the bound box according to a heading.
 
Box2d MinAreaBoundingBox () const
 Get the bounding box with the minimal area.
 
void ExtremePoints (const double heading, Vec2d *const first, Vec2d *const last) const
 Get the extreme points along a heading direction.
 
Polygon2d ExpandByDistance (const double distance) const
 Expand this polygon by a distance.
 
Polygon2d PolygonExpandByDistance (const double distance) const
 
void CalculateVertices (const Vec2d &shift_vec)
 
std::string DebugString () const
 Get a string containing essential information about the polygon for debugging purpose.
 
double min_x () const
 
double max_x () const
 
double min_y () const
 
double max_y () const
 
LineSegment2d MinLineSegment () const
 

静态 Public 成员函数

static bool ComputeConvexHull (const std::vector< Vec2d > &points, Polygon2d *const polygon, bool check_area=true)
 Compute the convex hull of a group of points.
 

Protected 成员函数

void BuildFromPoints (bool check_area=true)
 
int Next (int at) const
 
int Prev (int at) const
 

静态 Protected 成员函数

static bool ClipConvexHull (const LineSegment2d &line_segment, std::vector< Vec2d > *const points)
 

Protected 属性

std::vector< Vec2d > points_
 
int num_points_ = 0
 
std::vector< LineSegment2d > line_segments_
 
bool is_convex_ = false
 
double area_ = 0.0
 
double min_x_ = 0.0
 
double max_x_ = 0.0
 
double min_y_ = 0.0
 
double max_y_ = 0.0
 

详细描述

The class of polygon in 2-D.

在文件 polygon2d.h 第 42 行定义.

构造及析构函数说明

◆ Polygon2d() [1/3]

apollo::common::math::Polygon2d::Polygon2d ( )
default

Empty constructor.

◆ Polygon2d() [2/3]

apollo::common::math::Polygon2d::Polygon2d ( const Box2d &  box)
explicit

Constructor which takes a box.

参数
boxThe box to construct the polygon.

在文件 polygon2d.cc 第 35 行定义.

35 {
36 box.GetAllCorners(&points_);
38}
std::vector< Vec2d > points_
Definition polygon2d.h:333
void BuildFromPoints(bool check_area=true)
Definition polygon2d.cc:214

◆ Polygon2d() [3/3]

apollo::common::math::Polygon2d::Polygon2d ( std::vector< Vec2d >  points,
bool  check_area = true 
)
explicit

Constructor which takes a vector of points as its vertices.

参数
pointsThe points to construct the polygon.

在文件 polygon2d.cc 第 40 行定义.

41 : points_(std::move(points)) {
42 BuildFromPoints(check_area);
43}
const std::vector< Vec2d > & points() const
Get the vertices of the polygon.
Definition polygon2d.h:65

成员函数说明

◆ AABoundingBox()

AABox2d apollo::common::math::Polygon2d::AABoundingBox ( ) const

Get the axis-aligned bound box of the polygon.

返回
The axis-aligned bound box of the polygon.

在文件 polygon2d.cc 第 522 行定义.

◆ area()

double apollo::common::math::Polygon2d::area ( ) const
inline

Get the area of the polygon.

返回
The area of the polygon.

在文件 polygon2d.h 第 91 行定义.

91{ return area_; }

◆ BoundingBoxWithHeading()

Box2d apollo::common::math::Polygon2d::BoundingBoxWithHeading ( const double  heading) const

Get the bound box according to a heading.

参数
headingThe specified heading of the bounding box.
返回
The bound box according to the specified heading.

在文件 polygon2d.cc 第 526 行定义.

526 {
527 CHECK_GE(points_.size(), 3U);
528 const Vec2d direction_vec = Vec2d::CreateUnitVec2d(heading);
529 Vec2d px1;
530 Vec2d px2;
531 Vec2d py1;
532 Vec2d py2;
533 ExtremePoints(heading, &px1, &px2);
534 ExtremePoints(heading - M_PI_2, &py1, &py2);
535 const double x1 = px1.InnerProd(direction_vec);
536 const double x2 = px2.InnerProd(direction_vec);
537 const double y1 = py1.CrossProd(direction_vec);
538 const double y2 = py2.CrossProd(direction_vec);
539 return Box2d(
540 (x1 + x2) / 2.0 * direction_vec +
541 (y1 + y2) / 2.0 * Vec2d(direction_vec.y(), -direction_vec.x()),
542 heading, x2 - x1, y2 - y1);
543}
void ExtremePoints(const double heading, Vec2d *const first, Vec2d *const last) const
Get the extreme points along a heading direction.
Definition polygon2d.cc:500
static Vec2d CreateUnitVec2d(const double angle)
Creates a unit-vector with a given angle to the positive x semi-axis
Definition vec2d.cc:29

◆ BuildFromPoints()

void apollo::common::math::Polygon2d::BuildFromPoints ( bool  check_area = true)
protected

在文件 polygon2d.cc 第 214 行定义.

214 {
215 num_points_ = static_cast<int>(points_.size());
216 CHECK_GE(num_points_, 3);
217
218 // Make sure the points are in ccw order.
219 area_ = 0.0;
220 for (int i = 1; i < num_points_; ++i) {
221 area_ += CrossProd(points_[0], points_[i - 1], points_[i]);
222 }
223 if (area_ < 0) {
224 area_ = -area_;
225 std::reverse(points_.begin(), points_.end());
226 }
227 area_ /= 2.0;
228 if (check_area) {
229 CHECK_GT(area_, kMathEpsilon);
230 }
231
232 // Construct line_segments.
234 for (int i = 0; i < num_points_; ++i) {
235 line_segments_.emplace_back(points_[i], points_[Next(i)]);
236 }
237
238 // Check convexity.
239 is_convex_ = true;
240 for (int i = 0; i < num_points_; ++i) {
241 if (CrossProd(points_[Prev(i)], points_[i], points_[Next(i)]) <=
242 -kMathEpsilon) {
243 is_convex_ = false;
244 break;
245 }
246 }
247
248 // Compute aabox.
249 min_x_ = points_[0].x();
250 max_x_ = points_[0].x();
251 min_y_ = points_[0].y();
252 max_y_ = points_[0].y();
253 for (const auto &point : points_) {
254 min_x_ = std::min(min_x_, point.x());
255 max_x_ = std::max(max_x_, point.x());
256 min_y_ = std::min(min_y_, point.y());
257 max_y_ = std::max(max_y_, point.y());
258 }
259}
std::vector< LineSegment2d > line_segments_
Definition polygon2d.h:335
constexpr double kMathEpsilon
Definition vec2d.h:35
double CrossProd(const Vec2d &start_point, const Vec2d &end_point_1, const Vec2d &end_point_2)
Cross product between two 2-D vectors from the common start point, and end at two other points.
Definition math_utils.cc:28

◆ CalculateVertices()

void apollo::common::math::Polygon2d::CalculateVertices ( const Vec2d &  shift_vec)

在文件 polygon2d.cc 第 672 行定义.

672 {
673 for (size_t i = 0; i < num_points_; ++i) {
674 points_[i] += shift_vec;
675 }
676 for (auto &point : points_) {
677 max_x_ = std::fmax(point.x(), max_x_);
678 min_x_ = std::fmin(point.x(), min_x_);
679 max_y_ = std::fmax(point.y(), max_y_);
680 min_y_ = std::fmin(point.y(), min_y_);
681 }
682}

◆ ClipConvexHull()

bool apollo::common::math::Polygon2d::ClipConvexHull ( const LineSegment2d &  line_segment,
std::vector< Vec2d > *const  points 
)
staticprotected

在文件 polygon2d.cc 第 314 行定义.

315 {
316 if (line_segment.length() <= kMathEpsilon) {
317 return true;
318 }
319 CHECK_NOTNULL(points);
320 const size_t n = points->size();
321 if (n < 3) {
322 return false;
323 }
324 std::vector<double> prod(n);
325 std::vector<int> side(n);
326 for (size_t i = 0; i < n; ++i) {
327 prod[i] = CrossProd(line_segment.start(), line_segment.end(), (*points)[i]);
328 if (std::abs(prod[i]) <= kMathEpsilon) {
329 side[i] = 0;
330 } else {
331 side[i] = ((prod[i] < 0) ? -1 : 1);
332 }
333 }
334
335 std::vector<Vec2d> new_points;
336 for (size_t i = 0; i < n; ++i) {
337 if (side[i] >= 0) {
338 new_points.push_back((*points)[i]);
339 }
340 const size_t j = ((i == n - 1) ? 0 : (i + 1));
341 if (side[i] * side[j] < 0) {
342 const double ratio = prod[j] / (prod[j] - prod[i]);
343 new_points.emplace_back(
344 (*points)[i].x() * ratio + (*points)[j].x() * (1.0 - ratio),
345 (*points)[i].y() * ratio + (*points)[j].y() * (1.0 - ratio));
346 }
347 }
348
349 points->swap(new_points);
350 return points->size() >= 3U;
351}

◆ ComputeConvexHull()

bool apollo::common::math::Polygon2d::ComputeConvexHull ( const std::vector< Vec2d > &  points,
Polygon2d *const  polygon,
bool  check_area = true 
)
static

Compute the convex hull of a group of points.

参数
pointsThe target points. To compute the convex hull of them.
polygonThe convex hull of the points.
返回
If successfully compute the convex hull.

在文件 polygon2d.cc 第 261 行定义.

262 {
263 CHECK_NOTNULL(polygon);
264 const int n = static_cast<int>(points.size());
265 if (n < 3) {
266 return false;
267 }
268 std::vector<int> sorted_indices(n);
269 for (int i = 0; i < n; ++i) {
270 sorted_indices[i] = i;
271 }
272 std::sort(sorted_indices.begin(), sorted_indices.end(),
273 [&](const int idx1, const int idx2) {
274 const Vec2d &pt1 = points[idx1];
275 const Vec2d &pt2 = points[idx2];
276 const double dx = pt1.x() - pt2.x();
277 if (std::abs(dx) > kMathEpsilon) {
278 return dx < 0.0;
279 }
280 return pt1.y() < pt2.y();
281 });
282 int count = 0;
283 std::vector<int> results;
284 results.reserve(n);
285 int last_count = 1;
286 for (int i = 0; i < n + n; ++i) {
287 if (i == n) {
288 last_count = count;
289 }
290 const int idx = sorted_indices[(i < n) ? i : (n + n - 1 - i)];
291 const Vec2d &pt = points[idx];
292 while (count > last_count &&
293 CrossProd(points[results[count - 2]], points[results[count - 1]],
294 pt) <= kMathEpsilon) {
295 results.pop_back();
296 --count;
297 }
298 results.push_back(idx);
299 ++count;
300 }
301 --count;
302 if (count < 3) {
303 return false;
304 }
305 std::vector<Vec2d> result_points;
306 result_points.reserve(count);
307 for (int i = 0; i < count; ++i) {
308 result_points.push_back(points[results[i]]);
309 }
310 *polygon = Polygon2d(result_points, check_area);
311 return true;
312}
Polygon2d()=default
Empty constructor.

◆ ComputeIoU()

double apollo::common::math::Polygon2d::ComputeIoU ( const Polygon2d &  other_polygon) const

Compute intersection over union ratio of this polygon and the other polygon.

Note: this function only works for computing overlap between two convex polygons.

参数
other_polygonThe target polygon. To compute its overlap with this polygon.
返回
A value between 0.0 and 1.0, meaning no intersection to fully overlaping

在文件 polygon2d.cc 第 367 行定义.

367 {
368 Polygon2d overlap_polygon;
369 if (!ComputeOverlap(other_polygon, &overlap_polygon)) {
370 return 0.0;
371 }
372 double intersection_area = overlap_polygon.area();
373 double union_area = area_ + other_polygon.area() - overlap_polygon.area();
374 return intersection_area / union_area;
375}
bool ComputeOverlap(const Polygon2d &other_polygon, Polygon2d *const overlap_polygon) const
Compute the overlap of this polygon and the other polygon if any.
Definition polygon2d.cc:353

◆ ComputeOverlap()

bool apollo::common::math::Polygon2d::ComputeOverlap ( const Polygon2d &  other_polygon,
Polygon2d *const  overlap_polygon 
) const

Compute the overlap of this polygon and the other polygon if any.

Note: this function only works for computing overlap between two convex polygons.

参数
other_polygonThe target polygon. To compute its overlap with this polygon.
overlap_polygonThe overlapped polygon.
Ifthere is an overlapped polygon.

在文件 polygon2d.cc 第 353 行定义.

354 {
355 CHECK_GE(points_.size(), 3U);
356 CHECK_NOTNULL(overlap_polygon);
357 ACHECK(is_convex_ && other_polygon.is_convex());
358 std::vector<Vec2d> points = other_polygon.points();
359 for (int i = 0; i < num_points_; ++i) {
361 return false;
362 }
363 }
364 return ComputeConvexHull(points, overlap_polygon, false);
365}
static bool ComputeConvexHull(const std::vector< Vec2d > &points, Polygon2d *const polygon, bool check_area=true)
Compute the convex hull of a group of points.
Definition polygon2d.cc:261
static bool ClipConvexHull(const LineSegment2d &line_segment, std::vector< Vec2d > *const points)
Definition polygon2d.cc:314
#define ACHECK(cond)
Definition log.h:80

◆ Contains() [1/2]

bool apollo::common::math::Polygon2d::Contains ( const LineSegment2d &  line_segment) const

Check if the polygon contains a line segment.

参数
line_segmentThe target line segment. To check if the polygon contains it.
返回
Whether the polygon contains the line segment or not.

在文件 polygon2d.cc 第 173 行定义.

173 {
174 if (line_segment.length() <= kMathEpsilon) {
175 return IsPointIn(line_segment.start());
176 }
177 CHECK_GE(points_.size(), 3U);
178 if (!IsPointIn(line_segment.start())) {
179 return false;
180 }
181 if (!IsPointIn(line_segment.end())) {
182 return false;
183 }
184 if (!is_convex_) {
185 std::vector<LineSegment2d> overlaps = GetAllOverlaps(line_segment);
186 double total_length = 0;
187 for (const auto &overlap_seg : overlaps) {
188 total_length += overlap_seg.length();
189 }
190 return total_length >= line_segment.length() - kMathEpsilon;
191 }
192 return true;
193}
std::vector< LineSegment2d > GetAllOverlaps(const LineSegment2d &line_segment) const
Get all overlapped line segments of a line segment and this polygon.
Definition polygon2d.cc:446
bool IsPointIn(const Vec2d &point) const
Check if a point is within the polygon.
Definition polygon2d.cc:130

◆ Contains() [2/2]

bool apollo::common::math::Polygon2d::Contains ( const Polygon2d &  polygon) const

Check if the polygon contains another polygon.

参数
polygonThe target polygon. To check if this polygon contains it.
返回
Whether this polygon contains another polygon or not.

在文件 polygon2d.cc 第 195 行定义.

195 {
196 CHECK_GE(points_.size(), 3U);
197 if (area_ < polygon.area() - kMathEpsilon) {
198 return false;
199 }
200 if (!IsPointIn(polygon.points()[0])) {
201 return false;
202 }
203 const auto &line_segments = polygon.line_segments();
204 return std::all_of(line_segments.begin(), line_segments.end(),
205 [&](const LineSegment2d &line_segment) {
206 return Contains(line_segment);
207 });
208}
const std::vector< LineSegment2d > & line_segments() const
Get the edges of the polygon.
Definition polygon2d.h:71

◆ DebugString()

std::string apollo::common::math::Polygon2d::DebugString ( ) const

Get a string containing essential information about the polygon for debugging purpose.

返回
Essential information about the polygon for debugging purpose.

在文件 polygon2d.cc 第 695 行定义.

695 {
696 return absl::StrCat("polygon2d ( num_points = ", num_points_, " points = (",
697 absl::StrJoin(points_, " ", util::DebugStringFormatter()),
698 " ) ", is_convex_ ? "convex" : "non-convex",
699 " area = ", area_, " )");
700}

◆ DistanceSquareTo()

double apollo::common::math::Polygon2d::DistanceSquareTo ( const Vec2d &  point) const

Compute the square of distance from a point to the polygon.

If the point is within the polygon, return 0. Otherwise, this square of distance is the minimal square of distance from the point to the edges of the polygon.

参数
pointThe point to compute whose square of distance to the polygon.
返回
The square of distance from the point to the polygon.

在文件 polygon2d.cc 第 57 行定义.

57 {
58 CHECK_GE(points_.size(), 3U);
59 if (IsPointIn(point)) {
60 return 0.0;
61 }
62 double distance_sqr = std::numeric_limits<double>::infinity();
63 for (int i = 0; i < num_points_; ++i) {
64 distance_sqr =
65 std::min(distance_sqr, line_segments_[i].DistanceSquareTo(point));
66 }
67 return distance_sqr;
68}
double DistanceSquareTo(const Vec2d &point) const
Compute the square of distance from a point to the polygon.
Definition polygon2d.cc:57

◆ DistanceTo() [1/4]

double apollo::common::math::Polygon2d::DistanceTo ( const Box2d &  box) const

Compute the distance from a box to the polygon.

If the box is within the polygon, or it has overlap with the polygon, return 0. Otherwise, this distance is the minimal distance among the distances from the edges of the box to the polygon.

参数
boxThe box to compute whose distance to the polygon.
返回
The distance from the box to the polygon.

在文件 polygon2d.cc 第 93 行定义.

93 {
94 CHECK_GE(points_.size(), 3U);
95 return DistanceTo(Polygon2d(box));
96}
double DistanceTo(const Vec2d &point) const
Compute the distance from a point to the polygon.
Definition polygon2d.cc:45

◆ DistanceTo() [2/4]

double apollo::common::math::Polygon2d::DistanceTo ( const LineSegment2d &  line_segment) const

Compute the distance from a line segment to the polygon.

If the line segment is within the polygon, or it has intersect with the polygon, return 0. Otherwise, this distance is the minimal distance between the distances from the two ends of the line segment to the polygon.

参数
line_segmentThe line segment to compute whose distance to the polygon.
返回
The distance from the line segment to the polygon.

在文件 polygon2d.cc 第 70 行定义.

70 {
71 if (line_segment.length() <= kMathEpsilon) {
72 return DistanceTo(line_segment.start());
73 }
74 CHECK_GE(points_.size(), 3U);
75 if (IsPointIn(line_segment.center())) {
76 return 0.0;
77 }
78 if (std::any_of(line_segments_.begin(), line_segments_.end(),
79 [&](const LineSegment2d &poly_seg) {
80 return poly_seg.HasIntersect(line_segment);
81 })) {
82 return 0.0;
83 }
84
85 double distance = std::min(DistanceTo(line_segment.start()),
86 DistanceTo(line_segment.end()));
87 for (int i = 0; i < num_points_; ++i) {
88 distance = std::min(distance, line_segment.DistanceTo(points_[i]));
89 }
90 return distance;
91}

◆ DistanceTo() [3/4]

double apollo::common::math::Polygon2d::DistanceTo ( const Polygon2d &  polygon) const

Compute the distance from another polygon to the polygon.

If the other polygon is within this polygon, or it has overlap with this polygon, return 0. Otherwise, this distance is the minimal distance among the distances from the edges of the other polygon to this polygon.

参数
polygonThe polygon to compute whose distance to this polygon.
返回
The distance from the other polygon to this polygon.

在文件 polygon2d.cc 第 98 行定义.

98 {
99 CHECK_GE(points_.size(), 3U);
100 CHECK_GE(polygon.num_points(), 3);
101
102 if (IsPointIn(polygon.points()[0])) {
103 return 0.0;
104 }
105 if (polygon.IsPointIn(points_[0])) {
106 return 0.0;
107 }
108 double distance = std::numeric_limits<double>::infinity();
109 for (int i = 0; i < num_points_; ++i) {
110 distance = std::min(distance, polygon.DistanceTo(line_segments_[i]));
111 }
112 return distance;
113}

◆ DistanceTo() [4/4]

double apollo::common::math::Polygon2d::DistanceTo ( const Vec2d &  point) const

Compute the distance from a point to the polygon.

If the point is within the polygon, return 0. Otherwise, this distance is the minimal distance from the point to the edges of the polygon.

参数
pointThe point to compute whose distance to the polygon.
返回
The distance from the point to the polygon.

在文件 polygon2d.cc 第 45 行定义.

45 {
46 CHECK_GE(points_.size(), 3U);
47 if (IsPointIn(point)) {
48 return 0.0;
49 }
50 double distance = std::numeric_limits<double>::infinity();
51 for (int i = 0; i < num_points_; ++i) {
52 distance = std::min(distance, line_segments_[i].DistanceTo(point));
53 }
54 return distance;
55}

◆ DistanceToBoundary()

double apollo::common::math::Polygon2d::DistanceToBoundary ( const Vec2d &  point) const

Compute the distance from a point to the boundary of the polygon.

This distance is equal to the minimal distance from the point to the edges of the polygon.

参数
pointThe point to compute whose distance to the polygon.
返回
The distance from the point to the polygon's boundary.

在文件 polygon2d.cc 第 115 行定义.

115 {
116 double distance = std::numeric_limits<double>::infinity();
117 for (int i = 0; i < num_points_; ++i) {
118 distance = std::min(distance, line_segments_[i].DistanceTo(point));
119 }
120 return distance;
121}

◆ ExpandByDistance()

Polygon2d apollo::common::math::Polygon2d::ExpandByDistance ( const double  distance) const

Expand this polygon by a distance.

参数
distanceThe specified distance. To expand this polygon by it.
返回
The polygon after expansion.

在文件 polygon2d.cc 第 604 行定义.

604 {
605 if (!is_convex_) {
606 Polygon2d convex_polygon;
607 ComputeConvexHull(points_, &convex_polygon);
608 ACHECK(convex_polygon.is_convex());
609 return convex_polygon.ExpandByDistance(distance);
610 }
611 const double kMinAngle = 0.1;
612 std::vector<Vec2d> points;
613 for (int i = 0; i < num_points_; ++i) {
614 const double start_angle = line_segments_[Prev(i)].heading() - M_PI_2;
615 const double end_angle = line_segments_[i].heading() - M_PI_2;
616 const double diff = WrapAngle(end_angle - start_angle);
617 if (diff <= kMathEpsilon) {
618 points.push_back(points_[i] +
619 Vec2d::CreateUnitVec2d(start_angle) * distance);
620 } else {
621 const int count = static_cast<int>(diff / kMinAngle) + 1;
622 for (int k = 0; k <= count; ++k) {
623 const double angle = start_angle + diff * static_cast<double>(k) /
624 static_cast<double>(count);
625 points.push_back(points_[i] + Vec2d::CreateUnitVec2d(angle) * distance);
626 }
627 }
628 }
629 Polygon2d new_polygon;
630 ACHECK(ComputeConvexHull(points, &new_polygon));
631 return new_polygon;
632}
double WrapAngle(const double angle)
Wrap angle to [0, 2 * PI).
Definition math_utils.cc:48
constexpr float kMinAngle
Definition lane_object.h:31

◆ ExtremePoints()

void apollo::common::math::Polygon2d::ExtremePoints ( const double  heading,
Vec2d *const  first,
Vec2d *const  last 
) const

Get the extreme points along a heading direction.

参数
headingThe specified heading.
firstThe point on the boundary of this polygon with the minimal projection onto the heading direction.
lastThe point on the boundary of this polygon with the maximal projection onto the heading direction.

在文件 polygon2d.cc 第 500 行定义.

501 {
502 CHECK_GE(points_.size(), 3U);
503 CHECK_NOTNULL(first);
504 CHECK_NOTNULL(last);
505
506 const Vec2d direction_vec = Vec2d::CreateUnitVec2d(heading);
507 double min_proj = std::numeric_limits<double>::infinity();
508 double max_proj = -std::numeric_limits<double>::infinity();
509 for (const auto &pt : points_) {
510 const double proj = pt.InnerProd(direction_vec);
511 if (proj < min_proj) {
512 min_proj = proj;
513 *first = pt;
514 }
515 if (proj > max_proj) {
516 max_proj = proj;
517 *last = pt;
518 }
519 }
520}

◆ GetAllOverlaps()

std::vector< LineSegment2d > apollo::common::math::Polygon2d::GetAllOverlaps ( const LineSegment2d &  line_segment) const

Get all overlapped line segments of a line segment and this polygon.

There are possibly multiple overlapped line segments if this polygon is not convex.

参数
line_segmentThe target line segment. To get its all overlapped line segments with this polygon.
返回
A group of overlapped line segments.

在文件 polygon2d.cc 第 446 行定义.

447 {
448 CHECK_GE(points_.size(), 3U);
449
450 if (line_segment.length() <= kMathEpsilon) {
451 std::vector<LineSegment2d> overlaps;
452 if (IsPointIn(line_segment.start())) {
453 overlaps.push_back(line_segment);
454 }
455 return overlaps;
456 }
457 std::vector<double> projections;
458 if (IsPointIn(line_segment.start())) {
459 projections.push_back(0.0);
460 }
461 if (IsPointIn(line_segment.end())) {
462 projections.push_back(line_segment.length());
463 }
464 for (const auto &poly_seg : line_segments_) {
465 Vec2d pt;
466 if (poly_seg.GetIntersect(line_segment, &pt)) {
467 projections.push_back(line_segment.ProjectOntoUnit(pt));
468 }
469 }
470 std::sort(projections.begin(), projections.end());
471 std::vector<std::pair<double, double>> overlaps;
472 for (size_t i = 0; i + 1 < projections.size(); ++i) {
473 const double start_proj = projections[i];
474 const double end_proj = projections[i + 1];
475 if (end_proj - start_proj <= kMathEpsilon) {
476 continue;
477 }
478 const Vec2d reference_point =
479 line_segment.start() +
480 (start_proj + end_proj) / 2.0 * line_segment.unit_direction();
481 if (!IsPointIn(reference_point)) {
482 continue;
483 }
484 if (overlaps.empty() ||
485 start_proj > overlaps.back().second + kMathEpsilon) {
486 overlaps.emplace_back(start_proj, end_proj);
487 } else {
488 overlaps.back().second = end_proj;
489 }
490 }
491 std::vector<LineSegment2d> overlap_line_segments;
492 for (const auto &overlap : overlaps) {
493 overlap_line_segments.emplace_back(
494 line_segment.start() + overlap.first * line_segment.unit_direction(),
495 line_segment.start() + overlap.second * line_segment.unit_direction());
496 }
497 return overlap_line_segments;
498}

◆ GetAllVertices() [1/2]

std::vector< Vec2d > apollo::common::math::Polygon2d::GetAllVertices ( ) const

Get all vertices of the polygon

在文件 polygon2d.cc 第 444 行定义.

444{ return points_; }

◆ GetAllVertices() [2/2]

void apollo::common::math::Polygon2d::GetAllVertices ( std::vector< Vec2d > *const  vertices) const

Get all vertices of the polygon

参数
Allvertices of the polygon

在文件 polygon2d.cc 第 437 行定义.

437 {
438 if (vertices == nullptr) {
439 return;
440 }
441 *vertices = points_;
442}

◆ GetOverlap()

bool apollo::common::math::Polygon2d::GetOverlap ( const LineSegment2d &  line_segment,
Vec2d *const  first,
Vec2d *const  last 
) const

Get the overlap of a line segment and this polygon.

If they have overlap, output the two ends of the overlapped line segment.

参数
line_segmentThe target line segment. To get its overlap with this polygon.
firstFirst end of the overlapped line segment.
secondSecond end of the overlapped line segment.
返回
If the target line segment has overlap with this polygon.

在文件 polygon2d.cc 第 395 行定义.

396 {
397 CHECK_GE(points_.size(), 3U);
398 CHECK_NOTNULL(first);
399 CHECK_NOTNULL(last);
400
401 if (line_segment.length() <= kMathEpsilon) {
402 if (!IsPointIn(line_segment.start())) {
403 return false;
404 }
405 *first = line_segment.start();
406 *last = line_segment.start();
407 return true;
408 }
409
410 double min_proj = line_segment.length();
411 double max_proj = 0;
412 if (IsPointIn(line_segment.start())) {
413 *first = line_segment.start();
414 min_proj = 0.0;
415 }
416 if (IsPointIn(line_segment.end())) {
417 *last = line_segment.end();
418 max_proj = line_segment.length();
419 }
420 for (const auto &poly_seg : line_segments_) {
421 Vec2d pt;
422 if (poly_seg.GetIntersect(line_segment, &pt)) {
423 const double proj = line_segment.ProjectOntoUnit(pt);
424 if (proj < min_proj) {
425 min_proj = proj;
426 *first = pt;
427 }
428 if (proj > max_proj) {
429 max_proj = proj;
430 *last = pt;
431 }
432 }
433 }
434 return min_proj <= max_proj + kMathEpsilon;
435}

◆ HasOverlap() [1/2]

bool apollo::common::math::Polygon2d::HasOverlap ( const LineSegment2d &  line_segment) const

Check if a line segment has overlap with this polygon.

参数
line_segmentThe target line segment. To check if it has overlap with this polygon.
返回
Whether the target line segment has overlap with this polygon or not.

在文件 polygon2d.cc 第 377 行定义.

377 {
378 CHECK_GE(points_.size(), 3U);
379 if ((line_segment.start().x() < min_x_ && line_segment.end().x() < min_x_) ||
380 (line_segment.start().x() > max_x_ && line_segment.end().x() > max_x_) ||
381 (line_segment.start().y() < min_y_ && line_segment.end().y() < min_y_) ||
382 (line_segment.start().y() > max_y_ && line_segment.end().y() > max_y_)) {
383 return false;
384 }
385
386 if (std::any_of(line_segments_.begin(), line_segments_.end(),
387 [&](const LineSegment2d &poly_seg) {
388 return poly_seg.HasIntersect(line_segment);
389 })) {
390 return true;
391 }
392 return false;
393}

◆ HasOverlap() [2/2]

bool apollo::common::math::Polygon2d::HasOverlap ( const Polygon2d &  polygon) const

Check if this polygon has overlap with another polygon.

参数
polygonThe target polygon. To check if it has overlap with this polygon.
返回
If this polygon has overlap with another polygon.

在文件 polygon2d.cc 第 149 行定义.

149 {
150 CHECK_GE(points_.size(), 3U);
151 CHECK_GE(polygon.num_points(), 3);
152 if (polygon.max_x() < min_x() || polygon.min_x() > max_x() ||
153 polygon.max_y() < min_y() || polygon.min_y() > max_y()) {
154 return false;
155 }
156
157 if (IsPointIn(polygon.points()[0])) {
158 return true;
159 }
160
161 if (polygon.IsPointIn(points_[0])) {
162 return true;
163 }
164
165 for (int i = 0; i < polygon.num_points(); ++i) {
166 if (HasOverlap(polygon.line_segments()[i])) {
167 return true;
168 }
169 }
170 return false;
171}
bool HasOverlap(const LineSegment2d &line_segment) const
Check if a line segment has overlap with this polygon.
Definition polygon2d.cc:377

◆ is_convex()

bool apollo::common::math::Polygon2d::is_convex ( ) const
inline

Check if the polygon is convex.

返回
Whether the polygon is convex or not.

在文件 polygon2d.h 第 85 行定义.

85{ return is_convex_; }

◆ IsPointIn()

bool apollo::common::math::Polygon2d::IsPointIn ( const Vec2d &  point) const

Check if a point is within the polygon.

参数
pointThe target point. To check if it is within the polygon.
返回
Whether a point is within the polygon or not.

在文件 polygon2d.cc 第 130 行定义.

130 {
131 CHECK_GE(points_.size(), 3U);
132 if (IsPointOnBoundary(point)) {
133 return true;
134 }
135 int j = num_points_ - 1;
136 int c = 0;
137 for (int i = 0; i < num_points_; ++i) {
138 if ((points_[i].y() > point.y()) != (points_[j].y() > point.y())) {
139 const double side = CrossProd(point, points_[i], points_[j]);
140 if (points_[i].y() < points_[j].y() ? side > 0.0 : side < 0.0) {
141 ++c;
142 }
143 }
144 j = i;
145 }
146 return c & 1;
147}
bool IsPointOnBoundary(const Vec2d &point) const
Check if a point is on the boundary of the polygon.
Definition polygon2d.cc:123

◆ IsPointOnBoundary()

bool apollo::common::math::Polygon2d::IsPointOnBoundary ( const Vec2d &  point) const

Check if a point is on the boundary of the polygon.

参数
pointThe target point. To check if it is on the boundary of the polygon.
返回
Whether a point is on the boundary of the polygon or not.

在文件 polygon2d.cc 第 123 行定义.

123 {
124 CHECK_GE(points_.size(), 3U);
125 return std::any_of(
126 line_segments_.begin(), line_segments_.end(),
127 [&](const LineSegment2d &poly_seg) { return poly_seg.IsPointIn(point); });
128}

◆ line_segments()

const std::vector< LineSegment2d > & apollo::common::math::Polygon2d::line_segments ( ) const
inline

Get the edges of the polygon.

返回
The edges of the polygon.

在文件 polygon2d.h 第 71 行定义.

71 {
72 return line_segments_;
73 }

◆ max_x()

double apollo::common::math::Polygon2d::max_x ( ) const
inline

在文件 polygon2d.h 第 320 行定义.

320{ return max_x_; }

◆ max_y()

double apollo::common::math::Polygon2d::max_y ( ) const
inline

在文件 polygon2d.h 第 322 行定义.

322{ return max_y_; }

◆ min_x()

double apollo::common::math::Polygon2d::min_x ( ) const
inline

在文件 polygon2d.h 第 319 行定义.

319{ return min_x_; }

◆ min_y()

double apollo::common::math::Polygon2d::min_y ( ) const
inline

在文件 polygon2d.h 第 321 行定义.

321{ return min_y_; }

◆ MinAreaBoundingBox()

Box2d apollo::common::math::Polygon2d::MinAreaBoundingBox ( ) const

Get the bounding box with the minimal area.

返回
The bounding box with the minimal area.

在文件 polygon2d.cc 第 545 行定义.

545 {
546 CHECK_GE(points_.size(), 3U);
547 if (!is_convex_) {
548 Polygon2d convex_polygon;
549 ComputeConvexHull(points_, &convex_polygon);
550 ACHECK(convex_polygon.is_convex());
551 return convex_polygon.MinAreaBoundingBox();
552 }
553 double min_area = std::numeric_limits<double>::infinity();
554 double min_area_at_heading = 0.0;
555 int left_most = 0;
556 int right_most = 0;
557 int top_most = 0;
558 for (int i = 0; i < num_points_; ++i) {
559 const auto &line_segment = line_segments_[i];
560 double proj = 0.0;
561 double min_proj = line_segment.ProjectOntoUnit(points_[left_most]);
562 while ((proj = line_segment.ProjectOntoUnit(points_[Prev(left_most)])) <
563 min_proj) {
564 min_proj = proj;
565 left_most = Prev(left_most);
566 }
567 while ((proj = line_segment.ProjectOntoUnit(points_[Next(left_most)])) <
568 min_proj) {
569 min_proj = proj;
570 left_most = Next(left_most);
571 }
572 double max_proj = line_segment.ProjectOntoUnit(points_[right_most]);
573 while ((proj = line_segment.ProjectOntoUnit(points_[Prev(right_most)])) >
574 max_proj) {
575 max_proj = proj;
576 right_most = Prev(right_most);
577 }
578 while ((proj = line_segment.ProjectOntoUnit(points_[Next(right_most)])) >
579 max_proj) {
580 max_proj = proj;
581 right_most = Next(right_most);
582 }
583 double prod = 0.0;
584 double max_prod = line_segment.ProductOntoUnit(points_[top_most]);
585 while ((prod = line_segment.ProductOntoUnit(points_[Prev(top_most)])) >
586 max_prod) {
587 max_prod = prod;
588 top_most = Prev(top_most);
589 }
590 while ((prod = line_segment.ProductOntoUnit(points_[Next(top_most)])) >
591 max_prod) {
592 max_prod = prod;
593 top_most = Next(top_most);
594 }
595 const double area = max_prod * (max_proj - min_proj);
596 if (area < min_area) {
597 min_area = area;
598 min_area_at_heading = line_segment.heading();
599 }
600 }
601 return BoundingBoxWithHeading(min_area_at_heading);
602}
Box2d BoundingBoxWithHeading(const double heading) const
Get the bound box according to a heading.
Definition polygon2d.cc:526
double area() const
Get the area of the polygon.
Definition polygon2d.h:91

◆ MinLineSegment()

LineSegment2d apollo::common::math::Polygon2d::MinLineSegment ( ) const

在文件 polygon2d.cc 第 684 行定义.

684 {
685 double min_length_of_line_segment = 10e3;
686 LineSegment2d line_segment;
687 for (auto &line_segment_ : line_segments_) {
688 if (line_segment_.length() < min_length_of_line_segment) {
689 line_segment = line_segment_;
690 }
691 }
692 return line_segment;
693}

◆ Next()

int apollo::common::math::Polygon2d::Next ( int  at) const
protected

在文件 polygon2d.cc 第 210 行定义.

210{ return at >= num_points_ - 1 ? 0 : at + 1; }

◆ num_points()

int apollo::common::math::Polygon2d::num_points ( ) const
inline

Get the number of vertices of the polygon.

返回
The number of vertices of the polygon.

在文件 polygon2d.h 第 79 行定义.

79{ return num_points_; }

◆ points()

const std::vector< Vec2d > & apollo::common::math::Polygon2d::points ( ) const
inline

Get the vertices of the polygon.

返回
The vertices of the polygon.

在文件 polygon2d.h 第 65 行定义.

65{ return points_; }

◆ PolygonExpandByDistance()

Polygon2d apollo::common::math::Polygon2d::PolygonExpandByDistance ( const double  distance) const

在文件 polygon2d.cc 第 634 行定义.

634 {
635 if (!is_convex_) {
636 Polygon2d convex_polygon;
637 ComputeConvexHull(points_, &convex_polygon);
638 ACHECK(convex_polygon.is_convex());
639 return convex_polygon.PolygonExpandByDistance(distance);
640 }
641 std::vector<Vec2d> points;
642 for (int i = 0; i < num_points_; ++i) {
643 double v1x = points_[Prev(i)].x() - points_[i].x();
644 double v1y = points_[Prev(i)].y() - points_[i].y();
645 double n1 = std::sqrt(v1x * v1x + v1y * v1y);
646 v1x /= n1;
647 v1y /= n1;
648
649 double v2x = points_[Next(i)].x() - points_[i].x();
650 double v2y = points_[Next(i)].y() - points_[i].y();
651 double n2 = std::sqrt(v2x * v2x + v2y * v2y);
652 v2x /= n2;
653 v2x /= n2;
654
655 double l = distance / sqrt((1 - (v1x * v2x + v1y * v2y)) / 2);
656
657 double vx = v1x + v2x;
658 double vy = v1y + v2y;
659 double n = l / sqrt(vx * vx + vy * vy);
660
661 vx *= n;
662 vy *= n;
663
664 Vec2d point(vx + points_[i].x(), vy + points_[i].y());
665 points.push_back(point);
666 }
667 Polygon2d new_polygon;
668 ACHECK(ComputeConvexHull(points, &new_polygon));
669 return new_polygon;
670}

◆ Prev()

int apollo::common::math::Polygon2d::Prev ( int  at) const
protected

在文件 polygon2d.cc 第 212 行定义.

212{ return at == 0 ? num_points_ - 1 : at - 1; }

类成员变量说明

◆ area_

double apollo::common::math::Polygon2d::area_ = 0.0
protected

在文件 polygon2d.h 第 337 行定义.

◆ is_convex_

bool apollo::common::math::Polygon2d::is_convex_ = false
protected

在文件 polygon2d.h 第 336 行定义.

◆ line_segments_

std::vector<LineSegment2d> apollo::common::math::Polygon2d::line_segments_
protected

在文件 polygon2d.h 第 335 行定义.

◆ max_x_

double apollo::common::math::Polygon2d::max_x_ = 0.0
protected

在文件 polygon2d.h 第 339 行定义.

◆ max_y_

double apollo::common::math::Polygon2d::max_y_ = 0.0
protected

在文件 polygon2d.h 第 341 行定义.

◆ min_x_

double apollo::common::math::Polygon2d::min_x_ = 0.0
protected

在文件 polygon2d.h 第 338 行定义.

◆ min_y_

double apollo::common::math::Polygon2d::min_y_ = 0.0
protected

在文件 polygon2d.h 第 340 行定义.

◆ num_points_

int apollo::common::math::Polygon2d::num_points_ = 0
protected

在文件 polygon2d.h 第 334 行定义.

◆ points_

std::vector<Vec2d> apollo::common::math::Polygon2d::points_
protected

在文件 polygon2d.h 第 333 行定义.


该类的文档由以下文件生成: