icp.cpp
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) 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 the copyright holder(s) 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 
00040 #include <pcl/console/parse.h>
00041 #include <pcl/io/pcd_io.h>
00042 #include <pcl/point_types.h>
00043 #include <pcl/registration/icp.h>
00044 #include <pcl/registration/icp_nl.h>
00045 
00046 #include <string>
00047 #include <iostream>
00048 #include <fstream>
00049 #include <vector>
00050 
00051 typedef pcl::PointXYZ PointType;
00052 typedef pcl::PointCloud<PointType> Cloud;
00053 typedef Cloud::ConstPtr CloudConstPtr;
00054 typedef Cloud::Ptr CloudPtr;
00055 
00056 int
00057 main (int argc, char **argv)
00058 {
00059   double dist = 0.05;
00060   pcl::console::parse_argument (argc, argv, "-d", dist);
00061 
00062   double rans = 0.05;
00063   pcl::console::parse_argument (argc, argv, "-r", rans);
00064 
00065   int iter = 50;
00066   pcl::console::parse_argument (argc, argv, "-i", iter);
00067 
00068   bool nonLinear = false;
00069   pcl::console::parse_argument (argc, argv, "-n", nonLinear);
00070 
00071   std::vector<int> pcd_indices;
00072   pcd_indices = pcl::console::parse_file_extension_argument (argc, argv, ".pcd");
00073 
00074   CloudPtr model (new Cloud);
00075   if (pcl::io::loadPCDFile (argv[pcd_indices[0]], *model) == -1)
00076   {
00077     std::cout << "Could not read file" << std::endl;
00078     return -1;
00079   }
00080   std::cout << argv[pcd_indices[0]] << " width: " << model->width << " height: " << model->height << std::endl;
00081 
00082   std::string result_filename (argv[pcd_indices[0]]);
00083   result_filename = result_filename.substr (result_filename.rfind ("/") + 1);
00084   pcl::io::savePCDFile (result_filename.c_str (), *model);
00085   std::cout << "saving first model to " << result_filename << std::endl;
00086 
00087   Eigen::Matrix4f t (Eigen::Matrix4f::Identity ());
00088 
00089   for (size_t i = 1; i < pcd_indices.size (); i++)
00090   {
00091     CloudPtr data (new Cloud);
00092     if (pcl::io::loadPCDFile (argv[pcd_indices[i]], *data) == -1)
00093     {
00094       std::cout << "Could not read file" << std::endl;
00095       return -1;
00096     }
00097     std::cout << argv[pcd_indices[i]] << " width: " << data->width << " height: " << data->height << std::endl;
00098 
00099     pcl::IterativeClosestPoint<PointType, PointType> *icp;
00100 
00101     if (nonLinear)
00102     {
00103       std::cout << "Using IterativeClosestPointNonLinear" << std::endl;
00104       icp = new pcl::IterativeClosestPointNonLinear<PointType, PointType>();
00105     }
00106     else
00107     {
00108       std::cout << "Using IterativeClosestPoint" << std::endl;
00109       icp = new pcl::IterativeClosestPoint<PointType, PointType>();
00110     }
00111 
00112     icp->setMaximumIterations (iter);
00113     icp->setMaxCorrespondenceDistance (dist);
00114     icp->setRANSACOutlierRejectionThreshold (rans);
00115 
00116     icp->setInputTarget (model);
00117 
00118     icp->setInputSource (data);
00119 
00120     CloudPtr tmp (new Cloud);
00121     icp->align (*tmp);
00122 
00123     t = t * icp->getFinalTransformation ();
00124 
00125     pcl::transformPointCloud (*data, *tmp, t);
00126 
00127     std::cout << icp->getFinalTransformation () << std::endl;
00128 
00129     *model = *data;
00130 
00131     std::string result_filename (argv[pcd_indices[i]]);
00132     result_filename = result_filename.substr (result_filename.rfind ("/") + 1);
00133     pcl::io::savePCDFileBinary (result_filename.c_str (), *tmp);
00134     std::cout << "saving result to " << result_filename << std::endl;
00135   }
00136 
00137   return 0;
00138 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:24:53