目的
使用VTK加载STL格式模型
新建空白工程,添加QWidget控件并提升为QVTKOpenGLNativeWidget。
pro
1 2 3 4 5
| INCLUDEPATH += /usr/include/vtk-9.1
LIBS += -lvtkGUISupportQt-9.1 -lvtksys-9.1 -lvtkInteractionStyle-9.1 \ -lvtkRenderingOpenGL2-9.1 -lvtkRenderingCore-9.1 -lvtkCommonExecutionModel-9.1 \ -lvtkCommonCore-9.1 -lvtkIOGeometry-9.1
|
头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <vtkAutoInit.h> #include <vtkBMPReader.h> #include <vtkFloatArray.h> #include <vtkGenericOpenGLRenderWindow.h> #include <vtkImageActor.h> #include <vtkImageChangeInformation.h> #include <vtkImageData.h> #include <vtkImageImport.h> #include <vtkImageViewer2.h> #include <vtkInteractorStyleImage.h> #include <vtkJPEGReader.h> #include <vtkLookupTable.h> #include <vtkMetaImageReader.h> #include <vtkNamedColors.h> #include <vtkNew.h> #include <vtkPointData.h> #include <vtkPolyDataMapper.h> #include <vtkProperty.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <vtkSTLReader.h> #include <vtkSmartPointer.h> #include <vtkXMLImageDataWriter.h> #include <vtkImageBlend.h> #include <vtkImageCanvasSource2D.h> #include <vtkImageIterator.h> #include <vtkImageLuminance.h> #include <vtkImageStencil.h> #include <vtkImageStencilData.h> #include <vtkImageViewer.h> #include <vtkPNGReader.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2) VTK_MODULE_INIT(vtkInteractionStyle)
|
读取
1 2 3 4 5 6
| QString selectFilePath = "Gear_with_50_teeths.stl";
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New(); reader->SetFileName(selectFilePath.toStdString().c_str()); reader->Update();
|
转换
1 2 3
| vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(reader->GetOutputPort());
|
加载
1 2 3
| vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper);
|
渲染
1 2 3
| vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddActor(actor); renderer->SetBackground(.0, .0, .0);
|
显示
1 2 3
| vtkSmartPointer<vtkGenericOpenGLRenderWindow> window = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New(); ui->widget->setRenderWindow(window); ui->widget->renderWindow()->AddRenderer(renderer);
|
效果
编译运行程序
说明
要想在窗口显示(window),需要设置渲染器(renderer),渲染器需要输入(actor),输入需要数据(mapper),数据要先读取(reader)。可以看到流程上和上一篇一样。
流程为
flowchart TB
A[reader] --> B[mapper]
B --> C[actor]
C --> D[renderer]
D --> E[window]