[vtkusers] Unstructuredgrid creation in vtk with C++.

SAÏD ELKASMI elkasmis at hotmail.com
Thu Dec 19 02:59:16 EST 2002


Hi vtkusers,
I'm using the following C++ method to read from a file an unstructured grid. 
The file contains a list of vertices and a connectivity table to define 
tetras.

The file looks like ( for nx=2, ny=2, nz=2):

            8           6
   0.0000000000000000E+000  0.0000000000000000E+000  0.0000000000000000E+000
    1.000000000000000       0.0000000000000000E+000  0.0000000000000000E+000
   0.0000000000000000E+000   1.000000000000000       0.0000000000000000E+000
    1.000000000000000        1.000000000000000       0.0000000000000000E+000
   0.0000000000000000E+000  0.0000000000000000E+000   1.000000000000000
    1.000000000000000       0.0000000000000000E+000   1.000000000000000
   0.0000000000000000E+000   1.000000000000000        1.000000000000000
    1.000000000000000        1.000000000000000        1.000000000000000
            2           4           3           6
            2           3           5           6
            1           2           3           5
            7           4           6           3
            7           3           6           5
            8           4           6           7



nvtx is the number of vertices and nelt is the tetras one.

void ReadUGridMesh::readUGridMesh(const char * path){
		int nvtx, nelt, nfaces;
		int engine = 0;
		float xyz[3];
		int tID[4];
		FILE *file;
		if((file = fopen(path,"r")) == NULL){
				cout << "Can't read file. \n";
		}
		fscanf(file,"%d %d %d", &nvtx, &nelt, &nfaces);
		printf("%d %d %d",nvtx,nelt,nfaces);
		printf("\n");

		ugPoints->SetNumberOfPoints(nvtx);
		while(engine < nvtx){
				fscanf(file,"%E  %E  %E",&xyz[0], &xyz[1], &xyz[2]);
				ugPoints->InsertPoint(engine,xyz);
				printf("%E %E  %E ",xyz[0], xyz[1],xyz[2]);
				printf("\n");
				engine ++;
		}

		vtkIdList  *ID = vtkIdList::New();
		vtkCellArray *tetras = vtkCellArray::New();
		int types[nelt];
		ID->SetNumberOfIds(4);

		engine = 0;
		while(engine < nelt){
				fscanf(file,"%d %d %d %d",&tID[0],&tID[1],&tID[2],&tID[3]);

				for(int incr = 0; incr < 4; incr++){
						ID->SetId(incr,tID[incr]);
				}

				// create tetras.
				vtkTetra *tetra = vtkTetra::New();
				tetra->GetPointIds()->SetNumberOfIds(4);
				for(int r = 0; r <4 ; r++){
						tetra->GetPointIds()->SetId(r,ID->GetId(r));
				}
				tetras->InsertNextCell(1);
				tetras->InsertNextCell(tetra);
				printf("%d \n",tetras->GetSize());
				//types[engine] = VTK_TETRA;
				// END create tetras.

				//printf("%d %d %d %d",tID[0],tID[1],tID[2],tID[3]);
				//printf("\n");

				engine++;
		}

		printf("%d",tetras->GetSize());
		char xx;
		cout << "\n";
		cin >> xx;
		ug->SetPoints(ugPoints);
		ugPoints->Delete();
		ug->SetCells(types,tetras);
		tetras->Delete();
}

After this method is run, I call the following one to render the grid but it 
doesn't work.

void ReadUGridMesh::renderUGMesh(){
		ugmp->SetInput(ug);
		ugmp->ImmediateModeRenderingOn();
		ugactor->SetMapper(ugmp);
		ugactor->GetProperty()->SetColor(1,0,1);
		ugactor->AddPosition(0,0.001,0);

		renderer->AddActor(ugactor);

		renWin->AddRenderer(renderer);
		renWin->SetSize(900,600);
		renWinInt->SetRenderWindow(renWin);
		renderer->SetBackground(.6,.6,.6);
		renWin->Render();
		renWinInt->Initialize();
		renWinInt->Start();
}

Can anybody help me to do that ?
Buena journatta !


_________________________________________________________________
MSN Messenger : discutez en direct avec vos amis ! 
http://www.msn.fr/msger/default.asp




More information about the vtkusers mailing list