(大鬼王的VTK日记)VTK显示点云并上色

#include "vtkInclude.h"

int main()
{
	//看文件有多少行
	int number = 1396;

	FILE* file;
	file = fopen("1.ply", "r");

	vtkSmartPointer points = vtkSmartPointer::New();
	vtkSmartPointer cells = vtkSmartPointer::New();
	vtkSmartPointer lookup = vtkSmartPointer::New();
	vtkSmartPointer polydata = vtkSmartPointer::New();
	vtkSmartPointer myscalar = vtkSmartPointer::New();
	lookup->SetNumberOfTableValues(number);
	lookup->Build();

	float x,y,z;
	int r,g,b;
	int i = 0;
	vtkIdType idtype;
	while (!feof(file))
	{
		fscanf(file, "%f %f %f %d %d %d", &x, &y, &z, &r, &g, &b);
		myscalar->InsertNextTuple1(1);
		idtype = points->InsertNextPoint(x, y, z);
		//为每个点计算颜色 r:0-255 g:0-255 b:0-255
		lookup->SetTableValue(i, r/255, g / 255, b/255, 1);
		cells->InsertNextCell(1, &idtype);
		i++;
	}
	polydata->SetPoints(points);
	polydata->SetVerts(cells);
	polydata->GetPointData()->SetScalars(myscalar);

	vtkSmartPointer mapper = vtkSmartPointer::New();
	mapper->SetInputData(polydata);
	mapper->SetLookupTable(lookup);

	vtkSmartPointer actor = vtkSmartPointer::New();
	actor->SetMapper(mapper);
	actor->GetProperty()->SetPointSize(1);

	vtkSmartPointer render = vtkSmartPointer::New();
	render->AddActor(actor);

	vtkRenderWindow *renderWindow = vtkRenderWindow::New();
	renderWindow->AddRenderer(render);
	render->SetBackground(1, 1, 1);

	vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
	iren->SetRenderWindow(renderWindow);

	vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
	iren->SetInteractorStyle(style);

	iren->Initialize();
	iren->Start();
	return 0;
}

(大鬼王的VTK日记)VTK显示点云并上色_第1张图片

include里面内容自己挑着加就好。

你可能感兴趣的:((大鬼王的VTK日记)VTK显示点云并上色)