Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
conditional_euclidean_clustering.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 the copyright holder(s) 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 */
36
37#ifndef PCL_SEGMENTATION_IMPL_CONDITIONAL_EUCLIDEAN_CLUSTERING_HPP_
38#define PCL_SEGMENTATION_IMPL_CONDITIONAL_EUCLIDEAN_CLUSTERING_HPP_
39
40#include <pcl/segmentation/conditional_euclidean_clustering.h>
41#include <pcl/search/auto.h>
42
43template<typename PointT> void
45{
46 // Prepare output (going to use push_back)
47 clusters.clear ();
48 if (extract_removed_clusters_)
49 {
50 small_clusters_->clear ();
51 large_clusters_->clear ();
52 }
53
54 // Validity checks
55 if (!initCompute () || input_->points.empty () || indices_->empty () || !condition_function_)
56 return;
57
58 // Initialize the search class
59 if (!searcher_)
60 {
61 searcher_.reset (pcl::search::autoSelectMethod<PointT>(input_, indices_, false, pcl::search::Purpose::radius_search)); // not requiring sorted results is much faster
62 }
63 else
64 {
65 searcher_->setInputCloud (input_, indices_);
66 }
67 // If searcher_ gives sorted results, we can skip the first one because it is the query point itself
68 const int nn_start_idx = searcher_->getSortedResults () ? 1 : 0;
69
70 // Temp variables used by search class
71 Indices nn_indices;
72 std::vector<float> nn_distances;
73
74 // Create a bool vector of processed point indices, and initialize it to false
75 // Need to have it contain all possible points because radius search can not return indices into indices
76 std::vector<bool> processed (input_->size (), false);
77
78 // Process all points indexed by indices_
79 for (const auto& iindex : (*indices_)) // iindex = input index
80 {
81 // Has this point been processed before?
82 if (iindex == UNAVAILABLE || processed[iindex])
83 continue;
84
85 // Set up a new growing cluster
86 Indices current_cluster;
87 int cii = 0; // cii = cluster indices iterator
88
89 // Add the point to the cluster
90 current_cluster.push_back (iindex);
91 processed[iindex] = true;
92
93 // Process the current cluster (it can be growing in size as it is being processed)
94 while (cii < static_cast<int> (current_cluster.size ()))
95 {
96 // Search for neighbors around the current seed point of the current cluster
97 if (searcher_->radiusSearch ((*input_)[current_cluster[cii]], cluster_tolerance_, nn_indices, nn_distances) < 1)
98 {
99 cii++;
100 continue;
101 }
102
103 // Process the neighbors
104 for (int nii = nn_start_idx; nii < static_cast<int> (nn_indices.size ()); ++nii) // nii = neighbor indices iterator
105 {
106 // Has this point been processed before?
107 if (nn_indices[nii] == UNAVAILABLE || processed[nn_indices[nii]])
108 continue;
109
110 // Validate if condition holds
111 if (condition_function_ ((*input_)[current_cluster[cii]], (*input_)[nn_indices[nii]], nn_distances[nii]))
112 {
113 // Add the point to the cluster
114 current_cluster.push_back (nn_indices[nii]);
115 processed[nn_indices[nii]] = true;
116 }
117 }
118 cii++;
119 }
120
121 // If extracting removed clusters, all clusters need to be saved, otherwise only the ones within the given cluster size range
122 if (extract_removed_clusters_ ||
123 (static_cast<int> (current_cluster.size ()) >= min_cluster_size_ &&
124 static_cast<int> (current_cluster.size ()) <= max_cluster_size_))
125 {
127 pi.header = input_->header;
128 pi.indices.resize (current_cluster.size ());
129 for (int ii = 0; ii < static_cast<int> (current_cluster.size ()); ++ii) // ii = indices iterator
130 pi.indices[ii] = current_cluster[ii];
131
132 if (extract_removed_clusters_ && static_cast<int> (current_cluster.size ()) < min_cluster_size_)
133 small_clusters_->push_back (pi);
134 else if (extract_removed_clusters_ && static_cast<int> (current_cluster.size ()) > max_cluster_size_)
135 large_clusters_->push_back (pi);
136 else
137 clusters.push_back (pi);
138 }
139 }
140
141 deinitCompute ();
142}
143
144#define PCL_INSTANTIATE_ConditionalEuclideanClustering(T) template class PCL_EXPORTS pcl::ConditionalEuclideanClustering<T>;
145
146#endif // PCL_SEGMENTATION_IMPL_CONDITIONAL_EUCLIDEAN_CLUSTERING_HPP_
147
void segment(IndicesClusters &clusters)
Segment the input into separate clusters.
@ radius_search
The search method will mainly be used for radiusSearch.
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
std::vector< pcl::PointIndices > IndicesClusters
::pcl::PCLHeader header