[vtkusers] Problems building UnstructuredGrid, desperately needing help!

Bruno da Silva de Oliveira bruno at esss.com.br
Tue Oct 25 13:21:50 EDT 2005


Hi Lisa,


Well, a cell is made of point ids, that reference geometry, which 
finally define the complete shape of the cell.

Suppose we have this point list:

0 - 0, 0, 0
1 - 1, 0, 0
2 - 0, 1, 0
3 - 0, 0, 1
4 - 2, 0, 0
5 - 1, 2, 0
...

and so on. Suppose the first cell (a tetra) is made of points 2, 3, 4 
and 1. To generate this cell you would do:

ids = vtk.vtkIdList()
ids.InsertNextId(2)
ids.InsertNextId(3)
ids.InsertNextId(4)
ids.InsertNextId(1)

grid.InsertNextCell(VTK_TETRA, ids)

Hope the example made things a little clear.

So, I believe your grid.connectivity list is actually an array of cell 
-> ids of the points that form that cell, right? So to build all your cells:

for ids in grid.connectivity: # an array of point ids for each cell
   id_list = vtk.vtkIdList()
   for id in ids:
       id_list.InsertNextId(id)   
   if len(ids) == 4: # a tetra
       cell_type = VTK_TETRA
   elif ... # other types of cells
   ...
  
   grid.InsertNextCell(cell_type, id_list)


Setting up your point coordinates is pretty straighforward.

HTH,
         

Lise Angell wrote:

>Hi Bruno, thanks for your reply!
>
>Concerning the insertion of the ids, I'm not sure I have understood
>what the InsertNextId is doing...
>
>What I want to accomplish is this:
>- insert cells into the grid so that my grid consists of several cells
>of the same type (most often tetrahedra).
>
>All the examples I have found on the net build a grid of only one cell...
>
>My point coordinates lies in grid.coords (nnodes x 3 array) and the
>connectivity info is in grid.connectivity (ncells x nnodes array)
>How can I build the grid from this?
>
>Lise
>
>On 10/25/05, Bruno da Silva de Oliveira <bruno at esss.com.br> wrote:
>  
>
>>Hi Lise,
>>
>> From the code you provided I can't be certain where the problem is. I
>>can tell you thought that I have used InsertNextCell(cell_type, id_list)
>>and it works fine.
>>
>>Hmm, looking at the code below, I see that you're inserting the ids of
>>the points hard-coded to 1-4... this is mostly certainly wrong. The id
>>must be the same id from the vtkPoints that you read the points
>>coordinates into... also, don't forget to set them in the
>>vtkUnstructuredGrid with SetPoints.
>>
>>HTH,
>>
>>Lise Angell wrote:
>>
>>    
>>
>>>Hi everybody!
>>>
>>>I'm trying to translate C++ code to Python, but I'm having some
>>>trouble so I would really appreciate some help (I'm not that
>>>experienced with VTK).
>>>
>>>My program does the following:
>>>- reads point coordinates and connectivity from file (not a vtk formatted file)
>>>- builds a vtkUnstructuredGrid and visualizes it.
>>>
>>>The problem arises in the building of the unstructuredGrid...
>>>
>>>The method
>>>vtkUnstructuredGrid::InsertNextCell(int type, vtkIdType npts, vtkIdType *pts)
>>>seems to be unreachable from Python (although it works fine in C++,
>>>for code sample, see below)...
>>>When I try to run it I get
>>>
>>>Traceback (most recent call last):
>>> File "readBinarySimres.py", line 1092, in ?
>>>   u_field = mount_vtk_structures(fields)
>>> File "readBinarySimres.py", line 828, in mount_vtk_structures
>>>   vtk_field.InsertNextCell(cell_type, npts, pt_list)
>>>TypeError: function takes exactly 2 arguments (3 given)
>>>
>>>
>>>
>>>So I tried to use the other method instead,
>>>InsertNextCell (int type, vtkIdList *ptIds)
>>>
>>>for el in range (0,nel):
>>> id_list = vtk.vtkIdList()
>>> (cell_type, npts, id_list) = convertElm(grid, el, id_list)  #define
>>>the connectivity in the cell
>>> vtk_field.InsertNextCell(cell_type, id_list)
>>>
>>>
>>>def convertElm(grid, e, ids):
>>> vtk_cell_type=0; npt=0
>>> nsd = grid.nsd; nne = grid.nne
>>>
>>> # check element type
>>> elm_type = grid.elm_type
>>>
>>> if elm_type == 'ElmB4n2D':
>>>   vtk_cell_type = VTK_QUAD
>>>   #print 'element type ', vtk_cell_type
>>>   npt = 4
>>>   ids.InsertNextId(1)
>>>   ids.InsertNextId(2)
>>>   ids.InsertNextId(4)
>>>   ids.InsertNextId(3)
>>> [...]
>>> return (vtk_cell_type, npt, ids)
>>>
>>>
>>>
>>>Now I get a Bus error while visualizing (with GeometryFilter,
>>>WarpScalar, PolyDataNormals and PolyDataMapper)
>>>
>>>Any ideas what is wrong or how I can work around this?
>>>
>>>Best regards,
>>>Lise
>>>
>>>(Running Mac OSX Tiger, python2.3 and VTK from cvs 11.oct 2005)
>>>
>>>###--------------------------------------------------------------------------
>>>###--------------------------------------------------------------------------
>>>
>>>C++ code that works:
>>>
>>>  // Grab the grid and related parameters
>>> const GridFE& grid = scalar_field.grid();
>>> const int      nsd = grid.getNoSpaceDim();
>>> const int      nno = grid.getNoNodes();
>>> const int      nel = grid.getNoElms();
>>>
>>> // Allocate Vtk data structures for the coordinates and data fields.
>>> vtkPoints* pts = vtkPoints::New();
>>> pts->SetNumberOfPoints (nno);
>>>
>>> vtkDoubleArray* data = vtkDoubleArray::New();
>>> data->SetNumberOfValues (nno);
>>> //------------------------------------------------------------------------
>>> // First, tell vtk structures about the "geometry" (nodal coords & values)
>>> //------------------------------------------------------------------------
>>> for (int i = 0; i < nno; i++) {                // run through all nodes
>>>   s = scalar_field.valueNode(i+1);
>>>
>>>   x[0]=grid.getCoor (i+1,1);  // x coordinate of node i+1
>>>   x[1] = x[2] = scale*s;      // Assumes 1D,  scaled field values as 2nd/3rd
>>>   if (nsd > 1)
>>>     x[1]=grid.getCoor (i+1,2);  // y coordinate of node i+1
>>>   if (nsd  > 2)
>>>     x[2]=grid.getCoor (i+1,3);  // z coordinate of node i+1
>>>
>>>
>>>   data->SetValue(i,s);      // put field value in vtk object
>>>   pts->SetPoint(i,x);               // put points in vtk object
>>> }
>>> vtk_u_grid = vtkUnstructuredGrid::New();
>>>
>>> vtk_u_grid->Allocate (nel,0);                   // set no of elements
>>> vtk_u_grid->SetPoints (pts);                    // put points in grid
>>> vtk_u_grid->GetPointData()->SetScalars(data);  // put field values in grid
>>>
>>> //---------------------------------------------------------
>>> // Next, tell vtk grid structure about the element topology
>>> //---------------------------------------------------------
>>>
>>> // Maximum 8 nodes in the allowable Vtk cell types.
>>> int vtk_cell_type = 0;
>>> int npt = 0, pt_list[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
>>>
>>> for (int e = 1; e <= nel; ++e) {
>>>   convertElm(grid, e, vtk_cell_type, npt, pt_list);
>>>   vtk_u_grid->InsertNextCell (vtk_cell_type, npt, pt_list);
>>> }
>>>
>>>
>>>
>>>###--------------------------------------------------------------------------
>>>###--------------------------------------------------------------------------
>>>
>>>
>>>
>>>--
>>>"Je forme une entreprise qui n'eut jamais d'exemple,
>>>et dont l'exécution n'aura point d'imitateur."  J-J Rousseau
>>>_______________________________________________
>>>This is the private VTK discussion list.
>>>Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>>Follow this link to subscribe/unsubscribe:
>>>http://www.vtk.org/mailman/listinfo/vtkusers
>>>
>>>
>>>
>>>      
>>>
>>--
>>Bruno da Silva de Oliveira
>>bruno at esss.com.br
>>ESSS - Engineering Simulation and Scientific Software
>>http://www.esss.com.br
>>
>>_______________________________________________
>>This is the private VTK discussion list.
>>Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>Follow this link to subscribe/unsubscribe:
>>http://www.vtk.org/mailman/listinfo/vtkusers
>>
>>    
>>
>
>
>--
>"Je forme une entreprise qui n'eut jamais d'exemple,
>et dont l'exécution n'aura point d'imitateur."  J-J Rousseau
>
>  
>

-- 
Bruno da Silva de Oliveira
bruno at esss.com.br
ESSS - Engineering Simulation and Scientific Software
http://www.esss.com.br




More information about the vtkusers mailing list