主要从《点云库pcl学习教程》这本书中摘录的,把学到的东西简单的记下,方便以后查找。
/** \brief Save point cloud data to a PCD file containing n-D points
* \param[in] file_name the output file name
* \param[in] cloud the point cloud data message
* \param[in] origin the sensor acquisition origin
* \param[in] orientation the sensor acquisition orientation
* \param[in] binary_mode true for binary mode, false (default) for ASCII
*
* Caution: PointCloud structures containing an RGB field have
* traditionally used packed float values to store RGB data. Storing a
* float as ASCII can introduce variations to the smallest bits, and
* thus significantly alter the data. This is a known issue, and the fix
* involves switching RGB data to be stored as a packed integer in
* future versions of PCL.
* \ingroup io
*/
inline int
savePCDFile (const std::string &file_name, const sensor_msgs::PointCloud2 &cloud,
const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (), //点云的原点,默认为0
const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),//点云的方向
const bool binary_mode = false)//是否保存为二进制格式,默认为false
{
PCDWriter w;
return (w.write (file_name, cloud, origin, orientation, binary_mode));
}
/** \brief Templated version for saving point cloud data to a PCD file
* containing a specific given cloud format
* \param[in] file_name the output file name
* \param[in] cloud the point cloud data message
* \param[in] binary_mode true for binary mode, false (default) for ASCII
*
* Caution: PointCloud structures containing an RGB field have
* traditionally used packed float values to store RGB data. Storing a
* float as ASCII can introduce variations to the smallest bits, and
* thus significantly alter the data. This is a known issue, and the fix
* involves switching RGB data to be stored as a packed integer in
* future versions of PCL.
* \ingroup io
*/
template<typename PointT> inline int
savePCDFile (const std::string &file_name, const pcl::PointCloud<PointT> &cloud, bool binary_mode = false)
{
PCDWriter w;
return (w.write<PointT> (file_name, cloud, binary_mode));
}
/**
* \brief Templated version for saving point cloud data to a PCD file
* containing a specific given cloud format.
*
* This version is to retain backwards compatibility.
* \param[in] file_name the output file name
* \param[in] cloud the point cloud data message
*
* Caution: PointCloud structures containing an RGB field have
* traditionally used packed float values to store RGB data. Storing a
* float as ASCII can introduce variations to the smallest bits, and
* thus significantly alter the data. This is a known issue, and the fix
* involves switching RGB data to be stored as a packed integer in
* future versions of PCL.
* \ingroup io
*/
template<typename PointT> inline int
savePCDFileASCII (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
{
PCDWriter w;
return (w.write<PointT> (file_name, cloud, false));
}
/**
* \brief Templated version for saving point cloud data to a PCD file
* containing a specific given cloud format.
*
* This version is to retain backwards compatibility.
* \param[in] file_name the output file name
* \param[in] cloud the point cloud data message
* \ingroup io
*/
template<typename PointT> inline int
savePCDFileBinary (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
{
PCDWriter w;
return (w.write<PointT> (file_name, cloud, true));
}
/**
* \brief Templated version for saving point cloud data to a PCD file
* containing a specific given cloud format
*
* \param[in] file_name the output file name
* \param[in] cloud the point cloud data message
* \param[in] indices the set of indices to save
* \param[in] binary_mode true for binary mode, false (default) for ASCII
*
* Caution: PointCloud structures containing an RGB field have
* traditionally used packed float values to store RGB data. Storing a
* float as ASCII can introduce variations to the smallest bits, and
* thus significantly alter the data. This is a known issue, and the fix
* involves switching RGB data to be stored as a packed integer in
* future versions of PCL.
* \ingroup io
*/
template<typename PointT> int
savePCDFile (const std::string &file_name,
const pcl::PointCloud<PointT> &cloud,
const std::vector<int> &indices,
const bool binary_mode = false)
{
// Save the data
PCDWriter w;
return (w.write<PointT> (file_name, cloud, indices, binary_mode));
}
}
}
实例
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int
main(int argc,char**argv)
{
pcl::PointCloud<pcl::PointXYZ> cloud;
// 创建点云
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::io::savePCDFileASCII("test_pcd.pcd",cloud);
std::cerr<<"Saved "<<cloud.points.size()<<" data points to test_pcd.pcd."<<std::endl;
for(size_t i=0;i<cloud.points.size();++i)
std::cerr<<" "<<cloud.points[i].x<<" "<<cloud.points[i].y<<" "<<cloud.points[i].z<<std::endl;
return(0);
}