Point Cloud Library (PCL)  1.12.1-dev
plane_coefficient_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$
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/common/angles.h>
43 #include <pcl/memory.h> // for pcl::make_shared, pcl::shared_ptr
44 #include <pcl/pcl_macros.h>
45 #include <pcl/segmentation/comparator.h>
46 
47 namespace pcl
48 {
49  /** \brief PlaneCoefficientComparator is a Comparator that operates on plane coefficients, for use in planar segmentation.
50  * In conjunction with OrganizedConnectedComponentSegmentation, this allows planes to be segmented from organized data.
51  *
52  * \author Alex Trevor
53  */
54  template<typename PointT, typename PointNT>
55  class PlaneCoefficientComparator: public Comparator<PointT>
56  {
57  public:
60 
62  using PointCloudNPtr = typename PointCloudN::Ptr;
64 
65  using Ptr = shared_ptr<PlaneCoefficientComparator<PointT, PointNT> >;
66  using ConstPtr = shared_ptr<const PlaneCoefficientComparator<PointT, PointNT> >;
67 
69 
70  /** \brief Empty constructor for PlaneCoefficientComparator. */
72  : normals_ ()
73  , angular_threshold_ (pcl::deg2rad (2.0f))
74  , distance_threshold_ (0.02f)
75  , depth_dependent_ (true)
76  , z_axis_ (Eigen::Vector3f (0.0, 0.0, 1.0) )
77  {
78  }
79 
80  /** \brief Constructor for PlaneCoefficientComparator.
81  * \param[in] plane_coeff_d a reference to a vector of d coefficients of plane equations. Must be the same size as the input cloud and input normals. a, b, and c coefficients are in the input normals.
82  */
83  PlaneCoefficientComparator (shared_ptr<std::vector<float> >& plane_coeff_d)
84  : normals_ ()
85  , plane_coeff_d_ (plane_coeff_d)
86  , angular_threshold_ (pcl::deg2rad (2.0f))
87  , distance_threshold_ (0.02f)
88  , depth_dependent_ (true)
89  , z_axis_ (Eigen::Vector3f (0.0f, 0.0f, 1.0f) )
90  {
91  }
92 
93  /** \brief Destructor for PlaneCoefficientComparator. */
94 
95  ~PlaneCoefficientComparator () override = default;
96 
97  void
98  setInputCloud (const PointCloudConstPtr& cloud) override
99  {
100  input_ = cloud;
101  }
102 
103  /** \brief Provide a pointer to the input normals.
104  * \param[in] normals the input normal cloud
105  */
106  inline void
108  {
109  normals_ = normals;
110  }
111 
112  /** \brief Get the input normals. */
113  inline PointCloudNConstPtr
115  {
116  return (normals_);
117  }
118 
119  /** \brief Provide a pointer to a vector of the d-coefficient of the planes' hessian normal form. a, b, and c are provided by the normal cloud.
120  * \param[in] plane_coeff_d a pointer to the plane coefficients.
121  */
122  void
123  setPlaneCoeffD (shared_ptr<std::vector<float> >& plane_coeff_d)
124  {
125  plane_coeff_d_ = plane_coeff_d;
126  }
127 
128  /** \brief Provide a pointer to a vector of the d-coefficient of the planes' hessian normal form. a, b, and c are provided by the normal cloud.
129  * \param[in] plane_coeff_d a pointer to the plane coefficients.
130  */
131  void
132  setPlaneCoeffD (std::vector<float>& plane_coeff_d)
133  {
134  plane_coeff_d_ = pcl::make_shared<std::vector<float> >(plane_coeff_d);
135  }
136 
137  /** \brief Get a pointer to the vector of the d-coefficient of the planes' hessian normal form. */
138  const std::vector<float>&
139  getPlaneCoeffD () const
140  {
141  return (*plane_coeff_d_);
142  }
143 
144  /** \brief Set the tolerance in radians for difference in normal direction between neighboring points, to be considered part of the same plane.
145  * \param[in] angular_threshold the tolerance in radians
146  */
147  virtual void
148  setAngularThreshold (float angular_threshold)
149  {
150  angular_threshold_ = std::cos (angular_threshold);
151  }
152 
153  /** \brief Get the angular threshold in radians for difference in normal direction between neighboring points, to be considered part of the same plane. */
154  inline float
156  {
157  return (std::acos (angular_threshold_) );
158  }
159 
160  /** \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.
161  * \param[in] distance_threshold the tolerance in meters (at 1m)
162  * \param[in] depth_dependent whether to scale the threshold based on range from the sensor (default: false)
163  */
164  void
165  setDistanceThreshold (float distance_threshold,
166  bool depth_dependent = false)
167  {
168  distance_threshold_ = distance_threshold;
169  depth_dependent_ = depth_dependent;
170  }
171 
172  /** \brief Get the distance threshold in meters (d component of plane equation) between neighboring points, to be considered part of the same plane. */
173  inline float
175  {
176  return (distance_threshold_);
177  }
178 
179  /** \brief Compare points at two indices by their plane equations. True if the angle between the normals is less than the angular threshold,
180  * and the difference between the d component of the normals is less than distance threshold, else false
181  * \param idx1 The first index for the comparison
182  * \param idx2 The second index for the comparison
183  */
184  bool
185  compare (int idx1, int idx2) const override
186  {
187  float threshold = distance_threshold_;
188  if (depth_dependent_)
189  {
190  Eigen::Vector3f vec = (*input_)[idx1].getVector3fMap ();
191 
192  float z = vec.dot (z_axis_);
193  threshold *= z * z;
194  }
195  return ( (std::fabs ((*plane_coeff_d_)[idx1] - (*plane_coeff_d_)[idx2]) < threshold)
196  && ((*normals_)[idx1].getNormalVector3fMap ().dot ((*normals_)[idx2].getNormalVector3fMap () ) > angular_threshold_ ) );
197  }
198 
199  protected:
201  shared_ptr<std::vector<float> > plane_coeff_d_;
205  Eigen::Vector3f z_axis_;
206 
207  public:
209  };
210 }
Define standard C methods to do angle calculations.
Comparator is the base class for comparators that compare two points given some function.
Definition: comparator.h:55
PointCloudConstPtr input_
Definition: comparator.h:98
shared_ptr< Comparator< PointT > > Ptr
Definition: comparator.h:61
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: comparator.h:59
shared_ptr< const Comparator< PointT > > ConstPtr
Definition: comparator.h:62
PlaneCoefficientComparator is a Comparator that operates on plane coefficients, for use in planar seg...
void setDistanceThreshold(float distance_threshold, bool depth_dependent=false)
Set the tolerance in meters for difference in perpendicular distance (d component of plane equation) ...
virtual void setAngularThreshold(float angular_threshold)
Set the tolerance in radians for difference in normal direction between neighboring points,...
float getAngularThreshold() const
Get the angular threshold in radians for difference in normal direction between neighboring points,...
PointCloudNConstPtr getInputNormals() const
Get the input normals.
shared_ptr< std::vector< float > > plane_coeff_d_
void setInputNormals(const PointCloudNConstPtr &normals)
Provide a pointer to the input normals.
typename PointCloudN::ConstPtr PointCloudNConstPtr
float getDistanceThreshold() const
Get the distance threshold in meters (d component of plane equation) between neighboring points,...
bool compare(int idx1, int idx2) const override
Compare points at two indices by their plane equations.
PlaneCoefficientComparator(shared_ptr< std::vector< float > > &plane_coeff_d)
Constructor for PlaneCoefficientComparator.
const std::vector< float > & getPlaneCoeffD() const
Get a pointer to the vector of the d-coefficient of the planes' hessian normal form.
PlaneCoefficientComparator()
Empty constructor for PlaneCoefficientComparator.
void setPlaneCoeffD(shared_ptr< std::vector< float > > &plane_coeff_d)
Provide a pointer to a vector of the d-coefficient of the planes' hessian normal form.
void setPlaneCoeffD(std::vector< float > &plane_coeff_d)
Provide a pointer to a vector of the d-coefficient of the planes' hessian normal form.
~PlaneCoefficientComparator() override=default
Destructor for PlaneCoefficientComparator.
void setInputCloud(const PointCloudConstPtr &cloud) override
Set the input cloud for the comparator.
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< PointCloud< PointNT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointNT > > ConstPtr
Definition: point_cloud.h:414
float deg2rad(float alpha)
Convert an angle from degrees to radians.
Definition: angles.hpp:67
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
Defines functions, macros and traits for allocating and using memory.
Definition: bfgs.h:10
Defines all the PCL and non-PCL macros used.