vtk实战(十)—文本格式转vtp格式

vtkxmlpolydatawriter()用来写入VTK XML polydata 格式的文件。
vtkcellarray()支持的列表结构形式:(n,id1,id2,…,idn, n,id1,id2,…,idn, …)其中n是在结构中点的数目,ID为偏移相关点列表。这种数据结构的优点:紧凑性,简单,易于外部数据的接口。然而,它是完全不足够的随机访问。

#include "vtkVersion.h"
#include "vtkSmartPointer.h"
#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkXMLPolyDataWriter.h"
#include <vtkVertexGlyphFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <iostream>

vtkPolyData * custom_reader(std::istream & infile)
{
    vtkIdType number_of_points, number_of_triangles;
    infile >> number_of_points >> number_of_triangles;

    vtkSmartPointer<vtkPoints> points
        = vtkSmartPointer<vtkPoints>::New();
    points->SetNumberOfPoints(number_of_points);
    for (vtkIdType i = 0; i < number_of_points; i++)
    {
        double x, y, z;
        infile >> x >> y >> z;
        points->SetPoint(i, x, y, z);
    }

    vtkSmartPointer<vtkCellArray> polys
        = vtkSmartPointer<vtkCellArray>::New();
    for (vtkIdType i = 0; i < number_of_triangles; i++)
    {
        vtkIdType a, b, c;
        infile >> a >> b >> c;
        polys->InsertNextCell(3);
        polys->InsertCellPoint(a);
        polys->InsertCellPoint(b);
        polys->InsertCellPoint(c);
    }
    vtkPolyData * polyData = vtkPolyData::New();
    polyData->SetPoints(points);
    polyData->SetPolys(polys);

    return polyData;
}

int main()
{
    std::string inputFilename = "888.txt";
    std::ifstream fin(inputFilename.c_str());

    vtkSmartPointer<vtkPolyData> polyData
        = vtkSmartPointer<vtkPolyData>::Take(
        custom_reader(fin));

    vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
        vtkSmartPointer<vtkVertexGlyphFilter>::New();
    glyphFilter->SetInputConnection(polyData->GetProducerPort());

    glyphFilter->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(glyphFilter->GetOutputPort());

    vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->SetBackground(.3, .6, .4); 

    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindow->Render();
    renderWindowInteractor->Start();
    vtkSmartPointer<vtkXMLPolyDataWriter> writer =
        vtkSmartPointer<vtkXMLPolyDataWriter>::New();
    writer->SetInput(polyData);

    writer->SetFileName("888.vtp");
    writer->Write();
    return 0;
}

代码链接:http://pan.baidu.com/s/1c1Degu

你可能感兴趣的:(VTK)