[vtkusers] importing unstructured grid data from numpy array

Eric E. Monson emonson at cs.duke.edu
Wed May 26 12:36:28 EDT 2010


Wow, that will come in handy -- thanks for pointing it out! 

Do you have to be careful, then, about not deleting the numpy array (or letting it go out of scope)? I know there are times when I have to use the deep=True flag in numpy_to_vtk() to make sure that I don't end up trying to access bad memory after I've added the array as some vtkDataSet attribute.

Thanks,
-Eric


On May 26, 2010, at 12:08 PM, David Gobbi wrote:

> The 'l' is just long i.e. platform dependent, I couldn't remember the
> numpy IDs for 32 and 64 bit ints.
> 
> You can pass a numpy array to any VTK method that takes a void
> pointer, it is a very convenient way of sharing memory between numpy
> and VTK.  This is a built-in feature of the python wrappers so there
> isn't anything you have to import to use it: any python "buffer"
> object like strings, structs, arrays, etc. should work, too.
> 
>   David
> 
> 
> On Wed, May 26, 2010 at 8:01 AM, Eric E. Monson <emonson at cs.duke.edu> wrote:
>> Cool, thanks for pointing that out, David. Does the 'I' guarantee a 64-bit int? I know you can make sure the array is 64-bit int type by using:
>> 
>> a = numpy.array([0, 1, 2, 3], dtype='int64')
>> 
>> -Eric
>> 
>> 
>> On May 26, 2010, at 9:50 AM, David Gobbi wrote:
>> 
>>> Even on older version of VTK, it should be possible to do this:
>>> 
>>> import numpy
>>> import vtk
>>> a = numpy.array([0, 1, 2, 3], 'l')
>>> c = vtk.vtkIdTypeArray()
>>> c.SetVoidArray(a, a.size, 1)
>>> 
>>> The type of the numpy array must be a 64-bit type if vtkIdType is 64-bit.
>>> 
>>> - David
>>> 
>>> 
>>> On Wed, May 26, 2010 at 6:53 AM, Eric E. Monson <emonson at cs.duke.edu> wrote:
>>>> It's probably more about which VTK version you have installed, rather than which Python version. It looks like that feature was added on Dec 29th, 2009, so the latest 5.6.0 release should have it, but you may have 5.4 from your package manager.
>>>> 
>>>> You could build VTK 5.6 from source and install (after uninstalling the old one). Or, I guess you could try just downloading the source and replacing your current numpy_support.py with the new one at [vtksource]/Wrapping/Python/vtk/util/numpy_support.py. (Of course that's not the recommended method, and I don't know if it will break anything else, but you could try it as a short-term hack if you don't want to build from source and until you move to an OS version that has a package for 5.6.)
>>>> 
>>>> Talk to you later,
>>>> -Eric
>>>> 
>>>> On May 26, 2010, at 7:23 AM, Florian Bruckner wrote:
>>>> 
>>>>> OK, sounds good. but I cannot find the numpy_to_vtkIdTypeArray()
>>>>> function. should it also be in numpy_support module
>>>>> (/usr/lib/pymodules/python2.6/vtk/util/numpy_support.py) ?
>>>>> 
>>>>> the python version i use is fairly new (Python 2.6.5 (r265:79063, Apr 16
>>>>> 2010, 13:57:41)) so i think it should be there but it is not?
>>>>> 
>>>>> do i have to install any additional packages?
>>>>> 
>>>>> thanks
>>>>> FloB
>>>>> 
>>>>> 
>>>>> On Wed, 2010-05-26 at 06:59 -0400, Eric E. Monson wrote:
>>>>>> Hey Florian,
>>>>>> 
>>>>>> That's what the numpy_to_vtkIdTypeArray() is for -- creating a vtkIdTypeArray. You can't just cast another type of VTK array to IdType, so that's why they have a special function for it. As another note, you can also get vtkIntArray out of numpy_to_vtk() if you specifically send in an int type numpy array.
>>>>>> 
>>>>>> Hope that helps,
>>>>>> -Eric
>>>>>> 
>>>>>> On May 26, 2010, at 4:14 AM, Florian Bruckner wrote:
>>>>>> 
>>>>>>> thanks for your help at first.
>>>>>>> 
>>>>>>> your example works well, but I have still troubles when I want to insert
>>>>>>> the cell data. I'm still a python newbie, but I could figure out that
>>>>>>> numpy_to_vtk gives me a vtkFloatArray whereas the SetCells() function of
>>>>>>> my vtkCellArray requires a vtkIdTypeArray.
>>>>>>> 
>>>>>>> is there something like a typecast operator in python or is there a
>>>>>>> conversion function available somewhere?
>>>>>>> 
>>>>>>> my code looks like the following:
>>>>>>>>> tets = array([[0,1,2,3]])
>>>>>>>>> ccV = VN.numpy_to_vtk(tets, deep=True)
>>>>>>>>> cells = vtk.vtkCellArray()
>>>>>>>>> cells.SetCells(1, ccV)  ==> error
>>>>>>>>> ug.SetCells(VTK_TETRA, cells)
>>>>>>> 
>>>>>>> bye
>>>>>>> FloB
>>>>>>> 
>>>>>>> 
>>>>>>> On Tue, 2010-05-25 at 09:31 -0400, Eric E. Monson wrote:
>>>>>>>> Hey Florian,
>>>>>>>> 
>>>>>>>> There are pretty easy ways to do it, but I haven't used tvtk for a while, so I don't remember the comparison. You can do something like this:
>>>>>>>> 
>>>>>>>> # ===================================
>>>>>>>> from vtk.util import numpy_support as VN
>>>>>>>> import vtk
>>>>>>>> import numpy as N
>>>>>>> 
>>>>>>>> ug = vtk.vtkUnstructuredGrid()
>>>>>>>> pts = vtk.vtkPoints()
>>>>>>>> 
>>>>>>>> # Create the points array and set the UG to use them
>>>>>>>> pp = N.random.rand(10,3)
>>>>>>>> ppV = VN.numpy_to_vtk(pp, deep=True)
>>>>>>>> pts.SetData(ppV)
>>>>>>>> ug.SetPoints(pts)
>>>>>>>> 
>>>>>>>> # Add an attribute array (scalars) to the UG
>>>>>>>> aa = N.random.rand(10)
>>>>>>>> aaV = VN.numpy_to_vtk(aa,deep=True)
>>>>>>>> aaV.SetName('Attribute1')
>>>>>>>> ug.GetPointData().AddArray(aaV)
>>>>>>>> # ===================================
>>>>>>>> 
>>>>>>>> So it's not too bad. Also, note that there is a vtk_to_numpy() and a numpy_to_vtkIdTypeArray(), which often comes in quite handy for me.
>>>>>>>> 
>>>>>>>> Talk to you later,
>>>>>>>> -Eric
>>>>>>>> 
>>>>>>>> ------------------------------------------------------
>>>>>>>> Eric E Monson
>>>>>>>> Duke Visualization Technology Group
>>>>>>>> 
>>>>>>>> 
>>>>>>>> On May 25, 2010, at 5:44 AM, Florian Bruckner wrote:
>>>>>>>> 
>>>>>>>>> hello,
>>>>>>>>> 
>>>>>>>>> I would need a method to read unstructured grid data from a numpy array.
>>>>>>>>> Does anyone know a function like vtkImageInputFrom/ExportToArray.py that
>>>>>>>>> also handles unstructured grids? or is there a possibility to transform
>>>>>>>>> image data to unstructured grids?
>>>>>>>>> 
>>>>>>>>> i only found the tvtk library, but in order to use it I will have to
>>>>>>>>> install the whole ETS project. In addition the documentation of this
>>>>>>>>> library mention that it is more 'pytonic' to use tvtk but that it should
>>>>>>>>> also be possible to do the same things with pure vtk.
>>>>>>>>> 
>>>>>>>>> thanks
>>>>>>>>> FloB
>>>>>>>>> 
>>>>>>>>> 
>>>>>>>>> _______________________________________________
>>>>>>>>> Powered by www.kitware.com
>>>>>>>>> 
>>>>>>>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>>>>>>>> 
>>>>>>>>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>>>>>>>> 
>>>>>>>>> Follow this link to subscribe/unsubscribe:
>>>>>>>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>>>>>> 
>>>>>>> 
>>>>>> 
>>>>> 
>>>> 
>>>> _______________________________________________
>>>> Powered by www.kitware.com
>>>> 
>>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>>> 
>>>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>>> 
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>> 
>> 
>> 




More information about the vtkusers mailing list