ok, I have finally figured out a way. <br><br>Suppose you have a VTK file called test.vtk containing the following data.<br><br># vtk DataFile Version 2.0<br>Cube example<br>ASCII<br><br>DATASET POLYDATA<br>POINTS 8 float<br>
0.0 0.0 0.0<br>1.0 0.0 0.0<br>1.0 1.0 0.0<br>0.0 1.0 0.0<br>0.0 0.0 1.0<br>1.0 0.0 1.0<br>1.0 1.0 1.0<br>0.0 1.0 1.0<br><br>POLYGONS 3 12<br>3 0 1 2 <br>3 4 5 6<br>3 7 4 2<br><br><br>Now I use interactions on iPython to demonstrate the accessing to POLYGONS.<br>
<br>First, we prepare the accessing.<br><br>In [1]: import vtk<br><br>In [2]: Reader = vtk.vtkDataSetReader()<br><br>In [3]: Reader.SetFileName('test.vtk')<br><br>In [4]: Reader.Update()<br><br>In [5]: Data = Reader.GetOutput()<br>
<br>In [6]: CellArray = Data.GetPolys()<br><br>In [7]: Polygons = CellArray.GetData()<br><br><br>Now check the number of cells/polygons and number of points in cells/polygons<br><br>In [8]: CellArray.GetNumberOfCells()<br>
Out[8]: 3L<br><br>In [9]: Polygons.GetNumberOfTuples()<br>Out[9]: 12L<br><br><br>All cells/polygons can be accessed like this:<br><br>In [10]: for i in xrange(0, Polygons.GetNumberOfTuples()):<br> ....: print Polygons.GetValue(i)<br>
....: <br>3<br>0<br>1<br>2<br>3<br>4<br>5<br>6<br>3<br>7<br>4<br>2<br><br>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.<br>
<br>If all your cells/polygons are of the same size, e.g., all triangles, here is an easy way.<br><br>In [11]: for i in xrange(0, CellArray.GetNumberOfCells()):<br> ....: print [Polygons.GetValue(j) for j in xrange(i*3+1, i*3+4) ]<br>
....: <br>[0L, 1L, 2L]<br>[3L, 4L, 5L]<br>[6L, 3L, 7L]<br><br>A full and maintained version is on my Blog at <a href="http://forrestbao.blogspot.com/2012/06/vtk-polygons-and-other-cells-as.html">http://forrestbao.blogspot.com/2012/06/vtk-polygons-and-other-cells-as.html</a><br>
<br clear="all">Cheers, <br>Forrest<br><br><div class="gmail_quote">On Sat, Jun 30, 2012 at 5:23 AM, Alex Southern <span dir="ltr"><<a href="mailto:mrapsouthern@gmail.com" target="_blank">mrapsouthern@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>What are the chances? with in 1 minute
of me thinking about asking this, you actually ask it.<br>
<br>
I dont have the solution myself yet, but I was just looking at (in
C++)<br>
<br>
vtkCellArray *polys;<br>
vtkSmartPointer<vtkPolyDataReader> pd =
vtkSmartPointer<vtkPolyDataReader>::New();<br>
<br>
pd->SetFileName( "test.vtk" );<br>
polys = pd->GetOutput()->GetPolys();<br>
<br>
so I guess for python it is..<br>
<br>
Data = Reader.GetOutput().GetPolys() ???? I dont know Python so
well.<br>
<br>
If it works, or you find the solution please followup.<br>
<br>
Thanks<br>
A.<div><div><br>
<br>
On 6/30/2012 1:18 AM, Forrest Sheng Bao wrote:<br>
</div></div></div>
<blockquote type="cite"><div><div>Hi,
<div><br>
</div>
<div>I am struggling to access polygon faces in my VTK files using
Python. Suppose I have the following segment in my </div>
<div>file:</div>
<div><br>
</div>
<div>
<div>POLYGONS 275558 1102232</div>
<div>3 0 1 2</div>
<div>3 3 2 1</div>
<div>3 0 45 46</div>
<div>3 0 46 1</div>
<div>3 0 2 50</div>
</div>
<div><br>
</div>
<div>I used the piece of code below to prepare: </div>
<div><br>
</div>
<div>
<div> import vtk</div>
<div> Reader = vtk.vtkDataSetReader()</div>
<div> Reader.SetFileName('my_test_file.vtk')</div>
<div> Reader.ReadAllScalarsOn() # Activate the reading of
all scalars</div>
<div> Reader.Update() </div>
<div> Data = Reader.GetOutput()</div>
</div>
<div>
CellData = Data.GetCell(0)</div>
<div> </div>
<div><br>
</div>
<div>I assume CellData.GetPointId(x) is what I should use. But I
got wrong point ID after x =3. </div>
<div><br>
</div>
<div>Can someone help me on this? </div>
<div><br clear="all">
Cheers, <br>
Forrest<br>
</div>
</div></div></blockquote></div></blockquote></div>