Skip to content

Read / Write

Create and read a Pointcloud from file

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

pcl::PointCloud<pcl::PointXYZ>::Ptr mynewcloud(new pcl::PointCloud <pcl::PointXYZ>);
pcl::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 :

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

To use it, do as follow :

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

Write to file

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

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

Center cloud

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

/**
    * @brief Center given cloud to (0,0,0) based on closest points to center
    * @tparam PointT Should be a pcl::PointXYZxxx
    * @param emp_cloud Cloud to center
    * @param verbose If has to output debug on console
*/
template <typename  PointT>
void 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 :

centerCloud<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 :

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