Point Cloud Library (PCL)  1.7.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
50 pcl::SampleConsensusModelStick<PointT>::isSampleGood (const std::vector<int> &samples) const
51 {
52  if (
53  (input_->points[samples[0]].x != input_->points[samples[1]].x)
54  &&
55  (input_->points[samples[0]].y != input_->points[samples[1]].y)
56  &&
57  (input_->points[samples[0]].z != input_->points[samples[1]].z))
58  return (true);
59 
60  return (false);
61 }
62 
63 //////////////////////////////////////////////////////////////////////////
64 template <typename PointT> bool
66  const std::vector<int> &samples, Eigen::VectorXf &model_coefficients)
67 {
68  // Need 2 samples
69  if (samples.size () != 2)
70  {
71  PCL_ERROR ("[pcl::SampleConsensusModelStick::computeModelCoefficients] Invalid set of samples given (%zu)!\n", samples.size ());
72  return (false);
73  }
74 
75  model_coefficients.resize (7);
76  model_coefficients[0] = input_->points[samples[0]].x;
77  model_coefficients[1] = input_->points[samples[0]].y;
78  model_coefficients[2] = input_->points[samples[0]].z;
79 
80  model_coefficients[3] = input_->points[samples[1]].x;
81  model_coefficients[4] = input_->points[samples[1]].y;
82  model_coefficients[5] = input_->points[samples[1]].z;
83 
84 // model_coefficients[3] = input_->points[samples[1]].x - model_coefficients[0];
85 // model_coefficients[4] = input_->points[samples[1]].y - model_coefficients[1];
86 // model_coefficients[5] = input_->points[samples[1]].z - model_coefficients[2];
87 
88 // model_coefficients.template segment<3> (3).normalize ();
89  // We don't care about model_coefficients[6] which is the width (radius) of the stick
90 
91  return (true);
92 }
93 
94 //////////////////////////////////////////////////////////////////////////
95 template <typename PointT> void
97  const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
98 {
99  // Needs a valid set of model coefficients
100  if (!isModelValid (model_coefficients))
101  return;
102 
103  float sqr_threshold = static_cast<float> (radius_max_ * radius_max_);
104  distances.resize (indices_->size ());
105 
106  // Obtain the line point and direction
107  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
108  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
109  line_dir.normalize ();
110 
111  // Iterate through the 3d points and calculate the distances from them to the line
112  for (size_t i = 0; i < indices_->size (); ++i)
113  {
114  // Calculate the distance from the point to the line
115  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
116  float sqr_distance = (line_pt - input_->points[(*indices_)[i]].getVector4fMap ()).cross3 (line_dir).squaredNorm ();
117 
118  if (sqr_distance < sqr_threshold)
119  // Need to estimate sqrt here to keep MSAC and friends general
120  distances[i] = sqrt (sqr_distance);
121  else
122  // Penalize outliers by doubling the distance
123  distances[i] = 2 * sqrt (sqr_distance);
124  }
125 }
126 
127 //////////////////////////////////////////////////////////////////////////
128 template <typename PointT> void
130  const Eigen::VectorXf &model_coefficients, const double threshold, std::vector<int> &inliers)
131 {
132  // Needs a valid set of model coefficients
133  if (!isModelValid (model_coefficients))
134  return;
135 
136  float sqr_threshold = static_cast<float> (threshold * threshold);
137 
138  int nr_p = 0;
139  inliers.resize (indices_->size ());
140  error_sqr_dists_.resize (indices_->size ());
141 
142  // Obtain the line point and direction
143  Eigen::Vector4f line_pt1 (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
144  Eigen::Vector4f line_pt2 (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
145  Eigen::Vector4f line_dir = line_pt2 - line_pt1;
146  //Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
147  //Eigen::Vector4f line_dir (model_coefficients[3] - model_coefficients[0], model_coefficients[4] - model_coefficients[1], model_coefficients[5] - model_coefficients[2], 0);
148  line_dir.normalize ();
149  //float norm = line_dir.squaredNorm ();
150 
151  // Iterate through the 3d points and calculate the distances from them to the line
152  for (size_t i = 0; i < indices_->size (); ++i)
153  {
154  // Calculate the distance from the point to the line
155  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
156  Eigen::Vector4f dir = input_->points[(*indices_)[i]].getVector4fMap () - line_pt1;
157  //float u = dir.dot (line_dir);
158 
159  // If the point falls outside of the segment, ignore it
160  //if (u < 0.0f || u > 1.0f)
161  // continue;
162 
163  float sqr_distance = dir.cross3 (line_dir).squaredNorm ();
164  if (sqr_distance < sqr_threshold)
165  {
166  // Returns the indices of the points whose squared distances are smaller than the threshold
167  inliers[nr_p] = (*indices_)[i];
168  error_sqr_dists_[nr_p] = static_cast<double> (sqr_distance);
169  ++nr_p;
170  }
171  }
172  inliers.resize (nr_p);
173  error_sqr_dists_.resize (nr_p);
174 }
175 
176 ///////////////////////////////////////////////////////////////////////////
177 template <typename PointT> int
179  const Eigen::VectorXf &model_coefficients, const double threshold)
180 {
181  // Needs a valid set of model coefficients
182  if (!isModelValid (model_coefficients))
183  return (0);
184 
185  float sqr_threshold = static_cast<float> (threshold * threshold);
186 
187  int nr_i = 0, nr_o = 0;
188 
189  // Obtain the line point and direction
190  Eigen::Vector4f line_pt1 (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
191  Eigen::Vector4f line_pt2 (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
192  Eigen::Vector4f line_dir = line_pt2 - line_pt1;
193  line_dir.normalize ();
194 
195  //Eigen::Vector4f line_dir (model_coefficients[3] - model_coefficients[0], model_coefficients[4] - model_coefficients[1], model_coefficients[5] - model_coefficients[2], 0);
196  //Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
197 
198  // Iterate through the 3d points and calculate the distances from them to the line
199  for (size_t i = 0; i < indices_->size (); ++i)
200  {
201  // Calculate the distance from the point to the line
202  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
203  Eigen::Vector4f dir = input_->points[(*indices_)[i]].getVector4fMap () - line_pt1;
204  //float u = dir.dot (line_dir);
205 
206  // If the point falls outside of the segment, ignore it
207  //if (u < 0.0f || u > 1.0f)
208  // continue;
209 
210  float sqr_distance = dir.cross3 (line_dir).squaredNorm ();
211  // Use a larger threshold (4 times the radius) to get more points in
212  if (sqr_distance < sqr_threshold)
213  nr_i++;
214  else if (sqr_distance < 4 * sqr_threshold)
215  nr_o++;
216  }
217 
218  return (nr_i - nr_o < 0 ? 0 : nr_i - nr_o);
219 }
220 
221 //////////////////////////////////////////////////////////////////////////
222 template <typename PointT> void
224  const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
225 {
226  // Needs a valid set of model coefficients
227  if (!isModelValid (model_coefficients))
228  {
229  optimized_coefficients = model_coefficients;
230  return;
231  }
232 
233  // Need at least 2 points to estimate a line
234  if (inliers.size () <= 2)
235  {
236  PCL_ERROR ("[pcl::SampleConsensusModelStick::optimizeModelCoefficients] Not enough inliers found to support a model (%zu)! Returning the same coefficients.\n", inliers.size ());
237  optimized_coefficients = model_coefficients;
238  return;
239  }
240 
241  optimized_coefficients.resize (7);
242 
243  // Compute the 3x3 covariance matrix
244  Eigen::Vector4f centroid;
245  Eigen::Matrix3f covariance_matrix;
246 
247  computeMeanAndCovarianceMatrix (*input_, inliers, covariance_matrix, centroid);
248 
249  optimized_coefficients[0] = centroid[0];
250  optimized_coefficients[1] = centroid[1];
251  optimized_coefficients[2] = centroid[2];
252 
253  // Extract the eigenvalues and eigenvectors
254  Eigen::Vector3f eigen_values;
255  Eigen::Vector3f eigen_vector;
256  pcl::eigen33 (covariance_matrix, eigen_values);
257  pcl::computeCorrespondingEigenVector (covariance_matrix, eigen_values [2], eigen_vector);
258 
259  optimized_coefficients.template segment<3> (3).matrix () = eigen_vector;
260 }
261 
262 //////////////////////////////////////////////////////////////////////////
263 template <typename PointT> void
265  const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields)
266 {
267  // Needs a valid model coefficients
268  if (!isModelValid (model_coefficients))
269  return;
270 
271  // Obtain the line point and direction
272  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
273  Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
274 
275  projected_points.header = input_->header;
276  projected_points.is_dense = input_->is_dense;
277 
278  // Copy all the data fields from the input cloud to the projected one?
279  if (copy_data_fields)
280  {
281  // Allocate enough space and copy the basics
282  projected_points.points.resize (input_->points.size ());
283  projected_points.width = input_->width;
284  projected_points.height = input_->height;
285 
286  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
287  // Iterate over each point
288  for (size_t i = 0; i < projected_points.points.size (); ++i)
289  // Iterate over each dimension
290  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[i], projected_points.points[i]));
291 
292  // Iterate through the 3d points and calculate the distances from them to the line
293  for (size_t i = 0; i < inliers.size (); ++i)
294  {
295  Eigen::Vector4f pt (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z, 0);
296  // double k = (DOT_PROD_3D (points[i], p21) - dotA_B) / dotB_B;
297  float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
298 
299  Eigen::Vector4f pp = line_pt + k * line_dir;
300  // Calculate the projection of the point on the line (pointProj = A + k * B)
301  projected_points.points[inliers[i]].x = pp[0];
302  projected_points.points[inliers[i]].y = pp[1];
303  projected_points.points[inliers[i]].z = pp[2];
304  }
305  }
306  else
307  {
308  // Allocate enough space and copy the basics
309  projected_points.points.resize (inliers.size ());
310  projected_points.width = static_cast<uint32_t> (inliers.size ());
311  projected_points.height = 1;
312 
313  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
314  // Iterate over each point
315  for (size_t i = 0; i < inliers.size (); ++i)
316  // Iterate over each dimension
317  pcl::for_each_type <FieldList> (NdConcatenateFunctor <PointT, PointT> (input_->points[inliers[i]], projected_points.points[i]));
318 
319  // Iterate through the 3d points and calculate the distances from them to the line
320  for (size_t i = 0; i < inliers.size (); ++i)
321  {
322  Eigen::Vector4f pt (input_->points[inliers[i]].x, input_->points[inliers[i]].y, input_->points[inliers[i]].z, 0);
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.points[i].x = pp[0];
329  projected_points.points[i].y = pp[1];
330  projected_points.points[i].z = pp[2];
331  }
332  }
333 }
334 
335 //////////////////////////////////////////////////////////////////////////
336 template <typename PointT> bool
338  const std::set<int> &indices, const Eigen::VectorXf &model_coefficients, const double threshold)
339 {
340  // Needs a valid set of model coefficients
341  if (!isModelValid (model_coefficients))
342  return (false);
343 
344  // Obtain the line point and direction
345  Eigen::Vector4f line_pt (model_coefficients[0], model_coefficients[1], model_coefficients[2], 0);
346  Eigen::Vector4f line_dir (model_coefficients[3] - model_coefficients[0], model_coefficients[4] - model_coefficients[1], model_coefficients[5] - model_coefficients[2], 0);
347  //Eigen::Vector4f line_dir (model_coefficients[3], model_coefficients[4], model_coefficients[5], 0);
348  line_dir.normalize ();
349 
350  float sqr_threshold = static_cast<float> (threshold * threshold);
351  // Iterate through the 3d points and calculate the distances from them to the line
352  for (std::set<int>::const_iterator it = indices.begin (); it != indices.end (); ++it)
353  {
354  // Calculate the distance from the point to the line
355  // D = ||(P2-P1) x (P1-P0)|| / ||P2-P1|| = norm (cross (p2-p1, p2-p0)) / norm(p2-p1)
356  if ((line_pt - input_->points[*it].getVector4fMap ()).cross3 (line_dir).squaredNorm () > sqr_threshold)
357  return (false);
358  }
359 
360  return (true);
361 }
362 
363 #define PCL_INSTANTIATE_SampleConsensusModelStick(T) template class PCL_EXPORTS pcl::SampleConsensusModelStick<T>;
364 
365 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_STICK_H_
366