第一个 VTK 示例

获取 VTKData(可选)

VTKData 包含了 VTK 示例程序用到的数据资源,我们可以在 VTK 官网 下载,例如与 VTK 安装 对应的 VTKData-9.1.0.tar.gz 资料包。

将其解压到 vtk 源代码中的 Examples 目录:

tar zxvf VTKData-9.1.0.tar.gz -C ~/workspace/Projects/VTK/vtk/Examples/

SimpleView 示例

SimpleView 示例代码位于 VTK 仓库的 Examples/GUI/Qt/ 目录中

cd Examples/GUI/Qt/SimpleView/

SimpleView 工程目录结构

├── CMakeLists.txt
├── Icons
│   ├── fileopen.png
│   ├── filesave.png
│   ├── help.png
│   ├── icons.qrc
│   └── print.png
├── main.cxx
├── SimpleView.cxx
├── SimpleView.h
└── SimpleView.ui

【CMakeLists.txt】

cmake_minimum_required(VERSION 3.8...3.12 FATAL_ERROR)
project(SimpleView)

find_package(VTK
  COMPONENTS
    CommonCore
    GUISupportQt
    InfovisCore
    RenderingFreeType
    ViewsQt)
if (NOT VTK_FOUND)
  message("Skipping example: {VTK_NOT_FOUND_MESSAGE}")
  return ()
endif ()

find_package(Qt5 COMPONENTS Widgets Gui)
if (NOT TARGET Qt5::Widgets OR NOT TARGET Qt5::Gui)
  message("Skipping example:{Qt5_NOT_FOUND_MESSAGE}")
  return ()
endif ()

# Set your files and resources here
set(Srcs
  SimpleView.cxx
  main.cxx)

set(Hdrs
  SimpleView.h)

set(UIs
  SimpleView.ui)

set(QRCs
  Icons/icons.qrc)

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

source_group("Resources" FILES
  {UIs}{QRCs})

add_executable(SimpleView MACOSX_BUNDLE
  {Srcs}{Hdrs} {UIs}{QRCs})
target_link_libraries(SimpleView
  PRIVATE
    ${VTK_LIBRARIES}
    Qt5::Gui
    Qt5::Widgets)

【main.cxx】

#include <QtGui/QSurfaceFormat>
#include <QtWidgets/QApplication>
#include "QVTKRenderWidget.h"
#include "SimpleView.h"

extern int qInitResources_icons();

int main(int argc, char** argv)
{
  // needed to ensure appropriate OpenGL context is created for VTK rendering.
  QSurfaceFormat::setDefaultFormat(QVTKRenderWidget::defaultFormat());

  // QT Stuff
  QApplication app(argc, argv);

  QApplication::setStyle("fusion");

  qInitResources_icons();

  SimpleView mySimpleView;
  mySimpleView.show();

  return app.exec();
}

【SimpleView.ui】

【SimpleView.h】

#ifndef SimpleView_H
#define SimpleView_H
#include "vtkSmartPointer.h" // Required for smart pointer internal ivars.
#include <QMainWindow>

// Forward Qt class declarations
class Ui_SimpleView;

// Forward VTK class declarations
class vtkQtTableView;

class SimpleView : public QMainWindow
{
  Q_OBJECT

public:
  // Constructor/Destructor
  SimpleView();
  ~SimpleView() override;

public Q_SLOTS:

  virtual void slotOpenFile();
  virtual void slotExit();

protected:
protected Q_SLOTS:

private:
  vtkSmartPointer<vtkQtTableView> TableView;

  // Designer form
  Ui_SimpleView* ui;
};

#endif // SimpleView_H

【SimpleView.cxx】

#include "SimpleView.h"
#include "ui_SimpleView.h"

#include "vtkGenericOpenGLRenderWindow.h"
#include "vtkSmartPointer.h"
#include <vtkDataObjectToTable.h>
#include <vtkElevationFilter.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkQtTableView.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkVectorText.h>

// Constructor
SimpleView::SimpleView()
{
  this->ui = new Ui_SimpleView;
  this->ui->setupUi(this);

  // Qt Table View
  this->TableView = vtkSmartPointer<vtkQtTableView>::New();

  // Place the table view in the designer form
  this->ui->tableFrame->layout()->addWidget(this->TableView->GetWidget());

  // Geometry
  vtkNew<vtkVectorText> text;
  text->SetText("VTK and Qt!");
  vtkNew<vtkElevationFilter> elevation;
  elevation->SetInputConnection(text->GetOutputPort());
  elevation->SetLowPoint(0, 0, 0);
  elevation->SetHighPoint(10, 0, 0);

  // Mapper
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(elevation->GetOutputPort());

  // Actor in scene
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  // VTK Renderer
  vtkNew<vtkRenderer> ren;

  // Add Actor to renderer
  ren->AddActor(actor);

  // VTK/Qt wedded
  vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
  this->ui->qvtkWidget->setRenderWindow(renderWindow);
  this->ui->qvtkWidget->renderWindow()->AddRenderer(ren);

  // Just a bit of Qt interest: Culling off the
  // point data and handing it to a vtkQtTableView
  vtkNew<vtkDataObjectToTable> toTable;
  toTable->SetInputConnection(elevation->GetOutputPort());
  toTable->SetFieldType(vtkDataObjectToTable::POINT_DATA);

  // Here we take the end of the VTK pipeline and give it to a Qt View
  this->TableView->SetRepresentationFromInputConnection(toTable->GetOutputPort());

  // Set up action signals and slots
  connect(this->ui->actionOpenFile, SIGNAL(triggered()), this, SLOT(slotOpenFile()));
  connect(this->ui->actionExit, SIGNAL(triggered()), this, SLOT(slotExit()));
};

SimpleView::~SimpleView()
{
  // The smart pointers should clean up for up
}

// Action to be taken upon file open
void SimpleView::slotOpenFile() {}

void SimpleView::slotExit()
{
  qApp->exit();
}

编译运行

在 SimpleView 目录中创建一个 build 目录

mkdir build
cd build

生成 Makefile

cmake ..

如果 cmake 执行过程中遇到如下错误,则需要修改 VTK 工程的 CMake 配置,将 VTK_MODULE_ENABLE_VTK_GuiSupportQtVTK_MODULE_ENABLE_VTK_ViewsQt 配置项设置为 YES(在 cmake-gui 界面勾选 Advanced 框才会显示)。具体可参考 VTK 编译安装

CMake Warning at CMakeLists.txt:4 (find_package):
  Found package configuration file:

    /usr/local/lib/cmake/vtk-9.1/vtk-config.cmake

  but it set VTK_FOUND to FALSE so package "VTK" is considered to be NOT
  FOUND.  Reason given by package:

  Could not find the VTK package with the following required components:
  GUISupportQt.

一切顺利,现在可以编译了

make
./SimpleView

运行 SimpleView 可执行文件,会出现如下窗口,鼠标拖动(可配合 Ctrl、Shift 和 Alt 键)可以看到三维效果哦!

GitHub 仓库:getiot/vtk-courses