Point Cloud Library (PCL)  1.14.0-dev
gpu_extract_labeled_clusters.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $Id:$
36  *
37  */
38 
39 #pragma once
40 
41 #include <pcl/gpu/segmentation/gpu_extract_labeled_clusters.h>
42 
43 template <typename PointT>
44 void
46  const typename pcl::PointCloud<PointT>::Ptr& host_cloud_,
47  const pcl::gpu::Octree::Ptr& tree,
48  float tolerance,
49  std::vector<PointIndices>& clusters,
50  unsigned int min_pts_per_cluster,
51  unsigned int max_pts_per_cluster)
52 {
53 
54  // Create a bool vector of processed point indices, and initialize it to false
55  // cloud is a DeviceArray<PointType>
56  std::vector<bool> processed(host_cloud_->size(), false);
57 
58  int max_answers;
59 
60  if (max_pts_per_cluster > host_cloud_->size())
61  max_answers = static_cast<int>(host_cloud_->size());
62  else
63  max_answers = max_pts_per_cluster;
64 
65  // to store the current cluster
67 
68  // Process all points in the cloud
69  for (std::size_t i = 0; i < host_cloud_->size(); ++i) {
70  // if we already processed this point continue with the next one
71  if (processed[i])
72  continue;
73  // now we will process this point
74  processed[i] = true;
75 
76  // Create the query queue on the device, point based not indices
77  pcl::gpu::Octree::Queries queries_device;
78  // Create the query queue on the host
80 
81  // Buffer in a new PointXYZ type
82  PointT t = (*host_cloud_)[i];
83  PointXYZ p;
84  p.x = t.x;
85  p.y = t.y;
86  p.z = t.z;
87 
88  // Push the starting point in the vector
89  queries_host.push_back(p);
90  // Clear vector
91  r.indices.clear();
92  // Push the starting point in
93  r.indices.push_back(static_cast<int>(i));
94 
95  unsigned int found_points = static_cast<unsigned int>(queries_host.size());
96  unsigned int previous_found_points = 0;
97 
98  pcl::gpu::NeighborIndices result_device;
99 
100  // once the area stop growing, stop also iterating.
101  while (previous_found_points < found_points) {
102  // Move queries to GPU
103  queries_device.upload(queries_host);
104  // Execute search
105  tree->radiusSearch(queries_device, tolerance, max_answers, result_device);
106 
107  // Store the previously found number of points
108  previous_found_points = found_points;
109 
110  // Host buffer for results
111  std::vector<int> sizes, data;
112 
113  // Copy results from GPU to Host
114  result_device.sizes.download(sizes);
115  result_device.data.download(data);
116 
117  for (std::size_t qp = 0; qp < sizes.size(); qp++) {
118  for (int qp_r = 0; qp_r < sizes[qp]; qp_r++) {
119  if (processed[data[qp_r + qp * max_answers]])
120  continue;
121  // Only add if label matches the original label
122  if ((*host_cloud_)[i].label ==
123  (*host_cloud_)[data[qp_r + qp * max_answers]].label) {
124  processed[data[qp_r + qp * max_answers]] = true;
125  PointT t_l = (*host_cloud_)[data[qp_r + qp * max_answers]];
126  PointXYZ p_l;
127  p_l.x = t_l.x;
128  p_l.y = t_l.y;
129  p_l.z = t_l.z;
130  queries_host.push_back(p_l);
131  found_points++;
132  r.indices.push_back(data[qp_r + qp * max_answers]);
133  }
134  }
135  }
136  }
137  // If this queue is satisfactory, add to the clusters
138  if (found_points >= min_pts_per_cluster && found_points <= max_pts_per_cluster) {
139  std::sort(r.indices.begin(), r.indices.end());
140  // @todo: check if the following is actually still needed
141  // r.indices.erase (std::unique (r.indices.begin (), r.indices.end ()),
142  // r.indices.end ());
143 
144  r.header = host_cloud_->header;
145  clusters.push_back(r); // We could avoid a copy by working directly in the vector
146  }
147  }
148 }
149 
150 template <typename PointT>
151 void
153  std::vector<PointIndices>& clusters)
154 {
155  // Initialize the GPU search tree
156  if (!tree_) {
157  tree_.reset(new pcl::gpu::Octree());
158  ///@todo what do we do if input isn't a PointXYZ cloud?
159  tree_->setCloud(input_);
160  }
161  if (!tree_->isBuilt()) {
162  tree_->build();
163  }
164  /*
165  if(tree_->cloud_.size() != host_cloud.size ())
166  {
167  PCL_ERROR("[pcl::gpu::EuclideanClusterExtraction] size of host cloud and device
168  cloud don't match!\n"); return;
169  }
170  */
171  // Extract the actual clusters
172  extractLabeledEuclideanClusters<PointT>(host_cloud_,
173  tree_,
174  cluster_tolerance_,
175  clusters,
176  min_pts_per_cluster_,
177  max_pts_per_cluster_);
178 
179  // Sort the clusters based on their size (largest one first)
180  std::sort(clusters.rbegin(), clusters.rend(), compareLabeledPointClusters);
181 }
182 
183 #define PCL_INSTANTIATE_extractLabeledEuclideanClusters(T) \
184  template void PCL_EXPORTS pcl::gpu::extractLabeledEuclideanClusters<T>( \
185  const typename pcl::PointCloud<T>::Ptr&, \
186  const pcl::gpu::Octree::Ptr&, \
187  float, \
188  std::vector<PointIndices>&, \
189  unsigned int, \
190  unsigned int);
191 #define PCL_INSTANTIATE_EuclideanLabeledClusterExtraction(T) \
192  template class PCL_EXPORTS pcl::gpu::EuclideanLabeledClusterExtraction<T>;
void push_back(const PointT &pt)
Insert a new point in the cloud, at the end of the container.
Definition: point_cloud.h:663
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:392
std::size_t size() const
Definition: point_cloud.h:443
std::vector< PointT, Eigen::aligned_allocator< PointT > > VectorType
Definition: point_cloud.h:411
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
void upload(const T *host_ptr, std::size_t size)
Uploads data to internal buffer in GPU memory.
void download(T *host_ptr) const
Downloads data from internal buffer to CPU memory.
void extract(std::vector< PointIndices > &clusters)
extract clusters of a PointCloud given by <setInputCloud(), setIndices()>
Octree implementation on GPU.
Definition: octree.hpp:59
shared_ptr< Octree > Ptr
Types.
Definition: octree.hpp:69
bool compareLabeledPointClusters(const pcl::PointIndices &a, const pcl::PointIndices &b)
Sort clusters method (for std::sort).
void extractLabeledEuclideanClusters(const typename pcl::PointCloud< PointT >::Ptr &host_cloud_, const pcl::gpu::Octree::Ptr &tree, float tolerance, std::vector< PointIndices > &clusters, unsigned int min_pts_per_cluster, unsigned int max_pts_per_cluster)
::pcl::PCLHeader header
Definition: PointIndices.h:18
A point structure representing Euclidean xyz coordinates.
A point structure representing Euclidean xyz coordinates, and the RGB color.
DeviceArray< int > sizes
DeviceArray< int > data