其他分享
首页 > 其他分享> > VTK: vtkPolyData 裁剪(1)

VTK: vtkPolyData 裁剪(1)

作者:互联网

目的:熟悉vtkClipClosedSurface 对vtkPolyData 进行裁剪;

 从左到右依次为:原始PolyData,VTK自带例子裁剪结果,修改后的裁剪结果。

对比代码:重点在于如何构建裁剪平面,需要注意的是裁剪平面的法向量指向的会被保留,其它会被裁剪掉。 

后续:根据用户在窗口的勾画区域对vtkPolyData 进行裁剪,难点就是如何构建重建平面组合。

    // VTK自带例子
    vtkNew<vtkPlane> plane1;
	plane1->SetOrigin(center[0], center[1], center[2]);
	plane1->SetNormal(0.0, -1.0, 0.0);
	vtkNew<vtkPlane> plane2;
	plane2->SetOrigin(center[0], center[1], center[2]);
	plane2->SetNormal(0.0, 0.0, 1.0);
	vtkNew<vtkPlane> plane3;
	plane3->SetOrigin(center[0], center[1], center[2]);
	plane3->SetNormal(-1.0, 0.0, 0.0);

	vtkNew<vtkPlaneCollection> planes;
	planes->AddItem(plane1);
	planes->AddItem(plane2);
	planes->AddItem(plane3);

    // 修改后裁剪
    double cutValue = 0.1;
	vtkNew<vtkPlane> plane1;
	plane1->SetOrigin(center[0]- cutValue, center[1], center[2]);
	plane1->SetNormal(1.0, 0.0, 0.0);
	vtkNew<vtkPlane> plane2;
	plane2->SetOrigin(center[0] + cutValue, center[1], center[2]);
	plane2->SetNormal(-1.0, 0.0, 0.0);
	vtkNew<vtkPlane> plane3;
	plane3->SetOrigin(center[0], center[1]- cutValue, center[2]);
	plane3->SetNormal(0.0, 1.0, 0.0);

	vtkNew<vtkPlane> plane4;
	plane4->SetOrigin(center[0], center[1] + cutValue, center[2]);
	plane4->SetNormal(0.0, -1.0, 0.0);

	vtkNew<vtkPlaneCollection> planes;
	planes->AddItem(plane1);
	planes->AddItem(plane2);
	planes->AddItem(plane3);
	planes->AddItem(plane4);

// VTK 自带例子的学习
// https://kitware.github.io/vtk-examples/site/Cxx/Meshes/ClipClosedSurface/

void ClipClosedSurface()
{
	vtkNew<vtkNamedColors> colors;

	// PolyData to process
	vtkSmartPointer<vtkPolyData> polyData;

	// Create a sphere
	vtkNew<vtkSphereSource> sphereSource;
	sphereSource->SetThetaResolution(20);
	sphereSource->SetPhiResolution(11);
	sphereSource->Update();

	polyData = sphereSource->GetOutput();

	double cutValue = 0.1;
	auto center = polyData->GetCenter();
	vtkNew<vtkPlane> plane1;
	plane1->SetOrigin(center[0]- cutValue, center[1], center[2]);
	plane1->SetNormal(1.0, 0.0, 0.0);
	vtkNew<vtkPlane> plane2;
	plane2->SetOrigin(center[0] + cutValue, center[1], center[2]);
	plane2->SetNormal(-1.0, 0.0, 0.0);
	vtkNew<vtkPlane> plane3;
	plane3->SetOrigin(center[0], center[1]- cutValue, center[2]);
	plane3->SetNormal(0.0, 1.0, 0.0);

	vtkNew<vtkPlane> plane4;
	plane4->SetOrigin(center[0], center[1] + cutValue, center[2]);
	plane4->SetNormal(0.0, -1.0, 0.0);

	vtkNew<vtkPlaneCollection> planes;
	planes->AddItem(plane1);
	planes->AddItem(plane2);
	planes->AddItem(plane3);
	planes->AddItem(plane4);

	vtkNew<vtkClipClosedSurface> clipper;
	clipper->SetInputData(polyData);
	clipper->SetClippingPlanes(planes);
	//clipper->SetActivePlaneId(2);
	//clipper->SetScalarModeToColors();
	//clipper->SetClipColor(colors->GetColor3d("Banana").GetData());
	//clipper->SetBaseColor(colors->GetColor3d("Tomato").GetData());
	//clipper->SetActivePlaneColor(colors->GetColor3d("SandyBrown").GetData());

	vtkNew<vtkDataSetMapper> clipMapper;
	clipMapper->SetInputConnection(clipper->GetOutputPort());

	vtkNew<vtkActor> clipActor;
	clipActor->SetMapper(clipMapper);
	clipActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
	clipActor->GetProperty()->SetInterpolationToFlat();

	vtkNew<vtkAxesActor> axes;

	// Create graphics stuff
	//
	vtkNew<vtkRenderer> ren1;
	ren1->SetBackground(colors->GetColor3d("SteelBlue").GetData());

	vtkNew<vtkRenderWindow> renWin;
	renWin->AddRenderer(ren1);
	renWin->SetSize(512, 512);
	renWin->SetWindowName("ClipClosedSurface");

	vtkNew<vtkRenderWindowInteractor> iren;
	iren->SetRenderWindow(renWin);

	// Add the actors to the renderer, set the background and size
	//
	ren1->AddActor(clipActor);
	ren1->AddActor(axes);
	// Generate an interesting view
	//
	ren1->ResetCamera();
	ren1->GetActiveCamera()->Azimuth(120);
	ren1->GetActiveCamera()->Elevation(30);
	ren1->GetActiveCamera()->Dolly(1.0);
	ren1->ResetCameraClippingRange();

	renWin->Render();
	iren->Initialize();
	iren->Start();

}

标签:center,0.0,VTK,裁剪,vtkPolyData,plane3,plane1,planes,vtkNew
来源: https://blog.csdn.net/yunzhaoqiang/article/details/122596454