icp_nl.h

Go to the documentation of this file.
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  * $Id: icp_nl.h 1370 2011-06-19 01:06:01Z jspricke $
00035  *
00036  */
00037 
00038 #ifndef PCL_ICP_NL_H_
00039 #define PCL_ICP_NL_H_
00040 
00041 // PCL includes
00042 #include "pcl/registration/icp.h"
00043 #include "pcl/sample_consensus/ransac.h"
00044 #include "pcl/sample_consensus/sac_model_registration.h"
00045 
00046 #include <cminpack.h>
00047 
00048 namespace pcl
00049 {
00055   template <typename PointSource, typename PointTarget>
00056   class IterativeClosestPointNonLinear : public IterativeClosestPoint<PointSource, PointTarget>
00057   {
00058     using IterativeClosestPoint<PointSource, PointTarget>::reg_name_;
00059     using IterativeClosestPoint<PointSource, PointTarget>::getClassName;
00060     using IterativeClosestPoint<PointSource, PointTarget>::indices_;
00061     using IterativeClosestPoint<PointSource, PointTarget>::target_;
00062     using IterativeClosestPoint<PointSource, PointTarget>::nr_iterations_;
00063     using IterativeClosestPoint<PointSource, PointTarget>::max_iterations_;
00064     using IterativeClosestPoint<PointSource, PointTarget>::previous_transformation_;
00065     using IterativeClosestPoint<PointSource, PointTarget>::final_transformation_;
00066     using IterativeClosestPoint<PointSource, PointTarget>::transformation_;
00067     using IterativeClosestPoint<PointSource, PointTarget>::transformation_epsilon_;
00068     using IterativeClosestPoint<PointSource, PointTarget>::converged_;
00069     using IterativeClosestPoint<PointSource, PointTarget>::corr_dist_threshold_;
00070     using IterativeClosestPoint<PointSource, PointTarget>::inlier_threshold_;
00071     using IterativeClosestPoint<PointSource, PointTarget>::min_number_correspondences_;
00072 
00073     using IterativeClosestPoint<PointSource, PointTarget>::rigid_transformation_estimation_;
00074 
00075     typedef pcl::PointCloud<PointSource> PointCloudSource;
00076     typedef typename PointCloudSource::Ptr PointCloudSourcePtr;
00077     typedef typename PointCloudSource::ConstPtr PointCloudSourceConstPtr;
00078 
00079     typedef pcl::PointCloud<PointTarget> PointCloudTarget;
00080 
00081     typedef PointIndices::Ptr PointIndicesPtr;
00082     typedef PointIndices::ConstPtr PointIndicesConstPtr;
00083 
00084     public:
00086       IterativeClosestPointNonLinear ()
00087       {
00088         min_number_correspondences_ = 4;
00089         reg_name_ = "IterativeClosestPointNonLinear";
00090 
00091         rigid_transformation_estimation_ = 
00092           boost::bind (&IterativeClosestPointNonLinear<PointSource, PointTarget>::estimateRigidTransformationLM, 
00093               this, _1, _2, _3, _4, _5);
00094       }
00095 
00102       void 
00103       estimateRigidTransformationLM (const PointCloudSource &cloud_src, const PointCloudTarget &cloud_tgt, Eigen::Matrix4f &transformation_matrix);
00104 
00113       void 
00114       estimateRigidTransformationLM (const PointCloudSource &cloud_src, const std::vector<int> &indices_src, 
00115                                      const PointCloudTarget &cloud_tgt, const std::vector<int> &indices_tgt, 
00116                                      Eigen::Matrix4f &transformation_matrix);
00117 
00118     protected:
00119       using IterativeClosestPoint<PointSource, PointTarget>::computeTransformation;
00120 
00121     private:
00126       inline double 
00127       computeMedian (double *fvec, int m);
00128 
00137       static int 
00138       functionToOptimize (void *p, int m, int n, const double *x, double *fvec, int iflag);
00139       static int 
00148       functionToOptimizeIndices (void *p, int m, int n, const double *x, double *fvec, int iflag);
00149 
00155       inline double
00156       distHuber (const Eigen::Vector4f &p_src, const Eigen::Vector4f &p_tgt, double sigma)
00157       {
00158         Eigen::Array4f diff = (p_tgt.array () - p_src.array ()).abs ();
00159         double norm = 0.0;
00160         for (int i = 0; i < 3; ++i)
00161         {
00162           if (diff[i] < sigma)
00163             norm += diff[i] * diff[i];
00164           else
00165             norm += 2.0 * sigma * diff[i] - sigma * sigma;
00166         }
00167         return (norm);
00168       }
00169 
00174       inline double
00175       distHuber (double diff, double sigma)
00176       {
00177         double norm = 0.0;
00178         if (diff < sigma)
00179           norm += diff * diff;
00180         else
00181           norm += 2.0 * sigma * diff - sigma * sigma;
00182         return (norm);
00183       }
00184 
00190       inline double
00191       distGedikli (double val, double clipping)
00192       {
00193         clipping *= 1.5;
00194         return (1.0 / (1.0 + pow (val / clipping, 4)));
00195       }
00196 
00201       inline double
00202       distL1 (const Eigen::Vector4f &p_src, const Eigen::Vector4f &p_tgt)
00203       {
00204         return ((p_src.array () - p_tgt.array ()).abs ().sum ());
00205       }
00206 
00211       inline double
00212       distL2Sqr (const Eigen::Vector4f &p_src, const Eigen::Vector4f &p_tgt)
00213       {
00214         return ((p_src - p_tgt).squaredNorm ());
00215       }
00216 
00218       std::vector<double> weights_;
00219 
00221       boost::mutex tmp_mutex_;
00222 
00224       const PointCloudSource *tmp_src_;
00225 
00227       const PointCloudTarget  *tmp_tgt_;
00228 
00230       const std::vector<int> *tmp_idx_src_;
00231 
00233       const std::vector<int> *tmp_idx_tgt_;
00234   };
00235 }
00236 
00237 #include "pcl/registration/impl/icp_nl.hpp"
00238 
00239 #endif  //#ifndef PCL_ICP_NL_H_