Point Cloud Library (PCL)  1.10.0-dev
conditional_euclidean_clustering.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/pcl_base.h>
41 #include <pcl/pcl_macros.h>
42 #include <pcl/search/pcl_search.h>
43 
44 #include <functional>
45 
46 namespace pcl
47 {
48  using IndicesClusters = std::vector<pcl::PointIndices>;
50 
51  /** \brief @b ConditionalEuclideanClustering performs segmentation based on Euclidean distance and a user-defined clustering condition.
52  * \details The condition that need to hold is currently passed using a function pointer.
53  * For more information check the documentation of setConditionFunction() or the usage example below:
54  * \code
55  * bool
56  * enforceIntensitySimilarity (const pcl::PointXYZI& point_a, const pcl::PointXYZI& point_b, float squared_distance)
57  * {
58  * if (std::abs (point_a.intensity - point_b.intensity) < 0.1f)
59  * return (true);
60  * else
61  * return (false);
62  * }
63  * // ...
64  * // Somewhere down to the main code
65  * // ...
66  * pcl::ConditionalEuclideanClustering<pcl::PointXYZI> cec (true);
67  * cec.setInputCloud (cloud_in);
68  * cec.setConditionFunction (&enforceIntensitySimilarity);
69  * // Points within this distance from one another are going to need to validate the enforceIntensitySimilarity function to be part of the same cluster:
70  * cec.setClusterTolerance (0.09f);
71  * // Size constraints for the clusters:
72  * cec.setMinClusterSize (5);
73  * cec.setMaxClusterSize (30);
74  * // The resulting clusters (an array of pointindices):
75  * cec.segment (*clusters);
76  * // The clusters that are too small or too large in size can also be extracted separately:
77  * cec.getRemovedClusters (small_clusters, large_clusters);
78  * \endcode
79  * \author Frits Florentinus
80  * \ingroup segmentation
81  */
82  template<typename PointT>
83  class ConditionalEuclideanClustering : public PCLBase<PointT>
84  {
85  protected:
87 
92 
93  public:
94  /** \brief Constructor.
95  * \param[in] extract_removed_clusters Set to true if you want to be able to extract the clusters that are too large or too small (default = false)
96  */
97  ConditionalEuclideanClustering (bool extract_removed_clusters = false) :
98  searcher_ (),
99  condition_function_ (),
100  cluster_tolerance_ (0.0f),
101  min_cluster_size_ (1),
102  max_cluster_size_ (std::numeric_limits<int>::max ()),
103  extract_removed_clusters_ (extract_removed_clusters),
104  small_clusters_ (new pcl::IndicesClusters),
105  large_clusters_ (new pcl::IndicesClusters)
106  {
107  }
108 
109  /** \brief Provide a pointer to the search object.
110  * \param[in] tree a pointer to the spatial search object.
111  */
112  inline void
114  {
115  searcher_ = tree;
116  }
117 
118  /** \brief Get a pointer to the search method used.
119  */
120  inline const SearcherPtr&
121  getSearchMethod () const
122  {
123  return searcher_;
124  }
125 
126  /** \brief Set the condition that needs to hold for neighboring points to be considered part of the same cluster.
127  * \details Any two points within a certain distance from one another will need to evaluate this condition in order to be made part of the same cluster.
128  * The distance can be set using setClusterTolerance().
129  * <br>
130  * Note that for a point to be part of a cluster, the condition only needs to hold for at least 1 point pair.
131  * To clarify, the following statement is false:
132  * Any two points within a cluster always evaluate this condition function to true.
133  * <br><br>
134  * The input arguments of the condition function are:
135  * <ul>
136  * <li>PointT The first point of the point pair</li>
137  * <li>PointT The second point of the point pair</li>
138  * <li>float The squared distance between the points</li>
139  * </ul>
140  * The output argument is a boolean, returning true will merge the second point into the cluster of the first point.
141  * \param[in] condition_function The condition function that needs to hold for clustering
142  */
143  inline void
144  setConditionFunction (bool (*condition_function) (const PointT&, const PointT&, float))
145  {
146  condition_function_ = condition_function;
147  }
148 
149  /** \brief Set the condition that needs to hold for neighboring points to be considered part of the same cluster.
150  * This is an overloaded function provided for convenience. See the documentation for setConditionFunction(). */
151  inline void
152  setConditionFunction (std::function<bool (const PointT&, const PointT&, float)> condition_function)
153  {
154  condition_function_ = condition_function;
155  }
156 
157  /** \brief Set the spatial tolerance for new cluster candidates.
158  * \details Any two points within this distance from one another will need to evaluate a certain condition in order to be made part of the same cluster.
159  * The condition can be set using setConditionFunction().
160  * \param[in] cluster_tolerance The distance to scan for cluster candidates (default = 0.0)
161  */
162  inline void
163  setClusterTolerance (float cluster_tolerance)
164  {
165  cluster_tolerance_ = cluster_tolerance;
166  }
167 
168  /** \brief Get the spatial tolerance for new cluster candidates.*/
169  inline float
171  {
172  return (cluster_tolerance_);
173  }
174 
175  /** \brief Set the minimum number of points that a cluster needs to contain in order to be considered valid.
176  * \param[in] min_cluster_size The minimum cluster size (default = 1)
177  */
178  inline void
179  setMinClusterSize (int min_cluster_size)
180  {
181  min_cluster_size_ = min_cluster_size;
182  }
183 
184  /** \brief Get the minimum number of points that a cluster needs to contain in order to be considered valid.*/
185  inline int
187  {
188  return (min_cluster_size_);
189  }
190 
191  /** \brief Set the maximum number of points that a cluster needs to contain in order to be considered valid.
192  * \param[in] max_cluster_size The maximum cluster size (default = unlimited)
193  */
194  inline void
195  setMaxClusterSize (int max_cluster_size)
196  {
197  max_cluster_size_ = max_cluster_size;
198  }
199 
200  /** \brief Get the maximum number of points that a cluster needs to contain in order to be considered valid.*/
201  inline int
203  {
204  return (max_cluster_size_);
205  }
206 
207  /** \brief Segment the input into separate clusters.
208  * \details The input can be set using setInputCloud() and setIndices().
209  * <br>
210  * The size constraints for the resulting clusters can be set using setMinClusterSize() and setMaxClusterSize().
211  * <br>
212  * The region growing parameters can be set using setConditionFunction() and setClusterTolerance().
213  * <br>
214  * \param[out] clusters The resultant set of indices, indexing the points of the input cloud that correspond to the clusters
215  */
216  void
217  segment (IndicesClusters &clusters);
218 
219  /** \brief Get the clusters that are invalidated due to size constraints.
220  * \note The constructor of this class needs to be initialized with true, and the segment method needs to have been called prior to using this method.
221  * \param[out] small_clusters The resultant clusters that contain less than min_cluster_size points
222  * \param[out] large_clusters The resultant clusters that contain more than max_cluster_size points
223  */
224  inline void
225  getRemovedClusters (IndicesClustersPtr &small_clusters, IndicesClustersPtr &large_clusters)
226  {
227  if (!extract_removed_clusters_)
228  {
229  PCL_WARN("[pcl::ConditionalEuclideanClustering::getRemovedClusters] You need to set extract_removed_clusters to true (in this class' constructor) if you want to use this functionality.\n");
230  return;
231  }
232  small_clusters = small_clusters_;
233  large_clusters = large_clusters_;
234  }
235 
236  private:
237  /** \brief A pointer to the spatial search object */
238  SearcherPtr searcher_;
239 
240  /** \brief The condition function that needs to hold for clustering */
241  std::function<bool (const PointT&, const PointT&, float)> condition_function_;
242 
243  /** \brief The distance to scan for cluster candidates (default = 0.0) */
244  float cluster_tolerance_;
245 
246  /** \brief The minimum cluster size (default = 1) */
247  int min_cluster_size_;
248 
249  /** \brief The maximum cluster size (default = unlimited) */
250  int max_cluster_size_;
251 
252  /** \brief Set to true if you want to be able to extract the clusters that are too large or too small (default = false) */
253  bool extract_removed_clusters_;
254 
255  /** \brief The resultant clusters that contain less than min_cluster_size points */
256  pcl::IndicesClustersPtr small_clusters_;
257 
258  /** \brief The resultant clusters that contain more than max_cluster_size points */
259  pcl::IndicesClustersPtr large_clusters_;
260 
261  public:
263  };
264 }
265 
266 #ifdef PCL_NO_PRECOMPILE
267 #include <pcl/segmentation/impl/conditional_euclidean_clustering.hpp>
268 #endif
void setSearchMethod(const SearcherPtr &tree)
Provide a pointer to the search object.
void setMinClusterSize(int min_cluster_size)
Set the minimum number of points that a cluster needs to contain in order to be considered valid...
void segment(IndicesClusters &clusters)
Segment the input into separate clusters.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
int getMaxClusterSize()
Get the maximum number of points that a cluster needs to contain in order to be considered valid...
void setMaxClusterSize(int max_cluster_size)
Set the maximum number of points that a cluster needs to contain in order to be considered valid...
std::vector< pcl::PointIndices > IndicesClusters
typename pcl::search::Search< PointT >::Ptr SearcherPtr
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: pcl_macros.h:371
void setClusterTolerance(float cluster_tolerance)
Set the spatial tolerance for new cluster candidates.
void getRemovedClusters(IndicesClustersPtr &small_clusters, IndicesClustersPtr &large_clusters)
Get the clusters that are invalidated due to size constraints.
void setConditionFunction(bool(*condition_function)(const PointT &, const PointT &, float))
Set the condition that needs to hold for neighboring points to be considered part of the same cluster...
ConditionalEuclideanClustering performs segmentation based on Euclidean distance and a user-defined c...
PCL base class.
Definition: pcl_base.h:69
shared_ptr< std::vector< pcl::PointIndices > > IndicesClustersPtr
int getMinClusterSize()
Get the minimum number of points that a cluster needs to contain in order to be considered valid...
float getClusterTolerance()
Get the spatial tolerance for new cluster candidates.
ConditionalEuclideanClustering(bool extract_removed_clusters=false)
Constructor.
void setConditionFunction(std::function< bool(const PointT &, const PointT &, float)> condition_function)
Set the condition that needs to hold for neighboring points to be considered part of the same cluster...
shared_ptr< pcl::search::Search< PointT > > Ptr
Definition: search.h:80
A point structure representing Euclidean xyz coordinates, and the RGB color.
const SearcherPtr & getSearchMethod() const
Get a pointer to the search method used.
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
Defines all the PCL and non-PCL macros used.