VTK的三维模型保存、读取之保存为vtk格式

基于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

具体介绍请看上面链接。

整个源代码如下:

#include "vtkSmartPointer.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkDICOMImageReader.h"
#include "vtkPolyDataWriter.h"
#include "vtkContourFilter.h"


#include "vtkPolyDataReader.h"
#include "vtkPolyDataMapper.h"
#include "vtkCamera.h"


void build3DView()
{
vtkSmartPointer aRenderer =
vtkSmartPointer::New();
vtkSmartPointer renWin =
vtkSmartPointer::New();
renWin->AddRenderer(aRenderer);


vtkSmartPointer iren =
vtkSmartPointer::New();
iren->SetRenderWindow(renWin);


vtkDICOMImageReader *dicomReader = vtkDICOMImageReader::New();
dicomReader->SetDataByteOrderToLittleEndian();
dicomReader->SetDirectoryName("D:\\dcm\\02");


/* vtkSmartPointer dicomReader =
vtkSmartPointer::New();  


dicomReader->SetFilePrefix("D:\\dcm\\new\\00");
dicomReader->SetFilePattern("%s%d.dcm");
*/
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 skinExtractor =
vtkSmartPointer::New();
skinExtractor->SetInputConnection(dicomReader->GetOutputPort());
skinExtractor->SetValue(0, 100);    //值越大,保留的部分越少。


// VTK 保存
vtkSmartPointer vtkWriter = vtkSmartPointer::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();
}


void readVTKFile()
{
vtkSmartPointer aRenderer =
vtkSmartPointer::New();
vtkSmartPointer renWin =
vtkSmartPointer::New();
renWin->AddRenderer(aRenderer);


vtkSmartPointer iren =
vtkSmartPointer::New();
iren->SetRenderWindow(renWin);


vtkSmartPointer vtkReader = vtkSmartPointer::New();
vtkReader->SetFileName("test.vtk");


vtkSmartPointer skinMapper =
vtkSmartPointer::New();
skinMapper->SetInputConnection(vtkReader->GetOutputPort());
skinMapper->ScalarVisibilityOff();    //这样不会带颜色


vtkSmartPointer skin =
vtkSmartPointer::New();
skin->SetMapper(skinMapper); 


vtkSmartPointer aCamera =
vtkSmartPointer::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();
}


void main()   //PS: 由于本人是使用分开的独立两个函数因此需要先调用build函数构建模型待生成了vtk格式文件后再读取。
{
//build3DView();//读入序列图像提取轮廓构建模型并保存
readVTKFile();//读取文件,读入以后也需要做构建平面之类的基本工作,以便显示

}

你可能感兴趣的:(研究学习)