[vtkusers] Python-VTK : Defining the cells of a vtkUnstructuredGrid

Aurélien Marsan aur.marsan at gmail.com
Wed Jul 30 17:29:51 EDT 2014


Ok, found it. It was not a big issue... ! Just had to take a look on the
help.

I had to switch to lines in the SetCells method, according to the
description in the help.

bloc.SetCells(
>         numpy_support.numpy_to_vtk(cellslocations, deep = 1, array_type =
> vtk.vtkUnsignedCharArray().GetDataType()),
>         numpy_support.numpy_to_vtk(cellstypes, deep = 1, array_type =
> vtk.vtkIdTypeArray().GetDataType()),
>         vtkCells
>         )


becomes :

bloc.SetCells(
>         numpy_support.numpy_to_vtk(cellstypes, deep = 1, array_type =
> vtk.vtkUnsignedCharArray().GetDataType()),
>         numpy_support.numpy_to_vtk(cellslocations, deep = 1, array_type =
> vtk.vtkIdTypeArray().GetDataType()),
>         vtkCells
>         )
>

(note the switch between cellstypes and cellslocations in the method)

All is working well now.
Sorry for the email. (but an example of defining an UnstructuredGrid from
numpy arrays is now online)

Regards,

Aurélien


2014-07-30 17:07 GMT-04:00 Aurélien Marsan <aur.marsan at gmail.com>:

> Hi all,
>
> I am still facing some issues in order to build a vtkUnstructuredGrid from
> numpy arrays.
>
> I have read the example, that shows how to build a vtkUnstructuredGrid
> cell by cell :
>
> http://vtk.org/gitweb?p=VTK.git;a=blob;f=Examples/DataManipulation/Python/pointToCellData.py
>
> But in my case, I have large numpy arrays which contains
> - the points coordinates
> - the cell types
> - the definition of cells (npts,p0,p1,...p(npts-1)
>
> and I would like to avoid any loops in the python code.
>
> Then, I tried the following code, where
>
>    - <cells> contains the definition of the cells (npts,p0,p1,...p(npts-1)
>    - <cellslocations> contains the index of the cells in the <cells> array
>    - <cellstype> is an array of integer that describes the types of the
>    cells
>
>     bloc = vtk.vtkUnstructuredGrid()
>>
>>
>>     vtkArray =
>> numpy_support.numpy_to_vtk(numpy.ascontiguousarray(coords), deep = 1)
>>     points = vtk.vtkPoints()
>>     points.SetData(vtkArray)
>>
>>     bloc.SetPoints(points)
>>
>>     vtkCells = vtk.vtkCellArray()
>>     vtkCells.SetCells(number_of_cells, numpy_support.numpy_to_vtk(cells,
>> deep = 1, array_type = vtk.vtkIdTypeArray().GetDataType()))
>
>     bloc.SetCells(
>>         numpy_support.numpy_to_vtk(cellslocations, deep = 1, array_type =
>> vtk.vtkUnsignedCharArray().GetDataType()),
>>         numpy_support.numpy_to_vtk(cellstypes, deep = 1, array_type =
>> vtk.vtkIdTypeArray().GetDataType()),
>>         vtkCells
>>         )
>>     bloc.Update()
>>     bloc.UpdateData()
>>     print bloc
>>     return bloc
>>
>
> This code works. But when I am trying to cut the resulting block using a
> VTKCutter, I have the following error message:
>
>> Generic Warning: In
>> /home/builder/pisi/tmp/VTK-5.6.0-2/work/VTK/Graphics/vtkContourGrid.cxx,
>> line 195
>> Unknown cell type 171
>>
>
> It is obvious that I do something wrong.... But I can not find where.
> Do you have some advice ?
> is it really possible to define an unstructured grid like this, avoiding
> any python loop ?
>
> Many thanks in advance,
>
> Regards,
>
> Aurélien
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20140730/7ea13c9c/attachment.html>


More information about the vtkusers mailing list