Point Cloud Library (PCL)  1.11.1
sac_model_stick.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: sac_model_line.hpp 2328 2011-08-31 08:11:00Z rusu $
38  *
39  */
40 
41 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_STICK_H_
42 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_STICK_H_
43 
44 #include <pcl/sample_consensus/sac_model_stick.h>
45 #include <pcl/common/centroid.h>
46 #include <pcl/common/concatenate.h>
47 
48 //////////////////////////////////////////////////////////////////////////
49 template <typename PointT> bool
51 {
52  if (samples.size () != sample_size_)
53  {
54  PCL_ERROR ("[pcl::SampleConsensusModelStick::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
55  return (false);
56  }
57  if (
58  ((*input_)[samples[0]].x != (*input_)[samples[1]].x)
59  &&
60  ((*input_)[samples[0]].y != (*input_)[samples[1]].y)
61  &&
62  ((*input_)[samples[0]].z != (*input_)[samples[1]].z))
63  {
64  return (true);
65  }
66 
67  return (false);
68 }
69 
70 //////////////////////////////////////////////////////////////////////////
71 template <typename PointT> bool
73  const Indices &samples, Eigen::VectorXf &model_coefficients) const
74 {
75  // Need 2 samples
76  if (samples.size () != sample_size_)
77  {
78  PCL_ERROR ("[pcl::SampleConsensusModelStick::computeModelCoefficients] Invalid set of samples given (%lu)!\n", samples.size ());
79  return (false);
80  }
81 
82  model_coefficients.resize (model_size_);
83  model_coefficients[0] = (*input_)[samples[0]].x;
84  model_coefficients[1] = (*input_)[samples[0]].y;
85  model_coefficients[2] = (*input_)[samples[0]].z;
86 
87  model_coefficients[3] = (*input_)[samples[1]].x;
88  model_coefficients[4] = (*input_)[samples[1]].y;
89  model_coefficients[5] = (*input_)[samples[1]].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 // model_coefficients.template segment<3> (3).normalize ();
96  // We don't care about model_coefficients[6] which is the width (radius) of the stick
97 
98  return (true);
99 }
100 
101 //////////////////////////////////////////////////////////////////////////
102 template <typename PointT> void
104  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
105 {
106  // Needs a valid set of model coefficients
107  if (!isModelValid (model_coefficients))
108  {
109  PCL_ERROR ("[pcl::SampleConsensusModelStick::getDistancesToModel] Given model is invalid!\n");
110  return;
111  }
112 
113  float sqr_threshold = static_cast<float> (radius_max_ * radius_max_);
114  distances.resize (indices_->size ());
115 
116  // Obtain the line point and direction
117  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
118  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
119  line_dir.normalize ();
120 
121  // Iterate through the 3d points and calculate the distances from them to the line
122  for (std::size_t i = 0; i < indices_->size (); ++i)
123  {
124  // Calculate the distance from the point to the line
125  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
126  float sqr_distance = (line_pt - (*input_)[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ();
127 
128  if (sqr_distance < sqr_threshold)
129  {
130  // Need to estimate sqrt here to keep MSAC and friends general
131  distances[i] = sqrt (sqr_distance);
132  }
133  else
134  {
135  // Penalize outliers by doubling the distance
136  distances[i] = 2 * sqrt (sqr_distance);
137  }
138  }
139 }
140 
141 //////////////////////////////////////////////////////////////////////////
142 template <typename PointT> void
144  const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
145 {
146  // Needs a valid set of model coefficients
147  if (!isModelValid (model_coefficients))
148  {
149  PCL_ERROR ("[pcl::SampleConsensusModelStick::selectWithinDistance] Given model is invalid!\n");
150  return;
151  }
152 
153  float sqr_threshold = static_cast<float> (threshold * threshold);
154 
155  inliers.clear ();
156  error_sqr_dists_.clear ();
157  inliers.reserve (indices_->size ());
158  error_sqr_dists_.reserve (indices_->size ());
159 
160  // Obtain the line point and direction
161  Eigen::Vector4f line_pt1 (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
162  Eigen::Vector4f line_pt2 (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
163  Eigen::Vector4f line_dir = line_pt2 - line_pt1;
164  //Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
165  //Eigen::Vector4f line_dir (model_coefficients[3] - model_coefficients[0], model_coefficients[4] - model_coefficients[1], model_coefficients[5] - model_coefficients[2], 0);
166  line_dir.normalize ();
167  //float norm = line_dir.squaredNorm ();
168 
169  // Iterate through the 3d points and calculate the distances from them to the line
170  for (std::size_t i = 0; i < indices_->size (); ++i)
171  {
172  // Calculate the distance from the point to the line
173  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
174  Eigen::Vector4f dir = (*input_)[(*indices_)[i]].getVector4fMap () - line_pt1;
175  //float u = dir.dot (line_dir);
176 
177  // If the point falls outside of the segment, ignore it
178  //if (u < 0.0f || u > 1.0f)
179  // continue;
180 
181  float sqr_distance = dir.cross3 (line_dir).squaredNorm ();
182  if (sqr_distance < sqr_threshold)
183  {
184  // Returns the indices of the points whose squared distances are smaller than the threshold
185  inliers.push_back ((*indices_)[i]);
186  error_sqr_dists_.push_back (static_cast<double> (sqr_distance));
187  }
188  }
189 }
190 
191 ///////////////////////////////////////////////////////////////////////////
192 template <typename PointT> std::size_t
194  const Eigen::VectorXf &model_coefficients, const double threshold) const
195 {
196  // Needs a valid set of model coefficients
197  if (!isModelValid (model_coefficients))
198  {
199  PCL_ERROR ("[pcl::SampleConsensusModelStick::countWithinDistance] Given model is invalid!\n");
200  return (0);
201  }
202 
203  float sqr_threshold = static_cast<float> (threshold * threshold);
204 
205  std::size_t nr_i = 0, nr_o = 0;
206 
207  // Obtain the line point and direction
208  Eigen::Vector4f line_pt1 (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
209  Eigen::Vector4f line_pt2 (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
210  Eigen::Vector4f line_dir = line_pt2 - line_pt1;
211  line_dir.normalize ();
212 
213  //Eigen::Vector4f line_dir (model_coefficients[3] - model_coefficients[0], model_coefficients[4] - model_coefficients[1], model_coefficients[5] - model_coefficients[2], 0);
214  //Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
215 
216  // Iterate through the 3d points and calculate the distances from them to the line
217  for (std::size_t i = 0; i < indices_->size (); ++i)
218  {
219  // Calculate the distance from the point to the line
220  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
221  Eigen::Vector4f dir = (*input_)[(*indices_)[i]].getVector4fMap () - line_pt1;
222  //float u = dir.dot (line_dir);
223 
224  // If the point falls outside of the segment, ignore it
225  //if (u < 0.0f || u > 1.0f)
226  // continue;
227 
228  float sqr_distance = dir.cross3 (line_dir).squaredNorm ();
229  // Use a larger threshold (4 times the radius) to get more points in
230  if (sqr_distance < sqr_threshold)
231  {
232  nr_i++;
233  }
234  else if (sqr_distance < 4.0f * sqr_threshold)
235  {
236  nr_o++;
237  }
238  }
239 
240  return (nr_i <= nr_o ? 0 : nr_i - nr_o);
241 }
242 
243 //////////////////////////////////////////////////////////////////////////
244 template <typename PointT> void
246  const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const
247 {
248  // Needs a valid set of model coefficients
249  if (!isModelValid (model_coefficients))
250  {
251  optimized_coefficients = model_coefficients;
252  return;
253  }
254 
255  // Need more than the minimum sample size to make a difference
256  if (inliers.size () <= sample_size_)
257  {
258  PCL_ERROR ("[pcl::SampleConsensusModelStick::optimizeModelCoefficients] Not enough inliers to refine/optimize the model's coefficients (%lu)! Returning the same coefficients.\n", inliers.size ());
259  optimized_coefficients = model_coefficients;
260  return;
261  }
262 
263  optimized_coefficients.resize (model_size_);
264 
265  // Compute the 3x3 covariance matrix
266  Eigen::Vector4f centroid;
267  Eigen::Matrix3f covariance_matrix;
268 
269  computeMeanAndCovarianceMatrix (*input_, inliers, covariance_matrix, centroid);
270 
271  optimized_coefficients[0] = centroid[0];
272  optimized_coefficients[1] = centroid[1];
273  optimized_coefficients[2] = centroid[2];
274 
275  // Extract the eigenvalues and eigenvectors
276  Eigen::Vector3f eigen_values;
277  Eigen::Vector3f eigen_vector;
278  pcl::eigen33 (covariance_matrix, eigen_values);
279  pcl::computeCorrespondingEigenVector (covariance_matrix, eigen_values [2], eigen_vector);
280 
281  optimized_coefficients.template segment<3> (3).matrix () = eigen_vector;
282 }
283 
284 //////////////////////////////////////////////////////////////////////////
285 template <typename PointT> void
287  const Indices &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields) const
288 {
289  // Needs a valid model coefficients
290  if (!isModelValid (model_coefficients))
291  {
292  PCL_ERROR ("[pcl::SampleConsensusModelStick::projectPoints] Given model is invalid!\n");
293  return;
294  }
295 
296  // Obtain the line point and direction
297  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
298  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0.0f);
299 
300  projected_points.header = input_->header;
301  projected_points.is_dense = input_->is_dense;
302 
303  // Copy all the data fields from the input cloud to the projected one?
304  if (copy_data_fields)
305  {
306  // Allocate enough space and copy the basics
307  projected_points.points.resize (input_->size ());
308  projected_points.width = input_->width;
309  projected_points.height = input_->height;
310 
311  using FieldList = typename pcl::traits::fieldList<PointT>::type;
312  // Iterate over each point
313  for (std::size_t i = 0; i < projected_points.size (); ++i)
314  {
315  // Iterate over each dimension
316  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[i], projected_points[i]));
317  }
318 
319  // Iterate through the 3d points and calculate the distances from them to the line
320  for (const auto &inlier : inliers)
321  {
322  Eigen::Vector4f pt ((*input_)[inlier].x, (*input_)[inlier].y, (*input_)[inlier].z, 0.0f);
323  // double k = (DOT_PROD_3D (points[i], p21) - dotA_B) / dotB_B;
324  float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
325 
326  Eigen::Vector4f pp = line_pt + k * line_dir;
327  // Calculate the projection of the point on the line (pointProj = A + k * B)
328  projected_points[inlier].x = pp[0];
329  projected_points[inlier].y = pp[1];
330  projected_points[inlier].z = pp[2];
331  }
332  }
333  else
334  {
335  // Allocate enough space and copy the basics
336  projected_points.points.resize (inliers.size ());
337  projected_points.width = inliers.size ();
338  projected_points.height = 1;
339 
340  using FieldList = typename pcl::traits::fieldList<PointT>::type;
341  // Iterate over each point
342  for (std::size_t i = 0; i < inliers.size (); ++i)
343  {
344  // Iterate over each dimension
345  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> ((*input_)[inliers[i]], projected_points[i]));
346  }
347 
348  // Iterate through the 3d points and calculate the distances from them to the line
349  for (std::size_t i = 0; i < inliers.size (); ++i)
350  {
351  Eigen::Vector4f pt ((*input_)[inliers[i]].x, (*input_)[inliers[i]].y, (*input_)[inliers[i]].z, 0.0f);
352  // double k = (DOT_PROD_3D (points[i], p21) - dotA_B) / dotB_B;
353  float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
354 
355  Eigen::Vector4f pp = line_pt + k * line_dir;
356  // Calculate the projection of the point on the line (pointProj = A + k * B)
357  projected_points[i].x = pp[0];
358  projected_points[i].y = pp[1];
359  projected_points[i].z = pp[2];
360  }
361  }
362 }
363 
364 //////////////////////////////////////////////////////////////////////////
365 template <typename PointT> bool
367  const std::set<index_t> &indices, const Eigen::VectorXf &model_coefficients, const double threshold) const
368 {
369  // Needs a valid set of model coefficients
370  if (!isModelValid (model_coefficients))
371  {
372  PCL_ERROR ("[pcl::SampleConsensusModelStick::doSamplesVerifyModel] Given model is invalid!\n");
373  return (false);
374  }
375 
376  // Obtain the line point and direction
377  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0.0f);
378  Eigen::Vector4f line_dir (model_coefficients[3] - model_coefficients[0], model_coefficients[4] - model_coefficients[1], model_coefficients[5] - model_coefficients[2], 0.0f);
379  //Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
380  line_dir.normalize ();
381 
382  float sqr_threshold = static_cast<float> (threshold * threshold);
383  // Iterate through the 3d points and calculate the distances from them to the line
384  for (const auto &index : indices)
385  {
386  // Calculate the distance from the point to the line
387  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
388  if ((line_pt - (*input_)[index].getVector4fMap ()).cross3 (line_dir).squaredNorm () > sqr_threshold)
389  {
390  return (false);
391  }
392  }
393 
394  return (true);
395 }
396 
397 #define PCL_INSTANTIATE_SampleConsensusModelStick(T) template class PCL_EXPORTS pcl::SampleConsensusModelStick<T>;
398 
399 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_STICK_H_
400 
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::PointCloud::points
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:411
pcl::SampleConsensusModelStick::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_stick.hpp:143
pcl::SampleConsensusModelStick::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 stick model coefficients.
Definition: sac_model_stick.hpp:366
pcl::SampleConsensusModelStick::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_stick.hpp:193
pcl::SampleConsensusModelStick::computeModelCoefficients
bool computeModelCoefficients(const Indices &samples, Eigen::VectorXf &model_coefficients) const override
Check whether the given index samples can form a valid stick model, compute the model coefficients fr...
Definition: sac_model_stick.hpp:72
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: distances.h:55
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::SampleConsensusModelStick::isSampleGood
bool isSampleGood(const Indices &samples) const override
Check if a sample of indices results in a good sample of points indices.
Definition: sac_model_stick.hpp:50
pcl::PointCloud::width
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:414
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::PointCloud::size
std::size_t size() const
Definition: point_cloud.h:459
pcl::computeCorrespondingEigenVector
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
pcl::SampleConsensusModelStick::getDistancesToModel
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const override
Compute all squared distances from the cloud data to a given stick model.
Definition: sac_model_stick.hpp:103
pcl::SampleConsensusModelStick::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 stick model.
Definition: sac_model_stick.hpp:286
centroid.h
pcl::SampleConsensusModelStick::optimizeModelCoefficients
void optimizeModelCoefficients(const Indices &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients) const override
Recompute the stick coefficients using the given inlier set and return them to the user.
Definition: sac_model_stick.hpp:245