Pcl: Not able to refresh PCLVisualizer

Created on 4 Apr 2017  路  7Comments  路  Source: PointCloudLibrary/pcl

Hello, im drawing some lines in a defined cloud. This cloud changes in every loop. I want the visualizer
to being refresh every loop (automatically). The problem is i cant refresh the PCLVisualizer and those lines. Not gonna copy the full code (around 600 lines):

// Defines and includes
...

// Definition of variables
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
...

boost::shared_ptr<pcl::visualization::PCLVisualizer> Vista ()
{
   boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("Vista 3D"));
    viewer->setBackgroundColor(0,0,0);

    // One of the lines
    viewer->addLine<pcl::PointXYZ, pcl::PointXYZ> (recta[0].distancia , recta[1].distancia, 0, 1, 0, "Recta principal");
    viewer->setShapeRenderingProperties (pcl::visualization::PCL_VISUALIZER_LINE_WIDTH, 2, "Recta principal");

    // The pointcloud
    viewer->addPointCloud<pcl::PointXYZ> (frontal, "Nube cargada");
    viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "Nube cargada");
    return (viewer);
}

void detect_line()
{
    // Algorithm for detecting the line
    ...
}

int main(int argc, const char **argv)
{
    // Some initial configuration
    while(1)
    {
         // Get the PCD file
         ...
         // Call detect_line()
         ...
         //Viewer + Spin()
        viewer = Vista();
        viewer->spin();
     }
}

What i get is one viewer, ok, but i have to close it manually anytime. Is there some way the viewer refreshes automatically in any loop?

My Environment

  • Operating System and version: Ubuntu 16.04
  • Compiler: cmake
  • PCL Version: 1.7.2

Thx a lot.
By the way, i looked for this in the pcl forum, but didnt found any useful.

Most helpful comment

Note that you should call resetCamera() __after__ adding point clouds to the visualizer.

All 7 comments

Hi @RamonGomez ,

These kind of questions should be asked on pcl-users forum, but since the forum is essentially dead I'll answer here.

If I got you right, here is a simple example you can start from:

int main(int argc, const char **argv)
{
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("Vista 3D"));
    viewer->setBackgroundColor(0,0,0);

   // Some initial configuration

    while(1)
    {
       // Clear the view
       viewer->removeAllShapes();
       viewer->removeAllPointClouds();

       // Get the PCD file
       frontal = pcl::io::loadPCDFile....

       // One of the lines
       viewer->addLine<pcl::PointXYZ, pcl::PointXYZ> (recta[0].distancia , recta[1].distancia, 0, 1, 0, "Recta principal");
       viewer->setShapeRenderingProperties (pcl::visualization::PCL_VISUALIZER_LINE_WIDTH, 2, "Recta principal");

        // The pointcloud
        viewer->addPointCloud<pcl::PointXYZ> (frontal, "Nube cargada");
        viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "Nube cargada");         

        viewer->spinOnce(100);
     }
}

Otherwise you can lookup updatePointCloud() method of PCLVisualizer class.
Read docs here: http://docs.pointclouds.org/trunk/classpcl_1_1visualization_1_1_p_c_l_visualizer.html

Hello @Logrus

I added the "removeall" lines and spinOnce to the function and its working. Thanks a lot.

BTW, i get a uncentered view. Do you know how to change this viewpoint? I tried with "setCameraPosition" but seems not working.

Try to use viewer->resetCamera (); before loop (see next comment). It usually centers the point cloud in the view. However, setCameraPosition should also work, but it can be a bit hard to get it right. You can search through PCL code for examples.

Note that you should call resetCamera() __after__ adding point clouds to the visualizer.

resetCamera() not working. Gonna try setCameraPosition. Thank you both :)

Finally i got an aproximated view. Thank you.

In my case, on the other hand, call resetCamera() is working while spin() or spinOnce() is not effective

Was this page helpful?
0 / 5 - 0 ratings