vtk点云数据的显示[转]

时间:2023-11-24 10:27:56

#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkPoints.h"
#include "vtkPolyVertex.h"
#include "vtkUnstructuredGrid.h"
#include "vtkDataSetMapper.h" int main(int argc, char* argv[])
{
FILE *fp = NULL;
if (argc==)
{
if ((fp=fopen("venus.asc","r"))== NULL)
{
printf("Error in open file mbr.asc\n");
return ;
}
}
else
{
if ((fp=fopen(argv[],"r"))== NULL)
{
printf("Error in open file %s\n", argv[]);
return ;
}
} vtkRenderer *ren=vtkRenderer::New();
double arr[]; vtkPoints * points = vtkPoints::New();
int n=;
while(!feof(fp))//首先读取点云数据到点表points同时指定点对应的id:
{
int ret=fscanf(fp,"%lf %lf %lf",&arr[],&arr[],&arr[]);
if(ret!=)
break;
points->InsertPoint(n,arr[],arr[],arr[]);
n++;
}
printf("%d\n", n);
fclose(fp); vtkPolyVertex * polyvertex = vtkPolyVertex::New();
polyvertex->GetPointIds()->SetNumberOfIds(n);
int i=;
for(i=;i<n;i++)//建立拓扑关系
{
polyvertex->GetPointIds()->SetId(i,i);
} vtkUnstructuredGrid * grid=vtkUnstructuredGrid::New();
grid->SetPoints(points);
grid->InsertNextCell(polyvertex->GetCellType(),
polyvertex->GetPointIds()); vtkDataSetMapper *map1 = vtkDataSetMapper::New();
map1->SetInput(grid); vtkActor *actor1 = vtkActor::New();
actor1->SetMapper(map1);
actor1->GetProperty()->SetColor(0.194,0.562, 0.75); ren->AddActor(actor1);
ren->SetBackground(, , ); vtkRenderWindow* win=vtkRenderWindow::New();
win->AddRenderer(ren);
win->SetSize(,);
win->BordersOn();
//win->FullScreenOn();
//win->HideCursor(); vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(win);
vtkInteractorStyleTrackballCamera *style =
vtkInteractorStyleTrackballCamera::New();
iren->SetInteractorStyle(style); iren->Start();
ren->Delete();
win->Delete();
iren->Delete(); return ;
}
编译命令为
gcc -o 可执行文件名 源文件名 -I 头文件目录 需要的库文件 -Wno-dprecated: gcc -o cloud cloud.cxx -I /usr/include/vtk-5.0 /usr/lib/libvtkRendering.so -Wno-deprecated 运行: ./cloud venus.asc //数据文件 转为:http://www.cnblogs.com/lilun/archive/2013/06/10/3131240.html