VTK根据三维坐标点集生成点云

一个简单的利用VTK根据三维坐标点集生成点云的例子,仅供参考。

一、环境:vtk-8.1 & vs2013(需自行配置vtk的环境)

二、我所读取的三维坐标点集为txt格式文件,每个点的x,y,z坐标为单独一行。至于读取方法可以根据自己的点集数据结构重写读取方法,源码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

VTK_MODULE_INIT(vtkRenderingOpenGL2); //vs和cmake的编译选项不一致,非cmake生成的工程需添加,否则运行报错

int main()
{

	std::string fliename = "../bin/config/pointcloud.txt";
	std::ifstream ifs(fliename.c_str());
	std::string singleLine;
	double x, y, z,low_z = DBL_MAX,high_z = DBL_MIN;

	vtkSmartPointer points = vtkSmartPointer::New();
        //分行读取文件
	while (std::getline(ifs,singleLine))
	{
		std::stringstream linestream;
		linestream << singleLine;
		linestream >> x >> y >> z;
		if (z < low_z)
			low_z = z;
		else if (z > high_z)
			high_z = z;

		points->InsertNextPoint(x, y, z);
	}
	ifs.close();

	vtkSmartPointer polydata = vtkSmartPointer::New();
	polydata->SetPoints(points);

	vtkSmartPointer glyphFilter = vtkSmartPointer::New();
	glyphFilter->SetInputData(polydata);
	glyphFilter->Update();

	vtkElevationFilter *elevationFilter = vtkElevationFilter::New();
	elevationFilter->SetInputConnection(glyphFilter->GetOutputPort());
	elevationFilter->SetLowPoint(0, 0, low_z);
	elevationFilter->SetHighPoint(0, 0, high_z);

	vtkPolyDataMapper *dataMapper = vtkPolyDataMapper::New();
	dataMapper->SetInputConnection(elevationFilter->GetOutputPort());

	vtkActor *actor = vtkActor::New();
	actor->SetMapper(dataMapper);

	vtkRenderer *renderer = vtkRenderer::New();
	renderer->AddActor(actor);
	renderer->SetBackground(.0, .0, .0);//设置背景色
	
	vtkRenderWindow *renderwind = vtkRenderWindow::New();
	renderwind->AddRenderer(renderer);

	vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
	
	vtkRenderWindowInteractor *renderwindIt = vtkRenderWindowInteractor::New();
	renderwindIt->SetRenderWindow(renderwind);
	renderwindIt->SetInteractorStyle(style);

	renderwind->Render();
	renderwindIt->Start();


	return 0;
}

下面附一张读取的部分点云图:

VTK根据三维坐标点集生成点云_第1张图片

你可能感兴趣的:(VTK根据三维坐标点集生成点云)