[vtkusers] vtk and python for large amout of data

Berk Geveci berk.geveci at kitware.com
Thu Dec 5 11:50:32 EST 2002


> 
> Now I would like to move the data as complete blocks of array, for example
> something like:
> 
> from vtk import *
> import Numeric
> 
>     list1=[3,2,3,4,3,5,2,8,3,9,1,5]
>     enumber=3
>     earray=Numeric.array(list1)
>     list2=[5,5,5]
>     ctarray=Numeric.array(list2,'int')
> 
>     grid=vtkUnstructuredGrid()
>     IdTArray=vtkIdTypeArray()
>     IdTArray.SetVoidArray(earray,enumber,1)
>     vtkCArray=vtkCellArray()
>     vtkCArray.SetCells(enumber,IdTArray)
>     grid.SetCells(ctarray,vtkCArray)
> 
> Of course this doesn't work because SetVoidArray and SetCells are expecting
> pointers
> which I can't give them (in Python).

Why not ? The python wrappers are smart enough to figure it out (I have
to admit I am very impressed, I think python wrappers are the only ones
which allow this, kudos to whoever added the feature) . The following
works for me:

import Numeric

# Create an array (float)
a=Numeric.zeros((5,), Numeric.Float32)
for i in range(5):
    a[i] = i

from vtk import *
# Create the corresponding vtk array
fa=vtkFloatArray()
# Assign the pointer. NOTE: DO NOT destroy the original
# array object, it is responsible of keeping the actual
# memory block around and will delete it if destroyed, i.e:
# If you do
# a = None
# then
# fa.GetTuple1(0)
# will access invalid (freed) memory
fa.SetVoidArray(a, 5, 1)
print fa.GetTuple1(2)

>>>> prints 2

# To show that the two arrays point to the same memory.
a[3]=11
print fa.GetTuple1(3)

>>>> prints 11

# Now let's do another type
a=Numeric.zeros((5,),Numeric.Int32)
a[0]=5
ar=vtkIdTypeArray()
ar.SetVoidArray(a, 5, 1)
print ar.GetValue(0)

>>>> prints 5

# Cell array
ca=vtkCellArray()
ca.SetCells(1, ar)

Unfortunately, to create an unstructured grid, you have to use the
following method:

void vtkUnstructuredGrid::SetCells(vtkUnsignedCharArray *cellTypes,
                                   vtkIntArray *cellLocations, 
                                   vtkCellArray *cells);

This means that you have to compute cellTypes, cellLocations and cells.
This is because the following is not wrapped:

void  vtkUnstructuredGrid::SetCells(int *types, vtkCellArray *cells);

(since it takes a pointer to a variable length in array).

-Berk







More information about the vtkusers mailing list