本文讲解使用的环境是vs2019+pcl1.12.0+vtk9.1+qt6.0,最后再展示一个示例程序
1 编译VTK
vtk下载地址如下:
https://vtk.org/download/
然后用cmake构建,修改一下几个地方
然后打开生成的vs项目,生成ALL_BUILD,
最后再点击INSTALL生成
最后生成如下库和文件
2 编译PCL
先下载安装releases版本,把PCL其他的依赖库安装一下,免得自己去编译,下载地址如下
https://github.com/PointCloudLibrary/pcl/releases
下载后,然后安装,安装完成后就会生成下面的依赖库,然后将里面的vtk全部替换成自己刚才编译的vtk文件
然后下载PCL源码,地址如下:
https://github.com/PointCloudLibrary/pcl
下载完后,打开下面这个文件
打开文件后根据自己的路径修改下面的代码
- # Find and set Boost flags
-
- # If we would like to compile against a dynamically linked Boost
- if(PCL_BUILD_WITH_BOOST_DYNAMIC_LINKING_WIN32 AND WIN32)
- set(Boost_USE_STATIC_LIBS OFF)
- set(Boost_USE_STATIC OFF)
- set(Boost_USE_MULTITHREAD ON)
- string(APPEND CMAKE_CXX_FLAGS " -DBOOST_ALL_DYN_LINK -DBOOST_ALL_NO_LIB")
- else()
- if(NOT PCL_SHARED_LIBS OR WIN32)
- set(Boost_USE_STATIC_LIBS ON)
- set(Boost_USE_STATIC ON)
- endif()
- endif()
-
- #增加下面两行代码
- set(Boost_LIBRARY_DIR D:/2021_software/PCL/install/PCL1.12.0/3rdParty/Boost/lib)
- set(Boost_INCLUDE_DIR D:/2021_software/PCL/install/PCL1.12.0/3rdParty/Boost/include/boost-1_76)
-
- set(Boost_ADDITIONAL_VERSIONS
- "1.79.0" "1.79" "1.78.0" "1.78" "1.77.0" "1.77" "1.76.0" "1.76" "1.75.0" "1.75"
- "1.74.0" "1.74" "1.73.0" "1.73" "1.72.0" "1.72" "1.71.0" "1.71" "1.70.0" "1.70"
- "1.69.0" "1.69" "1.68.0" "1.68" "1.67.0" "1.67" "1.66.0" "1.66" "1.65.1" "1.65.0" "1.65")
-
- # Optional boost modules
- find_package(Boost 1.65.0 QUIET COMPONENTS serialization mpi)
- if(Boost_SERIALIZATION_FOUND)
- set(BOOST_SERIALIZATION_FOUND TRUE)
- endif()
-
- # Required boost modules65
- set(BOOST_REQUIRED_MODULES filesystem date_time iostreams system)
- find_package(Boost 1.65.0 REQUIRED COMPONENTS ${BOOST_REQUIRED_MODULES})
-
- if(Boost_FOUND)
- set(BOOST_FOUND TRUE)
- endif()
添加FLANN和OPENNI2目录
修改vtk目录和qt版本
这个就可以生成vs项目了,生成项目后编制再安装就可以得到头文件和库文件
3 编译示例
新建一个QVTKWindow.h文件
- #ifndef QVTKWINDOW_H
- #define QVTKWINDOW_H
-
- // Point Cloud Library
- #include <pcl/point_cloud.h>
- #include <pcl/point_types.h>
- #include <pcl/visualization/pcl_visualizer.h>
-
- // Visualization Toolkit (VTK)
- #include <vtkRenderWindow.h>
- #include <QVTKRenderWidget.h>
- #include <QVTKOpenGLNativeWidget.h>
-
- typedef pcl::PointXYZRGBA PointT;
- typedef pcl::PointCloud<PointT> PointCloudT;
-
- //点云数据
- typedef struct vtkpointcloud{
- float x;
- float y;
- float z;
- unsigned int red;
- unsigned int green;
- unsigned int blue;
- }VTK_POINT_CLOUD_S;
-
- class QVTKWindow : public QVTKOpenGLNativeWidget
- {
- Q_OBJECT
- public:
- explicit QVTKWindow(int win_size,QWidget *parent = nullptr);
- ~QVTKWindow();
- private:
- pcl::visualization::PCLVisualizer::Ptr viewer;
- PointCloudT::Ptr cloud;
- };
- #endif // QVTKWINDOW_H
新建一个QVTKWindow.cpp文件
- #include "QVTKWindow.h"
- #include <qpainter.h>
- #include <qdebug.h>
- #include <pcl/common/io.h>
- #include <pcl/io/io.h>
- #include <pcl/io/pcd_io.h>
- #include <pcl/io/obj_io.h>
- #include <pcl/PolygonMesh.h>
- #include <pcl/point_cloud.h>
- #include <pcl/io/vtk_lib_io.h>//loadPolygonFileOBJ所属头文件;
- #include <pcl/visualization/pcl_visualizer.h>
- #include <pcl/common/common_headers.h>
- #include <pcl/features/normal_3d.h>
- #include <pcl/io/pcd_io.h>
- #include <pcl/visualization/pcl_visualizer.h>
- #include <pcl/console/parse.h>
- #include <pcl/io/ply_io.h>
- #include "vtkGenericOpenGLRenderWindow.h"
- QVTKWindow::QVTKWindow(QWidget *parent)
- : QVTKOpenGLNativeWidget(parent)
- {
- #if VTK_MAJOR_VERSION > 8
- auto renderer2 = vtkSmartPointer<vtkRenderer>::New();
- auto renderWindow2 = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
- renderWindow2->AddRenderer(renderer2);
- viewer.reset(new pcl::visualization::PCLVisualizer(renderer2, renderWindow2, "viewer", false));
- this->setRenderWindow(viewer->getRenderWindow());
- viewer->setupInteractor(this->interactor(), this->renderWindow());
- #else
- viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
- this->SetRenderWindow(viewer->getRenderWindow());
- viewer->setupInteractor(this->GetInteractor(), this->GetRenderWindow());
- #endif
- std::string fileName = "3.ply";
- pcl::PolygonMesh meshData;//读取原始数据
- pcl::io::loadPolygonFile(fileName,meshData);
-
- pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
- cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
- pcl::fromPCLPointCloud2(meshData.cloud, *cloud);//将obj数据转换为点云数据
-
- // 显示结果图
- viewer->setBackgroundColor (0, 0, 0); //设置背景
- pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud, "z");
- viewer->addPointCloud<pcl::PointXYZ>(cloud, fildColor, "sample cloud");
- viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
- viewer->resetCamera ();
- update ();
- }
-
- QVTKWindow::~QVTKWindow()
- {
- }
然后将上面的类添加到主界面的布局中
- QVTKWindow *win = new QVTKWindow(100,this);
- ui->verticalLayout->addWidget(win);
生成结果如下
测试代码和点云文件,我放到CSDN上面了,有需要的同学可以下载
https://download.csdn.net/download/qq_40732350/85602521