GetPolys() vertex indices weird order

Hi,

When I retrieve the polygon data from a mesh (the cube below as an example), I get something like this:

>>> vtk_to_numpy(mesh.GetPolys().GetData())
>>> [ 4  0  1  3  2  4  4  6  7  5  4  8 10 11  9  4 12 13 15 14  4 16 18 19 17  4 20 21 23 22]

The way I’m interpreting it is, each triangle is defined by its vertices indices, and looking at the output data and the cube, it looks like the first 3 values, 4 0 1, make sense as in that they define a triangle, but the next 3 values, 3 2 4, it appears to define a triangle that goes across the middle of the cube, which doesn’t seem right. Am I interpreting the data correctly of is there something else going on?

Thanks!

The first “4” is the number of points in the polygon, and “0 1 3 2” are the points. The next “4” is for the next polygon, with points “4 6 7 5”.

I see the light now, thanks!

Not directly a VTK question, but if for any of you guys a solution to this can come easier than it is for me at the moment, I would appreciate it.
I’m having troubles to split that array. In this example, all polygons have 4 vertices so it’s easier if that assumption is made, but I’d like to handle situations where different polygons might have different number of vertices.
So I’d like to split this (numpy) array:

[ 4 0 1 3 2 4 4 6 7 5 4 8 10 11 9 4 12 13 15 14 4 16 18 19 17 4 20 21 23 22]

into something like this:

[ [0 1 3 2] [ 4 6 7 5] [8 10 11 9] [12 13 15 14] [16 18 19 17] [20 21 23 22]]

Basically leaving out the number that indicates the number of vertices the next polygons has, and group the indices of each polygon into a subarray.

Thanks!

def splitcells(cellarray):
    cells = []
    loc = 0
    while loc < len(cellarray):
        n = cellarray[loc]
        loc += n + 1
        cells.append(cellarray[loc-n:loc])
    return cells
>>> cellarray = [4,0,1,3,2,4,4,6,7,5,4,8,10,11,9,4,12,13,15,14,4,16,18,19,17,4,20,21,23,22]
>>> cells = splitcells(cellarray)
>>> cells
[[0, 1, 3, 2], [4, 6, 7, 5], [8, 10, 11, 9], [12, 13, 15, 14], [16, 18, 19, 17], [20, 21, 23, 22]]




2 Likes

Thanks! Much appreciated