我的编译环境是VS2013+PCL1.8.0.
首先是你创建的对话框类的.h文件:
#pragma once
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/cloud_viewer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
using namespace std;
using namespace pcl;
using namespace io;
// PCL 对话框
class PCL : public CDialogEx
{
DECLARE_DYNAMIC(PCL)
public:
PCL(CWnd* pParent = NULL); // 标准构造函数
virtual BOOL OnInitDialog(); //这边需要定义一个初始化函数
virtual ~PCL();
// 对话框数据
enum { IDD = IDD_PCLSHOW };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer; //申明VTK变量
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ>> m_cloud;
vtkRenderWindow* renwin;
vtkRenderWindowInteractor* iren;
afx_msg void OnBnClickedShow();
};
其次是.cpp文件
前面的都不需要修改
只需在BEGIN_MESSAGE_MAP(PCL, CDialogEx)
ON_BN_CLICKED(IDC_SHOW, &PCL::OnBnClickedShow)
END_MESSAGE_MAP()后面添加如下代码:
BOOL PCL::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// TODO: 在此添加额外的初始化代码
viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
//m_cloud=new pcl::PointCloud<pcl::PointXYZ>;
m_cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
viewer->setBackgroundColor(255, 255, 255); //背景颜色
viewer->addCoordinateSystem(1.0f);
// TODO: adddraw code for native data here
CStatic *pclStatic = new CStatic();
LPRECT rect = new CRect;
pclStatic = (CStatic*)GetDlgItem(IDC_STATIC);
renwin = viewer->getRenderWindow();
renwin->SetParentId(pclStatic->m_hWnd);
iren->GlobalWarningDisplayOff();
iren = vtkRenderWindowInteractor::New();
pclStatic->GetWindowRect(rect);
renwin->SetSize(rect->right - rect->left, rect->bottom - rect->top);
renwin->SetPosition(0, 0);
iren->SetRenderWindow(renwin);
viewer->createInteractor();
iren->Render();
renwin->Render();
//iren->SetGlobalWarningDisplay(0);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
CString filePath; //定义一个打开文件的全局变量
void PCL::OnBnClickedShow() //按钮控件
{
// TODO: 在此添加控件通知处理程序代码
//打开文件*.pcd *.ply *.obj
CString defaultDir = _T("请选择路径"); //设置默认打开文件夹
CString fileFilter = _T("文件(*.pcd;*.ply;*.obj)|*.pcd;*.ply;*.obj|All File (*.*)|*.*||"); //设置文件过滤
CFileDialog fileDlg(true, defaultDir, _T(""), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, fileFilter, NULL);
//弹出选择文件对话框
if (fileDlg.DoModal() == IDOK)
{
filePath = fileDlg.GetPathName();//得到完整的文件名和目录名拓展名
CString filename = fileDlg.GetFileName();
}
std::string file = CT2A(filePath.GetBuffer()); //CString格式转String格式
//std::string filename = "huba_part.pcd";
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPCDFile(file, *cloud); //载入点云
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 0, 0); //设置点云颜色
viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "sample cloud");
viewer->resetCamera();//使点云显示在屏幕中间,并绕中心操作
iren->Render();
renwin->Render();
}
下面是效果图:
需要源码的私信我。
作者:雨田
出处:https://blog.csdn.net/weixin_43341380/article/details/93972717
版权所有,欢迎保留原文链接进行转载