C++常用操作

保存txt文件为.pcd

    // 保存txt文件为.pcd
    /*
    *   filename: 文件名,全路径
    *   parent_dir: 保存的pcd路径
    */
    bool savetxt2pcd(const std::string filename, const std::string parent_dir)
    {
        pcl::PointCloud<pcl::PointXYZIL>::Ptr cloudil(new pcl::PointCloud<pcl::PointXYZIL>);
        txt2pcd::read_txt_pcd(filename, cloudil);

        auto vec = txt2pcd::split(filename, std::string(".txt"));
        auto filename_vec = txt2pcd::split(vec[0], "/");
        auto sub_dir_name = *(filename_vec.end()-2);
        auto pcd_name = filename_vec.back();
        // for(const auto& ele:vec)
        // {
        //     std::cout<< ele<< ", ";
        // }
        std::cout<<std::endl;

        std::cout<< sub_dir_name<<", " <<pcd_name <<std::endl;

        std::string output_dir = std::string(parent_dir) +"/" +sub_dir_name;
        std::string output_file = pcd_name+std::string(".pcd");

        // 创建文件夹
        // make output directory
        if (!boost::filesystem::exists(boost::filesystem::path(output_dir))) {
            if(boost::filesystem::create_directory(boost::filesystem::path(output_dir)))
                std::cout<<" --- mkdir -p "<<output_dir<<std::endl;
        }
        auto final_name = output_dir+"/"+output_file;
        // std::cout<<" --- final_name " << final_name << std::endl;

        // save
        if(pcl::io::savePCDFile(final_name, *cloudil, true)!=-1)
        {
            std::cout<<" --- save  " << final_name << std::endl;
        }



    }


读取txt到点云

    // 读取txt
    void read_txt_pcd(const std::string filename, pcl::PointCloud<pcl::PointXYZIL>::Ptr cloudil)
    {
        std::ifstream file(filename);
        std::string line;
        pcl::PointXYZIL tmp_point;
        while(std::getline(file, line))
        {
            std::stringstream ss(line);
            ss >> tmp_point.x;
            ss >> tmp_point.y;
            ss >> tmp_point.z;
            ss >> tmp_point.intensity;
            ss >> tmp_point.label;
            cloudil->push_back(tmp_point);

        }
        file.close();
        std::cout<<" --- num: "<< cloudil->size()<<std::endl;
    }

pcl可视化

    // 可视化 xyz i l的数据
    void visualize_xyz_i_l(const pcl::PointCloud<pcl::PointXYZIL>::Ptr cloud_xyzil)
    {
        pcl::PointCloud<pcl::PointXYZRGB>::Ptr ccloud(new pcl::PointCloud<pcl::PointXYZRGB>);
        for (const auto& upt : *cloud_xyzil)
        {
            pcl::PointXYZRGB cpt;
            cpt.x = upt.x;
            cpt.y = upt.y;
            cpt.z = upt.z;
            cpt.r = upt.intensity;
            cpt.g = upt.intensity;
            cpt.b = upt.intensity;
            ccloud->points.push_back(cpt);
        }
        pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));
        viewer->setBackgroundColor(1., 1., 1.);
        pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgbs(ccloud);
        viewer->addPointCloud<pcl::PointXYZRGB>(ccloud, rgbs, "pointcloud");
        while (!viewer->wasStopped())
        {
            viewer->spinOnce(100);
            // boost::this_thread::sleep(boost::posix_time::microseconds(1000));
        }
        viewer->close();
    } 

    void visualize_xyz_l(const pcl::PointCloud<pcl::PointXYZL>::Ptr cloud_xyzl)
    {
        pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));
        viewer->setBackgroundColor(1., 1., 1.);
        pcl::visualization::PointCloudColorHandlerLabelField<pcl::PointXYZL> labels(cloud_xyzl);
        viewer->addPointCloud<pcl::PointXYZL>(cloud_xyzl, labels, "pointcloud");


        viewer->registerPointPickingCallback(txt2pcd::pp_callback_PointsSelect, (void*)&txt2pcd::out_cloud_ptr);
        while(!viewer->wasStopped())
        {
            viewer->spinOnce(100);
        }
        viewer->close();
    }

分割字符串

    std::vector<std::string> split(const std::string &str, const std::string &pattern)
    {
        std::vector<std::string> res;
        if(str == "")
            return res;
        //在字符串末尾也加入分隔符,方便截取最后一段
        std::string strs = str + pattern;
        size_t pos = strs.find(pattern);

        while(pos != strs.npos)
        {
            std::string temp = strs.substr(0, pos);
            res.push_back(temp);
            //去掉已分割的字符串,在剩下的字符串中进行分割
            strs = strs.substr(pos+1, strs.size());
            pos = strs.find(pattern);
        }

        return res;
    }

获取目录下所有文件名

    // 遍历文件夹下所有文件
    /*
    *   dir 存放文件的目录
    *   suffix 文件名后缀
    *   filenames   output 文件名vector
    */
	#include <boost/filesystem.hpp>
	#include <boost/filesystem/path.hpp>

    bool get_filenames(const std::string &dir, const std::string &suffix, std::vector<std::string> &filenames)
    {
        boost::filesystem::path path(dir);
        if(!boost::filesystem::exists(path))
        {
            return false;
        }

        boost::filesystem::directory_iterator end_iter;
        int i = 0;
        for(boost::filesystem::directory_iterator iter(path); iter!=end_iter; ++iter)
        {      
            // std::cout<<i++<<std::endl;
            // std::cout<<"path: "<<iter->path()<<std::endl;

            if(boost::filesystem::extension(iter->path())==suffix)
            {
                if( std::string::npos != std::string(iter->path().c_str()).find("frames"))
                {
                    continue;
                }

                std::cout<<"path: "<<iter->path()<<std::endl;
                filenames.push_back(std::string(iter->path().c_str()));
            }
        }
        return true;
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值