vtk创建圆柱体
- 引言
- 开发环境
- 示例
- 运行效果
引言
本文记录使用vtk创建一个圆柱体。
开发环境
使用QtCreator4.11.2创建一个空项目,使用的vtk9.2的库和头文件,基于Qt5.14.2。
示例
这里提供pro文件中的内容和main.cpp中的内容。内容如下:
.pro
QT += core#greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += 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.0SOURCES += \main.cppSOUPDIR = $$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=vtkEventDataDevice3DDEFINES += 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
main.cpp
#include <vtkNew.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#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[])
{vtkNew<vtkNamedColors> colors;std::array<unsigned char,4> backGroundColor{{26,51,102,255}};colors->SetColor("BkgColor",backGroundColor.data());vtkNew<vtkCylinderSource> cyLinderSource;cyLinderSource->SetResolution(8);//设置圆柱体的面数vtkNew<vtkPolyDataMapper> mapper;mapper->SetInputConnection(cyLinderSource->GetOutputPort());//与数据建立绑定,后续直接取数组,取到的数据是最新的vtkNew<vtkActor> actor;actor->SetMapper(mapper);actor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());actor->RotateX(30.0);//整数——朝我旋转, 负数——向屏幕内旋转actor->RotateY(-45.0);//竖直方向
// actor->RotateZ(45.0);//负数——向右旋转,正数——向左旋转vtkNew<vtkRenderer> render;render->AddActor(actor);render->SetBackground(colors->GetColor3d("BkgColor").GetData());render->ResetCamera();render->GetActiveCamera()->Zoom(1.5);//将相机的视角进行放大,>!放大,<1缩小vtkNew<vtkRenderWindow> renderWindow;renderWindow->AddRenderer(render);renderWindow->SetSize(300,300);renderWindow->SetWindowName("CyLinder");renderWindow->Render();vtkNew<vtkRenderWindowInteractor> interactor;interactor->SetRenderWindow(renderWindow);interactor->Initialize();//指向初始化interactor->Start();//启动时间循环return 0;
}
运行效果
此工程中存在一个问题就是程序刚启动的时候会出现黑色的窗口,紧接着才变为上图中的效果。目前不知如何消除这种现象。