lum.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) 2012, Jochen Sprickerhof
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/common/transforms.h>
00044 #include <pcl/registration/lum.h>
00045 #include <pcl/registration/correspondence_estimation.h>
00046 
00047 #include <iostream>
00048 #include <string>
00049 
00050 #include <vector>
00051 
00052 typedef pcl::PointXYZ PointType;
00053 typedef pcl::PointCloud<PointType> Cloud;
00054 typedef Cloud::ConstPtr CloudConstPtr;
00055 typedef Cloud::Ptr CloudPtr;
00056 typedef std::pair<std::string, CloudPtr> CloudPair;
00057 typedef std::vector<CloudPair> CloudVector;
00058 
00059 int
00060 main (int argc, char **argv)
00061 {
00062   double dist = 2.5;
00063   pcl::console::parse_argument (argc, argv, "-d", dist);
00064 
00065   int iter = 10;
00066   pcl::console::parse_argument (argc, argv, "-i", iter);
00067 
00068   int lumIter = 1;
00069   pcl::console::parse_argument (argc, argv, "-l", lumIter);
00070 
00071   pcl::registration::LUM<PointType> lum;
00072   lum.setMaxIterations (lumIter);
00073   lum.setConvergenceThreshold (0.001f);
00074 
00075   std::vector<int> pcd_indices;
00076   pcd_indices = pcl::console::parse_file_extension_argument (argc, argv, ".pcd");
00077 
00078   CloudVector clouds;
00079   for (size_t i = 0; i < pcd_indices.size (); i++)
00080   {
00081     CloudPtr pc (new Cloud);
00082     pcl::io::loadPCDFile (argv[pcd_indices[i]], *pc);
00083     clouds.push_back (CloudPair (argv[pcd_indices[i]], pc));
00084     std::cout << "loading file: " << argv[pcd_indices[i]] << " size: " << pc->size () << std::endl;
00085     lum.addPointCloud (clouds[i].second);
00086   }
00087 
00088   for (int i = 0; i < iter; i++)
00089   {
00090     for (size_t i = 1; i < clouds.size (); i++)
00091       for (size_t j = 0; j < i; j++)
00092       {
00093         Eigen::Vector4f ci, cj;
00094         pcl::compute3DCentroid (*(clouds[i].second), ci);
00095         pcl::compute3DCentroid (*(clouds[j].second), cj);
00096         Eigen::Vector4f diff = ci - cj;
00097 
00098         //std::cout << i << " " << j << " " << diff.norm () << std::endl;
00099 
00100         if(diff.norm () < 5.0 && (i - j == 1 || i - j > 20))
00101         {
00102           if(i - j > 20)
00103             std::cout << "add connection between " << i << " (" << clouds[i].first << ") and " << j << " (" << clouds[j].first << ")" << std::endl;
00104           pcl::registration::CorrespondenceEstimation<PointType, PointType> ce;
00105           ce.setInputTarget (clouds[i].second);
00106           ce.setInputSource (clouds[j].second);
00107           pcl::CorrespondencesPtr corr (new pcl::Correspondences);
00108           ce.determineCorrespondences (*corr, dist);
00109           if (corr->size () > 2)
00110             lum.setCorrespondences (j, i, corr);
00111         }
00112       }
00113 
00114     lum.compute ();
00115 
00116     for(int i = 0; i < lum.getNumVertices (); i++)
00117     {
00118       //std::cout << i << ": " << lum.getTransformation (i) (0, 3) << " " << lum.getTransformation (i) (1, 3) << " " << lum.getTransformation (i) (2, 3) << std::endl;
00119       clouds[i].second = lum.getTransformedCloud (i);
00120     }
00121   }
00122 
00123   for(int i = 0; i < lum.getNumVertices (); i++)
00124   {
00125     std::string result_filename (clouds[i].first);
00126     result_filename = result_filename.substr (result_filename.rfind ("/") + 1);
00127     pcl::io::savePCDFileBinary (result_filename.c_str (), *(clouds[i].second));
00128     //std::cout << "saving result to " << result_filename << std::endl;
00129   }
00130 
00131   return 0;
00132 }


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