开发者

Convert vtkPoints to numpy array?

开发者 https://www.devze.com 2023-02-20 18:47 出处:网络
I am using Mayavi2 in a Python script to calculate 3d iso-surfaces. As a result I get a vtkPoints object. Now I want to convert this vtkPoints object (\'vtkout\' in the code sample below) to a simple

I am using Mayavi2 in a Python script to calculate 3d iso-surfaces. As a result I get a vtkPoints object. Now I want to convert this vtkPoints object ('vtkout' in the code sample below) to a simple numpy array with 3 lines containing all x, y and z values. I get vtkout using a code like this:

import numpy
from enthought.mayavi import mlab
import array

randVol = numpy.random.rand(50,50,50)        # fill volume with some random potential
X, Y, Z = numpy.mgrid[0:50, 0:50, 0:50]      # grid
surf = mlab.contour3d(X, Y, Z, randVol, contours=[0.5])   # calc contour
vtkout = surf.contour.contour_filter.output.points        # get the vtkPoints object

At the moment I use the following code to extract the points into an array:

pointsArray = numpy.zeros((3, vtkout.number_of_points))
for n in range(vtkout.number_of_points):
    pointsArray[0,n] = vtko开发者_StackOverflowut[n][0]
    pointsArray[1,n] = vtkout[n][1]
    pointsArray[2,n] = vtkout[n][2]

I wonder if there is no general routine doing such conversions for me in a convenient, fast and safe way?


vtk_points.to_array() did not work for me (to_array() doesn't seem to exist in plain vtk).

What has actually worked in my case is using the numpy_support module:

from vtk.util import numpy_support
as_numpy = numpy_support.vtk_to_numpy(vtk_points.GetData())


As confirmed from comments on the original post, you might try:

vtkout.to_array().T

This is a direct method that does not require looping.

0

精彩评论

暂无评论...
验证码 换一张
取 消