Point Cloud Library (PCL)  1.7.0
/tmp/buildd/pcl-1.7-1.7.0/registration/include/pcl/registration/correspondence_rejection_var_trimmed.h
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2012, Open Perception, 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 Open Perception, 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  * $Id$
00037  *
00038  */
00039 #ifndef PCL_REGISTRATION_CORRESPONDENCE_REJECTION_VAR_TRIMMED_H_
00040 #define PCL_REGISTRATION_CORRESPONDENCE_REJECTION_VAR_TRIMMED_H_
00041 
00042 #include <pcl/registration/correspondence_rejection.h>
00043 #include <pcl/point_cloud.h>
00044 
00045 #include <vector>
00046 
00047 namespace pcl
00048 {
00049   namespace registration
00050   {
00051     /**
00052       * @b CorrespondenceRejectoVarTrimmed implements a simple correspondence
00053       * rejection method by considering as inliers a certain percentage of correspondences 
00054       * with the least distances. The percentage of inliers is computed internally as mentioned
00055       * in the paper 'Outlier Robust ICP for minimizing Fractional RMSD, J. M. Philips et al'
00056       *
00057       * \note If \ref setInputCloud and \ref setInputTarget are given, then the
00058       * distances between correspondences will be estimated using the given XYZ
00059       * data, and not read from the set of input correspondences.
00060       *
00061       * \author Aravindhan K Krishnan. This code is ported from libpointmatcher (https://github.com/ethz-asl/libpointmatcher)
00062       * \ingroup registration
00063       */
00064     class PCL_EXPORTS CorrespondenceRejectorVarTrimmed: public CorrespondenceRejector
00065     {
00066       using CorrespondenceRejector::input_correspondences_;
00067       using CorrespondenceRejector::rejection_name_;
00068       using CorrespondenceRejector::getClassName;
00069 
00070       public:
00071         typedef boost::shared_ptr<CorrespondenceRejectorVarTrimmed> Ptr;
00072         typedef boost::shared_ptr<const CorrespondenceRejectorVarTrimmed> ConstPtr;
00073 
00074         /** \brief Empty constructor. */
00075         CorrespondenceRejectorVarTrimmed () : 
00076           trimmed_distance_ (0), 
00077           factor_ (),
00078           min_ratio_ (0.05),
00079           max_ratio_ (0.95),
00080           lambda_ (0.95),
00081           data_container_ ()
00082         {
00083           rejection_name_ = "CorrespondenceRejectorVarTrimmed";
00084         }
00085 
00086         /** \brief Get a list of valid correspondences after rejection from the original set of correspondences.
00087           * \param[in] original_correspondences the set of initial correspondences given
00088           * \param[out] remaining_correspondences the resultant filtered set of remaining correspondences
00089           */
00090         void 
00091         getRemainingCorrespondences (const pcl::Correspondences& original_correspondences, 
00092                                      pcl::Correspondences& remaining_correspondences);
00093 
00094         /** \brief Get the trimmed distance used for thresholding in correspondence rejection. */
00095         inline double
00096         getTrimmedDistance () const { return trimmed_distance_; };
00097 
00098         /** \brief Provide a source point cloud dataset (must contain XYZ
00099           * data!), used to compute the correspondence distance.  
00100           * \param[in] cloud a cloud containing XYZ data
00101           */
00102         template <typename PointT> inline void 
00103         setInputSource (const typename pcl::PointCloud<PointT>::ConstPtr &cloud)
00104         {
00105           if (!data_container_)
00106             data_container_.reset (new DataContainer<PointT>);
00107           boost::static_pointer_cast<DataContainer<PointT> > (data_container_)->setInputSource (cloud);
00108         }
00109 
00110         /** \brief Provide a source point cloud dataset (must contain XYZ
00111           * data!), used to compute the correspondence distance.  
00112           * \param[in] cloud a cloud containing XYZ data
00113           */
00114         template <typename PointT> inline void 
00115         setInputCloud (const typename pcl::PointCloud<PointT>::ConstPtr &cloud)
00116         {
00117           PCL_WARN ("[pcl::registration::%s::setInputCloud] setInputCloud is deprecated. Please use setInputSource instead.\n", getClassName ().c_str ());
00118           if (!data_container_)
00119             data_container_.reset (new DataContainer<PointT>);
00120           boost::static_pointer_cast<DataContainer<PointT> > (data_container_)->setInputSource (cloud);
00121         }
00122 
00123         /** \brief Provide a target point cloud dataset (must contain XYZ
00124           * data!), used to compute the correspondence distance.  
00125           * \param[in] target a cloud containing XYZ data
00126           */
00127         template <typename PointT> inline void 
00128         setInputTarget (const typename pcl::PointCloud<PointT>::ConstPtr &target)
00129         {
00130           if (!data_container_)
00131             data_container_.reset (new DataContainer<PointT>);
00132           boost::static_pointer_cast<DataContainer<PointT> > (data_container_)->setInputTarget (target);
00133         }
00134         
00135         /** \brief Provide a pointer to the search object used to find correspondences in
00136           * the target cloud.
00137           * \param[in] tree a pointer to the spatial search object.
00138           * \param[in] force_no_recompute If set to true, this tree will NEVER be 
00139           * recomputed, regardless of calls to setInputTarget. Only use if you are 
00140           * confident that the tree will be set correctly.
00141           */
00142         template <typename PointT> inline void
00143         setSearchMethodTarget (const boost::shared_ptr<pcl::search::KdTree<PointT> > &tree, 
00144                                bool force_no_recompute = false) 
00145         { 
00146           boost::static_pointer_cast< DataContainer<PointT> > 
00147             (data_container_)->setSearchMethodTarget (tree, force_no_recompute );
00148         }
00149 
00150         /** \brief Get the computed inlier ratio used for thresholding in correspondence rejection. */
00151         inline double
00152         getTrimFactor () const { return factor_; }
00153 
00154         /** brief set the minimum overlap ratio
00155           * \param[in] ratio the overlap ratio [0..1]
00156           */
00157         inline void
00158         setMinRatio (double ratio) { min_ratio_ = ratio; }
00159 
00160         /** brief get the minimum overlap ratio
00161           */
00162         inline double
00163         getMinRatio () const { return min_ratio_; }
00164 
00165         /** brief set the maximum overlap ratio
00166           * \param[in] ratio the overlap ratio [0..1]
00167           */
00168         inline void
00169         setMaxRatio (double ratio) { max_ratio_ = ratio; }
00170 
00171         /** brief get the maximum overlap ratio
00172           */
00173         inline double
00174         getMaxRatio () const { return max_ratio_; }
00175 
00176       protected:
00177 
00178         /** \brief Apply the rejection algorithm.
00179           * \param[out] correspondences the set of resultant correspondences.
00180           */
00181         inline void 
00182         applyRejection (pcl::Correspondences &correspondences)
00183         {
00184           getRemainingCorrespondences (*input_correspondences_, correspondences);
00185         }
00186 
00187         /** \brief The inlier distance threshold (based on the computed trim factor) between two correspondent points in source <-> target.
00188           */
00189         double trimmed_distance_;
00190 
00191         /** \brief The factor for correspondence rejection. Only factor times the total points sorted based on 
00192          *  the correspondence distances will be considered as inliers. Remaining points are rejected. This factor is
00193          *  computed internally 
00194          */
00195         double factor_;
00196 
00197         /** \brief The minimum overlap ratio between the input and target clouds
00198          */
00199         double min_ratio_;
00200 
00201         /** \brief The maximum overlap ratio between the input and target clouds
00202          */
00203         double max_ratio_;
00204 
00205        /** \brief part of the term that balances the root mean square difference. This is an internal parameter
00206          */
00207         double lambda_;
00208 
00209         typedef boost::shared_ptr<DataContainerInterface> DataContainerPtr;
00210 
00211         /** \brief A pointer to the DataContainer object containing the input and target point clouds */
00212         DataContainerPtr data_container_;
00213 
00214       private:
00215 
00216         /** \brief finds the optimal inlier ratio. This is based on the paper 'Outlier Robust ICP for minimizing Fractional RMSD, J. M. Philips et al'
00217          */
00218         inline float optimizeInlierRatio (std::vector <double> &dists);
00219     };
00220   }
00221 }
00222 
00223 #include <pcl/registration/impl/correspondence_rejection_var_trimmed.hpp>
00224 
00225 #endif    // PCL_REGISTRATION_CORRESPONDENCE_REJECTION_VAR_TRIMMED_H_