Search code examples
c++depthpoint-cloudspoint-cloud-library

Wrtie Data from C++ File (Point Cloud Library)


I am using the point cloud library to take in a depth map and then write the PCD file every second or so to the memory so that it can be picked up by another program.

I have the program correctly rendering a depth map with a visualizer and it all works except the one line to actually write the file.

Here is my code:

 #include <pcl/io/openni_grabber.h>
 #include <iostream>
 #include <pcl/io/pcd_io.h>
 #include <pcl/visualization/cloud_viewer.h>

 class SimpleOpenNIViewer
 {
   public:
     SimpleOpenNIViewer () : viewer ("PCL OpenNI Viewer") {}

     void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &cloud)
     {
       if (!viewer.wasStopped())
         viewer.showCloud (cloud);
         //this is the line to write the file.
         //I am not sure it is the correct location.
         pcl::io::savePCDFileASCII ("test_pcd_here.pcd", cloud);
     }

     void run ()
     {
       pcl::Grabber* interface = new pcl::OpenNIGrabber();

       boost::function<void (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)> f =
         boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);

       interface->registerCallback (f);

       interface->start ();

       while (!viewer.wasStopped())
       {
         boost::this_thread::sleep (boost::posix_time::seconds (1));
       }

       interface->stop ();
     }

     pcl::visualization::CloudViewer viewer;
 };

 int main ()
 {
   SimpleOpenNIViewer v;
   v.run ();
   return 0;
 }

Here is the error I am getting while trying to cmake the file:

/home/patrick/Desktop/kinect/grabber/openni_grabber.cpp: In member function ‘void SimpleOpenNIViewer::cloud_cb_(const ConstPtr&)’:
/home/patrick/Desktop/kinect/grabber/openni_grabber.cpp:15:59: error: no matching function for call to ‘savePCDFileASCII(const char [18], const ConstPtr&)’
/home/patrick/Desktop/kinect/grabber/openni_grabber.cpp:15:59: note: candidate is:
/usr/include/pcl-1.6/pcl/io/pcd_io.h:704:5: note: template<class PointT> int pcl::io::savePCDFileASCII(const string&, const pcl::PointCloud<PointT>&)
make[2]: *** [CMakeFiles/openni_grabber.dir/openni_grabber.cpp.o] Error 1
make[1]: *** [CMakeFiles/openni_grabber.dir/all] Error 2
make: *** [all] Error 2

Solution

  • The savePCDFileASCII() function is expecting a const reference to PointCloud while you supply a pointer. You have to dereference the pointer:

    pcl::io::savePCDFileASCII ("test_pcd_here.pcd", *cloud);
    

    Keep in mind that your callback function is triggered as often as possible (more than once a second) so you may want to throttle the export. And more importantly, if you try to write the data while the other program is reading it (or the other way around), either program may crash (race condition) so you'll need some form of synchronization or stream the PointCloud directly through other means (socket or pipe for example).