Point Cloud Library (PCL)  1.11.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
edge_aware_plane_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  * $Id: extract_clusters.h 5027 2012-03-12 03:10:45Z rusu $
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/segmentation/boost.h>
43 #include <pcl/segmentation/plane_coefficient_comparator.h>
44 
45 namespace pcl
46 {
47  /** \brief EdgeAwarePlaneComparator is a Comparator that operates on plane coefficients,
48  * for use in planar segmentation.
49  * In conjunction with OrganizedConnectedComponentSegmentation, this allows planes to be segmented from organized data.
50  *
51  * \author Stefan Holzer, Alex Trevor
52  */
53  template<typename PointT, typename PointNT>
55  {
56  public:
59 
61  using PointCloudNPtr = typename PointCloudN::Ptr;
63 
64  using Ptr = shared_ptr<EdgeAwarePlaneComparator<PointT, PointNT> >;
65  using ConstPtr = shared_ptr<const EdgeAwarePlaneComparator<PointT, PointNT> >;
66 
74 
75  /** \brief Empty constructor for PlaneCoefficientComparator. */
78  curvature_threshold_ (0.04f),
80  {
81  }
82 
83  /** \brief Empty constructor for PlaneCoefficientComparator.
84  * \param[in] distance_map the distance map to use
85  */
86  EdgeAwarePlaneComparator (const float *distance_map) :
87  distance_map_ (distance_map),
89  curvature_threshold_ (0.04f),
91  {
92  }
93 
94  /** \brief Destructor for PlaneCoefficientComparator. */
95 
97  {
98  }
99 
100  /** \brief Set a distance map to use. For an example of a valid distance map see
101  * IntegralImageNormalEstimation::getDistanceMap
102  * \param[in] distance_map the distance map to use
103  */
104  inline void
105  setDistanceMap (const float *distance_map)
106  {
107  distance_map_ = distance_map;
108  }
109 
110  /** \brief Return the distance map used. */
111  const float*
112  getDistanceMap () const
113  {
114  return (distance_map_);
115  }
116 
117  /** \brief Set the curvature threshold for creating a new segment
118  * \param[in] curvature_threshold a threshold for the curvature
119  */
120  void
121  setCurvatureThreshold (float curvature_threshold)
122  {
123  curvature_threshold_ = curvature_threshold;
124  }
125 
126  /** \brief Get the curvature threshold. */
127  inline float
129  {
130  return (curvature_threshold_);
131  }
132 
133  /** \brief Set the distance map threshold -- the number of pixel away from a border / nan
134  * \param[in] distance_map_threshold the distance map threshold
135  */
136  void
137  setDistanceMapThreshold (float distance_map_threshold)
138  {
139  distance_map_threshold_ = distance_map_threshold;
140  }
141 
142  /** \brief Get the distance map threshold (in pixels). */
143  inline float
145  {
146  return (distance_map_threshold_);
147  }
148 
149  /** \brief Set the euclidean distance threshold.
150  * \param[in] euclidean_distance_threshold the euclidean distance threshold in meters
151  */
152  void
153  setEuclideanDistanceThreshold (float euclidean_distance_threshold)
154  {
155  euclidean_distance_threshold_ = euclidean_distance_threshold;
156  }
157 
158  /** \brief Get the euclidean distance threshold. */
159  inline float
161  {
163  }
164 
165  protected:
166  /** \brief Compare two neighboring points, by using normal information, curvature, and euclidean distance information.
167  * \param[in] idx1 The index of the first point.
168  * \param[in] idx2 The index of the second point.
169  */
170  bool
171  compare (int idx1, int idx2) const override
172  {
173  // Note: there are two distance thresholds here that make sense to scale with depth.
174  // dist_threshold is on the perpendicular distance to the plane, as in plane comparator
175  // We additionally check euclidean distance to ensure that we don't have neighboring coplanar points
176  // that aren't close in euclidean space (think two tables separated by a meter, viewed from an angle
177  // where the surfaces are adjacent in image space).
178  float dist_threshold = distance_threshold_;
179  float euclidean_dist_threshold = euclidean_distance_threshold_;
180  if (depth_dependent_)
181  {
182  Eigen::Vector3f vec = input_->points[idx1].getVector3fMap ();
183  float z = vec.dot (z_axis_);
184  dist_threshold *= z * z;
185  euclidean_dist_threshold *= z * z;
186  }
187 
188  float dx = input_->points[idx1].x - input_->points[idx2].x;
189  float dy = input_->points[idx1].y - input_->points[idx2].y;
190  float dz = input_->points[idx1].z - input_->points[idx2].z;
191  float dist = std::sqrt (dx*dx + dy*dy + dz*dz);
192 
193  bool normal_ok = (normals_->points[idx1].getNormalVector3fMap ().dot (normals_->points[idx2].getNormalVector3fMap () ) > angular_threshold_ );
194  bool dist_ok = (dist < euclidean_dist_threshold);
195 
196  bool curvature_ok = normals_->points[idx1].curvature < curvature_threshold_;
197  bool plane_d_ok = std::abs ((*plane_coeff_d_)[idx1] - (*plane_coeff_d_)[idx2]) < dist_threshold;
198 
200  curvature_ok = false;
201 
202  return (dist_ok && normal_ok && curvature_ok && plane_d_ok);
203  }
204 
205  protected:
206  const float* distance_map_;
210  };
211 }
void setDistanceMap(const float *distance_map)
Set a distance map to use.
typename PointCloudN::Ptr PointCloudNPtr
void setCurvatureThreshold(float curvature_threshold)
Set the curvature threshold for creating a new segment.
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: comparator.h:59
EdgeAwarePlaneComparator(const float *distance_map)
Empty constructor for PlaneCoefficientComparator.
float getEuclideanDistanceThreshold() const
Get the euclidean distance threshold.
const float * getDistanceMap() const
Return the distance map used.
float getDistanceMapThreshold() const
Get the distance map threshold (in pixels).
~EdgeAwarePlaneComparator()
Destructor for PlaneCoefficientComparator.
shared_ptr< PointCloud< PointNT > > Ptr
Definition: point_cloud.h:428
shared_ptr< const Comparator< PointT > > ConstPtr
Definition: comparator.h:62
EdgeAwarePlaneComparator()
Empty constructor for PlaneCoefficientComparator.
PlaneCoefficientComparator is a Comparator that operates on plane coefficients, for use in planar seg...
PointCloud represents the base class in PCL for storing collections of 3D points. ...
shared_ptr< std::vector< float > > plane_coeff_d_
shared_ptr< const PointCloud< PointNT > > ConstPtr
Definition: point_cloud.h:429
bool compare(int idx1, int idx2) const override
Compare two neighboring points, by using normal information, curvature, and euclidean distance inform...
EdgeAwarePlaneComparator is a Comparator that operates on plane coefficients, for use in planar segme...
typename PointCloudN::ConstPtr PointCloudNConstPtr
shared_ptr< Comparator< PointT > > Ptr
Definition: comparator.h:61
PointCloudConstPtr input_
Definition: comparator.h:100
float getCurvatureThreshold() const
Get the curvature threshold.
void setDistanceMapThreshold(float distance_map_threshold)
Set the distance map threshold – the number of pixel away from a border / nan.
void setEuclideanDistanceThreshold(float euclidean_distance_threshold)
Set the euclidean distance threshold.