Part 4 MFC+VTK+VS2010 测试VTK的MFC对话框程序
1、新建空的对话框程序PointCloudViewer,添加一个按钮Openpcd,和一个picture control ,ID:IDC_PCDVIEWER。
头文件PointCloudViewer.h
<span style="font-size:14px;">// PointCloudViewerDlg.h : 头文件
//
#include "pcl\point_cloud.h"
#include "pcl\point_types.h"
#include "pclvisualization_mfc\include\pcl_mfc_visualizer.h"
//#include "pclvisualization_mfc\include\pcl_mfc_visualizer.hpp"
#include "vtkRenderer.h"
#include <pcl/io/pcd_io.h>
#pragma once
// CPointCloudViewerDlg 对话框
class CPointCloudViewerDlg : public CDialogEx
{
// 构造
public:
CPointCloudViewerDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum { IDD = IDD_POINTCLOUDVIEWER_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
private:
//视图窗口
pcl::mfc_visualization::PCLVisualizer *viewer;
//vtkRenderer *pvtkRenderer;
POINT ptBorder;
//数据点
sensor_msgs::PointCloud2::Ptr binary_blob;
//数据点句柄
pcl::mfc_visualization::PointCloudGeometryHandlerXYZ<sensor_msgs::PointCloud2>::Ptr xyz_Handler;
pcl::mfc_visualization::PointCloudColorHandlerRGBField<sensor_msgs::PointCloud2>::Ptr color_Handler;
//传感器位置方向矩阵
Eigen::Vector4f sensor_origin;
Eigen::Quaternion<float> sensor_orientation;
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnBnClickedOpenpcd();
};</span><span style="font-size:18px;">
</span>
2、PointCloudViewer.cpp
<span style="font-size:14px;">// PointCloudViewerDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "PointCloudViewer.h"
#include "PointCloudViewerDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CPointCloudViewerDlg 对话框
CPointCloudViewerDlg::CPointCloudViewerDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CPointCloudViewerDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//begin tyz
//初始化
this->viewer = NULL;
sensor_origin = Eigen::Vector4f::Zero();
sensor_orientation = Eigen::Quaternionf::Identity ();
}
void CPointCloudViewerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CPointCloudViewerDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SIZE()
ON_BN_CLICKED(IDC_OPENPCD, &CPointCloudViewerDlg::OnBnClickedOpenpcd)
END_MESSAGE_MAP()
// CPointCloudViewerDlg 消息处理程序
BOOL CPointCloudViewerDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
//利用PictControl控件加载PCD窗口
CWnd *viewer_pcWnd;
viewer_pcWnd = this->GetDlgItem(IDC_PCDVIEWER);
this->viewer = new pcl::mfc_visualization::PCLVisualizer(viewer_pcWnd);
CRect cRectPCL;
this->viewer->GetClientRect(&cRectPCL);
CRect cRectClient;
GetClientRect(&cRectClient);
this->ptBorder.x = cRectClient.Width() - cRectPCL.Width();
this->ptBorder.y = cRectClient.Height() - cRectPCL.Height();
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
ShowWindow(SW_MAXIMIZE);
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CPointCloudViewerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CPointCloudViewerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CPointCloudViewerDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CPointCloudViewerDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if (::IsWindow(this->GetSafeHwnd()))
{
if (this->viewer)
{
cx -= ptBorder.x;
cy -= ptBorder.y;
this->GetDlgItem(IDC_PCLVIWER)->SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
this->viewer->SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
}
}
}
void CPointCloudViewerDlg::OnBnClickedOpenpcd()
{
// TODO: 在此添加控件通知处理程序代码
this->viewer->removeAllPointClouds ();
// TODO: Add your control notification handler code here
static TCHAR BASED_CODE szFilter[] = _T("PCD (*.pcd )|*.pcd||");
CFileDialog cFileDialog(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT| OFN_NOCHANGEDIR ,szFilter);
if (cFileDialog.DoModal() == IDOK)
{
/
//文档名称
std::string filename;
filename = cFileDialog.GetFileName();
//reset data
this->binary_blob.reset();
binary_blob = sensor_msgs::PointCloud2::Ptr (new sensor_msgs::PointCloud2);
// read new data
//*.pcd文件
pcl::PCDReader pcd_reader;
if (pcd_reader.read ((char*)_bstr_t(filename.c_str()), *binary_blob) != 0) //* load the file
{
MessageBox (_T("Couldn't read PCData file!"));
return;
}
}
if (binary_blob == NULL)
{
MessageBox("Please load PCD file firstly!");
return;
}
else
{
//其他句柄
if (pcl::getFieldIndex(*binary_blob, "rgb") > 0)
{
color_Handler = pcl::mfc_visualization::PointCloudColorHandlerRGBField<sensor_msgs::PointCloud2>::Ptr
(new pcl::mfc_visualization::PointCloudColorHandlerRGBField<sensor_msgs::PointCloud2> (binary_blob));
this->viewer->addPointCloud(binary_blob, color_Handler, sensor_origin, sensor_orientation);
}
else
{
xyz_Handler = pcl::mfc_visualization::PointCloudGeometryHandlerXYZ<sensor_msgs::PointCloud2>::Ptr
(new pcl::mfc_visualization::PointCloudGeometryHandlerXYZ<sensor_msgs::PointCloud2> (binary_blob));
this->viewer->addPointCloud(binary_blob, xyz_Handler, sensor_origin, sensor_orientation);
}
this->viewer->resetCamera();
}
}
</span>
3、配置文件
1)添加头文件:项目右击—>属性—>C/C++—>附加包含目录:
D:\Program Files\PCL 1.6.0\3rdParty\Boost\include;
D:\Program Files\PCL 1.6.0\3rdParty\Eigen\include;
D:\Program Files\PCL 1.6.0\3rdParty\FLANN\include;
D:\Program Files\PCL 1.6.0\3rdParty\Qhull\include;
D:\Program Files\PCL 1.6.0\3rdParty\VTK\include\vtk-5.8;
D:\Program Files\OpenNI\Include;
D:\Program Files\PCL 1.6.0\include\pcl-1.6;
D:\C++Workspace\VCLESSON\PCL\PointCloudViewer\PointCloudViewer\pclvisualization_mfc\include
2)添加bin中的文件:项目右击—>属性—>链接器—>常规—>附加库目录:
D:\Program Files\PCL 1.6.0\3rdParty\Boost\lib;
D:\Program Files\PCL 1.6.0\3rdParty\Qhull\lib;
D:\Program Files\PCL 1.6.0\3rdParty\FLANN\lib;
D:\Program Files\PCL 1.6.0\3rdParty\VTK\lib\vtk-5.8;
D:\Program Files\PCL 1.6.0\lib;D:\Program Files\OpenNI\Lib64;
D:\VTK\VTKbin\bin\Debug
3)添加lib文件:项目右击—>属性—>链接器—>输入—>附加依赖项 添加:
opengl32.lib;
pcl_kdtree_debug.lib;
pcl_io_debug.lib;
pcl_search_debug.lib;
pcl_segmentation_debug.lib;
pcl_apps_debug.lib;
pcl_features_debug.lib;
pcl_filters_debug.lib;
pcl_visualization_debug.lib;
pcl_common_debug.lib;
flann_cpp_s-gd.lib;
libboost_system-vc100-mt-gd-1_47.lib;
libboost_filesystem-vc100-mt-gd-1_47.lib;
libboost_thread-vc100-mt-gd-1_47.lib;
libboost_date_time-vc100-mt-gd-1_47.lib;
libboost_iostreams-vc100-mt-gd-1_47.lib;
vtkalglib-gd.lib;
vtkCharts-gd.lib;
vtkCommon-gd.lib;
vtkDICOMParser-gd.lib;
vtkexoIIc-gd.lib;
vtkexpat-gd.lib;
vtkFiltering-gd.lib;
vtkfreetype-gd.lib;
vtkftgl-gd.lib;
vtkGenericFiltering-gd.lib;
vtkGeovis-gd.lib;
vtkGraphics-gd.lib;
vtkhdf5-gd.lib;
vtkHybrid-gd.lib;
vtkImaging-gd.lib;
vtkInfovis-gd.lib;
vtkIO-gd.lib;
vtkjpeg-gd.lib;
vtklibxml2-gd.lib;
vtkmetaio-gd.lib;
vtkNetCDF-gd.lib;
vtkNetCDF_cxx-gd.lib;
vtkpng-gd.lib;
vtkproj4-gd.lib;
vtkRendering-gd.lib;
vtksqlite-gd.lib;
vtksys-gd.lib;
vtktiff-gd.lib;
vtkverdict-gd.lib;
vtkViews-gd.lib;
vtkVolumeRendering-gd.lib;
vtkWidgets-gd.lib;
vtkzlib-gd.lib;%(AdditionalDependencies)