将两个 vtkpolydata 数据合并

因为一般 VTK 自己的形状都是 polydata的,我们有时候需要将两个或者多个数据进行合并显示。

直接说,使用的类是 vtkAppendPolyData,输入多个数据,直接 update() 就可以得到想要的结果。之后可以加一个 vtkCleanPolyData 的对象进行基本的数据处理 - 去掉重复的点。

示例 创建了一个 圆锥和圆柱(圆管)。

	//Create a cone
	vtkSmartPointer<vtkConeSource> coneSource =
		vtkSmartPointer<vtkConeSource>::New();
	coneSource->SetCenter(0, 0, 0);
	coneSource->SetRadius(2);
	coneSource->SetHeight(10);
	coneSource->SetResolution(50);
	coneSource->Update();	
	
	vtkIntArray *scalars = vtkIntArray::New();
	vtkPointData *conePointDataPointer = coneSource->GetOutput()->GetPointData();
	vtkPolyData *conePolyDataPointer = coneSource->GetOutput();
	scalars->SetNumberOfTuples(conePolyDataPointer->GetNumberOfPoints());
	scalars->SetNumberOfComponents(1);
	for (int i = 0; i < conePolyDataPointer->GetNumberOfPoints(); i++)
	{
		scalars->SetTuple1(i, 1);
	}
	conePointDataPointer->SetScalars(scalars);
	// second 
	vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New();
	line->SetPoint1(1.0, 0, 0);
	line->SetPoint2(0, 1.0, 20);

	vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
	tube->SetInputConnection(line->GetOutputPort());
	tube->SetRadius(2);
	tube->SetNumberOfSides(50);
	tube->SetCapping(1);
	tube->Update();
	vtkIntArray *scalarstube = vtkIntArray::New();
	vtkPointData *tubePointDataPointer = tube->GetOutput()->GetPointData();
	vtkPolyData *tubePolyDataPointer = tube->GetOutput();
	scalarstube->SetNumberOfTuples(tubePolyDataPointer->GetNumberOfPoints());
	scalarstube->SetNumberOfComponents(1);
	for (int i = 0; i < tubePolyDataPointer->GetNumberOfPoints(); i++)
	{
		scalarstube->SetTuple1(i, 0);
	}
	tubePointDataPointer->SetScalars(scalarstube);

	// combine two poly data
	vtkSmartPointer<vtkAppendPolyData> appendFilter =
		vtkSmartPointer<vtkAppendPolyData>::New();
	appendFilter->AddInputData(tube->GetOutput());
	appendFilter->AddInputData(coneSource->GetOutput());
	appendFilter->Update();
	// Remove any duplicate points.
	vtkSmartPointer<vtkCleanPolyData> cleanFilter =
		vtkSmartPointer<vtkCleanPolyData>::New();
	cleanFilter->SetInputConnection(appendFilter->GetOutputPort());
	cleanFilter->Update();
	//Create a mapper and actor
	vtkSmartPointer<vtkPolyDataMapper> appendmapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
	appendmapper->SetInputConnection(cleanFilter->GetOutputPort());

	vtkSmartPointer<vtkActor> appendactor =
		vtkSmartPointer<vtkActor>::New();
	appendactor->SetMapper(appendmapper);	



	//Create a renderer, render window, and interactor
	vtkSmartPointer<vtkRenderer> renderer =
		vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renderWindow =
		vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

	//Add the actors to the scene
	renderer->AddActor(appendactor);	
	renderer->SetBackground(.3, .2, .1); // Background color dark red

	//Render and interact
	renderWindow->Render();
	renderWindowInteractor->Start();

结果

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值