Point Cloud Library (PCL)  1.14.1-dev
organized_multi_plane_segmentation.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  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/segmentation/planar_region.h>
43 #include <pcl/pcl_base.h>
44 #include <pcl/pcl_macros.h>
45 #include <pcl/common/angles.h>
46 #include <pcl/common/utils.h>
47 #include <pcl/PointIndices.h>
48 #include <pcl/ModelCoefficients.h>
49 #include <pcl/segmentation/plane_coefficient_comparator.h>
50 #include <pcl/segmentation/plane_refinement_comparator.h>
51 
52 namespace pcl
53 {
54  /** \brief OrganizedMultiPlaneSegmentation finds all planes present in the
55  * input cloud, and outputs a vector of plane equations, as well as a vector
56  * of point clouds corresponding to the inliers of each detected plane. Only
57  * planes with more than min_inliers points are detected.
58  * Templated on point type, normal type, and label type
59  *
60  * \author Alex Trevor, Suat Gedikli
61  */
62  template<typename PointT, typename PointNT, typename PointLT>
64  {
69 
70  public:
72  using PointCloudPtr = typename PointCloud::Ptr;
74 
76  using PointCloudNPtr = typename PointCloudN::Ptr;
78 
80  using PointCloudLPtr = typename PointCloudL::Ptr;
82 
86 
90 
91  /** \brief Constructor for OrganizedMultiPlaneSegmentation. */
93 
94  /** \brief Destructor for OrganizedMultiPlaneSegmentation. */
95 
96  ~OrganizedMultiPlaneSegmentation () override = default;
97 
98  /** \brief Provide a pointer to the input normals.
99  * \param[in] normals the input normal cloud
100  */
101  inline void
103  {
104  normals_ = normals;
105  }
106 
107  /** \brief Get the input normals. */
108  inline PointCloudNConstPtr
110  {
111  return (normals_);
112  }
113 
114  /** \brief Set the minimum number of inliers required for a plane
115  * \param[in] min_inliers the minimum number of inliers required per plane
116  */
117  inline void
118  setMinInliers (unsigned min_inliers)
119  {
120  min_inliers_ = min_inliers;
121  }
122 
123  /** \brief Get the minimum number of inliers required per plane. */
124  inline unsigned
125  getMinInliers () const
126  {
127  return (min_inliers_);
128  }
129 
130  /** \brief Set the tolerance in radians for difference in normal direction between neighboring points, to be considered part of the same plane.
131  * \param[in] angular_threshold the tolerance in radians
132  */
133  inline void
134  setAngularThreshold (double angular_threshold)
135  {
136  angular_threshold_ = angular_threshold;
137  }
138 
139  /** \brief Get the angular threshold in radians for difference in normal direction between neighboring points, to be considered part of the same plane. */
140  inline double
142  {
143  return (angular_threshold_);
144  }
145 
146  /** \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.
147  * \param[in] distance_threshold the tolerance in meters
148  */
149  inline void
150  setDistanceThreshold (double distance_threshold)
151  {
152  distance_threshold_ = distance_threshold;
153  }
154 
155  /** \brief Get the distance threshold in meters (d component of plane equation) between neighboring points, to be considered part of the same plane. */
156  inline double
158  {
159  return (distance_threshold_);
160  }
161 
162  /** \brief Set the maximum curvature allowed for a planar region.
163  * \param[in] maximum_curvature the maximum curvature
164  */
165  inline void
166  setMaximumCurvature (double maximum_curvature)
167  {
168  maximum_curvature_ = maximum_curvature;
169  }
170 
171  /** \brief Get the maximum curvature allowed for a planar region. */
172  inline double
174  {
175  return (maximum_curvature_);
176  }
177 
178  /** \brief Provide a pointer to the comparator to be used for segmentation.
179  * \param[in] compare A pointer to the comparator to be used for segmentation.
180  */
181  void
183  {
184  compare_ = compare;
185  }
186 
187  /** \brief Provide a pointer to the comparator to be used for refinement.
188  * \param[in] compare A pointer to the comparator to be used for refinement.
189  */
190  void
192  {
193  refinement_compare_ = compare;
194  }
195 
196  /** \brief Set whether or not to project boundary points to the plane, or leave them in the original 3D space.
197  * \param[in] project_points true if points should be projected, false if not.
198  */
199  void
200  setProjectPoints (bool project_points)
201  {
202  project_points_ = project_points;
203  }
204 
205  /** \brief Segmentation of all planes in a point cloud given by setInputCloud(), setIndices()
206  * \param[out] model_coefficients a vector of model_coefficients for each plane found in the input cloud
207  * \param[out] inlier_indices a vector of inliers for each detected plane
208  * \param[out] centroids a vector of centroids for each plane
209  * \param[out] covariances a vector of covariance matrices for the inliers of each plane
210  * \param[out] labels a point cloud for the connected component labels of each pixel
211  * \param[out] label_indices a vector of PointIndices for each labeled component
212  */
213  void
214  segment (std::vector<ModelCoefficients>& model_coefficients,
215  std::vector<PointIndices>& inlier_indices,
216  std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> >& centroids,
217  std::vector <Eigen::Matrix3f, Eigen::aligned_allocator<Eigen::Matrix3f> >& covariances,
218  pcl::PointCloud<PointLT>& labels,
219  std::vector<pcl::PointIndices>& label_indices);
220 
221  /** \brief Segmentation of all planes in a point cloud given by setInputCloud(), setIndices()
222  * \param[out] model_coefficients a vector of model_coefficients for each plane found in the input cloud
223  * \param[out] inlier_indices a vector of inliers for each detected plane
224  */
225  void
226  segment (std::vector<ModelCoefficients>& model_coefficients,
227  std::vector<PointIndices>& inlier_indices);
228 
229  /** \brief Segmentation of all planes in a point cloud given by setInputCloud(), setIndices()
230  * \param[out] regions a list of resultant planar polygonal regions
231  */
232  void
233  segment (std::vector<PlanarRegion<PointT>, Eigen::aligned_allocator<PlanarRegion<PointT> > >& regions);
234 
235  /** \brief Perform a segmentation, as well as an additional refinement step. This helps with including points whose normals may not match neighboring points well, but may match the planar model well.
236  * \param[out] regions A list of regions generated by segmentation and refinement.
237  */
238  void
239  segmentAndRefine (std::vector<PlanarRegion<PointT>, Eigen::aligned_allocator<PlanarRegion<PointT> > >& regions);
240 
241  /** \brief Perform a segmentation, as well as additional refinement step. Returns intermediate data structures for use in
242  * subsequent processing.
243  * \param[out] regions A vector of PlanarRegions generated by segmentation
244  * \param[out] model_coefficients A vector of model coefficients for each segmented plane
245  * \param[out] inlier_indices A vector of PointIndices, indicating the inliers to each segmented plane
246  * \param[out] labels A PointCloud<PointLT> corresponding to the resulting segmentation.
247  * \param[out] label_indices A vector of PointIndices for each label
248  * \param[out] boundary_indices A vector of PointIndices corresponding to the outer boundary / contour of each label
249  */
250  void
251  segmentAndRefine (std::vector<PlanarRegion<PointT>, Eigen::aligned_allocator<PlanarRegion<PointT> > >& regions,
252  std::vector<ModelCoefficients>& model_coefficients,
253  std::vector<PointIndices>& inlier_indices,
254  PointCloudLPtr& labels,
255  std::vector<pcl::PointIndices>& label_indices,
256  std::vector<pcl::PointIndices>& boundary_indices);
257 
258  /** \brief Perform a refinement of an initial segmentation, by comparing points to adjacent regions detected by the initial segmentation.
259  * \param [in] model_coefficients The list of segmented model coefficients
260  * \param [in] inlier_indices The list of segmented inlier indices, corresponding to each model
261  * \param [in] labels The labels produced by the initial segmentation
262  * \param [in] label_indices The list of indices corresponding to each label
263  */
264  void
265  refine (std::vector<ModelCoefficients>& model_coefficients,
266  std::vector<PointIndices>& inlier_indices,
267  PointCloudLPtr& labels,
268  std::vector<pcl::PointIndices>& label_indices);
269 
270  protected:
271 
272  /** \brief A pointer to the input normals */
274 
275  /** \brief The minimum number of inliers required for each plane. */
276  unsigned min_inliers_{1000};
277 
278  /** \brief The tolerance in radians for difference in normal direction between neighboring points, to be considered part of the same plane. */
280 
281  /** \brief 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. */
282  double distance_threshold_{0.02};
283 
284  /** \brief The tolerance for maximum curvature after fitting a plane. Used to remove smooth, but non-planar regions. */
285  double maximum_curvature_{0.001};
286 
287  /** \brief Whether or not points should be projected to the plane, or left in the original 3D space. */
288  bool project_points_{false};
289 
290  /** \brief A comparator for comparing neighboring pixels' plane equations. */
292 
293  /** \brief A comparator for use on the refinement step. Compares points to regions segmented in the first pass. */
295 
296  /** \brief Class getName method. */
297  virtual std::string
298  getClassName () const
299  {
300  return ("OrganizedMultiPlaneSegmentation");
301  }
302  };
303 
304 }
305 
306 #ifdef PCL_NO_PRECOMPILE
307 #include <pcl/segmentation/impl/organized_multi_plane_segmentation.hpp>
308 #endif
Define standard C methods to do angle calculations.
OrganizedMultiPlaneSegmentation finds all planes present in the input cloud, and outputs a vector of ...
void setInputNormals(const PointCloudNConstPtr &normals)
Provide a pointer to the input normals.
double getDistanceThreshold() const
Get the distance threshold in meters (d component of plane equation) between neighboring points,...
void segment(std::vector< ModelCoefficients > &model_coefficients, std::vector< PointIndices > &inlier_indices, std::vector< Eigen::Vector4f, Eigen::aligned_allocator< Eigen::Vector4f > > &centroids, std::vector< Eigen::Matrix3f, Eigen::aligned_allocator< Eigen::Matrix3f > > &covariances, pcl::PointCloud< PointLT > &labels, std::vector< pcl::PointIndices > &label_indices)
Segmentation of all planes in a point cloud given by setInputCloud(), setIndices()
void setRefinementComparator(const PlaneRefinementComparatorPtr &compare)
Provide a pointer to the comparator to be used for refinement.
unsigned getMinInliers() const
Get the minimum number of inliers required per plane.
void setDistanceThreshold(double distance_threshold)
Set the tolerance in meters for difference in perpendicular distance (d component of plane equation) ...
bool project_points_
Whether or not points should be projected to the plane, or left in the original 3D space.
PlaneComparatorPtr compare_
A comparator for comparing neighboring pixels' plane equations.
double distance_threshold_
The tolerance in meters for difference in perpendicular distance (d component of plane equation) to t...
unsigned min_inliers_
The minimum number of inliers required for each plane.
void segmentAndRefine(std::vector< PlanarRegion< PointT >, Eigen::aligned_allocator< PlanarRegion< PointT > > > &regions)
Perform a segmentation, as well as an additional refinement step.
void setComparator(const PlaneComparatorPtr &compare)
Provide a pointer to the comparator to be used for segmentation.
double maximum_curvature_
The tolerance for maximum curvature after fitting a plane.
~OrganizedMultiPlaneSegmentation() override=default
Destructor for OrganizedMultiPlaneSegmentation.
OrganizedMultiPlaneSegmentation()=default
Constructor for OrganizedMultiPlaneSegmentation.
void setMinInliers(unsigned min_inliers)
Set the minimum number of inliers required for a plane.
virtual std::string getClassName() const
Class getName method.
double getMaximumCurvature() const
Get the maximum curvature allowed for a planar region.
typename PlaneRefinementComparator::ConstPtr PlaneRefinementComparatorConstPtr
void setProjectPoints(bool project_points)
Set whether or not to project boundary points to the plane, or leave them in the original 3D space.
typename PlaneComparator::ConstPtr PlaneComparatorConstPtr
double angular_threshold_
The tolerance in radians for difference in normal direction between neighboring points,...
PointCloudNConstPtr getInputNormals() const
Get the input normals.
void setMaximumCurvature(double maximum_curvature)
Set the maximum curvature allowed for a planar region.
void setAngularThreshold(double angular_threshold)
Set the tolerance in radians for difference in normal direction between neighboring points,...
double getAngularThreshold() const
Get the angular threshold in radians for difference in normal direction between neighboring points,...
PointCloudNConstPtr normals_
A pointer to the input normals.
PlaneRefinementComparatorPtr refinement_compare_
A comparator for use on the refinement step.
typename PlaneRefinementComparator::Ptr PlaneRefinementComparatorPtr
void refine(std::vector< ModelCoefficients > &model_coefficients, std::vector< PointIndices > &inlier_indices, PointCloudLPtr &labels, std::vector< pcl::PointIndices > &label_indices)
Perform a refinement of an initial segmentation, by comparing points to adjacent regions detected by ...
PCL base class.
Definition: pcl_base.h:70
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
PlanarRegion represents a set of points that lie in a plane.
Definition: planar_region.h:52
PlaneCoefficientComparator is a Comparator that operates on plane coefficients, for use in planar seg...
shared_ptr< PlaneCoefficientComparator< PointT, PointNT > > Ptr
shared_ptr< const PlaneCoefficientComparator< PointT, PointNT > > ConstPtr
PlaneRefinementComparator is a Comparator that operates on plane coefficients, for use in planar segm...
shared_ptr< PlaneRefinementComparator< PointT, PointNT, PointLT > > Ptr
shared_ptr< const PlaneRefinementComparator< PointT, PointNT, PointLT > > ConstPtr
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
float deg2rad(float alpha)
Convert an angle from degrees to radians.
Definition: angles.hpp:67
Defines all the PCL and non-PCL macros used.