Read / Write#

Create and read a Pointcloud from file#

To read a cloud from a file, the following function can be used :

1pcl::PointCloud<pcl::PointXYZ>::Ptr mynewcloud(new pcl::PointCloud <pcl::PointXYZ>);
2pcl::io::loadPCDFile("file/path/filename.pcd", *mynewcloud);
The type of cloud can be any of pcl::Pointxxxx. pcl::PointXYZ is a simple cloud file with (x,y,z) positions. If you need color handling, pcl::PointXYZRGB can be used.
Read the PCL documentation for more information.

Read from a file (FileExplorer (Windows))#

For quick use while testing on multiple files, the following method allows to pick a file from FileExplorer on Windows :

1/**
2 * @brief Get cloud from file explorer
3 * @tparam PointT Should be a pcl::PointXYZxxx
4 * @param emp_cloud Cloud to fill with file
5 * @param verbose If has to output debug on console
6*/
7template <typename PointT>
8void getCloud(typename pcl::PointCloud<PointT>::Ptr& emp_cloud, bool verbose = false);

To use it, do as follow :

1pcl::PointCloud<pcl::PointXYZ>::Ptr mynewcloud(new pcl::PointCloud <pcl::PointXYZ>);
2getCloud(mynewcloud);

Write to file#

To write a cloud to a file, the following function can be used :

1pcl::io::savePCDFileASCII("file/path/filename.pcd", *mynewcloud);

Center cloud#

If your cloud has an offset, it can be centered with the following function :

1/**
2 * @brief Center given cloud to (0,0,0) based on closest points to center
3 * @tparam PointT Should be a pcl::PointXYZxxx
4 * @param emp_cloud Cloud to center
5 * @param verbose If has to output debug on console
6*/
7template <typename  PointT>
8void centerCloud(typename pcl::PointCloud<PointT>::Ptr& emp_cloud, bool verbose = false);

What this function does is looking for the point with the smallest x (can be negative) from the center, same with the point with the smallest y and the one with the smallest z value. It then moves the whole cloud by the inverted values to have it lying around the origin.

To use it, do as follow :

1centerCloud<pcl::PointXYZ>(mynewcloud);

Get and center cloud#

Combining the two last functions, the following one load a file thanks to the FileExplorer and then center it :

 1/**
 2* @brief Get cloud from file explorer and center it
 3* @tparam PointT Should be a pcl::PointXYZxxx
 4* @param emp_cloud Cloud to fill and center with file
 5* @param verbose If has to output debug on console
 6* @example
 7* pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
 8* getAndCenterCloud<pcl::PointXYZ>(cloud,true);
 9*/
10template <typename  PointT>
11void getAndCenterCloud(typename pcl::PointCloud<PointT>::Ptr& emp_cloud, bool verbose = false);

Algorithm 3D Algorithm PCL Point Cloud