序:我们这里实现的功能是打开.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); } }