pcl2_to_matlab_file.cpp
Go to the documentation of this file.
00001 //Extract the current point cloud to a matlab file in the format
00002 //X1..Xn
00003 //Y1..Yn
00004 //Z1..Zn
00005 // the idea is to use a bag file, stop in the interesting frame an being able to save that pointcloud
00006 
00007 #include <ros/ros.h>
00008 #include <sensor_msgs/PointCloud2.h>
00009 #include <iostream>
00010 #include <fstream>
00011 #include <sstream>
00012 #include <iomanip>
00013 #include <boost/foreach.hpp>
00014 
00015 //int nc=176; //tamany de la matriu
00016 //int nr=144;
00017 
00018 int nImages = 0;
00019 std::ofstream rimage;
00020 
00021 int callback(const sensor_msgs::PointCloud2ConstPtr& msg)
00022 {
00023   int nc=msg->width;
00024   int nr=msg->height;
00025   std::stringstream rimagefilename;
00026   rimagefilename << "frameTOFDepth" << std::setw(2) << std::setfill('0') << nImages << ".txt";
00027   std::cout << rimagefilename.str() << std::endl;
00028   rimage.open (rimagefilename.str().c_str(), std::ios::out);
00029   if (!rimage){
00030     std::cerr << "Unable to open the file:\n\t" << rimagefilename << std::endl;
00031     return 1; // terminate with error
00032   }
00033 
00034   for (int ii=0;ii<nr;ii++) {
00035     for (int jj=0;jj<nc;jj++){
00036       int index2 = (ii)*nc +(jj);
00037       float *pstep2 = (float*)&msg->data[(index2) * msg->point_step];
00038       //TODO: es pot direccionar pel nom del camp???  msg->data[(index2) * msg->point_step].x
00039       rimage << ii << " " << jj << " " << pstep2[2]*1000 << std::endl;
00040     }
00041   }
00042 
00043   rimage.close();
00044   
00045   //  ros::shutdown();
00046   nImages++;
00047   return 0;
00048 }
00049 
00050 int main(int argc, char **argv)
00051 {
00052   ros::init(argc, argv, "listener");
00053   ros::NodeHandle nh;
00054   // to create a subscriber, you can do this (as above):
00055   ros::Subscriber sub = nh.subscribe<sensor_msgs::PointCloud2> ("/input", 1, callback);
00056   std::cout << "ini " << std::endl;
00057   ros::spin();
00058   std::cout << "end " << std::endl;
00059   return 0;
00060 }


iri_pcl_app
Author(s): Sergi Foix
autogenerated on Fri Dec 6 2013 20:23:18