[vtkusers] How to cut the vtkUnstructuredgrid dataset

Rockydut Rockydut at gmail.com
Mon Nov 1 23:11:36 EDT 2010


Mybe I know how to deal with this problem. It should add the
aBeamMapper->AddClippingPlane(plane) to show the the result like a volume.


#include <vtkpoints.h>
#include <vtkHexahedron.h>
#include <vtkCellArray.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDataSetSurfaceFilter.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkPlane.h>
#include <vtkCutter.h>
#include <vtkDataSetMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include "vtkSmartPointer.h"
#define VTK_CREATE(type, name) vtkSmartPointer<type> name =
vtkSmartPointer<type>::New()

int main(int argc, char* argv[])
{
	float P0[] = {0.0, 0.0, 0.0};
	float P1[] = {1.0, 0.0, 0.0};
	float P2[] = {1.0, 1.0, 0.0};
	float P3[] = {0.0, 1.0, 0.0};
	float P4[] = {0.0, 0.0, 1.0};
	float P5[] = {1.0, 0.0, 1.0};
	float P6[] = {1.0, 1.0, 1.0};
	float P7[] = {0.0, 1.0, 1.0};

	//Create the points
	VTK_CREATE(vtkPoints, points);
	points->InsertNextPoint(P0);
	points->InsertNextPoint(P1);
	points->InsertNextPoint(P2);
	points->InsertNextPoint(P3);
	points->InsertNextPoint(P4);
	points->InsertNextPoint(P5);
	points->InsertNextPoint(P6);
	points->InsertNextPoint(P7);

	//Create a hexahedron from the points
	VTK_CREATE(vtkHexahedron, hexa);
	hexa->GetPointIds()->SetId(0,0);
	hexa->GetPointIds()->SetId(1,1);
	hexa->GetPointIds()->SetId(2,2);
	hexa->GetPointIds()->SetId(3,3);
	hexa->GetPointIds()->SetId(4,4);
	hexa->GetPointIds()->SetId(5,5);
	hexa->GetPointIds()->SetId(6,6);
	hexa->GetPointIds()->SetId(7,7);

	//Add the hexahedron to a cell array
	VTK_CREATE(vtkCellArray, hexs);
	hexs->InsertNextCell(hexa);

	//Add the points and hexahedron to an unstructured grid
	VTK_CREATE(vtkUnstructuredGrid, uGrid);
	uGrid->SetPoints(points);
	uGrid->InsertNextCell(hexa->GetCellType(), hexa->GetPointIds());
// 
// 	VTK_CREATE(vtkDataSetSurfaceFilter, surface);
// 	surface->SetInput(uGrid);
// 	surface->Update();

	VTK_CREATE(vtkDataSetMapper, aBeamMapper);
//	aBeamMapper->SetInput(surface->GetOutput());
	aBeamMapper->SetInput(uGrid);
	

	VTK_CREATE(vtkActor, aBeamActor);
	aBeamActor->SetMapper(aBeamMapper);
	aBeamActor->AddPosition(0,0,0);
	aBeamActor->GetProperty()->SetColor(1,1,0);
//	aBeamActor->GetProperty()->SetOpacity(0.60);
	aBeamActor->GetProperty()->EdgeVisibilityOn();
	aBeamActor->GetProperty()->SetEdgeColor(1,1,1);
	aBeamActor->GetProperty()->SetLineWidth(1.5);

	//create a plane to cut,here it cuts in the XZ direction (xz
normal=(1,0,0);XY =(0,0,1),YZ =(0,1,0)
	VTK_CREATE(vtkPlane, plane);
	plane->SetOrigin(0.1,0,0);
	plane->SetNormal(1,0,0);

	aBeamMapper->AddClippingPlane(plane);

	//create cutter
	VTK_CREATE(vtkCutter, cutter);
	cutter->SetCutFunction(plane);
//	cutter->SetInput(aBeamActor->GetMapper()->GetInput());
	cutter->SetInput(uGrid);
	cutter->Update();
	VTK_CREATE(vtkDataSetMapper, cutterMapper);
	cutterMapper->SetInputConnection(cutter->GetOutputPort());

	//create plane actor
	VTK_CREATE(vtkActor, planeActor);
	planeActor->GetProperty()->SetColor(1,0.5,0.5);
	planeActor->GetProperty()->SetLineWidth(2);
	planeActor->SetMapper(cutterMapper);

	// Setup a renderer, render window, and interactor
	VTK_CREATE(vtkRenderer, renderer);
	VTK_CREATE(vtkRenderWindow, renderWindow);
	//renderWindow->SetWindowName("Test");

	renderWindow->AddRenderer(renderer);
	VTK_CREATE(vtkRenderWindowInteractor,renderWindowInteractor);
	renderWindowInteractor->SetRenderWindow(renderWindow);

	//Add the actor to the scene
	renderer->AddActor(aBeamActor);
	renderer->AddActor(planeActor);
	renderer->SetBackground(0,0,0);// Background color white
	
	//Render and interact
	renderWindow->Render();
	renderWindowInteractor->Start();
	
	return EXIT_SUCCESS;
}
-- 
View this message in context: http://vtk.1045678.n5.nabble.com/How-to-cut-the-vtkUnstructuredgrid-dataset-tp3243830p3246126.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list