Point Cloud Library (PCL)  1.8.1
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 #ifndef PCL_SEGMENTATION_CONDITIONAL_EUCLIDEAN_CLUSTERING_H_
39 #define PCL_SEGMENTATION_CONDITIONAL_EUCLIDEAN_CLUSTERING_H_
40 
41 #include <boost/function.hpp>
42 
43 #include <pcl/pcl_base.h>
44 #include <pcl/search/pcl_search.h>
45 
46 namespace pcl
47 {
48  typedef std::vector<pcl::PointIndices> IndicesClusters;
49  typedef boost::shared_ptr<std::vector<pcl::PointIndices> > IndicesClustersPtr;
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 (fabs (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 Set the condition that needs to hold for neighboring points to be considered part of the same cluster.
110  * \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.
111  * The distance can be set using setClusterTolerance().
112  * <br>
113  * Note that for a point to be part of a cluster, the condition only needs to hold for at least 1 point pair.
114  * To clarify, the following statement is false:
115  * Any two points within a cluster always evaluate this condition function to true.
116  * <br><br>
117  * The input arguments of the condition function are:
118  * <ul>
119  * <li>PointT The first point of the point pair</li>
120  * <li>PointT The second point of the point pair</li>
121  * <li>float The squared distance between the points</li>
122  * </ul>
123  * The output argument is a boolean, returning true will merge the second point into the cluster of the first point.
124  * \param[in] condition_function The condition function that needs to hold for clustering
125  */
126  inline void
127  setConditionFunction (bool (*condition_function) (const PointT&, const PointT&, float))
128  {
129  condition_function_ = condition_function;
130  }
131 
132  /** \brief Set the condition that needs to hold for neighboring points to be considered part of the same cluster.
133  * This is an overloaded function provided for convenience. See the documentation for setConditionFunction(). */
134  inline void
135  setConditionFunction (boost::function<bool (const PointT&, const PointT&, float)> condition_function)
136  {
137  condition_function_ = condition_function;
138  }
139 
140  /** \brief Set the spatial tolerance for new cluster candidates.
141  * \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.
142  * The condition can be set using setConditionFunction().
143  * \param[in] cluster_tolerance The distance to scan for cluster candidates (default = 0.0)
144  */
145  inline void
146  setClusterTolerance (float cluster_tolerance)
147  {
148  cluster_tolerance_ = cluster_tolerance;
149  }
150 
151  /** \brief Get the spatial tolerance for new cluster candidates.*/
152  inline float
154  {
155  return (cluster_tolerance_);
156  }
157 
158  /** \brief Set the minimum number of points that a cluster needs to contain in order to be considered valid.
159  * \param[in] min_cluster_size The minimum cluster size (default = 1)
160  */
161  inline void
162  setMinClusterSize (int min_cluster_size)
163  {
164  min_cluster_size_ = min_cluster_size;
165  }
166 
167  /** \brief Get the minimum number of points that a cluster needs to contain in order to be considered valid.*/
168  inline int
170  {
171  return (min_cluster_size_);
172  }
173 
174  /** \brief Set the maximum number of points that a cluster needs to contain in order to be considered valid.
175  * \param[in] max_cluster_size The maximum cluster size (default = unlimited)
176  */
177  inline void
178  setMaxClusterSize (int max_cluster_size)
179  {
180  max_cluster_size_ = max_cluster_size;
181  }
182 
183  /** \brief Get the maximum number of points that a cluster needs to contain in order to be considered valid.*/
184  inline int
186  {
187  return (max_cluster_size_);
188  }
189 
190  /** \brief Segment the input into separate clusters.
191  * \details The input can be set using setInputCloud() and setIndices().
192  * <br>
193  * The size constraints for the resulting clusters can be set using setMinClusterSize() and setMaxClusterSize().
194  * <br>
195  * The region growing parameters can be set using setConditionFunction() and setClusterTolerance().
196  * <br>
197  * \param[out] clusters The resultant set of indices, indexing the points of the input cloud that correspond to the clusters
198  */
199  void
200  segment (IndicesClusters &clusters);
201 
202  /** \brief Get the clusters that are invalidated due to size constraints.
203  * \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.
204  * \param[out] small_clusters The resultant clusters that contain less than min_cluster_size points
205  * \param[out] large_clusters The resultant clusters that contain more than max_cluster_size points
206  */
207  inline void
208  getRemovedClusters (IndicesClustersPtr &small_clusters, IndicesClustersPtr &large_clusters)
209  {
210  if (!extract_removed_clusters_)
211  {
212  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");
213  return;
214  }
215  small_clusters = small_clusters_;
216  large_clusters = large_clusters_;
217  }
218 
219  private:
220  /** \brief A pointer to the spatial search object */
221  SearcherPtr searcher_;
222 
223  /** \brief The condition function that needs to hold for clustering */
224  boost::function<bool (const PointT&, const PointT&, float)> condition_function_;
225 
226  /** \brief The distance to scan for cluster candidates (default = 0.0) */
227  float cluster_tolerance_;
228 
229  /** \brief The minimum cluster size (default = 1) */
230  int min_cluster_size_;
231 
232  /** \brief The maximum cluster size (default = unlimited) */
233  int max_cluster_size_;
234 
235  /** \brief Set to true if you want to be able to extract the clusters that are too large or too small (default = false) */
236  bool extract_removed_clusters_;
237 
238  /** \brief The resultant clusters that contain less than min_cluster_size points */
239  pcl::IndicesClustersPtr small_clusters_;
240 
241  /** \brief The resultant clusters that contain more than max_cluster_size points */
242  pcl::IndicesClustersPtr large_clusters_;
243 
244  public:
245  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
246  };
247 }
248 
249 #ifdef PCL_NO_PRECOMPILE
250 #include <pcl/segmentation/impl/conditional_euclidean_clustering.hpp>
251 #endif
252 
253 #endif // PCL_SEGMENTATION_CONDITIONAL_EUCLIDEAN_CLUSTERING_H_
254 
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.
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...
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...
boost::shared_ptr< pcl::search::Search< PointT > > Ptr
Definition: search.h:81
ConditionalEuclideanClustering performs segmentation based on Euclidean distance and a user-defined c...
void setConditionFunction(boost::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...
PCL base class.
Definition: pcl_base.h:68
pcl::search::Search< PointT >::Ptr SearcherPtr
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.
boost::shared_ptr< std::vector< pcl::PointIndices > > IndicesClustersPtr
A point structure representing Euclidean xyz coordinates, and the RGB color.
std::vector< pcl::PointIndices > IndicesClusters