利用VTK构造球形(表面)点云

来源:互联网 发布:java 扫描二维码登陆 编辑:程序博客网 时间:2024/05/16 07:32

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;}// VisualizevtkSmartPointer<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 greenrenderWindow->Render();renderWindowInteractor->Start();style->Delete();return EXIT_SUCCESS;}



效果图:


0 0
原创粉丝点击