在本程序中,定义了一个imread()函数,用于读取“.vts”文件,第一个参数为文件路径,第二个参数为输出对象,可以输出为tkStructuredGrid型对象,也可以输出为vtkActor型对象。
同时还定义了Window类,该类可以直接添加并显示actor对象。
下面是imread()函数的定义:
/**********************【定义数据读入函数imread,begin】*********************/ /**1.1、读取.vts型文件,输出vtkStructuredGrid型对象sgrid**/ void imread(const char* path_buffer,vtkSmartPointer<vtkStructuredGrid> sgrid) { /*将文件路径分解为:‘盘符(drive)’,‘路径(dir)’,‘文件名(fname)’,‘扩展名(ext)’*/ char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _splitpath( path_buffer, drive, dir, fname, ext ); string extName=ext; string className=sgrid->GetClassName(); vtkSmartPointer<vtkXMLStructuredGridReader> reader=vtkSmartPointer<vtkXMLStructuredGridReader>::New(); if(className=="vtkStructuredGrid"&&extName==".vts") { reader->SetFileName(path_buffer); reader->Update(); sgrid->ShallowCopy(reader->GetOutput()) ; } else { cout<<"The file format is different from the output type!"; } } /**1.2、读取.vts型文件,输出vtkActor型对象actor**/ void imread(const char* path_buffer,vtkSmartPointer<vtkActor> actor) { /*将文件路径分解为:‘盘符(drive)’,‘路径(dir)’,‘文件名(fname)’,‘扩展名(ext)’*/ char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _splitpath( path_buffer, drive, dir, fname, ext ); string extName=ext; string className=actor->GetClassName(); vtkSmartPointer<vtkXMLStructuredGridReader> reader=vtkSmartPointer<vtkXMLStructuredGridReader>::New(); if((className=="vtkOpenGLActor"||className=="vtkActor")&&extName==".vts") { reader->SetFileName(path_buffer); /*由于我们是用VTK的pipeline处理数据,所以每次加载都要更新; 在此读入文件,reader的输出将是一个有效的 structured grid data.*/ reader->Update(); vtkSmartPointer<vtkStructuredGridGeometryFilter> geometryFilter = vtkSmartPointer<vtkStructuredGridGeometryFilter>::New(); geometryFilter->SetInputConnection(reader->GetOutputPort()); geometryFilter->Update(); // Visualize vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(geometryFilter->GetOutputPort()); actor->SetMapper(mapper); } else { cout<<"The file format is different from the output type!"; } } /**********************【定义数据读入函数imread,end】*********************/
下面是Window函数的定义:
/************************【Window begin】********************************/ Window::Window() { ren1=vtkSmartPointer<vtkRenderer>::New(); ren1->SetBackground( 1., 1., 1. ); renWin=vtkSmartPointer<vtkRenderWindow>::New(); renWin->AddRenderer( ren1); renWin->SetSize( 800, 600 ); renWin->SetMultiSamples( 0 ); iren=vtkSmartPointer<vtkRenderWindowInteractor>::New(); iren->SetRenderWindow( renWin ); camera=vtkSmartPointer<vtkCamera>::New(); // camera->SetClippingRange( 0.576398, 28.8199 ); // camera->SetFocalPoint( 0.0463079, -0.0356571, 1.01993 ); // camera->SetPosition( -2.47044, 2.39516, -3.56066 ); // camera->SetViewUp( 0.607296, -0.513537, -0.606195 ); // ren1->SetActiveCamera( camera ); } void Window::addActor(vtkSmartPointer<vtkActor> actor) { ren1->AddActor(actor); } void Window::setBackgroundColor(float color[]) { this->ren1->SetBackground(color[0],color[1],color[2]); } /************************【Window end】********************************/
下面是测试程序:
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRenderingOpenGL) VTK_MODULE_INIT(vtkInteractionStyle) #endif #include <iostream> using namespace std; #include <vtkSmartPointer.h> #include <vtkPolyDataMapper.h> #include <vtkXMLStructuredGridReader.h> #include <vtkStructuredGridGeometryFilter.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <shape.h> int main(int argc, char* argv[]) { // Verify input arguments std::string inputFilename = "D:/Qt/QtVtk/Testing/Data/multicomb_2.vts"; vtkSmartPointer<vtkActor> actor=vtkSmartPointer<vtkActor>::New(); imread(inputFilename.c_str(),actor); Window win; win.addActor(actor); float color[3]={0.3,.6,.3}; win.setBackgroundColor(color); win.show(); return 0; }
下面是程序运行结果:
从测试程序来看,主程序得到了极大的简化。