aabox2d.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 Defines the AABox2d class.
  19. */
  20. #pragma once
  21. #include <string>
  22. #include <vector>
  23. #include "modules/common/math/vec2d.h"
  24. /**
  25. * @namespace apollo::common::math
  26. * @brief apollo::common::math
  27. */
  28. namespace apollo {
  29. namespace common {
  30. namespace math {
  31. /**
  32. * @class AABox2d
  33. * @brief Implements a class of (undirected) axes-aligned bounding boxes in 2-D.
  34. * This class is referential-agnostic.
  35. */
  36. class AABox2d {
  37. public:
  38. /**
  39. * @brief Default constructor.
  40. * Creates an axes-aligned box with zero length and width at the origin.
  41. */
  42. AABox2d() = default;
  43. /**
  44. * @brief Parameterized constructor.
  45. * Creates an axes-aligned box with given center, length, and width.
  46. * @param center The center of the box
  47. * @param length The size of the box along the x-axis
  48. * @param width The size of the box along the y-axis
  49. */
  50. AABox2d(const Vec2d &center, const double length, const double width);
  51. /**
  52. * @brief Parameterized constructor.
  53. * Creates an axes-aligned box from two opposite corners.
  54. * @param one_corner One corner of the box
  55. * @param opposite_corner The opposite corner to the first one
  56. */
  57. AABox2d(const Vec2d &one_corner, const Vec2d &opposite_corner);
  58. /**
  59. * @brief Parameterized constructor.
  60. * Creates an axes-aligned box containing all points in a given vector.
  61. * @param points Vector of points to be included inside the box.
  62. */
  63. explicit AABox2d(const std::vector<Vec2d> &points);
  64. /**
  65. * @brief Getter of center_
  66. * @return Center of the box
  67. */
  68. const Vec2d &center() const { return center_; }
  69. /**
  70. * @brief Getter of x-component of center_
  71. * @return x-component of the center of the box
  72. */
  73. double center_x() const { return center_.x(); }
  74. /**
  75. * @brief Getter of y-component of center_
  76. * @return y-component of the center of the box
  77. */
  78. double center_y() const { return center_.y(); }
  79. /**
  80. * @brief Getter of length_
  81. * @return The length of the box
  82. */
  83. double length() const { return length_; }
  84. /**
  85. * @brief Getter of width_
  86. * @return The width of the box
  87. */
  88. double width() const { return width_; }
  89. /**
  90. * @brief Getter of half_length_
  91. * @return Half of the length of the box
  92. */
  93. double half_length() const { return half_length_; }
  94. /**
  95. * @brief Getter of half_width_
  96. * @return Half of the width of the box
  97. */
  98. double half_width() const { return half_width_; }
  99. /**
  100. * @brief Getter of length_*width_
  101. * @return The area of the box
  102. */
  103. double area() const { return length_ * width_; }
  104. /**
  105. * @brief Returns the minimum x-coordinate of the box
  106. *
  107. * @return x-coordinate
  108. */
  109. double min_x() const { return center_.x() - half_length_; }
  110. /**
  111. * @brief Returns the maximum x-coordinate of the box
  112. *
  113. * @return x-coordinate
  114. */
  115. double max_x() const { return center_.x() + half_length_; }
  116. /**
  117. * @brief Returns the minimum y-coordinate of the box
  118. *
  119. * @return y-coordinate
  120. */
  121. double min_y() const { return center_.y() - half_width_; }
  122. /**
  123. * @brief Returns the maximum y-coordinate of the box
  124. *
  125. * @return y-coordinate
  126. */
  127. double max_y() const { return center_.y() + half_width_; }
  128. /**
  129. * @brief Gets all corners in counter clockwise order.
  130. *
  131. * @param corners Output where the corners are written
  132. */
  133. void GetAllCorners(std::vector<Vec2d> *const corners) const;
  134. /**
  135. * @brief Determines whether a given point is in the box.
  136. *
  137. * @param point The point we wish to test for containment in the box
  138. */
  139. bool IsPointIn(const Vec2d &point) const;
  140. /**
  141. * @brief Determines whether a given point is on the boundary of the box.
  142. *
  143. * @param point The point we wish to test for boundary membership
  144. */
  145. bool IsPointOnBoundary(const Vec2d &point) const;
  146. /**
  147. * @brief Determines the distance between a point and the box.
  148. *
  149. * @param point The point whose distance to the box we wish to determine.
  150. */
  151. double DistanceTo(const Vec2d &point) const;
  152. /**
  153. * @brief Determines the distance between two boxes.
  154. *
  155. * @param box Another box.
  156. */
  157. double DistanceTo(const AABox2d &box) const;
  158. /**
  159. * @brief Determines whether two boxes overlap.
  160. *
  161. * @param box Another box
  162. */
  163. bool HasOverlap(const AABox2d &box) const;
  164. /**
  165. * @brief Shift the center of AABox by the input vector.
  166. *
  167. * @param shift_vec The vector by which we wish to shift the box
  168. */
  169. void Shift(const Vec2d &shift_vec);
  170. /**
  171. * @brief Changes box to include another given box, as well as the current
  172. * one.
  173. *
  174. * @param other_box Another box
  175. */
  176. void MergeFrom(const AABox2d &other_box);
  177. /**
  178. * @brief Changes box to include a given point, as well as the current box.
  179. *
  180. * @param other_point Another point
  181. */
  182. void MergeFrom(const Vec2d &other_point);
  183. /**
  184. * @brief Gets a human-readable debug string
  185. *
  186. * @return A string
  187. */
  188. std::string DebugString() const;
  189. private:
  190. Vec2d center_;
  191. double length_ = 0.0;
  192. double width_ = 0.0;
  193. double half_length_ = 0.0;
  194. double half_width_ = 0.0;
  195. };
  196. } // namespace math
  197. } // namespace common
  198. } // namespace apollo