00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * 00035 */ 00036 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_ 00037 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_ 00038 00039 template <typename PointT> 00040 inline void pcl::registration::CorrespondenceRejectorSampleConsensus<PointT>::applyRejection(pcl::registration::Correspondences &correspondences) 00041 { 00042 std::vector<int> source_indices; 00043 std::vector<int> target_indices; 00044 source_indices.resize(input_correspondences_->size()); 00045 target_indices.resize(input_correspondences_->size()); 00046 for (unsigned int i = 0; i < input_correspondences_->size(); ++i) 00047 { 00048 source_indices[i] = input_correspondences_->at(i).indexQuery; 00049 target_indices[i] = input_correspondences_->at(i).indexMatch; 00050 } 00051 00052 // from pcl/registration/icp.hpp: 00053 std::vector<int> source_indices_good; 00054 std::vector<int> target_indices_good; 00055 { 00056 // From the set of correspondences found, attempt to remove outliers 00057 // Create the registration model 00058 typedef typename pcl::SampleConsensusModelRegistration<PointT>::Ptr SampleConsensusModelRegistrationPtr; 00059 SampleConsensusModelRegistrationPtr model; 00060 model.reset (new pcl::SampleConsensusModelRegistration<PointT> (input_, source_indices)); 00061 // Pass the target_indices 00062 model->setInputTarget (target_, target_indices); 00063 // Create a RANSAC model 00064 pcl::RandomSampleConsensus<PointT> sac (model, inlier_threshold_); 00065 sac.setMaxIterations (max_iterations_); 00066 00067 // Compute the set of inliers 00068 if (!sac.computeModel ()) 00069 { 00070 correspondences = *input_correspondences_; 00071 best_transformation_.setIdentity(); 00072 } 00073 else 00074 { 00075 std::vector<int> inliers; 00076 sac.getInliers (inliers); 00077 correspondences.resize(inliers.size()); 00078 for (size_t i = 0; i < inliers.size (); ++i) 00079 correspondences[i] = input_correspondences_->at(inliers[i]); 00080 00081 // get best transformation 00082 Eigen::VectorXf model_coefficients; 00083 sac.getModelCoefficients(model_coefficients); 00084 best_transformation_.row (0) = model_coefficients.segment<4>(0); 00085 best_transformation_.row (1) = model_coefficients.segment<4>(4); 00086 best_transformation_.row (2) = model_coefficients.segment<4>(8); 00087 best_transformation_.row (3) = model_coefficients.segment<4>(12); 00088 } 00089 00090 00091 } 00092 } 00093 00094 template <typename PointT> 00095 void pcl::registration::CorrespondenceRejectorSampleConsensus<PointT>::getCorrespondences(const pcl::registration::Correspondences& original_correspondences, pcl::registration::Correspondences& remaining_correspondences) 00096 { 00097 std::vector<int> source_indices; 00098 std::vector<int> target_indices; 00099 source_indices.resize(original_correspondences.size()); 00100 target_indices.resize(original_correspondences.size()); 00101 for (unsigned int i = 0; i < original_correspondences.size(); ++i) 00102 { 00103 source_indices[i] = original_correspondences.at(i).indexQuery; 00104 target_indices[i] = original_correspondences.at(i).indexMatch; 00105 } 00106 00107 // from pcl/registration/icp.hpp: 00108 std::vector<int> source_indices_good; 00109 std::vector<int> target_indices_good; 00110 { 00111 // From the set of correspondences found, attempt to remove outliers 00112 // Create the registration model 00113 typedef typename pcl::SampleConsensusModelRegistration<PointT>::Ptr SampleConsensusModelRegistrationPtr; 00114 SampleConsensusModelRegistrationPtr model; 00115 model.reset (new pcl::SampleConsensusModelRegistration<PointT> (input_, source_indices)); 00116 // Pass the target_indices 00117 model->setInputTarget (target_, target_indices); 00118 // Create a RANSAC model 00119 pcl::RandomSampleConsensus<PointT> sac (model, inlier_threshold_); 00120 sac.setMaxIterations (max_iterations_); 00121 00122 // Compute the set of inliers 00123 if (!sac.computeModel ()) 00124 { 00125 remaining_correspondences = original_correspondences; 00126 best_transformation_.setIdentity(); 00127 } 00128 else 00129 { 00130 std::vector<int> inliers; 00131 sac.getInliers (inliers); 00132 remaining_correspondences.resize(inliers.size()); 00133 for (size_t i = 0; i < inliers.size (); ++i) 00134 remaining_correspondences[i] = original_correspondences.at(inliers[i]); 00135 00136 // get best transformation 00137 Eigen::VectorXf model_coefficients; 00138 sac.getModelCoefficients(model_coefficients); 00139 best_transformation_.row (0) = model_coefficients.segment<4>(0); 00140 best_transformation_.row (1) = model_coefficients.segment<4>(4); 00141 best_transformation_.row (2) = model_coefficients.segment<4>(8); 00142 best_transformation_.row (3) = model_coefficients.segment<4>(12); 00143 } 00144 00145 00146 } 00147 } 00148 00149 #endif /* PCL_REGISTRATION_IMPL_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_HPP_ */