Point Cloud Library (PCL)  1.12.1-dev
sac_model_line.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_LINE_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_LINE_H_
43 
44 #include <pcl/sample_consensus/sac_model_line.h>
45 #include <pcl/common/centroid.h>
46 #include <pcl/common/concatenate.h>
47 #include <pcl/common/eigen.h> // for eigen33
48 
49 //////////////////////////////////////////////////////////////////////////
50 template <typename PointT> bool
52 {
53  if (samples.size () != sample_size_)
54  {
55  PCL_ERROR ("[pcl::SampleConsensusModelLine::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
56  return (false);
57  }
58 
59  // Make sure that the two sample points are not identical
60  if (
61  std::abs ((*input_)[samples[0]].x - (*input_)[samples[1]].x) <= std::numeric_limits<float>::epsilon ()
62  &&
63  std::abs ((*input_)[samples[0]].y - (*input_)[samples[1]].y) <= std::numeric_limits<float>::epsilon ()
64  &&
65  std::abs ((*input_)[samples[0]].z - (*input_)[samples[1]].z) <= std::numeric_limits<float>::epsilon ())
66  {
67  PCL_ERROR ("[pcl::SampleConsensusModelLine::isSampleGood] The two sample points are (almost) identical!\n");
68  return (false);
69  }
70 
71  return (true);
72 }
73 
74 //////////////////////////////////////////////////////////////////////////
75 template <typename PointT> bool
77  const Indices &samples, Eigen::VectorXf &model_coefficients) const
78 {
79  // Make sure that the samples are valid
80  if (!isSampleGood (samples))
81  {
82  PCL_ERROR ("[pcl::SampleConsensusModelLine::computeModelCoefficients] Invalid set of samples given!\n");
83  return (false);
84  }
85 
86  model_coefficients.resize (model_size_);
87  model_coefficients[0] = (*input_)[samples[0]].x;
88  model_coefficients[1] = (*input_)[samples[0]].y;
89  model_coefficients[2] = (*input_)[samples[0]].z;
90 
91  model_coefficients[3] = (*input_)[samples[1]].x - model_coefficients[0];
92  model_coefficients[4] = (*input_)[samples[1]].y - model_coefficients[1];
93  model_coefficients[5] = (*input_)[samples[1]].z - model_coefficients[2];
94 
95  // This precondition should hold if the samples have been found to be good
96  assert (model_coefficients.template tail<3> ().squaredNorm () > 0.0f);
97 
98  model_coefficients.template tail<3> ().normalize ();
99  PCL_DEBUG ("[pcl::SampleConsensusModelLine::computeModelCoefficients] Model is (%g,%g,%g,%g,%g,%g).\n",
100  model_coefficients[0], model_coefficients[1], model_coefficients[2],
101  model_coefficients[3], model_coefficients[4], model_coefficients[5]);
102  return (true);
103 }
104 
105 //////////////////////////////////////////////////////////////////////////
106 template <typename PointT> void
108  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
109 {
110  // Needs a valid set of model coefficients
111  if (!isModelValid (model_coefficients))
112  {
113  return;
114  }
115 
116  distances.resize (indices_->size ());
117 
118  // Obtain the line point and direction
119  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
120  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
121  line_dir.normalize ();
122 
123  // Iterate through the 3d points and calculate the distances from them to the line
124  for (std::size_t i = 0; i < indices_->size (); ++i)
125  {
126  // Calculate the distance from the point to the line
127  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
128  // Need to estimate sqrt here to keep MSAC and friends general
129  distances[i] = sqrt ((line_pt - (*input_)[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ());
130  }
131 }
132 
133 //////////////////////////////////////////////////////////////////////////
134 template <typename PointT> void
136  const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
137 {
138  // Needs a valid set of model coefficients
139  if (!isModelValid (model_coefficients))
140  return;
141 
142  double sqr_threshold = threshold * threshold;
143 
144  inliers.clear ();
145  error_sqr_dists_.clear ();
146  inliers.reserve (indices_->size ());
147  error_sqr_dists_.reserve (indices_->size ());
148 
149  // Obtain the line point and direction
150  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
151  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
152  line_dir.normalize ();
153 
154  // Iterate through the 3d points and calculate the distances from them to the line
155  for (std::size_t i = 0; i < indices_->size (); ++i)
156  {
157  // Calculate the distance from the point to the line
158  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
159  double sqr_distance = (line_pt - (*input_)[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ();
160 
161  if (sqr_distance < sqr_threshold)
162  {
163  // Returns the indices of the points whose squared distances are smaller than the threshold
164  inliers.push_back ((*indices_)[i]);
165  error_sqr_dists_.push_back (sqr_distance);
166  }
167  }
168 }
169 
170 //////////////////////////////////////////////////////////////////////////
171 template <typename PointT> std::size_t
173  const Eigen::VectorXf &model_coefficients, const double threshold) const
174 {
175  // Needs a valid set of model coefficients
176  if (!isModelValid (model_coefficients))
177  return (0);
178 
179  double sqr_threshold = threshold * threshold;
180 
181  std::size_t nr_p = 0;
182 
183  // Obtain the line point and direction
184  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
185  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
186  line_dir.normalize ();
187 
188  // Iterate through the 3d points and calculate the distances from them to the line
189  for (std::size_t i = 0; i < indices_->size (); ++i)
190  {
191  // Calculate the distance from the point to the line
192  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
193  double sqr_distance = (line_pt - (*input_)[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ();
194 
195  if (sqr_distance < sqr_threshold)
196  nr_p++;
197  }
198  return (nr_p);
199 }
200 
201 //////////////////////////////////////////////////////////////////////////
202 template <typename PointT> void
204  const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
205 {
206  // Needs a valid set of model coefficients
207  if (!isModelValid (model_coefficients))
208  {
209  optimized_coefficients = model_coefficients;
210  return;
211  }
212 
213  // Need more than the minimum sample size to make a difference
214  if (inliers.size () <= sample_size_)
215  {
216  PCL_ERROR ("[pcl::SampleConsensusModelLine::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
217  optimized_coefficients = model_coefficients;
218  return;
219  }
220 
221  optimized_coefficients.resize (model_size_);
222 
223  // Compute the 3x3 covariance matrix
224  Eigen::Vector4f centroid;
225  if (0 == compute3DCentroid (*input_, inliers, centroid))
226  {
227  PCL_WARN ("[pcl::SampleConsensusModelLine::optimizeModelCoefficients] compute3DCentroid failed (returned 0) because there are no valid inliers.\n");
228  optimized_coefficients = model_coefficients;
229  return;
230  }
231  Eigen::Matrix3f covariance_matrix;
232  computeCovarianceMatrix (*input_, inliers, centroid, covariance_matrix);
233  optimized_coefficients[0] = centroid[0];
234  optimized_coefficients[1] = centroid[1];
235  optimized_coefficients[2] = centroid[2];
236 
237  // Extract the eigenvalues and eigenvectors
238  EIGEN_ALIGN16 Eigen::Vector3f eigen_values;
239  EIGEN_ALIGN16 Eigen::Vector3f eigen_vector;
240  pcl::eigen33 (covariance_matrix, eigen_values);
241  pcl::computeCorrespondingEigenVector (covariance_matrix, eigen_values [2], eigen_vector);
242  //pcl::eigen33 (covariance_matrix, eigen_vectors, eigen_values);
243 
244  optimized_coefficients.template tail<3> ().matrix () = eigen_vector;
245 }
246 
247 //////////////////////////////////////////////////////////////////////////
248 template <typename PointT> void
250  const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields) const
251 {
252  // Needs a valid model coefficients
253  if (!isModelValid (model_coefficients))
254  return;
255 
256  // Obtain the line point and direction
257  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
258  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
259 
260  projected_points.header = input_->header;
261  projected_points.is_dense = input_->is_dense;
262 
263  // Copy all the data fields from the input cloud to the projected one?
264  if (copy_data_fields)
265  {
266  // Allocate enough space and copy the basics
267  projected_points.resize (input_->size ());
268  projected_points.width = input_->width;
269  projected_points.height = input_->height;
270 
271  using FieldList = typename pcl::traits::fieldList<PointT>::type;
272  // Iterate over each point
273  for (std::size_t i = 0; i < projected_points.size (); ++i)
274  // Iterate over each dimension
275  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[i], projected_points[i]));
276 
277  // Iterate through the 3d points and calculate the distances from them to the line
278  for (const auto &inlier : inliers)
279  {
280  Eigen::Vector4f pt ((*input_)[inlier].x, (*input_)[inlier].y, (*input_)[inlier].z, 0.0f);
281  // double k = (DOT_PROD_3D (points[i], p21) - dotA_B) / dotB_B;
282  float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
283 
284  Eigen::Vector4f pp = line_pt + k * line_dir;
285  // Calculate the projection of the point on the line (pointProj = A + k * B)
286  projected_points[inlier].x = pp[0];
287  projected_points[inlier].y = pp[1];
288  projected_points[inlier].z = pp[2];
289  }
290  }
291  else
292  {
293  // Allocate enough space and copy the basics
294  projected_points.resize (inliers.size ());
295  projected_points.width = inliers.size ();
296  projected_points.height = 1;
297 
298  using FieldList = typename pcl::traits::fieldList<PointT>::type;
299  // Iterate over each point
300  for (std::size_t i = 0; i < inliers.size (); ++i)
301  // Iterate over each dimension
302  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[inliers[i]], projected_points[i]));
303 
304  // Iterate through the 3d points and calculate the distances from them to the line
305  for (std::size_t i = 0; i < inliers.size (); ++i)
306  {
307  Eigen::Vector4f pt ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z, 0.0f);
308  // double k = (DOT_PROD_3D (points[i], p21) - dotA_B) / dotB_B;
309  float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
310 
311  Eigen::Vector4f pp = line_pt + k * line_dir;
312  // Calculate the projection of the point on the line (pointProj = A + k * B)
313  projected_points[i].x = pp[0];
314  projected_points[i].y = pp[1];
315  projected_points[i].z = pp[2];
316  }
317  }
318 }
319 
320 //////////////////////////////////////////////////////////////////////////
321 template <typename PointT> bool
323  const std::set<index_t> &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
324 {
325  // Needs a valid set of model coefficients
326  if (!isModelValid (model_coefficients))
327  return (false);
328 
329  // Obtain the line point and direction
330  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
331  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
332  line_dir.normalize ();
333 
334  double sqr_threshold = threshold * threshold;
335  // Iterate through the 3d points and calculate the distances from them to the line
336  for (const auto &index : indices)
337  {
338  // Calculate the distance from the point to the line
339  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
340  if ((line_pt - (*input_)[index].getVector4fMap ()).cross3 (line_dir).squaredNorm () > sqr_threshold)
341  return (false);
342  }
343 
344  return (true);
345 }
346 
347 #define PCL_INSTANTIATE_SampleConsensusModelLine(T) template class PCL_EXPORTS pcl::SampleConsensusModelLine<T>;
348 
349 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_LINE_H_
350 
Define methods for centroid estimation and covariance matrix calculus.
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
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:403
void resize(std::size_t count)
Resizes the container to contain count elements.
Definition: point_cloud.h:462
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:392
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
std::size_t size() const
Definition: point_cloud.h:443
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid line model, compute the model coefficients fro...
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 getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all squared distances from the cloud data to a given line model.
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
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 line model coefficients.
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the line coefficients using the given inlier set and return them to the user.
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 line 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.
void computeCorrespondingEigenVector(const Matrix &mat, const typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the corresponding eigenvector to the given eigenvalue of the symmetric positive semi defin...
Definition: eigen.hpp:226
unsigned int computeCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, const Eigen::Matrix< Scalar, 4, 1 > &centroid, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix)
Compute the 3x3 covariance matrix of a given set of points.
Definition: centroid.hpp:191
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
unsigned int compute3DCentroid(ConstCloudIterator< PointT > &cloud_iterator, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the 3D (X-Y-Z) centroid of a set of points and return it as a 3D vector.
Definition: centroid.hpp:56
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
Helper functor structure for concatenate.
Definition: concatenate.h:50