读取非标ply文件
/**
* @brief 读取ply数据
*
* @param path 点云路径
* @return pcl::PointCloud<pcl::PointXYZ>::Ptr 返回点云数据
*/
pcl::PointCloud<pcl::PointXYZ>::Ptr fileToPCLPointXYZ(std::string path)
{
// std::string path = "/home/crazy/MyCode/camel_100/data/1_2023-10-19-15-01-44.ply";
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); // 创建点云对象
std::ifstream ply_file(path);
if (!ply_file.is_open())
{
// LOG(INFO)("Unable to open PLY file.");
std::cout << "Unable to open PLY file." << std::endl;
return cloud;
}
std::string line;
int pp = 0;
// 读取文件头部
while (getline(ply_file, line))
{
// ROS_INFO("line = %s", line);
pp++;
// if (line == "end_header")
// break;
if (line.find("end_header") != std::string::npos)
{
break;
}
}
getline(ply_file, line);
std::cout << " line = " << line << " pp = " << pp << std::endl;
int num = 0;
double min_z = 0;
// 逐行读取点云数据
while (getline(ply_file, line))
{
std::istringstream iss(line);
// std::cout << " line = " << line << std::endl;
double x, y, z;
if (iss >> x >> y >> z)
{
if (num < 1)
{
min_z = z / 1000.0;
}
min_z = std::min(z / 1000.0, min_z);
pcl::PointXYZ point;
point.x = x / 1000.0;
point.y = y / 1000.0;
point.z = z / 1000.0; //- min_z
// point.z = 0;
cloud->points.push_back(point); // 将点添加到点云对象中
num++;
}
}
// 关闭文件
ply_file.close();
cloud->width = cloud->points.size();
cloud->height = 1;
return cloud;
}