Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
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 }