Point Cloud Library (PCL)  1.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ia_ransac.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2012, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  *
00037  */
00038 #ifndef IA_RANSAC_H_
00039 #define IA_RANSAC_H_
00040 
00041 #include "pcl/registration/registration.h"
00042 #include "pcl/registration/transformation_estimation_svd.h"
00043 
00044 namespace pcl
00045 {
00051   template <typename PointSource, typename PointTarget, typename FeatureT>
00052   class SampleConsensusInitialAlignment : public Registration<PointSource, PointTarget>
00053   {
00054     public:
00055       using Registration<PointSource, PointTarget>::reg_name_;
00056       using Registration<PointSource, PointTarget>::input_;
00057       using Registration<PointSource, PointTarget>::indices_;
00058       using Registration<PointSource, PointTarget>::target_;
00059       using Registration<PointSource, PointTarget>::final_transformation_;
00060       using Registration<PointSource, PointTarget>::transformation_;
00061       using Registration<PointSource, PointTarget>::corr_dist_threshold_;
00062       using Registration<PointSource, PointTarget>::min_number_correspondences_;
00063       using Registration<PointSource, PointTarget>::max_iterations_;
00064       using Registration<PointSource, PointTarget>::tree_;
00065       using Registration<PointSource, PointTarget>::transformation_estimation_;
00066       using Registration<PointSource, PointTarget>::getClassName;
00067 
00068       typedef typename Registration<PointSource, PointTarget>::PointCloudSource PointCloudSource;
00069       typedef typename PointCloudSource::Ptr PointCloudSourcePtr;
00070       typedef typename PointCloudSource::ConstPtr PointCloudSourceConstPtr;
00071 
00072       typedef typename Registration<PointSource, PointTarget>::PointCloudTarget PointCloudTarget;
00073 
00074       typedef PointIndices::Ptr PointIndicesPtr;
00075       typedef PointIndices::ConstPtr PointIndicesConstPtr;
00076 
00077       typedef pcl::PointCloud<FeatureT> FeatureCloud;
00078       typedef typename FeatureCloud::Ptr FeatureCloudPtr;
00079       typedef typename FeatureCloud::ConstPtr FeatureCloudConstPtr;
00080 
00081 
00082       class ErrorFunctor
00083       {
00084         public:
00085           virtual float operator () (float d) const = 0;
00086       };
00087 
00088       class HuberPenalty : public ErrorFunctor
00089       {
00090         private:
00091           HuberPenalty () {}
00092         public:
00093           HuberPenalty (float threshold)  : threshold_ (threshold) {}
00094           virtual float operator () (float e) const
00095           { 
00096             if (e <= threshold_)
00097               return (0.5 * e*e); 
00098             else
00099               return (0.5 * threshold_ * (2.0 * fabs (e) - threshold_));
00100           }
00101         protected:
00102           float threshold_;
00103       };
00104 
00105       class TruncatedError : public ErrorFunctor
00106       {
00107         private:
00108           TruncatedError () {}
00109         public:
00110           TruncatedError (float threshold) : threshold_ (threshold) {}
00111           virtual float operator () (float e) const
00112           { 
00113             if (e <= threshold_)
00114               return (e / threshold_);
00115             else
00116               return (1.0);
00117           }
00118         protected:
00119           float threshold_;
00120       };
00121 
00122       typedef typename KdTreeFLANN<FeatureT>::Ptr FeatureKdTreePtr; 
00124       SampleConsensusInitialAlignment () : nr_samples_(3), min_sample_distance_ (0), k_correspondences_ (10)
00125       {
00126         reg_name_ = "SampleConsensusInitialAlignment";
00127         feature_tree_.reset (new pcl::KdTreeFLANN<FeatureT>);
00128         max_iterations_ = 1000;
00129         transformation_estimation_.reset (new pcl::registration::TransformationEstimationSVD<PointSource, PointTarget>);
00130       };
00131 
00135       void 
00136       setSourceFeatures (const FeatureCloudConstPtr &features);
00137 
00139       inline FeatureCloudConstPtr const 
00140       getSourceFeatures () { return (input_features_); }
00141 
00145       void 
00146       setTargetFeatures (const FeatureCloudConstPtr &features);
00147 
00149       inline FeatureCloudConstPtr const 
00150       getTargetFeatures () { return (target_features_); }
00151 
00155       void 
00156       setMinSampleDistance (float min_sample_distance) { min_sample_distance_ = min_sample_distance; }
00157 
00159       float 
00160       getMinSampleDistance () { return (min_sample_distance_); }
00161 
00165       void 
00166       setNumberOfSamples (int nr_samples) { nr_samples_ = nr_samples; }
00167 
00169       int 
00170       getNumberOfSamples () { return (nr_samples_); }
00171 
00176       void
00177       setCorrespondenceRandomness (int k) { k_correspondences_ = k; }
00178 
00180       int
00181       getCorrespondenceRandomness () { return (k_correspondences_); }
00182 
00187       void
00188       setErrorFunction (const boost::shared_ptr<ErrorFunctor> & error_functor) { error_functor_ = error_functor; }
00189 
00193       boost::shared_ptr<ErrorFunctor>
00194       getErrorFunction () { return (error_functor_); }
00195 
00196     protected:
00200       inline int 
00201       getRandomIndex (int n) { return (n * (rand () / (RAND_MAX + 1.0))); };
00202       
00210       void 
00211       selectSamples (const PointCloudSource &cloud, int nr_samples, float min_sample_distance, 
00212                      std::vector<int> &sample_indices);
00213 
00221       void 
00222       findSimilarFeatures (const FeatureCloud &input_features, const std::vector<int> &sample_indices, 
00223                            std::vector<int> &corresponding_indices);
00224 
00229       float 
00230       computeErrorMetric (const PointCloudSource &cloud, float threshold);
00231 
00235       virtual void 
00236       computeTransformation (PointCloudSource &output, const Eigen::Matrix4f& guess);
00237 
00239       FeatureCloudConstPtr input_features_;
00240 
00242       FeatureCloudConstPtr target_features_;  
00243 
00245       int nr_samples_;
00246 
00248       float min_sample_distance_;
00249 
00251       int k_correspondences_;
00252      
00254       FeatureKdTreePtr feature_tree_;               
00255 
00257       boost::shared_ptr<ErrorFunctor> error_functor_;
00258     public:
00259       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00260   };
00261 }
00262 
00263 #include "pcl/registration/impl/ia_ransac.hpp"
00264 
00265 #endif  //#ifndef IA_RANSAC_H_