Apollo 10.0
自动驾驶开放平台
aabox2d.cc
浏览该文件的文档.
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
18
19#include <algorithm>
20#include <cmath>
21
22#include "absl/strings/str_cat.h"
23#include "cyber/common/log.h"
24
26
27namespace apollo {
28namespace common {
29namespace math {
30
31AABox2d::AABox2d(const Vec2d &center, const double length, const double width)
32 : center_(center),
33 length_(length),
34 width_(width),
35 half_length_(length / 2.0),
36 half_width_(width / 2.0) {
37 CHECK_GT(length_, -kMathEpsilon);
38 CHECK_GT(width_, -kMathEpsilon);
39}
40
41AABox2d::AABox2d(const Vec2d &one_corner, const Vec2d &opposite_corner)
42 : AABox2d((one_corner + opposite_corner) / 2.0,
43 std::abs(one_corner.x() - opposite_corner.x()),
44 std::abs(one_corner.y() - opposite_corner.y())) {}
45
46AABox2d::AABox2d(const std::vector<Vec2d> &points) {
47 ACHECK(!points.empty());
48 double min_x = points[0].x();
49 double max_x = points[0].x();
50 double min_y = points[0].y();
51 double max_y = points[0].y();
52 for (const auto &point : points) {
53 min_x = std::min(min_x, point.x());
54 max_x = std::max(max_x, point.x());
55 min_y = std::min(min_y, point.y());
56 max_y = std::max(max_y, point.y());
57 }
58
59 center_ = {(min_x + max_x) / 2.0, (min_y + max_y) / 2.0};
60 length_ = max_x - min_x;
61 width_ = max_y - min_y;
62 half_length_ = length_ / 2.0;
63 half_width_ = width_ / 2.0;
64}
65
66void AABox2d::GetAllCorners(std::vector<Vec2d> *const corners) const {
67 CHECK_NOTNULL(corners)->clear();
68 corners->reserve(4);
69 corners->emplace_back(center_.x() + half_length_, center_.y() - half_width_);
70 corners->emplace_back(center_.x() + half_length_, center_.y() + half_width_);
71 corners->emplace_back(center_.x() - half_length_, center_.y() + half_width_);
72 corners->emplace_back(center_.x() - half_length_, center_.y() - half_width_);
73}
74
75bool AABox2d::IsPointIn(const Vec2d &point) const {
76 return std::abs(point.x() - center_.x()) <= half_length_ + kMathEpsilon &&
77 std::abs(point.y() - center_.y()) <= half_width_ + kMathEpsilon;
78}
79
80bool AABox2d::IsPointOnBoundary(const Vec2d &point) const {
81 const double dx = std::abs(point.x() - center_.x());
82 const double dy = std::abs(point.y() - center_.y());
83 return (std::abs(dx - half_length_) <= kMathEpsilon &&
84 dy <= half_width_ + kMathEpsilon) ||
85 (std::abs(dy - half_width_) <= kMathEpsilon &&
86 dx <= half_length_ + kMathEpsilon);
87}
88
89double AABox2d::DistanceTo(const Vec2d &point) const {
90 const double dx = std::abs(point.x() - center_.x()) - half_length_;
91 const double dy = std::abs(point.y() - center_.y()) - half_width_;
92 if (dx <= 0.0) {
93 return std::max(0.0, dy);
94 }
95 if (dy <= 0.0) {
96 return dx;
97 }
98 return hypot(dx, dy);
99}
100
101double AABox2d::DistanceTo(const AABox2d &box) const {
102 const double dx =
103 std::abs(box.center_x() - center_.x()) - box.half_length() - half_length_;
104 const double dy =
105 std::abs(box.center_y() - center_.y()) - box.half_width() - half_width_;
106 if (dx <= 0.0) {
107 return std::max(0.0, dy);
108 }
109 if (dy <= 0.0) {
110 return dx;
111 }
112 return hypot(dx, dy);
113}
114
115bool AABox2d::HasOverlap(const AABox2d &box) const {
116 return std::abs(box.center_x() - center_.x()) <=
117 box.half_length() + half_length_ &&
118 std::abs(box.center_y() - center_.y()) <=
119 box.half_width() + half_width_;
120}
121
122void AABox2d::Shift(const Vec2d &shift_vec) { center_ += shift_vec; }
123
124void AABox2d::MergeFrom(const AABox2d &other_box) {
125 const double x1 = std::min(min_x(), other_box.min_x());
126 const double x2 = std::max(max_x(), other_box.max_x());
127 const double y1 = std::min(min_y(), other_box.min_y());
128 const double y2 = std::max(max_y(), other_box.max_y());
129 center_ = Vec2d((x1 + x2) / 2.0, (y1 + y2) / 2.0);
130 length_ = x2 - x1;
131 width_ = y2 - y1;
132 half_length_ = length_ / 2.0;
133 half_width_ = width_ / 2.0;
134}
135
136void AABox2d::MergeFrom(const Vec2d &other_point) {
137 const double x1 = std::min(min_x(), other_point.x());
138 const double x2 = std::max(max_x(), other_point.x());
139 const double y1 = std::min(min_y(), other_point.y());
140 const double y2 = std::max(max_y(), other_point.y());
141 center_ = Vec2d((x1 + x2) / 2.0, (y1 + y2) / 2.0);
142 length_ = x2 - x1;
143 width_ = y2 - y1;
144 half_length_ = length_ / 2.0;
145 half_width_ = width_ / 2.0;
146}
147
148std::string AABox2d::DebugString() const {
149 return absl::StrCat("aabox2d ( center = ", center_.DebugString(),
150 " length = ", length_, " width = ", width_, " )");
151}
152
153} // namespace math
154} // namespace common
155} // namespace apollo
Defines the AABox2d class.
Implements a class of (undirected) axes-aligned bounding boxes in 2-D.
Definition aabox2d.h:42
double max_y() const
Returns the maximum y-coordinate of the box
Definition aabox2d.h:145
double min_x() const
Returns the minimum x-coordinate of the box
Definition aabox2d.h:124
double min_y() const
Returns the minimum y-coordinate of the box
Definition aabox2d.h:138
void MergeFrom(const AABox2d &other_box)
Changes box to include another given box, as well as the current one.
Definition aabox2d.cc:124
double center_y() const
Getter of y-component of center_
Definition aabox2d.h:87
double half_length() const
Getter of half_length_
Definition aabox2d.h:105
std::string DebugString() const
Gets a human-readable debug string
Definition aabox2d.cc:148
bool IsPointOnBoundary(const Vec2d &point) const
Determines whether a given point is on the boundary of the box.
Definition aabox2d.cc:80
bool HasOverlap(const AABox2d &box) const
Determines whether two boxes overlap.
Definition aabox2d.cc:115
double half_width() const
Getter of half_width_
Definition aabox2d.h:111
double center_x() const
Getter of x-component of center_
Definition aabox2d.h:81
void Shift(const Vec2d &shift_vec)
Shift the center of AABox by the input vector.
Definition aabox2d.cc:122
bool IsPointIn(const Vec2d &point) const
Determines whether a given point is in the box.
Definition aabox2d.cc:75
double DistanceTo(const Vec2d &point) const
Determines the distance between a point and the box.
Definition aabox2d.cc:89
AABox2d()=default
Default constructor.
double max_x() const
Returns the maximum x-coordinate of the box
Definition aabox2d.h:131
void GetAllCorners(std::vector< Vec2d > *const corners) const
Gets all corners in counter clockwise order.
Definition aabox2d.cc:66
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
std::string DebugString() const
Returns a human-readable string representing this object
Definition vec2d.cc:125
double y() const
Getter for y component
Definition vec2d.h:57
double x() const
Getter for x component
Definition vec2d.h:54
#define ACHECK(cond)
Definition log.h:80
Math-related util functions.
constexpr double kMathEpsilon
Definition vec2d.h:35
class register implement
Definition arena_queue.h:37
Definition future.h:29