[vtkusers] Raw pointer returned as a string in Python
Guillaume Dumont
rippalka at gmail.com
Wed Jul 9 15:32:22 EDT 2014
Thanks a lot David for your directions.
I managed to accomplish what you said by doing:
>> zbuff_str = visPts.Initialize(True)
>> address = int(zbuff_str[1:17], 16)
>> w, h = renderWin.GetSize()
>> size = w*h
>> FLOATP = ctypes.c_float * size
>> zbuff_buff = FLOATP.from_address(address)
>> # will do a copy here, `np.frombuffer(zbuff_buff)' would be more adequat
>> zbuff = np.fromiter(zbuff_buff, dtype=np.float, count=size)
>> ...
>> visPts.IsPointOccluded(point, zbuff)
By using numpy.frombuffer I get a crash in `IsPointOccluded'. I didn't get
deeper into why this would happen, but this is ok for my use to copy the
whole thing.
It is a simple prototype so I don't mind the leak for now :)
Thanks again,
Guillaume
On Wed, Jul 9, 2014 at 1:03 PM, David Gobbi <david.gobbi at gmail.com> wrote:
> Hi Guillaume,
>
> I'm surprised that the "float *Initialize()" method is even wrapped.
> Generally, such methods only return a "_xxxx_void_p" string if
> they return "void *".
>
> Anyway, in VTK 6.2, the IsPointOccluded() method is wrapped and
> you will be able to call it after you turn the "zBuffer" into a python
> container of some sort. I'd recommend using a numpy array as the
> container for the sake of efficiency.
>
> So, what you will have to do is find a way of creating a numpy float
> array from the hexadecimal address in the string. I have no idea
> how to do it, but I'm sure there is a way.
>
> Note, however, that the docs for vtkSelectVisiblePoints::Initialize()
> say that it returns a new pointer that must be deleted. In other words,
> you will also have to find some way of deleting the pointer from
> Python or else you will end up with a memory leak.
>
> - David
>
> On Wed, Jul 9, 2014 at 10:19 AM, Guillaume Dumont <rippalka at gmail.com>
> wrote:
> > Hi all,
> >
> > I am having a little issue concerning a function in Python that returns
> what
> > seems like a pointer as a string.
> >
> >>> visPts = vtk.vtkSelectVisiblePoints()
> >>> visPts.SetInputData(polyData)
> >>> visPts.SetRenderer(ren)
> >>> zbuff = visPts.Initialize(True)
> >>> print zbuff
> >>> print type(zbuff)
> >
> > and it prints:
> > _000000000dc90040_void_p
> > <type 'str'>
> >
> > I understand that `vtkSelectVisiblePoints::vtInitialize(bool)' returns a
> raw
> > pointer (float*) which I believe cannot directly be exposed to Python,
> but
> > is there a way to pass it to
> `vtkSelectVisiblePoints::IsPointOccluded(const
> > double[3], connst float* zBuffer)' in Python somehow? Or is it just a
> > limitation of the Python interface?
> >
> > Thanks a lot for your help!
> >
> > Guillaume
>
--
Guillaume Dumont
Kremlin-Bicêtre, France
Email: me at guillaumedumont.com
EPITA SCIA 2013
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20140709/d60906c6/attachment.html>
More information about the vtkusers
mailing list