Point Cloud Library (PCL)  1.7.0
correspondence_rejection_sample_consensus.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, 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$
38  *
39  */
40 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_
41 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_
42 
43 #include <boost/unordered_map.hpp>
44 
45 ///////////////////////////////////////////////////////////////////////////////////////////
46 template <typename PointT> void
48  const typename pcl::registration::CorrespondenceRejectorSampleConsensus<PointT>::PointCloudConstPtr &cloud)
49 {
50  setInputSource (cloud);
51 }
52 
53 ///////////////////////////////////////////////////////////////////////////////////////////
54 template <typename PointT> typename pcl::registration::CorrespondenceRejectorSampleConsensus<PointT>::PointCloudConstPtr const
56 {
57  return (getInputSource ());
58 }
59 
60 ///////////////////////////////////////////////////////////////////////////////////////////
61 template <typename PointT> void
63  const typename pcl::registration::CorrespondenceRejectorSampleConsensus<PointT>::PointCloudConstPtr &cloud)
64 {
65  setInputTarget (cloud);
66 }
67 
68 ///////////////////////////////////////////////////////////////////////////////////////////
69 template <typename PointT> void
71  int max_iterations)
72 {
73  setMaximumIterations (max_iterations);
74 }
75 
76 ///////////////////////////////////////////////////////////////////////////////////////////
77 template <typename PointT> int
79 {
80  return (getMaximumIterations ());
81 }
82 
83 ///////////////////////////////////////////////////////////////////////////////////////////
84 template <typename PointT> void
86  const pcl::Correspondences& original_correspondences,
87  pcl::Correspondences& remaining_correspondences)
88 {
89  if (!input_)
90  {
91  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] No input cloud dataset was given!\n", getClassName ().c_str ());
92  return;
93  }
94 
95  if (!target_)
96  {
97  PCL_ERROR ("[pcl::registration::%s::getRemainingCorrespondences] No input target dataset was given!\n", getClassName ().c_str ());
98  return;
99  }
100 
101  int nr_correspondences = static_cast<int> (original_correspondences.size ());
102  std::vector<int> source_indices (nr_correspondences);
103  std::vector<int> target_indices (nr_correspondences);
104 
105  // Copy the query-match indices
106  for (size_t i = 0; i < original_correspondences.size (); ++i)
107  {
108  source_indices[i] = original_correspondences[i].index_query;
109  target_indices[i] = original_correspondences[i].index_match;
110  }
111 
112  // from pcl/registration/icp.hpp:
113  std::vector<int> source_indices_good;
114  std::vector<int> target_indices_good;
115  {
116  // From the set of correspondences found, attempt to remove outliers
117  // Create the registration model
118  typedef typename pcl::SampleConsensusModelRegistration<PointT>::Ptr SampleConsensusModelRegistrationPtr;
119  SampleConsensusModelRegistrationPtr model;
120  model.reset (new pcl::SampleConsensusModelRegistration<PointT> (input_, source_indices));
121  // Pass the target_indices
122  model->setInputTarget (target_, target_indices);
123  // Create a RANSAC model
124  pcl::RandomSampleConsensus<PointT> sac (model, inlier_threshold_);
125  sac.setMaxIterations (max_iterations_);
126 
127  // Compute the set of inliers
128  if (!sac.computeModel ())
129  {
130  remaining_correspondences = original_correspondences;
131  best_transformation_.setIdentity ();
132  return;
133  }
134  else
135  {
136  if (refine_ && !sac.refineModel ())
137  {
138  PCL_ERROR ("[pcl::registration::CorrespondenceRejectorSampleConsensus::getRemainingCorrespondences] Could not refine the model! Returning an empty solution.\n");
139  return;
140  }
141 
142  std::vector<int> inliers;
143  sac.getInliers (inliers);
144 
145  if (inliers.size () < 3)
146  {
147  remaining_correspondences = original_correspondences;
148  best_transformation_.setIdentity ();
149  return;
150  }
151  boost::unordered_map<int, int> index_to_correspondence;
152  for (int i = 0; i < nr_correspondences; ++i)
153  index_to_correspondence[original_correspondences[i].index_query] = i;
154 
155  remaining_correspondences.resize (inliers.size ());
156  for (size_t i = 0; i < inliers.size (); ++i)
157  remaining_correspondences[i] = original_correspondences[index_to_correspondence[inliers[i]]];
158 
159  // get best transformation
160  Eigen::VectorXf model_coefficients;
161  sac.getModelCoefficients (model_coefficients);
162  best_transformation_.row (0) = model_coefficients.segment<4>(0);
163  best_transformation_.row (1) = model_coefficients.segment<4>(4);
164  best_transformation_.row (2) = model_coefficients.segment<4>(8);
165  best_transformation_.row (3) = model_coefficients.segment<4>(12);
166  }
167  }
168 }
169 
170 #endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_