VTK读取一个TXT文档中的三维点坐标显示三维点云 (vtk 非智能指针删除)

VTK读取一个TXT文档中的三维点坐标就可以显示三维点云,txt文档中的格式为

X坐标 Y坐标  Z坐标

如下所示:



附上代码如下:

  1. #include <iostream>  
  2. #include <vector>  
  3. #include "vtkActor.h"  
  4. #include "vtkRenderer.h"  
  5. #include "vtkRenderWindow.h"  
  6. #include "vtkRenderWindowInteractor.h"  
  7. #include "vtkProperty.h"  
  8. #include "vtkInteractorStyleTrackballCamera.h"  
  9. #include "vtkPoints.h"  
  10. #include "vtkPolyVertex.h"  
  11. #include "vtkUnstructuredGrid.h"  
  12. #include "vtkDataSetMapper.h"  
  13. #include "vtkPolyData.h"  
  14. #include "vtkCellArray.h"  
  15. #include "vtkInteractorStyleTrackball.h"  
  16. #include "vtkPolyDataMapper.h"  
  17. using namespace std;  
  18.   
  19. void main(int argc, char* argv[])  
  20. {   
  21.     vtkPoints *m_Points = vtkPoints::New();  
  22.     vtkCellArray *vertices = vtkCellArray::New();   //_存放细胞顶点,用于渲染(显示点云所必须的)  
  23.     vtkPolyData *polyData = vtkPolyData::New();  
  24.     vtkPolyDataMapper *pointMapper = vtkPolyDataMapper::New();  
  25.     vtkActor *pointActor = vtkActor::New();  
  26.     vtkRenderer *ren1= vtkRenderer::New();  
  27.     vtkRenderWindow *renWin = vtkRenderWindow::New();  
  28.     vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();  
  29.     vtkInteractorStyleTrackball *istyle = vtkInteractorStyleTrackball::New();  
  30.   
  31.     //_读进点云数据信息  
  32.     FILE*fp = NULL; fp=fopen("point.txt","r");  //2DpointDatas.txt  
  33.     if(!fp)  
  34.     {  
  35.           
  36.         printf("打开文件失败!!\n");  
  37.         int m;  
  38.         cin>>m;  
  39.         exit(0);  
  40.     }  
  41.     double x=0,y=0,z=0;  
  42.     int i = 0;  
  43.     while (!feof(fp))  
  44.     {  
  45.         fscanf(fp,"%lf %lf %lf",&x,&y,&z);    
  46.         m_Points->InsertPoint(i,x,y,z);      //_加入点信息  
  47.         vertices->InsertNextCell(1);     //_加入细胞顶点信息----用于渲染点集  
  48.         vertices->InsertCellPoint(i);  
  49.         i ++;  
  50.     }     
  51.     fclose(fp);  
  52.   
  53.     //_创建待显示数据源  
  54.   
  55.     polyData->SetPoints(m_Points);       //_设置点集  
  56.     polyData->SetVerts(vertices);        //_设置渲染顶点  
  57.     pointMapper->SetInput( polyData );  
  58.   
  59.     pointActor->SetMapper( pointMapper );  
  60.     pointActor->GetProperty()->SetColor(0.0,0.1,1.0);  
  61.     pointActor->GetProperty()->SetAmbient(0.5);  
  62.     pointActor->GetProperty()->SetPointSize(2);  
  63.     //pointActor->GetProperty()->SetRepresentationToWireframe();  
  64.     //pointActor->GetProperty()->SetRepresentationToSurface();  
  65.   
  66.     ren1->AddActor( pointActor );  
  67.     ren1->SetBackground( 0, 0, 0);  
  68.   
  69.     renWin->AddRenderer( ren1 );  
  70.     renWin->SetSize(800,800);  
  71.   
  72.     iren->SetInteractorStyle(istyle);    
  73.     iren->SetRenderWindow(renWin);  //交互  
  74.   
  75.     renWin->Render();  
  76.     iren->Start();  
  77.   
  78.     //删除各指针  
  79.     m_Points->Delete();  
  80.     vertices->Delete();  
  81.     polyData->Delete();  
  82.     pointMapper->Delete();  
  83.     pointActor->Delete();  
  84.     ren1->Delete();  
  85.     renWin->Delete();  
  86.     iren->Delete();  
  87.     istyle->Delete();  
  88. }   


智能指针版本

  1. #include <iostream>  
  2. #include <vector>  
  3. #include <vtkActor.h>  
  4. #include <vtkRenderer.h>  
  5. #include <vtkRenderWindow.h>  
  6. #include <vtkRenderWindowInteractor.h>  
  7. #include <vtkProperty.h>  
  8. #include <vtkInteractorStyleTrackball.h>  
  9. #include <vtkInteractorStyleTrackballCamera.h>  
  10. #include <vtkPoints.h>  
  11. #include <vtkPolyData.h>  
  12. #include <vtkCellArray.h>  
  13. #include <vtkPolyDataMapper.h>  
  14. #include <vtkSmartPointer.h>  
  15. using namespace std;  
  16.   
  17. void main(int argc, char* argv[])  
  18. {   
  19.     vtkSmartPointer<vtkPoints> m_Points = vtkSmartPointer<vtkPoints>::New();  
  20.     vtkSmartPointer<vtkCellArray> vertices =vtkSmartPointer<vtkCellArray>::New();   //_存放细胞顶点,用于渲染(显示点云所必须的)  
  21.     vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();  
  22.     vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();  
  23.     vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();  
  24.     vtkSmartPointer<vtkRenderer> ren1=vtkSmartPointer< vtkRenderer>::New();  
  25.     vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();  
  26.     vtkSmartPointer<vtkRenderWindowInteractor> iren =vtkSmartPointer<vtkRenderWindowInteractor>::New();  
  27.     vtkSmartPointer<vtkInteractorStyleTrackballCamera> istyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();  
  28.   
  29.     //_读进点云数据信息  
  30.     FILE*fp = NULL; fp=fopen("E:\\斯坦福兔子3000点.txt","r"); //2DpointDatas.txt  
  31.     if(!fp)  
  32.     {  
  33.   
  34.         printf("打开文件失败!!\n");  
  35.         int m;  
  36.         cin>>m;  
  37.         exit(0);  
  38.     }  
  39.     double x=0,y=0,z=0;  
  40.     int i = 0;  
  41.     while (!feof(fp))  
  42.     {  
  43.         fscanf(fp,"%lf %lf %lf",&x,&y,&z);    
  44.         m_Points->InsertPoint(i,x,y,z);      //_加入点信息  
  45.         vertices->InsertNextCell(1);     //_加入细胞顶点信息----用于渲染点集  
  46.         vertices->InsertCellPoint(i);  
  47.         i ++;  
  48.     }     
  49.     fclose(fp);  
  50.   
  51.     //_创建待显示数据源  
  52.   
  53.     polyData->SetPoints(m_Points);       //_设置点集  
  54.     polyData->SetVerts(vertices);        //_设置渲染顶点  
  55.     pointMapper->SetInput( polyData );  
  56.   
  57.     pointActor->SetMapper( pointMapper );  
  58.     pointActor->GetProperty()->SetColor(0.0,0.1,1.0);  
  59.     pointActor->GetProperty()->SetAmbient(0.5);  
  60.     pointActor->GetProperty()->SetPointSize(2);  
  61.     //pointActor->GetProperty()->SetRepresentationToWireframe();  
  62.     //pointActor->GetProperty()->SetRepresentationToSurface();  
  63.   
  64.     ren1->AddActor( pointActor );  
  65.     ren1->SetBackground( 0, 0, 0);  
  66.   
  67.     renWin->AddRenderer( ren1 );  
  68.     renWin->SetSize(800,800);  
  69.   
  70.     iren->SetInteractorStyle(istyle);    
  71.     iren->SetRenderWindow(renWin);  //交互  
  72.   
  73.     renWin->Render();  
  74.     iren->Start();  
  75. }   



附上结果图片


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭