PCL L1: Concatenate point clouds using PCL

Concatenate point clouds

  • Introduction
    • Folder Structure
    • Source Code
    • Future work

Introduction

We give an easy and straightforward approach to concatenate several pcd files by using PCL but without any optimizations.

Folder Structure

Con_PL  -- con_pl.cpp
		-- pcd (folder)
			-- 000000.pcd
			-- 000001.pcd
			-- 000002.pcd
			-- ...  

Source Code

#include  //standard input output
#include  //PCD headfile in PCL
#include  // PCL对各种格式的点的支持头文件

using namespace std;
using namespace pcl;

int main (int argc, char** argv)
{
  int count,i ;
  char format[512]={0};
	char namelist_in[1024][1024]={0};

  cout <<"Input the number of PCDs to concatenate: \n";
  cin >> count;

  sprintf(format,"./pcd/%%%d%dd.%s", 0, 6, "pcd");
  for (i=0;i<count;i++){
    sprintf (namelist_in[i],format,i);
  }

  pcl::PointCloud<pcl::PointXYZI>::Ptr cloudRes (new pcl::PointCloud<pcl::PointXYZI>); // 创建Result点云(指针)
  pcl::PointCloud<pcl::PointXYZI>::Ptr cloudOrg (new pcl::PointCloud<pcl::PointXYZI>); // 创建Org点云(指针)
  pcl::PointCloud<pcl::PointXYZI>::Ptr cloudAdd (new pcl::PointCloud<pcl::PointXYZI>); // Next Point Cloud 


 if (pcl::io::loadPCDFile<pcl::PointXYZI> ("./pcd/000000.pcd", *cloudOrg) == -1) //* 读入PCD格式的文件,如果文件不存在,返回-1
  {
    PCL_ERROR ("Couldn't read PCD files \n"); //文件不存在时,返回错误,终止程序。
    return (-1);
  }
  
  if (pcl::io::loadPCDFile<pcl::PointXYZI> ("./pcd/000001.pcd", *cloudAdd) == -1) //* 读入PCD格式的文件,如果文件不存在,返回-1
  {
    PCL_ERROR ("Couldn't read PCD files \n"); //文件不存在时,返回错误,终止程序。
    return (-1);
  }
  *cloudRes=*cloudOrg+*cloudAdd;
  
  for(int pl=2;pl<count;pl++){
  cout << "Concatenating the " << pl+1 <<"th Point Cloud ... \n";
  
  cloudAdd->clear();
  if (pcl::io::loadPCDFile<pcl::PointXYZI> (namelist_in[pl], *cloudAdd) == -1) //* 读入PCD格式的文件,如果文件不存在,返回-1
  {
    PCL_ERROR ("Couldn't read PCD files \n"); //文件不存在时,返回错误,终止程序。
    return (-1);
  }
  *cloudRes=*cloudRes+*cloudAdd;
  }

  pcl::io::savePCDFile("CloudRes.pcd",*cloudRes);
  std::cout << "Loaded "
            << cloudRes->width * cloudRes->height
            << " data points from test_file.pcd with the following fields: "
            << std::endl;
	for (size_t i = 0; i < 5; ++i) // 为了方便观察,只显示前5个点
    std::cout << "    " << cloudRes->points[i].x
              << " "    << cloudRes->points[i].y
              << " "    << cloudRes->points[i].z
              << " "    << cloudRes->points[i].intensity 
              << std::endl;

  return (0);
}

Future work

This approach has been not designed for thousands of Point Cloud files to concatenate. In case of that, the issue of RAM overflow has to be reconsidered. Probably, we can temperately save the concatenated result point clouds to ROM then read again every 50 times (up to your computer).

你可能感兴趣的:(PCL)