vtk线段的显示

一剑行天下 posted @ 2008年11月16日 00:12 in vtk , 2537 阅读


#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkSphereSource.h"
#include "vtkPoints.h"
#include "vtkPolyVertex.h"
#include "vtkUnstructuredGrid.h"
#include "vtkDataSetMapper.h"
#include "vtkCamera.h"
#include "vtkLine.h"
#include "vtkAxes.h"
#include "vtkTubeFilter.h"

int main(int argc, char* argv[])
{
    FILE *fp = NULL;
    if (argc==1)
    {
        if ((fp=fopen("line.asc","r"))== NULL)
        {
            printf("Error in open file mbr.asc\n");
            return 1;
        }
    }
    else
    {
        if ((fp=fopen(argv[1],"r"))== NULL)
        {
            printf("Error in open file %s\n", argv[1]);
            return 1;
        }
    }

    vtkRenderer *ren=vtkRenderer::New();
    double arr[6];

    vtkPoints * points = vtkPoints::New();
    int n=0;
    while(!feof(fp))//读点
    {
        int ret=fscanf(fp,"%lf %lf %lf",&arr[0],&arr[1],&arr[2]);
        if(ret!=3)
            break;     
        points->InsertPoint(n,arr[0],arr[1],arr[2]);
        n++;
    }
    printf("%d\n", n);
    fclose(fp);

    vtkUnstructuredGrid * grid=vtkUnstructuredGrid::New();
    grid->SetPoints(points);
    int i=0;
    for(i=0;i<n;i++)  //建立拓扑关系
    {
        vtkLine *aline = vtkLine::New();
        aline->GetPointIds()->SetNumberOfIds(2);
        aline->GetPointIds()->SetId(0,i);
        i++;
        aline->GetPointIds()->SetId(1,i);
        grid->InsertNextCell(aline->GetCellType(),
            aline->GetPointIds());
    }
   
    vtkDataSetMapper *map1 = vtkDataSetMapper::New();
    map1->SetInput(grid);

    vtkActor *actor1 = vtkActor::New();
    actor1->SetMapper(map1);
    actor1->GetProperty()->SetColor(0.0,0.0, 1);

    ren->AddActor(actor1);
    ren->SetBackground(1, 1, 1);

    vtkRenderWindow* win=vtkRenderWindow::New();
    win->AddRenderer(ren);
    win->SetSize(400,400);

    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();

    iren->SetRenderWindow(win);
    win->Render();
    iren->Start();
    //iren->Initialize();

    ren->Delete();
    win->Delete();
    iren->Delete();

    return 0;
}
编译命令如下:

                       
        gcc -o lines lines.cxx -I /usr/include/vtk-5.0 /usr/lib/libvtkRendering.so -Wno-deprecated
                       
               


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter