38 #ifndef PCL_REGISTRATION_IMPL_IA_FPCS_H_
39 #define PCL_REGISTRATION_IMPL_IA_FPCS_H_
41 #include <pcl/registration/ia_fpcs.h>
44 #include <pcl/sample_consensus/sac_model_plane.h>
45 #include <pcl/registration/transformation_estimation_3point.h>
48 template <
typename Po
intT>
inline float
51 const float max_dist_sqr = max_dist * max_dist;
52 const std::size_t s = cloud.
size ();
57 float mean_dist = 0.f;
59 std::vector <int> ids (2);
60 std::vector <float> dists_sqr (2);
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)
69 for (
int i = 0; i < 1000; i++)
72 if (dists_sqr[1] < max_dist_sqr)
74 mean_dist += std::sqrt (dists_sqr[1]);
79 return (mean_dist / num);
84 template <
typename Po
intT>
inline float
86 float max_dist,
int nr_threads)
88 const float max_dist_sqr = max_dist * max_dist;
89 const std::size_t s = indices.size ();
94 float mean_dist = 0.f;
96 std::vector <int> ids (2);
97 std::vector <float> dists_sqr (2);
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)
106 for (
int i = 0; i < 1000; i++)
109 if (dists_sqr[1] < max_dist_sqr)
111 mean_dist += std::sqrt (dists_sqr[1]);
116 return (mean_dist / num);
121 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
126 approx_overlap_ (0.5f),
128 score_threshold_ (FLT_MAX),
130 max_norm_diff_ (90.f),
132 fitness_score_ (FLT_MAX),
134 max_base_diameter_sqr_ (),
135 use_normals_ (
false),
136 normalize_delta_ (
true),
139 coincidation_limit_ (),
141 max_inlier_dist_sqr_ (),
142 small_error_ (0.00001f)
144 reg_name_ =
"pcl::registration::FPCSInitialAlignment";
146 ransac_iterations_ = 1000;
152 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
void
155 const Eigen::Matrix4f &guess)
160 final_transformation_ = guess;
162 std::vector <MatchingCandidates> all_candidates (max_iterations_);
166 #pragma omp parallel num_threads (nr_threads_)
170 std::srand (
static_cast <unsigned int> (std::time (NULL)) ^ omp_get_thread_num ());
171 #pragma omp for schedule (dynamic)
173 for (
int i = 0; i < max_iterations_; i++)
177 #pragma omp flush (abort)
181 std::vector <int> base_indices (4);
183 all_candidates[i] = candidates;
188 if (selectBase (base_indices, ratio) == 0)
192 if (bruteForceCorrespondences (base_indices[0], base_indices[1], pairs_a) == 0 &&
193 bruteForceCorrespondences (base_indices[2], base_indices[3], pairs_b) == 0)
196 std::vector <std::vector <int> > matches;
197 if (determineBaseMatches (base_indices, matches, pairs_a, pairs_b, ratio) == 0)
200 handleMatches (base_indices, matches, candidates);
201 if (candidates.size () != 0)
202 all_candidates[i] = candidates;
208 abort = (candidates.size () > 0 ? candidates[0].fitness_score < score_threshold_ : abort);
213 #pragma omp flush (abort)
221 finalCompute (all_candidates);
231 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
bool
234 std::srand (
static_cast <unsigned int> (std::time (NULL)));
241 if (!input_ || !target_)
243 PCL_ERROR (
"[%s::initCompute] Source or target dataset not given!\n", reg_name_.c_str ());
247 if (!target_indices_ || target_indices_->size () == 0)
249 target_indices_.reset (
new std::vector <int> (
static_cast <int> (target_->size ())));
251 for (std::vector <int>::iterator it = target_indices_->begin (), it_e = target_indices_->end (); it != it_e; it++)
253 target_cloud_updated_ =
true;
257 if (nr_samples_ != 0)
259 const int ss =
static_cast <int> (indices_->size ());
260 const int sample_fraction_src = std::max (1,
static_cast <int> (ss / nr_samples_));
263 for (
int i = 0; i < ss; i++)
264 if (rand () % sample_fraction_src == 0)
265 source_indices_->push_back ((*indices_) [i]);
268 source_indices_ = indices_;
271 if (source_normals_ && target_normals_ && source_normals_->size () == input_->size () && target_normals_->size () == target_->size ())
275 if (target_cloud_updated_)
277 tree_->setInputCloud (target_, target_indices_);
278 target_cloud_updated_ =
false;
282 const int min_iterations = 4;
283 const float diameter_fraction = 0.3f;
286 Eigen::Vector4f pt_min, pt_max;
288 diameter_ = (pt_max - pt_min).norm ();
291 float max_base_diameter = diameter_* approx_overlap_ * 2.f;
292 max_base_diameter_sqr_ = max_base_diameter * max_base_diameter;
295 if (normalize_delta_)
297 float mean_dist = getMeanPointDensity <PointTarget> (target_, *target_indices_, 0.05f * diameter_, nr_threads_);
302 if (max_iterations_ == 0)
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));
309 if (score_threshold_ == FLT_MAX)
310 score_threshold_ = 1.f - approx_overlap_;
312 if (max_iterations_ < 4)
315 if (max_runtime_ < 1)
316 max_runtime_ = INT_MAX;
319 max_pair_diff_ = delta_ * 2.f;
320 max_edge_diff_ = delta_ * 4.f;
321 coincidation_limit_ = delta_ * 2.f;
322 max_mse_ = powf (delta_* 2.f, 2.f);
323 max_inlier_dist_sqr_ = powf (delta_ * 2.f, 2.f);
326 fitness_score_ = FLT_MAX;
333 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
int
335 std::vector <int> &base_indices,
338 const float too_close_sqr = max_base_diameter_sqr_*0.01;
340 Eigen::VectorXf coefficients (4);
343 Eigen::Vector4f centre_pt;
344 float nearest_to_plane = FLT_MAX;
347 for (
int i = 0; i < ransac_iterations_; i++)
350 if (selectBaseTriangle (base_indices) < 0)
353 std::vector <int> base_triple (base_indices.begin (), base_indices.end () - 1);
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]]);
362 for (std::vector <int>::iterator it = target_indices_->begin (), it_e = target_indices_->end (); it != it_e; it++)
364 const PointTarget *pt4 = &(target_->points[*it]);
369 float d4 = (pt4->getVector3fMap () - centre_pt.head (3)).squaredNorm ();
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_)
378 if (dist_to_plane < nearest_to_plane)
380 base_indices[3] = *it;
381 nearest_to_plane = dist_to_plane;
386 if (nearest_to_plane != FLT_MAX)
389 setupBase (base_indices, ratio);
400 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
int
403 int nr_points =
static_cast <int> (target_indices_->size ());
407 base_indices[0] = (*target_indices_)[rand () % nr_points];
408 int *index1 = &base_indices[0];
411 for (
int i = 0; i < ransac_iterations_; i++)
413 int *index2 = &(*target_indices_)[rand () % nr_points];
414 int *index3 = &(*target_indices_)[rand () % nr_points];
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 ();
421 if (t > best_t && u.squaredNorm () < max_base_diameter_sqr_ && v.squaredNorm () < max_base_diameter_sqr_)
424 base_indices[1] = *index2;
425 base_indices[2] = *index3;
430 return (best_t == 0.f ? -1 : 0);
435 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
void
437 std::vector <int> &base_indices,
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 ());
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++)
451 for (std::vector <int>::const_iterator k = copy.begin (), k_e = copy.end (); k != k_e; k++)
453 if (k == j || k == i)
456 std::vector <int>::const_iterator l = copy.begin ();
457 while (l == i || l == j || l == k)
467 float t = segmentToSegmentDist (temp, ratio_temp);
471 ratio[0] = ratio_temp[0];
472 ratio[1] = ratio_temp[1];
481 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
float
483 const std::vector <int> &base_indices,
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 ();
497 float D = a * c - b * b;
498 float sN = 0.f, sD = D;
499 float tN = 0.f, tD = D;
502 if (D < small_error_)
511 sN = (b * e - c * d);
512 tN = (a * e - b * d);
552 else if ((-d + b) > a)
563 ratio[0] = (std::abs (sN) < small_error_) ? 0.f : sN / sD;
564 ratio[1] = (std::abs (tN) < small_error_) ? 0.f : tN / tD;
566 Eigen::Vector3f x = w + (ratio[0] * u) - (ratio[1] * v);
572 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
int
578 const float max_norm_diff = 0.5f * max_norm_diff_ * M_PI / 180.f;
582 float ref_norm_angle = (use_normals_ ? (target_normals_->points[idx1].getNormalVector3fMap () -
583 target_normals_->points[idx2].getNormalVector3fMap ()).norm () : 0.f);
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++)
591 const PointSource *pt1 = &(*input_)[*it_out];
592 for ( ; it_in != it_in_e; it_in++)
594 const PointSource *pt2 = &(*input_)[*it_in];
598 if (std::abs(dist - ref_dist) < max_pair_diff_)
603 const NormalT *pt1_n = &(source_normals_->points[*it_out]);
604 const NormalT *pt2_n = &(source_normals_->points[*it_in]);
606 float norm_angle_1 = (pt1_n->getNormalVector3fMap () - pt2_n->getNormalVector3fMap ()).norm ();
607 float norm_angle_2 = (pt1_n->getNormalVector3fMap () + pt2_n->getNormalVector3fMap ()).norm ();
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)
621 return (pairs.size () == 0 ? -1 : 0);
626 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
int
628 const std::vector <int> &base_indices,
629 std::vector <std::vector <int> > &matches,
632 const float (&ratio)[2])
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]]);
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++)
647 const PointSource *pt1 = &(input_->points[it_pair->index_match]);
648 const PointSource *pt2 = &(input_->points[it_pair->index_query]);
651 for (
int i = 0; i < 2; i++, it_pt++)
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);
661 tree_e->setInputCloud (cloud_e);
663 std::vector <int> ids;
664 std::vector <float> dists_sqr;
667 for (pcl::Correspondences::const_iterator it_pair = pairs_b.begin (), it_pair_e = pairs_b.end () ; it_pair != it_pair_e; it_pair++)
669 const PointTarget *pt1 = &(input_->points[it_pair->index_match]);
670 const PointTarget *pt2 = &(input_->points[it_pair->index_query]);
673 for (
int i = 0; i < 2; i++)
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);
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++)
684 std::vector <int> match_indices (4);
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;
692 if (checkBaseMatch (match_indices, dist_base) < 0)
695 matches.push_back (match_indices);
701 return (matches.size () > 0 ? 0 : -1);
706 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
int
708 const std::vector <int> &match_indices,
709 const float (&dist_ref)[4])
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;
723 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
void
725 const std::vector <int> &base_indices,
726 std::vector <std::vector <int> > &matches,
729 candidates.resize (1);
730 float fitness_score = FLT_MAX;
733 for (std::vector <std::vector <int> >::iterator match_indices = matches.begin (), it_e = matches.end (); match_indices != it_e; match_indices++)
735 Eigen::Matrix4f transformation_temp;
739 linkMatchWithBase (base_indices, *match_indices, correspondences_temp);
742 if (validateMatch (base_indices, *match_indices, correspondences_temp, transformation_temp) < 0)
746 if (validateTransformation (transformation_temp, fitness_score) < 0)
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;
759 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
void
761 const std::vector <int> &base_indices,
762 std::vector <int> &match_indices,
766 Eigen::Vector4f centre_base, centre_match;
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];
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];
781 std::vector <int> copy = match_indices;
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++)
789 float best_diff_sqr = FLT_MAX;
792 for (it_match = copy.begin (); it_match != it_match_e; it_match++)
796 float diff_sqr = std::abs(dist_sqr_1 - dist_sqr_2);
798 if (diff_sqr < best_diff_sqr)
800 best_diff_sqr = diff_sqr;
801 best_index = *it_match;
807 *it_match_orig = best_index;
813 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
int
815 const std::vector <int> &base_indices,
816 const std::vector <int> &match_indices,
818 Eigen::Matrix4f &transformation)
822 correspondences_temp.erase (correspondences_temp.end () - 1);
825 transformation_estimation_->estimateRigidTransformation (*input_, *target_, correspondences_temp, transformation);
832 std::size_t nr_points = correspondences_temp.size ();
834 for (std::size_t i = 0; i < nr_points; i++)
838 return (mse < max_mse_ ? 0 : -1);
843 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
int
845 Eigen::Matrix4f &transformation,
846 float &fitness_score)
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);
855 float inlier_score_temp = 0;
856 std::vector <int> ids;
857 std::vector <float> dists_sqr;
858 PointCloudSourceIterator it = source_transformed.
begin ();
860 for (std::size_t i = 0; i < nr_points; it++, i++)
863 tree_->nearestKSearch (*it, 1, ids, dists_sqr);
864 inlier_score_temp += (dists_sqr[0] < max_inlier_dist_sqr_ ? 1 : 0);
867 if (nr_points - i + inlier_score_temp < terminate_value)
872 inlier_score_temp /=
static_cast <float> (nr_points);
873 float fitness_score_temp = 1.f - inlier_score_temp;
875 if (fitness_score_temp > fitness_score)
878 fitness_score = fitness_score_temp;
884 template <
typename Po
intSource,
typename Po
intTarget,
typename NormalT,
typename Scalar>
void
886 const std::vector <MatchingCandidates > &candidates)
889 int nr_candidates =
static_cast <int> (candidates.size ());
891 float best_score = FLT_MAX;
892 for (
int i = 0; i < nr_candidates; i++)
894 const float &fitness_score = candidates [i][0].fitness_score;
895 if (fitness_score < best_score)
897 best_score = fitness_score;
903 if (!(best_index < 0))
905 fitness_score_ = candidates [best_index][0].fitness_score;
906 final_transformation_ = candidates [best_index][0].transformation;
907 *correspondences_ = candidates [best_index][0].correspondences;
910 converged_ = fitness_score_ < score_threshold_;
916 #endif // PCL_REGISTRATION_IMPL_IA_4PCS_H_