Point Cloud Library (PCL)  1.7.1
sac_model_parallel_plane.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_PARALLELPLANE_H_
42 #define PCL_SAMPLE_CONSENSUS_MODEL_PARALLELPLANE_H_
43 
44 #include <pcl/sample_consensus/sac_model_plane.h>
45 #include <pcl/common/common.h>
46 
47 namespace pcl
48 {
49  /** \brief @b SampleConsensusModelParallelPlane defines a model for 3D plane segmentation using additional
50  * angular constraints. The plane must be parallel to a user-specified axis
51  * (\ref setAxis) within an user-specified angle threshold (\ref setEpsAngle).
52  *
53  * Code example for a plane model, parallel (within a 15 degrees tolerance) with the Z axis:
54  * \code
55  * SampleConsensusModelParallelPlane<pcl::PointXYZ> model (cloud);
56  * model.setAxis (Eigen::Vector3f (0.0, 0.0, 1.0));
57  * model.setEpsAngle (pcl::deg2rad (15));
58  * \endcode
59  *
60  * \note Please remember that you need to specify an angle > 0 in order to activate the axis-angle constraint!
61  *
62  * \author Radu B. Rusu, Nico Blodow
63  * \ingroup sample_consensus
64  */
65  template <typename PointT>
67  {
68  public:
72 
73  typedef boost::shared_ptr<SampleConsensusModelParallelPlane> Ptr;
74 
75  /** \brief Constructor for base SampleConsensusModelParallelPlane.
76  * \param[in] cloud the input point cloud dataset
77  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
78  */
80  bool random = false)
81  : SampleConsensusModelPlane<PointT> (cloud, random)
82  , axis_ (Eigen::Vector3f::Zero ())
83  , eps_angle_ (0.0)
84  , sin_angle_ (-1.0)
85  {
86  }
87 
88  /** \brief Constructor for base SampleConsensusModelParallelPlane.
89  * \param[in] cloud the input point cloud dataset
90  * \param[in] indices a vector of point indices to be used from \a cloud
91  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
92  */
94  const std::vector<int> &indices,
95  bool random = false)
96  : SampleConsensusModelPlane<PointT> (cloud, indices, random)
97  , axis_ (Eigen::Vector3f::Zero ())
98  , eps_angle_ (0.0)
99  , sin_angle_ (-1.0)
100  {
101  }
102 
103  /** \brief Empty destructor */
105 
106  /** \brief Set the axis along which we need to search for a plane perpendicular to.
107  * \param[in] ax the axis along which we need to search for a plane perpendicular to
108  */
109  inline void
110  setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
111 
112  /** \brief Get the axis along which we need to search for a plane perpendicular to. */
113  inline Eigen::Vector3f
114  getAxis () { return (axis_); }
115 
116  /** \brief Set the angle epsilon (delta) threshold.
117  * \param[in] ea the maximum allowed difference between the plane normal and the given axis.
118  * \note You need to specify an angle > 0 in order to activate the axis-angle constraint!
119  */
120  inline void
121  setEpsAngle (const double ea) { eps_angle_ = ea; sin_angle_ = fabs (sin (ea));}
122 
123  /** \brief Get the angle epsilon (delta) threshold. */
124  inline double
125  getEpsAngle () { return (eps_angle_); }
126 
127  /** \brief Select all the points which respect the given model coefficients as inliers.
128  * \param[in] model_coefficients the coefficients of a plane model that we need to compute distances to
129  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
130  * \param[out] inliers the resultant model inliers
131  */
132  void
133  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
134  const double threshold,
135  std::vector<int> &inliers);
136 
137  /** \brief Count all the points which respect the given model coefficients as inliers.
138  *
139  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
140  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
141  * \return the resultant number of inliers
142  */
143  virtual int
144  countWithinDistance (const Eigen::VectorXf &model_coefficients,
145  const double threshold);
146 
147  /** \brief Compute all distances from the cloud data to a given plane model.
148  * \param[in] model_coefficients the coefficients of a plane model that we need to compute distances to
149  * \param[out] distances the resultant estimated distances
150  */
151  void
152  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
153  std::vector<double> &distances);
154 
155  /** \brief Return an unique id for this model (SACMODEL_PARALLEL_PLANE). */
156  inline pcl::SacModel
157  getModelType () const { return (SACMODEL_PARALLEL_PLANE); }
158 
159  protected:
160  /** \brief Check whether a model is valid given the user constraints.
161  * \param[in] model_coefficients the set of model coefficients
162  */
163  bool
164  isModelValid (const Eigen::VectorXf &model_coefficients);
165 
166  /** \brief The axis along which we need to search for a plane perpendicular to. */
167  Eigen::Vector3f axis_;
168 
169  /** \brief The maximum allowed difference between the plane and the given axis. */
170  double eps_angle_;
171 
172  /** \brief The sine of the angle*/
173  double sin_angle_;
174  };
175 }
176 
177 #ifdef PCL_NO_PRECOMPILE
178 #include <pcl/sample_consensus/impl/sac_model_parallel_plane.hpp>
179 #endif
180 
181 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_PARALLELPLANE_H_