Point Cloud Library (PCL)  1.14.1-dev
ground_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  *
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/memory.h>
43 #include <pcl/pcl_macros.h>
44 #include <pcl/common/angles.h>
45 #include <pcl/segmentation/comparator.h>
46 
47 
48 namespace pcl
49 {
50  /** \brief GroundPlaneComparator is a Comparator for detecting smooth surfaces suitable for driving.
51  * In conjunction with OrganizedConnectedComponentSegmentation, this allows smooth groundplanes / road surfaces to be segmented from point clouds.
52  *
53  * \author Alex Trevor
54  */
55  template<typename PointT, typename PointNT>
56  class GroundPlaneComparator: public Comparator<PointT>
57  {
58  public:
61 
63  using PointCloudNPtr = typename PointCloudN::Ptr;
65 
66  using Ptr = shared_ptr<GroundPlaneComparator<PointT, PointNT> >;
67  using ConstPtr = shared_ptr<const GroundPlaneComparator<PointT, PointNT> >;
68 
70 
71  /** \brief Empty constructor for GroundPlaneComparator. */
73  : normals_ ()
74  , angular_threshold_ (std::cos (pcl::deg2rad (2.0f)))
75  , road_angular_threshold_ ( std::cos(pcl::deg2rad (10.0f)))
76  , distance_threshold_ (0.1f)
77  , depth_dependent_ (true)
78  , z_axis_ (Eigen::Vector3f (0.0, 0.0, 1.0) )
79  , desired_road_axis_ (Eigen::Vector3f(0.0, -1.0, 0.0))
80  {
81  }
82 
83  /** \brief Constructor for GroundPlaneComparator.
84  * \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.
85  */
86  GroundPlaneComparator (shared_ptr<std::vector<float> >& plane_coeff_d)
87  : normals_ ()
88  , plane_coeff_d_ (plane_coeff_d)
89  , angular_threshold_ (std::cos (pcl::deg2rad (3.0f)))
90  , distance_threshold_ (0.1f)
91  , depth_dependent_ (true)
92  , z_axis_ (Eigen::Vector3f (0.0f, 0.0f, 1.0f))
93  , road_angular_threshold_ ( std::cos(pcl::deg2rad (40.0f)))
94  , desired_road_axis_ (Eigen::Vector3f(0.0, -1.0, 0.0))
95  {
96  }
97 
98  /** \brief Destructor for GroundPlaneComparator. */
99 
101  = default;
102  /** \brief Provide the input cloud.
103  * \param[in] cloud the input point cloud.
104  */
105  void
106  setInputCloud (const PointCloudConstPtr& cloud) override
107  {
108  input_ = cloud;
109  }
110 
111  /** \brief Provide a pointer to the input normals.
112  * \param[in] normals the input normal cloud.
113  */
114  inline void
116  {
117  normals_ = normals;
118  }
119 
120  /** \brief Get the input normals. */
121  inline PointCloudNConstPtr
123  {
124  return (normals_);
125  }
126 
127  /** \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.
128  * \param[in] plane_coeff_d a pointer to the plane coefficients.
129  */
130  void
131  setPlaneCoeffD (shared_ptr<std::vector<float> >& plane_coeff_d)
132  {
133  plane_coeff_d_ = plane_coeff_d;
134  }
135 
136  /** \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.
137  * \param[in] plane_coeff_d a pointer to the plane coefficients.
138  */
139  void
140  setPlaneCoeffD (std::vector<float>& plane_coeff_d)
141  {
142  plane_coeff_d_ = pcl::make_shared<std::vector<float> >(plane_coeff_d);
143  }
144 
145  /** \brief Get a pointer to the vector of the d-coefficient of the planes' hessian normal form. */
146  const std::vector<float>&
147  getPlaneCoeffD () const
148  {
149  return (*plane_coeff_d_);
150  }
151 
152  /** \brief Set the tolerance in radians for difference in normal direction between neighboring points, to be considered part of the same plane.
153  * \param[in] angular_threshold the tolerance in radians
154  */
155  virtual void
156  setAngularThreshold (float angular_threshold)
157  {
158  angular_threshold_ = std::cos (angular_threshold);
159  }
160 
161  /** \brief Set the tolerance in radians for difference in normal direction between a point and the expected ground normal.
162  * \param[in] angular_threshold the
163  */
164  virtual void
165  setGroundAngularThreshold (float angular_threshold)
166  {
167  road_angular_threshold_ = std::cos (angular_threshold);
168  }
169 
170  /** \brief Set the expected ground plane normal with respect to the sensor. Pixels labeled as ground must be within ground_angular_threshold radians of this normal to be labeled as ground.
171  * \param[in] normal The normal direction of the expected ground plane.
172  */
173  void
174  setExpectedGroundNormal (Eigen::Vector3f normal)
175  {
176  desired_road_axis_ = normal;
177  }
178 
179 
180  /** \brief Get the angular threshold in radians for difference in normal direction between neighboring points, to be considered part of the same plane. */
181  inline float
183  {
184  return (std::acos (angular_threshold_) );
185  }
186 
187  /** \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.
188  * \param[in] distance_threshold the tolerance in meters (at 1m)
189  * \param[in] depth_dependent whether to scale the threshold based on range from the sensor (default: false)
190  */
191  void
192  setDistanceThreshold (float distance_threshold,
193  bool depth_dependent = false)
194  {
195  distance_threshold_ = distance_threshold;
196  depth_dependent_ = depth_dependent;
197  }
198 
199  /** \brief Get the distance threshold in meters (d component of plane equation) between neighboring points, to be considered part of the same plane. */
200  inline float
202  {
203  return distance_threshold_;
204  }
205 
206  /** \brief Compare points at two indices by their plane equations. True if the angle between the normals is less than the angular threshold,
207  * and the difference between the d component of the normals is less than distance threshold, else false
208  * \param idx1 The first index for the comparison
209  * \param idx2 The second index for the comparison
210  */
211  bool
212  compare (int idx1, int idx2) const override
213  {
214  // Normal must be similar to neighbor
215  // Normal must be similar to expected normal
216  // TODO check logic in this class: which member variables are needed?
217  // float threshold = distance_threshold_;
218  // if (depth_dependent_)
219  // {
220  // Eigen::Vector3f vec = (*input_)[idx1].getVector3fMap ();
221 
222  // float z = vec.dot (z_axis_);
223  // threshold *= z * z;
224  // }
225 
226  return ( ((*normals_)[idx1].getNormalVector3fMap ().dot (desired_road_axis_) > road_angular_threshold_) &&
227  ((*normals_)[idx1].getNormalVector3fMap ().dot ((*normals_)[idx2].getNormalVector3fMap () ) > angular_threshold_ ));
228 
229  // Euclidean proximity of neighbors does not seem to be required -- pixel adjacency handles this well enough
230  //return ( ((*normals_)[idx1].getNormalVector3fMap ().dot (desired_road_axis_) > road_angular_threshold_) &&
231  // ((*normals_)[idx1].getNormalVector3fMap ().dot ((*normals_)[idx2].getNormalVector3fMap () ) > angular_threshold_ ) &&
232  // (pcl::euclideanDistance ((*input_)[idx1], (*input_)[idx2]) < distance_threshold_ ));
233  }
234 
235  protected:
237  shared_ptr<std::vector<float> > plane_coeff_d_;
242  Eigen::Vector3f z_axis_;
243  Eigen::Vector3f desired_road_axis_;
244 
245  public:
247  };
248 }
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
GroundPlaneComparator is a Comparator for detecting smooth surfaces suitable for driving.
void setExpectedGroundNormal(Eigen::Vector3f normal)
Set the expected ground plane normal with respect to the sensor.
GroundPlaneComparator(shared_ptr< std::vector< float > > &plane_coeff_d)
Constructor for GroundPlaneComparator.
typename PointCloudN::Ptr PointCloudNPtr
void setInputNormals(const PointCloudNConstPtr &normals)
Provide a pointer to the input normals.
void setDistanceThreshold(float distance_threshold, bool depth_dependent=false)
Set the tolerance in meters for difference in perpendicular distance (d component of plane equation) ...
PointCloudNConstPtr getInputNormals() const
Get the input normals.
const std::vector< float > & getPlaneCoeffD() const
Get a pointer to the vector of the d-coefficient of the planes' hessian normal form.
float getDistanceThreshold() const
Get the distance threshold in meters (d component of plane equation) between neighboring points,...
typename PointCloudN::ConstPtr PointCloudNConstPtr
bool compare(int idx1, int idx2) const override
Compare points at two indices by their plane equations.
void setInputCloud(const PointCloudConstPtr &cloud) override
Provide the input cloud.
void setPlaneCoeffD(std::vector< float > &plane_coeff_d)
Provide a pointer to a vector of the d-coefficient of the planes' hessian normal form.
virtual void setGroundAngularThreshold(float angular_threshold)
Set the tolerance in radians for difference in normal direction between a point and the expected grou...
GroundPlaneComparator()
Empty constructor for GroundPlaneComparator.
float getAngularThreshold() const
Get the angular threshold in radians for difference in normal direction between neighboring points,...
~GroundPlaneComparator() override=default
Destructor for GroundPlaneComparator.
shared_ptr< std::vector< float > > plane_coeff_d_
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.
virtual void setAngularThreshold(float angular_threshold)
Set the tolerance in radians for difference in normal direction between neighboring points,...
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.