vtk 盒子的显示

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


#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkCubeSource.h"
#include "vtkAxes.h"
#include "vtkTubeFilter.h"
#include "vtkSphereSource.h"
int main(int argc, char* argv[])
{
    FILE *fp = NULL;
    if (argc==1)
    {
        if ((fp=fopen("mbr.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];
    while (!feof(fp))
    {
        int ret = fscanf(fp, "%lf%lf%lf%lf%lf%lf", &arr[0], &arr[1], &arr[2],
                &arr[3], &arr[4], &arr[5]);
        if (ret!=6)
            break;
        vtkCubeSource *cube=vtkCubeSource::New();
        cube->SetBounds(arr[0],arr[3],arr[1],arr[4],arr[2],arr[5]);

        vtkPolyDataMapper * map=vtkPolyDataMapper::New();
        map->SetInputConnection(cube->GetOutputPort());

        vtkActor *actor=vtkActor::New();
        actor->SetMapper(map);
        /*-----------------------------*/
        actor->GetProperty()->SetColor(0.194, 0.562, 0.75);
        //actor->GetProperty()->SetLineStipplePattern(1);
        //actor->GetProperty()->SetOpacity(0.4);//设定透明度
        //actor->GetProperty()->SetRepresentationToWireframe();
//设定显示方式为线框模式
        ren->AddActor(actor);
    }
    fclose(fp);

    ren->SetBackground(1, 1, 1);
    //    ren->WorldToView();
    ren->Transparent();
    vtkRenderWindow* win=vtkRenderWindow::New();
    win->AddRenderer(ren);
    win->SetSize(400,400);

    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();

    iren->SetRenderWindow(win);
    vtkInteractorStyleTrackballCamera *style =
        vtkInteractorStyleTrackballCamera::New();
    iren->SetInteractorStyle(style);   

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

    return 0;
}
编译命令为:

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

运行并查看结果:

                       
        ./cube cube.asc
                       
               


登录 *


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