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

Line segment in 2-D. 更多...

#include <line_segment2d.h>

apollo::common::math::LineSegment2d 的协作图:

Public 成员函数

 LineSegment2d ()
 Empty constructor.
 
 LineSegment2d (const Vec2d &start, const Vec2d &end)
 Constructor with start point and end point.
 
const Vec2dstart () const
 Get the start point.
 
const Vec2dend () const
 Get the end point.
 
const Vec2dunit_direction () const
 Get the unit direction from the start point to the end point.
 
Vec2d center () const
 Get the center of the line segment.
 
Vec2d rotate (const double angle)
 Get a new line-segment with the same start point, but rotated counterclock-wise by the given amount.
 
double heading () const
 Get the heading of the line segment.
 
double cos_heading () const
 Get the cosine of the heading.
 
double sin_heading () const
 Get the sine of the heading.
 
double length () const
 Get the length of the line segment.
 
double length_sqr () const
 Get the square of length of the line segment.
 
double DistanceTo (const Vec2d &point) const
 Compute the shortest distance from a point on the line segment to a point in 2-D.
 
double DistanceTo (const Vec2d &point, Vec2d *const nearest_pt) const
 Compute the shortest distance from a point on the line segment to a point in 2-D, and get the nearest point on the line segment.
 
double DistanceSquareTo (const Vec2d &point) const
 Compute the square of the shortest distance from a point on the line segment to a point in 2-D.
 
double DistanceSquareTo (const Vec2d &point, Vec2d *const nearest_pt) const
 Compute the square of the shortest distance from a point on the line segment to a point in 2-D, and get the nearest point on the line segment.
 
bool IsPointIn (const Vec2d &point) const
 Check if a point is within the line segment.
 
bool HasIntersect (const LineSegment2d &other_segment) const
 Check if the line segment has an intersect with another line segment in 2-D.
 
bool GetIntersect (const LineSegment2d &other_segment, Vec2d *const point) const
 Compute the intersect with another line segment in 2-D if any.
 
double ProjectOntoUnit (const Vec2d &point) const
 Compute the projection of a vector onto the line segment.
 
double ProductOntoUnit (const Vec2d &point) const
 Compute the cross product of a vector onto the line segment.
 
double GetPerpendicularFoot (const Vec2d &point, Vec2d *const foot_point) const
 Compute perpendicular foot of a point in 2-D on the straight line expanded from the line segment.
 
std::string DebugString () const
 Get the debug string including the essential information.
 

详细描述

Line segment in 2-D.

在文件 line_segment2d.h40 行定义.

构造及析构函数说明

◆ LineSegment2d() [1/2]

apollo::common::math::LineSegment2d::LineSegment2d ( )

Empty constructor.

在文件 line_segment2d.cc42 行定义.

42{ unit_direction_ = Vec2d(1, 0); }

◆ LineSegment2d() [2/2]

apollo::common::math::LineSegment2d::LineSegment2d ( const Vec2d start,
const Vec2d end 
)

Constructor with start point and end point.

参数
startThe start point of the line segment.
endThe end point of the line segment.

在文件 line_segment2d.cc44 行定义.

45 : start_(start), end_(end) {
46 const double dx = end_.x() - start_.x();
47 const double dy = end_.y() - start_.y();
48 length_ = hypot(dx, dy);
49 unit_direction_ =
50 (length_ <= kMathEpsilon ? Vec2d(0, 0)
51 : Vec2d(dx / length_, dy / length_));
52 heading_ = unit_direction_.Angle();
53}
const Vec2d & end() const
Get the end point.
const Vec2d & start() const
Get the start point.
double y() const
Getter for y component
Definition vec2d.h:57
double Angle() const
Gets the angle between the vector and the positive x semi-axis
Definition vec2d.cc:37
double x() const
Getter for x component
Definition vec2d.h:54
constexpr double kMathEpsilon
Definition vec2d.h:35

成员函数说明

◆ center()

Vec2d apollo::common::math::LineSegment2d::center ( ) const
inline

Get the center of the line segment.

返回
The center of the line segment.

在文件 line_segment2d.h76 行定义.

76{ return (start_ + end_) / 2.0; }

◆ cos_heading()

double apollo::common::math::LineSegment2d::cos_heading ( ) const
inline

Get the cosine of the heading.

返回
The cosine of the heading.

在文件 line_segment2d.h94 行定义.

94{ return unit_direction_.x(); }

◆ DebugString()

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

Get the debug string including the essential information.

返回
Information of the line segment for debugging.

在文件 line_segment2d.cc222 行定义.

222 {
223 return absl::StrCat("segment2d ( start = ", start_.DebugString(),
224 " end = ", end_.DebugString(), " )");
225}
std::string DebugString() const
Returns a human-readable string representing this object
Definition vec2d.cc:125

◆ DistanceSquareTo() [1/2]

double apollo::common::math::LineSegment2d::DistanceSquareTo ( const Vec2d point) const

Compute the square of the shortest distance from a point on the line segment to a point in 2-D.

参数
pointThe point to compute the squared of the distance to.
返回
The square of the shortest distance from points on the line segment to the input point.

在文件 line_segment2d.cc103 行定义.

103 {
104 if (length_ <= kMathEpsilon) {
105 return point.DistanceSquareTo(start_);
106 }
107 const double x0 = point.x() - start_.x();
108 const double y0 = point.y() - start_.y();
109 const double proj = x0 * unit_direction_.x() + y0 * unit_direction_.y();
110 if (proj <= 0.0) {
111 return Square(x0) + Square(y0);
112 }
113 if (proj >= length_) {
114 return point.DistanceSquareTo(end_);
115 }
116 return Square(x0 * unit_direction_.y() - y0 * unit_direction_.x());
117}
T Square(const T value)
Compute squared value.
Definition math_utils.h:141

◆ DistanceSquareTo() [2/2]

double apollo::common::math::LineSegment2d::DistanceSquareTo ( const Vec2d point,
Vec2d *const  nearest_pt 
) const

Compute the square of the shortest distance from a point on the line segment to a point in 2-D, and get the nearest point on the line segment.

参数
pointThe point to compute the squared of the distance to.
nearest_ptThe nearest point on the line segment to the input point.
返回
The shortest distance from points on the line segment to the input point.

在文件 line_segment2d.cc119 行定义.

120 {
121 CHECK_NOTNULL(nearest_pt);
122 if (length_ <= kMathEpsilon) {
123 *nearest_pt = start_;
124 return point.DistanceSquareTo(start_);
125 }
126 const double x0 = point.x() - start_.x();
127 const double y0 = point.y() - start_.y();
128 const double proj = x0 * unit_direction_.x() + y0 * unit_direction_.y();
129 if (proj <= 0.0) {
130 *nearest_pt = start_;
131 return Square(x0) + Square(y0);
132 }
133 if (proj >= length_) {
134 *nearest_pt = end_;
135 return point.DistanceSquareTo(end_);
136 }
137 *nearest_pt = start_ + unit_direction_ * proj;
138 return Square(x0 * unit_direction_.y() - y0 * unit_direction_.x());
139}
double DistanceSquareTo(const Vec2d &other) const
Returns the squared distance to the given vector
Definition vec2d.cc:51

◆ DistanceTo() [1/2]

double apollo::common::math::LineSegment2d::DistanceTo ( const Vec2d point) const

Compute the shortest distance from a point on the line segment to a point in 2-D.

参数
pointThe point to compute the distance to.
返回
The shortest distance from points on the line segment to point.

在文件 line_segment2d.cc65 行定义.

65 {
66 if (length_ <= kMathEpsilon) {
67 return point.DistanceTo(start_);
68 }
69 const double x0 = point.x() - start_.x();
70 const double y0 = point.y() - start_.y();
71 const double proj = x0 * unit_direction_.x() + y0 * unit_direction_.y();
72 if (proj <= 0.0) {
73 return hypot(x0, y0);
74 }
75 if (proj >= length_) {
76 return point.DistanceTo(end_);
77 }
78 return std::abs(x0 * unit_direction_.y() - y0 * unit_direction_.x());
79}

◆ DistanceTo() [2/2]

double apollo::common::math::LineSegment2d::DistanceTo ( const Vec2d point,
Vec2d *const  nearest_pt 
) const

Compute the shortest distance from a point on the line segment to a point in 2-D, and get the nearest point on the line segment.

参数
pointThe point to compute the distance to.
nearest_ptThe nearest point on the line segment to the input point.
返回
The shortest distance from points on the line segment to the input point.

在文件 line_segment2d.cc81 行定义.

82 {
83 CHECK_NOTNULL(nearest_pt);
84 if (length_ <= kMathEpsilon) {
85 *nearest_pt = start_;
86 return point.DistanceTo(start_);
87 }
88 const double x0 = point.x() - start_.x();
89 const double y0 = point.y() - start_.y();
90 const double proj = x0 * unit_direction_.x() + y0 * unit_direction_.y();
91 if (proj < 0.0) {
92 *nearest_pt = start_;
93 return hypot(x0, y0);
94 }
95 if (proj > length_) {
96 *nearest_pt = end_;
97 return point.DistanceTo(end_);
98 }
99 *nearest_pt = start_ + unit_direction_ * proj;
100 return std::abs(x0 * unit_direction_.y() - y0 * unit_direction_.x());
101}
double DistanceTo(const Vec2d &other) const
Returns the distance to the given vector
Definition vec2d.cc:47

◆ end()

const Vec2d & apollo::common::math::LineSegment2d::end ( ) const
inline

Get the end point.

返回
The end point of the line segment.

在文件 line_segment2d.h64 行定义.

64{ return end_; }

◆ GetIntersect()

bool apollo::common::math::LineSegment2d::GetIntersect ( const LineSegment2d other_segment,
Vec2d *const  point 
) const

Compute the intersect with another line segment in 2-D if any.

参数
other_segmentThe line segment to compute the intersect.
pointthe computed intersect between the line segment and the input other_segment.
返回
Whether the line segment has an intersect with the input other_segment.

在文件 line_segment2d.cc167 行定义.

168 {
169 CHECK_NOTNULL(point);
170 if (IsPointIn(other_segment.start())) {
171 *point = other_segment.start();
172 return true;
173 }
174 if (IsPointIn(other_segment.end())) {
175 *point = other_segment.end();
176 return true;
177 }
178 if (other_segment.IsPointIn(start_)) {
179 *point = start_;
180 return true;
181 }
182 if (other_segment.IsPointIn(end_)) {
183 *point = end_;
184 return true;
185 }
186 if (length_ <= kMathEpsilon || other_segment.length() <= kMathEpsilon) {
187 return false;
188 }
189 const double cc1 = CrossProd(start_, end_, other_segment.start());
190 const double cc2 = CrossProd(start_, end_, other_segment.end());
191 if (cc1 * cc2 >= -kMathEpsilon) {
192 return false;
193 }
194 const double cc3 =
195 CrossProd(other_segment.start(), other_segment.end(), start_);
196 const double cc4 =
197 CrossProd(other_segment.start(), other_segment.end(), end_);
198 if (cc3 * cc4 >= -kMathEpsilon) {
199 return false;
200 }
201 const double ratio = cc4 / (cc4 - cc3);
202 *point = Vec2d(start_.x() * ratio + end_.x() * (1.0 - ratio),
203 start_.y() * ratio + end_.y() * (1.0 - ratio));
204 return true;
205}
bool IsPointIn(const Vec2d &point) const
Check if a point is within the line segment.
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

◆ GetPerpendicularFoot()

double apollo::common::math::LineSegment2d::GetPerpendicularFoot ( const Vec2d point,
Vec2d *const  foot_point 
) const

Compute perpendicular foot of a point in 2-D on the straight line expanded from the line segment.

参数
pointThe point to compute the perpendicular foot from.
foot_pointThe computed perpendicular foot from the input point to the straight line expanded from the line segment.
返回
The distance from the input point to the perpendicular foot.

在文件 line_segment2d.cc208 行定义.

209 {
210 CHECK_NOTNULL(foot_point);
211 if (length_ <= kMathEpsilon) {
212 *foot_point = start_;
213 return point.DistanceTo(start_);
214 }
215 const double x0 = point.x() - start_.x();
216 const double y0 = point.y() - start_.y();
217 const double proj = x0 * unit_direction_.x() + y0 * unit_direction_.y();
218 *foot_point = start_ + unit_direction_ * proj;
219 return std::abs(x0 * unit_direction_.y() - y0 * unit_direction_.x());
220}

◆ HasIntersect()

bool apollo::common::math::LineSegment2d::HasIntersect ( const LineSegment2d other_segment) const

Check if the line segment has an intersect with another line segment in 2-D.

参数
other_segmentThe line segment to check if it has an intersect.
返回
Whether the line segment has an intersect with the input other_segment.

在文件 line_segment2d.cc162 行定义.

162 {
163 Vec2d point;
164 return GetIntersect(other_segment, &point);
165}
bool GetIntersect(const LineSegment2d &other_segment, Vec2d *const point) const
Compute the intersect with another line segment in 2-D if any.

◆ heading()

double apollo::common::math::LineSegment2d::heading ( ) const
inline

Get the heading of the line segment.

返回
The heading, which is the angle between unit direction and x-axis.

在文件 line_segment2d.h88 行定义.

88{ return heading_; }

◆ IsPointIn()

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

Check if a point is within the line segment.

参数
pointThe point to check if it is within the line segment.
返回
Whether the input point is within the line segment or not.

在文件 line_segment2d.cc141 行定义.

141 {
142 if (length_ <= kMathEpsilon) {
143 return std::abs(point.x() - start_.x()) <= kMathEpsilon &&
144 std::abs(point.y() - start_.y()) <= kMathEpsilon;
145 }
146 const double prod = CrossProd(point, start_, end_);
147 if (std::abs(prod) > kMathEpsilon) {
148 return false;
149 }
150 return IsWithin(point.x(), start_.x(), end_.x()) &&
151 IsWithin(point.y(), start_.y(), end_.y());
152}

◆ length()

double apollo::common::math::LineSegment2d::length ( ) const

Get the length of the line segment.

返回
The length of the line segment.

在文件 line_segment2d.cc61 行定义.

61{ return length_; }

◆ length_sqr()

double apollo::common::math::LineSegment2d::length_sqr ( ) const

Get the square of length of the line segment.

返回
The square of length of the line segment.

在文件 line_segment2d.cc63 行定义.

63{ return length_ * length_; }

◆ ProductOntoUnit()

double apollo::common::math::LineSegment2d::ProductOntoUnit ( const Vec2d point) const

Compute the cross product of a vector onto the line segment.

参数
pointThe end of the vector (starting from the start point of the line segment) to compute the cross product onto the line segment.
返回
The cross product of the unit direction and the vector, which is from the start point of the line segment to the input point.

在文件 line_segment2d.cc158 行定义.

158 {
159 return unit_direction_.CrossProd(point - start_);
160}
double CrossProd(const Vec2d &other) const
Returns the "cross" product between these two Vec2d (non-standard).
Definition vec2d.cc:57

◆ ProjectOntoUnit()

double apollo::common::math::LineSegment2d::ProjectOntoUnit ( const Vec2d point) const

Compute the projection of a vector onto the line segment.

参数
pointThe end of the vector (starting from the start point of the line segment) to compute the projection onto the line segment.
返回
The projection of the vector, which is from the start point of the line segment to the input point, onto the unit direction.

在文件 line_segment2d.cc154 行定义.

154 {
155 return unit_direction_.InnerProd(point - start_);
156}
double InnerProd(const Vec2d &other) const
Returns the inner product between these two Vec2d.
Definition vec2d.cc:61

◆ rotate()

Vec2d apollo::common::math::LineSegment2d::rotate ( const double  angle)

Get a new line-segment with the same start point, but rotated counterclock-wise by the given amount.

返回
The rotated line-segment's end-point.

在文件 line_segment2d.cc55 行定义.

55 {
56 Vec2d diff_vec = end_ - start_;
57 diff_vec.SelfRotate(angle);
58 return start_ + diff_vec;
59}
void SelfRotate(const double angle)
rotate the vector itself by angle.
Definition vec2d.cc:70

◆ sin_heading()

double apollo::common::math::LineSegment2d::sin_heading ( ) const
inline

Get the sine of the heading.

返回
The sine of the heading.

在文件 line_segment2d.h100 行定义.

100{ return unit_direction_.y(); }

◆ start()

const Vec2d & apollo::common::math::LineSegment2d::start ( ) const
inline

Get the start point.

返回
The start point of the line segment.

在文件 line_segment2d.h58 行定义.

58{ return start_; }

◆ unit_direction()

const Vec2d & apollo::common::math::LineSegment2d::unit_direction ( ) const
inline

Get the unit direction from the start point to the end point.

返回
The start point of the line segment.

在文件 line_segment2d.h70 行定义.

70{ return unit_direction_; }

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