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