line_segment2d.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  17. * @file
  18. * @brief Define the LineSegment2d class.
  19. */
  20. #pragma once
  21. #include <string>
  22. #include "modules/common/math/vec2d.h"
  23. /**
  24. * @namespace apollo::common::math
  25. * @brief apollo::common::math
  26. */
  27. namespace apollo {
  28. namespace common {
  29. namespace math {
  30. /**
  31. * @class LineSegment2d
  32. * @brief Line segment in 2-D.
  33. */
  34. class LineSegment2d {
  35. public:
  36. /**
  37. * @brief Empty constructor.
  38. */
  39. LineSegment2d();
  40. /**
  41. * @brief Constructor with start point and end point.
  42. * @param start The start point of the line segment.
  43. * @param end The end point of the line segment.
  44. */
  45. LineSegment2d(const Vec2d &start, const Vec2d &end);
  46. /**
  47. * @brief Get the start point.
  48. * @return The start point of the line segment.
  49. */
  50. const Vec2d &start() const { return start_; }
  51. /**
  52. * @brief Get the end point.
  53. * @return The end point of the line segment.
  54. */
  55. const Vec2d &end() const { return end_; }
  56. /**
  57. * @brief Get the unit direction from the start point to the end point.
  58. * @return The start point of the line segment.
  59. */
  60. const Vec2d &unit_direction() const { return unit_direction_; }
  61. /**
  62. * @brief Get the center of the line segment.
  63. * @return The center of the line segment.
  64. */
  65. Vec2d center() const { return (start_ + end_) / 2.0; }
  66. /** @brief Get a new line-segment with the same start point, but rotated
  67. * counterclock-wise by the given amount.
  68. * @return The rotated line-segment's end-point.
  69. */
  70. Vec2d rotate(const double angle);
  71. /**
  72. * @brief Get the heading of the line segment.
  73. * @return The heading, which is the angle between unit direction and x-axis.
  74. */
  75. double heading() const { return heading_; }
  76. /**
  77. * @brief Get the cosine of the heading.
  78. * @return The cosine of the heading.
  79. */
  80. double cos_heading() const { return unit_direction_.x(); }
  81. /**
  82. * @brief Get the sine of the heading.
  83. * @return The sine of the heading.
  84. */
  85. double sin_heading() const { return unit_direction_.y(); }
  86. /**
  87. * @brief Get the length of the line segment.
  88. * @return The length of the line segment.
  89. */
  90. double length() const;
  91. /**
  92. * @brief Get the square of length of the line segment.
  93. * @return The square of length of the line segment.
  94. */
  95. double length_sqr() const;
  96. /**
  97. * @brief Compute the shortest distance from a point on the line segment
  98. * to a point in 2-D.
  99. * @param point The point to compute the distance to.
  100. * @return The shortest distance from points on the line segment to point.
  101. */
  102. double DistanceTo(const Vec2d &point) const;
  103. /**
  104. * @brief Compute the shortest distance from a point on the line segment
  105. * to a point in 2-D, and get the nearest point on the line segment.
  106. * @param point The point to compute the distance to.
  107. * @param nearest_pt The nearest point on the line segment
  108. * to the input point.
  109. * @return The shortest distance from points on the line segment
  110. * to the input point.
  111. */
  112. double DistanceTo(const Vec2d &point, Vec2d *const nearest_pt) const;
  113. /**
  114. * @brief Compute the square of the shortest distance from a point
  115. * on the line segment to a point in 2-D.
  116. * @param point The point to compute the squared of the distance to.
  117. * @return The square of the shortest distance from points
  118. * on the line segment to the input point.
  119. */
  120. double DistanceSquareTo(const Vec2d &point) const;
  121. /**
  122. * @brief Compute the square of the shortest distance from a point
  123. * on the line segment to a point in 2-D,
  124. * and get the nearest point on the line segment.
  125. * @param point The point to compute the squared of the distance to.
  126. * @param nearest_pt The nearest point on the line segment
  127. * to the input point.
  128. * @return The shortest distance from points on the line segment
  129. * to the input point.
  130. */
  131. double DistanceSquareTo(const Vec2d &point, Vec2d *const nearest_pt) const;
  132. /**
  133. * @brief Check if a point is within the line segment.
  134. * @param point The point to check if it is within the line segment.
  135. * @return Whether the input point is within the line segment or not.
  136. */
  137. bool IsPointIn(const Vec2d &point) const;
  138. /**
  139. * @brief Check if the line segment has an intersect
  140. * with another line segment in 2-D.
  141. * @param other_segment The line segment to check if it has an intersect.
  142. * @return Whether the line segment has an intersect
  143. * with the input other_segment.
  144. */
  145. bool HasIntersect(const LineSegment2d &other_segment) const;
  146. /**
  147. * @brief Compute the intersect with another line segment in 2-D if any.
  148. * @param other_segment The line segment to compute the intersect.
  149. * @param point the computed intersect between the line segment and
  150. * the input other_segment.
  151. * @return Whether the line segment has an intersect
  152. * with the input other_segment.
  153. */
  154. bool GetIntersect(const LineSegment2d &other_segment,
  155. Vec2d *const point) const;
  156. /**
  157. * @brief Compute the projection of a vector onto the line segment.
  158. * @param point The end of the vector (starting from the start point of the
  159. * line segment) to compute the projection onto the line segment.
  160. * @return The projection of the vector, which is from the start point of
  161. * the line segment to the input point, onto the unit direction.
  162. */
  163. double ProjectOntoUnit(const Vec2d &point) const;
  164. /**
  165. * @brief Compute the cross product of a vector onto the line segment.
  166. * @param point The end of the vector (starting from the start point of the
  167. * line segment) to compute the cross product onto the line segment.
  168. * @return The cross product of the unit direction and
  169. * the vector, which is from the start point of
  170. * the line segment to the input point.
  171. */
  172. double ProductOntoUnit(const Vec2d &point) const;
  173. /**
  174. * @brief Compute perpendicular foot of a point in 2-D on the straight line
  175. * expanded from the line segment.
  176. * @param point The point to compute the perpendicular foot from.
  177. * @param foot_point The computed perpendicular foot from the input point to
  178. * the straight line expanded from the line segment.
  179. * @return The distance from the input point to the perpendicular foot.
  180. */
  181. double GetPerpendicularFoot(const Vec2d &point,
  182. Vec2d *const foot_point) const;
  183. /**
  184. * @brief Get the debug string including the essential information.
  185. * @return Information of the line segment for debugging.
  186. */
  187. std::string DebugString() const;
  188. private:
  189. Vec2d start_;
  190. Vec2d end_;
  191. Vec2d unit_direction_;
  192. double heading_ = 0.0;
  193. double length_ = 0.0;
  194. };
  195. } // namespace math
  196. } // namespace common
  197. } // namespace apollo