vtk实战(八)—读取.raw文件

时间:2022-04-25 12:58:27

主要用到两个类

vtkparticlereader ()
vtkPolyDataMapper ()

vtkparticlereader( )用来读取二进制或文本文件,格式是:“,”。该文本文件可以包含一个逗号分隔的值集。在大多数情况vtkparticlereader( )可以自动确定文件是否为文本或二进制。相对于二进制文件,支持随机存取文件的文件。
vtkpolydatamapper( )用来映射的多边形数据,使之到绘制图形到硬件/软件。

#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkParticleReader.h>
#include <vtkPolyDataMapper.h>


int main()
{

std::string filePath = "Particles.raw";
vtkSmartPointer<vtkParticleReader> reader =
vtkSmartPointer<vtkParticleReader>::New();

reader->SetFileName(filePath.c_str());
reader->SetDataByteOrderToBigEndian();
reader->Update();

vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
std::cout << "number of pieces: " << mapper->GetNumberOfPieces() << std::endl;
mapper->SetScalarRange(4, 9);

vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();

actor->SetMapper(mapper);
actor->GetProperty()->SetPointSize(4);

vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

renderer->AddActor(actor);
renderer->SetBackground(.2, .3, .4);

renderWindow->Render();
renderWindowInteractor->Start();

return 0;
}

vtk实战(八)—读取.raw文件

程序源码:http://pan.baidu.com/s/1o8cSd1w