从文件夹中读取所有的pcd文件并发布

代码非常简单,不多说了直接上代码

#include "ros/ros.h"
#include "sensor_msgs/PointCloud2.h"
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/conversions.h>
#include <pcl_ros/point_cloud.h> // 注意pcl_ros的不同
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/common/common.h>
#include <pcl/common/time.h>
#include <random>
#include <glob.h>
#include <fstream>

class PointCloudPublisher
{
public:
    PointCloudPublisher()
    {
        // 初始化节点句柄
        nh_ = new ros::NodeHandle("~");

        msg_ = sensor_msgs::PointCloud2();

        filenames = std::vector<std::string>(100);

        pcd_dir = "/home/and/work/ld_ws/src/extrinsic_calibration/src/pcd/";

        getFiles(pcd_dir);

        point_cloud_publisher_ = nh_->advertise<sensor_msgs::PointCloud2>("pointcloud_publisher", 10);

        // 设置定时发布点云
        timer_ = nh_->createTimer(ros::Duration(5.0 / 1.0), &PointCloudPublisher::timerCallback, this); // 每1秒发布一次
    }

    void timerCallback(const ros::TimerEvent &)
    {
        pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
        for (auto &i : filenames)
        {
            ROS_INFO("读取点云文件:%s",  i.c_str());
            pcl::PCDReader reader;
            reader.read(i.c_str(), *cloud);
            if (!cloud->points.empty())
            {
                // 更新消息时间戳
                pcl::toROSMsg(*cloud, msg_);
                msg_.header.stamp = ros::Time::now();
                ROS_INFO("发布点云");
                msg_.header.frame_id = "rslidar";
                point_cloud_publisher_.publish(msg_);
            }
            else
            {
                ROS_INFO("点云为空");
            }
        }
    }

    void getFiles(std::string path)
    {
        DIR *pDir;
        struct dirent *ptr;
        if (!(pDir = opendir(path.c_str())))
        {
            cout << "Folder doesn't Exist!" << endl;
            return;
        }
        while ((ptr = readdir(pDir)) != 0)
        {
            if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0)
            {
                filenames.push_back(path + "/" + ptr->d_name);
            }
            ROS_INFO("文件名为:%s",  ptr->d_name);
        }
        closedir(pDir);
    }

private:
    ros::NodeHandle *nh_;
    ros::Publisher point_cloud_publisher_;
    sensor_msgs::PointCloud2 msg_;
    ros::Timer timer_;
    std::vector<std::string> filenames;
    std::string pcd_dir;
};

int main(int argc, char **argv)
{
    setlocale(LC_CTYPE, "zh_CN.utf8");
    ros::init(argc, argv, "pointcloud_publisher");
    PointCloudPublisher gpcp;
    ros::spin();
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例的C++代码,可以将文件夹的所有.txt文件转换为对应文件名的.pcd文件。请确保您已经安装了PCL库,并将其链接到您的项目。 ```cpp #include <iostream> #include <string> #include <vector> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> int main(int argc, char** argv) { std::string folder_path = "your_folder_path"; // 替换为您的文件夹路径 std::vector<std::string> file_names; // 获取文件夹所有.txt文件文件pcl::getAllFilesInDirectory(folder_path, ".txt", file_names); for (const auto& file_name : file_names) { std::string txt_file_path = folder_path + "/" + file_name; std::string pcd_file_path = folder_path + "/" + file_name.substr(0, file_name.find_last_of('.')) + ".pcd"; // 读取.txt文件 pcl::PointCloud<pcl::PointXYZ> cloud; if (pcl::io::loadPCDFile<pcl::PointXYZ>(txt_file_path, cloud) == -1) { std::cerr << "Failed to load " << txt_file_path << std::endl; continue; } // 保存为.pcd文件 pcl::io::savePCDFileBinary(pcd_file_path, cloud); std::cout << "Converted " << txt_file_path << " to " << pcd_file_path << std::endl; } return 0; } ``` 请将代码的"your_folder_path"替换为您实际的文件夹路径。此代码假设您的.txt文件可以直接作为.pcd文件读取,且点的类型为pcl::PointXYZ。如果您的文件格式或点类型不同,请相应调整代码。 编译并运行此代码后,它将遍历指定文件夹的所有.txt文件,并将它们转换为对应的.pcd文件。转换后的.pcd文件将保存在相同的文件夹。如果转换成功,将会在控制台输出转换的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值