Apollo 10.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
120 double DistanceTo(const Vec2d &point, Vec2d& closest_point) const;
121
132 double DistanceTo(const LineSegment2d &line_segment) const;
133
148 double DistanceTo(const LineSegment2d &line_segment,
149 Vec2d& polygon_closest_point,
150 Vec2d& segment_closest_point) const;
151
161 double DistanceTo(const Box2d &box) const;
162
172 double DistanceTo(const Polygon2d &polygon) const;
173
187 double DistanceTo(const Polygon2d& polygon,
188 Vec2d& self_closest_point,
189 Vec2d& other_closest_point) const;
190
199 double DistanceSquareTo(const Vec2d &point) const;
200
206 bool IsPointIn(const Vec2d &point) const;
207
214 bool IsPointOnBoundary(const Vec2d &point) const;
215
222 bool Contains(const LineSegment2d &line_segment) const;
223
229 bool Contains(const Polygon2d &polygon) const;
230
237 static bool ComputeConvexHull(const std::vector<Vec2d> &points,
238 Polygon2d *const polygon,
239 bool check_area = true);
240
248 bool HasOverlap(const LineSegment2d &line_segment) const;
249
259 bool GetOverlap(const LineSegment2d &line_segment, Vec2d *const first,
260 Vec2d *const last) const;
261
266 void GetAllVertices(std::vector<Vec2d> *const vertices) const;
267
271 std::vector<Vec2d> GetAllVertices() const;
272
281 std::vector<LineSegment2d> GetAllOverlaps(
282 const LineSegment2d &line_segment) const;
283
290 bool HasOverlap(const Polygon2d &polygon) const;
291
292 // Only compute overlaps between two convex polygons.
302 bool ComputeOverlap(const Polygon2d &other_polygon,
303 Polygon2d *const overlap_polygon) const;
304
305 // Only compute intersection over union ratio between two convex polygons.
315 double ComputeIoU(const Polygon2d &other_polygon) const;
316
321 AABox2d AABoundingBox() const;
322
328 Box2d BoundingBoxWithHeading(const double heading) const;
329
335
344 void ExtremePoints(const double heading, Vec2d *const first,
345 Vec2d *const last) const;
346
352 Polygon2d ExpandByDistance(const double distance) const;
353
354 Polygon2d PolygonExpandByDistance(const double distance) const;
355
356 void CalculateVertices(const Vec2d &shift_vec);
357
363 std::string DebugString() const;
364
365 double min_x() const { return min_x_; }
366 double max_x() const { return max_x_; }
367 double min_y() const { return min_y_; }
368 double max_y() const { return max_y_; }
370
371 protected:
372 void BuildFromPoints(bool check_area = true);
373 int Next(int at) const;
374 int Prev(int at) const;
375
376 static bool ClipConvexHull(const LineSegment2d &line_segment,
377 std::vector<Vec2d> *const points);
378
379 std::vector<Vec2d> points_;
380 int num_points_ = 0;
381 std::vector<LineSegment2d> line_segments_;
382 bool is_convex_ = false;
383 double area_ = 0.0;
384 double min_x_ = 0.0;
385 double max_x_ = 0.0;
386 double min_y_ = 0.0;
387 double max_y_ = 0.0;
388};
389
390} // namespace math
391} // namespace common
392} // 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:525
bool HasOverlap(const LineSegment2d &line_segment) const
Check if a line segment has overlap with this polygon.
Definition polygon2d.cc:456
std::string DebugString() const
Get a string containing essential information about the polygon for debugging purpose.
Definition polygon2d.cc:774
std::vector< Vec2d > points_
Definition polygon2d.h:379
bool IsPointIn(const Vec2d &point) const
Check if a point is within the polygon.
Definition polygon2d.cc:209
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:340
bool Contains(const LineSegment2d &line_segment) const
Check if the polygon contains a line segment.
Definition polygon2d.cc:252
Polygon2d()=default
Empty constructor.
LineSegment2d MinLineSegment() const
Definition polygon2d.cc:763
Box2d BoundingBoxWithHeading(const double heading) const
Get the bound box according to a heading.
Definition polygon2d.cc:605
Polygon2d ExpandByDistance(const double distance) const
Expand this polygon by a distance.
Definition polygon2d.cc:683
Box2d MinAreaBoundingBox() const
Get the bounding box with the minimal area.
Definition polygon2d.cc:624
AABox2d AABoundingBox() const
Get the axis-aligned bound box of the polygon.
Definition polygon2d.cc:601
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:751
void ExtremePoints(const double heading, Vec2d *const first, Vec2d *const last) const
Get the extreme points along a heading direction.
Definition polygon2d.cc:579
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:474
double ComputeIoU(const Polygon2d &other_polygon) const
Compute intersection over union ratio of this polygon and the other polygon.
Definition polygon2d.cc:446
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:74
void BuildFromPoints(bool check_area=true)
Definition polygon2d.cc:293
std::vector< Vec2d > GetAllVertices() const
Get all vertices of the polygon
Definition polygon2d.cc:523
std::vector< LineSegment2d > line_segments_
Definition polygon2d.h:381
bool IsPointOnBoundary(const Vec2d &point) const
Check if a point is on the boundary of the polygon.
Definition polygon2d.cc:202
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:432
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:713
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:393
double DistanceToBoundary(const Vec2d &point) const
Compute the distance from a point to the boundary of the polygon.
Definition polygon2d.cc:194
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
Define the LineSegment2d class.
class register implement
Definition arena_queue.h:37