C++ – PCL: Visualize a point cloud

c++point-cloud-librarypoint-clouds

I'm trying to visualize a point cloud using PCL CloudViewer. The problem is that I'm quite new to C++ and I have found two tutorials first demonstrating the creation of PointCloud and second demonstrating the visualization of a PointCloud. However, I'm not able to combine these two tutorials.

Here is what I have come up with:

#include <iostream>

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

int main (int argc, char** argv)
{
  pcl::PointCloud<pcl::PointXYZ> cloud;
  
  // Fill in the cloud data
  cloud.width    = 5;
  cloud.height   = 1;
  cloud.is_dense = false;
  cloud.points.resize (cloud.width * cloud.height);

  for (size_t i = 0; i < cloud.points.size (); ++i)
  {
    cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
    cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
    cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
  }

  pcl::visualization::CloudViewer viewer ("Simple Cloud Viewer");
  viewer.showCloud (cloud);

  while (!viewer.wasStopped ())
  {
  }

  return (0);
}

but that even do not compile:

error: no matching function for call to   
‘pcl::visualization::CloudViewer::showCloud(pcl::PointCloud<pcl::PointXYZ>&)’

Best Solution

Your error message tells you what you need to do:

error: no matching function for call to ‘pcl::visualization::CloudViewer::showCloud(pcl::PointCloud<pcl::PointXYZ>&)’

So go to the documentation for CloudViewer and see what arguments this member function takes: http://docs.pointclouds.org/1.5.1/classpcl_1_1visualization_1_1_cloud_viewer.html

There we see that it takes a const MonochromeCloud::ConstPtr &cloud not the raw reference that you are passing in. This is a typedef of a smart pointer from boost:

typedef boost::shared_ptr<const PointCloud<PointT> > pcl::PointCloud< PointT >::ConstPtr

So when you create your cloud you need to wrap it in one of these smart pointers instead of making it a local variable. Something like (untested):

pcl::MonochromeCloud::ConstPtr cloud(new pcl::PointCloud<pcl::PointXYZ>());

Then, when you pass in the variable cloud, it will have the correct type and you won't get the error that you report. You will also have to change your cloud.foos to cloud->foos.

Looking at the second example you give, they do this as well:

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
Related Question