使用VTK读入点云文件的基础代码:
头文件:
也许不是全部都用到,为了接下来得工程还是全部都包含进去了
#include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkSphereSource.h" #include "vtkShrinkFilter.h" #include "vtkElevationFilter.h" #include "vtkDataSetMapper.h" #include "vtkActor.h" #include "vtkCullerCollection.h" #include "vtkInteractorStyleTrackballCamera.h" #include "vtkPolyVertex.h" #include "vtkUnstructuredGrid.h" #include <iostream> #include "vtkPolyDataMapper.h" #include "vtkScanPointReader.h" #include "vtkProperty.h"
main部分:
//读取点云数据 void main() { FILE *fp = NULL; fp = fopen( "C:\test.sp", "r"); if ( fp == NULL) { printf("Error in open file test.sp "); getchar(); exit(-1); }else printf("open scan point success "); vtkRenderer *ren = vtkRenderer::New(); double arr[3] = {0}; vtkPoints *points = vtkPoints::New(); int n = 0; while(!feof(fp))//首先读取点云数据到点表points同时指定点对应的id: { 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); fclose(fp); vtkPolyVertex *polyvertex = vtkPolyVertex::New(); polyvertex->GetPointIds()->SetNumberOfIds(n); int i = 0; for( i = 0;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(1,0, 0); ren->AddActor(actor1); ren->SetBackground(1, 1, 1); vtkRenderWindow* win=vtkRenderWindow::New(); win->AddRenderer(ren); win->SetSize(400,400); win->BordersOn(); //On则运行起来将会是全屏显示 //win->FullScreenOn(); //win->HideCursor(); vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(win); vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New(); iren->SetInteractorStyle(style); iren->Initialize(); iren->Start(); ren->Delete(); win->Delete(); iren->Delete(); }
运行结果:
本文使用的是 vs2005+vtk5.10.1
使用的是vs里面的控制台程序编写。
这样读入点云的方式是最原始的方式。