VTK读取点数据构建点云

 

#include <vtkVertexGlyphFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <sstream>

#include <vtkAutoInit.h> 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);

int main(int, char* [])
{
	std::string filename = "C:\\Users\\wangjun\\Downloads\\point_data.txt";
	std::ifstream filestream(filename.c_str());

	std::string line;
	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
	while (std::getline(filestream, line))
	{
		double x, y, z;
		std::stringstream linestream;
		linestream << line;
		linestream >> x >> y >> z;

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

	filestream.close();

	vtkSmartPointer<vtkPolyData> polyData =
		vtkSmartPointer<vtkPolyData>::New();

	polyData->SetPoints(points);

	vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
	glyphFilter->SetInputData(polyData);
	glyphFilter->Update();

	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputConnection(glyphFilter->GetOutputPort());

	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);

	vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
	renderer->AddActor(actor);
	renderer->SetBackground(.3, .6, .3);

	vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
	renWin->AddRenderer(renderer);

	vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	iren->SetRenderWindow(renWin);
	vtkSmartPointer< vtkInteractorStyleTrackballCamera> style = vtkSmartPointer< vtkInteractorStyleTrackballCamera>::New();
	iren->SetInteractorStyle(style);

	renWin->SetSize(600, 600);
	renWin->Render();
	iren->Start();

	return 0;
}