Apollo 9.0
自动驾驶开放平台
polygon2d.h
浏览该文件的文档.
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
22#pragma once
23
24#include <string>
25#include <vector>
26
29
34namespace apollo {
35namespace common {
36namespace math {
37
42class Polygon2d {
43 public:
47 Polygon2d() = default;
48
53 explicit Polygon2d(const Box2d &box);
54
59 explicit Polygon2d(std::vector<Vec2d> points, bool check_area = true);
60
65 const std::vector<Vec2d> &points() const { return points_; }
66
71 const std::vector<LineSegment2d> &line_segments() const {
72 return line_segments_;
73 }
74
79 int num_points() const { return num_points_; }
80
85 bool is_convex() const { return is_convex_; }
86
91 double area() const { return area_; }
92
100 double DistanceToBoundary(const Vec2d &point) const;
101
109 double DistanceTo(const Vec2d &point) const;
110
121 double DistanceTo(const LineSegment2d &line_segment) const;
122
132 double DistanceTo(const Box2d &box) const;
133
143 double DistanceTo(const Polygon2d &polygon) const;
144
153 double DistanceSquareTo(const Vec2d &point) const;
154
160 bool IsPointIn(const Vec2d &point) const;
161
168 bool IsPointOnBoundary(const Vec2d &point) const;
169
176 bool Contains(const LineSegment2d &line_segment) const;
177
183 bool Contains(const Polygon2d &polygon) const;
184
191 static bool ComputeConvexHull(const std::vector<Vec2d> &points,
192 Polygon2d *const polygon,
193 bool check_area = true);
194
202 bool HasOverlap(const LineSegment2d &line_segment) const;
203
213 bool GetOverlap(const LineSegment2d &line_segment, Vec2d *const first,
214 Vec2d *const last) const;
215
220 void GetAllVertices(std::vector<Vec2d> *const vertices) const;
221
225 std::vector<Vec2d> GetAllVertices() const;
226
235 std::vector<LineSegment2d> GetAllOverlaps(
236 const LineSegment2d &line_segment) const;
237
244 bool HasOverlap(const Polygon2d &polygon) const;
245
246 // Only compute overlaps between two convex polygons.
256 bool ComputeOverlap(const Polygon2d &other_polygon,
257 Polygon2d *const overlap_polygon) const;
258
259 // Only compute intersection over union ratio between two convex polygons.
269 double ComputeIoU(const Polygon2d &other_polygon) const;
270
275 AABox2d AABoundingBox() const;
276
282 Box2d BoundingBoxWithHeading(const double heading) const;
283
289
298 void ExtremePoints(const double heading, Vec2d *const first,
299 Vec2d *const last) const;
300
306 Polygon2d ExpandByDistance(const double distance) const;
307
308 Polygon2d PolygonExpandByDistance(const double distance) const;
309
310 void CalculateVertices(const Vec2d &shift_vec);
311
317 std::string DebugString() const;
318
319 double min_x() const { return min_x_; }
320 double max_x() const { return max_x_; }
321 double min_y() const { return min_y_; }
322 double max_y() const { return max_y_; }
324
325 protected:
326 void BuildFromPoints(bool check_area = true);
327 int Next(int at) const;
328 int Prev(int at) const;
329
330 static bool ClipConvexHull(const LineSegment2d &line_segment,
331 std::vector<Vec2d> *const points);
332
333 std::vector<Vec2d> points_;
334 int num_points_ = 0;
335 std::vector<LineSegment2d> line_segments_;
336 bool is_convex_ = false;
337 double area_ = 0.0;
338 double min_x_ = 0.0;
339 double max_x_ = 0.0;
340 double min_y_ = 0.0;
341 double max_y_ = 0.0;
342};
343
344} // namespace math
345} // namespace common
346} // namespace apollo
The class of Box2d.
Implements a class of (undirected) axes-aligned bounding boxes in 2-D.
Definition aabox2d.h:42
Rectangular (undirected) bounding box in 2-D.
Definition box2d.h:52
The class of polygon in 2-D.
Definition polygon2d.h:42
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 HasOverlap(const LineSegment2d &line_segment) const
Check if a line segment has overlap with this polygon.
Definition polygon2d.cc:377
std::string DebugString() const
Get a string containing essential information about the polygon for debugging purpose.
Definition polygon2d.cc:695
std::vector< Vec2d > points_
Definition polygon2d.h:333
bool IsPointIn(const Vec2d &point) const
Check if a point is within the polygon.
Definition polygon2d.cc:130
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
bool Contains(const LineSegment2d &line_segment) const
Check if the polygon contains a line segment.
Definition polygon2d.cc:173
Polygon2d()=default
Empty constructor.
LineSegment2d MinLineSegment() const
Definition polygon2d.cc:684
Box2d BoundingBoxWithHeading(const double heading) const
Get the bound box according to a heading.
Definition polygon2d.cc:526
Polygon2d ExpandByDistance(const double distance) const
Expand this polygon by a distance.
Definition polygon2d.cc:604
Box2d MinAreaBoundingBox() const
Get the bounding box with the minimal area.
Definition polygon2d.cc:545
AABox2d AABoundingBox() const
Get the axis-aligned bound box of the polygon.
Definition polygon2d.cc:522
double DistanceTo(const Vec2d &point) const
Compute the distance from a point to the polygon.
Definition polygon2d.cc:45
const std::vector< LineSegment2d > & line_segments() const
Get the edges of the polygon.
Definition polygon2d.h:71
void CalculateVertices(const Vec2d &shift_vec)
Definition polygon2d.cc:672
void ExtremePoints(const double heading, Vec2d *const first, Vec2d *const last) const
Get the extreme points along a heading direction.
Definition polygon2d.cc:500
bool GetOverlap(const LineSegment2d &line_segment, Vec2d *const first, Vec2d *const last) const
Get the overlap of a line segment and this polygon.
Definition polygon2d.cc:395
double ComputeIoU(const Polygon2d &other_polygon) const
Compute intersection over union ratio of this polygon and the other polygon.
Definition polygon2d.cc:367
const std::vector< Vec2d > & points() const
Get the vertices of the polygon.
Definition polygon2d.h:65
double DistanceSquareTo(const Vec2d &point) const
Compute the square of distance from a point to the polygon.
Definition polygon2d.cc:57
void BuildFromPoints(bool check_area=true)
Definition polygon2d.cc:214
std::vector< Vec2d > GetAllVertices() const
Get all vertices of the polygon
Definition polygon2d.cc:444
std::vector< LineSegment2d > line_segments_
Definition polygon2d.h:335
bool IsPointOnBoundary(const Vec2d &point) const
Check if a point is on the boundary of the polygon.
Definition polygon2d.cc:123
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
int num_points() const
Get the number of vertices of the polygon.
Definition polygon2d.h:79
Polygon2d PolygonExpandByDistance(const double distance) const
Definition polygon2d.cc:634
bool is_convex() const
Check if the polygon is convex.
Definition polygon2d.h:85
double area() const
Get the area of the polygon.
Definition polygon2d.h:91
static bool ClipConvexHull(const LineSegment2d &line_segment, std::vector< Vec2d > *const points)
Definition polygon2d.cc:314
double DistanceToBoundary(const Vec2d &point) const
Compute the distance from a point to the boundary of the polygon.
Definition polygon2d.cc:115
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
Define the LineSegment2d class.
class register implement