[vtkusers] 3D numpy array to vtkDataSet

Quentan Qi quentan at gmail.com
Sun Jul 19 13:40:42 EDT 2015


Dear Cory,

I may fail to explain my problem, so I made an example <https://github.com/quentan/Test_ImageData> to show what I want to express, as you suggested. https://github.com/quentan/Test_ImageData <https://github.com/quentan/Test_ImageData>

This example generates a quadric with its implicit form: F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9 

The left viewport renders the quadric with vtkQuadric, which works perfectly.

The right viewport renders the same quadric with a meshgrid generated with numpy. The meshgrid is converted to vtkFloatArray then to vtkImageData. Its shape is right, but locates at wrong position, which seems like it was scaled to some positive values. 

Note the small perpendicular coordinate lines in the corner.

I guess the issue raises around line 120 - 123. If any other wrongs happen, point it for me, please.

Cheers,
Quentan


> On 18 Jul 2015, at 15:09, Cory Quammen <cory.quammen at kitware.com> wrote:
> 
> Quentan,
> 
> vtkImageData is in fact a subclass of vtkDataSet, so no conversion is needed. You said, "vtkImageData scales all points to positive integer values", but this should not be the case. Something strange is going on.
> 
> Could you expand the code above into a complete example that can be run through vtkpython? That will make it easier to take a look. In particular, please add a part that shows the vtkImageData are being scaled to positive integers. It's fine if you fill the array with random values.
> 
> Thanks,
> Cory
> 
> 
> On Sat, Jul 18, 2015 at 6:08 AM, Quentan Qi <quentan at gmail.com <mailto:quentan at gmail.com>> wrote:
> Hi there,
> 
> I am going to read a set of discrete 3D points (float value, not int value) and show both the points and the fitting surface via VTK’s implicit functions, say vtkImplicitVolume or vtkImplicitDataSet.
> 
> It works fine of the numpy arrary to vtkImageData conversion in the following code, but is out of my mind how to convert it to vtkDataSet, which is my requirement.
> 
> ```Python
>     # Convert numpy array to VTK array (vtkFloatArray)
>     vtk_data_array = numpy_support.numpy_to_vtk(
>         num_array=ndarray.transpose(2, 1, 0).ravel(),  # ndarray contains the fitting result from the points. It is a 3D array
>         deep=True,
>         array_type=vtk.VTK_FLOAT)
> 
>     # Convert the VTK array to vtkImageData
>     img_vtk = vtk.vtkImageData()
>     img_vtk.SetDimensions(ndarray.shape)
>     img_vtk.SetSpacing(spacing[::-1])
>     img_vtk.GetPointData().SetScalars(vtk_data_array)
> 
>     # …
>     implicit_volume = vtk.vtkImplicitVolume()  # I want a vtkImplicitDataSet, whose input is a vtkDataSet
>     implicit_volume.SetVolume(img_vtk)
> ```
> 
> Because vtkImageData scales all points to positive integer values so that the rendering result mismatches the original points, I deem vtkDataSet or its subclass can do it. Anybody knows how to convert?
> 
> Cheers,
> Quentan
> 
> _______________________________________________
> Powered by www.kitware.com <http://www.kitware.com/>
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html <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 <http://www.vtk.org/Wiki/VTK_FAQ>
> 
> Search the list archives at: http://markmail.org/search/?q=vtkusers <http://markmail.org/search/?q=vtkusers>
> 
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers <http://public.kitware.com/mailman/listinfo/vtkusers>
> 
> 
> 
> 
> -- 
> Cory Quammen
> R&D Engineer
> Kitware, Inc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150719/ca66bc37/attachment.html>


More information about the vtkusers mailing list