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;
}
效果图: