引言
该示例为官网上的例子。在一个窗口中创建了多个不同的几何体。
其效果如下:
示例
开发环境
使用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 <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkPolyDataMapper.h>
#include <vtkArrowSource.h>//箭头
#include <vtkConeSource.h>//圆锥
#include <vtkCubeSource.h>//立方体
#include <vtkCylinderSource.h>//圆柱体
#include <vtkPSphereSource.h>//支持碎片的球体
#include <vtkDiskSource.h>
#include <vtkLineSource.h>//直线
#include <vtkRegularPolygonSource.h>//规则多边形
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkSmartPointer.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkTextMapper.h>
#include <vtkTextProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <array>
#include <vector>
#include <vtkCamera.h>
#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};
不明白这个是什么意思,望能知其意的人指点。