pointcloud_to_webgl.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 
00003 #include <boost/lexical_cast.hpp>
00004 
00005 #include <pcl/point_types.h>
00006 #include <pcl_ros/point_cloud.h>
00007 #include <pcl/io/pcd_io.h>
00008 #include <pcl/common/centroid.h>
00009 
00010 class PointCloudToWebgl {
00011 
00012 public:
00013 
00017   PointCloudToWebgl(const std::string& input_cloud, const int& cloud_format,
00018       const std::string& output_cloud) :
00019     input_cloud_(input_cloud), cloud_format_(cloud_format), output_cloud_(output_cloud)
00020   {}
00021 
00025   void convert()
00026   {
00027     ROS_INFO_STREAM("[PointCloudToWebgl:] Opening file " << input_cloud_);
00028 
00029     // Init the cloud
00030     pcl::PointCloud<pcl::PointXYZRGB> cloud;
00031 
00032     // Read point cloud
00033     if (cloud_format_ == 0)
00034     {
00035       // NO RGB
00036       pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz (new pcl::PointCloud<pcl::PointXYZ>);
00037       if (pcl::io::loadPCDFile<pcl::PointXYZ> (input_cloud_, *cloud_xyz) == -1) //* load the file
00038       {
00039         ROS_ERROR_STREAM("[PointCloudToWebgl:] Couldn't read file " << input_cloud_);
00040         return;
00041       }
00042       copyPointCloud(*cloud_xyz, cloud);
00043     }
00044     else if (cloud_format_ == 1)
00045     {
00046       // RGB
00047       pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_xyzrgb (new pcl::PointCloud<pcl::PointXYZRGB>);
00048       if (pcl::io::loadPCDFile<pcl::PointXYZRGB> (input_cloud_, *cloud_xyzrgb) == -1) //* load the file
00049       {
00050         ROS_ERROR_STREAM("[PointCloudToWebgl:] Couldn't read file " << input_cloud_);
00051         return;
00052       }
00053       copyPointCloud(*cloud_xyzrgb, cloud);
00054     }
00055 
00056     // Compute the cloud centroid
00057     Eigen::Vector4f centroid;
00058     pcl::compute3DCentroid(cloud, centroid);
00059 
00060     // Save int file
00061     ROS_INFO_STREAM("[PointCloudToWebgl:] Saving webgl file to " << output_cloud_);
00062     std::fstream f_webgl(output_cloud_.c_str(), std::ios::out);
00063     for (unsigned int i=0; i<cloud.size(); i++)
00064     {
00065       int r = 224;
00066       int g = 224;
00067       int b = 224;
00068       if (cloud_format_ == 1)
00069       {
00070         r = (int)cloud[i].r;
00071         g = (int)cloud[i].g;
00072         b = (int)cloud[i].b;
00073       }
00074       f_webgl << cloud[i].x - centroid[0] << "," <<
00075                  cloud[i].y - centroid[1] << "," <<
00076                  cloud[i].z - centroid[2] << "," <<
00077                  r << "," <<
00078                  g << "," <<
00079                  b << std::endl;
00080 
00081     }
00082     f_webgl.close();
00083     ROS_INFO("[PointCloudToWebgl:] Saved!");
00084   }
00085 
00086   private:
00087   std::string input_cloud_;
00088   std::string output_cloud_;
00089   int cloud_format_;
00090 };
00091 
00095 int main(int argc, char **argv)
00096 {
00097   if (argc < 4)
00098   {
00099     std::cout << "Usage: " << argv[0] << " INPUT_PCD FORMAT OUTPUT_CSV" << std::endl;
00100     std::cout << "  Example: " << argv[0] << " input_cloud.pcd 0 output_cloud.csv" << std::endl;
00101     return 0;
00102   }
00103 
00104   // Read inputs
00105   std::string input_cloud(argv[1]);
00106   std::string output_cloud(argv[3]);
00107   int cloud_format = boost::lexical_cast<int>(argv[2]);
00108 
00109   // Convert
00110   PointCloudToWebgl converter(input_cloud, cloud_format, output_cloud);
00111   converter.convert();
00112 
00113   return 0;
00114 }
00115 


pointcloud_tools
Author(s): Pep Lluis Negre
autogenerated on Sat Jun 8 2019 20:10:22