Point Cloud Library (PCL)  1.9.1-dev
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 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
43  lower_trl_boundary_ (-1.f),
44  upper_trl_boundary_ (-1.f),
45  lambda_ (0.5f),
46  use_trl_score_ (false),
47  indices_validation_ (new std::vector <int>)
48 {
49  reg_name_ = "pcl::registration::KFPCSInitialAlignment";
50 }
51 
52 
53 ///////////////////////////////////////////////////////////////////////////////////////////
54 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> bool
56 {
57  // due to sparse keypoint cloud, do not normalize delta with estimated point density
58  if (normalize_delta_)
59  {
60  PCL_WARN ("[%s::initCompute] Delta should be set according to keypoint precision! Normalization according to point cloud density is ignored.", reg_name_.c_str ());
61  normalize_delta_ = false;
62  }
63 
64  // initialize as in fpcs
66 
67  // set the threshold values with respect to keypoint charactersitics
68  max_pair_diff_ = delta_ * 1.414f; // diff between 2 points of delta_ accuracy
69  coincidation_limit_ = delta_ * 2.828f; // diff between diff of 2 points
70  max_edge_diff_ = delta_ * 3.f; // diff between 2 points + some inaccuracy due to quadruple orientation
71  max_mse_ = powf (delta_ * 4.f, 2.f); // diff between 2 points + some registration inaccuracy
72  max_inlier_dist_sqr_ = powf (delta_ * 8.f, 2.f); // set rel. high, because MSAC is used (residual based score function)
73 
74  // check use of translation costs and calculate upper boundary if not set by user
75  if (upper_trl_boundary_ < 0)
76  upper_trl_boundary_ = diameter_ * (1.f - approx_overlap_) * 0.5f;
77 
78  if (!(lower_trl_boundary_ < 0) && upper_trl_boundary_ > lower_trl_boundary_)
79  use_trl_score_ = true;
80  else
81  lambda_ = 0.f;
82 
83  // generate a subset of indices of size ransac_iterations_ on which to evaluate candidates on
84  std::size_t nr_indices = indices_->size ();
85  if (nr_indices < size_t (ransac_iterations_))
86  indices_validation_ = indices_;
87  else
88  for (int i = 0; i < ransac_iterations_; i++)
89  indices_validation_->push_back ((*indices_)[rand () % nr_indices]);
90 
91  return (true);
92 }
93 
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////
96 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
98  const std::vector <int> &base_indices,
99  std::vector <std::vector <int> > &matches,
100  MatchingCandidates &candidates)
101 {
102  candidates.clear ();
103  float fitness_score = FLT_MAX;
104 
105  // loop over all Candidate matches
106  for (auto &match : matches)
107  {
108  Eigen::Matrix4f transformation_temp;
109  pcl::Correspondences correspondences_temp;
110  fitness_score = FLT_MAX; // reset to FLT_MAX to accept all candidates and not only best
111 
112  // determine corresondences between base and match according to their distance to centroid
113  linkMatchWithBase (base_indices, match, correspondences_temp);
114 
115  // check match based on residuals of the corresponding points after transformation
116  if (validateMatch (base_indices, match, correspondences_temp, transformation_temp) < 0)
117  continue;
118 
119  // check resulting transformation using a sub sample of the source point cloud
120  // all candidates are stored and later sorted according to their fitness score
121  validateTransformation (transformation_temp, fitness_score);
122 
123  // store all valid match as well as associated score and transformation
124  candidates.push_back (MatchingCandidate (fitness_score, correspondences_temp, transformation_temp));
125  }
126 }
127 
128 
129 ///////////////////////////////////////////////////////////////////////////////////////////
130 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> int
132  Eigen::Matrix4f &transformation,
133  float &fitness_score)
134 {
135  // transform sub sampled source cloud
136  PointCloudSource source_transformed;
137  pcl::transformPointCloud (*input_, *indices_validation_, source_transformed, transformation);
138 
139  const std::size_t nr_points = source_transformed.size ();
140  float score_a = 0.f, score_b = 0.f;
141 
142  // residual costs based on mse
143  std::vector <int> ids;
144  std::vector <float> dists_sqr;
145  for (PointCloudSourceIterator it = source_transformed.begin (), it_e = source_transformed.end (); it != it_e; ++it)
146  {
147  // search for nearest point using kd tree search
148  tree_->nearestKSearch (*it, 1, ids, dists_sqr);
149  score_a += (dists_sqr[0] < max_inlier_dist_sqr_ ? dists_sqr[0] : max_inlier_dist_sqr_); // MSAC
150  }
151 
152  score_a /= (max_inlier_dist_sqr_ * nr_points); // MSAC
153  //score_a = 1.f - (1.f - score_a) / (1.f - approx_overlap_); // make score relative to estimated overlap
154 
155  // translation score (solutions with small translation are down-voted)
156  float scale = 1.f;
157  if (use_trl_score_)
158  {
159  float trl = transformation.rightCols <1> ().head (3).norm ();
160  float trl_ratio = (trl - lower_trl_boundary_) / (upper_trl_boundary_ - lower_trl_boundary_);
161 
162  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
163  scale += lambda_;
164  }
165 
166  // calculate the fitness and return unsuccessful if smaller than previous ones
167  float fitness_score_temp = (score_a + lambda_ * score_b) / scale;
168  if (fitness_score_temp > fitness_score)
169  return (-1);
170 
171  fitness_score = fitness_score_temp;
172  return (0);
173 }
174 
175 
176 ///////////////////////////////////////////////////////////////////////////////////////////
177 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
179  const std::vector <MatchingCandidates > &candidates)
180 {
181  // reorganize candidates into single vector
182  size_t total_size = 0;
183  for (const auto &candidate : candidates)
184  total_size += candidate.size ();
185 
186  candidates_.clear ();
187  candidates_.reserve (total_size);
188 
189  for (const auto &candidate : candidates)
190  for (const auto &match : candidate)
191  candidates_.push_back (match);
192 
193  // sort according to score value
194  std::sort (candidates_.begin (), candidates_.end (), by_score ());
195 
196  // return here if no score was valid, i.e. all scores are FLT_MAX
197  if (candidates_[0].fitness_score == FLT_MAX)
198  {
199  converged_ = false;
200  return;
201  }
202 
203  // save best candidate as output result
204  // note, all other candidates are accessible via getNBestCandidates () and getTBestCandidates ()
205  fitness_score_ = candidates_ [0].fitness_score;
206  final_transformation_ = candidates_ [0].transformation;
207  *correspondences_ = candidates_ [0].correspondences;
208 
209  // here we define convergence if resulting score is above threshold
210  converged_ = fitness_score_ < score_threshold_;
211 }
212 
213 ///////////////////////////////////////////////////////////////////////////////////////////
214 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
216  int n,
217  float min_angle3d,
218  float min_translation3d,
219  MatchingCandidates &candidates)
220 {
221  candidates.clear ();
222 
223  // loop over all candidates starting from the best one
224  for (MatchingCandidates::iterator it_candidate = candidates_.begin (), it_e = candidates_.end (); it_candidate != it_e; it_candidate++)
225  {
226  // stop if current candidate has no valid score
227  if (it_candidate->fitness_score == FLT_MAX)
228  return;
229 
230  // check if current candidate is a unique one compared to previous using the min_diff threshold
231  bool unique = true;
232  MatchingCandidates::iterator it = candidates.begin (), it_e2 = candidates.end ();
233  while (unique && it != it_e2)
234  {
235  Eigen::Matrix4f diff = it_candidate->transformation.colPivHouseholderQr ().solve (it->transformation);
236  const float angle3d = Eigen::AngleAxisf (diff.block <3, 3> (0, 0)).angle ();
237  const float translation3d = diff.block <3, 1> (0, 3).norm ();
238  unique = angle3d > min_angle3d && translation3d > min_translation3d;
239  it++;
240  }
241 
242  // add candidate to best candidates
243  if (unique)
244  candidates.push_back (*it_candidate);
245 
246  // stop if n candidates are reached
247  if (candidates.size () == n)
248  return;
249  }
250 }
251 
252 ///////////////////////////////////////////////////////////////////////////////////////////
253 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
255  float t,
256  float min_angle3d,
257  float min_translation3d,
258  MatchingCandidates &candidates)
259 {
260  candidates.clear ();
261 
262  // loop over all candidates starting from the best one
263  for (MatchingCandidates::iterator it_candidate = candidates_.begin (), it_e = candidates_.end (); it_candidate != it_e; it_candidate++)
264  {
265  // stop if current candidate has score below threshold
266  if (it_candidate->fitness_score > t)
267  return;
268 
269  // check if current candidate is a unique one compared to previous using the min_diff threshold
270  bool unique = true;
271  MatchingCandidates::iterator it = candidates.begin (), it_e2 = candidates.end ();
272  while (unique && it != it_e2)
273  {
274  Eigen::Matrix4f diff = it_candidate->transformation.colPivHouseholderQr ().solve (it->transformation);
275  const float angle3d = Eigen::AngleAxisf (diff.block <3, 3> (0, 0)).angle ();
276  const float translation3d = diff.block <3, 1> (0, 3).norm ();
277  unique = angle3d > min_angle3d && translation3d > min_translation3d;
278  it++;
279  }
280 
281  // add candidate to best candidates
282  if (unique)
283  candidates.push_back (*it_candidate);
284  }
285 }
286 
287 ///////////////////////////////////////////////////////////////////////////////////////////
288 
289 #endif // PCL_REGISTRATION_IMPL_IA_KFPCS_H_
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:456
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=true)
Apply an affine transform defined by an Eigen Transform.
Definition: transforms.hpp:215
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
iterator begin()
Definition: point_cloud.h:455
KFPCSInitialAlignment computes corresponding four point congruent sets based on keypoints as describe...
Definition: ia_kfpcs.h:54
size_t size() const
Definition: point_cloud.h:461