记录一下PCL与Qt结合的学习历程
代码实现
QVTK.h
#pragma once
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/pcl_visualizer.h>
#include <QtWidgets/QMainWindow>
#include "ui_QVTK.h"
class QVTK : public QMainWindow
{
Q_OBJECT
public:
QVTK(QWidget *parent = 0);
~QVTK();
private:
Ui::QVTKClass ui;
//------------点云数据存储---------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
//------------初始化vtk部件--------------
void initialVtkWidget();
private slots:
//-------------创建打开槽----------------
void onOpen();
};
QVTK.cpp
#include "QVTK.h"
#include <QFileDialog>
#include <iostream>
#include <vtkRenderWindow.h>
QVTK::QVTK(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//初始化
initialVtkWidget();
//连接信号和槽
connect(ui.actionOpen, SIGNAL(clicked()), this, SLOT(onOpen()));
}
QVTK::~QVTK()
{
}
void QVTK::initialVtkWidget()
{
cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
viewer->setBackgroundColor(0, 0, 0); //设置背景颜色为黑色
ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());//****核心代码*****
viewer->setupInteractor(ui.qvtkWidget->GetInteractor(), ui.qvtkWidget->GetRenderWindow());//******核心代码*******
ui.qvtkWidget->update();
}
//---------------加载点云数据--------------------
void QVTK::onOpen()
{
//只能打开PCD文件
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open PointCloud"), ".",
tr("Open PCD files(*.pcd)"));
if (!fileName.isEmpty())
{
std::string file_name = fileName.toStdString();
pcl::io::loadPCDFile(file_name, *cloud);
//-----------------2---------------------
//移除窗口点云(可实现添加多个点云)
viewer->removeAllPointClouds();
//设置点云颜色
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color(cloud, 255, 0, 0);
//点云颜色渲染
viewer->addPointCloud(cloud, cloud_color, "cloud");
//设置点云大小
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud");
viewer->resetCamera();
ui.qvtkWidget->update();
}
}
main.cpp
#include "QVTK.h"
#include <QtWidgets/QApplication>
#include <vtkOutputWindow.h>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QTextCodec* code = QTextCodec::codecForName("GB2312");
vtkOutputWindow::SetGlobalWarningDisplay(0);
QApplication a(argc, argv);//定义并创建应用程序
QVTK w; //定义并创建窗口
w.show(); //显示窗口
return a.exec(); //应用程序运行
}