<div dir="ltr">This is fantastic David!!!<div><br></div><div>One word of caution to people using numpy array -> VTK array route:</div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">c = numpy.array([1,2,3], 'f')</span><br></div><div><div style="font-size:12.8px">a = vtk.vtkFloatArray()</div><div style="font-size:12.8px">a.SetArray(c, c.size, True)</div><div style="font-size:12.8px">a.array = c  # keep reference to "c"</div></div><div><br></div><div>This is safe only as long as you keep the a object around. If you do something like this, you are in trouble:</div><div><br></div><div>pd = vtk.vtkPointData()</div><div>pd.AddArray(a)</div><div>del a</div><div><br></div><div>In this case, the Python object goes away and therefore releases the reference to c. In the dataset_adapter module, I implemented a way around this issue. I suggest taking a look at that which is in the numpyTovtkDataArray() method.</div><div><br></div><div>Best,</div><div>-berk</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 16, 2017 at 8:37 AM, David Gobbi <span dir="ltr"><<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi All,<div><br></div><div>Yesterday I merged the wrapping of the vtkSOADataArrays, and also added a new wrapper hint to assist with VTK's zero-copy methods.</div><div><br></div><div>Zero-copy is when an array uses an existing block of memory.  In the Python wrappers, this generally means making a VTK array from a numpy array or vice-versa.  Or making a VTK image from a PIL image.  Or using memmap to share a VTK data set among multiple processes.</div><div><br></div><div>The new wrapper hint VTK_ZEROCOPY can be applied to method parameters that are used for zero-copy operations:</div><div><br></div><div>  void vtkFloatArray::SetArray(VTK_ZE<wbr>ROCOPY float *ptr, vtkIdType n, int save);</div><div><br></div><div>This hint allows the Python wrappers to do the zero-copy magic with SetArray:</div><div><br></div><div>  import numpy</div><div>  import vtk</div><div>  c = numpy.array([1,2,3], 'f')</div><div>  a = vtk.vtkFloatArray()</div><div>  a.SetArray(c, c.size, True)</div><div>  a.array = c  # keep reference to "c"</div><div><br></div><div>Astute VTKers will note that this was already possible with SetVoidArray(), as used by the vtk.numpy_support module, but SetArray() adds type checking.</div><div><br></div><div>Also, SetArray() works with the newly-wrapped vtkSOADataArrays, so perhaps in the future numpy_support.numpy_to_vtk() can be expanded to support SOA arrays:</div><div><br></div><div>  c0 = numpy.array([1,2,3], 'f')</div><div>  c1 = numpy.array([4,5,6], 'f')</div><div>  a = vtk.vtkSOADataArrayTemplate[<wbr>c0.dtype]()</div><div>  a.SetNumberOfComponents(2)</div><div>  a.SetArray(0, c0, c0.size, True, True)</div><div>  a.SetArray(1, c1, c1.size, True, True)</div><div><br></div><div>In the future this could be done with  a = vtk.numpy_support.numpy_to_vtk<wbr>( (c0,c1) ).</div><div><br></div><div>I haven't yet implemented the mechanism to go in the other direction, i.e. to create a numpy array from a VTK SOA array.</div><div><br></div><div>Also: vtkSOADataArrayTemplate is now wrapped, but vtkAOSDataArrayTemplate is not.  Currently the wrappers think that vtkDataArray is the immediate superclass of vtkFloatArray etc.  This could be changed, if there are use cases for directly instantiating vtkAOSDataArrayTemplate.</div><div><br></div><div>Cheers,</div><div> - David</div></div>
<br>______________________________<wbr>_________________<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/<wbr>opensource/opensource.html</a><br>
<br>
Search the list archives at: <a href="http://markmail.org/search/?q=vtk-developers" rel="noreferrer" target="_blank">http://markmail.org/search/?q=<wbr>vtk-developers</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/vtk-developers" rel="noreferrer" target="_blank">http://public.kitware.com/<wbr>mailman/listinfo/vtk-<wbr>developers</a><br>
<br>
<br></blockquote></div><br></div>