如何从tvtk PolyData实例中提取顶点数据?

时间:2014-09-12 13:47:13

标签: python vtk enthought mayavi

我正在尝试从tvtk.PolyData实例中提取顶点数据。例如:

from numpy import array
from tvtk.api import tvtk
from mayavi.sources.vtk_data_source import VTKDataSource
from mayavi.modules.surface import Surface
from mayavi import mlab

pointArr=array([[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,1,1],[1,1,1]],dtype="float64")
faceArr=array([[0,1,2,3],[2,3,4,5]])
faces = tvtk.PolyData()
faces.points=pointArr
print faces.points
faces.polys=faceArr
faces.point_data.scalars = pointArr[:,2]
faces.point_data.scalars.name = 'Height'
src = VTKDataSource(data = faces)
mlab.pipeline.surface(src, opacity=1.)
mlab.show()

打印

vtkPoints (0x9811398)
  Debug: Off
  Modified Time: 838816
  Reference Count: 2
  Registered Events: 
    Registered Observers:
      vtkObserver (0x98112b8)
        Event: 33
        EventName: ModifiedEvent
        Command: 0x9811688
        Priority: 0
        Tag: 1
  Data: 0x9811480
  Data Array Name: Points
  Number Of Points: 6
  Bounds: 
    Xmin,Xmax: (0, 1)
    Ymin,Ymax: (0, 1)
    Zmin,Zmax: (0, 1)

这里发生了什么?我期待看到NumPy数组:

[[ 0.  0.  0.]
 [ 1.  0.  0.]
 [ 1.  1.  0.]
 [ 0.  1.  0.]
 [ 0.  1.  1.]
 [ 1.  1.  1.]]

1 个答案:

答案 0 :(得分:3)

tvtk的重点是处理python对象(包括numpy数组)和VTK对象持有类之间的转换。如果您调查faces.points,您会看到它实际上是一个名为Points的tvtk类实例,但您可以使用

轻松恢复numpy表示
np.array( faces.points )

不同的视图不应该复制内存,所以这一切都非常有效。

编辑:自然tvtk还提供基于事件的API和图形编辑元素