Skip to content

Instantly share code, notes, and snippets.

@Logrus
Created April 5, 2014 07:27
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Logrus/9988524 to your computer and use it in GitHub Desktop.
Save Logrus/9988524 to your computer and use it in GitHub Desktop.
PCL Viewer with Qt GUI minimal code
cmake_minimum_required(VERSION 2.6)
PROJECT(qt_vtk_pcl)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
find_package(PCL 1.7 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
FIND_PACKAGE(VTK)
INCLUDE(${VTK_USE_FILE})
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
file(GLOB UI_FILES *.ui)
file(GLOB QT_WRAP *.h)
file(GLOB CXX_FILES *.cpp)
QT4_WRAP_UI(UISrcs ${UI_FILES})
QT4_WRAP_CPP(MOCSrcs ${QT_WRAP})
ADD_EXECUTABLE(qt_vtk_pcl ${CXX_FILES} ${UISrcs} ${MOCSrcs})
TARGET_LINK_LIBRARIES(qt_vtk_pcl QVTK ${PCL_LIBRARIES})
#include "pclwindow.h"
#include "build/ui_pclwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <pcl/point_types.h>
#include <pcl/common/common.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/pcd_io.h>
#include <vtkRenderWindow.h>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
pcl::visualization::PCLVisualizer pviz ("test_viz", false);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz (new pcl::PointCloud<pcl::PointXYZ>);
Pclwindow::Pclwindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Pclwindow)
{
ui->setupUi(this);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz (new pcl::PointCloud<pcl::PointXYZ>);
for (float y = -0.5f; y <= 0.5f; y += 0.01f)
{
for (float z = -0.5f; z <= 0.5f; z += 0.01f)
{
pcl::PointXYZ point;
point.x = 2.0f - y;
point.y = y;
point.z = z;
cloud_xyz->points.push_back (point);
}
}
cloud_xyz->width = cloud_xyz->points.size ();
cloud_xyz->height = 1;
vtkSmartPointer<vtkRenderWindow> renderWindow = pviz.getRenderWindow();
ui->widget->SetRenderWindow (renderWindow);
pviz.setupInteractor (ui->widget->GetInteractor (), ui->widget->GetRenderWindow ());
pviz.getInteractorStyle ()->setKeyboardModifier (pcl::visualization::INTERACTOR_KB_MOD_SHIFT);
pviz.addPointCloud<pcl::PointXYZ>(cloud_xyz);
pviz.setBackgroundColor(0, 0, 0.1);
ui->widget->show();
}
Pclwindow::~Pclwindow()
{
delete ui;
}
void Pclwindow::on_action_triggered()
{
this->close();
}
void Pclwindow::on_action_2_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Files (*.pcd)"));
if (fileName != "")
{
pviz.removeAllPointClouds();
ui->widget->setDisabled(true);
ui->menuBar->setDisabled(true);
boost::thread t(boost::bind(pcl::io::loadPCDFile<pcl::PointXYZ>, fileName.toStdString(), boost::ref(*cloud_xyz)));
//for (int i = 0; i <= (int) sec; i++)
//{
// ui->progressBar->setValue((int) ((i/sec)*100));
// ui->label->setText(QString::fromStdString("Wait for "+boost::lexical_cast<std::string>(((int)sec)-i)+" sec"));
//boost::this_thread::sleep(boost::posix_time::seconds(1)); // wait 1 sec
//}
t.join();
ui->progressBar->setValue(100);
ui->widget->setEnabled(true);
ui->menuBar->setEnabled(true);
if (cloud_xyz->size() > 0)
{
pviz.addPointCloud<pcl::PointXYZ>(cloud_xyz);
ui->progressBar->setValue(0);
}
};
}
#ifndef PCLWINDOW_H
#define PCLWINDOW_H
#include <QMainWindow>
namespace Ui {
class Pclwindow;
}
class Pclwindow : public QMainWindow
{
Q_OBJECT
public:
explicit Pclwindow(QWidget *parent = 0);
~Pclwindow();
private slots:
void on_action_triggered();
void on_action_2_triggered();
private:
Ui::Pclwindow *ui;
};
#endif // PCLWINDOW_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Pclwindow</class>
<widget class="QMainWindow" name="Pclwindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>710</width>
<height>414</height>
</rect>
</property>
<property name="windowTitle">
<string>Pclwindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QVTKWidget" name="widget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>601</width>
<height>271</height>
</size>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>710</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string>File</string>
</property>
<addaction name="action_2"/>
<addaction name="action"/>
</widget>
<addaction name="menu"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="action">
<property name="text">
<string>Exit</string>
</property>
</action>
<action name="action_2">
<property name="text">
<string>Open file</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QVTKWidget</class>
<extends>QWidget</extends>
<header>QVTKWidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
#include <QApplication>
#include "pclwindow.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Pclwindow w;
w.show();
app.exec();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment