系列索引:VTK入门教程索引
先使用VTK显示一张图片。
新建一个Qt工程,将QWidget提升为QVTKOpenGLNativeWidget
pro
添加库文件路径
1 2 3 4 5 6
| INCLUDEPATH += /usr/include/vtk-9.1
LIBS += -L/usr/local/lib -lvtkGUISupportQt-9.1 -lvtksys-9.1 -lvtkInteractionStyle-9.1 \ -lvtkRenderingOpenGL2-9.1 -lvtkRenderingCore-9.1 -lvtkCommonExecutionModel-9.1 \ -lvtkCommonCore-9.1 -lvtkIOGeometry-9.1 -lvtkIOImage-9.1 -lvtkRenderingImage-9.1 \ -lvtkViewsCore-9.1 -lvtkImagingCore-9.1 -lvtkInteractionImage-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 36
| #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 <vtkEventQtSlotConnect.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
| public slots: void updateCoords(vtkObject*obj);
private: vtkEventQtSlotConnect* m_Connections;
|
读取数据
在构造函数中添加
1 2 3 4 5 6
| QString selectFilePath = "/home/jackey/Pictures/vtk/q.png";
vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New(); reader->SetFileName(selectFilePath.toStdString().c_str()); reader->Update();
|
图片查看器
1 2 3
| vtkSmartPointer< vtkImageViewer2 > m_pImageViewer= vtkSmartPointer< vtkImageViewer2 >::New(); m_pImageViewer->SetInputConnection(reader->GetOutputPort());
|
设置渲染窗口
创建OpenGL窗口
1
| vtkSmartPointer<vtkGenericOpenGLRenderWindow> window = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
|
设置渲染窗口
1 2 3
| ui->widget->setRenderWindow(window); m_pImageViewer->SetRenderWindow(window); m_pImageViewer->Render();
|
图片是在view上显示的,view依附于window,所以要想界面显示图片就得刷新viewer
事件
先绑定事件
1 2 3 4 5
| m_Connections = vtkEventQtSlotConnect::New(); m_Connections->Connect(window->GetInteractor(), vtkCommand::MouseMoveEvent, this, SLOT(updateCoords(vtkObject*)));
|
槽函数
1 2 3 4 5 6 7 8 9 10 11 12 13
| void MainWindow::updateCoords(vtkObject *obj) { vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::SafeDownCast(obj); int event_pos[2]; iren->GetEventPosition(event_pos); QString str; str.sprintf("x=%d : y=%d", event_pos[0], event_pos[1]); ui->statusbar->showMessage(str); }
|
这样鼠标从窗口划过后,状态栏会显示鼠标坐标。
效果
编译运行程序
程序流程图为
flowchart TB
A[reader] --> B[viewer]
B --> C[window]