C++ – Adding two different point clouds to viewer (Point Cloud Library (PCL))


I just started to use the great point cloud library and wanted to display two point clouds within one viewer but each in a different colour.

When I use one point cloud object (pointer?!) it works just fine but if I want to add a second one, only the second one will be displayed in the viewer.

I'm using pcl version 1.6 and did it pretty much like in this tutorial.
Maybe you guys have a suggetion.

The relevant code snippet is below. Thanks in advance!!!

  boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer_two_clouds (new pcl::visualization::PCLVisualizer("3D Viewer"));

     // cloud: green / cloud2: red
  pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color1 (cloud, 0, 255, 0);
  pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color2 (cloud2, 255, 0, 0);

  //add both
  viewer_two_clouds->addPointCloud<pcl::PointXYZRGB> (cloud, single_color1, "sample_cloud_1");
  viewer_two_clouds->addPointCloud<pcl::PointXYZRGB> (cloud2, single_color2, "sample_cloud_2");

  // set coordinateSystem and init camera

      boost::this_thread::sleep (boost::posix_time::microseconds(100000));


Best Solution

In order to apply transformations (such as rotations and translations) to a point cloud you already loaded you should use the pcl::transformPointCloud function (see here). This function takes 3 arguments: the input cloud, the output cloud and an Eigen::Transform. Simply define a translation transformation and feed it into the function in order to translate your cloud correctly.

There is a good Eigen tutorial (here) that shows you how to define and use space transformations.

Related Question