打开点云文件(支持pcd,ply)
pcl::PointCloud<pcl::PointXYZ>::Ptr PclTool::openPointCloudFile(const std::string& filename)
{
std::string fileExtension = std::filesystem::path(filename).extension().string();
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (fileExtension == ".pcd" || fileExtension == ".PCD")
{
if (pcl::io::loadPCDFile<pcl::PointXYZ>(filename, *cloud) == -1)
{
std::cout << "Unable to open PCD file:" << filename << std::endl;
return nullptr;
}
}
else if (fileExtension == ".ply" || fileExtension == ".PLY")
{
if (pcl::io::loadPLYFile<pcl::PointXYZ>(filename, *cloud) == -1)
{
std::cout << "Unable to open PLY file: " << filename << std::endl;
return nullptr;
}
}
else
{
std::cout << "不支持的文件格式: " << fileExtension << std::endl;
return nullptr;
}
return cloud;
}
保存点云文件
bool PclTool::savePointCloudFile(const pcl::PointCloud<pcl::PointXYZ>::Ptr& cloud, const std::string& filename)
{
if (!cloud || cloud->empty())
{
std::cout << "ERROR : Invalid point cloud data " << std::endl;
return false;
}
pcl::io::savePCDFileASCII(filename, *cloud);
return true;
}