Apollo 10.0
自动驾驶开放平台
box2d.h
浏览该文件的文档.
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
37namespace apollo {
38namespace common {
39namespace math {
40
52class Box2d {
53 public:
54 Box2d() = default;
63 Box2d(const Vec2d &center, const double heading, const double length,
64 const double width);
65
76 Box2d(const Vec2d &point, double heading, double front_length,
77 double back_length, double width);
78
85 Box2d(const LineSegment2d &axis, const double width);
86
91 explicit Box2d(const AABox2d &aabox);
92
99 static Box2d CreateAABox(const Vec2d &one_corner,
100 const Vec2d &opposite_corner);
101
106 const Vec2d &center() const { return center_; }
107
112 double center_x() const { return center_.x(); }
113
118 double center_y() const { return center_.y(); }
119
124 double length() const { return length_; }
125
130 double width() const { return width_; }
131
136 double half_length() const { return half_length_; }
137
142 double half_width() const { return half_width_; }
143
148 double heading() const { return heading_; }
149
154 double cos_heading() const { return cos_heading_; }
155
160 double sin_heading() const { return sin_heading_; }
161
166 double area() const { return length_ * width_; }
167
172 double diagonal() const { return std::hypot(length_, width_); }
173
178 void GetAllCorners(std::vector<Vec2d> *const corners) const;
179
184 const std::vector<Vec2d> &GetAllCorners() const;
185
191 bool IsPointIn(const Vec2d &point) const;
192
198 bool IsPointOnBoundary(const Vec2d &point) const;
199
205 double DistanceTo(const Vec2d &point) const;
206
212 double DistanceTo(const LineSegment2d &line_segment) const;
213
219 double DistanceTo(const Box2d &box) const;
220
226 bool HasOverlap(const LineSegment2d &line_segment) const;
227
233 bool HasOverlap(const Box2d &box) const;
234
239 AABox2d GetAABox() const;
240
245 void RotateFromCenter(const double rotate_angle);
246
251 void Shift(const Vec2d &shift_vec);
252
257 void LongitudinalExtend(const double extension_length);
258
259 void LateralExtend(const double extension_length);
260
265 std::string DebugString() const;
266
267 void InitCorners();
268
269 double max_x() const { return max_x_; }
270 double min_x() const { return min_x_; }
271 double max_y() const { return max_y_; }
272 double min_y() const { return min_y_; }
273
274 private:
275 inline bool is_inside_rectangle(const Vec2d &point) const {
276 return (point.x() >= 0.0 && point.x() <= width_ && point.y() >= 0.0 &&
277 point.y() <= length_);
278 }
279
280 Vec2d center_;
281 double length_ = 0.0;
282 double width_ = 0.0;
283 double half_length_ = 0.0;
284 double half_width_ = 0.0;
285 double heading_ = 0.0;
286 double cos_heading_ = 1.0;
287 double sin_heading_ = 0.0;
288
289 std::vector<Vec2d> corners_;
290
291 double max_x_ = std::numeric_limits<double>::lowest();
292 double min_x_ = std::numeric_limits<double>::max();
293 double max_y_ = std::numeric_limits<double>::lowest();
294 double min_y_ = std::numeric_limits<double>::max();
295};
296
297} // namespace math
298} // namespace common
299} // namespace apollo
Defines the AABox2d class.
Implements a class of (undirected) axes-aligned bounding boxes in 2-D.
Definition aabox2d.h:42
Rectangular (undirected) bounding box in 2-D.
Definition box2d.h:52
std::string DebugString() const
Gets a human-readable description of the box
Definition box2d.cc:418
void Shift(const Vec2d &shift_vec)
Shifts this box by a given vector
Definition box2d.cc:393
double diagonal() const
Getter of the size of the diagonal of the box
Definition box2d.h:172
bool IsPointOnBoundary(const Vec2d &point) const
Tests points for membership in the boundary of the box
Definition box2d.cc:157
double min_y() const
Definition box2d.h:272
double half_width() const
Getter of half the width
Definition box2d.h:142
void LateralExtend(const double extension_length)
Definition box2d.cc:412
bool HasOverlap(const LineSegment2d &line_segment) const
Determines whether this box overlaps a given line segment
Definition box2d.cc:184
AABox2d GetAABox() const
Gets the smallest axes-aligned box containing the current one
Definition box2d.cc:378
double center_x() const
Getter of the x-coordinate of the center of the box
Definition box2d.h:112
void RotateFromCenter(const double rotate_angle)
Rotate from center.
Definition box2d.cc:386
double width() const
Getter of the width
Definition box2d.h:130
const Vec2d & center() const
Getter of the center of the box
Definition box2d.h:106
double length() const
Getter of the length
Definition box2d.h:124
bool IsPointIn(const Vec2d &point) const
Tests points for membership in the box
Definition box2d.cc:149
double half_length() const
Getter of half the length
Definition box2d.h:136
double min_x() const
Definition box2d.h:270
double heading() const
Getter of the heading
Definition box2d.h:148
double sin_heading() const
Getter of the sine of the heading
Definition box2d.h:160
double DistanceTo(const Vec2d &point) const
Determines the distance between the box and a given point
Definition box2d.cc:168
double area() const
Getter of the area of the box
Definition box2d.h:166
double cos_heading() const
Getter of the cosine of the heading
Definition box2d.h:154
double max_y() const
Definition box2d.h:271
const std::vector< Vec2d > & GetAllCorners() const
Getter of the corners of the box
Definition box2d.cc:147
static Box2d CreateAABox(const Vec2d &one_corner, const Vec2d &opposite_corner)
Creates an axes-aligned Box2d from two opposite corners
Definition box2d.cc:131
void LongitudinalExtend(const double extension_length)
Extend the box longitudinally
Definition box2d.cc:406
double center_y() const
Getter of the y-coordinate of the center of the box
Definition box2d.h:118
double max_x() const
Definition box2d.h:269
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
double y() const
Getter for y component
Definition vec2d.h:57
double x() const
Getter for x component
Definition vec2d.h:54
Define the LineSegment2d class.
class register implement
Definition arena_queue.h:37
Defines the Vec2d class.