Search code examples
c++point-cloud-library

Save screenshot using keyboard callback event


I am trying to save screenshot inside PCL Visualizer window using keyboard callback event. Below is the code snippet:

#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>

void keyboardEventOccurred (const pcl::visualization::KeyboardEvent &event, void* viewer)
{
  if (event.getKeySym () == "s" && event.keyDown ())
  {
    viewer->saveScreenshot("Image.png");
  }
}

int main (int argc, char** argv)
{
    pcl::visualization::PCLVisualizer viewer("Cloud Viewer");

    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr body (new pcl::PointCloud<pcl::PointXYZRGBA>);
    pcl::io::loadPCDFile ("body.pcd", *body);
    viewer.addPointCloud (body,"body");
    viewer.registerKeyboardCallback (keyboardEventOccurred, &viewer);
    viewer.spin();
    return 0;
}

However, there is some mismatch between data fields and it is returning following error:

[100%] Building CXX object CMakeFiles/pcl_visualizer.dir/pcl_visualizer.cpp.o
/home/ravi/pcl_visualizer/pcl_visualizer.cpp: In function ‘void keyboardEventOccurred(const pcl::visualization::KeyboardEvent&, void*)’:
/home/ravi/pcl_visualizer/pcl_visualizer.cpp:8:11: error: ‘void*’ is not a pointer-to-object type
     viewer->saveScreenshot("Image.png");
           ^
make[2]: *** [CMakeFiles/pcl_visualizer.dir/pcl_visualizer.cpp.o] Error 1

Any workaround, please?


Solution

  • The error message you have is telling you everything really; The point of void* in your callback is that it the callback provider doesn't know, or care, what type of pointer is being used for the callback.

    You seem to pass it a pointer to the viewer object, but all the compiler knows or cares about is that it's an n bit number that represents some memory to provide to the callback.

    By removing the concept of type by using void*, there's no way for the compiler to establish what is meant by viewer->XXX.

    To resolve the problem, the easiest way is to cast the pointer to be what you think it should be; something like

    pcl::visualization::PCLVisualizer* v = static_cast<pcl::visualization::PCLVisualizer*>(viewer)
    

    you can then do v->XXX()

    On a side note, I would also suggest you brush up on your pointers in your C++ book.