Ubuntu18.04 + Qt5.10.1 + VTK9.0环境配置+测试

版权声明:博主原创文章,可以转载,但要注明出处。维护版权,人人有责。 https://blog.csdn.net/dongdong_csdn/article/details/80877121

Ubuntu18.04 + Qt5.10.1 + VTK9.0环境配置+测试

浪费了一整天的时间,尝试使用 Visual Stdio 编译VTK9.0,然后结合MFC开发。官方的文档提供的方法,换了几个版本的Cmake,照着网上各路大神的方法各种改,然而就是出错。应该是我电脑里的VS与VTK八字不合吧。于是换了Ubuntu18.04,同样是官方的方法,一次成功。。。。。论安装软件的坑,是永远也论不完的,所以就不要论了
以下是配置时的几点需要注意的,要以官方的方法为准!!!我之前在Ubuntu中折腾过几次,所以有些依赖的库可能已经有了,缺少库就直接安装就好,如果出错有大牛指点最好,没有就只能上网搜了。god bless you!!!
官方方法的连接:
https://www.vtk.org/Wiki/VTK/Configure_and_Build

VTK

最好用git clone的方式,不需要科学上网也是可以的。指令见官方链接!
VTK是基于opengl开发的(书上是这么写的),所以要装opengl!!!

sudo apt-get install libgl1-mesa-dev
sudo apt-get install libglu1-mesa-dev
sudo apt-get install freeglut3-dev

我安装的时候,出现了文件大小不符,无法下载的情况,解决方法就是换,换成主服务器的就好了,虽然速度慢了,装完可以再换回去。

安装Qt

下载相应的版本http://download.qt.io/archive/qt/
linux上有64位的,我就直接用了,昨天折腾一天,现在有现成的就直接用吧。图形安装,很友好。
装完之后需要在“工具”->“选项”->“构建和运行”进行设置,要指定GCC与G++的路径。(安装之前最好都把GCC装上)

cmake

官方的ccmake用起来不是很友好,也没必要花时间去搞,建议使用cmake-gui,图形的,和windows一样,使用如下命令安装
sudo apt install cmake-qt-gui
跳转到build的目录下,启动cmake-gui
sudo cmake-gui <VTK源码的绝对路径>
第一次configure之后需要(cmake上有个advance要打勾!)
将VTK_Group_Qt打钩
还有官方的方法里提到的几个选项也要注意
第二次configure之后
需要制定Qt相应模块的目录
点击configure发现没有红色的选项了
然后就可以点general
之后就是编译
sudo make
之后就是漫长的等待。。。。。。
结束后
sudo make install
至此VTK编译结束

测试例程

例程是官方的例子,直接copy。

1、新建Qt工程

目录结构如下
这里写图片描述

2、在pro文件中添加头文件和库路径

完整的pro如下,注意路径要改成自己的!!我这里的头文件放在/home/ieeeee/VTKInstall/vtk-9.0
动态库放在/home/ieeeee/VTKInstall/VTKso/
这里要强调添加动态库的时候的格式,比如添加libvtkChartsCore-9.0.so要写成-lvtkChartsCore-9.0,否则会报错collect2: error: ld returned 1 exit status

#-------------------------------------------------
#
# Project created by QtCreator 2018-07-01T19:32:43
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = hello
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

INCLUDEPATH += /home/ieeeee/VTKInstall/vtk-9.0


#LIBS += -L/home/ieeeee/VTKInstall/VTKa/lib*
LIBS += -L/home/ieeeee/VTKInstall/VTKso/ \
-lvtkChartsCore-9.0 \
-lvtkCommonColor-9.0 \
-lvtkCommonComputationalGeometry-9.0 \
-lvtkChartsCore-9.0 \
-lvtkCommonColor-9.0 \
-lvtkCommonComputationalGeometry-9.0 \
-lvtkCommonCore-9.0 \
-lvtkCommonDataModel-9.0 \
-lvtkCommonExecutionModel-9.0 \
-lvtkCommonMath-9.0 \
-lvtkCommonMisc-9.0 \
-lvtkCommonSystem-9.0 \
-lvtkCommonTransforms-9.0 \
-lvtkDICOMParser-9.0 \
-lvtkDomainsChemistry-9.0 \
-lvtkDomainsChemistryOpenGL2-9.0 \
-lvtkdoubleconversion-9.0 \
-lvtkexodusII-9.0 \
-lvtkexpat-9.0 \
-lvtkFiltersAMR-9.0 \
-lvtkFiltersCore-9.0 \
-lvtkFiltersExtraction-9.0 \
-lvtkFiltersFlowPaths-9.0 \
-lvtkFiltersGeneral-9.0 \
-lvtkFiltersGeneric-9.0 \
-lvtkFiltersGeometry-9.0 \
-lvtkFiltersHybrid-9.0 \
-lvtkFiltersHyperTree-9.0 \
-lvtkFiltersImaging-9.0 \
-lvtkFiltersModeling-9.0 \
-lvtkFiltersParallel-9.0 \
-lvtkFiltersParallelImaging-9.0 \
-lvtkFiltersPoints-9.0 \
-lvtkFiltersProgrammable-9.0 \
-lvtkFiltersSelection-9.0 \
-lvtkFiltersSMP-9.0 \
-lvtkFiltersSources-9.0 \
-lvtkFiltersStatistics-9.0 \
-lvtkFiltersTexture-9.0 \
-lvtkFiltersTopology-9.0 \
-lvtkFiltersVerdict-9.0 \
-lvtkfreetype-9.0 \
-lvtkGeovisCore-9.0 \
-lvtkgl2ps-9.0 \
-lvtkglew-9.0 \
-lvtkGUISupportQt-9.0 \
-lvtkGUISupportQtOpenGL-9.0 \
-lvtkGUISupportQtSQL-9.0 \
-lvtkhdf5-9.0 \
-lvtkhdf5_hl-9.0 \
-lvtkImagingColor-9.0 \
-lvtkImagingCore-9.0 \
-lvtkImagingFourier-9.0 \
-lvtkImagingGeneral-9.0 \
-lvtkImagingHybrid-9.0 \
-lvtkImagingMath-9.0 \
-lvtkImagingMorphological-9.0 \
-lvtkImagingSources-9.0 \
-lvtkImagingStatistics-9.0 \
-lvtkImagingStencil-9.0 \
-lvtkInfovisCore-9.0 \
-lvtkInfovisLayout-9.0 \
-lvtkInteractionImage-9.0 \
-lvtkInteractionStyle-9.0 \
-lvtkInteractionWidgets-9.0 \
-lvtkIOAMR-9.0 \
-lvtkIOAsynchronous-9.0 \
-lvtkIOCore-9.0 \
-lvtkIOEnSight-9.0 \
-lvtkIOExodus-9.0 \
-lvtkIOExport-9.0 \
-lvtkIOExportOpenGL2-9.0 \
-lvtkIOExportPDF-9.0 \
-lvtkIOGeometry-9.0 \
-lvtkIOImage-9.0 \
-lvtkIOImport-9.0 \
-lvtkIOInfovis-9.0 \
-lvtkIOLegacy-9.0 \
-lvtkIOLSDyna-9.0 \
-lvtkIOMINC-9.0 \
-lvtkIOMovie-9.0 \
-lvtkIONetCDF-9.0 \
-lvtkIOParallel-9.0 \
-lvtkIOParallelXML-9.0 \
-lvtkIOPLY-9.0 \
-lvtkIOSegY-9.0 \
-lvtkIOSQL-9.0 \
-lvtkIOTecplotTable-9.0 \
-lvtkIOVideo-9.0 \
-lvtkIOXML-9.0 \
-lvtkIOXMLParser-9.0 \
-lvtkjpeg-9.0 \
-lvtkjsoncpp-9.0 \
-lvtklibharu-9.0 \
-lvtklibxml2-9.0 \
-lvtkLocalExample-9.0 \
-lvtklz4-9.0 \
-lvtklzma-9.0 \
-lvtkmetaio-9.0 \
-lvtkNetCDF-9.0 \
-lvtknetcdfcpp-9.0 \
-lvtkogg-9.0 \
-lvtkParallelCore-9.0 \
-lvtkpng-9.0 \
-lvtkproj-9.0 \
-lvtkRenderingAnnotation-9.0 \
-lvtkRenderingContext2D-9.0 \
-lvtkRenderingContextOpenGL2-9.0 \
-lvtkRenderingCore-9.0 \
-lvtkRenderingFreeType-9.0 \
-lvtkRenderingGL2PSOpenGL2-9.0 \
-lvtkRenderingImage-9.0 \
-lvtkRenderingLabel-9.0 \
-lvtkRenderingLOD-9.0 \
-lvtkRenderingOpenGL2-9.0 \
-lvtkRenderingQt-9.0 \
-lvtkRenderingVolume-9.0 \
-lvtkRenderingVolumeOpenGL2-9.0 \
-lvtksqlite-9.0 \
-lvtksys-9.0 \
-lvtktheora-9.0 \
-lvtktiff-9.0 \
-lvtkverdict-9.0 \
-lvtkViewsContext2D-9.0 \
-lvtkViewsCore-9.0 \
-lvtkViewsInfovis-9.0 \
-lvtkViewsQt-9.0 \
-lvtkzlib-9.0

3、main.cpp中添加代码

只为测试,直接都放main里了。。。
注意:以下两行必须添加!

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);

否则会在运行时一闪而过,并且报错Error: no override found for 'vtkPolyDataMapper'
完整文件如下:

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

#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkCamera.h>
#include <vtkSmartPointer.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
//VTK_MODULE_INIT(vtkInteractionStyle);


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    vtkCylinderSource *cylinder = vtkCylinderSource::New();
    cylinder->SetResolution(8);

    // The mapper is responsible for pushing the geometry into the graphics
    // library. It may also do color mapping, if scalars or other attributes
    // are defined.
    //
    vtkPolyDataMapper *cylinderMapper = vtkPolyDataMapper::New();
    cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

    // The actor is a grouping mechanism: besides the geometry (mapper), it
    // also has a property, transformation matrix, and/or texture map.
    // Here we set its color and rotate it -22.5 degrees.
    vtkActor *cylinderActor = vtkActor::New();
    cylinderActor->SetMapper(cylinderMapper);
    cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
    cylinderActor->RotateX(30.0);
    cylinderActor->RotateY(-45.0);

    // Create the graphics structure. The renderer renders into the
    // render window. The render window interactor captures mouse events
    // and will perform appropriate camera or actor manipulation
    // depending on the nature of the events.
    //
    vtkRenderer *ren1 = vtkRenderer::New();
    vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren1);
    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);

    // Add the actors to the renderer, set the background and size
    //
    ren1->AddActor(cylinderActor);
    ren1->SetBackground(0.1, 0.2, 0.4);
    renWin->SetSize(200, 200);

    // We'll zoom in a little by accessing the camera and invoking a "Zoom"
    // method on it.
    ren1->ResetCamera();
    ren1->GetActiveCamera()->Zoom(1.5);
    renWin->Render();

    // This starts the event loop and as a side effect causes an initial render.
    iren->Start();

    // Exiting from here, we have to delete all the instances that
    // have been created.
    cylinder->Delete();
    cylinderMapper->Delete();
    cylinderActor->Delete();
    ren1->Delete();
    renWin->Delete();
    iren->Delete();


    return a.exec();
}

4、运行结果

这里写图片描述
看到结果的一刹那,眼泪都快掉下来了。。。。。

展开阅读全文

没有更多推荐了,返回首页