Apollo  6.0
Open source self driving car software
util.h
Go to the documentation of this file.
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 <fstream>
20 #include <memory>
21 #include <numeric>
22 #include <string>
23 #include <vector>
24 
25 #include "cyber/common/log.h"
30 
31 namespace apollo {
32 namespace perception {
33 namespace camera {
34 
35 bool Equal(float x, float target, float eps = 1e-6f);
36 bool Equal(double x, double target, double eps = 1e-6);
37 
38 // @brief whether rect1 is covered by rect2
39 template <typename T>
40 bool IsCovered(const base::Rect<T> &rect1, const base::Rect<T> &rect2,
41  float thresh) {
42  base::RectF inter = rect1 & rect2;
43  return inter.Area() / rect1.Area() > thresh;
44 }
45 template <typename T>
46 bool IsCoveredHorizon(const base::Rect<T> &rect1, const base::Rect<T> &rect2,
47  float thresh) {
48  base::RectF inter = rect1 & rect2;
49  if (inter.Area() > 0) {
50  return inter.width / rect1.width > thresh;
51  }
52  return false;
53 }
54 template <typename T>
55 bool IsCoveredVertical(const base::Rect<T> &rect1, const base::Rect<T> &rect2,
56  float thresh) {
57  base::RectF inter = rect1 & rect2;
58  if (inter.Area() > 0) {
59  return inter.height / rect1.height > thresh;
60  }
61  return false;
62 }
63 
64 template <typename T>
65 bool Contain(const std::vector<T> &array, const T &element) {
66  for (const auto &item : array) {
67  if (item == element) {
68  return true;
69  }
70  }
71  return false;
72 }
73 
74 template <typename T>
75 bool OutOfValidRegion(const base::BBox2D<T> box, const T width, const T height,
76  const T border_size = 0) {
77  if (box.xmin < border_size || box.ymin < border_size) {
78  return true;
79  }
80  if (box.xmax + border_size > width || box.ymax + border_size > height) {
81  return true;
82  }
83  return false;
84 }
85 template <typename T>
86 bool OutOfValidRegion(const base::Rect<T> rect, const T width, const T height,
87  const T border_size = 0) {
88  base::BBox2D<T> box(rect);
89  return OutOfValidRegion(box, width, height, border_size);
90 }
91 
92 template <typename T>
93 void RefineBox(const base::Rect<T> &box_in, const T width, const T height,
94  base::Rect<T> *box_out) {
95  if (!box_out) {
96  return;
97  }
98  *box_out = box_in;
99  if (box_out->x < 0) {
100  box_out->width += box_out->x;
101  box_out->x = 0;
102  }
103  if (box_out->y < 0) {
104  box_out->height += box_out->y;
105  box_out->y = 0;
106  }
107  if (box_out->x >= width) {
108  box_out->x = 0;
109  box_out->width = 0;
110  }
111  if (box_out->y >= height) {
112  box_out->y = 0;
113  box_out->height = 0;
114  }
115  box_out->width = (box_out->x + box_out->width <= width) ? box_out->width
116  : width - box_out->x;
117  box_out->height = (box_out->y + box_out->height <= height)
118  ? box_out->height
119  : height - box_out->y;
120  if (box_out->width < 0) {
121  box_out->width = 0;
122  }
123  if (box_out->height < 0) {
124  box_out->height = 0;
125  }
126 }
127 
128 template <typename T>
129 void RefineBox(const base::BBox2D<T> &box_in, const T width, const T height,
130  base::BBox2D<T> *box_out) {
131  if (!box_out) {
132  return;
133  }
134  base::Rect<T> rect;
135  RefineBox(base::Rect<T>(box_in), width, height, &rect);
136  *box_out = base::BBox2D<T>(rect);
137 }
138 
139 bool LoadAnchors(const std::string &path, std::vector<float> *anchors);
140 bool LoadTypes(const std::string &path,
141  std::vector<base::ObjectSubType> *types);
142 bool LoadExpand(const std::string &path, std::vector<float> *expands);
143 
144 bool ResizeCPU(const base::Blob<uint8_t> &src_gpu,
145  std::shared_ptr<base::Blob<float>> dst, int stepwidth,
146  int start_axis);
147 
148 std::string GetCyberWorkRoot();
149 void FillObjectPolygonFromBBox3D(base::Object *object_ptr);
150 
151 template <typename T>
152 void CalculateMeanAndVariance(const std::vector<T> &data, T *mean,
153  T *variance) {
154  if (!mean || !variance) {
155  return;
156  }
157  if (data.empty()) {
158  *mean = 0;
159  *variance = 0;
160  return;
161  }
162  T sum = std::accumulate(data.begin(), data.end(), static_cast<T>(0));
163  *mean = sum / data.size();
164 
165  std::vector<T> diff(data.size());
166  std::transform(data.begin(), data.end(), diff.begin(),
167  [mean](T x) { return x - *mean; });
168  T sum_of_diff_sqrs = std::inner_product(diff.begin(), diff.end(),
169  diff.begin(), static_cast<T>(0));
170  *variance = sum_of_diff_sqrs / data.size();
171 }
172 
173 } // namespace camera
174 } // namespace perception
175 } // namespace apollo
bool IsCoveredHorizon(const base::Rect< T > &rect1, const base::Rect< T > &rect2, float thresh)
Definition: util.h:46
T ymin
Definition: box.h:154
void FillObjectPolygonFromBBox3D(base::Object *object_ptr)
T width
Definition: box.h:119
bool LoadExpand(const std::string &path, std::vector< float > *expands)
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool LoadAnchors(const std::string &path, std::vector< float > *anchors)
void CalculateMeanAndVariance(const std::vector< T > &data, T *mean, T *variance)
Definition: util.h:152
T x
Definition: box.h:117
void RefineBox(const base::Rect< T > &box_in, const T width, const T height, base::Rect< T > *box_out)
Definition: util.h:93
T y
Definition: box.h:118
bool ResizeCPU(const base::Blob< uint8_t > &src_gpu, std::shared_ptr< base::Blob< float >> dst, int stepwidth, int start_axis)
Definition: object.h:34
bool OutOfValidRegion(const base::BBox2D< T > box, const T width, const T height, const T border_size=0)
Definition: util.h:75
bool LoadTypes(const std::string &path, std::vector< base::ObjectSubType > *types)
bool Equal(float x, float target, float eps=1e-6f)
bool IsCoveredVertical(const base::Rect< T > &rect1, const base::Rect< T > &rect2, float thresh)
Definition: util.h:55
T ymax
Definition: box.h:156
T height
Definition: box.h:120
T xmin
Definition: box.h:153
T xmax
Definition: box.h:155
std::string GetCyberWorkRoot()
Definition: box.h:33
bool IsCovered(const base::Rect< T > &rect1, const base::Rect< T > &rect2, float thresh)
Definition: util.h:40
float diff(Image< float > *I, int x1, int y1, int x2, int y2)
Definition: segment_image.h:44
T Area() const
Definition: box.h:66
bool Contain(const std::vector< T > &array, const T &element)
Definition: util.h:65