序:我們這里實現的功能是打開.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); } }