Apollo  6.0
Open source self driving car software
i_ground.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 <utility>
19 #include <vector>
20 
25 
26 namespace apollo {
27 namespace perception {
28 namespace common {
31  IZero4(params);
32  nr_support = 0;
33  is_detected = false;
34  }
35 
37  ICopy4(pi.params, this->params);
38  this->nr_support = pi.GetNrSupport();
39  this->is_detected = pi.GetStatus();
40  return (*this);
41  }
42 
43  float GetDegreeNormalToX() const {
44  float normal[3];
45  ICopy3(params, normal);
46  if (normal[0] < 0) {
47  INeg3(normal);
48  }
49  // normalize:
50  IScale3(normal, IRec(ISqrt(ISquaresum3(normal))));
51  return IRadiansToDegree(IAcos(normal[0]));
52  }
53 
54  float get_degree_normal_to_y() const {
55  float normal[3];
56  ICopy3(params, normal);
57  if (normal[1] < 0) {
58  INeg3(normal);
59  }
60  // normalize:
61  IScale3(normal, IRec(ISqrt(ISquaresum3(normal))));
62  return IRadiansToDegree(IAcos(normal[1]));
63  }
64 
65  float GetDegreeNormalToZ() const {
66  float normal[3];
67  ICopy3(params, normal);
68  if (normal[2] < 0) {
69  INeg3(normal);
70  }
71  // normalize:
72  IScale3(normal, IRec(ISqrt(ISquaresum3(normal))));
73  return IRadiansToDegree(IAcos(normal[2]));
74  }
75 
77  if (params[0] < 0) {
78  INeg4(params);
79  }
80  }
81 
83  if (params[1] < 0) {
84  INeg4(params);
85  }
86  }
87 
89  if (params[2] < 0) {
90  INeg4(params);
91  }
92  }
93 
94  void ForceInvalid() {
95  IZero4(params);
96  nr_support = 0;
97  }
98 
99  void ForceUnitNorm() {
100  float norm = IL2Norm3(params);
101  IScale4(params, IRec(norm));
102  }
103 
104  bool IsValid() const {
105  if (nr_support && (params[0] != 0 || params[1] != 0 || params[2] != 0)) {
106  return true;
107  }
108  return false;
109  }
110 
111  int GetNrSupport() const { return nr_support; }
112 
113  void SetNrSupport(int nr) { nr_support = nr; }
114 
115  bool GetStatus() const { return is_detected; }
116 
117  void SetStatus(bool flag) { is_detected = flag; }
118 
119  float params[4];
120 
121  private:
122  bool is_detected;
123  int nr_support;
124 };
125 
128  theta = phi = d = 0;
129  nr_support = 0;
130  is_detected = false;
131  }
132 
134  this->theta = pi.theta;
135  this->phi = pi.phi;
136  this->d = pi.d;
137  this->nr_support = pi.GetNrSupport();
138  this->is_detected = pi.GetStatus();
139  return (*this);
140  }
141 
142  bool IsValid() const { return (nr_support > 0); }
143 
144  void ForceInvalid() {
145  theta = phi = d = 0;
146  nr_support = 0;
147  }
148 
149  int GetNrSupport() const { return nr_support; }
150 
151  void SetNrSupport(int nr) { nr_support = nr; }
152 
153  bool GetStatus() const { return is_detected; }
154 
155  void SetStatus(bool flag) { is_detected = flag; }
156 
157  float theta;
158  float phi;
159  float d;
160 
161  private:
162  bool is_detected;
163  int nr_support;
164 };
165 
167  PlaneFitGroundDetectorParam() { SetDefault(); }
168  bool Validate() const;
169  void SetDefault();
170  unsigned int nr_points_max;
171  unsigned int nr_grids_fine;
172  unsigned int nr_grids_coarse;
173  unsigned int nr_z_comp_candis;
192 };
193 
196  void Reserve(unsigned int size) { indices.reserve(size); }
197  void Clear() { indices.clear(); }
198  inline void PushIndex(int i) { indices.push_back(i); }
199  int Prune(unsigned int min_nr_samples, unsigned int max_nr_samples);
200  unsigned int Size() const {
201  return static_cast<unsigned int>(indices.size());
202  }
203  int &operator[](unsigned int i) {
204  assert(i >= 0 && i < indices.size());
205  return indices.at(i);
206  }
207  int &operator[](int i) {
208  assert(i >= 0 && i < static_cast<int>(indices.size()));
209  return indices.at(i);
210  }
211  const int &operator[](unsigned int i) const {
212  assert(i >= 0 && i < indices.size());
213  return indices.at(i);
214  }
215  const int &operator[](int i) const {
216  assert(i >= 0 && i < static_cast<int>(indices.size()));
217  return indices.at(i);
218  }
219  std::vector<int> indices;
221 };
222 
224 
226 
228  public:
230  : param_(param) {}
231  virtual ~BaseGroundDetector() {}
232  virtual bool Detect(const float *point_cloud, float *height_above_ground,
233  unsigned int nr_points,
234  unsigned int nr_point_elements) = 0;
235 
236  protected:
238 };
239 
241  static const int dim_point_ = 3;
242 
243  public:
246  bool Init();
247  bool Detect(const float *point_cloud, float *height_above_ground,
248  unsigned int nr_points, unsigned int nr_point_elements);
249  const char *GetLabel() const;
250  const VoxelGridXY<float> *GetGrid() const;
251  const GroundPlaneLiDAR *GetGroundPlane(int r, int c) const;
252  unsigned int GetGridDimX() const;
253  unsigned int GetGridDimY() const;
254  float GetUnknownHeight();
255  PlaneFitPointCandIndices **GetCandis() const;
256 
257  protected:
258  void CleanUp();
259  void InitOrderTable(const VoxelGridXY<float> *vg, std::pair<int, int> *order);
260  int Fit();
261  int FitLine(unsigned int r);
262  int FitGrid(const float *point_cloud, PlaneFitPointCandIndices *candi,
263  GroundPlaneLiDAR *groundplane, unsigned int nr_points,
264  unsigned int nr_point_element, float dist_thre);
265  int FitInOrder();
266  int FilterCandidates(int r, int c, const float *point_cloud,
268  std::vector<std::pair<int, int>> *neighbors,
269  unsigned int nr_point_element);
270  int FitGridWithNeighbors(int r, int c, const float *point_cloud,
271  GroundPlaneLiDAR *groundplane,
272  unsigned int nr_points,
273  unsigned int nr_point_element, float dist_thre);
274  void GetNeighbors(int r, int c, int rows, int cols,
275  std::vector<std::pair<int, int>> *neighbors);
276  float CalculateAngleDist(const GroundPlaneLiDAR &plane,
277  const std::vector<std::pair<int, int>> &neighbors);
278  int Filter();
279  int FilterLine(unsigned int r);
280  int FilterGrid(const Voxel<float> &vg, const float *point_cloud,
281  PlaneFitPointCandIndices *candi, unsigned int nr_points,
282  unsigned int nr_point_element);
283  int Smooth();
284  int SmoothLine(unsigned int up, unsigned int r, unsigned int dn);
285  int CompleteGrid(const GroundPlaneSpherical &lt,
286  const GroundPlaneSpherical &rt,
287  const GroundPlaneSpherical &up,
289  int SmoothGrid(const GroundPlaneSpherical &g, const GroundPlaneSpherical &lt,
290  const GroundPlaneSpherical &rt, const GroundPlaneSpherical &up,
292  int CompareZ(const float *point_cloud, const std::vector<int> &point_indices,
293  const float *z_values, PlaneFitPointCandIndices *candi,
294  unsigned int nr_points, unsigned int nr_point_element,
295  unsigned int nr_compares);
296  void ComputeAdaptiveThreshold();
297  void ComputeSignedGroundHeight(const float *point_cloud,
298  float *height_above_ground,
299  unsigned int nr_points,
300  unsigned int nr_point_elements);
301  void ComputeSignedGroundHeightLine(const float *point_cloud,
302  const GroundPlaneLiDAR *up,
303  const GroundPlaneLiDAR *cn,
304  const GroundPlaneLiDAR *dn,
305  float *height_above_ground, unsigned int r,
306  unsigned int nr_points,
307  unsigned int nr_point_elements);
308 
309  protected:
315  std::pair<float, bool> **ground_z_;
316  float **pf_thresholds_;
317  unsigned int *map_fine_to_coarse_;
318  char *labels_;
320  float *pf_threeds_;
322  std::pair<int, int> *order_table_;
323 };
324 
325 } // namespace common
326 } // namespace perception
327 } // namespace apollo
void ForcePositiveNormalZ()
Definition: i_ground.h:88
float planefit_orien_threshold
Definition: i_ground.h:187
void IPlaneSpherToEucli(const GroundPlaneSpherical &src, GroundPlaneLiDAR *dst)
float get_degree_normal_to_y() const
Definition: i_ground.h:54
bool GetStatus() const
Definition: i_ground.h:153
const int & operator[](int i) const
Definition: i_ground.h:215
const int & operator[](unsigned int i) const
Definition: i_ground.h:211
T ISquaresum3(const T x[3])
Definition: i_blas.h:2536
void Reserve(unsigned int size)
Definition: i_ground.h:196
void IScale4(T x[4], T sf)
Definition: i_blas.h:1874
BaseGroundDetector(const PlaneFitGroundDetectorParam &param)
Definition: i_ground.h:229
PlaneFitPointCandIndices ** local_candis_
Definition: i_ground.h:314
T IL2Norm3(const T x[3])
Definition: i_blas.h:2812
void ForcePositiveNormalX()
Definition: i_ground.h:76
void ForcePositiveNormalY()
Definition: i_ground.h:82
void ForceInvalid()
Definition: i_ground.h:94
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
GroundPlaneLiDAR ** ground_planes_
Definition: i_ground.h:312
GroundPlaneSpherical & operator=(const GroundPlaneSpherical &pi)
Definition: i_ground.h:133
unsigned int nr_z_comp_candis
Definition: i_ground.h:173
int nr_ransac_iter_threshold
Definition: i_ground.h:190
void ICopy3(const T src[3], T dst[3])
Definition: i_blas.h:40
VoxelGridXY< float > * vg_fine_
Definition: i_ground.h:310
int & operator[](unsigned int i)
Definition: i_ground.h:203
float IRec(float a)
Definition: i_basic.h:69
float IRadiansToDegree(float r)
Definition: i_basic.h:260
float theta
Definition: i_ground.h:157
unsigned int nr_grids_fine
Definition: i_ground.h:171
bool GetStatus() const
Definition: i_ground.h:115
unsigned int nr_grids_coarse
Definition: i_ground.h:172
unsigned int nr_inliers_min_threshold
Definition: i_ground.h:176
unsigned int nr_z_comp_fail_threshold
Definition: i_ground.h:174
GroundPlaneSpherical ** ground_planes_sphe_
Definition: i_ground.h:313
float params[4]
Definition: i_ground.h:119
const PlaneFitGroundDetectorParam & param_
Definition: i_ground.h:237
void SetNrSupport(int nr)
Definition: i_ground.h:151
virtual ~BaseGroundDetector()
Definition: i_ground.h:231
void ForceUnitNorm()
Definition: i_ground.h:99
float GetDegreeNormalToX() const
Definition: i_ground.h:43
float planefit_filter_threshold
Definition: i_ground.h:186
int * sampled_indices_
Definition: i_ground.h:321
void IZero4(T a[4])
Definition: i_blas.h:338
void INeg4(T x[4])
Definition: i_blas.h:421
unsigned int nr_points_max
Definition: i_ground.h:170
float GetDegreeNormalToZ() const
Definition: i_ground.h:65
float IAcos(float alpha)
Definition: i_basic.h:239
const int I_DEFAULT_SEED
Definition: i_rand.h:24
std::vector< int > indices
Definition: i_ground.h:219
float * sampled_z_values_
Definition: i_ground.h:319
float planefit_dist_threshold_near
Definition: i_ground.h:184
Definition: i_struct_s.h:178
int GetNrSupport() const
Definition: i_ground.h:111
float ** pf_thresholds_
Definition: i_ground.h:316
float planefit_dist_threshold_far
Definition: i_ground.h:185
void PushIndex(int i)
Definition: i_ground.h:198
GroundPlaneSpherical()
Definition: i_ground.h:127
char * labels_
Definition: i_ground.h:318
VoxelGridXY< float > * vg_coarse_
Definition: i_ground.h:311
GroundPlaneLiDAR & operator=(const GroundPlaneLiDAR &pi)
Definition: i_ground.h:36
void SetStatus(bool flag)
Definition: i_ground.h:155
float termi_inlier_percen_threshold
Definition: i_ground.h:188
bool Init(const char *binary_name)
void INeg3(T x[3])
Definition: i_blas.h:415
void SetStatus(bool flag)
Definition: i_ground.h:117
unsigned int * map_fine_to_coarse_
Definition: i_ground.h:317
bool IsValid() const
Definition: i_ground.h:142
std::pair< int, int > * order_table_
Definition: i_ground.h:322
GroundPlaneLiDAR()
Definition: i_ground.h:30
float ISqrt(float a)
Definition: i_basic.h:76
float candidate_filter_threshold
Definition: i_ground.h:189
unsigned int nr_samples_max_threshold
Definition: i_ground.h:177
int GetNrSupport() const
Definition: i_ground.h:149
void ForceInvalid()
Definition: i_ground.h:144
std::pair< float, bool > ** ground_z_
Definition: i_ground.h:315
unsigned int nr_samples_min_threshold
Definition: i_ground.h:175
void IPlaneEucliToSpher(const GroundPlaneLiDAR &src, GroundPlaneSpherical *dst)
void IScale3(T x[3], T sf)
Definition: i_blas.h:1868
void SetNrSupport(int nr)
Definition: i_ground.h:113
float * pf_threeds_
Definition: i_ground.h:320
PlaneFitPointCandIndices()
Definition: i_ground.h:195
float sample_region_z_lower
Definition: i_ground.h:178
float sample_region_z_upper
Definition: i_ground.h:179
bool IsValid() const
Definition: i_ground.h:104
int & operator[](int i)
Definition: i_ground.h:207
void ICopy4(const T src[4], T dst[4])
Definition: i_blas.h:46
unsigned int Size() const
Definition: i_ground.h:200