The CloudViewer

The CloudViewer is a straight forward, simple point cloud visualization, meant to get you up and viewing clouds in as little code as possible.

Note

The CloudViewer class is NOT meant to be used in multi-threaded applications! Please check the documentation on PCLVisualizer or read the PCLVisualizer tutorial for thread safe visualization.

Simple Cloud Visualization

If you just want to visualize something in your app with a few lines of code, use a snippet like the following one:

 1 #include <pcl/visualization/cloud_viewer.h>
 2 //...
 3 void
 4 foo ()
 5 {
 6   pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
 7   //... populate cloud
 8   pcl::visualization::CloudViewer viewer ("Simple Cloud Viewer");
 9   viewer.showCloud (cloud);
10   while (!viewer.wasStopped ())
11   {
12   }
13 }

A more complete sample:

The following shows how to run code on the visualization thread. The PCLVisualizer is the back end of the CloudViewer, but its running in its own thread. To access it you must use callback functions, to avoid the visualization concurrency issues. However care must be taken to avoid race conditions in your code, as the callbacks will be called from the visualization thread.

 1#include <pcl/visualization/cloud_viewer.h>
 2#include <iostream>
 3#include <pcl/common/io.h>
 4#include <pcl/io/pcd_io.h>
 5    
 6int user_data;
 7    
 8void 
 9viewerOneOff (pcl::visualization::PCLVisualizer& viewer)
10{
11    viewer.setBackgroundColor (1.0, 0.5, 1.0);
12    pcl::PointXYZ o;
13    o.x = 1.0;
14    o.y = 0;
15    o.z = 0;
16    viewer.addSphere (o, 0.25, "sphere", 0);
17    std::cout << "i only run once" << std::endl;
18    
19}
20    
21void 
22viewerPsycho (pcl::visualization::PCLVisualizer& viewer)
23{
24    static unsigned count = 0;
25    std::stringstream ss;
26    ss << "Once per viewer loop: " << count++;
27    viewer.removeShape ("text", 0);
28    viewer.addText (ss.str(), 200, 300, "text", 0);
29    
30    //FIXME: possible race condition here:
31    user_data++;
32}
33    
34int 
35main ()
36{
37    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
38    pcl::io::loadPCDFile ("my_point_cloud.pcd", *cloud);
39    
40    pcl::visualization::CloudViewer viewer("Cloud Viewer");
41    
42    //blocks until the cloud is actually rendered
43    viewer.showCloud(cloud);
44    
45    //use the following functions to get access to the underlying more advanced/powerful
46    //PCLVisualizer
47    
48    //This will only get called once
49    viewer.runOnVisualizationThreadOnce (viewerOneOff);
50    
51    //This will get called once per visualization iteration
52    viewer.runOnVisualizationThread (viewerPsycho);
53    while (!viewer.wasStopped ())
54    {
55    //you can also do cool processing here
56    //FIXME: Note that this is running in a separate thread from viewerPsycho
57    //and you should guard against race conditions yourself...
58    user_data++;
59    }
60    return 0;
61}

Compiling and running the program

Add the following lines to your CMakeLists.txt file:

 1cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
 2
 3project(cloud_viewer)
 4
 5find_package(PCL 1.2 REQUIRED)
 6
 7include_directories(${PCL_INCLUDE_DIRS})
 8link_directories(${PCL_LIBRARY_DIRS})
 9add_definitions(${PCL_DEFINITIONS})
10
11add_executable (cloud_viewer cloud_viewer.cpp)
12target_link_libraries (cloud_viewer ${PCL_LIBRARIES})

After you have made the executable, you can run it like so:

$ ./cloud_viewer