读取非标ply文件

读取非标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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值