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