Apollo  6.0
Open source self driving car software
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
apollo::common::math::Polygon2d Class Reference

The class of polygon in 2-D. More...

#include <polygon2d.h>

Inheritance diagram for apollo::common::math::Polygon2d:
Inheritance graph
Collaboration diagram for apollo::common::math::Polygon2d:
Collaboration graph

Public Member Functions

 Polygon2d ()=default
 Empty constructor. More...
 
 Polygon2d (const Box2d &box)
 Constructor which takes a box. More...
 
 Polygon2d (std::vector< Vec2d > points)
 Constructor which takes a vector of points as its vertices. More...
 
const std::vector< Vec2d > & points () const
 Get the vertices of the polygon. More...
 
const std::vector< LineSegment2d > & line_segments () const
 Get the edges of the polygon. More...
 
int num_points () const
 Get the number of vertices of the polygon. More...
 
bool is_convex () const
 Check if the polygon is convex. More...
 
double area () const
 Get the area of the polygon. More...
 
double 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. More...
 
double 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. More...
 
double 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. More...
 
double 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. More...
 
double 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. More...
 
double 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. More...
 
bool IsPointIn (const Vec2d &point) const
 Check if a point is within the polygon. More...
 
bool IsPointOnBoundary (const Vec2d &point) const
 Check if a point is on the boundary of the polygon. More...
 
bool Contains (const LineSegment2d &line_segment) const
 Check if the polygon contains a line segment. More...
 
bool Contains (const Polygon2d &polygon) const
 Check if the polygon contains another polygon. More...
 
bool HasOverlap (const LineSegment2d &line_segment) const
 Check if a line segment has overlap with this polygon. More...
 
bool 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. More...
 
void GetAllVertices (std::vector< Vec2d > *const vertices) const
 Get all vertices of the polygon. More...
 
std::vector< Vec2dGetAllVertices () const
 Get all vertices of the polygon. More...
 
std::vector< LineSegment2dGetAllOverlaps (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. More...
 
bool HasOverlap (const Polygon2d &polygon) const
 Check if this polygon has overlap with another polygon. More...
 
bool 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. More...
 
double 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. More...
 
AABox2d AABoundingBox () const
 Get the axis-aligned bound box of the polygon. More...
 
Box2d BoundingBoxWithHeading (const double heading) const
 Get the bound box according to a heading. More...
 
Box2d MinAreaBoundingBox () const
 Get the bounding box with the minimal area. More...
 
void ExtremePoints (const double heading, Vec2d *const first, Vec2d *const last) const
 Get the extreme points along a heading direction. More...
 
Polygon2d ExpandByDistance (const double distance) const
 Expand this polygon by a distance. More...
 
std::string DebugString () const
 Get a string containing essential information about the polygon for debugging purpose. More...
 
double min_x () const
 
double max_x () const
 
double min_y () const
 
double max_y () const
 

Static Public Member Functions

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

Protected Member Functions

void BuildFromPoints ()
 
int Next (int at) const
 
int Prev (int at) const
 

Static Protected Member Functions

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

Protected Attributes

std::vector< Vec2dpoints_
 
int num_points_ = 0
 
std::vector< LineSegment2dline_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
 

Detailed Description

The class of polygon in 2-D.

Constructor & Destructor Documentation

◆ 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.

Parameters
boxThe box to construct the polygon.

◆ Polygon2d() [3/3]

apollo::common::math::Polygon2d::Polygon2d ( std::vector< Vec2d points)
explicit

Constructor which takes a vector of points as its vertices.

Parameters
pointsThe points to construct the polygon.

Member Function Documentation

◆ AABoundingBox()

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

Get the axis-aligned bound box of the polygon.

Returns
The axis-aligned bound box of the polygon.

◆ area()

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

Get the area of the polygon.

Returns
The area of the polygon.

◆ BoundingBoxWithHeading()

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

Get the bound box according to a heading.

Parameters
headingThe specified heading of the bounding box.
Returns
The bound box according to the specified heading.

◆ BuildFromPoints()

void apollo::common::math::Polygon2d::BuildFromPoints ( )
protected

◆ ClipConvexHull()

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

◆ ComputeConvexHull()

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

Compute the convex hull of a group of points.

Parameters
pointsThe target points. To compute the convex hull of them.
polygonThe convex hull of the points.
Returns
If successfully compute the convex hull.

◆ 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.

Parameters
other_polygonThe target polygon. To compute its overlap with this polygon.
Returns
A value between 0.0 and 1.0, meaning no intersection to fully overlaping

◆ 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.

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

◆ Contains() [1/2]

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

Check if the polygon contains a line segment.

Parameters
line_segmentThe target line segment. To check if the polygon contains it.
Returns
Whether the polygon contains the line segment or not.

◆ Contains() [2/2]

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

Check if the polygon contains another polygon.

Parameters
polygonThe target polygon. To check if this polygon contains it.
Returns
Whether this polygon contains another polygon or not.

◆ DebugString()

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

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

Returns
Essential information about the polygon for debugging purpose.

◆ 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.

Parameters
pointThe point to compute whose square of distance to the polygon.
Returns
The square of distance from the point to the polygon.

◆ DistanceTo() [1/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.

Parameters
pointThe point to compute whose distance to the polygon.
Returns
The distance from the point to the polygon.

◆ 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.

Parameters
line_segmentThe line segment to compute whose distance to the polygon.
Returns
The distance from the line segment to the polygon.

◆ DistanceTo() [3/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.

Parameters
boxThe box to compute whose distance to the polygon.
Returns
The distance from the box to the polygon.

◆ DistanceTo() [4/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.

Parameters
polygonThe polygon to compute whose distance to this polygon.
Returns
The distance from the other polygon to this polygon.

◆ 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.

Parameters
pointThe point to compute whose distance to the polygon.
Returns
The distance from the point to the polygon's boundary.

◆ ExpandByDistance()

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

Expand this polygon by a distance.

Parameters
distanceThe specified distance. To expand this polygon by it.
Returns
The polygon after expansion.

◆ 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.

Parameters
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.

◆ 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.

Parameters
line_segmentThe target line segment. To get its all overlapped line segments with this polygon.
Returns
A group of overlapped line segments.

◆ GetAllVertices() [1/2]

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

Get all vertices of the polygon.

Parameters
Allvertices of the polygon

◆ GetAllVertices() [2/2]

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

Get all vertices of the polygon.

◆ 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.

Parameters
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.
Returns
If the target line segment has overlap with this polygon.

◆ HasOverlap() [1/2]

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

Check if a line segment has overlap with this polygon.

Parameters
line_segmentThe target line segment. To check if it has overlap with this polygon.
Returns
Whether the target line segment has overlap with this polygon or not.

◆ HasOverlap() [2/2]

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

Check if this polygon has overlap with another polygon.

Parameters
polygonThe target polygon. To check if it has overlap with this polygon.
Returns
If this polygon has overlap with another polygon.

◆ is_convex()

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

Check if the polygon is convex.

Returns
Whether the polygon is convex or not.

◆ IsPointIn()

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

Check if a point is within the polygon.

Parameters
pointThe target point. To check if it is within the polygon.
Returns
Whether a point is within the polygon or not.

◆ IsPointOnBoundary()

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

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

Parameters
pointThe target point. To check if it is on the boundary of the polygon.
Returns
Whether a point is on the boundary of the polygon or not.

◆ line_segments()

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

Get the edges of the polygon.

Returns
The edges of the polygon.

◆ max_x()

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

◆ max_y()

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

◆ min_x()

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

◆ min_y()

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

◆ MinAreaBoundingBox()

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

Get the bounding box with the minimal area.

Returns
The bounding box with the minimal area.

◆ Next()

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

◆ num_points()

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

Get the number of vertices of the polygon.

Returns
The number of vertices of the polygon.

◆ points()

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

Get the vertices of the polygon.

Returns
The vertices of the polygon.

◆ Prev()

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

Member Data Documentation

◆ area_

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

◆ is_convex_

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

◆ line_segments_

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

◆ max_x_

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

◆ max_y_

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

◆ min_x_

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

◆ min_y_

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

◆ num_points_

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

◆ points_

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

The documentation for this class was generated from the following file: