[vtkusers] How to cut the vtkUnstructuredgrid dataset

Rockydut Rockydut at gmail.com
Sun Oct 31 02:12:13 EDT 2010


Dear colleagues:

        I use the VTK for finite element visualization. The
vtkUnstructrueGrid class is used to handle the data structure, which is
shown below. However, when I cut it using vtkCutter it draws a line along
the diagonal, i.e. only the surface of volume is drawn.  So, how to show or
cut the unstrcucturedgrid data which looks like a volume even though it be
cutted at any section.
 				
#include <vector>
using namespace std;
#define ELEM vector<int>

#include "vtkSmartPointer.h"
#define VTK_CREATE(type, name) vtkSmartPointer<type> name =
vtkSmartPointer<type>::New()
#define VTK_DELETE(p) if(p){p->Delete(); p=NULL;}

#include <algorithm>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkFloatArray.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDataSetMapper.h>
#include <vtkLODActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPointData.h>

int main(int argc, char* argv[])
{
	int nX=10, nY=10, nZ=10;
	int nElem = nX*nY*nZ;
	int nNode = (nX+1)*(nY+1)*(nZ+1);

	ELEM* pElem = new ELEM[nElem];
	if(!pElem)
		return 0;

	int NodeIndex[8];
	int index;
	//Create element
	for (int i=0; i <nZ; i++)
	{
		for (int j=0; j<nY; j++)
		{
			for (int k=0; k<nX; k++)
			{			
				index = i*nX*nY+j*nX+k;				
				NodeIndex[0]=i*(nX+1)*(nY+1)+j*(nX+1)+k;
				NodeIndex[1]=NodeIndex[0]+1;
				NodeIndex[2]=NodeIndex[0]+(nX+1)+1;
				NodeIndex[3]=NodeIndex[2]-1;
				NodeIndex[4]=NodeIndex[0]+(nX+1)*(nY+1);
				NodeIndex[5]=NodeIndex[4]+1;
				NodeIndex[6]=NodeIndex[5]+(nX+1);
				NodeIndex[7]=NodeIndex[6]-1;
				pElem[index].insert(pElem[index].begin(), NodeIndex, NodeIndex+8);
			}
		}
	}

	//generate coordinates
	float* pCoord = new float[nNode*3];
	if (!pCoord)
		return 0;
	float XDis = (float)(nX*1.0f)/nX;
	float YDis = (float)(nY*1.0f)/nY;
	float ZDis = (float)(nZ*1.0f)/nZ;
	for (int i=0; i<=nZ; i++)
	{
		for (int j=0; j<=nY; j++)
		{
			for (int k=0; k<=nX; k++)
			{
				index = i*(nX+1)*(nY+1)+j*(nX+1)+k;
				index *= 3;
				pCoord[index] = k*XDis;
				pCoord[index+1] = j*YDis;
				pCoord[index+2] = i*ZDis;
			}
		}
	}

	//generate color index
	float* pScalar = new float[nNode];
	if (!pScalar)
		return 0;
	for (int i=0; i<nNode; i++)
	{
		pScalar[i] = (float)i;
	}
		
	int nCell = nElem;
	int nVertex = nNode;
	VTK_CREATE(vtkPoints, points);
	VTK_CREATE(vtkCellArray, cell);
	VTK_CREATE(vtkFloatArray, scalar);

	float *max = max_element(pScalar, &pScalar[nNode-1]);
	float *min = min_element(pScalar, &pScalar[nNode-1]);
	float maxcolor = 1.0f, mincolor = 0.1f;
	points->SetNumberOfPoints(nVertex);
	scalar->SetNumberOfTuples(nVertex);
	for (int i=0; i<nVertex; i++)
	{
		points->InsertPoint(i, pCoord[i*3], pCoord[i*3+1], pCoord[i*3+2]);
		float val = mincolor+(pScalar[i]-*min)/(*max-*min)*(maxcolor-mincolor);
		scalar->InsertValue(i, val);
	}
	
	VTK_CREATE(vtkUnstructuredGrid, grid);	
	grid->Allocate(nCell, 1);
	int elem[8];
	int size = 8;
	for (int i=0; i<nCell; i++)
	{			
		for (int j=0; j<size; j++)
			elem[j] = pElem[i].at(j);
		grid->InsertNextCell(VTK_HEXAHEDRON, size, elem);
	}

	grid->SetPoints(points);
	grid->GetPointData()->SetScalars(scalar);

 	VTK_CREATE(vtkDataSetMapper, mapper);
 	mapper->SetInput(grid);

	VTK_CREATE(vtkLODActor, actor);
	actor->SetNumberOfCloudPoints(nVertex/10);	
	actor->SetMapper(mapper);

	VTK_CREATE(vtkRenderWindow, win);
	VTK_CREATE(vtkRenderer,	renderer);
	win->AddRenderer(renderer);

	VTK_CREATE(vtkRenderWindowInteractor, iren);
	iren->SetRenderWindow(win);

	VTK_CREATE(vtkInteractorStyleTrackballCamera, style);
	iren->SetInteractorStyle(style);
	iren->SetRenderWindow(win);		
	iren->Initialize();

	renderer->AddActor(actor);
	renderer->ResetCamera();

	win->SetSize(600,600);
	win->Render();
	iren->Start();

	delete []pElem;
	delete []pCoord;
	delete []pScalar;
	return 0;
}
-- 
View this message in context: http://vtk.1045678.n5.nabble.com/How-to-cut-the-vtkUnstructuredgrid-dataset-tp3243830p3243830.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list