Point Cloud Library (PCL)  1.10.1
euclidean_cluster_comparator.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, 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  */
39 
40 #pragma once
41 
42 #include <pcl/pcl_macros.h>
43 #include <pcl/segmentation/boost.h>
44 #include <pcl/segmentation/comparator.h>
45 #include <pcl/point_types.h>
46 
47 namespace pcl
48 {
49  namespace experimental
50  {
51  template<typename PointT, typename PointLT = pcl::Label>
53  {
54  protected:
55 
57 
58  public:
59  using typename Comparator<PointT>::PointCloud;
61 
63  using PointCloudLPtr = typename PointCloudL::Ptr;
65 
68 
69  using ExcludeLabelSet = std::set<std::uint32_t>;
72 
73  /** \brief Default constructor for EuclideanClusterComparator. */
75  : distance_threshold_ (0.005f)
76  , depth_dependent_ ()
77  {}
78 
79  void
80  setInputCloud (const PointCloudConstPtr& cloud) override
81  {
82  input_ = cloud;
83  Eigen::Matrix3f rot = input_->sensor_orientation_.toRotationMatrix ();
84  z_axis_ = rot.col (2);
85  }
86 
87  /** \brief Set the tolerance in meters for difference in perpendicular distance (d component of plane equation) to the plane between neighboring points, to be considered part of the same plane.
88  * \param[in] distance_threshold the tolerance in meters
89  * \param depth_dependent
90  */
91  inline void
92  setDistanceThreshold (float distance_threshold, bool depth_dependent)
93  {
94  distance_threshold_ = distance_threshold;
95  depth_dependent_ = depth_dependent;
96  }
97 
98  /** \brief Get the distance threshold in meters (d component of plane equation) between neighboring points, to be considered part of the same plane. */
99  inline float
101  {
102  return (distance_threshold_);
103  }
104 
105  /** \brief Set label cloud
106  * \param[in] labels The label cloud
107  */
108  void
109  setLabels (const PointCloudLPtr& labels)
110  {
111  labels_ = labels;
112  }
113 
116  {
117  return exclude_labels_;
118  }
119 
120  /** \brief Set labels in the label cloud to exclude.
121  * \param exclude_labels a vector of bools corresponding to whether or not a given label should be considered
122  */
123  void
125  {
126  exclude_labels_ = exclude_labels;
127  }
128 
129  /** \brief Compare points at two indices by their euclidean distance
130  * \param idx1 The first index for the comparison
131  * \param idx2 The second index for the comparison
132  */
133  bool
134  compare (int idx1, int idx2) const override
135  {
136  if (labels_ && exclude_labels_)
137  {
138  assert (labels_->size () == input_->size ());
139  const std::uint32_t &label1 = (*labels_)[idx1].label;
140  const std::uint32_t &label2 = (*labels_)[idx2].label;
141 
142  const std::set<std::uint32_t>::const_iterator it1 = exclude_labels_->find (label1);
143  if (it1 == exclude_labels_->end ())
144  return false;
145 
146  const std::set<std::uint32_t>::const_iterator it2 = exclude_labels_->find (label2);
147  if (it2 == exclude_labels_->end ())
148  return false;
149  }
150 
151  float dist_threshold = distance_threshold_;
152  if (depth_dependent_)
153  {
154  Eigen::Vector3f vec = input_->points[idx1].getVector3fMap ();
155  float z = vec.dot (z_axis_);
156  dist_threshold *= z * z;
157  }
158 
159  const float dist = ((*input_)[idx1].getVector3fMap ()
160  - (*input_)[idx2].getVector3fMap ()).norm ();
161  return (dist < dist_threshold);
162  }
163 
164  protected:
165 
166 
167  /** \brief Set of labels with similar size as the input point cloud,
168  * aggregating points into groups based on a similar label identifier.
169  *
170  * It needs to be set in conjunction with the \ref exclude_labels_
171  * member in order to provided a masking functionality.
172  */
174 
175  /** \brief Specifies which labels should be excluded com being clustered.
176  *
177  * If a label is not specified, it's assumed by default that it's
178  * intended be excluded
179  */
181 
183 
185 
186  Eigen::Vector3f z_axis_;
187  };
188  } // namespace experimental
189 
190 
191  /** \brief EuclideanClusterComparator is a comparator used for finding clusters based on euclidian distance.
192  *
193  * \author Alex Trevor
194  */
195  template<typename PointT, typename PointNT, typename PointLT = deprecated::T>
197  {
198  protected:
199 
201 
202  public:
203 
207 
210 
212 
213  /** \brief Default constructor for EuclideanClusterComparator. */
214  PCL_DEPRECATED("remove PointNT from template parameters")
216  : normals_ ()
217  , angular_threshold_ (0.0f)
218  {}
219 
220  /** \brief Provide a pointer to the input normals.
221  * \param[in] normals the input normal cloud
222  */
223  PCL_DEPRECATED("EuclideadClusterComparator never actually used normals and angular threshold, this function has no effect on the behavior of the comparator. It is deprecated and will be removed in future releases.")
224  inline void
225  setInputNormals (const PointCloudNConstPtr& normals) { normals_ = normals; }
226 
227  /** \brief Get the input normals. */
228  PCL_DEPRECATED("EuclideadClusterComparator never actually used normals and angular threshold, this function has no effect on the behavior of the comparator. It is deprecated and will be removed in future releases.")
229  inline PointCloudNConstPtr
230  getInputNormals () const { return (normals_); }
231 
232  /** \brief Set the tolerance in radians for difference in normal direction between neighboring points, to be considered part of the same plane.
233  * \param[in] angular_threshold the tolerance in radians
234  */
235  PCL_DEPRECATED("EuclideadClusterComparator never actually used normals and angular threshold, this function has no effect on the behavior of the comparator. It is deprecated and will be removed in future releases.")
236  inline void
237  setAngularThreshold (float angular_threshold)
238  {
239  angular_threshold_ = std::cos (angular_threshold);
240  }
241 
242  /** \brief Get the angular threshold in radians for difference in normal direction between neighboring points, to be considered part of the same plane. */
243  PCL_DEPRECATED("EuclideadClusterComparator never actually used normals and angular threshold, this function has no effect on the behavior of the comparator. It is deprecated and will be removed in future releases.")
244  inline float
245  getAngularThreshold () const { return (std::acos (angular_threshold_) ); }
246 
247  /** \brief Set labels in the label cloud to exclude.
248  * \param[in] exclude_labels a vector of bools corresponding to whether or not a given label should be considered
249  */
250  PCL_DEPRECATED("use setExcludeLabels(const ExcludeLabelSetConstPtr &) instead")
251  void
252  setExcludeLabels (const std::vector<bool>& exclude_labels)
253  {
254  exclude_labels_ = boost::make_shared<std::set<std::uint32_t> > ();
255  for (std::size_t i = 0; i < exclude_labels.size (); ++i)
256  if (exclude_labels[i])
257  exclude_labels_->insert (i);
258  }
259 
260  protected:
261 
263 
265  };
266 
267  template<typename PointT, typename PointLT>
268  class EuclideanClusterComparator<PointT, PointLT, deprecated::T>
269  : public experimental::EuclideanClusterComparator<PointT, PointLT> {};
270 }
pcl_macros.h
Defines all the PCL and non-PCL macros used.
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
point_types.h
pcl::experimental::EuclideanClusterComparator::compare
bool compare(int idx1, int idx2) const override
Compare points at two indices by their euclidean distance.
Definition: euclidean_cluster_comparator.h:134
pcl::experimental::EuclideanClusterComparator::getDistanceThreshold
float getDistanceThreshold() const
Get the distance threshold in meters (d component of plane equation) between neighboring points,...
Definition: euclidean_cluster_comparator.h:100
pcl::EuclideanClusterComparator::PointCloudNConstPtr
typename PointCloudN::ConstPtr PointCloudNConstPtr
Definition: euclidean_cluster_comparator.h:206
pcl::experimental::EuclideanClusterComparator< PointT, deprecated::T >::ExcludeLabelSet
std::set< std::uint32_t > ExcludeLabelSet
Definition: euclidean_cluster_comparator.h:69
pcl::experimental::EuclideanClusterComparator< PointT, deprecated::T >::PointCloudLPtr
typename PointCloudL::Ptr PointCloudLPtr
Definition: euclidean_cluster_comparator.h:63
pcl::experimental::EuclideanClusterComparator::setDistanceThreshold
void setDistanceThreshold(float distance_threshold, bool depth_dependent)
Set the tolerance in meters for difference in perpendicular distance (d component of plane equation) ...
Definition: euclidean_cluster_comparator.h:92
pcl::experimental::EuclideanClusterComparator< PointT, deprecated::T >::PointCloudLConstPtr
typename PointCloudL::ConstPtr PointCloudLConstPtr
Definition: euclidean_cluster_comparator.h:64
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:52
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:620
pcl::Comparator::ConstPtr
shared_ptr< const Comparator< PointT > > ConstPtr
Definition: comparator.h:61
pcl::experimental::EuclideanClusterComparator::getExcludeLabels
const ExcludeLabelSetConstPtr & getExcludeLabels() const
Definition: euclidean_cluster_comparator.h:115
pcl::experimental::EuclideanClusterComparator
Definition: euclidean_cluster_comparator.h:52
pcl::experimental::EuclideanClusterComparator::z_axis_
Eigen::Vector3f z_axis_
Definition: euclidean_cluster_comparator.h:186
pcl::experimental::EuclideanClusterComparator::distance_threshold_
float distance_threshold_
Definition: euclidean_cluster_comparator.h:182
pcl::experimental::EuclideanClusterComparator::EuclideanClusterComparator
EuclideanClusterComparator()
Default constructor for EuclideanClusterComparator.
Definition: euclidean_cluster_comparator.h:74
pcl::experimental::EuclideanClusterComparator::exclude_labels_
ExcludeLabelSetConstPtr exclude_labels_
Specifies which labels should be excluded com being clustered.
Definition: euclidean_cluster_comparator.h:180
pcl::experimental::EuclideanClusterComparator::labels_
PointCloudLPtr labels_
Set of labels with similar size as the input point cloud, aggregating points into groups based on a s...
Definition: euclidean_cluster_comparator.h:173
pcl::experimental::EuclideanClusterComparator::setExcludeLabels
void setExcludeLabels(const ExcludeLabelSetConstPtr &exclude_labels)
Set labels in the label cloud to exclude.
Definition: euclidean_cluster_comparator.h:124
pcl::experimental::EuclideanClusterComparator< PointT, deprecated::T >::ExcludeLabelSetConstPtr
shared_ptr< const ExcludeLabelSet > ExcludeLabelSetConstPtr
Definition: euclidean_cluster_comparator.h:71
pcl::experimental::EuclideanClusterComparator::depth_dependent_
bool depth_dependent_
Definition: euclidean_cluster_comparator.h:184
pcl::Comparator
Comparator is the base class for comparators that compare two points given some function.
Definition: comparator.h:53
pcl::EuclideanClusterComparator::getAngularThreshold
float getAngularThreshold() const
Get the angular threshold in radians for difference in normal direction between neighboring points,...
Definition: euclidean_cluster_comparator.h:245
pcl::experimental::EuclideanClusterComparator::setInputCloud
void setInputCloud(const PointCloudConstPtr &cloud) override
Set the input cloud for the comparator.
Definition: euclidean_cluster_comparator.h:80
pcl::EuclideanClusterComparator
EuclideanClusterComparator is a comparator used for finding clusters based on euclidian distance.
Definition: euclidean_cluster_comparator.h:196
pcl::Comparator::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: comparator.h:58
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:415
pcl::EuclideanClusterComparator::PointCloudNPtr
typename PointCloudN::Ptr PointCloudNPtr
Definition: euclidean_cluster_comparator.h:205
pcl::EuclideanClusterComparator::getInputNormals
PointCloudNConstPtr getInputNormals() const
Get the input normals.
Definition: euclidean_cluster_comparator.h:230
pcl::EuclideanClusterComparator::setExcludeLabels
void setExcludeLabels(const std::vector< bool > &exclude_labels)
Set labels in the label cloud to exclude.
Definition: euclidean_cluster_comparator.h:252
pcl::experimental::EuclideanClusterComparator< PointT, deprecated::T >::ExcludeLabelSetPtr
shared_ptr< ExcludeLabelSet > ExcludeLabelSetPtr
Definition: euclidean_cluster_comparator.h:70
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:416
pcl::EuclideanClusterComparator::setAngularThreshold
void setAngularThreshold(float angular_threshold)
Set the tolerance in radians for difference in normal direction between neighboring points,...
Definition: euclidean_cluster_comparator.h:237
pcl::EuclideanClusterComparator::angular_threshold_
float angular_threshold_
Definition: euclidean_cluster_comparator.h:264
PCL_DEPRECATED
#define PCL_DEPRECATED(message)
Definition: pcl_macros.h:94
pcl::Comparator::input_
PointCloudConstPtr input_
Definition: comparator.h:99
pcl::Comparator::Ptr
shared_ptr< Comparator< PointT > > Ptr
Definition: comparator.h:60
pcl::EuclideanClusterComparator::normals_
PointCloudNConstPtr normals_
Definition: euclidean_cluster_comparator.h:262
pcl::EuclideanClusterComparator::setInputNormals
void setInputNormals(const PointCloudNConstPtr &normals)
Provide a pointer to the input normals.
Definition: euclidean_cluster_comparator.h:225
pcl::shared_ptr
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:108
pcl::experimental::EuclideanClusterComparator::setLabels
void setLabels(const PointCloudLPtr &labels)
Set label cloud.
Definition: euclidean_cluster_comparator.h:109