VTK9.1.0在Windows10+VS2019+Qt 5.15.2环境下编译安装以及VTK应用于QT

发布时间 2023-07-21 16:22:03作者: 一杯清酒邀明月

下载VTK安装包
在VTK官网 Download | VTK 中下载VTK9.1.0待编译源码,解压后在路径Documentation/dev/bulid.md中可以看到官方提供的Prerequisites以及简易教程

编译环境安装
按照官方提供的Prerequisites,安装以下环境:

CMake
Version 3.12 or newer, however, the latest version is always recommended
Qt 5.15.2
Visual Studio Community Edition
Visual Studio 2019
Ninja
Unzip the binary and put it in PATH. Note that newerVisual Studio releases come with a version of ninja already and shouldalready exist in PATH within the command prompt.
编译目录规划
按照官方推荐方式,设置四个文件夹:

VTK-9.1.0.rc2-src
存放VTK官方下载的待编译源码

VTK-9.1.0.rc2-build
存放使用Cmake编译VTK时生成的二进制文件

VTK-9.1.0.rc2-install-debug
存放使用Visual Studio生成的lib文件的Debug版本

VTK-9.1.0.rc2-install-release
存放使用Visual Studio生成的lib文件的Release版本

 

使用Cmake编译VTK库
打开安装的cmake-gui,选择 VTK 源码路径VTK-9.1.0.rc2-src,以及二进制存放路径VTK-9.1.0.rc2-build,点击Configure开始,在弹出的窗口中选择 Visual Studio 16 2019编译,选择后点击 Finish

 

  • 完成Configure后会出现一些红色选项,下面需要解决红色选项

 

其中CMAKE_INSTALL_PREFIX是VTK 库要安装的位置,VTK 编译后生成的库文件所在位置,将位置修改为VTK-9.1.0.rc2-install-debug, 在后续的生成中,先生成Release版本的VTK库,生成后将生成的文件移动到VTK-9.1.0.rc2-install-release,然后生成Debug版本的VTK库,此时生成的文件就存放于VTK-9.1.0.rc2-install-debug文件夹中

 

  • 在搜索窗口中搜索QT,将所有的选项设置为WANT,然后点击Configure

 

 

Configure后会出现新的红色区域,这里是让我们选择 Qt5 的位置,基本是Qt_DIR/5.15.2/msvc2019_64/lib/cmake/Qt*,按左侧的名字勾选,VTK_QT_VERSION为5

 

Configuring done 后,如果只有白色界面,代表没有错误。如果你的界面依旧有红色,返回检查以上哪步没有勾选,一直Configure到只有白色界面,然后点击 Generate
Generate done 后,如果只有白色界面,代表没有错误,然后点击Open Project便会打开Visual Studio 2019

 

Visual Studio 2019 编译安装 VTK

  • Cmake完成以后,点击Open Project便会打开Visual Studio 2019,选择 生成->批生成,在出现的界面中选择ALL_BUILD的Release版本,点击生成后等待生成完成,需要40分钟左右

 

 

  • 把ALL_BUILD Release后面的√去掉,在INSTALL Release后面勾选,点击生成

 

生成完成后即安装VTK的Release版本,此时会安装在前边设置的CMAKE_INSTALL_PREFIX中,即VTK-9.1.0.rc2-install-debug,然后需要把文件剪切到VTK-9.1.0.rc2-install-release文件夹里,此时VTK Release版本生成完成,生成的库文件如下:

 

在生成->批生成里,把刚刚勾选的INSTALL Release后面的√取消。 然后可以直接一起勾选ALL_BUILD DEBUG x64 与INSTALL Debug,生成结束以后,会在VTK-9.1.0.rc2-install-debug文件夹里生成库文件。此时编译阶段完成。生成的 Debug 版本的库后面带 d,与Release版本相区别。
在QT中使用VTK
我写了一个在QT中使用VTK的demo项目,链接为GitHub - isongxw/Qt-VTK-Demo,可直接下载查看,下面是具体步骤

将生成的VTK-9.1.0.rc2-install-debug\bin 与 VTK-9.1.0.rc2-install-release\bin添加到环境变量并重启
在Qt Creator创建QT项目,在*pro文件中添加INCLUDEPATH += "D:\VTK\VTK-9.1.0.rc2-install-debug\include\vtk-9.1"与LIBS,其中LIBS需要添加的lib文件较多,使用以下代码来生成LIBS指令

 1 import os
 2  
 3 def main():
 4     BASEDIR = "D:\VTK\VTK-9.1.0.rc2-install-release\lib"
 5  
 6     fn = os.listdir(BASEDIR)
 7     with open("lib_r.txt", 'w') as fp:
 8         for i in fn:
 9             fp.write("LIBS += {}\{}\n".format(BASEDIR, i))
10     print("hello")
11  
12  
13 if __name__ == "__main__":
14     main()

 

在界面中添加OpenGL Widget,右键点击提升为,提升为QVTKOpenGLNativeWidget,界面如下

  • 添加示例代码使用VTK绘制一个球形,mainwindows.cpp代码如下:
 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 #include <QSurfaceFormat>
 4 #include <QVTKOpenGLNativeWidget.h>
 5 #include <vtkSphereSource.h>
 6 #include <vtkPolyDataMapper.h>
 7 #include <vtkActor.h>
 8 #include <vtkRenderer.h>
 9 #include <vtkRenderWindow.h>
10 #include <vtkGenericOpenGLRenderWindow.h>
11 #include <vtkNamedColors.h>
12 #include <vtkProperty.h>
13 #include <vtkSmartPointer.h>
14  
15 #include "vtkAutoInit.h"
16 VTK_MODULE_INIT(vtkRenderingOpenGL2);
17 VTK_MODULE_INIT(vtkInteractionStyle);
18 VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
19 VTK_MODULE_INIT(vtkRenderingFreeType);
20  
21  
22 MainWindow::MainWindow(QWidget *parent)
23     : QMainWindow(parent)
24     , ui(new Ui::MainWindow)
25 {
26     ui->setupUi(this);
27     QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
28     vtkNew<vtkNamedColors> colors;
29     vtkNew<vtkSphereSource> sphereSource;
30     vtkNew<vtkPolyDataMapper> sphereMapper;
31     sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
32     vtkNew<vtkActor> sphereActor;
33     sphereActor->SetMapper(sphereMapper);
34     sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
35     vtkNew<vtkRenderer> renderer;
36     renderer->AddActor(sphereActor);
37     renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
38     vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
39     renderWindow->AddRenderer(renderer);
40     renderWindow->SetWindowName("RenderWindowNoUIFile");
41     setCentralWidget(ui->openGLWidget);
42     ui->openGLWidget->setRenderWindow(renderWindow);
43 }
44  
45 MainWindow::~MainWindow()
46 {
47     delete ui;
48 }

点击运行后即可出现Qt窗口中的VTK界面