[vtkusers] Writing VTK filters in Python and C
David Gobbi
dgobbi at irus.rri.ca
Wed Feb 20 23:15:37 EST 2002
Hi Robb,
A couple years ago I wrote classes to move vtkImageData (also works
for vtkStucturedPoints) into and out of a NumPy array. These
classes were in VTK 3.2, but are not in VTK 4.x because they weren't
sufficiently well tested:
http://public.kitware.com/cgi-bin/cvsweb.cgi/~checkout~/vtk/python/vtkImageExportToArray.py?rev=1.1&content-type=text/plain&cvsroot=vtk
http://public.kitware.com/cgi-bin/cvsweb.cgi/~checkout~/vtk/python/vtkImageImportFromArray.py?rev=1.1&content-type=text/plain&cvsroot=vtk
They might work right off-the-bat (or might not) but in any case
you can take a peek inside to see how they work. They convert
the NumPy array into a python string, and then have VTK access
the raw data in the string via a 'void *'.
In fact any vtkDataArray can be created from a python string using
>>> array.SetVoidArray(s,len(s)/typesize,1)
where 'typesize' is 1 for UnsignedCharArray, 2 for ShortArray,
8 for DoubleArray, etc. and 'save' must be set to 1 because VTK
cannot be allowed to free memory that was allocated by python.
Vice-versa doesn't work, i.e. void *GetVoidPointer() isn't available
in python. Maybe sometime in the future it will be.
- David
--
David Gobbi, MSc dgobbi at irus.rri.ca
Advanced Imaging Research Group
Robarts Research Institute, University of Western Ontario
On Wed, 20 Feb 2002, Robert Brown wrote:
> I've got a vtkProgrammableFilter set up but the way I was getting the vtkStructuredPoints input was not working. I got a void pointer which Python didn't know how to deal with. I've got a vtkStructuredPoints object -- do I have to use the point by point get and set functions to work with it? I'd like to copy everything into a Numpy array, then put it back into the structuredpoints (ImageData) for output.
>
> Thanks,
>
> Robb
>
> Yes, Ken and Berk are right (of course). The vtkProgrammableFilter
> will do the trick. Note that the 'vtkStructuredPoints' data type
> is equivalent to vtkImageData for most intents and purposes here.
>
> Look at the man pages for the particular data type you want to use
> as well as the pages for vtkDataArray. Building data sets has
> changed slightly (for the better) in VTK 4.x compared to 3.x.
>
> - David
>
>
> On Fri, 15 Feb 2002, Ken Martin wrote:
>
> > Isn't this a case where vtkProgramableFilter can be used ? It is a
> > class that lets you fill in the execute method for a filter etc, and
> > the execute method can be written in Tcl, Python, Java. I think there
> > is a test of it in VTK/Graphics/Testing/Tcl
> >
> > Ken
> _______________________________________________
> This is the private VTK discussion list.
> Please keep messages on-topic. Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
More information about the vtkusers
mailing list