[vtkusers] access polygons in Python

Forrest Sheng Bao forrest.bao at gmail.com
Sun Jul 1 00:52:58 EDT 2012


ok, I have finally figured out a way.

Suppose you have a VTK file called test.vtk containing the following data.

# vtk DataFile Version 2.0
Cube example
ASCII

DATASET POLYDATA
POINTS 8 float
0.0 0.0 0.0
1.0 0.0 0.0
1.0 1.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
1.0 0.0 1.0
1.0 1.0 1.0
0.0 1.0 1.0

POLYGONS 3 12
3 0 1 2
3 4 5 6
3 7 4 2


Now I use interactions on iPython to demonstrate the accessing to POLYGONS.

First, we prepare the accessing.

In [1]: import vtk

In [2]: Reader = vtk.vtkDataSetReader()

In [3]: Reader.SetFileName('test.vtk')

In [4]: Reader.Update()

In [5]: Data = Reader.GetOutput()

In [6]: CellArray = Data.GetPolys()

In [7]: Polygons = CellArray.GetData()


Now check the number of cells/polygons and number of points in
cells/polygons

In [8]: CellArray.GetNumberOfCells()
Out[8]: 3L

In [9]: Polygons.GetNumberOfTuples()
Out[9]: 12L


All cells/polygons can be accessed like this:

In [10]: for i in xrange(0,  Polygons.GetNumberOfTuples()):
   ....:         print Polygons.GetValue(i)
   ....:
3
0
1
2
3
4
5
6
3
7
4
2

Please note that the numbers (3's here) indicating sizes of cells (i.e.,
the numbers at the beginning of every line in Cell/Polygon segment in a VTK
file) are also retrieved and printed.

If all your cells/polygons are of the same size, e.g., all triangles, here
is an easy way.

In [11]: for i in xrange(0,  CellArray.GetNumberOfCells()):
   ....:         print [Polygons.GetValue(j) for j in xrange(i*3+1, i*3+4) ]
   ....:
[0L, 1L, 2L]
[3L, 4L, 5L]
[6L, 3L, 7L]

A full and maintained version is on my Blog at
http://forrestbao.blogspot.com/2012/06/vtk-polygons-and-other-cells-as.html

Cheers,
Forrest

On Sat, Jun 30, 2012 at 5:23 AM, Alex Southern <mrapsouthern at gmail.com>wrote:

>  What are the chances? with in 1 minute of me thinking about asking this,
> you actually ask it.
>
> I dont have the solution myself yet, but I was just looking at (in C++)
>
> vtkCellArray *polys;
> vtkSmartPointer<vtkPolyDataReader> pd =
> vtkSmartPointer<vtkPolyDataReader>::New();
>
> pd->SetFileName( "test.vtk" );
> polys = pd->GetOutput()->GetPolys();
>
> so I guess for python it is..
>
> Data = Reader.GetOutput().GetPolys()   ???? I dont know Python so well.
>
> If it works, or you find the solution please followup.
>
> Thanks
> A.
>
>
> On 6/30/2012 1:18 AM, Forrest Sheng Bao wrote:
>
> Hi,
>
>  I am struggling to access polygon faces in my VTK files using Python.
> Suppose I have the following segment in my
> file:
>
>  POLYGONS 275558 1102232
> 3 0 1 2
> 3 3 2 1
> 3 0 45 46
> 3 0 46 1
> 3 0 2 50
>
>  I used the piece of code below to prepare:
>
>      import vtk
>     Reader = vtk.vtkDataSetReader()
>     Reader.SetFileName('my_test_file.vtk')
>     Reader.ReadAllScalarsOn()  # Activate the reading of all scalars
>     Reader.Update()
>     Data = Reader.GetOutput()
>      CellData = Data.GetCell(0)
>
>
>  I assume CellData.GetPointId(x) is what I should use. But I got wrong
> point ID after x =3.
>
>  Can someone help me on this?
>
> Cheers,
> Forrest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120630/0d961389/attachment.htm>


More information about the vtkusers mailing list