io.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) 2010, Willow Garage, Inc.
00006  *  Copyright (c) 2012-, Open Perception, Inc.
00007  *
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of the copyright holder(s) nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  */
00038 
00039 #include <vtkPolyData.h>
00040 #include <vtkCleanPolyData.h>
00041 #include <vtkSmartPointer.h>
00042 
00043 #include <pcl/visualization/common/io.h>
00044 #include <pcl/io/pcd_io.h>
00045 #include <pcl/visualization/eigen.h>
00046 
00048 void
00049 pcl::visualization::getCorrespondingPointCloud (vtkPolyData *src, 
00050                                                 const pcl::PointCloud<pcl::PointXYZ> &tgt, 
00051                                                 std::vector<int> &indices)
00052 {
00053   // Iterate through the points and copy the data in a pcl::PointCloud
00054   pcl::PointCloud<pcl::PointXYZ> cloud;
00055   cloud.height = 1; cloud.width = static_cast<uint32_t> (src->GetNumberOfPoints ());
00056   cloud.is_dense = false;
00057   cloud.points.resize (cloud.width * cloud.height);
00058   for (int i = 0; i < src->GetNumberOfPoints (); i++)
00059   {
00060     double p[3];
00061     src->GetPoint (i, p);
00062     cloud.points[i].x = static_cast<float> (p[0]); 
00063     cloud.points[i].y = static_cast<float> (p[1]); 
00064     cloud.points[i].z = static_cast<float> (p[2]);
00065   }
00066 
00067   // Compute a kd-tree for tgt
00068   pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
00069   boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ> > tgt_ptr (new pcl::PointCloud<pcl::PointXYZ> (tgt));
00070   kdtree.setInputCloud (tgt_ptr);
00071 
00072   std::vector<int> nn_indices (1);
00073   std::vector<float> nn_dists (1);
00074   // For each point on screen, find its correspondent in the target
00075   for (size_t i = 0; i < cloud.points.size (); ++i)
00076   {
00077     kdtree.nearestKSearch (cloud.points[i], 1, nn_indices, nn_dists);
00078     indices.push_back (nn_indices[0]);
00079   }
00080   // Sort and remove duplicate indices
00081   std::sort (indices.begin (), indices.end ());
00082   indices.erase (std::unique (indices.begin (), indices.end ()), indices.end ()); 
00083 }
00084 
00086 bool 
00087 pcl::visualization::savePointData (vtkPolyData* data, const std::string &out_file, const boost::shared_ptr<CloudActorMap> &actors)
00088 {
00089   // Clean the data (no duplicates!)
00090   vtkSmartPointer<vtkCleanPolyData> cleaner = vtkSmartPointer<vtkCleanPolyData>::New ();
00091   cleaner->SetTolerance (0.0);
00092   cleaner->SetInput (data);
00093   cleaner->ConvertLinesToPointsOff ();
00094   cleaner->ConvertPolysToLinesOff ();
00095   cleaner->ConvertStripsToPolysOff ();
00096   cleaner->PointMergingOn ();
00097   cleaner->Update ();
00098 
00099   // If we pruned any points, print the number of points pruned to screen
00100   if (cleaner->GetOutput ()->GetNumberOfPoints () != data->GetNumberOfPoints ())
00101   {
00102     int nr_pts_pruned = static_cast<int> (data->GetNumberOfPoints () - cleaner->GetOutput ()->GetNumberOfPoints ());
00103     pcl::console::print_highlight ("Number of points pruned: "); pcl::console::print_value ("%d\n", nr_pts_pruned);
00104   }
00105 
00106   // Attempting to load all Point Cloud data input files (using the actor name)...
00107   CloudActorMap::iterator it;
00108   int i = 1;
00109   for (it = actors->begin (); it != actors->end (); ++it)
00110   {
00111     std::string file_name = (*it).first;
00112 
00113     // Is there a ".pcd" in the name? If no, then do not attempt to load this actor
00114     std::string::size_type position;
00115     if ((position = file_name.find (".pcd")) == std::string::npos)
00116       continue;
00117 
00118     // Strip the ".pcd-X"
00119     file_name = file_name.substr (0, position) + ".pcd";
00120 
00121     pcl::console::print_debug ("  Load: %s ... ", file_name.c_str ());
00122     // Assume the name of the actor is the name of the file
00123     pcl::PCLPointCloud2 cloud;
00124     if (pcl::io::loadPCDFile (file_name, cloud) == -1)
00125     {
00126       pcl::console::print_error (stdout, "[failed]\n");
00127       return (false);
00128     }
00129     else
00130       pcl::console::print_debug ("[success]\n");
00131  
00132     pcl::PointCloud<pcl::PointXYZ> cloud_xyz;
00133     pcl::fromPCLPointCloud2 (cloud, cloud_xyz);
00134     // Get the corresponding indices that we need to save from this point cloud
00135     std::vector<int> indices;
00136     getCorrespondingPointCloud (cleaner->GetOutput (), cloud_xyz, indices);
00137 
00138     // Copy the indices and save the file
00139     pcl::PCLPointCloud2 cloud_out;
00140     pcl::copyPointCloud (cloud, indices, cloud_out);
00141     std::stringstream ss;
00142     ss << out_file << i++ << ".pcd";
00143     pcl::console::print_debug ("  Save: %s ... ", ss.str ().c_str ());
00144     if (pcl::io::savePCDFile (ss.str (), cloud_out, Eigen::Vector4f::Zero (),
00145                               Eigen::Quaternionf::Identity (), true) == -1)
00146     {
00147       pcl::console::print_error (stdout, "[failed]\n");
00148       return (false);
00149     }
00150     else
00151       pcl::console::print_debug ("[success]\n");
00152   }
00153 
00154   return (true);
00155 }


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