Point Cloud Library (PCL)  1.11.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
sac_model_plane.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009, 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_IMPL_SAC_MODEL_PLANE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_PLANE_H_
43 
44 #include <pcl/sample_consensus/sac_model_plane.h>
45 #include <pcl/common/centroid.h>
46 #include <pcl/common/eigen.h>
47 #include <pcl/common/concatenate.h>
48 #include <pcl/point_types.h>
49 
50 //////////////////////////////////////////////////////////////////////////
51 template <typename PointT> bool
52 pcl::SampleConsensusModelPlane<PointT>::isSampleGood (const Indices &samples) const
53 {
54  if (samples.size () != sample_size_)
55  {
56  PCL_ERROR ("[pcl::SampleConsensusModelPlane::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
57  return (false);
58  }
59  // Get the values at the two points
60  pcl::Array4fMapConst p0 = input_->points[samples[0]].getArray4fMap ();
61  pcl::Array4fMapConst p1 = input_->points[samples[1]].getArray4fMap ();
62  pcl::Array4fMapConst p2 = input_->points[samples[2]].getArray4fMap ();
63 
64  Eigen::Array4f dy1dy2 = (p1-p0) / (p2-p0);
65 
66  return ( (dy1dy2[0] != dy1dy2[1]) || (dy1dy2[2] != dy1dy2[1]) );
67 }
68 
69 //////////////////////////////////////////////////////////////////////////
70 template <typename PointT> bool
72  const Indices &samples, Eigen::VectorXf &model_coefficients) const
73 {
74  // Need 3 samples
75  if (samples.size () != sample_size_)
76  {
77  PCL_ERROR ("[pcl::SampleConsensusModelPlane::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
78  return (false);
79  }
80 
81  pcl::Array4fMapConst p0 = input_->points[samples[0]].getArray4fMap ();
82  pcl::Array4fMapConst p1 = input_->points[samples[1]].getArray4fMap ();
83  pcl::Array4fMapConst p2 = input_->points[samples[2]].getArray4fMap ();
84 
85  // Compute the segment values (in 3d) between p1 and p0
86  Eigen::Array4f p1p0 = p1 - p0;
87  // Compute the segment values (in 3d) between p2 and p0
88  Eigen::Array4f p2p0 = p2 - p0;
89 
90  // Avoid some crashes by checking for collinearity here
91  Eigen::Array4f dy1dy2 = p1p0 / p2p0;
92  if ( (dy1dy2[0] == dy1dy2[1]) && (dy1dy2[2] == dy1dy2[1]) ) // Check for collinearity
93  {
94  return (false);
95  }
96 
97  // Compute the plane coefficients from the 3 given points in a straightforward manner
98  // calculate the plane normal n = (p2-p1) x (p3-p1) = cross (p2-p1, p3-p1)
99  model_coefficients.resize (model_size_);
100  model_coefficients[0] = p1p0[1] * p2p0[2] - p1p0[2] * p2p0[1];
101  model_coefficients[1] = p1p0[2] * p2p0[0] - p1p0[0] * p2p0[2];
102  model_coefficients[2] = p1p0[0] * p2p0[1] - p1p0[1] * p2p0[0];
103  model_coefficients[3] = 0.0f;
104 
105  // Normalize
106  model_coefficients.normalize ();
107 
108  // ... + d = 0
109  model_coefficients[3] = -1.0f * (model_coefficients.template head<4>().dot (p0.matrix ()));
110 
111  return (true);
112 }
113 
114 //////////////////////////////////////////////////////////////////////////
115 template <typename PointT> void
117  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
118 {
119  // Needs a valid set of model coefficients
120  if (!isModelValid (model_coefficients))
121  {
122  PCL_ERROR ("[pcl::SampleConsensusModelPlane::getDistancesToModel] Given model is invalid!\n");
123  return;
124  }
125 
126  distances.resize (indices_->size ());
127 
128  // Iterate through the 3d points and calculate the distances from them to the plane
129  for (std::size_t i = 0; i < indices_->size (); ++i)
130  {
131  // Calculate the distance from the point to the plane normal as the dot product
132  // D = (P-A).N/|N|
133  /*distances[i] = std::abs (model_coefficients[0] * input_->points[(*indices_)[i]].x +
134  model_coefficients[1] * input_->points[(*indices_)[i]].y +
135  model_coefficients[2] * input_->points[(*indices_)[i]].z +
136  model_coefficients[3]);*/
137  Eigen::Vector4f pt (input_->points[(*indices_)[i]].x,
138  input_->points[(*indices_)[i]].y,
139  input_->points[(*indices_)[i]].z,
140  1.0f);
141  distances[i] = std::abs (model_coefficients.dot (pt));
142  }
143 }
144 
145 //////////////////////////////////////////////////////////////////////////
146 template <typename PointT> void
148  const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
149 {
150  // Needs a valid set of model coefficients
151  if (!isModelValid (model_coefficients))
152  {
153  PCL_ERROR ("[pcl::SampleConsensusModelPlane::selectWithinDistance] Given model is invalid!\n");
154  return;
155  }
156 
157  inliers.clear ();
158  error_sqr_dists_.clear ();
159  inliers.reserve (indices_->size ());
160  error_sqr_dists_.reserve (indices_->size ());
161 
162  // Iterate through the 3d points and calculate the distances from them to the plane
163  for (std::size_t i = 0; i < indices_->size (); ++i)
164  {
165  // Calculate the distance from the point to the plane normal as the dot product
166  // D = (P-A).N/|N|
167  Eigen::Vector4f pt (input_->points[(*indices_)[i]].x,
168  input_->points[(*indices_)[i]].y,
169  input_->points[(*indices_)[i]].z,
170  1.0f);
171 
172  float distance = std::abs (model_coefficients.dot (pt));
173 
174  if (distance < threshold)
175  {
176  // Returns the indices of the points whose distances are smaller than the threshold
177  inliers.push_back ((*indices_)[i]);
178  error_sqr_dists_.push_back (static_cast<double> (distance));
179  }
180  }
181 }
182 
183 //////////////////////////////////////////////////////////////////////////
184 template <typename PointT> std::size_t
186  const Eigen::VectorXf &model_coefficients, const double threshold) const
187 {
188  // Needs a valid set of model coefficients
189  if (!isModelValid (model_coefficients))
190  {
191  PCL_ERROR ("[pcl::SampleConsensusModelPlane::countWithinDistance] Given model is invalid!\n");
192  return (0);
193  }
194 
195  std::size_t nr_p = 0;
196 
197  // Iterate through the 3d points and calculate the distances from them to the plane
198  for (std::size_t i = 0; i < indices_->size (); ++i)
199  {
200  // Calculate the distance from the point to the plane normal as the dot product
201  // D = (P-A).N/|N|
202  Eigen::Vector4f pt (input_->points[(*indices_)[i]].x,
203  input_->points[(*indices_)[i]].y,
204  input_->points[(*indices_)[i]].z,
205  1.0f);
206  if (std::abs (model_coefficients.dot (pt)) < threshold)
207  {
208  nr_p++;
209  }
210  }
211  return (nr_p);
212 }
213 
214 //////////////////////////////////////////////////////////////////////////
215 template <typename PointT> void
217  const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
218 {
219  // Needs a valid set of model coefficients
220  if (!isModelValid (model_coefficients))
221  {
222  PCL_ERROR ("[pcl::SampleConsensusModelPlane::optimizeModelCoefficients] Given model is invalid!\n");
223  optimized_coefficients = model_coefficients;
224  return;
225  }
226 
227  // Need more than the minimum sample size to make a difference
228  if (inliers.size () <= sample_size_)
229  {
230  PCL_ERROR ("[pcl::SampleConsensusModelPlane::optimizeModelCoefficients] Not enough inliers found to optimize model coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
231  optimized_coefficients = model_coefficients;
232  return;
233  }
234 
235  Eigen::Vector4f plane_parameters;
236 
237  // Use Least-Squares to fit the plane through all the given sample points and find out its coefficients
238  EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
239  Eigen::Vector4f xyz_centroid;
240 
241  computeMeanAndCovarianceMatrix (*input_, inliers, covariance_matrix, xyz_centroid);
242 
243  // Compute the model coefficients
244  EIGEN_ALIGN16 Eigen::Vector3f::Scalar eigen_value;
245  EIGEN_ALIGN16 Eigen::Vector3f eigen_vector;
246  pcl::eigen33 (covariance_matrix, eigen_value, eigen_vector);
247 
248  // Hessian form (D = nc . p_plane (centroid here) + p)
249  optimized_coefficients.resize (model_size_);
250  optimized_coefficients[0] = eigen_vector [0];
251  optimized_coefficients[1] = eigen_vector [1];
252  optimized_coefficients[2] = eigen_vector [2];
253  optimized_coefficients[3] = 0.0f;
254  optimized_coefficients[3] = -1.0f * optimized_coefficients.dot (xyz_centroid);
255 
256  // Make sure it results in a valid model
257  if (!isModelValid (optimized_coefficients))
258  {
259  optimized_coefficients = model_coefficients;
260  }
261 }
262 
263 //////////////////////////////////////////////////////////////////////////
264 template <typename PointT> void
266  const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields) const
267 {
268  // Needs a valid set of model coefficients
269  if (!isModelValid (model_coefficients))
270  {
271  PCL_ERROR ("[pcl::SampleConsensusModelPlane::projectPoints] Given model is invalid!\n");
272  return;
273  }
274 
275  projected_points.header = input_->header;
276  projected_points.is_dense = input_->is_dense;
277 
278  Eigen::Vector4f mc (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
279 
280  // normalize the vector perpendicular to the plane...
281  mc.normalize ();
282  // ... and store the resulting normal as a local copy of the model coefficients
283  Eigen::Vector4f tmp_mc = model_coefficients;
284  tmp_mc[0] = mc[0];
285  tmp_mc[1] = mc[1];
286  tmp_mc[2] = mc[2];
287 
288  // Copy all the data fields from the input cloud to the projected one?
289  if (copy_data_fields)
290  {
291  // Allocate enough space and copy the basics
292  projected_points.points.resize (input_->points.size ());
293  projected_points.width = input_->width;
294  projected_points.height = input_->height;
295 
296  using FieldList = typename pcl::traits::fieldList<PointT>::type;
297  // Iterate over each point
298  for (std::size_t i = 0; i < input_->points.size (); ++i)
299  // Iterate over each dimension
300  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[i], projected_points.points[i]));
301 
302  // Iterate through the 3d points and calculate the distances from them to the plane
303  for (const auto &inlier : inliers)
304  {
305  // Calculate the distance from the point to the plane
306  Eigen::Vector4f p (input_->points[inlier].x,
307  input_->points[inlier].y,
308  input_->points[inlier].z,
309  1);
310  // use normalized coefficients to calculate the scalar projection
311  float distance_to_plane = tmp_mc.dot (p);
312 
313  pcl::Vector4fMap pp = projected_points.points[inlier].getVector4fMap ();
314  pp.matrix () = p - mc * distance_to_plane; // mc[3] = 0, therefore the 3rd coordinate is safe
315  }
316  }
317  else
318  {
319  // Allocate enough space and copy the basics
320  projected_points.points.resize (inliers.size ());
321  projected_points.width = static_cast<std::uint32_t> (inliers.size ());
322  projected_points.height = 1;
323 
324  using FieldList = typename pcl::traits::fieldList<PointT>::type;
325  // Iterate over each point
326  for (std::size_t i = 0; i < inliers.size (); ++i)
327  {
328  // Iterate over each dimension
329  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[inliers[i]], projected_points.points[i]));
330  }
331 
332  // Iterate through the 3d points and calculate the distances from them to the plane
333  for (std::size_t i = 0; i < inliers.size (); ++i)
334  {
335  // Calculate the distance from the point to the plane
336  Eigen::Vector4f p (input_->points[inliers[i]].x,
337  input_->points[inliers[i]].y,
338  input_->points[inliers[i]].z,
339  1.0f);
340  // use normalized coefficients to calculate the scalar projection
341  float distance_to_plane = tmp_mc.dot (p);
342 
343  pcl::Vector4fMap pp = projected_points.points[i].getVector4fMap ();
344  pp.matrix () = p - mc * distance_to_plane; // mc[3] = 0, therefore the 3rd coordinate is safe
345  }
346  }
347 }
348 
349 //////////////////////////////////////////////////////////////////////////
350 template <typename PointT> bool
352  const std::set<index_t> &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
353 {
354  // Needs a valid set of model coefficients
355  if (!isModelValid (model_coefficients))
356  {
357  PCL_ERROR ("[pcl::SampleConsensusModelPlane::doSamplesVerifyModel] Given model is invalid!\n");
358  return (false);
359  }
360 
361  for (const auto &index : indices)
362  {
363  Eigen::Vector4f pt (input_->points[index].x,
364  input_->points[index].y,
365  input_->points[index].z,
366  1.0f);
367  if (std::abs (model_coefficients.dot (pt)) > threshold)
368  {
369  return (false);
370  }
371  }
372 
373  return (true);
374 }
375 
376 #define PCL_INSTANTIATE_SampleConsensusModelPlane(T) template class PCL_EXPORTS pcl::SampleConsensusModelPlane<T>;
377 
378 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_PLANE_H_
379 
unsigned int computeMeanAndCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the normalized 3x3 covariance matrix and the centroid of a given set of points in a single lo...
Definition: centroid.hpp:489
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid plane model, compute the model coefficients fr...
bool doSamplesVerifyModel(const std::set< index_t > &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const override
Verify whether a subset of indices verifies the given plane model coefficients.
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:413
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the plane coefficients using the given inlier set and return them to the user...
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const override
Count all the points which respect the given model coefficients as inliers.
void eigen33(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the eigenvector and eigenvalue of the smallest eigenvalue of the symmetric positive semi d...
Definition: eigen.hpp:296
PointCloud represents the base class in PCL for storing collections of 3D points. ...
const Eigen::Map< const Eigen::Array4f, Eigen::Aligned > Array4fMapConst
void projectPoints(const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields=true) const override
Create a new point cloud with inliers projected onto the plane model.
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers) override
Select all the points which respect the given model coefficients as inliers.
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:415
Helper functor structure for concatenate.
Definition: concatenate.h:51
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
Definition: point_cloud.h:418
SampleConsensusModelPlane defines a model for 3D plane segmentation.
Eigen::Map< Eigen::Vector4f, Eigen::Aligned > Vector4fMap
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all distances from the cloud data to a given plane model.
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:407