Apollo  v5.5.0
Open source self driving car software
feature_descriptor.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 <vector>
20 
22 
23 namespace apollo {
24 namespace perception {
25 namespace lidar {
26 
28  public:
29  explicit FeatureDescriptor(base::PointFCloud* cloud) { cloud_ = cloud; }
30  FeatureDescriptor() : cloud_(nullptr) {}
31  ~FeatureDescriptor() = default;
32 
33  void SetCloud(base::PointFCloud* cloud) { cloud_ = cloud; }
34 
35  void ComputeHistogram(int bin_size, float* feature) {
36  GetMinMaxCenter();
37  int xstep = bin_size;
38  int ystep = bin_size;
39  int zstep = bin_size;
40  int stat_len = xstep + ystep + zstep;
41  std::vector<int> stat_feat(stat_len, 0);
42  float xsize =
43  (max_pt_.x - min_pt_.x) / static_cast<float>(xstep) + 0.000001f;
44  float ysize =
45  (max_pt_.y - min_pt_.y) / static_cast<float>(ystep) + 0.000001f;
46  float zsize =
47  (max_pt_.z - min_pt_.z) / static_cast<float>(zstep) + 0.000001f;
48 
49  int pt_num = static_cast<int>(cloud_->size());
50  for (int i = 0; i < pt_num; ++i) {
51  base::PointF& pt = cloud_->at(i);
52  ++stat_feat[static_cast<int>((pt.x - min_pt_.x) / xsize)];
53  ++stat_feat[xstep + static_cast<int>((pt.y - min_pt_.y) / ysize)];
54  ++stat_feat[xstep + ystep + static_cast<int>((pt.z - min_pt_.z) / zsize)];
55  }
56 
57  feature[0] = center_pt_.x / 10.0f;
58  feature[1] = center_pt_.y / 10.0f;
59  feature[2] = center_pt_.z;
60  feature[3] = xsize;
61  feature[4] = ysize;
62  feature[5] = zsize;
63  feature[6] = static_cast<float>(pt_num);
64  for (size_t i = 0; i < stat_feat.size(); ++i) {
65  feature[i + 7] =
66  static_cast<float>(stat_feat[i]) / static_cast<float>(pt_num);
67  }
68  }
69 
70  private:
71  void GetMinMaxCenter() {
72  float xsum = 0.0f;
73  float ysum = 0.0f;
74  float zsum = 0.0f;
75  min_pt_.x = min_pt_.y = min_pt_.z = FLT_MAX;
76  max_pt_.x = max_pt_.y = max_pt_.z = -FLT_MAX;
77 
78  float pt_num = static_cast<float>(cloud_->size());
79  for (int i = 0; i < static_cast<int>(pt_num); ++i) {
80  base::PointF& pt = cloud_->at(i);
81  xsum += pt.x;
82  ysum += pt.y;
83  zsum += pt.z;
84  min_pt_.x = std::min(min_pt_.x, pt.x);
85  max_pt_.x = std::max(max_pt_.x, pt.x);
86  min_pt_.y = std::min(min_pt_.y, pt.y);
87  max_pt_.y = std::max(max_pt_.y, pt.y);
88  min_pt_.z = std::min(min_pt_.z, pt.z);
89  max_pt_.z = std::max(max_pt_.z, pt.z);
90  }
91 
92  // center position
93  center_pt_.x = xsum / pt_num;
94  center_pt_.y = ysum / pt_num;
95  center_pt_.z = zsum / pt_num;
96  }
97 
98  base::PointFCloud* cloud_;
99  base::PointF min_pt_;
100  base::PointF max_pt_;
101  base::PointF center_pt_;
102 }; // class FeatureDescriptor
103 
104 } // namespace lidar
105 } // namespace perception
106 } // namespace apollo
Definition: blob.h:72
Definition: point.h:29
T x
Definition: point.h:30
size_t size() const
Definition: point_cloud.h:76
T z
Definition: point.h:32
void ComputeHistogram(int bin_size, float *feature)
Definition: feature_descriptor.h:35
FeatureDescriptor(base::PointFCloud *cloud)
Definition: feature_descriptor.h:29
void SetCloud(base::PointFCloud *cloud)
Definition: feature_descriptor.h:33
Definition: feature_descriptor.h:27
FeatureDescriptor()
Definition: feature_descriptor.h:30
T y
Definition: point.h:31
const PointT * at(size_t col, size_t row) const
Definition: point_cloud.h:57