利用VTK構造球形(表面)點雲

vtkPointSource用於在球體內產生指定數量的點,用戶可以指定球的半徑和球心位置。默認情況下,產生的點隨機分佈於球內,也可以產生隨機分佈於球表面的點雲。

#include <vtkPointSource.h>
#include <vtkExtractSelection.h>
#include <vtkPolyData.h>
#include <vtkSelectionNode.h> // for POINT and INDICES enum values
#include <vtkSelectionSource.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkSimplePointsReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include "vtkInteractorStyleTrackballCamera.h"


int main(int, char *[])
{
	// Note - this generates 50 points and a single poly-vertex cell.
	vtkSmartPointer<vtkPointSource> pointSource = 
		vtkSmartPointer<vtkPointSource>::New();
	pointSource->SetNumberOfPoints(1000);//設置點的個數
	pointSource->SetCenter(0,0,0);//設置球心
	pointSource->SetRadius(15.0); //設置半徑
	pointSource->SetDistributionToShell();//該模式爲隨機產生的點在球的表面,SetDistributionToUniform()這種模式隨機產生的點在球的內部
	pointSource->Update();

	//輸出PolyData的座標
	vtkPolyData* polydata = pointSource->GetOutput();
	// Write all of the coordinates of the points in the vtkPolyData to the console.
	for(vtkIdType i = 0; i < polydata->GetNumberOfPoints(); i++)
	{
		double p[3];
		polydata->GetPoint(i,p);
		// This is identical to:
		// polydata->GetPoints()->GetPoint(i,p);
		std::cout << "Point " << i << " : (" << p[0] << " " << p[1] << " " << p[2] << ")" << std::endl;
	}


	// Visualize
	vtkSmartPointer<vtkPolyDataMapper> mapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputConnection(pointSource->GetOutputPort());

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

	vtkSmartPointer<vtkRenderer> renderer =
		vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renderWindow =
		vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

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

	renderer->AddActor(actor);
	renderer->SetBackground(.3, .6, .3); // Background color green

	renderWindow->Render();
	renderWindowInteractor->Start();

	style->Delete();

	

	return EXIT_SUCCESS;
}



效果圖:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章