Point Cloud Library (PCL)  1.11.0
ia_kfpcs.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  *
7  * All rights reserved
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the copyright holder(s) nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
37 #ifndef PCL_REGISTRATION_IMPL_IA_KFPCS_H_
38 #define PCL_REGISTRATION_IMPL_IA_KFPCS_H_
39 
40 
41 namespace pcl
42 {
43 
44 namespace registration
45 {
46 
47 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
49  lower_trl_boundary_ (-1.f),
50  upper_trl_boundary_ (-1.f),
51  lambda_ (0.5f),
52  use_trl_score_ (false),
53  indices_validation_ (new std::vector <int>)
54 {
55  reg_name_ = "pcl::registration::KFPCSInitialAlignment";
56 }
57 
58 
59 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> bool
61 {
62  // due to sparse keypoint cloud, do not normalize delta with estimated point density
63  if (normalize_delta_)
64  {
65  PCL_WARN ("[%s::initCompute] Delta should be set according to keypoint precision! Normalization according to point cloud density is ignored.", reg_name_.c_str ());
66  normalize_delta_ = false;
67  }
68 
69  // initialize as in fpcs
71 
72  // set the threshold values with respect to keypoint charactersitics
73  max_pair_diff_ = delta_ * 1.414f; // diff between 2 points of delta_ accuracy
74  coincidation_limit_ = delta_ * 2.828f; // diff between diff of 2 points
75  max_edge_diff_ = delta_ * 3.f; // diff between 2 points + some inaccuracy due to quadruple orientation
76  max_mse_ = powf (delta_ * 4.f, 2.f); // diff between 2 points + some registration inaccuracy
77  max_inlier_dist_sqr_ = powf (delta_ * 8.f, 2.f); // set rel. high, because MSAC is used (residual based score function)
78 
79  // check use of translation costs and calculate upper boundary if not set by user
80  if (upper_trl_boundary_ < 0)
81  upper_trl_boundary_ = diameter_ * (1.f - approx_overlap_) * 0.5f;
82 
83  if (!(lower_trl_boundary_ < 0) && upper_trl_boundary_ > lower_trl_boundary_)
84  use_trl_score_ = true;
85  else
86  lambda_ = 0.f;
87 
88  // generate a subset of indices of size ransac_iterations_ on which to evaluate candidates on
89  std::size_t nr_indices = indices_->size ();
90  if (nr_indices < std::size_t (ransac_iterations_))
91  indices_validation_ = indices_;
92  else
93  for (int i = 0; i < ransac_iterations_; i++)
94  indices_validation_->push_back ((*indices_)[rand () % nr_indices]);
95 
96  return (true);
97 }
98 
99 
100 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
102  const std::vector <int> &base_indices,
103  std::vector <std::vector <int> > &matches,
104  MatchingCandidates &candidates)
105 {
106  candidates.clear ();
107 
108  // loop over all Candidate matches
109  for (auto &match : matches)
110  {
111  Eigen::Matrix4f transformation_temp;
112  pcl::Correspondences correspondences_temp;
113  float fitness_score = FLT_MAX; // reset to FLT_MAX to accept all candidates and not only best
114 
115  // determine corresondences between base and match according to their distance to centroid
116  linkMatchWithBase (base_indices, match, correspondences_temp);
117 
118  // check match based on residuals of the corresponding points after transformation
119  if (validateMatch (base_indices, match, correspondences_temp, transformation_temp) < 0)
120  continue;
121 
122  // check resulting transformation using a sub sample of the source point cloud
123  // all candidates are stored and later sorted according to their fitness score
124  validateTransformation (transformation_temp, fitness_score);
125 
126  // store all valid match as well as associated score and transformation
127  candidates.push_back (MatchingCandidate (fitness_score, correspondences_temp, transformation_temp));
128  }
129 }
130 
131 
132 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> int
134  Eigen::Matrix4f &transformation,
135  float &fitness_score)
136 {
137  // transform sub sampled source cloud
138  PointCloudSource source_transformed;
139  pcl::transformPointCloud (*input_, *indices_validation_, source_transformed, transformation);
140 
141  const std::size_t nr_points = source_transformed.size ();
142  float score_a = 0.f, score_b = 0.f;
143 
144  // residual costs based on mse
145  std::vector <int> ids;
146  std::vector <float> dists_sqr;
147  for (PointCloudSourceIterator it = source_transformed.begin (), it_e = source_transformed.end (); it != it_e; ++it)
148  {
149  // search for nearest point using kd tree search
150  tree_->nearestKSearch (*it, 1, ids, dists_sqr);
151  score_a += (dists_sqr[0] < max_inlier_dist_sqr_ ? dists_sqr[0] : max_inlier_dist_sqr_); // MSAC
152  }
153 
154  score_a /= (max_inlier_dist_sqr_ * nr_points); // MSAC
155  //score_a = 1.f - (1.f - score_a) / (1.f - approx_overlap_); // make score relative to estimated overlap
156 
157  // translation score (solutions with small translation are down-voted)
158  float scale = 1.f;
159  if (use_trl_score_)
160  {
161  float trl = transformation.rightCols <1> ().head (3).norm ();
162  float trl_ratio = (trl - lower_trl_boundary_) / (upper_trl_boundary_ - lower_trl_boundary_);
163 
164  score_b = (trl_ratio < 0.f ? 1.f : (trl_ratio > 1.f ? 0.f : 0.5f * sin (M_PI * trl_ratio + M_PI_2) + 0.5f)); // sinusoidal costs
165  scale += lambda_;
166  }
167 
168  // calculate the fitness and return unsuccessful if smaller than previous ones
169  float fitness_score_temp = (score_a + lambda_ * score_b) / scale;
170  if (fitness_score_temp > fitness_score)
171  return (-1);
172 
173  fitness_score = fitness_score_temp;
174  return (0);
175 }
176 
177 
178 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
180  const std::vector <MatchingCandidates > &candidates)
181 {
182  // reorganize candidates into single vector
183  std::size_t total_size = 0;
184  for (const auto &candidate : candidates)
185  total_size += candidate.size ();
186 
187  candidates_.clear ();
188  candidates_.reserve (total_size);
189 
190  for (const auto &candidate : candidates)
191  for (const auto &match : candidate)
192  candidates_.push_back (match);
193 
194  // sort according to score value
195  std::sort (candidates_.begin (), candidates_.end (), by_score ());
196 
197  // return here if no score was valid, i.e. all scores are FLT_MAX
198  if (candidates_[0].fitness_score == FLT_MAX)
199  {
200  converged_ = false;
201  return;
202  }
203 
204  // save best candidate as output result
205  // note, all other candidates are accessible via getNBestCandidates () and getTBestCandidates ()
206  fitness_score_ = candidates_ [0].fitness_score;
207  final_transformation_ = candidates_ [0].transformation;
208  *correspondences_ = candidates_ [0].correspondences;
209 
210  // here we define convergence if resulting score is above threshold
211  converged_ = fitness_score_ < score_threshold_;
212 }
213 
214 
215 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
217  int n,
218  float min_angle3d,
219  float min_translation3d,
220  MatchingCandidates &candidates)
221 {
222  candidates.clear ();
223 
224  // loop over all candidates starting from the best one
225  for (MatchingCandidates::iterator it_candidate = candidates_.begin (), it_e = candidates_.end (); it_candidate != it_e; ++it_candidate)
226  {
227  // stop if current candidate has no valid score
228  if (it_candidate->fitness_score == FLT_MAX)
229  return;
230 
231  // check if current candidate is a unique one compared to previous using the min_diff threshold
232  bool unique = true;
233  MatchingCandidates::iterator it = candidates.begin (), it_e2 = candidates.end ();
234  while (unique && it != it_e2)
235  {
236  Eigen::Matrix4f diff = it_candidate->transformation.colPivHouseholderQr ().solve (it->transformation);
237  const float angle3d = Eigen::AngleAxisf (diff.block <3, 3> (0, 0)).angle ();
238  const float translation3d = diff.block <3, 1> (0, 3).norm ();
239  unique = angle3d > min_angle3d && translation3d > min_translation3d;
240  ++it;
241  }
242 
243  // add candidate to best candidates
244  if (unique)
245  candidates.push_back (*it_candidate);
246 
247  // stop if n candidates are reached
248  if (candidates.size () == n)
249  return;
250  }
251 }
252 
253 
254 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
256  float t,
257  float min_angle3d,
258  float min_translation3d,
259  MatchingCandidates &candidates)
260 {
261  candidates.clear ();
262 
263  // loop over all candidates starting from the best one
264  for (MatchingCandidates::iterator it_candidate = candidates_.begin (), it_e = candidates_.end (); it_candidate != it_e; ++it_candidate)
265  {
266  // stop if current candidate has score below threshold
267  if (it_candidate->fitness_score > t)
268  return;
269 
270  // check if current candidate is a unique one compared to previous using the min_diff threshold
271  bool unique = true;
272  MatchingCandidates::iterator it = candidates.begin (), it_e2 = candidates.end ();
273  while (unique && it != it_e2)
274  {
275  Eigen::Matrix4f diff = it_candidate->transformation.colPivHouseholderQr ().solve (it->transformation);
276  const float angle3d = Eigen::AngleAxisf (diff.block <3, 3> (0, 0)).angle ();
277  const float translation3d = diff.block <3, 1> (0, 3).norm ();
278  unique = angle3d > min_angle3d && translation3d > min_translation3d;
279  ++it;
280  }
281 
282  // add candidate to best candidates
283  if (unique)
284  candidates.push_back (*it_candidate);
285  }
286 }
287 
288 } // namespace registration
289 } // namespace pcl
290 
291 #endif // PCL_REGISTRATION_IMPL_IA_KFPCS_H_
292 
Sorting of candidates based on fitness score value.
FPCSInitialAlignment computes corresponding four point congruent sets as described in: "4-points cong...
Definition: ia_fpcs.h:76
iterator end()
Definition: point_cloud.h:443
Container for matching candidate consisting of.
std::vector< MatchingCandidate, Eigen::aligned_allocator< MatchingCandidate > > MatchingCandidates
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)
Apply an affine transform defined by an Eigen Transform.
Definition: transforms.hpp:221
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
iterator begin()
Definition: point_cloud.h:442
std::size_t size() const
Definition: point_cloud.h:448
KFPCSInitialAlignment computes corresponding four point congruent sets based on keypoints as describe...
Definition: ia_kfpcs.h:54