[Part 4] 在Windows 10上源码编译PCL 1.8.1支持VTK和QT,可视化三维点云

本文首发于个人博客https://kezunlin.me/post/2d809f92/,欢迎阅读!

Part-4: Compile pcl with vtk qt5 support from source on windows.

Series

Guide

Tips

we'd better to use SSD to compile source code instead HDD. So put source code in C folder on windows.
use multiple processor to compile with /MP options.

prerequisit

software

download

wget https://www.vtk.org/files/release/8.1/VTK-8.1.0.zip
wget https://www.vtk.org/files/release/8.1/VTKData-8.1.0.zip
wget https://github.com/PointCloudLibrary/pcl/archive/pcl-1.8.1.zip

version

window: 10
qt: 5.7.1
qmake: 3.0 based on Qt 5.7.1
qtcreator: 4.2.0 based on Qt 5.7.1

qt-vs-tools: 2.2.1 
cmake/gui: 3.10.0

vtk: 8.1.0 (source)
vtk data: 8.1.0
pcl: 1.8.1 (source)

$ qmake -v
QMake version 3.0
Using Qt version 5.7.1 in C:/Qt/Qt5.7.1/5.7/msvc2015_64/lib

$ qtcreator -version
Qt Creator 4.2.0 based on Qt 5.7.1

$ cmake -version
cmake version 3.10.0

install vs2015

skip.

configure vs2015 with admin

when we compile pcl by vs2015 and install to folder C:/Program Files/PCL , we need administrative priviledges.
follow steps:

cmd+r 
regeidt
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

# new string entry
key: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe
value: ~ RUNASADMIN

vs2015 regedit for runasadmin

install qt

install qt to path C:\Qt\Qt5.7.1

add the following to system path.

C:\Qt\Qt5.7.1\5.7\msvc2015_64\bin
C:\Qt\Qt5.7.1\Tools\QtCreator\bin

install qt-vs-tools

online install by vs2015

start vs2015, install Qt Visual Sutido Tools by:

Tools---> Extensions and Update... ---> online search qt ---> install Qt Visual Sutido Tools ---> OK

restart vs2015 and new menu Qt VS Tools will be added to VS Main menu.

offline install by vsix

wget https://mirrors.tuna.tsinghua.edu.cn/qt/archive/vsaddin/qt-vs-tools-msvc2015-2.1.1.vsix

double click qt-vs-tools-msvc2015-2.1.1.vsix and install.

configure qt for vs

follow steps

Qt VS Tools ---> Qt Options ---> Add name and path

Add qt path for vs2015 so that we can find %PATH%\bin\qmake.exe

Name: Qt 5.7.1
Path: C:\Qt\Qt5.7.1\5.7\msvc2015_64\

qt options

new qt project from vs

start vs2015, and new Qt Gui Application.

new qt gui application

Compile and run first qt program.
Success.

install pcl third parties

install pcl by installer

install pcl to C:\Program Files\PCL 1.8.1\

choose to install OpenNI2 to C:\Program Files\PCL 1.8.1\3rdParty\OpenNI2\

this process will add 4 system env.

PCL_ROOT          C:\Program Files\PCL 1.8.1
OPENNI2_INCLUDE64 C:\Program Files\PCL 1.8.1\3rdParty\OpenNI2\Include\
OPENNI2_LIB64     C:\Program Files\PCL 1.8.1\3rdParty\OpenNI2\Lib\ 
OPENNI2_REDIST64  C:\Program Files\PCL 1.8.1\3rdParty\OpenNI2\Redist\

configure env

add env to Path.

%PCL_ROOT\bin
%PCL_ROOT%\3rdParty\FLANN\bin
%PCL_ROOT%\3rdParty\OpenNI2\Redist
%PCL_ROOT%\3rdParty\OpenNI2\Tools
%PCL_ROOT%\3rdParty\Qhull\bin
%PCL_ROOT%\3rdParty\VTK\bin

copy winpap package

copy winpap to C:\Program Files\PCL 1.8.1\3rdParty\, and 3rdParty folder looks like this:

$ ls -al

total 16
drwxr-xr-x 1 zunli 197609 0 1月  11 12:47 ./
drwxr-xr-x 1 zunli 197609 0 1月  11 08:03 ../
drwxr-xr-x 1 zunli 197609 0 1月  11 08:02 Boost/
drwxr-xr-x 1 zunli 197609 0 1月  11 08:01 Eigen/
drwxr-xr-x 1 zunli 197609 0 1月  11 08:02 FLANN/
drwxr-xr-x 1 zunli 197609 0 1月  11 08:04 OpenNI2/
drwxr-xr-x 1 zunli 197609 0 1月  11 08:02 Qhull/
drwxr-xr-x 1 zunli 197609 0 1月  11 08:03 VTK/
drwxr-xr-x 1 zunli 197609 0 1月  11 12:47 winpcap/

compile vtk

download vtk and data

wget https://www.vtk.org/files/release/8.1/VTK-8.1.0.zip
wget https://www.vtk.org/files/release/8.1/VTKData-8.1.0.zip

configure source

we need to compile vtk to replace C:\Program Files\PCL 1.8.1\3rdParty\VTK\

cd c:/compile/VTK-8.1.0
mkdir build && cd build 
cmake-gui ..

configure generator to Visual Studio 14 2015 Win64 with options

VTK_Group_Qt  ON
VTK_QT_VERSION     5  # by default 4
VTK_RENDERING_BACKEND OpenGL2 # by default OpenGL
Qt5_DIR  C:/Qt/Qt5.7.1/5.7/msvc2015_64/lib/cmake/Qt5

CMAKE_INSTALL_PREFIX  C:/Program Files/VTK  # by default
BUILD_SHARED_LIBS  ON
CMAKE_CONFIGURATION_TYPES Release # Release
CMAKE_CXX_FLAGS_RELEASE /MD /O2 /Ob2 /DNDEBUG /MP # for multiple processor

unzip VTKData-8.1.0.zip and copy MD5 to VTK-8.1.0/build/ExternalData/Objects/MD5

(1) With VTK = 8.1.0:

VTK_Group_Qt  ON

configure again, and then QT_QMAKE_EXECUTABLE and VTK_QT_VERSION will appear.

(2) we need to change VTK_QT_VERSION from 4 to 5.

VTK_QT_VERSION     5  # by default 4

(3) change VTK_RENDERING_BACKEND from OpenGL to OpenGL2.

VTK_RENDERING_BACKEND OpenGL2 # by default OpenGL

(4) set install path C:\Program Files\VTK\

CMAKE_INSTALL_PREFIX  C:/Program Files/VTK  

we need write privileges to install to C:\Program Files\VTK\

otherwise, when we install, error will occur:

1>  -- Install configuration: "Release"
1>  CMake Error at cmake_install.cmake:36 (file):
1>    file cannot create directory: C:/Program Files/VTK/lib/cmake/vtk-8.0.
1>    Maybe need administrative privileges.

(5) we should also notice BUILD_SHARED_LIBS

BUILD_SHARED_LIBS  ON

(6) add /MP for multiple processor compile

CMAKE_CXX_FLAGS_RELEASE /MD /O2 /Ob2 /DNDEBUG /MP # for multiple processor

# only exist for VTK
CMAKE_CXX_MP_FLAG ON
CMAKE_CXX_MP_NUM_PROCESSORS 8

(7) Configure and warning will occur

CMake Deprecation Warning at ThirdParty/libproj4/vtklibproj4/cmake/policies.cmake:2 (cmake_policy):
  The OLD behavior for policy CMP0022 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.
Call Stack (most recent call first):
  ThirdParty/libproj4/vtklibproj4/CMakeLists.txt:43 (include)

We can omit this warning.

compile

If everything goes well, then generate VTK.sln.

Open VTK.sln in vs2015 and build with Release X64.

NOTICE: this build process will take about 10 minutes.(10:35-10:45)

... waiting about 10 minutes ...
========== 生成: 成功 127 个,失败 0 个,最新 0 个,跳过 0 个 ==========

========== 生成: 成功 192 个,失败 0 个,最新 4 个,跳过 0 个 ==========

install vtk

choose INSTALL and generate , and VTK will be installed to C:\Program Files\VTK\.

Notice for QVTKWidgetPlugin and copy dll and lib to C:\Program Files\VTK\.

  • dll: copy C:\compile\VTK-8.1.0\build\bin\Release\QVTKWidgetPlugin.dll to C:\Program Files\VTK\bin\QVTKWidgetPlugin.dll
  • lib: copy C:\compile\VTK-8.1.0\build\lib\Release\QVTKWidgetPlugin.libto C:\Program Files\VTK\lib\QVTKWidgetPlugin.lib

we should also notice that dll has been copied to C:\Program Files\VTK\plugins\designer\QVTKWidgetPlugin.dll

copy vtk to pcl

copy vtk from C:\Program Files\VTK\ to C:/Program Files/PCL 1.8.1/3rdParty/VTK to replace old one.

install QVTKWidgetPlugin

copy C:\Program Files\VTK\plugins\designer\QVTKWidgetPlugin.dll to
C:\Qt\Qt5.7.1\5.7\msvc2015_64\plugins\designer

start vs2015 again, view mainwindow.ui in Qt Designer.

Now QVTKWidget will occur.

qvtkwidget_in_qt_designer

compile pcl

3rdParty

install pcl to get 3rdparty libraries and copy to proper folder.

  • Boost: C:/Boost/include lib version: 1.64 (auto find by cmake)
  • eigen3: C:/Program Files/Eigen/eigen3, version: 3.3.4 (auto find by cmake)
  • FLANN: C:/Program Files/FLANN/include lib bin (auto find by cmake)
  • OpenNI2: C:/Program Files/OpenNI2/Include Lib Redist (user input)
  • Qhull: C:/Program Files/Qhull/include lib bin (auto find by cmake)
  • winpcap: C:/Program Files/winpcap/Include Lib (user input)

cmake options

OpenNI2 include: C:/Program Files/OpenNI2/Include
OpenNI2 LIBRARY: C:/Program Files/OpenNI2/Lib/OpenNI2.lib

pcap  include: C:/Program Files/winpcap/Include
pcap  library: C:/Program Files/winpcap/Lib/x64/wpcap.lib

configure

cd c:/compile/pcl-1.8.1
mkdir build && cd build
cmake-gui ..

with options

QT_USE_FILE C:/compile/pcl-1.8.1/build/use-qt5.cmake
VTK_DIR  C:/Program Files/VTK/lib/cmake/vtk-8.1
CMAKE_INSTALL_PREFIX  C:/Program Files/PCL 

PCL_SHARED_LIBS       ON
PCL_QT_VERSION        5
PCL_ENABLE_SSE        ON

CMAKE_BUILD_TYPE       Release
CMAKE_CONFIGURATION_TYPES Release
CMAKE_INSTALL_PREFIX  C:/Program Files/PCL
CMAKE_CXX_FLAGS_RELEASE /MD /O2 /Ob2 /DNDEBUG /MP # for multiple processor

Build_visualization ON
Build_apps OFF
Build_examples OFF # error may occur

# Fix zlib and png
zlib include: C:/Program Files/VTK/include/vtk-8.1/vtkzlib
zlib library: C:/Program Files/VTK/lib/vtkzlib-8.1.lib

png include:  C:/Program Files/VTK/include/vtk-8.1/vtkpng
png library:  C:/Program Files/VTK/lib/vtkpng-8.1.lib

(1) configure include and library

boost include: C:/Program Files/PCL 1.8.1/3rdParty/Boost/include/boost-1_64 
boost library: C:/Program Files/PCL 1.8.1/3rdParty/Boost/lib

boost xxx library: C:/Program Files/PCL 1.8.1/3rdParty/Boost/lib/xxx.lib 
......
......

eigen include: C:/Program Files/PCL 1.8.1/3rdParty/Eigen/eigen3

FLANN include: C:/Program Files/PCL 1.8.1/3rdParty/FLANN/include
FLANN library: C:/Program Files/PCL 1.8.1/3rdParty/FLANN/lib/flann_cpp.lib  # NOTICE
# _s means static, no _s means dynamic. _gd means debug

OpenNI2 include: C:/Program Files/PCL 1.8.1/3rdParty/OpenNI2/Include
OpenNI2 LIBRARY: C:/Program Files/PCL 1.8.1/3rdParty/OpenNI2/Lib/OpenNI2.lib

qhull include: C:/Program Files/PCL 1.8.1/3rdParty/Qhull/include
qhull library: C:/Program Files/PCL 1.8.1/3rdParty/Qhull/lib/qhullstatic.lib        # NOTICE

pcap  include: C:/Program Files/PCL 1.8.1/3rdParty/winpcap/Include
#pcap  library: C:/Program Files/PCL 1.8.1/3rdParty/winpcap/Lib/x64/Packet.lib;C:/Program Files/PCL 1.8.1/3rdParty/winpcap/Lib/x64/wpcap.lib # 2 NOTICE 
pcap  library: C:/Program Files/PCL 1.8.1/3rdParty/winpcap/Lib/x64/wpcap.lib # 2 NOTICE 

(2) configure qt

QT_USE_FILE C:/pcl-1.8.1/build/use-qt5.cmake
PCL_QT_VERSION 5

(3) configure vtk dir

VTK_DIR  C:/Program Files/PCL 1.8.1/3rdParty/VTK/lib/cmake/vtk-8.0
# for finding VTKConfig.cmake
# VTK_MAJOR_VERSION 8, rendering backend: OpenGL2

(4) multiple processor compile with /MP

CMAKE_CXX_FLAGS_RELEASE /MD /O2 /Ob2 /DNDEBUG /MP # for multiple processor

(5) Configure and errors may occur:

Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)                 # OMIT
Could NOT find LIBUSB_1 (missing: LIBUSB_1_LIBRARY LIBUSB_1_INCLUDE_DIR)  # OMIT
Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR) 
Could NOT find PNG (missing: PNG_LIBRARY PNG_PNG_INCLUDE_DIR) 

(6) Fix zlib and png

zlib include: C:/Program Files/PCL 1.8.1/3rdParty/VTK/include/vtk-8.0/vtkzlib
zlib library: C:/Program Files/PCL 1.8.1/3rdParty/VTK/lib/vtkzlib-8.0.lib

png include:  C:/Program Files/PCL 1.8.1/3rdParty/VTK/include/vtk-8.0/vtkpng
png library:  C:/Program Files/PCL 1.8.1/3rdParty/VTK/lib/vtkpng-8.0.lib

(7) build examples and apps (for now, we don't build to save time)

Build_visualization ON
Build_apps ON
Build_examples ON

(8) configure will output

Selecting Windows SDK version 10.0.14393.0 to target Windows 10.0.15063.
Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) 
Found OpenMP
Eigen found (include: C:\Program Files\PCL 1.8.1\3rdParty\Eigen\eigen3, version: 3.3.4)
FLANN found (include: C:/Program Files/PCL 1.8.1/3rdParty/FLANN/include, lib: optimized;C:/Program Files/PCL 1.8.1/3rdParty/FLANN/lib/flann_cpp_s.lib;debug;C:/Program Files/PCL 1.8.1/3rdParty/FLANN/lib/flann_cpp_s-gd.lib)
Could NOT find LIBUSB_1 (missing: LIBUSB_1_LIBRARY LIBUSB_1_INCLUDE_DIR) 
OpenNI 2 found (include: C:/Program Files/PCL 1.8.1/3rdParty/OpenNI2/Include, lib: C:/Program Files/PCL 1.8.1/3rdParty/OpenNI2/Lib/OpenNI2.lib, redist: C:\Program Files\PCL 1.8.1\3rdParty\OpenNI2\Redist\)
QHULL found (include: C:/Program Files/PCL 1.8.1/3rdParty/Qhull/include, lib: optimized;C:/Program Files/PCL 1.8.1/3rdParty/Qhull/lib;debug;C:/Program Files/PCL 1.8.1/3rdParty/Qhull/lib)
Found CUDA Toolkit v8.0
CUDA NVCC target flags: -gencode;arch=compute_20,code=sm_20;-gencode;arch=compute_20,code=sm_21;-gencode;arch=compute_30,code=sm_30;-gencode;arch=compute_35,code=sm_35;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_52,code=sm_52;-gencode;arch=compute_53,code=sm_53;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61
VTK_MAJOR_VERSION 8, rendering backend: OpenGL2
VTK found (include: C:/Program Files/PCL 1.8.1/3rdParty/VTK/include/vtk-8.0, lib: vtkChartsCore;vtkCommonColor;vtkCommonCore;vtksys;vtkCommonDataModel;vtkCommonMath;vtkCommonMisc;vtkCommonSystem;vtkCommonTransforms;vtkCommonExecutionModel;vtkFiltersGeneral;vtkCommonComputationalGeometry;vtkFiltersCore;vtkInfovisCore;vtkFiltersExtraction;vtkFiltersStatistics;vtkImagingFourier;vtkImagingCore;vtkalglib;vtkRenderingContext2D;vtkRenderingCore;vtkFiltersGeometry;vtkFiltersSources;vtkRenderingFreeType;vtkfreetype;vtkzlib;vtkDICOMParser;vtkDomainsChemistry;vtkIOLegacy;vtkIOCore;vtklz4;vtkIOXMLParser;vtkexpat;vtkDomainsChemistryOpenGL2;vtkRenderingOpenGL2;vtkglew;vtkFiltersAMR;vtkIOXML;vtkParallelCore;vtkFiltersFlowPaths;vtkFiltersGeneric;vtkFiltersHybrid;vtkImagingSources;vtkFiltersHyperTree;vtkFiltersImaging;vtkImagingGeneral;vtkFiltersModeling;vtkFiltersParallel;vtkFiltersParallelImaging;vtkFiltersPoints;vtkFiltersProgrammable;vtkFiltersSMP;vtkFiltersSelection;vtkFiltersTexture;vtkFiltersTopology;vtkFiltersVerdict;verdict;vtkGUISupportQt;vtkInteractionStyle;vtkGUISupportQtSQL;vtkIOSQL;vtksqlite;vtkGeovisCore;vtkIOImage;vtkmetaio;vtkjpeg;vtkpng;vtktiff;vtkInfovisLayout;vtkImagingHybrid;vtkInteractionWidgets;vtkImagingColor;vtkRenderingAnnotation;vtkRenderingVolume;vtkViewsCore;vtkproj4;vtkIOAMR;vtkhdf5_hl;vtkhdf5;vtkIOEnSight;vtkIOExodus;vtkexoIIc;vtkNetCDF;vtkIOExport;vtkRenderingGL2PSOpenGL2;vtkgl2ps;vtklibharu;vtkIOExportOpenGL2;vtkIOGeometry;vtkIOImport;vtkIOInfovis;vtklibxml2;vtkIOLSDyna;vtkIOMINC;vtkIOMovie;vtkoggtheora;vtkIONetCDF;vtknetcdfcpp;vtkIOPLY;vtkIOParallel;vtkjsoncpp;vtkIOParallelXML;vtkIOTecplotTable;vtkIOVideo;vtkImagingMath;vtkImagingMorphological;vtkImagingStatistics;vtkImagingStencil;vtkInteractionImage;vtkRenderingContextOpenGL2;vtkRenderingImage;vtkRenderingLOD;vtkRenderingLabel;vtkRenderingQt;vtkRenderingVolumeOpenGL2;vtkTestingGenericBridge;vtkTestingIOSQL;vtkTestingRendering;vtkViewsContext2D;vtkViewsInfovis;vtkViewsQt
Boost version: 1.64.0
Found the following Boost libraries:
  system
  filesystem
  thread
  date_time
  iostreams
  chrono
  atomic
  regex
DOXYGEN_FOUND 
HTML_HELP_COMPILER 
Found CPack generators: NSIS
The following subsystems will be built:
  common
  octree
  io
  kdtree
  search
  sample_consensus
  filters
  2d
  geometry
  features
  ml
  segmentation
  surface
  registration
  keypoints
  tracking
  recognition
  stereo
  tools
The following subsystems will not be built:
  visualization: Disabled manually.
  apps: Disabled: visualization missing.
  outofcore: Requires visualization.
  examples: Code examples are disabled by default.
  people: Requires visualization.
  simulation: Disabled: visualization missing.
  global_tests: No reason
Configuring done

OK.

compile

If everything goes well, then generate PCL.sln

Open PCL.sln in vs2015 and build with Release X64.

NOTICE: this build process will take about 30 minutes.

... waiting for about 30 minutes ...

========== 全部重新生成: 成功 132 个,失败 0 个,跳过 0 个 ==========

install pcl

by default install need administrator privilidges to write to C:/Program Files/PCL/.

choose INSTALL and generate , and PCL will be installed to C:/Program Files/PCL.

Example

pcl only

pcd_write.cpp

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>

int main (int argc, char** argv)
{
  pcl::PointCloud<pcl::PointXYZ> cloud;

  // Fill in the cloud data
  cloud.width    = 5;
  cloud.height   = 1;
  cloud.is_dense = false;
  cloud.points.resize (cloud.width * cloud.height);

  for (size_t i = 0; i < cloud.points.size (); ++i)
  {
    cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
    cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
    cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
  }

  pcl::io::savePCDFileASCII ("test_pcd.pcd", cloud);
  std::cerr << "Saved " << cloud.points.size () << " data points to test_pcd.pcd." << std::endl;

  for (size_t i = 0; i < cloud.points.size (); ++i)
    std::cerr << "    " << cloud.points[i].x << " " << cloud.points[i].y << " " << cloud.points[i].z << std::endl;

  return (0);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(pcd_write_test)

# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

#set(PCL_DIR "/path/to/PCLConfig.cmake")
#find_package(PCL 1.8.1 REQUIRED)
find_package(PCL 1.8.1 REQUIRED COMPONENTS common io)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

message ([main] "PCL_DIR = ${PCL_DIR}")
message ([main] "PCL_FOUND = ${PCL_FOUND}")
message ([main] "PCL_INCLUDE_DIRS = ${PCL_INCLUDE_DIRS}")
message ([main] "PCL_LIBRARIES = ${PCL_LIBRARIES}")
message ([main] "PCL_COMMON_LIBRARIES = ${PCL_COMMON_LIBRARIES}")
message ([main] "PCL_IO_LIBRARIES = ${PCL_IO_LIBRARIES}")

add_executable(pcd_write_test pcd_write.cpp)
target_link_libraries(pcd_write_test ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES})

run demo

compile to pcd_write_test.exe and run, errors may occur:

pcd_write_test.exe: error while loading shared libraries: ?: cannot open shared object file: No such file or directory

we need to find out which dlls are imported by exe.

wget http://www.dependencywalker.com/depends22_x64.zip

Tips: use dependency walker to check which dlls are imported, and then copy dll to exe folder.
copy pcl/bin/*.dll and vtk/bin/*.dll to exe folder.

run exe and output

$ ./pcd_write_test.exe   

Saved 5 data points to test_pcd.pcd.
    1.28125 577.094 197.938
    828.125 599.031 491.375
    358.688 917.438 842.563
    764.5 178.281 879.531
    727.531 525.844 311.281

cat pcd

$ cat test_pcd.pcd    

# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z
SIZE 4 4 4
TYPE F F F
COUNT 1 1 1
WIDTH 5
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 5
DATA ascii
1.28125 577.09375 197.9375
828.125 599.03125 491.375
358.6875 917.4375 842.5625
764.5 178.28125 879.53125
727.53125 525.84375 311.28125

create pcl project with vtk and qt support.

pcl+vtk+qt PointCloud Viewer (RECOMMEND)

This project use QVTKWidgetPlugin to display point cloud data.

  • mainwindow.h
  • mainwindow.cpp
  • mainwindow.ui (Contains QVTKWidgetPlugin)
  • main.cpp
  • CMakeLists.txt

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

// PCL
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>

typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

// Boost
#include <boost/math/special_functions/round.hpp>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButtonOK_clicked(); // method1
    void pushButtonCancel_clicked(); // method2

private:
    Ui::MainWindow *ui;

protected:
	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
	PointCloudT::Ptr cloud;

	void initCloud();
	void initialVtkWidget();

	unsigned int red;
	unsigned int green;
	unsigned int blue;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

// qt
#include <QMessageBox>
#include <QFileDialog>

// vtk
//#include <vtkAutoInit.h> 
//VTK_MODULE_INIT(vtkRenderingOpenGL2);
//VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkRenderWindow.h> // must include

// pcl 
//#include <pcl/io/ply_io.h>
//#include <pcl/io/pcd_io.h>
//#include <pcl/filters/filter.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
	this->setWindowTitle("PCL viewer");

   // init point cloud data
	initCloud();

	// set up the QVTK window
	initialVtkWidget();

	// connect
	connect(ui->pushButtonCancel, SIGNAL(clicked()), this, SLOT(pushButtonCancel_clicked()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initCloud()
{
	// Setup the cloud pointer
	cloud.reset(new PointCloudT);
	// The number of points in the cloud
	cloud->points.resize(200);

	// The default color
	red = 128;
	green = 128;
	blue = 128;

	// Fill the cloud with some points
	for (size_t i = 0; i < cloud->points.size(); ++i)
	{
		cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
		cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);

		cloud->points[i].r = red;
		cloud->points[i].g = green;
		cloud->points[i].b = blue;
	}
}

void MainWindow::initialVtkWidget()
{
	viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
	viewer->addPointCloud(cloud, "cloud");
	viewer->resetCamera();

	ui->qvtkWidget->SetRenderWindow(viewer->getRenderWindow());
	viewer->setupInteractor(ui->qvtkWidget->GetInteractor(), ui->qvtkWidget->GetRenderWindow());
	ui->qvtkWidget->update();
}

void MainWindow::on_pushButtonOK_clicked()
{
    QString text = ui->lineEditName->text();
    QMessageBox::information(this, "OK", text);
    ui->pushButtonOK->setText( tr("(OK. click me)") );
}

void MainWindow::pushButtonCancel_clicked()
{
    QString text = ui->lineEditName->text();
    QMessageBox::information(this, "Cancel", text);
    ui->pushButtonCancel->setText( tr("(Cancel.click me)") );
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>553</width>
    <height>372</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QLabel" name="labelName">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>20</y>
      <width>67</width>
      <height>17</height>
     </rect>
    </property>
    <property name="text">
     <string>Name</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonOK">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>250</y>
      <width>99</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>OK</string>
    </property>
    <property name="icon">
     <iconset resource="resource.qrc">
      <normaloff>:/prefix/images/kezunlin_logo.png</normaloff>:/prefix/images/kezunlin_logo.png</iconset>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEditName">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>20</y>
      <width>71</width>
      <height>27</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButtonCancel">
    <property name="geometry">
     <rect>
      <x>330</x>
      <y>250</y>
      <width>99</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>Cancel</string>
    </property>
   </widget>
   <widget class="QVTKWidget" name="qvtkWidget">
    <property name="geometry">
     <rect>
      <x>240</x>
      <y>10</y>
      <width>261</width>
      <height>201</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>553</width>
     <height>25</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuHello_MainWIndow">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="separator"/>
    <addaction name="actionOpen"/>
   </widget>
   <widget class="QMenu" name="menuHelp">
    <property name="title">
     <string>Help</string>
    </property>
    <addaction name="actionHelp"/>
   </widget>
   <addaction name="menuHello_MainWIndow"/>
   <addaction name="menuHelp"/>
  </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="actionFile">
   <property name="text">
    <string>Open</string>
   </property>
  </action>
  <action name="actionSave">
   <property name="text">
    <string>Save</string>
   </property>
  </action>
  <action name="actionExit">
   <property name="text">
    <string>Exit</string>
   </property>
  </action>
  <action name="actionHelp">
   <property name="text">
    <string>Help</string>
   </property>
  </action>
  <action name="actionOpen">
   <property name="text">
    <string>Open</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>QVTKWidget</class>
   <extends>QWidget</extends>
   <header>QVTKWidget.h</header>
  </customwidget>
 </customwidgets>
 <resources>
  <include location="resource.qrc"/>
 </resources>
 <connections/>
</ui>

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.8)

project(point_cloud_viewer)

# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

find_package(GFLAGS REQUIRED) # user-defined
find_package(GLOG REQUIRED) # user-defined
include_directories(${GFLAGS_INCLUDE_DIRS})
include_directories(${GLOG_INCLUDE_DIRS})

# add macro GLOG_NO_ABBREVIATED_SEVERITIES
add_definitions( -DGLOG_NO_ABBREVIATED_SEVERITIES ) 

#==============================================================
# PCL (include qt+vtk)
#==============================================================
# better not use ${PCL_LIBRARIES}, use what we need:
# e.g. ${PCL_COMMON_LIBRARIES} ${PCL_VISUALIZATION_LIBRARIES}

#find_package(PCL 1.8.1 REQUIRED)
find_package(PCL 1.8.1 REQUIRED COMPONENTS common io filters visualization)

include_directories(${PCL_INCLUDE_DIRS})
#link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

message ([main] "PCL_DIR = ${PCL_DIR}")
message ([main] "PCL_FOUND = ${PCL_FOUND}")
message ([main] "PCL_INCLUDE_DIRS = ${PCL_INCLUDE_DIRS}")
message ([main] "PCL_LIBRARY_DIRS = ${PCL_LIBRARY_DIRS}")
message ([main] "PCL_LIBRARIES = ${PCL_LIBRARIES}")
message ([main] "PCL_COMMON_LIBRARIES = ${PCL_COMMON_LIBRARIES}")
message ([main] "PCL_IO_LIBRARIES = ${PCL_IO_LIBRARIES}")
message ([main] "PCL_FILTERS_LIBRARIES = ${PCL_FILTERS_LIBRARIES}")
message ([main] "PCL_VISUALIZATION_LIBRARIES = ${PCL_VISUALIZATION_LIBRARIES}")

#==============================================================
# Boost
#==============================================================
set(Boost_USE_RELEASE_LIBS ON)
set(Boost_USE_MULTITHREAD ON)
find_package(Boost 1.5.8 REQUIRED COMPONENTS math_tr1)
#include_directories(${Boost_INCLUDE_DIRS})

#==============================================================
# cpp,ui,resource files
#==============================================================
# cpp files
#aux_source_directory(. SRC_LIST)
set(SRC_LIST 
	main.cpp
	mainwindow.h
	mainwindow.cpp
)

# ui files
qt5_wrap_ui(ui_FILES mainwindow.ui)

# resource files
qt5_add_resources(qrc_FILES resource.qrc)

message( [Main] ${SRC_LIST} ) # ./main.cpp./mainwindow.cpp
message( [Main] ${ui_FILES} ) # build/ui_mainwindow.h
message( [Main] ${qrc_FILES} )# build/qrc_resource.cpp

#==============================================================
# generate and link target
#==============================================================
add_executable(${PROJECT_NAME} ${SRC_LIST} ${ui_FILES} ${qrc_FILES})

# link qt libraries
qt5_use_modules(${PROJECT_NAME} Core Widgets OpenGL Xml Gui Sql)

# link vtk and pcl libraries
target_link_libraries(${PROJECT_NAME} 
	${GLOG_LIBRARIES}

	${PCL_COMMON_LIBRARIES}
	${PCL_IO_LIBRARIES}
	${PCL_FILTERS_LIBRARIES}
	${PCL_VISUALIZATION_LIBRARIES}

	#${PCL_LIBRARIES} # lead to error when build target (no error now)
)

# test_pcd
add_executable(test_pcd test_pcd.cpp)
target_link_libraries(test_pcd
	${PCL_COMMON_LIBRARIES}
	${PCL_IO_LIBRARIES}
	${PCL_FILTERS_LIBRARIES}
	${PCL_VISUALIZATION_LIBRARIES}
)

snapshot

./point_cloud_viewer.exe

Point Cloud Viewer for pcd file

errors

when run example, errors may occur: (which has been fixed by now.)
vtkOutputWindow error: no overide found for 'vtkPolyDataMapper'

Reference

History

  • 20180112: created.
  • 20180208: add DependencyWalker,add pcl pcd demo, pcl+vtk+qt demo.
  • 20180211: add vs2015 regedit for runasadmin.
  • 20180301: change VTK-8.0.1 to VTK-8.1.0.

Copyright

posted @ 2019-11-11 16:24  kezunlin  阅读(751)  评论(1编辑  收藏  举报