Apollo  6.0
Open source self driving car software
conditional_clustering.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 
17 #pragma once
18 
19 #include <limits>
20 #include <memory>
21 #include <vector>
22 
25 
26 namespace apollo {
27 namespace perception {
28 namespace common {
29 
30 // @brief: define of the conditional clustering class
31 template <typename PointT>
33  public:
34  using IndicesClusters = std::vector<base::PointIndices>;
35 
36  ConditionClustering() = default;
37  explicit ConditionClustering(bool extract_removed_clusters = false)
38  : min_cluster_size_(1),
39  max_cluster_size_(std::numeric_limits<int>::max()),
40  extract_removed_clusters_(extract_removed_clusters),
41  small_clusters_(new IndicesClusters),
42  large_clusters_(new IndicesClusters) {}
43 
44  virtual ~ConditionClustering() = default;
45 
46  inline void set_cloud(
47  typename std::shared_ptr<base::PointCloud<PointT>> cloud) {
48  cloud_ = cloud;
49  }
50 
51  inline void set_candidate_param(void* param) { candidate_param_ = param; }
52 
54  int (*candidate_function)(const PointT&, void*, std::vector<int>*)) {
55  candidate_function_ = candidate_function;
56  }
57 
58  inline void set_condition_param(void* param) { condition_param_ = param; }
59 
60  inline void set_condition_function(bool (*condition_function)(const PointT&,
61  const PointT&,
62  void* param)) {
63  condition_function_ = condition_function;
64  }
65 
66  inline void set_min_cluster_size(size_t size) { min_cluster_size_ = size; }
67 
68  inline void set_max_cluster_size(size_t size) { max_cluster_size_ = size; }
69 
70  // main interface of ConditionClustering class to segment.
71  void Segment(IndicesClusters* xy_clusters);
72 
73  private:
74  typename std::shared_ptr<base::PointCloud<PointT>> cloud_;
75  size_t min_cluster_size_ = 0;
76  size_t max_cluster_size_ = 0;
77  bool extract_removed_clusters_ = true;
78  std::shared_ptr<IndicesClusters> small_clusters_;
79  std::shared_ptr<IndicesClusters> large_clusters_;
80  bool (*condition_function_)(const PointT&, const PointT&,
81  void* param) = nullptr;
82  int (*candidate_function_)(const PointT&, void*,
83  std::vector<int>* nn_indices_ptr) = nullptr;
84  void* candidate_param_ = nullptr;
85  void* condition_param_ = nullptr;
86 }; // class ConditionClustering
87 
88 template <typename PointT>
90  std::vector<int> nn_indices;
91  nn_indices.reserve(200);
92  std::vector<bool> processed(cloud_->size(), false);
93  // Process all points indexed by indices_
94  for (std::size_t iii = 0; iii < cloud_->size(); ++iii) {
95  if (processed[iii]) {
96  continue;
97  }
98  // Set up a new growing cluster
99  std::vector<int> current_cluster;
100  current_cluster.reserve(200);
101  std::size_t cii = 0;
102  current_cluster.push_back(static_cast<int>(iii));
103  processed[iii] = true;
104  // Process the current cluster
105  // (it can be growing in size as it is being processed)
106  while (cii < current_cluster.size()) {
107  nn_indices.clear();
108  if (candidate_function_(cloud_->at(current_cluster[cii]),
109  candidate_param_, &nn_indices) < 1) {
110  cii++;
111  continue;
112  }
113  for (std::size_t nii = 1; nii < nn_indices.size(); ++nii) {
114  if (nn_indices[nii] < 0 ||
115  nn_indices[nii] >= static_cast<int>(processed.size()) ||
116  processed[nn_indices[nii]]) {
117  continue;
118  }
119  if (condition_function_(cloud_->at(current_cluster[cii]),
120  cloud_->at(nn_indices[nii]),
121  condition_param_)) {
122  current_cluster.push_back(nn_indices[nii]);
123  processed[nn_indices[nii]] = true;
124  }
125  }
126  cii++;
127  }
128 
129  if (extract_removed_clusters_ ||
130  (current_cluster.size() >= min_cluster_size_ &&
131  current_cluster.size() <= max_cluster_size_)) {
133  pi.indices.resize(current_cluster.size());
134  for (int ii = 0; ii < static_cast<int>(current_cluster.size()); ++ii) {
135  pi.indices[ii] = current_cluster[ii];
136  }
137 
138  if (extract_removed_clusters_ &&
139  current_cluster.size() < min_cluster_size_) {
140  small_clusters_->push_back(pi);
141  } else if (extract_removed_clusters_ &&
142  current_cluster.size() > max_cluster_size_) {
143  large_clusters_->push_back(pi);
144  } else {
145  xy_clusters->push_back(pi);
146  }
147  }
148  }
149 }
150 
151 } // namespace common
152 } // namespace perception
153 } // namespace apollo
void set_cloud(typename std::shared_ptr< base::PointCloud< PointT >> cloud)
Definition: conditional_clustering.h:46
void set_condition_param(void *param)
Definition: conditional_clustering.h:58
std::vector< int > indices
Definition: point.h:75
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: future.h:29
std::vector< base::PointIndices > IndicesClusters
Definition: conditional_clustering.h:34
Definition: conditional_clustering.h:32
Definition: point_cloud.h:37
void set_candidate_function(int(*candidate_function)(const PointT &, void *, std::vector< int > *))
Definition: conditional_clustering.h:53
void set_candidate_param(void *param)
Definition: conditional_clustering.h:51
void set_condition_function(bool(*condition_function)(const PointT &, const PointT &, void *param))
Definition: conditional_clustering.h:60
ConditionClustering(bool extract_removed_clusters=false)
Definition: conditional_clustering.h:37
void set_max_cluster_size(size_t size)
Definition: conditional_clustering.h:68
void Segment(IndicesClusters *xy_clusters)
Definition: conditional_clustering.h:89
void set_min_cluster_size(size_t size)
Definition: conditional_clustering.h:66