Apollo  6.0
Open source self driving car software
obj_mapper.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 <map>
20 #include <vector>
21 
22 #include "cyber/common/log.h"
25 
26 namespace apollo {
27 namespace perception {
28 namespace camera {
29 
31  float hwl[3] = {0};
32  float bbox[4] = {0};
33  float ry = 0.0f;
34  bool is_veh = true;
35  bool check_dimension = true;
37 };
38 
39 // hyper parameters
41  ObjMapperParams() { set_default(); }
42 
43  void set_default();
44 
45  int nr_bins_z;
49 
51  float factor_small;
52  float learning_r;
53  float reproj_err;
54  float rz_ratio;
56  float stable_w;
57  float occ_ratio;
58  float depth_min;
59  float dist_far;
60  float eps_mapper;
61  float iou_suc;
62  float iou_high;
64 };
65 
66 class ObjMapper {
67  public:
68  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
69 
70  public:
71  ObjMapper() : width_(0), height_(0) {
72  memset(k_mat_, 0, sizeof(float) * 9);
73  resize_ry_score(params_.nr_bins_ry);
74  set_default_variance();
75  }
76 
78 
80  orientation_variance_(0) = 1.0f;
81  orientation_variance_(1) = orientation_variance_(2) = 0.0f;
82  position_uncertainty_ << 1, 0, 0, 0, 0, 0, 0, 0, 1;
83  }
84 
85  void Init(const float *k_mat, int width, int height) {
86  memcpy(k_mat_, k_mat, sizeof(float) * 9);
87  width_ = width;
88  height_ = height;
89  object_template_manager_ = ObjectTemplateManager::Instance();
90  }
91 
92  void resize_ry_score(int size) { ry_score_.resize(size); }
93 
94  std::vector<float> get_ry_score() { return ry_score_; }
95 
96  std::vector<float> get_ry_score(float ry) {
97  FillRyScoreSingleBin(ry);
98  return ry_score_;
99  }
100 
101  Eigen::Vector3d get_orientation_var() { return orientation_variance_; }
102 
103  Eigen::Matrix3d get_position_uncertainty() { return position_uncertainty_; }
104 
105  // main interface, center is the bottom-face center ("center" for short)
106  bool Solve3dBbox(const ObjMapperOptions &options, float center[3],
107  float hwl[3], float *ry);
108 
109  private:
110  bool Solve3dBboxGivenOneFullBboxDimensionOrientation(const float *bbox,
111  const float *hwl,
112  float *ry,
113  float *center);
114 
115  bool SolveCenterFromNearestVerticalEdge(const float *bbox, const float *hwl,
116  float ry, float *center,
117  float *center_2d) const;
118 
119  void UpdateCenterViaBackProjectZ(const float *bbox, const float *hwl,
120  const float *center_2d,
121  float *center) const {
122  float z = center[2];
123  common::IBackprojectCanonical(center_2d, k_mat_, z, center);
124  center[1] += hwl[0] / 2;
125  }
126 
127  float GetProjectionScore(float ry, const float *bbox, const float *hwl,
128  const float *center, bool check_truncation = false,
129  float *bbox_res = nullptr) const {
130  float rot[9] = {0};
131  GenRotMatrix(ry, rot);
132  float *bbox_near = nullptr;
133  float score = GetScoreViaRotDimensionCenter(
134  &k_mat_[0], width_, height_, bbox, rot, hwl, center, check_truncation,
135  bbox_res, bbox_near);
136  return score;
137  }
138 
139  void PostRefineZ(const float *bbox, const float *hwl, const float *center2d,
140  float ry, float *center, float dz = 0.2f, float rz = 1.0f,
141  bool fix_proj = true) const {
142  float z = center[2];
143  float score_best = 0.0f;
144  float x[2] = {center2d[0], center2d[1]};
145  float center_best[3] = {center[0], center[1], center[2]};
146  for (float offset = -rz; offset <= rz; offset += dz) {
147  float z_test = std::max(z + offset, params_.depth_min);
148  float center_test[3] = {center[0], center[1], z_test};
149  if (fix_proj) {
150  common::IBackprojectCanonical(x, k_mat_, z_test, center_test);
151  center_test[1] += hwl[0] / 2;
152  }
153  float score_test = GetProjectionScore(ry, bbox, hwl, center_test);
154  if (score_best < score_test) {
155  score_best = score_test;
156  memcpy(center_best, center_test, sizeof(float) * 3);
157  }
158  }
159  memcpy(center, center_best, sizeof(float) * 3);
160  }
161 
162  void PostRefineOrientation(const float *bbox, const float *hwl,
163  const float *center, float *ry);
164 
165  void GetCenter(const float *bbox, const float &z_ref, const float &ry,
166  const float *hwl, float *center,
167  float *x /*init outside*/) const;
168 
169  void FillRyScoreSingleBin(const float &ry) {
170  int nr_bins_ry = static_cast<int>(ry_score_.size());
171  memset(ry_score_.data(), 0, sizeof(float) * nr_bins_ry);
172  const float PI = common::Constant<float>::PI();
173  int index = static_cast<int>(
174  std::floor((ry + PI) * static_cast<float>(nr_bins_ry) / (2.0f * PI)));
175  ry_score_[index % nr_bins_ry] = 1.0f;
176  }
177 
178  private:
179  // canonical camera: p_matrix = k_matrix [I 0]
180  float k_mat_[9] = {0};
181  int width_ = 0;
182  int height_ = 0;
183  std::vector<float> ry_score_ = {};
184  ObjMapperParams params_;
185 
186  // per-dimension variance of the orientation [yaw, pitch, roll]
187  Eigen::Vector3d orientation_variance_;
188 
189  // covariance matrix for position uncertainty
190  Eigen::Matrix3d position_uncertainty_;
191 
192  // DISALLOW_COPY_AND_ASSIGN(ObjMapper);
193 
194  protected:
195  ObjectTemplateManager *object_template_manager_ = nullptr;
196 };
197 
198 } // namespace camera
199 } // namespace perception
200 } // namespace apollo
float iou_suc
Definition: obj_mapper.h:61
Definition: obj_mapper.h:66
float occ_ratio
Definition: obj_mapper.h:57
float eps_mapper
Definition: obj_mapper.h:60
std::vector< float > get_ry_score()
Definition: obj_mapper.h:94
float dist_far
Definition: obj_mapper.h:59
float reproj_err
Definition: obj_mapper.h:53
float bbox[4]
Definition: obj_mapper.h:32
float angle_resolution_degree
Definition: obj_mapper.h:63
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Eigen::Vector3d get_orientation_var()
Definition: obj_mapper.h:101
int boundary_len
Definition: obj_mapper.h:47
float iou_high
Definition: obj_mapper.h:62
int type_min_vol_index
Definition: obj_mapper.h:36
float depth_min
Definition: obj_mapper.h:58
Eigen::Vector3d Vector3d
Definition: frame_transform.h:27
bool check_dimension
Definition: obj_mapper.h:35
const double PI
Definition: const_var.h:77
void set_default_variance()
Definition: obj_mapper.h:79
float factor_small
Definition: obj_mapper.h:51
float small_bbox_height
Definition: obj_mapper.h:50
Definition: object_template_manager.h:49
void GenRotMatrix(const T &ry, T *rot)
Definition: twod_threed_util.h:101
float stable_w
Definition: obj_mapper.h:56
float rz_ratio
Definition: obj_mapper.h:54
void resize_ry_score(int size)
Definition: obj_mapper.h:92
float learning_r
Definition: obj_mapper.h:52
int nr_bins_z
Definition: obj_mapper.h:45
~ObjMapper()
Definition: obj_mapper.h:77
int max_nr_iter
Definition: obj_mapper.h:48
ObjMapperParams()
Definition: obj_mapper.h:41
T GetScoreViaRotDimensionCenter(const T *k_mat, int width, int height, const T *bbox, const T *rot, const T *hwl, const T *location, const bool &check_truncation, T *bbox_res=nullptr, T *bbox_near=nullptr)
Definition: twod_threed_util.h:177
float abnormal_h_veh
Definition: obj_mapper.h:55
std::vector< float > get_ry_score(float ry)
Definition: obj_mapper.h:96
void Init(const float *k_mat, int width, int height)
Definition: obj_mapper.h:85
float hwl[3]
Definition: obj_mapper.h:31
int nr_bins_ry
Definition: obj_mapper.h:46
bool is_veh
Definition: obj_mapper.h:34
ObjMapper()
Definition: obj_mapper.h:71
void IBackprojectCanonical(const T *x, const T *K, T depth, T *X)
Definition: i_util.h:164
Eigen::Matrix3d Matrix3d
Definition: base_map_fwd.h:33
float ry
Definition: obj_mapper.h:33
Eigen::Matrix3d get_position_uncertainty()
Definition: obj_mapper.h:103