vtk实战(九)—读取三维点坐标2

以文本文件格式读取并存到一个vtkPoints( ),然后放入vtkPolyData( ),最后使用vtkVertexGlyphFilter( )显示。
程序主要用到以下类:

vtkPoints( ) vtkPolyData( ) vtkVertexGlyphFilter( )

vtkPoints( )负责点云数据的存储和操作,vtkPolyData()可以用来保存点、线、面,vtkVertexGlyphFilter()用来加快点云处理速度。
程序代码:

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

#include <sstream>

int main()
{
    std::string filename = "8.txt";
    std::ifstream filestream(filename.c_str());

    std::string line;
    vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();

    while (std::getline(filestream, line))
    {
        double x, y, z;
        std::stringstream linestream;
        linestream << line;
        linestream >> x >> y >> z;

        points->InsertNextPoint(x, y, z);
    }

    filestream.close();

    vtkSmartPointer<vtkPolyData> polyData =
        vtkSmartPointer<vtkPolyData>::New();

    polyData->SetPoints(points);

    vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
        vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
    glyphFilter->SetInputConnection(polyData->GetProducerPort());
#else
    glyphFilter->SetInputData(polyData);
#endif
    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, .3); 

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

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}

程序运行结果:
vtk实战(九)—读取三维点坐标2_第1张图片

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

你可能感兴趣的:(VTK)