Windows Qt C++ VTK 借助msys环境搭建

发布时间:2024年01月16日

本示例仅仅是搭建环境,后续使用还得大佬指导。

Qt? 6.6.0 MinGW 64bit?

借助msys2 来安装VTK 包,把*.dll 链接进来,就可以用了。

先安装VTK 包。

Package: mingw-w64-x86_64-vtk - MSYS2 Packages

执行 pacman 命令:pacman -S mingw-w64-x86_64-vtk

鉴于csdn下载要C币,把demo例子全贴出来。

*.pro文件:

QT       += core gui
 QT += openglwidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#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

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

INCLUDEPATH += C:/msys64/mingw64/include/vtk
DEPENDPATH  += C:/msys64/mingw64/include/vtk

LIBS += -LC:/msys64/mingw64/bin/ -llibvtkTestingIOSQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkTestingRendering
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkUtilitiesBenchmarks
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkverdict
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkViewsContext2D
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkViewsCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkViewsInfovis
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkViewsQt
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkvpic
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkWrappingPythonCore3.11
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkWrappingTools
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkxdmf2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkxdmf3
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkxdmfcore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkzfp
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkAcceleratorsVTKmCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkAcceleratorsVTKmDataModel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkAcceleratorsVTKmFilters
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkChartsCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonArchive
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonColor
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonComputationalGeometry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonDataModel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonExecutionModel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonMath
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonMisc
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonPython
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonSystem
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkCommonTransforms
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkDICOMParser
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkDomainsChemistry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkDomainsChemistryOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkDomainsMicroscopy
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkexodusII
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkfides
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersAMR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersCellGrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersExtraction
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersFlowPaths
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersGeneral
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersGeneric
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersGeometry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersGeometryPreview
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersHybrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersHyperTree
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersImaging
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersModeling
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersOpenTURNS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersParallel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersParallelDIY2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersParallelImaging
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersParallelStatistics
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersPoints
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersProgrammable
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersPython
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersReduction
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersReebGraph
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersSelection
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersSMP
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersSources
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersStatistics
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersTensor
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersTexture
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersTopology
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkFiltersVerdict
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGeovisCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGeovisGDAL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGUISupportQt
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGUISupportQtQuick
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkGUISupportQtSQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkh5part
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingColor
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingFourier
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingGeneral
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingHybrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingMath
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingMorphological
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingSources
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingStatistics
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkImagingStencil
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInfovisBoostGraphAlgorithms
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInfovisCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInfovisLayout
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInteractionImage
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInteractionStyle
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkInteractionWidgets
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOADIOS2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOAMR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOAsynchronous
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCellGrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCesium3DTiles
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCGNSReader
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOChemistry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCityGML
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCONVERGECFD
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOEnSight
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOExodus
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOExport
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOExportGL2PS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOExportPDF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOFFMPEG
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOFides
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOFLUENTCFF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOGDAL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOGeoJSON
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOGeometry
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOH5part
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOH5Rage
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOHDF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOImage
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOImport
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOInfovis
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOIOSS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOLAS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOLegacy
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOLSDyna
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOMINC
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOMotionFX
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOMovie
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOMySQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIONetCDF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOOCCT
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOODBC
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOOggTheora
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOOMF
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOOpenVDB
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOParallel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOParallelExodus
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOParallelLSDyna
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOParallelXML
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOPDAL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOPIO
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOPLY
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOPostgreSQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOSegY
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOSQL
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkioss
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOTecplotTable
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOTRUCHAS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOVeraOut
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOVideo
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOVPIC
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOXdmf2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOXdmf3
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOXML
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkIOXMLParser
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkkissfft
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkloguru
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_cont_testing-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_cont-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_clean_grid-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_connected_components-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_contour-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_core-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_density_estimate-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_entity_extraction-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_field_conversion-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_field_transform-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_flow-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_geometry_refinement-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_image_processing-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_mesh_info-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_multi_block-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_resampling-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_scalar_topology-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_vector_analysis-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_filter_zfp-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_io-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_source-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkm_worklet-2.0
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkmdiympi_nompi
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkmetaio
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkParallelCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkParallelDIY
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkPythonContext2D
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkPythonInterpreter
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingAnnotation
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingCellGrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingContext2D
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingContextOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingCore
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingExternal
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingFFMPEGOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingFreeType
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingFreeTypeFontConfig
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingGL2PSOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingHyperTreeGrid
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingImage
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingLabel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingLICOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingLOD
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingMatplotlib
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingOpenVR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingOpenXR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingParallel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingQt
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingRayTracing
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingSceneGraph
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingUI
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVolume
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVolumeAMR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVolumeOpenGL2
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVR
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkRenderingVtkJS
LIBS += -LC:/msys64/mingw64/bin/ -llibvtksys
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkTestingDataModel
LIBS += -LC:/msys64/mingw64/bin/ -llibvtkTestingGenericBridge


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>


QT_BEGIN_NAMESPACE
namespace Ui
{
    class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();

    void test0();
    void test1();

    void test2();

    void test3();

    void test4();
    
    void create();



private:
    Ui::MainWindow* ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "QVTKOpenGLNativeWidget.h"

#include <vtkSmartPointer.h>
#include <vtkGenericOpenGLRenderWindow.h>

#include <vtkNamedColors.h>

#include <vtkSphereSource.h>

#include <vtkActor.h>

#include <vtkPolyDataMapper.h>

#include <vtkProperty.h>

#include <qsurfaceformat.h>
#include <QVTKOpenGLNativeWidget.h>

#include <vtkSphereSource.h>
#include <vtkCubeSource.h>

#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>

#include "vtkAutoInit.h"

#include <QVTKOpenGLNativeWidget.h>
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageActor.h>
#include <vtkInteractorStyleImage.h>



#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkAxesActor.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPoints.h>
#include <vtkPolyLine.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>

#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkCubeAxesActor2D.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkSimplePointsReader.h>
#include <vtkWarpScalar.h>
#include <vtkAxisActor2D.h>

#include <vtkOrientationMarkerWidget.h>


MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->test4();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::test0()
{
    //初始化VTK窗口,命名为qvtkWidget(原理和QPushButton这种QT自带的控件一样,只是该控件由VTK提供,用法跟QPushButton是一样的)
    QVTKOpenGLNativeWidget* qvtkWidget = new QVTKOpenGLNativeWidget();
    //初始化VTK的渲染器,平时用的比较多是vtkRenderWindow,但是在QT中要改用vtkGenericOpenGLRenderWindow,实质上与vtkRenderWindow功能一致

    vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();


    QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
    //QVTKOpenGLNativeWidget* widget = new QVTKOpenGLNativeWidget();

    vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();

    vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();//创建一个球体
    sphereSource->SetRadius(2.0f);


    vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New();//创建一个立方体
    // cubeSource->SetRadius(2.0f);


    vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();//球体数据映射到渲染场景中

    // sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
    sphereMapper->SetInputConnection(cubeSource->GetOutputPort());

    vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();

    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetColor(colors->GetColor4d("Red").GetData());

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(sphereActor);
    renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("RenderWindowNoUIFile");

    //将渲染器加入到VTK窗口中。可以先写这一行,后续再将准备好的vtkRenderer加入到renderWindow中也是可以同步数据的
    qvtkWidget->setRenderWindow(renderWindow);

    this->setCentralWidget(qvtkWidget);
}

void MainWindow::test1()
{
    QVTKOpenGLNativeWidget* qvtkWidget = new QVTKOpenGLNativeWidget();


    // 创建VTK渲染器和渲染窗口
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    // 设置VTK渲染窗口到Qt Widget
    qvtkWidget->setRenderWindow(renderWindow);

    // 创建频谱图数据
    vtkSmartPointer<vtkImageCanvasSource2D> imageSource = vtkSmartPointer<vtkImageCanvasSource2D>::New();
    imageSource->SetScalarTypeToUnsignedChar();
    imageSource->SetNumberOfScalarComponents(3);
    imageSource->SetExtent(0, 100, 0, 100, 0, 100);
    imageSource->SetDrawColor(255, 0, 0);  // 设置颜色为红色
    imageSource->FillBox(0, 100, 0, 100);

    // 创建频谱图演员
    vtkSmartPointer<vtkImageActor> imageActor = vtkSmartPointer<vtkImageActor>::New();
    imageActor->SetInputData(imageSource->GetOutput());

    // 添加演员到渲染器
    renderer->AddActor(imageActor);
    renderer->ResetCamera();

    // 创建VTK交互器
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // 设置VTK交互器样式
    vtkSmartPointer<vtkInteractorStyleImage> interactorStyle = vtkSmartPointer<vtkInteractorStyleImage>::New();
    renderWindowInteractor->SetInteractorStyle(interactorStyle);

    renderWindowInteractor->Start();

    qvtkWidget->show();
}

void MainWindow::test2()
{
    QVTKOpenGLNativeWidget* qvtkWidget = new QVTKOpenGLNativeWidget();

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(0.0, 0.0, 0.0);
    points->InsertNextPoint(1.0, 1.0, 1.0);
    points->InsertNextPoint(3.0, 2.0, 2.0);
    points->InsertNextPoint(4.0, 2.0, 2.0);
    points->InsertNextPoint(6.0, 2.0, 7.0);

    vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
    for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
    {
        vertices->InsertNextCell(1);
        vertices->InsertCellPoint(i);
    }

    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
    polydata->SetPoints(points);
    polydata->SetVerts(vertices);

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData(polydata);

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();


    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);

    renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());


    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderWindow->Render();
    renderWindowInteractor->Start();

    qvtkWidget->setRenderWindow(renderWindow);

    this->setCentralWidget(qvtkWidget);
}

void MainWindow::test3()
{
    // Read the file
    vtkSmartPointer<vtkSimplePointsReader> reader = vtkSmartPointer<vtkSimplePointsReader>::New();
    reader->SetFileName ( "d://simple.xyz" );
    reader->Update();

    vtkSmartPointer<vtkPolyData> inputPolyData = vtkSmartPointer<vtkPolyData>::New();
    inputPolyData ->CopyStructure(reader->GetOutput());


    // warp plane
    vtkSmartPointer<vtkWarpScalar> warp = vtkSmartPointer<vtkWarpScalar>::New();
    warp->SetInputData(inputPolyData);
    // warp->SetInput(inputPolyData);
    warp->SetScaleFactor(0.0);

    // Visualize
    vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
    mapper->SetInputConnection(warp->GetOutputPort());



    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->GetProperty()->SetPointSize(4);
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(.3, .6, .3);
    renderWindow->Render();

    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    renderWindowInteractor->SetInteractorStyle(style);

    // add & render CubeAxes
    vtkSmartPointer<vtkCubeAxesActor2D> axes = vtkSmartPointer<vtkCubeAxesActor2D>::New();
    axes->SetInputData(warp->GetOutput());
    axes->SetFontFactor(3.0);
    axes->SetFlyModeToNone();
    axes->SetCamera(renderer->GetActiveCamera());


    vtkSmartPointer<vtkAxisActor2D> xAxis = axes->GetXAxisActor2D();
    xAxis->SetAdjustLabels(1);

    // 添加坐标轴
    vtkSmartPointer<vtkAxesActor> axesActor = vtkSmartPointer<vtkAxesActor>::New();
    vtkSmartPointer<vtkOrientationMarkerWidget> widget = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
    widget->SetOutlineColor(0.9300, 0.5700, 0.1300);
    widget->SetOrientationMarker(axesActor);
    widget->SetInteractor(renderWindowInteractor);
    widget->SetViewport(0.0, 0.0, 0.2, 0.2);
    widget->SetEnabled(1);
    widget->InteractiveOn();

    renderer->AddViewProp(axes);
    renderWindowInteractor->Start();
}

void MainWindow::test4()
{
    // 创建读取器
    vtkSmartPointer<vtkSimplePointsReader> reader = vtkSmartPointer<vtkSimplePointsReader>::New();
    reader->SetFileName("points.txt"); // 替换为你的数据文件路径

    // 创建映射器和演员
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(reader->GetOutputPort());

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    // 创建渲染器和渲染窗口
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    // 创建交互器
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // 将演员添加到渲染器中
    renderer->AddActor(actor);

    // 设置渲染器和窗口属性
    renderer->SetBackground(1.0, 1.0, 1.0); // 设置背景颜色为白色
    renderWindow->SetSize(800, 600);

    // 重置相机以适应数据范围
    renderer->ResetCamera();

    // 绘制曲线
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(0.0, 0.0, 0.0);
    points->InsertNextPoint(1.0, 1.0, 0.0);
    points->InsertNextPoint(2.0, 0.0, 0.0);

    vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
    vtkSmartPointer<vtkPolyLine> line = vtkSmartPointer<vtkPolyLine>::New();
    line->GetPointIds()->SetNumberOfIds(3);
    for (int i = 0; i < 3; ++i)
    {
        line->GetPointIds()->SetId(i, i);
    }
    lines->InsertNextCell(line);

    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
    polydata->SetPoints(points);
    polydata->SetLines(lines);

    vtkSmartPointer<vtkPolyDataMapper> curveMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    curveMapper->SetInputData(polydata);

    vtkSmartPointer<vtkActor> curveActor = vtkSmartPointer<vtkActor>::New();
    curveActor->SetMapper(curveMapper);
    curveActor->GetProperty()->SetColor(1.0, 0.0, 0.0); // 设置曲线颜色为红色

    renderer->AddActor(curveActor);

    // 添加坐标轴
    vtkSmartPointer<vtkAxesActor> axesActor = vtkSmartPointer<vtkAxesActor>::New();
    vtkSmartPointer<vtkOrientationMarkerWidget> widget = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
    widget->SetOutlineColor(0.9300, 0.5700, 0.1300);
    widget->SetOrientationMarker(axesActor);
    widget->SetInteractor(renderWindowInteractor);
    widget->SetViewport(0.0, 0.0, 0.2, 0.2);
    widget->SetEnabled(1);
    widget->InteractiveOn();

    // 开始渲染
    renderWindow->Render();

    // 启动交互
    renderWindowInteractor->Start();
}

void MainWindow::create()
{
    //存放三角网的顶点
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    //存放Cell顶点
    vtkSmartPointer<vtkCellArray> strips = vtkSmartPointer<vtkCellArray>::New();
    //存放每个顶点的标量值
    vtkSmartPointer<vtkFloatArray> scalars = vtkSmartPointer<vtkFloatArray>::New();
    //
    vtkSmartPointer<vtkPolyData> PolyData = vtkSmartPointer<vtkPolyData>::New();
    vtkSmartPointer<vtkPolyDataMapper> PolyDataMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    vtkSmartPointer<vtkLODActor> Actor = vtkSmartPointer<vtkLODActor>::New();
    //定义用于将属性映射到RGB颜色值的传递函数
    vtkSmartPointer<vtkColorTransferFunction> clrTransferFunc = vtkSmartPointer<vtkColorTransferFunction>::New();
    clrTransferFunc->SetColorSpaceToRGB();
    //获取三角网的(序号,顶点坐标)
    std::vector<SurfaceDataReader::SeqPoint> pts = m_sfReader->getTrianglePtLst();
    //获取三角网所有顶点的序号
    std::vector<int> topIdLst = m_sfReader->getTopIdLst();
    //将顶点坐标加入points、z值作为标量加入scalars
    TGS::Point3D pt;
    for (int index = 0; index < pts.size(); ++index)
    {
        pt = pts[index].second;
        scalars->InsertValue(pts[index].first, pt.z);
        points->InsertPoint(pts[index].first, pt.x, pt.y, pt.z);
    }
    //遍历所有序号,按每3个序号组合成一个cell
    for (int i = 0; i < topIdLst.size(); i += 3)
    {
        strips->InsertNextCell(3);
        strips->InsertCellPoint(topIdLst[i]);
        strips->InsertCellPoint(topIdLst[i + 1]);
        strips->InsertCellPoint(topIdLst[i + 2]);
    }
    PolyData->SetPoints(points);
    PolyData->SetStrips(strips);
    PolyData->GetPointData()->SetScalars(scalars);
    //计算三角网格顶点的法向量,平滑过渡颜色
    vtkSmartPointer<vtkPolyDataNormals> normFilter = vtkSmartPointer<vtkPolyDataNormals>::New();
    normFilter->SetInputData(PolyData);
    //开启点法向量计算
    normFilter->SetComputePointNormals(1);
    //关闭单元法向量计算
    normFilter->SetComputeCellNormals(0);
    normFilter->SetAutoOrientNormals(1);
    normFilter->SetSplitting(0);
    normFilter->Update();
    //获取色标信息
    std::vector<ColorBar::ColorInfo> clrs = m_clrBar->getColorBar();
    double dRange = m_zMax - m_zMin;
    double dValue = 0.0;
    for (int i = 0; i < clrs.size(); i++)
    {
        dValue = m_zMin + dRange * clrs[i].m_percent;
        unsigned int a, r, g, b;
        m_clrBar->convertColorToArgb(clrs[i].m_color, a, r, g, b);
        clrTransferFunc->AddRGBPoint(dValue, r / 255.0, g / 255.0, b / 255.0);
    }
    clrTransferFunc->Build();
    //
    PolyDataMapper->SetInputData(normFilter->GetOutput());
    PolyDataMapper->SetScalarRange(m_zMin, m_zMax);
    PolyDataMapper->SetLookupTable(clrTransferFunc);
    PolyDataMapper->ScalarVisibilityOn();
    Actor->SetMapper(PolyDataMapper);
    double* scale = m_vtkView->zoomScale();
    Actor->SetScale(scale[0], scale[1], scale[2]);
    m_pRender->AddActor(Actor);
    //
    m_pCtLine->initRender();
    m_pCtLine->create();

}

main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

simple.xyz;

0.0 0.0 3.0
1.0 4.0 0.0
0.0 1.0 0.0
4.0 0.0 3.0
1.0 4.0 7.0
0.0 6.0 0.0

借鉴了:

c++ - 使用VTK的3D等高线图C ++示例? - Thinbug

https://zhaojichao.blog.csdn.net/article/details/131714961?spm=1001.2101.3001.6650.14&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-14-131714961-blog-123477490.235%5Ev40%5Epc_relevant_rights_sort&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-14-131714961-blog-123477490.235%5Ev40%5Epc_relevant_rights_sort&utm_relevant_index=21

文章来源:https://blog.csdn.net/tadpole_java/article/details/135621741
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。