PCLlab—PCD文件的打开和保存操作

序:我们这里实现的功能是打开.pcd文件并显示(显示功能已经在上一节介绍过来),然后选择要另存为的类型,这里支持ply和pcd格式的文件。

1. 文件打开

1.1. MFC设置

1.2. 代码编辑

void CPCLlabDoc::OnFileOpen()
{
    // TODO: Add your command handler code here
    CFileDialog dlg(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        (CString)"pointcloud(*.pcd)|*.pcd||",NULL);

    if(dlg.DoModal() != IDOK)
        return;
    CString filename = dlg.GetPathName();
    g_pcd_io.cloud_ = g_pcd_io.getData(filename.GetString());

    this->UpdateAllViews(0,0,0);//把文档被修改的信息通知给每个视图,刷新显示窗口
}

注:上述代码中我们定义了全局的类对象extern PCD_IO g_pcd_io;其变量定义和getData函数如下

//Variables
public:
    pcl::PCLPointCloud2::Ptr cloud_;
pcl::PCLPointCloud2::Ptr PCD_IO::getData(std::string filename)
{
    pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
    if(pcl::io::loadPCDFile(filename, *cloud) == -1) // load the file
    {
        PCL_ERROR ("Couldn't read file");
    }
    
    return cloud;
}

2. 文件保存

2.1. MFC设置

2.2. 代码编辑

void CPCLlabDoc::OnFileSave()
{
    // TODO: Add your command handler code here
    CFileDialog dlg(false, CString(".ply"), CString("pcd.ply"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        (CString)"polygon(*.ply)|*.ply|pointcloud(*.pcd)|*.pcd||",NULL);

    if(dlg.DoModal() != IDOK)
        return;
    CString filename = dlg.GetPathName();

    bool binary = false;
    bool use_camera = true;
    int pos = filename.ReverseFind('.');
    if(pos == -1)
        return;

    CString ext = filename.Right(filename.GetLength() - pos);
    if(ext == ".ply")
    {
        pcl::PointCloud<pcl::PointXYZRGB>::Ptr out(new pcl::PointCloud<pcl::PointXYZRGB>);
        pcl::PointCloud<pcl::PointXYZRGB>::Ptr result(new pcl::PointCloud<pcl::PointXYZRGB>);
        std::vector<int> index;
        pcl::fromPCLPointCloud2(*g_pcd_io.cloud_, *out);
        pcl::removeNaNFromPointCloud(*out, *result, index);//删除nan的点,否则meshlab无法打开
        pcl::PLYWriter writer;
        writer.write(filename.GetString(), *result);
        //writer.write (filename.GetString(), m_pcd_io.cloud_, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), binary, use_camera);
    }
    else if(ext == ".pcd")
    {
        pcl::io::savePCDFile(filename.GetString(), *g_pcd_io.cloud_, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), binary);
    }
}

 

转载请注明http://www.cnblogs.com/pcl-lab/articles/3965081.html

转载于:https://www.cnblogs.com/pcl-lab/articles/3965081.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值