[vtkusers] 3D numpy array to vtkDataSet

Quentan Qi quentan at gmail.com
Sat Jul 18 06:08:34 EDT 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150718/7aee31ce/attachment.html>


More information about the vtkusers mailing list