registration.hpp
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-2011, 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  * $Id: registration.hpp 5044 2012-03-12 21:32:58Z rusu $
00037  *
00038  */
00039 
00041 template <typename PointSource, typename PointTarget> inline void
00042 pcl::Registration<PointSource, PointTarget>::setInputTarget (const PointCloudTargetConstPtr &cloud)
00043 {
00044   if (cloud->points.empty ())
00045   {
00046     PCL_ERROR ("[pcl::%s::setInputTarget] Invalid or empty point cloud dataset given!\n", getClassName ().c_str ());
00047     return;
00048   }
00049   PointCloudTarget target = *cloud;
00050   // Set all the point.data[3] values to 1 to aid the rigid transformation
00051   for (size_t i = 0; i < target.points.size (); ++i)
00052     target.points[i].data[3] = 1.0;
00053 
00054   //target_ = cloud;
00055   target_ = target.makeShared ();
00056   tree_->setInputCloud (target_);
00057 }
00058 
00060 template <typename PointSource, typename PointTarget> inline double
00061 pcl::Registration<PointSource, PointTarget>::getFitnessScore (const std::vector<float> &distances_a, 
00062                                                               const std::vector<float> &distances_b)
00063 {
00064   unsigned int nr_elem = static_cast<unsigned int> (std::min (distances_a.size (), distances_b.size ()));
00065   Eigen::VectorXf map_a = Eigen::VectorXf::Map (&distances_a[0], nr_elem);
00066   Eigen::VectorXf map_b = Eigen::VectorXf::Map (&distances_b[0], nr_elem);
00067   return (static_cast<double> ((map_a - map_b).sum ()) / static_cast<double> (nr_elem));
00068 }
00069 
00071 template <typename PointSource, typename PointTarget> inline double
00072 pcl::Registration<PointSource, PointTarget>::getFitnessScore (double max_range)
00073 {
00074   double fitness_score = 0.0;
00075 
00076   // Transform the input dataset using the final transformation
00077   PointCloudSource input_transformed;
00078   transformPointCloud (*input_, input_transformed, final_transformation_);
00079 
00080   std::vector<int> nn_indices (1);
00081   std::vector<float> nn_dists (1);
00082 
00083   // For each point in the source dataset
00084   int nr = 0;
00085   for (size_t i = 0; i < input_transformed.points.size (); ++i)
00086   {
00087     Eigen::Vector4f p1 = Eigen::Vector4f (input_transformed.points[i].x,
00088                                           input_transformed.points[i].y,
00089                                           input_transformed.points[i].z, 0);
00090     // Find its nearest neighbor in the target
00091     tree_->nearestKSearch (input_transformed.points[i], 1, nn_indices, nn_dists);
00092     
00093     // Deal with occlusions (incomplete targets)
00094     if (nn_dists[0] > max_range)
00095       continue;
00096 
00097     Eigen::Vector4f p2 = Eigen::Vector4f (target_->points[nn_indices[0]].x,
00098                                           target_->points[nn_indices[0]].y,
00099                                           target_->points[nn_indices[0]].z, 0);
00100     // Calculate the fitness score
00101     fitness_score += fabs ((p1-p2).squaredNorm ());
00102     nr++;
00103   }
00104 
00105   if (nr > 0)
00106     return (fitness_score / nr);
00107   else
00108     return (std::numeric_limits<double>::max ());
00109 }
00110 
00112 template <typename PointSource, typename PointTarget> inline void
00113 pcl::Registration<PointSource, PointTarget>::align (PointCloudSource &output)
00114 {
00115   align (output, Eigen::Matrix4f::Identity());
00116 }
00117 
00119 template <typename PointSource, typename PointTarget> inline void
00120 pcl::Registration<PointSource, PointTarget>::align (PointCloudSource &output, const Eigen::Matrix4f& guess)
00121 {
00122   if (!initCompute ()) return;
00123 
00124   if (!target_)
00125   {
00126     PCL_WARN ("[pcl::%s::compute] No input target dataset was given!\n", getClassName ().c_str ());
00127     return;
00128   }
00129 
00130   // Resize the output dataset
00131   if (output.points.size () != indices_->size ())
00132     output.points.resize (indices_->size ());
00133   // Copy the header
00134   output.header   = input_->header;
00135   // Check if the output will be computed for all points or only a subset
00136   if (indices_->size () != input_->points.size ())
00137   {
00138     output.width    = static_cast<uint32_t> (indices_->size ());
00139     output.height   = 1;
00140   }
00141   else
00142   {
00143     output.width    = static_cast<uint32_t> (input_->width);
00144     output.height   = input_->height;
00145   }
00146   output.is_dense = input_->is_dense;
00147 
00148   // Copy the point data to output
00149   for (size_t i = 0; i < indices_->size (); ++i)
00150     output.points[i] = input_->points[(*indices_)[i]];
00151 
00152   // Set the internal point representation of choice
00153   if (point_representation_)
00154     tree_->setPointRepresentation (point_representation_);
00155 
00156   // Perform the actual transformation computation
00157   converged_ = false;
00158   final_transformation_ = transformation_ = previous_transformation_ = Eigen::Matrix4f::Identity ();
00159 
00160   // Right before we estimate the transformation, we set all the point.data[3] values to 1 to aid the rigid 
00161   // transformation
00162   for (size_t i = 0; i < indices_->size (); ++i)
00163     output.points[i].data[3] = 1.0;
00164 
00165   computeTransformation (output, guess);
00166 
00167   deinitCompute ();
00168 }
00169 


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:17:38