Apollo  6.0
Open source self driving car software
box2d.h
Go to the documentation of this file.
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 
23 #pragma once
24 
25 #include <limits>
26 #include <string>
27 #include <vector>
28 
32 
37 namespace apollo {
38 namespace common {
39 namespace math {
40 
52 class Box2d {
53  public:
54  Box2d() = default;
63  Box2d(const Vec2d &center, const double heading, const double length,
64  const double width);
65 
72  Box2d(const LineSegment2d &axis, const double width);
73 
78  explicit Box2d(const AABox2d &aabox);
79 
86  static Box2d CreateAABox(const Vec2d &one_corner,
87  const Vec2d &opposite_corner);
88 
93  const Vec2d &center() const { return center_; }
94 
99  double center_x() const { return center_.x(); }
100 
105  double center_y() const { return center_.y(); }
106 
111  double length() const { return length_; }
112 
117  double width() const { return width_; }
118 
123  double half_length() const { return half_length_; }
124 
129  double half_width() const { return half_width_; }
130 
135  double heading() const { return heading_; }
136 
141  double cos_heading() const { return cos_heading_; }
142 
147  double sin_heading() const { return sin_heading_; }
148 
153  double area() const { return length_ * width_; }
154 
159  double diagonal() const { return std::hypot(length_, width_); }
160 
165  void GetAllCorners(std::vector<Vec2d> *const corners) const;
166 
171  std::vector<Vec2d> GetAllCorners() const;
172 
178  bool IsPointIn(const Vec2d &point) const;
179 
185  bool IsPointOnBoundary(const Vec2d &point) const;
186 
192  double DistanceTo(const Vec2d &point) const;
193 
199  double DistanceTo(const LineSegment2d &line_segment) const;
200 
206  double DistanceTo(const Box2d &box) const;
207 
213  bool HasOverlap(const LineSegment2d &line_segment) const;
214 
220  bool HasOverlap(const Box2d &box) const;
221 
226  AABox2d GetAABox() const;
227 
232  void RotateFromCenter(const double rotate_angle);
233 
238  void Shift(const Vec2d &shift_vec);
239 
244  void LongitudinalExtend(const double extension_length);
245 
246  void LateralExtend(const double extension_length);
247 
252  std::string DebugString() const;
253 
254  void InitCorners();
255 
256  double max_x() const { return max_x_; }
257  double min_x() const { return min_x_; }
258  double max_y() const { return max_y_; }
259  double min_y() const { return min_y_; }
260 
261  private:
262  Vec2d center_;
263  double length_ = 0.0;
264  double width_ = 0.0;
265  double half_length_ = 0.0;
266  double half_width_ = 0.0;
267  double heading_ = 0.0;
268  double cos_heading_ = 1.0;
269  double sin_heading_ = 0.0;
270 
271  std::vector<Vec2d> corners_;
272 
273  double max_x_ = std::numeric_limits<double>::lowest();
274  double min_x_ = std::numeric_limits<double>::max();
275  double max_y_ = std::numeric_limits<double>::lowest();
276  double min_y_ = std::numeric_limits<double>::max();
277 };
278 
279 } // namespace math
280 } // namespace common
281 } // namespace apollo
double x() const
Getter for x component.
Definition: vec2d.h:54
double y() const
Getter for y component.
Definition: vec2d.h:57
const Vec2d & center() const
Getter of the center of the box.
Definition: box2d.h:93
Define the LineSegment2d class.
double center_y() const
Getter of the y-coordinate of the center of the box.
Definition: box2d.h:105
void Shift(const Vec2d &shift_vec)
Shifts this box by a given vector.
Defines the Vec2d class.
void LateralExtend(const double extension_length)
double center_x() const
Getter of the x-coordinate of the center of the box.
Definition: box2d.h:99
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
double max_x() const
Definition: box2d.h:256
double DistanceTo(const Vec2d &point) const
Determines the distance between the box and a given point.
std::string DebugString() const
Gets a human-readable description of the box.
double length() const
Getter of the length.
Definition: box2d.h:111
bool IsPointIn(const Vec2d &point) const
Tests points for membership in the box.
std::vector< Vec2d > GetAllCorners() const
Getter of the corners of the box.
double cos_heading() const
Getter of the cosine of the heading.
Definition: box2d.h:141
Rectangular (undirected) bounding box in 2-D.
Definition: box2d.h:52
double diagonal() const
Getter of the size of the diagonal of the box.
Definition: box2d.h:159
bool IsPointOnBoundary(const Vec2d &point) const
Tests points for membership in the boundary of the box.
Line segment in 2-D.
Definition: line_segment2d.h:40
Defines the AABox2d class.
Implements a class of 2-dimensional vectors.
Definition: vec2d.h:42
double heading() const
Getter of the heading.
Definition: box2d.h:135
AABox2d GetAABox() const
Gets the smallest axes-aligned box containing the current one.
double max_y() const
Definition: box2d.h:258
double width() const
Getter of the width.
Definition: box2d.h:117
void RotateFromCenter(const double rotate_angle)
Rotate from center.
double min_x() const
Definition: box2d.h:257
double min_y() const
Definition: box2d.h:259
double sin_heading() const
Getter of the sine of the heading.
Definition: box2d.h:147
void LongitudinalExtend(const double extension_length)
Extend the box longitudinally.
double half_length() const
Getter of half the length.
Definition: box2d.h:123
double half_width() const
Getter of half the width.
Definition: box2d.h:129
static Box2d CreateAABox(const Vec2d &one_corner, const Vec2d &opposite_corner)
Creates an axes-aligned Box2d from two opposite corners.
bool HasOverlap(const LineSegment2d &line_segment) const
Determines whether this box overlaps a given line segment.
double area() const
Getter of the area of the box.
Definition: box2d.h:153
Implements a class of (undirected) axes-aligned bounding boxes in 2-D. This class is referential-agno...
Definition: aabox2d.h:42