[VTK]VTK的三维模型保存、读取之保存为vtk格式
0. begin
基于VTK的模型构建以后经常需要对模型进行保存,当然我不想保存为一个二维的图像格式
因为那样重新读取太过复杂,如果能将之进行保存为模型类似的格式那么以后就只需要直接读取即可。
三维模型格式应该较多,百度、谷歌都没有找到较多的范例,不过还是有可使用的,这里只介绍.vtk 格式。
首先介绍一下本文中使用的方法所用到的主要的类:
vtkDataWriter
vtkPolyDataWriter is a source object that writes ASCII or binary polygonal data files in vtk format. See text for format details.
http://www.vtk.org/doc/nightly/html/classvtkPolyDataWriter.html#details
vtkDataReader
http://www.vtk.org/doc/nightly/html/classvtkDataReader.html
具体介绍请看上面链接。
1. detail
接下来具体介绍保存的方法:
vtkSmartPointer<vtkPolyDataWriter> vtkWriter = vtkSmartPointer<vtkPolyDataWriter>::New(); vtkWriter->SetInput(skinExtractor->GetOutput()); vtkWriter->SetFileName("test.vtk"); vtkWriter->Write();
很简单吧!只需要设置一下输入以及保存的文件名即可。
setinput的参数如何设置就依看官而定了。
读取更是容易
vtkSmartPointer<vtkPolyDataReader> vtkReader = vtkSmartPointer<vtkPolyDataReader>::New(); vtkReader->SetFileName("test.vtk");
下面提供整个源代码:
1. 读入序列图像提取轮廓构建模型并保存:
其中涉及到使用的图像数据这部分就由看官使用自己的构建步骤说使用素材替代即可。
void build3DView() { vtkSmartPointer<vtkRenderer> aRenderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New(); renWin->AddRenderer(aRenderer); vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New(); iren->SetRenderWindow(renWin); vtkSmartPointer<vtkJPEGReader> dicomReader = vtkSmartPointer<vtkJPEGReader>::New(); dicomReader->SetFilePrefix("C:/Users/DawnWind/Desktop/000/"); dicomReader->SetFilePattern("%s%d.jpg"); dicomReader->SetDataByteOrderToLittleEndian(); //dicomReader->SetDataSpacing(1, 0.925, 1.2); dicomReader->SetDataSpacing(1, 1, 1.4); dicomReader->SetFileNameSliceSpacing(1); //dicomReader->SetDataExtent(0, 209, 0, 209, 0, 83); dicomReader->SetDataExtent(0, 209, 0, 209, 0, 29); dicomReader->Update(); vtkSmartPointer<vtkContourFilter> skinExtractor = vtkSmartPointer<vtkContourFilter>::New(); skinExtractor->SetInputConnection(dicomReader->GetOutputPort()); skinExtractor->SetValue(0, 100); //值越大,保留的部分越少。 // VTK 保存 vtkSmartPointer<vtkPolyDataWriter> vtkWriter = vtkSmartPointer<vtkPolyDataWriter>::New(); vtkWriter->SetInput(skinExtractor->GetOutput()); vtkWriter->SetFileName("test.vtk"); vtkWriter->Write(); // Render renWin->Render(); // Initialize the event loop and then start it. iren->Initialize(); iren->Start(); }
2. 读取文件
读入以后也需要做构建平面之类的基本工作,以便显示
void readVTKFile() { vtkSmartPointer<vtkRenderer> aRenderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New(); renWin->AddRenderer(aRenderer); vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New(); iren->SetRenderWindow(renWin); vtkSmartPointer<vtkPolyDataReader> vtkReader = vtkSmartPointer<vtkPolyDataReader>::New(); vtkReader->SetFileName("test.vtk"); vtkSmartPointer<vtkPolyDataMapper> skinMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); skinMapper->SetInputConnection(vtkReader->GetOutputPort()); skinMapper->ScalarVisibilityOff(); //这样不会带颜色 vtkSmartPointer<vtkActor> skin = vtkSmartPointer<vtkActor>::New(); skin->SetMapper(skinMapper); vtkSmartPointer<vtkCamera> aCamera = vtkSmartPointer<vtkCamera>::New(); aCamera->SetViewUp (0, 0, -1); aCamera->SetPosition (0, 1, 0); aCamera->SetFocalPoint (0, 0, 0); aCamera->ComputeViewPlaneNormal(); aCamera->Azimuth(30.0); aCamera->Elevation(30.0); aCamera->Dolly(1.5); aRenderer->AddActor(skin); aRenderer->SetActiveCamera(aCamera); aRenderer->ResetCamera (); aRenderer->SetBackground(.2, .3, .4); aRenderer->ResetCameraClippingRange (); renWin->Render(); iren->Initialize(); iren->Start(); }
下面是调用readVTKFile函数进行读取的结果:
PS: 由于本人是使用分开的独立两个函数因此需要先调用build函数构建模型待生成了vtk格式文件后再读取。