VTK读取一个TXT文档中的三维点坐标显示三维点云

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

X坐标 Y坐标 Z坐标

如下所示:

VTK读取一个TXT文档中的三维点坐标显示三维点云_第1张图片

 

附上代码如下:

 

#include 
#include 
#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"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkInteractorStyleTrackball.h"
#include "vtkPolyDataMapper.h"
using namespace std;

void main(int argc, char* argv[])
{ 
	vtkPoints *m_Points = vtkPoints::New();
	vtkCellArray *vertices = vtkCellArray::New();	//_存放细胞顶点,用于渲染(显示点云所必须的)
	vtkPolyData *polyData = vtkPolyData::New();
	vtkPolyDataMapper *pointMapper = vtkPolyDataMapper::New();
	vtkActor *pointActor = vtkActor::New();
	vtkRenderer *ren1= vtkRenderer::New();
	vtkRenderWindow *renWin = vtkRenderWindow::New();
	vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
	vtkInteractorStyleTrackball *istyle = vtkInteractorStyleTrackball::New();

	//_读进点云数据信息
	FILE*fp = NULL; fp=fopen("point.txt","r");	//2DpointDatas.txt
	if(!fp)
	{
		
		printf("打开文件失败!!\n");
		int m;
		cin>>m;
		exit(0);
	}
	double x=0,y=0,z=0;
	int i = 0;
	while (!feof(fp))
	{
		fscanf(fp,"%lf %lf %lf",&x,&y,&z);	
		m_Points->InsertPoint(i,x,y,z);		//_加入点信息
		vertices->InsertNextCell(1);		//_加入细胞顶点信息----用于渲染点集
		vertices->InsertCellPoint(i);
		i ++;
	}	
	fclose(fp);

	//_创建待显示数据源

	polyData->SetPoints(m_Points);		//_设置点集
	polyData->SetVerts(vertices);		//_设置渲染顶点
	pointMapper->SetInput( polyData );

	pointActor->SetMapper( pointMapper );
	pointActor->GetProperty()->SetColor(0.0,0.1,1.0);
	pointActor->GetProperty()->SetAmbient(0.5);
	pointActor->GetProperty()->SetPointSize(2);
	//pointActor->GetProperty()->SetRepresentationToWireframe();
	//pointActor->GetProperty()->SetRepresentationToSurface();

	ren1->AddActor( pointActor );
	ren1->SetBackground( 0, 0, 0);

	renWin->AddRenderer( ren1 );
	renWin->SetSize(800,800);

	iren->SetInteractorStyle(istyle);  
	iren->SetRenderWindow(renWin);  //交互

	renWin->Render();
	iren->Start();

	//删除各指针
	m_Points->Delete();
	vertices->Delete();
	polyData->Delete();
	pointMapper->Delete();
	pointActor->Delete();
	ren1->Delete();
	renWin->Delete();
	iren->Delete();
	istyle->Delete();
} 

 

智能指针版本

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

void main(int argc, char* argv[])
{ 
	vtkSmartPointer m_Points = vtkSmartPointer::New();
	vtkSmartPointer vertices =vtkSmartPointer::New();	//_存放细胞顶点,用于渲染(显示点云所必须的)
	vtkSmartPointer polyData = vtkSmartPointer::New();
	vtkSmartPointer pointMapper = vtkSmartPointer::New();
	vtkSmartPointer pointActor = vtkSmartPointer::New();
	vtkSmartPointer ren1=vtkSmartPointer< vtkRenderer>::New();
	vtkSmartPointer renWin = vtkSmartPointer::New();
	vtkSmartPointer iren =vtkSmartPointer::New();
	vtkSmartPointer istyle = vtkSmartPointer::New();

	//_读进点云数据信息
	FILE*fp = NULL; fp=fopen("E:\\斯坦福兔子3000点.txt","r");	//2DpointDatas.txt
	if(!fp)
	{

		printf("打开文件失败!!\n");
		int m;
		cin>>m;
		exit(0);
	}
	double x=0,y=0,z=0;
	int i = 0;
	while (!feof(fp))
	{
		fscanf(fp,"%lf %lf %lf",&x,&y,&z);	
		m_Points->InsertPoint(i,x,y,z);		//_加入点信息
		vertices->InsertNextCell(1);		//_加入细胞顶点信息----用于渲染点集
		vertices->InsertCellPoint(i);
		i ++;
	}	
	fclose(fp);

	//_创建待显示数据源

	polyData->SetPoints(m_Points);		//_设置点集
	polyData->SetVerts(vertices);		//_设置渲染顶点
	pointMapper->SetInput( polyData );

	pointActor->SetMapper( pointMapper );
	pointActor->GetProperty()->SetColor(0.0,0.1,1.0);
	pointActor->GetProperty()->SetAmbient(0.5);
	pointActor->GetProperty()->SetPointSize(2);
	//pointActor->GetProperty()->SetRepresentationToWireframe();
	//pointActor->GetProperty()->SetRepresentationToSurface();

	ren1->AddActor( pointActor );
	ren1->SetBackground( 0, 0, 0);

	renWin->AddRenderer( ren1 );
	renWin->SetSize(800,800);

	iren->SetInteractorStyle(istyle);  
	iren->SetRenderWindow(renWin);  //交互

	renWin->Render();
	iren->Start();
} 

 

 

 

 

 

 

附上结果图片

VTK读取一个TXT文档中的三维点坐标显示三维点云_第2张图片

 

VTK读取一个TXT文档中的三维点坐标显示三维点云_第3张图片

VTK读取一个TXT文档中的三维点坐标显示三维点云_第4张图片

VTK读取一个TXT文档中的三维点坐标显示三维点云_第5张图片

如果您觉得这篇博文有用,请访问我的个人站:http://www.stubbornhuang.com,更多博文干货等着您。

你可能感兴趣的:(VTK,C++)