MFC 打开与保存点云PCD文件

关键一句代码:

CFileDialog FileDlg(true, sDefaultDir, _T(""), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, sFileFilter, NULL);

CFileDialog 第一个参数为Ture时代表打开文件,为false时代表保存文件

添加点云数据之前,需要清空原有的数据

m_spViewer->removeAllPointClouds();

打开PCD,并显示于界面上的VTK窗口

void CPointCloud::PC_OpenPointCloud(boost::shared_ptr& m_spViewer,
	vtkRenderWindowInteractor* m_rwndinIren,
	vtkRenderWindow* m_rwndRenWin, CString& sFilePath)
{
	//打开文件*.pcd *.ply *.obj
	CString sDefaultDir = _T("请选择路径"); //设置默认打开文件夹
	CString sFileFilter = _T("文件(*.pcd;*.ply;*.obj)|*.pcd;*.ply;*.obj|All File (*.*)|*.*||"); //设置文件过滤
	CFileDialog FileDlg(true, sDefaultDir, _T(""), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, sFileFilter, NULL);
	//弹出选择文件对话框
	if (FileDlg.DoModal() == IDOK)
	{
		sFilePath = FileDlg.GetPathName();//得到完整的文件名和目录名拓展名  
		CString sFilename = FileDlg.GetFileName();
	}
	string sFile = CT2A(sFilePath.GetBuffer());   //CString格式转String格式
	//std::string sFilename = "huba_part.pcd";
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::io::loadPCDFile(sFile, *cloud);          //载入点云

	pcl::visualization::PointCloudColorHandlerCustom single_color(cloud, 255, 255, 255);	//设置点云颜色
    //初始化点云数据
	m_spViewer->removeAllPointClouds();
	//添加点云数据
	m_spViewer->addPointCloud(cloud, single_color, "sample cloud");
	m_spViewer->resetCamera();//使点云显示在屏幕中间,并绕中心操作
	m_rwndinIren->Render();
	m_rwndRenWin->Render();
}

保存PCD:

void CApplicationDlg::OnBnClickedSavefile()
{

	//保存文件*.pcd *.ply *.obj
	CString sDefaultDir = _T("请选择路径"); //设置默认打开文件夹
	CString sFileFilter = _T("文件(*.pcd;*.ply;*.obj)|*.pcd;*.ply;*.obj|All File (*.*)|*.*||"); //设置文件过滤
	CFileDialog FileDlg(false, sDefaultDir, _T(""), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, sFileFilter, NULL);
	CString filename;
	if (FileDlg.DoModal() != IDOK)
		 filename = FileDlg.GetPathName();

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

	CString ext = filename.Right(filename.GetLength() - pos);
	pcl::io::savePCDFileASCII(filename.GetString(), *CloudFull);
}

你可能感兴趣的:(C++,点云PCL,MFC,mfc,c++,开发语言)