VTK+CTK+Qt5配置与测试

VTK、CTK的编译安装

下载

  1. VTK源码地址:Gitlab或者Github
  2. CTK源码地址:Github
  3. CMake, 3.12.3
  4. git
  5. Qt 5.11.2
  6. Windows: Visual Studio 2017, Mac: clang + make

    编译

    编译VTK

    $ mkdir Libs
    $ cd Libs
    $ git clone https://gitlab.kitware.com/vtk/vtk.git 
    #注:Windows下使用最新版本,Mac下使用v8.0.1,不然总出现OpenGL错误,未解决
    
    使用CMake-GUI配置,
  • Where is the source code选择Libs/VTK
  • Where to build the binaries选择Libs/VTK-build
  • CMAKE_INSTALL_PREFIX选择Libs/VTK-install
  • VTK_Group_QT勾选,
  • VTK_QT_VERSION选择5,
  • Qt5_DIR选择Qt5安装目录下的5.11.2/lib/cmake/Qt5目录,
  • Windows下可勾选Module_vtkGUISupportQtOpenGL,Mac下勾选运行时会报错,不知道为什么
  • VTK_DEBUG_LEAKS可勾选,程序运行完会提示是否有内存泄漏
  • 取消勾选BUILD_TEST

  • 多次configure没有错误及红色提示之后Generate
    编译工程, 安装

    编译CTK

    $ mkdir Libs
    $ cd Libs
    $ git clone https://github.com/commontk/CTK.git 
    

    使用CMake-GUI配置,

  • Where is the source code选择Libs/CTK
  • Where to build the binaries选择Libs/CTK-build
  • CMAKE_INSTALL_PREFIX选择Libs/CTK-install
  • CTK_QT_VERSION选择5,
  • Qt5_DIR选择Qt5安装目录下的5.11.2/lib/cmake/Qt5目录,
  • 勾选CMAKE_BUILD_QTDESIGNER_PLUGINS
  • 取消勾选BUILD_TEST
  • 多次configure没有错误及红色提示之后Generate
    编译工程, 安装,注意,CTK安装需要在CTK-build目录安装一次,然后在CTK-build/CTK-build目录再安装一次

测试

VTK测试

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(VTKTest)

set(CMAKE_CXX_STANDARD 14)

IF(APPLE)
    SET(VTK_DIR "/Users/fitsir/Libs/VTK-install/lib/cmake/vtk-8.0")
    SET(CTK_DIR "/Users/fitsir/Libs/CTK-install/lib/ctk-0.1/CMake")
ENDIF()

FIND_PACKAGE(VTK REQUIRED)
IF (VTK_FOUND)
    MESSAGE(STATUS "VTK found.")
    INCLUDE(${VTK_USE_FILE})
ENDIF()
FIND_PACKAGE(CTK REQUIRED)
IF(CTK_FOUND)
    MESSAGE(STATUS "CTK found.")
    INCLUDE(${CTK_USE_FILE})
ENDIF()
FIND_PACKAGE(Qt5 COMPONENTS Widgets OpenGL REQUIRED)
IF(Qt5_FOUND)
    MESSAGE(STATUS "Qt5 found.")
ENDIF()


add_executable(VTKTest main.cpp)
target_link_libraries(VTKTest
    ${VTK_LIBRARIES})

main.cpp

#include <iostream>
//VTK includes
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>

void vtk_without_qt();

int main() {
    std::cout << "Hello, World!" << std::endl;
    vtk_without_qt();
    return 0;
}

void vtk_without_qt(){
    auto cylinder = vtkSmartPointer<vtkCylinderSource>::New();
    cylinder->SetHeight(3.0);
    cylinder->SetRadius(1.0);
    cylinder->SetResolution(10);
    auto cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
    auto cylinderActor = vtkSmartPointer<vtkActor>::New();
    cylinderActor->SetMapper(cylinderMapper);
    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(cylinderActor);
    renderer->SetBackground(0.1, 0.2, 0.4);
    auto renWin = vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer(renderer);
    renWin->SetSize(300, 300);
    auto iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);
    auto style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    iren->SetInteractorStyle(style);

    iren->Initialize();
    iren->Start();
}

运行效果

vtk_without_qt.png

VTK+Qt5 测试

CMakeLists.txt

CMakeLists.txt只需添加Qt5相关的库

target_link_libraries(VTKTest
    ${VTK_LIBRARIES}
    ${Qt5Widgets_LIBRARIES}
    ${Qt5OpenGL_LIBRARIES})

main.cpp

#include <iostream>
//VTK includes
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>

#include <QVTKOpenGLWidget.h>
#include <QApplication>
#include <QVBoxLayout>
#include <QSurfaceFormat>
int vtk_with_qt(int argc, char* argv[]);

int main(int argc, char* argv[]) {
    std::cout << "Hello, World!" << std::endl;
    QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat());

    return vtk_with_qt(argc, argv);

}

int vtk_with_qt(int argc, char* argv[]){
    QApplication a(argc, argv);
    QWidget w;// = new QWidget(0);
    QVBoxLayout layout;// = new QVBoxLayout(0);
    QVTKOpenGLWidget v;// = new QVTKOpenGLWidget();
    v.setFixedSize(300, 300);
    layout.addWidget(&v);
    w.setLayout(&layout);

    auto cylinder = vtkSmartPointer<vtkCylinderSource>::New();
    cylinder->SetHeight(3.0);
    cylinder->SetRadius(1.0);
    cylinder->SetResolution(10);
    auto cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
    auto cylinderActor = vtkSmartPointer<vtkActor>::New();
    cylinderActor->SetMapper(cylinderMapper);
    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(cylinderActor);
    renderer->SetBackground(0.1, 0.2, 0.4);
    auto renWin = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
    renWin->AddRenderer(renderer);

    v.SetRenderWindow(renWin);
    w.show();

    return a.exec();
}

运行效果

vtk_with_qt.png

VTK+CTK+Qt5测试

CMakeList.txt

#Qt5 需要添加CTK依赖的一些库
FIND_PACKAGE(Qt5 COMPONENTS Widgets OpenGL Xml Network REQUIRED)
target_link_libraries(VTKTest
    ${VTK_LIBRARIES}
    ${Qt5Widgets_LIBRARIES}
    ${Qt5OpenGL_LIBRARIES}
    ${CTK_LIBRARIES})

main.cpp

#include <iostream>
//VTK includes
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>

#include <QVTKOpenGLWidget.h>
#include <QApplication>
#include <QVBoxLayout>
#include <QSurfaceFormat>
#include <ctkVTKRenderView.h>
#include <ctkSliderWidget.h>
int vtk_with_ctk_qt(int argc, char* argv[]);

int main(int argc, char* argv[]) {
    std::cout << "Hello, World!" << std::endl;
    QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat());

    return vtk_with_ctk_qt(argc, argv);

}


int vtk_with_ctk_qt(int argc, char* argv[]){
    QApplication a(argc, argv);
    QWidget w;// = new QWidget(0);
    QVBoxLayout layout;// = new QVBoxLayout(0);
    ctkVTKRenderView v;// = new QVTKOpenGLWidget();

    v.setFixedSize(300, 300);
    layout.addWidget(&v);
    ctkSliderWidget s;
    s.setRange(0, 100);
    layout.addWidget(&s);
    w.setLayout(&layout);

    auto cylinder = vtkSmartPointer<vtkCylinderSource>::New();
    cylinder->SetHeight(3.0);
    cylinder->SetRadius(1.0);
    cylinder->SetResolution(10);
    auto cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
    auto cylinderActor = vtkSmartPointer<vtkActor>::New();
    cylinderActor->SetMapper(cylinderMapper);
    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(cylinderActor);
    renderer->SetBackground(0.1, 0.2, 0.4);
    auto renWin = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
    renWin->AddRenderer(renderer);

    v.renderWindow()->AddRenderer(renderer);
    w.show();

    return a.exec();
}

运行效果

vtk_with_ctk_qt.png

源码

coding