使用vtk在一个窗口中创建多个几何体

引言

该示例为官网上的例子。在一个窗口中创建了多个不同的几何体。
其效果如下:
使用vtk在一个窗口中创建多个几何体_第1张图片

示例

开发环境

使用QtCreator4.11.2,Qt5.14.2。使用的vtk9.2的库及其头文件。创建空项目。

示例代码

其pro文件中的内容:

QT       += core

#greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11 vtk9.2

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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


SOUPDIR = $$PWD/../../SOUPdependency
vtk9.2 {
    contains(QT_ARCH, x86_64) {
        include($$SOUPDIR/vtk-9.2/vtk-9.2.pri)
    } else {
        include($$SOUPDIR/vtk-9.2-2017-omp-win32/vtk-9.2.pri)
    }
    DEFINES += vtkEventDataButton3D=vtkEventDataDevice3D
    DEFINES += vtkEventDataMove3D=vtkEventDataDevice3D
}

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

SOURCES += \
    main.cpp

main.cpp

#include 
#include 
#include 
#include //箭头
#include //圆锥
#include //立方体
#include //圆柱体
#include //支持碎片的球体
#include 
#include //直线
#include //规则多边形
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2)//渲染
VTK_MODULE_INIT(vtkInteractionStyle)//交互样式
VTK_MODULE_INIT(vtkRenderingFreeType)//文本图像
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)//体素

int main(int argc,char *argv[])
{
    vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();
    std::array<unsigned char,4> bkg{51,77,102,255};
    colors->SetColor("bkg",bkg.data());

    std::vector<vtkSmartPointer<vtkPolyDataAlgorithm>> geometricObjectSources;
    geometricObjectSources.push_back(vtkSmartPointer<vtkArrowSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkConeSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkCubeSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkCylinderSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkDiskSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkLineSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkRegularPolygonSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkPSphereSource>::New());

    std::vector<vtkSmartPointer<vtkPolyDataMapper>> mappers;
    std::vector<vtkSmartPointer<vtkActor>> actors;
    std::vector<vtkSmartPointer<vtkTextMapper>> textMappers;
    std::vector<vtkSmartPointer<vtkActor2D>> textActors;

    vtkNew<vtkTextProperty> textProperty;
    textProperty->SetFontSize(16);
    textProperty->SetJustificationToCentered();//文本居中
    textProperty->SetColor(colors->GetColor3d("LightGoldenrodYellow").GetData());

    for(unsigned int i = 0; i < geometricObjectSources.size();++i)
    {
        geometricObjectSources[i]->Update();//使输出为最新的
        mappers.push_back(vtkSmartPointer<vtkPolyDataMapper>::New());
        mappers[i]->SetInputConnection(geometricObjectSources[i]->GetOutputPort());

        actors.push_back(vtkSmartPointer<vtkActor>::New());
        actors[i]->SetMapper(mappers[i]);
        actors[i]->GetProperty()->SetColor(colors->GetColor3d("PeachPuff").GetData());

        textMappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
        textMappers[i]->SetInput(geometricObjectSources[i]->GetClassName());//设置输入的文本字符串
        textMappers[i]->SetTextProperty(textProperty);

        textActors.push_back(vtkSmartPointer<vtkActor2D>::New());
        textActors[i]->SetMapper(textMappers[i]);
        textActors[i]->SetPosition(120,16);
    }

    auto gridRow = 3;
    auto gridCol = 3;
    int renderSize = 300;

    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->SetWindowName("GeometricObjecName");
    renderWindow->SetSize(renderSize *gridCol,renderSize *gridRow);

    for(auto row = 0; row < gridRow; ++row)
    {
        for(auto col = 0; col < gridCol; ++col)
        {
            auto index = row *gridCol + col;
            vtkSmartPointer<vtkRenderer> render = vtkSmartPointer<vtkRenderer>::New();
            render->SetBackground(colors->GetColor3d("BkgColor").GetData());
            double viewport[4] = {static_cast<double>(col)/gridCol,static_cast<double>(gridRow - row -1) / gridRow,
                                  static_cast<double>(col +1) / gridCol,static_cast<double>(gridRow - row) / gridRow};
            render->SetViewport(viewport);
            if(index < geometricObjectSources.size())
            {
                render->AddActor(actors[index]);
                render->AddActor(textActors[index]);
                render->ResetCamera();
            }
            renderWindow->AddRenderer(render);
        }
    }
    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);

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

    return 0;
}

疑惑

本例中main.cpp里的代码有一行,

double viewport[4] = {static_cast<double>(col)/gridCol,static_cast<double>(gridRow - row -1) / gridRow,
                                  static_cast<double>(col +1) / gridCol,static_cast<double>(gridRow - row) / gridRow};

不明白这个是什么意思,望能知其意的人指点。

你可能感兴趣的:(VTK,VTK)