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 #include <pcl/visualization/common/io.h>
00038 #include <Eigen/Geometry>
00039 #include <pcl/io/pcd_io.h>
00040
00042 void
00043 pcl::visualization::getCorrespondingPointCloud (vtkPolyData *src,
00044 const pcl::PointCloud<pcl::PointXYZ> &tgt,
00045 std::vector<int> &indices)
00046 {
00047
00048 pcl::PointCloud<pcl::PointXYZ> cloud;
00049 cloud.height = 1; cloud.width = static_cast<uint32_t> (src->GetNumberOfPoints ());
00050 cloud.is_dense = false;
00051 cloud.points.resize (cloud.width * cloud.height);
00052 for (int i = 0; i < src->GetNumberOfPoints (); i++)
00053 {
00054 double p[3];
00055 src->GetPoint (i, p);
00056 cloud.points[i].x = static_cast<float> (p[0]);
00057 cloud.points[i].y = static_cast<float> (p[1]);
00058 cloud.points[i].z = static_cast<float> (p[2]);
00059 }
00060
00061
00062 pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
00063 boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ> > tgt_ptr (new pcl::PointCloud<pcl::PointXYZ> (tgt));
00064 kdtree.setInputCloud (tgt_ptr);
00065
00066 std::vector<int> nn_indices (1);
00067 std::vector<float> nn_dists (1);
00068
00069 for (size_t i = 0; i < cloud.points.size (); ++i)
00070 {
00071 kdtree.nearestKSearch (cloud.points[i], 1, nn_indices, nn_dists);
00072 indices.push_back (nn_indices[0]);
00073 }
00074
00075 std::sort (indices.begin (), indices.end ());
00076 indices.erase (std::unique (indices.begin (), indices.end ()), indices.end ());
00077 }
00078
00080 bool
00081 pcl::visualization::savePointData (vtkPolyData* data, const std::string &out_file, const boost::shared_ptr<CloudActorMap> &actors)
00082 {
00083
00084 vtkSmartPointer<vtkCleanPolyData> cleaner = vtkSmartPointer<vtkCleanPolyData>::New ();
00085 cleaner->SetTolerance (0.0);
00086 cleaner->SetInput (data);
00087 cleaner->ConvertLinesToPointsOff ();
00088 cleaner->ConvertPolysToLinesOff ();
00089 cleaner->ConvertStripsToPolysOff ();
00090 cleaner->PointMergingOn ();
00091 cleaner->Update ();
00092
00093
00094 if (cleaner->GetOutput ()->GetNumberOfPoints () != data->GetNumberOfPoints ())
00095 {
00096 int nr_pts_pruned = static_cast<int> (data->GetNumberOfPoints () - cleaner->GetOutput ()->GetNumberOfPoints ());
00097 pcl::console::print_highlight ("Number of points pruned: "); pcl::console::print_value ("%d\n", nr_pts_pruned);
00098 }
00099
00100
00101 CloudActorMap::iterator it;
00102 int i = 1;
00103 for (it = actors->begin (); it != actors->end (); ++it)
00104 {
00105 std::string file_name = (*it).first;
00106
00107
00108 std::string::size_type position;
00109 if ((position = file_name.find (".pcd")) == std::string::npos)
00110 continue;
00111
00112
00113 file_name = file_name.substr (0, position) + ".pcd";
00114
00115 pcl::console::print_debug (" Load: %s ... ", file_name.c_str ());
00116
00117 sensor_msgs::PointCloud2 cloud;
00118 if (pcl::io::loadPCDFile (file_name, cloud) == -1)
00119 {
00120 pcl::console::print_error (stdout, "[failed]\n");
00121 return (false);
00122 }
00123 else
00124 pcl::console::print_debug ("[success]\n");
00125
00126 pcl::PointCloud<pcl::PointXYZ> cloud_xyz;
00127 pcl::fromROSMsg (cloud, cloud_xyz);
00128
00129 std::vector<int> indices;
00130 getCorrespondingPointCloud (cleaner->GetOutput (), cloud_xyz, indices);
00131
00132
00133 sensor_msgs::PointCloud2 cloud_out;
00134 pcl::copyPointCloud (cloud, indices, cloud_out);
00135 std::stringstream ss;
00136 ss << out_file << i++ << ".pcd";
00137 pcl::console::print_debug (" Save: %s ... ", ss.str ().c_str ());
00138 if (pcl::io::savePCDFile (ss.str (), cloud_out, Eigen::Vector4f::Zero (),
00139 Eigen::Quaternionf::Identity (), true) == -1)
00140 {
00141 pcl::console::print_error (stdout, "[failed]\n");
00142 return (false);
00143 }
00144 else
00145 pcl::console::print_debug ("[success]\n");
00146 }
00147
00148 return (true);
00149 }