Point Cloud Library (PCL)  1.8.1-dev
sac_model_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_PLANE_H_
42 #define PCL_SAMPLE_CONSENSUS_MODEL_PLANE_H_
43 
44 #include <pcl/sample_consensus/sac_model.h>
45 #include <pcl/sample_consensus/model_types.h>
46 
47 namespace pcl
48 {
49 
50  /** \brief Project a point on a planar model given by a set of normalized coefficients
51  * \param[in] p the input point to project
52  * \param[in] model_coefficients the coefficients of the plane (a, b, c, d) that satisfy ax+by+cz+d=0
53  * \param[out] q the resultant projected point
54  */
55  template <typename Point> inline void
56  projectPoint (const Point &p, const Eigen::Vector4f &model_coefficients, Point &q)
57  {
58  // Calculate the distance from the point to the plane
59  Eigen::Vector4f pp (p.x, p.y, p.z, 1);
60  // use normalized coefficients to calculate the scalar projection
61  float distance_to_plane = pp.dot(model_coefficients);
62 
63 
64  //TODO: Why doesn't getVector4Map work here?
65  //Eigen::Vector4f q_e = q.getVector4fMap ();
66  //q_e = pp - model_coefficients * distance_to_plane;
67 
68  Eigen::Vector4f q_e = pp - distance_to_plane * model_coefficients;
69  q.x = q_e[0];
70  q.y = q_e[1];
71  q.z = q_e[2];
72  }
73 
74  /** \brief Get the distance from a point to a plane (signed) defined by ax+by+cz+d=0
75  * \param p a point
76  * \param a the normalized <i>a</i> coefficient of a plane
77  * \param b the normalized <i>b</i> coefficient of a plane
78  * \param c the normalized <i>c</i> coefficient of a plane
79  * \param d the normalized <i>d</i> coefficient of a plane
80  * \ingroup sample_consensus
81  */
82  template <typename Point> inline double
83  pointToPlaneDistanceSigned (const Point &p, double a, double b, double c, double d)
84  {
85  return (a * p.x + b * p.y + c * p.z + d);
86  }
87 
88  /** \brief Get the distance from a point to a plane (signed) defined by ax+by+cz+d=0
89  * \param p a point
90  * \param plane_coefficients the normalized coefficients (a, b, c, d) of a plane
91  * \ingroup sample_consensus
92  */
93  template <typename Point> inline double
94  pointToPlaneDistanceSigned (const Point &p, const Eigen::Vector4f &plane_coefficients)
95  {
96  return ( plane_coefficients[0] * p.x + plane_coefficients[1] * p.y + plane_coefficients[2] * p.z + plane_coefficients[3] );
97  }
98 
99  /** \brief Get the distance from a point to a plane (unsigned) defined by ax+by+cz+d=0
100  * \param p a point
101  * \param a the normalized <i>a</i> coefficient of a plane
102  * \param b the normalized <i>b</i> coefficient of a plane
103  * \param c the normalized <i>c</i> coefficient of a plane
104  * \param d the normalized <i>d</i> coefficient of a plane
105  * \ingroup sample_consensus
106  */
107  template <typename Point> inline double
108  pointToPlaneDistance (const Point &p, double a, double b, double c, double d)
109  {
110  return (fabs (pointToPlaneDistanceSigned (p, a, b, c, d)) );
111  }
112 
113  /** \brief Get the distance from a point to a plane (unsigned) defined by ax+by+cz+d=0
114  * \param p a point
115  * \param plane_coefficients the normalized coefficients (a, b, c, d) of a plane
116  * \ingroup sample_consensus
117  */
118  template <typename Point> inline double
119  pointToPlaneDistance (const Point &p, const Eigen::Vector4f &plane_coefficients)
120  {
121  return ( fabs (pointToPlaneDistanceSigned (p, plane_coefficients)) );
122  }
123 
124  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
125  /** \brief SampleConsensusModelPlane defines a model for 3D plane segmentation.
126  * The model coefficients are defined as:
127  * - \b a : the X coordinate of the plane's normal (normalized)
128  * - \b b : the Y coordinate of the plane's normal (normalized)
129  * - \b c : the Z coordinate of the plane's normal (normalized)
130  * - \b d : the fourth <a href="http://mathworld.wolfram.com/HessianNormalForm.html">Hessian component</a> of the plane's equation
131  *
132  * \author Radu B. Rusu
133  * \ingroup sample_consensus
134  */
135  template <typename PointT>
137  {
138  public:
144 
148 
149  typedef boost::shared_ptr<SampleConsensusModelPlane> Ptr;
150 
151  /** \brief Constructor for base SampleConsensusModelPlane.
152  * \param[in] cloud the input point cloud dataset
153  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
154  */
155  SampleConsensusModelPlane (const PointCloudConstPtr &cloud, bool random = false)
156  : SampleConsensusModel<PointT> (cloud, random)
157  {
158  model_name_ = "SampleConsensusModelPlane";
159  sample_size_ = 3;
160  model_size_ = 4;
161  }
162 
163  /** \brief Constructor for base SampleConsensusModelPlane.
164  * \param[in] cloud the input point cloud dataset
165  * \param[in] indices a vector of point indices to be used from \a cloud
166  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
167  */
169  const std::vector<int> &indices,
170  bool random = false)
171  : SampleConsensusModel<PointT> (cloud, indices, random)
172  {
173  model_name_ = "SampleConsensusModelPlane";
174  sample_size_ = 3;
175  model_size_ = 4;
176  }
177 
178  /** \brief Empty destructor */
180 
181  /** \brief Check whether the given index samples can form a valid plane model, compute the model coefficients from
182  * these samples and store them internally in model_coefficients_. The plane coefficients are:
183  * a, b, c, d (ax+by+cz+d=0)
184  * \param[in] samples the point indices found as possible good candidates for creating a valid model
185  * \param[out] model_coefficients the resultant model coefficients
186  */
187  bool
188  computeModelCoefficients (const std::vector<int> &samples,
189  Eigen::VectorXf &model_coefficients);
190 
191  /** \brief Compute all distances from the cloud data to a given plane model.
192  * \param[in] model_coefficients the coefficients of a plane model that we need to compute distances to
193  * \param[out] distances the resultant estimated distances
194  */
195  void
196  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
197  std::vector<double> &distances);
198 
199  /** \brief Select all the points which respect the given model coefficients as inliers.
200  * \param[in] model_coefficients the coefficients of a plane model that we need to compute distances to
201  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
202  * \param[out] inliers the resultant model inliers
203  */
204  void
205  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
206  const double threshold,
207  std::vector<int> &inliers);
208 
209  /** \brief Count all the points which respect the given model coefficients as inliers.
210  *
211  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
212  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
213  * \return the resultant number of inliers
214  */
215  virtual int
216  countWithinDistance (const Eigen::VectorXf &model_coefficients,
217  const double threshold);
218 
219  /** \brief Recompute the plane coefficients using the given inlier set and return them to the user.
220  * @note: these are the coefficients of the plane model after refinement (e.g. after SVD)
221  * \param[in] inliers the data inliers found as supporting the model
222  * \param[in] model_coefficients the initial guess for the model coefficients
223  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
224  */
225  void
226  optimizeModelCoefficients (const std::vector<int> &inliers,
227  const Eigen::VectorXf &model_coefficients,
228  Eigen::VectorXf &optimized_coefficients);
229 
230  /** \brief Create a new point cloud with inliers projected onto the plane model.
231  * \param[in] inliers the data inliers that we want to project on the plane model
232  * \param[in] model_coefficients the *normalized* coefficients of a plane model
233  * \param[out] projected_points the resultant projected points
234  * \param[in] copy_data_fields set to true if we need to copy the other data fields
235  */
236  void
237  projectPoints (const std::vector<int> &inliers,
238  const Eigen::VectorXf &model_coefficients,
239  PointCloud &projected_points,
240  bool copy_data_fields = true);
241 
242  /** \brief Verify whether a subset of indices verifies the given plane model coefficients.
243  * \param[in] indices the data indices that need to be tested against the plane model
244  * \param[in] model_coefficients the plane model coefficients
245  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
246  */
247  bool
248  doSamplesVerifyModel (const std::set<int> &indices,
249  const Eigen::VectorXf &model_coefficients,
250  const double threshold);
251 
252  /** \brief Return an unique id for this model (SACMODEL_PLANE). */
253  inline pcl::SacModel
254  getModelType () const { return (SACMODEL_PLANE); }
255 
256  protected:
259 
260  private:
261  /** \brief Check if a sample of indices results in a good sample of points
262  * indices.
263  * \param[in] samples the resultant index samples
264  */
265  virtual bool
266  isSampleGood (const std::vector<int> &samples) const;
267  };
268 }
269 
270 #ifdef PCL_NO_PRECOMPILE
271 #include <pcl/sample_consensus/impl/sac_model_plane.hpp>
272 #endif
273 
274 #endif //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_PLANE_H_
double pointToPlaneDistanceSigned(const Point &p, double a, double b, double c, double d)
Get the distance from a point to a plane (signed) defined by ax+by+cz+d=0.
SampleConsensusModelPlane(const PointCloudConstPtr &cloud, const std::vector< int > &indices, bool random=false)
Constructor for base SampleConsensusModelPlane.
boost::shared_ptr< SampleConsensusModelPlane > Ptr
pcl::SacModel getModelType() const
Return an unique id for this model (SACMODEL_PLANE).
unsigned int model_size_
The number of coefficients in the model.
Definition: sac_model.h:575
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, std::vector< int > &inliers)
Select all the points which respect the given model coefficients as inliers.
SampleConsensusModel< PointT >::PointCloud PointCloud
double pointToPlaneDistance(const Point &p, double a, double b, double c, double d)
Get the distance from a point to a plane (unsigned) defined by ax+by+cz+d=0.
SampleConsensusModel represents the base model class.
Definition: sac_model.h:66
std::string model_name_
The model name.
Definition: sac_model.h:534
void projectPoints(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true)
Create a new point cloud with inliers projected onto the plane model.
pcl::PointCloud< PointT >::Ptr PointCloudPtr
Definition: sac_model.h:71
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients)
Check whether the given index samples can form a valid plane model, compute the model coefficients fr...
SampleConsensusModel< PointT >::PointCloudConstPtr PointCloudConstPtr
SampleConsensusModel< PointT >::PointCloudPtr PointCloudPtr
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances)
Compute all distances from the cloud data to a given plane model.
SampleConsensusModelPlane(const PointCloudConstPtr &cloud, bool random=false)
Constructor for base SampleConsensusModelPlane.
SacModel
Definition: model_types.h:48
pcl::PointCloud< PointT >::ConstPtr PointCloudConstPtr
Definition: sac_model.h:70
void optimizeModelCoefficients(const std::vector< int > &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
Recompute the plane coefficients using the given inlier set and return them to the user...
virtual ~SampleConsensusModelPlane()
Empty destructor.
void projectPoint(const Point &p, const Eigen::Vector4f &model_coefficients, Point &q)
Project a point on a planar model given by a set of normalized coefficients.
A point structure representing Euclidean xyz coordinates, and the RGB color.
SampleConsensusModelPlane defines a model for 3D plane segmentation.
bool doSamplesVerifyModel(const std::set< int > &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
Verify whether a subset of indices verifies the given plane model coefficients.
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold)
Count all the points which respect the given model coefficients as inliers.
unsigned int sample_size_
The size of a sample from which the model is computed.
Definition: sac_model.h:572