Point Cloud Library (PCL)  1.7.1
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:
142 
146 
147  typedef boost::shared_ptr<SampleConsensusModelPlane> Ptr;
148 
149  /** \brief Constructor for base SampleConsensusModelPlane.
150  * \param[in] cloud the input point cloud dataset
151  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
152  */
153  SampleConsensusModelPlane (const PointCloudConstPtr &cloud, bool random = false)
154  : SampleConsensusModel<PointT> (cloud, random) {};
155 
156  /** \brief Constructor for base SampleConsensusModelPlane.
157  * \param[in] cloud the input point cloud dataset
158  * \param[in] indices a vector of point indices to be used from \a cloud
159  * \param[in] random if true set the random seed to the current time, else set to 12345 (default: false)
160  */
162  const std::vector<int> &indices,
163  bool random = false)
164  : SampleConsensusModel<PointT> (cloud, indices, random) {};
165 
166  /** \brief Empty destructor */
168 
169  /** \brief Check whether the given index samples can form a valid plane model, compute the model coefficients from
170  * these samples and store them internally in model_coefficients_. The plane coefficients are:
171  * a, b, c, d (ax+by+cz+d=0)
172  * \param[in] samples the point indices found as possible good candidates for creating a valid model
173  * \param[out] model_coefficients the resultant model coefficients
174  */
175  bool
176  computeModelCoefficients (const std::vector<int> &samples,
177  Eigen::VectorXf &model_coefficients);
178 
179  /** \brief Compute all distances from the cloud data to a given plane model.
180  * \param[in] model_coefficients the coefficients of a plane model that we need to compute distances to
181  * \param[out] distances the resultant estimated distances
182  */
183  void
184  getDistancesToModel (const Eigen::VectorXf &model_coefficients,
185  std::vector<double> &distances);
186 
187  /** \brief Select all the points which respect the given model coefficients as inliers.
188  * \param[in] model_coefficients the coefficients of a plane model that we need to compute distances to
189  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
190  * \param[out] inliers the resultant model inliers
191  */
192  void
193  selectWithinDistance (const Eigen::VectorXf &model_coefficients,
194  const double threshold,
195  std::vector<int> &inliers);
196 
197  /** \brief Count all the points which respect the given model coefficients as inliers.
198  *
199  * \param[in] model_coefficients the coefficients of a model that we need to compute distances to
200  * \param[in] threshold maximum admissible distance threshold for determining the inliers from the outliers
201  * \return the resultant number of inliers
202  */
203  virtual int
204  countWithinDistance (const Eigen::VectorXf &model_coefficients,
205  const double threshold);
206 
207  /** \brief Recompute the plane coefficients using the given inlier set and return them to the user.
208  * @note: these are the coefficients of the plane model after refinement (eg. after SVD)
209  * \param[in] inliers the data inliers found as supporting the model
210  * \param[in] model_coefficients the initial guess for the model coefficients
211  * \param[out] optimized_coefficients the resultant recomputed coefficients after non-linear optimization
212  */
213  void
214  optimizeModelCoefficients (const std::vector<int> &inliers,
215  const Eigen::VectorXf &model_coefficients,
216  Eigen::VectorXf &optimized_coefficients);
217 
218  /** \brief Create a new point cloud with inliers projected onto the plane model.
219  * \param[in] inliers the data inliers that we want to project on the plane model
220  * \param[in] model_coefficients the *normalized* coefficients of a plane model
221  * \param[out] projected_points the resultant projected points
222  * \param[in] copy_data_fields set to true if we need to copy the other data fields
223  */
224  void
225  projectPoints (const std::vector<int> &inliers,
226  const Eigen::VectorXf &model_coefficients,
227  PointCloud &projected_points,
228  bool copy_data_fields = true);
229 
230  /** \brief Verify whether a subset of indices verifies the given plane model coefficients.
231  * \param[in] indices the data indices that need to be tested against the plane model
232  * \param[in] model_coefficients the plane model coefficients
233  * \param[in] threshold a maximum admissible distance threshold for determining the inliers from the outliers
234  */
235  bool
236  doSamplesVerifyModel (const std::set<int> &indices,
237  const Eigen::VectorXf &model_coefficients,
238  const double threshold);
239 
240  /** \brief Return an unique id for this model (SACMODEL_PLANE). */
241  inline pcl::SacModel
242  getModelType () const { return (SACMODEL_PLANE); }
243 
244  protected:
245  /** \brief Check whether a model is valid given the user constraints.
246  * \param[in] model_coefficients the set of model coefficients
247  */
248  inline bool
249  isModelValid (const Eigen::VectorXf &model_coefficients)
250  {
251  // Needs a valid model coefficients
252  if (model_coefficients.size () != 4)
253  {
254  PCL_ERROR ("[pcl::SampleConsensusModelPlane::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
255  return (false);
256  }
257  return (true);
258  }
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_