Apollo 10.0
自动驾驶开放平台
box.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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#pragma once
17
18#include <algorithm>
19#include <sstream>
20#include <string>
21
24
25namespace apollo {
26namespace perception {
27namespace base {
28
29template <typename T>
30struct BBox2D;
31
32template <typename T>
33struct Rect {
34 Rect() : x(0), y(0), width(0), height(0) {}
35
36 Rect(const T &x_in, const T &y_in, const T &width_in, const T &height_in)
37 : x(x_in), y(y_in), width(width_in), height(height_in) {}
38
39 explicit Rect(const BBox2D<T> &bbox) {
40 this->x = bbox.xmin;
41 this->y = bbox.ymin;
42 this->width = bbox.xmax - bbox.xmin;
43 this->height = bbox.ymax - bbox.ymin;
44 }
45
46 Rect<T> &operator=(const BBox2D<T> &bbox) {
47 this->x = bbox.xmin;
48 this->y = bbox.ymin;
49 this->width = bbox.xmax - bbox.xmin;
50 this->height = bbox.ymax - bbox.ymin;
51 return *this;
52 }
53
55 Point2D<T> p;
56 p.x = this->x + this->width / 2;
57 p.y = this->y + this->height / 2;
58 return p;
59 }
60
62 this->x = p.x - this->width / 2;
63 this->y = p.y - this->height / 2;
64 }
65
66 T Area() const { return this->width * this->height; }
67
68 std::string ToStr() const {
69 std::stringstream ss;
70 ss << "[ " << width << " x " << height << " ] from ( " << x << " , " << y
71 << " )";
72 return ss.str();
73 }
74
75 friend Rect<T> operator&(const Rect<T> &rect1, const Rect<T> &rect2) {
76 T r1_xmin = rect1.x;
77 T r1_xmax = rect1.x + rect1.width;
78 T r1_ymin = rect1.y;
79 T r1_ymax = rect1.y + rect1.height;
80 T r2_xmin = rect2.x;
81 T r2_xmax = rect2.x + rect2.width;
82 T r2_ymin = rect2.y;
83 T r2_ymax = rect2.y + rect2.height;
84 if (r2_xmin <= r1_xmax && r2_xmax >= r1_xmin && r2_ymin <= r1_ymax &&
85 r2_ymax >= r1_ymin) {
86 T xmin = std::max(r1_xmin, r2_xmin);
87 T ymin = std::max(r1_ymin, r2_ymin);
88 T xmax = std::min(r1_xmax, r2_xmax);
89 T ymax = std::min(r1_ymax, r2_ymax);
90 return Rect<T>(xmin, ymin, xmax - xmin, ymax - ymin);
91 } else {
92 return Rect<T>(0, 0, 0, 0);
93 }
94 }
95
96 friend Rect<T> operator|(const Rect<T> &rect1, const Rect<T> &rect2) {
97 Rect<T> ret;
98 ret.x = std::min(rect1.x, rect2.x);
99 ret.y = std::min(rect1.y, rect2.y);
100 ret.width = std::max(rect1.x + rect1.width, rect2.x + rect2.width) - ret.x;
101 ret.height =
102 std::max(rect1.y + rect1.height, rect2.y + rect2.height) - ret.y;
103
104 return ret;
105 }
106
107 friend inline bool operator==(const Rect &rect1, const Rect &rect2) {
108 return (Equal(rect1.x, rect2.x) && Equal(rect1.y, rect2.y) &&
109 Equal(rect1.width, rect2.width) &&
110 Equal(rect1.height, rect2.height));
111 }
112
113 friend inline bool operator!=(const Rect &rect1, const Rect &rect2) {
114 return !(rect1 == rect2);
115 }
116
117 T x = 0; // top-left
118 T y = 0; // top-left
119 T width = 0;
120 T height = 0;
121};
122
123template <typename T>
124struct BBox2D {
125 BBox2D() : xmin(0), ymin(0), xmax(0), ymax(0) {}
126
127 BBox2D(const T &xmin_in, const T &ymin_in, const T &xmax_in, const T &ymax_in)
128 : xmin(xmin_in), ymin(ymin_in), xmax(xmax_in), ymax(ymax_in) {}
129
130 explicit BBox2D(const Rect<T> &rect) {
131 this->xmin = rect.x;
132 this->ymin = rect.y;
133 this->xmax = rect.x + rect.width;
134 this->ymax = rect.y + rect.height;
135 }
136
138 this->xmin = rect.x;
139 this->ymin = rect.y;
140 this->xmax = rect.x + rect.width;
141 this->ymax = rect.y + rect.height;
142 return *this;
143 }
144
146 Point2D<T> p;
147 p.x = this->xmin + (this->xmax - this->xmin) / 2;
148 p.y = this->ymin + (this->ymax - this->ymin) / 2;
149 return p;
150 }
151
152 T Area() const { return (xmax - xmin) * (ymax - ymin); }
153 T xmin = 0; // top-left
154 T ymin = 0; // top-left
155 T xmax = 0; // bottom-right
156 T ymax = 0; // bottom-right
157};
158
162
166
167} // namespace base
168} // namespace perception
169} // namespace apollo
Rect< int > RectI
Definition box.h:159
std::enable_if< std::is_integral< T >::value, bool >::type Equal(const T &lhs, const T &rhs)
Rect< double > RectD
Definition box.h:161
Rect< float > RectF
Definition box.h:160
BBox2D< float > BBox2DF
Definition box.h:164
BBox2D< int > BBox2DI
Definition box.h:163
BBox2D< double > BBox2DD
Definition box.h:165
class register implement
Definition arena_queue.h:37
BBox2D(const Rect< T > &rect)
Definition box.h:130
BBox2D< T > & operator=(const Rect< T > &rect)
Definition box.h:137
Point2D< T > Center() const
Definition box.h:145
BBox2D(const T &xmin_in, const T &ymin_in, const T &xmax_in, const T &ymax_in)
Definition box.h:127
Rect(const T &x_in, const T &y_in, const T &width_in, const T &height_in)
Definition box.h:36
friend bool operator==(const Rect &rect1, const Rect &rect2)
Definition box.h:107
void SetCenter(Point2D< T > p)
Definition box.h:61
Rect(const BBox2D< T > &bbox)
Definition box.h:39
Point2D< T > Center() const
Definition box.h:54
Rect< T > & operator=(const BBox2D< T > &bbox)
Definition box.h:46
std::string ToStr() const
Definition box.h:68
friend Rect< T > operator&(const Rect< T > &rect1, const Rect< T > &rect2)
Definition box.h:75
friend Rect< T > operator|(const Rect< T > &rect1, const Rect< T > &rect2)
Definition box.h:96
friend bool operator!=(const Rect &rect1, const Rect &rect2)
Definition box.h:113