Apollo  6.0
Open source self driving car software
aabox2d.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 
22 #pragma once
23 
24 #include <string>
25 #include <vector>
26 
28 
33 namespace apollo {
34 namespace common {
35 namespace math {
36 
42 class AABox2d {
43  public:
48  AABox2d() = default;
56  AABox2d(const Vec2d &center, const double length, const double width);
63  AABox2d(const Vec2d &one_corner, const Vec2d &opposite_corner);
69  explicit AABox2d(const std::vector<Vec2d> &points);
70 
75  const Vec2d &center() const { return center_; }
76 
81  double center_x() const { return center_.x(); }
82 
87  double center_y() const { return center_.y(); }
88 
93  double length() const { return length_; }
94 
99  double width() const { return width_; }
100 
105  double half_length() const { return half_length_; }
106 
111  double half_width() const { return half_width_; }
112 
117  double area() const { return length_ * width_; }
118 
124  double min_x() const { return center_.x() - half_length_; }
125 
131  double max_x() const { return center_.x() + half_length_; }
132 
138  double min_y() const { return center_.y() - half_width_; }
139 
145  double max_y() const { return center_.y() + half_width_; }
146 
152  void GetAllCorners(std::vector<Vec2d> *const corners) const;
153 
159  bool IsPointIn(const Vec2d &point) const;
160 
166  bool IsPointOnBoundary(const Vec2d &point) const;
167 
173  double DistanceTo(const Vec2d &point) const;
174 
180  double DistanceTo(const AABox2d &box) const;
181 
187  bool HasOverlap(const AABox2d &box) const;
188 
194  void Shift(const Vec2d &shift_vec);
195 
202  void MergeFrom(const AABox2d &other_box);
203 
209  void MergeFrom(const Vec2d &other_point);
210 
216  std::string DebugString() const;
217 
218  private:
219  Vec2d center_;
220  double length_ = 0.0;
221  double width_ = 0.0;
222  double half_length_ = 0.0;
223  double half_width_ = 0.0;
224 };
225 
226 } // namespace math
227 } // namespace common
228 } // namespace apollo
double x() const
Getter for x component.
Definition: vec2d.h:54
double width() const
Getter of width_.
Definition: aabox2d.h:99
bool IsPointIn(const Vec2d &point) const
Determines whether a given point is in the box.
double y() const
Getter for y component.
Definition: vec2d.h:57
void Shift(const Vec2d &shift_vec)
Shift the center of AABox by the input vector.
AABox2d()=default
Default constructor. Creates an axes-aligned box with zero length and width at the origin...
Defines the Vec2d class.
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool HasOverlap(const AABox2d &box) const
Determines whether two boxes overlap.
double center_x() const
Getter of x-component of center_.
Definition: aabox2d.h:81
bool IsPointOnBoundary(const Vec2d &point) const
Determines whether a given point is on the boundary of the box.
double center_y() const
Getter of y-component of center_.
Definition: aabox2d.h:87
double length() const
Getter of length_.
Definition: aabox2d.h:93
double half_width() const
Getter of half_width_.
Definition: aabox2d.h:111
void MergeFrom(const AABox2d &other_box)
Changes box to include another given box, as well as the current one.
void GetAllCorners(std::vector< Vec2d > *const corners) const
Gets all corners in counter clockwise order.
double area() const
Getter of length_*width_.
Definition: aabox2d.h:117
double max_y() const
Returns the maximum y-coordinate of the box.
Definition: aabox2d.h:145
double max_x() const
Returns the maximum x-coordinate of the box.
Definition: aabox2d.h:131
const Vec2d & center() const
Getter of center_.
Definition: aabox2d.h:75
std::string DebugString() const
Gets a human-readable debug string.
Implements a class of 2-dimensional vectors.
Definition: vec2d.h:42
double half_length() const
Getter of half_length_.
Definition: aabox2d.h:105
double min_y() const
Returns the minimum y-coordinate of the box.
Definition: aabox2d.h:138
double DistanceTo(const Vec2d &point) const
Determines the distance between a point and the box.
double min_x() const
Returns the minimum x-coordinate of the box.
Definition: aabox2d.h:124
Implements a class of (undirected) axes-aligned bounding boxes in 2-D. This class is referential-agno...
Definition: aabox2d.h:42