Point Cloud Library (PCL)  1.8.1-dev
ia_fpcs.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2014-, Open Perception, Inc.
6  * Copyright (C) 2008 Ben Gurion University of the Negev, Beer Sheva, Israel.
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 are met
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_REGISTRATION_IMPL_IA_FPCS_H_
39 #define PCL_REGISTRATION_IMPL_IA_FPCS_H_
40 
41 #include <pcl/registration/ia_fpcs.h>
42 #include <pcl/common/time.h>
43 #include <pcl/common/distances.h>
44 #include <pcl/sample_consensus/sac_model_plane.h>
45 #include <pcl/registration/transformation_estimation_3point.h>
46 
47 ///////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointT> inline float
49 pcl::getMeanPointDensity (const typename pcl::PointCloud<PointT>::ConstPtr &cloud, float max_dist, int nr_threads)
50 {
51  const float max_dist_sqr = max_dist * max_dist;
52  const std::size_t s = cloud.size ();
53 
55  tree.setInputCloud (cloud);
56 
57  float mean_dist = 0.f;
58  int num = 0;
59  std::vector <int> ids (2);
60  std::vector <float> dists_sqr (2);
61 
62 #ifdef _OPENMP
63 #pragma omp parallel for \
64  reduction (+:mean_dist, num) \
65  private (ids, dists_sqr) shared (tree, cloud) \
66  default (none)num_threads (nr_threads)
67 #endif
68 
69  for (int i = 0; i < 1000; i++)
70  {
71  tree.nearestKSearch (cloud->points[rand () % s], 2, ids, dists_sqr);
72  if (dists_sqr[1] < max_dist_sqr)
73  {
74  mean_dist += std::sqrt (dists_sqr[1]);
75  num++;
76  }
77  }
78 
79  return (mean_dist / num);
80 };
81 
82 
83 ///////////////////////////////////////////////////////////////////////////////////////////
84 template <typename PointT> inline float
85 pcl::getMeanPointDensity (const typename pcl::PointCloud<PointT>::ConstPtr &cloud, const std::vector <int> &indices,
86  float max_dist, int nr_threads)
87 {
88  const float max_dist_sqr = max_dist * max_dist;
89  const std::size_t s = indices.size ();
90 
92  tree.setInputCloud (cloud);
93 
94  float mean_dist = 0.f;
95  int num = 0;
96  std::vector <int> ids (2);
97  std::vector <float> dists_sqr (2);
98 
99 #ifdef _OPENMP
100 #pragma omp parallel for \
101  reduction (+:mean_dist, num) \
102  private (ids, dists_sqr) shared (tree, cloud, indices) \
103  default (none)num_threads (nr_threads)
104 #endif
105 
106  for (int i = 0; i < 1000; i++)
107  {
108  tree.nearestKSearch (cloud->points[indices[rand () % s]], 2, ids, dists_sqr);
109  if (dists_sqr[1] < max_dist_sqr)
110  {
111  mean_dist += std::sqrt (dists_sqr[1]);
112  num++;
113  }
114  }
115 
116  return (mean_dist / num);
117 };
118 
119 
120 ///////////////////////////////////////////////////////////////////////////////////////////
121 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
123  source_normals_ (),
124  target_normals_ (),
125  nr_threads_ (1),
126  approx_overlap_ (0.5f),
127  delta_ (1.f),
128  score_threshold_ (FLT_MAX),
129  nr_samples_ (0),
130  max_norm_diff_ (90.f),
131  max_runtime_ (0),
132  fitness_score_ (FLT_MAX),
133  diameter_ (),
134  max_base_diameter_sqr_ (),
135  use_normals_ (false),
136  normalize_delta_ (true),
137  max_pair_diff_ (),
138  max_edge_diff_ (),
139  coincidation_limit_ (),
140  max_mse_ (),
141  max_inlier_dist_sqr_ (),
142  small_error_ (0.00001f)
143 {
144  reg_name_ = "pcl::registration::FPCSInitialAlignment";
145  max_iterations_ = 0;
146  ransac_iterations_ = 1000;
148 }
149 
150 
151 ///////////////////////////////////////////////////////////////////////////////////////////
152 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
154  PointCloudSource &output,
155  const Eigen::Matrix4f &guess)
156 {
157  if (!initCompute ())
158  return;
159 
160  final_transformation_ = guess;
161  bool abort = false;
162  std::vector <MatchingCandidates> all_candidates (max_iterations_);
163  pcl::StopWatch timer;
164 
165  #ifdef _OPENMP
166  #pragma omp parallel num_threads (nr_threads_)
167  #endif
168  {
169  #ifdef _OPENMP
170  std::srand (static_cast <unsigned int> (std::time (NULL)) ^ omp_get_thread_num ());
171  #pragma omp for schedule (dynamic)
172  #endif
173  for (int i = 0; i < max_iterations_; i++)
174  {
175 
176  #ifdef _OPENMP
177  #pragma omp flush (abort)
178  #endif
179 
180  MatchingCandidates candidates (1);
181  std::vector <int> base_indices (4);
182  float ratio[2];
183  all_candidates[i] = candidates;
184 
185  if (!abort)
186  {
187  // select four coplanar point base
188  if (selectBase (base_indices, ratio) == 0)
189  {
190  // calculate candidate pair correspondences using diagonal lenghts of base
191  pcl::Correspondences pairs_a, pairs_b;
192  if (bruteForceCorrespondences (base_indices[0], base_indices[1], pairs_a) == 0 &&
193  bruteForceCorrespondences (base_indices[2], base_indices[3], pairs_b) == 0)
194  {
195  // determine candidate matches by combining pair correspondences based on segment distances
196  std::vector <std::vector <int> > matches;
197  if (determineBaseMatches (base_indices, matches, pairs_a, pairs_b, ratio) == 0)
198  {
199  // check and evaluate candidate matches and store them
200  handleMatches (base_indices, matches, candidates);
201  if (candidates.size () != 0)
202  all_candidates[i] = candidates;
203  }
204  }
205  }
206 
207  // check terminate early (time or fitness_score threshold reached)
208  abort = (candidates.size () > 0 ? candidates[0].fitness_score < score_threshold_ : abort);
209  abort = (abort ? abort : timer.getTimeSeconds () > max_runtime_);
210 
211 
212  #ifdef _OPENMP
213  #pragma omp flush (abort)
214  #endif
215  }
216  }
217  }
218 
219 
220  // determine best match over all trys
221  finalCompute (all_candidates);
222 
223  // apply the final transformation
224  pcl::transformPointCloud (*input_, output, final_transformation_);
225 
226  deinitCompute ();
227 }
228 
229 
230 ///////////////////////////////////////////////////////////////////////////////////////////
231 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> bool
233 {
234  std::srand (static_cast <unsigned int> (std::time (NULL)));
235 
236  // basic pcl initialization
238  return (false);
239 
240  // check if source and target are given
241  if (!input_ || !target_)
242  {
243  PCL_ERROR ("[%s::initCompute] Source or target dataset not given!\n", reg_name_.c_str ());
244  return (false);
245  }
246 
247  if (!target_indices_ || target_indices_->size () == 0)
248  {
249  target_indices_.reset (new std::vector <int> (static_cast <int> (target_->size ())));
250  int index = 0;
251  for (std::vector <int>::iterator it = target_indices_->begin (), it_e = target_indices_->end (); it != it_e; it++)
252  *it = index++;
253  target_cloud_updated_ = true;
254  }
255 
256  // if a sample size for the point clouds is given; prefarably no sampling of target cloud
257  if (nr_samples_ != 0)
258  {
259  const int ss = static_cast <int> (indices_->size ());
260  const int sample_fraction_src = std::max (1, static_cast <int> (ss / nr_samples_));
261 
262  source_indices_ = pcl::IndicesPtr (new std::vector <int>);
263  for (int i = 0; i < ss; i++)
264  if (rand () % sample_fraction_src == 0)
265  source_indices_->push_back ((*indices_) [i]);
266  }
267  else
268  source_indices_ = indices_;
269 
270  // check usage of normals
271  if (source_normals_ && target_normals_ && source_normals_->size () == input_->size () && target_normals_->size () == target_->size ())
272  use_normals_ = true;
273 
274  // set up tree structures
275  if (target_cloud_updated_)
276  {
277  tree_->setInputCloud (target_, target_indices_);
278  target_cloud_updated_ = false;
279  }
280 
281  // set predefined variables
282  const int min_iterations = 4;
283  const float diameter_fraction = 0.3f;
284 
285  // get diameter of input cloud (distance between farthest points)
286  Eigen::Vector4f pt_min, pt_max;
287  pcl::getMinMax3D (*target_, *target_indices_, pt_min, pt_max);
288  diameter_ = (pt_max - pt_min).norm ();
289 
290  // derive the limits for the random base selection
291  float max_base_diameter = diameter_* approx_overlap_ * 2.f;
292  max_base_diameter_sqr_ = max_base_diameter * max_base_diameter;
293 
294  // normalize the delta
295  if (normalize_delta_)
296  {
297  float mean_dist = getMeanPointDensity <PointTarget> (target_, *target_indices_, 0.05f * diameter_, nr_threads_);
298  delta_ *= mean_dist;
299  }
300 
301  // heuristic determination of number of trials to have high probabilty of finding a good solution
302  if (max_iterations_ == 0)
303  {
304  float first_est = std::log (small_error_) / std::log (1.0 - std::pow ((double) approx_overlap_, (double) min_iterations));
305  max_iterations_ = static_cast <int> (first_est / (diameter_fraction * approx_overlap_ * 2.f));
306  }
307 
308  // set further parameter
309  if (score_threshold_ == FLT_MAX)
310  score_threshold_ = 1.f - approx_overlap_;
311 
312  if (max_iterations_ < 4)
313  max_iterations_ = 4;
314 
315  if (max_runtime_ < 1)
316  max_runtime_ = INT_MAX;
317 
318  // calculate internal parameters based on the the estimated point density
319  max_pair_diff_ = delta_ * 2.f;
320  max_edge_diff_ = delta_ * 4.f;
321  coincidation_limit_ = delta_ * 2.f; // EDITED: originally std::sqrt (delta_ * 2.f)
322  max_mse_ = powf (delta_* 2.f, 2.f);
323  max_inlier_dist_sqr_ = powf (delta_ * 2.f, 2.f);
324 
325  // reset fitness_score
326  fitness_score_ = FLT_MAX;
327 
328  return (true);
329 }
330 
331 
332 ///////////////////////////////////////////////////////////////////////////////////////////
333 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> int
335  std::vector <int> &base_indices,
336  float (&ratio)[2])
337 {
338  const float too_close_sqr = max_base_diameter_sqr_*0.01;
339 
340  Eigen::VectorXf coefficients (4);
342  plane.setIndices (target_indices_);
343  Eigen::Vector4f centre_pt;
344  float nearest_to_plane = FLT_MAX;
345 
346  // repeat base search until valid quadruple was found or ransac_iterations_ number of trys were unsuccessfull
347  for (int i = 0; i < ransac_iterations_; i++)
348  {
349  // random select an appropriate point triple
350  if (selectBaseTriangle (base_indices) < 0)
351  continue;
352 
353  std::vector <int> base_triple (base_indices.begin (), base_indices.end () - 1);
354  plane.computeModelCoefficients (base_triple, coefficients);
355  pcl::compute3DCentroid (*target_, base_triple, centre_pt);
356 
357  // loop over all points in source cloud to find most suitable fourth point
358  const PointTarget *pt1 = &(target_->points[base_indices[0]]);
359  const PointTarget *pt2 = &(target_->points[base_indices[1]]);
360  const PointTarget *pt3 = &(target_->points[base_indices[2]]);
361 
362  for (std::vector <int>::iterator it = target_indices_->begin (), it_e = target_indices_->end (); it != it_e; it++)
363  {
364  const PointTarget *pt4 = &(target_->points[*it]);
365 
366  float d1 = pcl::squaredEuclideanDistance (*pt4, *pt1);
367  float d2 = pcl::squaredEuclideanDistance (*pt4, *pt2);
368  float d3 = pcl::squaredEuclideanDistance (*pt4, *pt3);
369  float d4 = (pt4->getVector3fMap () - centre_pt.head (3)).squaredNorm ();
370 
371  // check distance between points w.r.t minimum sampling distance; EDITED -> 4th point now also limited by max base line
372  if (d1 < too_close_sqr || d2 < too_close_sqr || d3 < too_close_sqr || d4 < too_close_sqr ||
373  d1 > max_base_diameter_sqr_ || d2 > max_base_diameter_sqr_ || d3 > max_base_diameter_sqr_)
374  continue;
375 
376  // check distance to plane to get point closest to plane
377  float dist_to_plane = pcl::pointToPlaneDistance (*pt4, coefficients);
378  if (dist_to_plane < nearest_to_plane)
379  {
380  base_indices[3] = *it;
381  nearest_to_plane = dist_to_plane;
382  }
383  }
384 
385  // check if at least one point fullfilled the conditions
386  if (nearest_to_plane != FLT_MAX)
387  {
388  // order points to build largest quadrangle and calcuate intersection ratios of diagonals
389  setupBase (base_indices, ratio);
390  return (0);
391  }
392  }
393 
394  // return unsuccessfull if no quadruple was selected
395  return (-1);
396 }
397 
398 
399 ///////////////////////////////////////////////////////////////////////////////////////////
400 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> int
402 {
403  int nr_points = static_cast <int> (target_indices_->size ());
404  float best_t = 0.f;
405 
406  // choose random first point
407  base_indices[0] = (*target_indices_)[rand () % nr_points];
408  int *index1 = &base_indices[0];
409 
410  // random search for 2 other points (as far away as overlap allows)
411  for (int i = 0; i < ransac_iterations_; i++)
412  {
413  int *index2 = &(*target_indices_)[rand () % nr_points];
414  int *index3 = &(*target_indices_)[rand () % nr_points];
415 
416  Eigen::Vector3f u = target_->points[*index2].getVector3fMap () - target_->points[*index1].getVector3fMap ();
417  Eigen::Vector3f v = target_->points[*index3].getVector3fMap () - target_->points[*index1].getVector3fMap ();
418  float t = u.cross (v).squaredNorm (); // triangle area (0.5 * sqrt(t)) should be maximal
419 
420  // check for most suitable point triple
421  if (t > best_t && u.squaredNorm () < max_base_diameter_sqr_ && v.squaredNorm () < max_base_diameter_sqr_)
422  {
423  best_t = t;
424  base_indices[1] = *index2;
425  base_indices[2] = *index3;
426  }
427  }
428 
429  // return if a triplet could be selected
430  return (best_t == 0.f ? -1 : 0);
431 }
432 
433 
434 ///////////////////////////////////////////////////////////////////////////////////////////
435 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
437  std::vector <int> &base_indices,
438  float (&ratio)[2])
439 {
440  float best_t = FLT_MAX;
441  const std::vector <int> copy (base_indices.begin (), base_indices.end ());
442  std::vector <int> temp (base_indices.begin (), base_indices.end ());
443 
444  // loop over all combinations of base points
445  for (std::vector <int>::const_iterator i = copy.begin (), i_e = copy.end (); i != i_e; i++)
446  for (std::vector <int>::const_iterator j = copy.begin (), j_e = copy.end (); j != j_e; j++)
447  {
448  if (i == j)
449  continue;
450 
451  for (std::vector <int>::const_iterator k = copy.begin (), k_e = copy.end (); k != k_e; k++)
452  {
453  if (k == j || k == i)
454  continue;
455 
456  std::vector <int>::const_iterator l = copy.begin ();
457  while (l == i || l == j || l == k)
458  l++;
459 
460  temp[0] = *i;
461  temp[1] = *j;
462  temp[2] = *k;
463  temp[3] = *l;
464 
465  // calculate diagonal intersection ratios and check for suitable segment to segment distances
466  float ratio_temp[2];
467  float t = segmentToSegmentDist (temp, ratio_temp);
468  if (t < best_t)
469  {
470  best_t = t;
471  ratio[0] = ratio_temp[0];
472  ratio[1] = ratio_temp[1];
473  base_indices = temp;
474  }
475  }
476  }
477 }
478 
479 
480 ///////////////////////////////////////////////////////////////////////////////////////////
481 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> float
483  const std::vector <int> &base_indices,
484  float (&ratio)[2])
485 {
486  // get point vectors
487  Eigen::Vector3f u = target_->points[base_indices[1]].getVector3fMap () - target_->points[base_indices[0]].getVector3fMap ();
488  Eigen::Vector3f v = target_->points[base_indices[3]].getVector3fMap () - target_->points[base_indices[2]].getVector3fMap ();
489  Eigen::Vector3f w = target_->points[base_indices[0]].getVector3fMap () - target_->points[base_indices[2]].getVector3fMap ();
490 
491  // calculate segment distances
492  float a = u.dot (u);
493  float b = u.dot (v);
494  float c = v.dot (v);
495  float d = u.dot (w);
496  float e = v.dot (w);
497  float D = a * c - b * b;
498  float sN = 0.f, sD = D;
499  float tN = 0.f, tD = D;
500 
501  // check segments
502  if (D < small_error_)
503  {
504  sN = 0.f;
505  sD = 1.f;
506  tN = e;
507  tD = c;
508  }
509  else
510  {
511  sN = (b * e - c * d);
512  tN = (a * e - b * d);
513 
514  if (sN < 0.f)
515  {
516  sN = 0.f;
517  tN = e;
518  tD = c;
519  }
520  else if (sN > sD)
521  {
522  sN = sD;
523  tN = e + b;
524  tD = c;
525  }
526  }
527 
528  if (tN < 0.f)
529  {
530  tN = 0.f;
531 
532  if (-d < 0.f)
533  sN = 0.f;
534 
535  else if (-d > a)
536  sN = sD;
537 
538  else
539  {
540  sN = -d;
541  sD = a;
542  }
543  }
544 
545  else if (tN > tD)
546  {
547  tN = tD;
548 
549  if ((-d + b) < 0.f)
550  sN = 0.f;
551 
552  else if ((-d + b) > a)
553  sN = sD;
554 
555  else
556  {
557  sN = (-d + b);
558  sD = a;
559  }
560  }
561 
562  // set intersection ratios
563  ratio[0] = (std::abs (sN) < small_error_) ? 0.f : sN / sD;
564  ratio[1] = (std::abs (tN) < small_error_) ? 0.f : tN / tD;
565 
566  Eigen::Vector3f x = w + (ratio[0] * u) - (ratio[1] * v);
567  return (x.norm ());
568 }
569 
570 
571 ///////////////////////////////////////////////////////////////////////////////////////////
572 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> int
574  int idx1,
575  int idx2,
576  pcl::Correspondences &pairs)
577 {
578  const float max_norm_diff = 0.5f * max_norm_diff_ * M_PI / 180.f;
579 
580  // calculate reference segment distance and normal angle
581  float ref_dist = pcl::euclideanDistance (target_->points[idx1], target_->points[idx2]);
582  float ref_norm_angle = (use_normals_ ? (target_normals_->points[idx1].getNormalVector3fMap () -
583  target_normals_->points[idx2].getNormalVector3fMap ()).norm () : 0.f);
584 
585  // loop over all pairs of points in source point cloud
586  std::vector <int>::iterator it_out = source_indices_->begin (), it_out_e = source_indices_->end () - 1;
587  std::vector <int>::iterator it_in, it_in_e = source_indices_->end ();
588  for ( ; it_out != it_out_e; it_out++)
589  {
590  it_in = it_out + 1;
591  const PointSource *pt1 = &(*input_)[*it_out];
592  for ( ; it_in != it_in_e; it_in++)
593  {
594  const PointSource *pt2 = &(*input_)[*it_in];
595 
596  // check point distance compared to reference dist (from base)
597  float dist = pcl::euclideanDistance (*pt1, *pt2);
598  if (std::abs(dist - ref_dist) < max_pair_diff_)
599  {
600  // add here normal evaluation if normals are given
601  if (use_normals_)
602  {
603  const NormalT *pt1_n = &(source_normals_->points[*it_out]);
604  const NormalT *pt2_n = &(source_normals_->points[*it_in]);
605 
606  float norm_angle_1 = (pt1_n->getNormalVector3fMap () - pt2_n->getNormalVector3fMap ()).norm ();
607  float norm_angle_2 = (pt1_n->getNormalVector3fMap () + pt2_n->getNormalVector3fMap ()).norm ();
608 
609  float norm_diff = std::min <float> (std::abs (norm_angle_1 - ref_norm_angle), std::abs (norm_angle_2 - ref_norm_angle));
610  if (norm_diff > max_norm_diff)
611  continue;
612  }
613 
614  pairs.push_back (pcl::Correspondence (*it_in, *it_out, dist));
615  pairs.push_back (pcl::Correspondence (*it_out, *it_in, dist));
616  }
617  }
618  }
619 
620  // return success if at least one correspondence was found
621  return (pairs.size () == 0 ? -1 : 0);
622 }
623 
624 
625 ///////////////////////////////////////////////////////////////////////////////////////////
626 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> int
628  const std::vector <int> &base_indices,
629  std::vector <std::vector <int> > &matches,
630  const pcl::Correspondences &pairs_a,
631  const pcl::Correspondences &pairs_b,
632  const float (&ratio)[2])
633 {
634  // calculate edge lengths of base
635  float dist_base[4];
636  dist_base[0] = pcl::euclideanDistance (target_->points[base_indices[0]], target_->points[base_indices[2]]);
637  dist_base[1] = pcl::euclideanDistance (target_->points[base_indices[0]], target_->points[base_indices[3]]);
638  dist_base[2] = pcl::euclideanDistance (target_->points[base_indices[1]], target_->points[base_indices[2]]);
639  dist_base[3] = pcl::euclideanDistance (target_->points[base_indices[1]], target_->points[base_indices[3]]);
640 
641  // loop over first point pair correspondences and store intermediate points 'e' in new point cloud
643  cloud_e->resize (pairs_a.size () * 2);
644  PointCloudSourceIterator it_pt = cloud_e->begin ();
645  for (pcl::Correspondences::const_iterator it_pair = pairs_a.begin (), it_pair_e = pairs_a.end () ; it_pair != it_pair_e; it_pair++)
646  {
647  const PointSource *pt1 = &(input_->points[it_pair->index_match]);
648  const PointSource *pt2 = &(input_->points[it_pair->index_query]);
649 
650  // calculate intermediate points using both ratios from base (r1,r2)
651  for (int i = 0; i < 2; i++, it_pt++)
652  {
653  it_pt->x = pt1->x + ratio[i] * (pt2->x - pt1->x);
654  it_pt->y = pt1->y + ratio[i] * (pt2->y - pt1->y);
655  it_pt->z = pt1->z + ratio[i] * (pt2->z - pt1->z);
656  }
657  }
658 
659  // initialize new kd tree of intermediate points from first point pair correspondences
661  tree_e->setInputCloud (cloud_e);
662 
663  std::vector <int> ids;
664  std::vector <float> dists_sqr;
665 
666  // loop over second point pair correspondences
667  for (pcl::Correspondences::const_iterator it_pair = pairs_b.begin (), it_pair_e = pairs_b.end () ; it_pair != it_pair_e; it_pair++)
668  {
669  const PointTarget *pt1 = &(input_->points[it_pair->index_match]);
670  const PointTarget *pt2 = &(input_->points[it_pair->index_query]);
671 
672  // calculate intermediate points using both ratios from base (r1,r2)
673  for (int i = 0; i < 2; i++)
674  {
675  PointTarget pt_e;
676  pt_e.x = pt1->x + ratio[i] * (pt2->x - pt1->x);
677  pt_e.y = pt1->y + ratio[i] * (pt2->y - pt1->y);
678  pt_e.z = pt1->z + ratio[i] * (pt2->z - pt1->z);
679 
680  // search for corresponding intermediate points
681  tree_e->radiusSearch (pt_e, coincidation_limit_, ids, dists_sqr);
682  for (std::vector <int>::iterator it = ids.begin (), it_e = ids.end (); it != it_e; it++)
683  {
684  std::vector <int> match_indices (4);
685 
686  match_indices[0] = pairs_a[static_cast <int> (std::floor ((float)(*it/2.f)))].index_match;
687  match_indices[1] = pairs_a[static_cast <int> (std::floor ((float)(*it/2.f)))].index_query;
688  match_indices[2] = it_pair->index_match;
689  match_indices[3] = it_pair->index_query;
690 
691  // EDITED: added coarse check of match based on edge length (due to rigid-body )
692  if (checkBaseMatch (match_indices, dist_base) < 0)
693  continue;
694 
695  matches.push_back (match_indices);
696  }
697  }
698  }
699 
700  // return unsuccessfull if no match was found
701  return (matches.size () > 0 ? 0 : -1);
702 }
703 
704 
705 ///////////////////////////////////////////////////////////////////////////////////////////
706 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> int
708  const std::vector <int> &match_indices,
709  const float (&dist_ref)[4])
710 {
711  float d0 = pcl::euclideanDistance (input_->points[match_indices[0]], input_->points[match_indices[2]]);
712  float d1 = pcl::euclideanDistance (input_->points[match_indices[0]], input_->points[match_indices[3]]);
713  float d2 = pcl::euclideanDistance (input_->points[match_indices[1]], input_->points[match_indices[2]]);
714  float d3 = pcl::euclideanDistance (input_->points[match_indices[1]], input_->points[match_indices[3]]);
715 
716  // check edge distances of match w.r.t the base
717  return (std::abs (d0 - dist_ref[0]) < max_edge_diff_ && std::abs (d1 - dist_ref[1]) < max_edge_diff_ &&
718  std::abs (d2 - dist_ref[2]) < max_edge_diff_ && std::abs (d3 - dist_ref[3]) < max_edge_diff_) ? 0 : -1;
719 }
720 
721 
722 ///////////////////////////////////////////////////////////////////////////////////////////
723 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
725  const std::vector <int> &base_indices,
726  std::vector <std::vector <int> > &matches,
727  MatchingCandidates &candidates)
728 {
729  candidates.resize (1);
730  float fitness_score = FLT_MAX;
731 
732  // loop over all Candidate matches
733  for (std::vector <std::vector <int> >::iterator match_indices = matches.begin (), it_e = matches.end (); match_indices != it_e; match_indices++)
734  {
735  Eigen::Matrix4f transformation_temp;
736  pcl::Correspondences correspondences_temp;
737 
738  // determine corresondences between base and match according to their distance to centroid
739  linkMatchWithBase (base_indices, *match_indices, correspondences_temp);
740 
741  // check match based on residuals of the corresponding points after
742  if (validateMatch (base_indices, *match_indices, correspondences_temp, transformation_temp) < 0)
743  continue;
744 
745  // check resulting using a sub sample of the source point cloud and compare to previous matches
746  if (validateTransformation (transformation_temp, fitness_score) < 0)
747  continue;
748 
749  // store best match as well as associated fitness_score and transformation
750  candidates[0].fitness_score = fitness_score;
751  candidates [0].transformation = transformation_temp;
752  correspondences_temp.erase (correspondences_temp.end () - 1);
753  candidates[0].correspondences = correspondences_temp;
754  }
755 }
756 
757 
758 ///////////////////////////////////////////////////////////////////////////////////////////
759 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
761  const std::vector <int> &base_indices,
762  std::vector <int> &match_indices,
763  pcl::Correspondences &correspondences)
764 {
765  // calculate centroid of base and target
766  Eigen::Vector4f centre_base, centre_match;
767  pcl::compute3DCentroid (*target_, base_indices, centre_base);
768  pcl::compute3DCentroid (*input_, match_indices, centre_match);
769 
770  PointTarget centre_pt_base;
771  centre_pt_base.x = centre_base[0];
772  centre_pt_base.y = centre_base[1];
773  centre_pt_base.z = centre_base[2];
774 
775  PointSource centre_pt_match;
776  centre_pt_match.x = centre_match[0];
777  centre_pt_match.y = centre_match[1];
778  centre_pt_match.z = centre_match[2];
779 
780  // find corresponding points according to their distance to the centroid
781  std::vector <int> copy = match_indices;
782 
783  std::vector <int>::const_iterator it_base = base_indices.begin (), it_base_e = base_indices.end ();
784  std::vector <int>::iterator it_match, it_match_e = copy.end ();
785  std::vector <int>::iterator it_match_orig = match_indices.begin ();
786  for (; it_base != it_base_e; it_base++, it_match_orig++)
787  {
788  float dist_sqr_1 = pcl::squaredEuclideanDistance (target_->points[*it_base], centre_pt_base);
789  float best_diff_sqr = FLT_MAX;
790  int best_index = -1;
791 
792  for (it_match = copy.begin (); it_match != it_match_e; it_match++)
793  {
794  // calculate difference of distances to centre point
795  float dist_sqr_2 = pcl::squaredEuclideanDistance (input_->points[*it_match], centre_pt_match);
796  float diff_sqr = std::abs(dist_sqr_1 - dist_sqr_2);
797 
798  if (diff_sqr < best_diff_sqr)
799  {
800  best_diff_sqr = diff_sqr;
801  best_index = *it_match;
802  }
803  }
804 
805  // assign new correspondence and update indices of matched targets
806  correspondences.push_back (pcl::Correspondence (best_index, *it_base, best_diff_sqr));
807  *it_match_orig = best_index;
808  }
809 }
810 
811 
812 ///////////////////////////////////////////////////////////////////////////////////////////
813 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> int
815  const std::vector <int> &base_indices,
816  const std::vector <int> &match_indices,
817  const pcl::Correspondences &correspondences,
818  Eigen::Matrix4f &transformation)
819 {
820  // only use triplet of points to simlify process (possible due to planar case)
821  pcl::Correspondences correspondences_temp = correspondences;
822  correspondences_temp.erase (correspondences_temp.end () - 1);
823 
824  // estimate transformation between correspondence set
825  transformation_estimation_->estimateRigidTransformation (*input_, *target_, correspondences_temp, transformation);
826 
827  // transform base points
828  PointCloudSource match_transformed;
829  pcl::transformPointCloud (*input_, match_indices, match_transformed, transformation);
830 
831  // calculate residuals of transformation and check against maximum threshold
832  std::size_t nr_points = correspondences_temp.size ();
833  float mse = 0.f;
834  for (std::size_t i = 0; i < nr_points; i++)
835  mse += pcl::squaredEuclideanDistance (match_transformed.points [i], target_->points [base_indices[i]]);
836 
837  mse /= nr_points;
838  return (mse < max_mse_ ? 0 : -1);
839 }
840 
841 
842 ///////////////////////////////////////////////////////////////////////////////////////////
843 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> int
845  Eigen::Matrix4f &transformation,
846  float &fitness_score)
847 {
848  // transform source point cloud
849  PointCloudSource source_transformed;
850  pcl::transformPointCloud (*input_, *source_indices_, source_transformed, transformation);
851 
852  std::size_t nr_points = source_transformed.size ();
853  std::size_t terminate_value = fitness_score > 1 ? 0 : static_cast <std::size_t> ((1.f - fitness_score) * nr_points);
854 
855  float inlier_score_temp = 0;
856  std::vector <int> ids;
857  std::vector <float> dists_sqr;
858  PointCloudSourceIterator it = source_transformed.begin ();
859 
860  for (std::size_t i = 0; i < nr_points; it++, i++)
861  {
862  // search for nearest point using kd tree search
863  tree_->nearestKSearch (*it, 1, ids, dists_sqr);
864  inlier_score_temp += (dists_sqr[0] < max_inlier_dist_sqr_ ? 1 : 0);
865 
866  // early terminating
867  if (nr_points - i + inlier_score_temp < terminate_value)
868  break;
869  }
870 
871  // check current costs and return unsuccessfull if larger than previous ones
872  inlier_score_temp /= static_cast <float> (nr_points);
873  float fitness_score_temp = 1.f - inlier_score_temp;
874 
875  if (fitness_score_temp > fitness_score)
876  return (-1);
877 
878  fitness_score = fitness_score_temp;
879  return (0);
880 }
881 
882 
883 ///////////////////////////////////////////////////////////////////////////////////////////
884 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
886  const std::vector <MatchingCandidates > &candidates)
887 {
888  // get best fitness_score over all trys
889  int nr_candidates = static_cast <int> (candidates.size ());
890  int best_index = -1;
891  float best_score = FLT_MAX;
892  for (int i = 0; i < nr_candidates; i++)
893  {
894  const float &fitness_score = candidates [i][0].fitness_score;
895  if (fitness_score < best_score)
896  {
897  best_score = fitness_score;
898  best_index = i;
899  }
900  }
901 
902  // check if a valid candidate was available
903  if (!(best_index < 0))
904  {
905  fitness_score_ = candidates [best_index][0].fitness_score;
906  final_transformation_ = candidates [best_index][0].transformation;
907  *correspondences_ = candidates [best_index][0].correspondences;
908 
909  // here we define convergence if resulting fitness_score is below 1-threshold
910  converged_ = fitness_score_ < score_threshold_;
911  }
912 }
913 
914 ///////////////////////////////////////////////////////////////////////////////////////////
915 
916 #endif // PCL_REGISTRATION_IMPL_IA_4PCS_H_
A point structure representing normal coordinates and the surface curvature estimate.
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
std::vector< MatchingCandidate, Eigen::aligned_allocator< MatchingCandidate > > MatchingCandidates
FPCSInitialAlignment computes corresponding four point congruent sets as described in: "4-points cong...
Definition: ia_fpcs.h:77
boost::shared_ptr< std::vector< int > > IndicesPtr
Definition: pcl_base.h:60
iterator begin()
Definition: point_cloud.h:442
void setIndices(const boost::shared_ptr< std::vector< int > > &indices)
Provide a pointer to the vector of indices that represents the input data.
Definition: sac_model.h:327
float squaredEuclideanDistance(const PointType1 &p1, const PointType2 &p2)
Calculate the squared euclidean distance between the two given points.
Definition: distances.h:174
Correspondence represents a match between two entities (e.g., points, descriptors, etc).
float euclideanDistance(const PointType1 &p1, const PointType2 &p2)
Calculate the euclidean distance between the two given points.
Definition: distances.h:196
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
PointCloudSource::Ptr PointCloudSourcePtr
Definition: registration.h:83
double pointToPlaneDistance(const Point &p, double a, double b, double c, double d)
Get the distance from a point to a plane (unsigned) defined by ax+by+cz+d=0.
TransformationEstimation3Points represents the class for transformation estimation based on: ...
bool computeModelCoefficients(const std::vector< int > &samples, Eigen::VectorXf &model_coefficients)
Check whether the given index samples can form a valid plane model, compute the model coefficients fr...
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transform, bool copy_all_fields=true)
Apply an affine transform defined by an Eigen Transform.
Definition: transforms.hpp:42
PCL base class.
Definition: pcl_base.h:68
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
void getMinMax3D(const pcl::PointCloud< PointT > &cloud, PointT &min_pt, PointT &max_pt)
Get the minimum and maximum values on each of the 3 (x-y-z) dimensions in a given pointcloud...
Definition: common.hpp:228
float getMeanPointDensity(const typename pcl::PointCloud< PointT >::ConstPtr &cloud, float max_dist, int nr_threads=1)
Compute the mean point density of a given point cloud.
Definition: ia_fpcs.hpp:49
size_t size() const
Definition: point_cloud.h:448
double getTimeSeconds()
Retrieve the time in seconds spent since the last call to reset().
Definition: time.h:86
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr())
Provide a pointer to the input dataset.
Definition: kdtree.hpp:77
Simple stopwatch.
Definition: time.h:65
KdTreeReciprocal::Ptr KdTreeReciprocalPtr
Definition: registration.h:80
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:50
int nearestKSearch(const PointT &point, int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const
Search for the k-nearest neighbors for the given query point.
Definition: kdtree.hpp:88
SampleConsensusModelPlane defines a model for 3D plane segmentation.