Python VTK:直接与PolyData协调

时间:2017-04-24 11:24:07

标签: python numpy vtk

我想将特定范围内的x,y和z的所有坐标组合转换为现在步骤1直接转换为vtk.polyData或vtk.points。我的第一种方法是使用itertools.product,但我认为这会有一个非常糟糕的运行时。所以我采用vtk的另一种方法,无论如何我还需要下一部分来支持我的程序。

使用itertools.product

的第一个appraoch
import numpy as np
import itertools
import vtk

x1=[10,11,12....310]
y1=[10,11,12....310]
z1=[0,1,2....65]

points1 = vtk.vtkPoints()                      
for coords in itertools.product(x1,y1,z1):
   points1.InsertNextPoint(coords)
boxPolyData1 = vtk.vtkPolyData()
boxPolyData1.SetPoints(points1)

到目前为止我使用vtk的方法:

import numpy as np
from vtk.util import numpy_support

coords = np.mgrid[10:310, 10:310, 0:65]
vtk_data_array = numpy_support.numpy_to_vtk(num_array=coords.ravel(),deep=True,array_type=vtk.VTK_FLOAT)

points = vtk.vtkPoints()
points.SetData(vtk_data_array)

但他只是撞毁了我的蟒蛇。有没有人有想法?

最好的问候!

1 个答案:

答案 0 :(得分:1)

coords列在np.column_stacknp.c_的列中,然后将其作为输入提供给num_array,就像这样 -

x,y,z = np.mgrid[10:310, 10:310, 0:65]
out_data = np.column_stack((x.ravel(), y.ravel(), z.ravel()))

vtk_data_array = numpy_support.numpy_to_vtk(num_array=out_data,\
                              deep=True,array_type=vtk.VTK_FLOAT)

或者,直接获取out_data -

out_data = np.mgrid[10:310, 10:310, 0:65].reshape(3,-1).T

使用initialization替换由3D创建的np.mgrid数组的另一种方法就是这样 -

def create_mgrid_array(d00,d01,d10,d11,d20,d21,dtype=int):
    df0 = d01-d00
    df1 = d11-d10
    df2 = d21-d20
    a = np.zeros((df0,df1,df2,3),dtype=dtype)
    X,Y,Z = np.ogrid[d00:d01,d10:d11,d20:d21]
    a[:,:,:,2] = Z
    a[:,:,:,1] = Y
    a[:,:,:,0] = X
    a.shape = (-1,3)
    return a

示例运行以展示create_mgrid_array -

的使用情况
In [151]: create_mgrid_array(3,6,10,14,20,22,dtype=int)
Out[151]: 
array([[ 3, 10, 20],
       [ 3, 10, 21],
       [ 3, 11, 20],
       [ 3, 11, 21],
       [ 3, 12, 20],
       [ 3, 12, 21],
       [ 3, 13, 20],
       [ 3, 13, 21],
       [ 4, 10, 20],
       [ 4, 10, 21],
       [ 4, 11, 20],
       [ 4, 11, 21],
       [ 4, 12, 20],
       [ 4, 12, 21],
       [ 4, 13, 20],
       [ 4, 13, 21],
       [ 5, 10, 20],
       [ 5, 10, 21],
       [ 5, 11, 20],
       [ 5, 11, 21],
       [ 5, 12, 20],
       [ 5, 12, 21],
       [ 5, 13, 20],
       [ 5, 13, 21]])

运行时测试

方法 -

def loopy_app():
    x1 = range(10,311)
    y1 = range(10,311)
    z1 = range(0,66)

    points1 = vtk.vtkPoints()                      
    for coords in itertools.product(x1,y1,z1):
       points1.InsertNextPoint(coords)
    return points1

def vectorized_app():
    out_data = create_mgrid_array(10,311,10,311,0,66,dtype=float)
    vtk_data_array = numpy_support.numpy_to_vtk(num_array=out_data,\
                                    deep=True,array_type=vtk.VTK_FLOAT)

    points2 = vtk.vtkPoints()
    points2.SetData(vtk_data_array)
    return points2

计时和验证 -

In [155]: # Verify outputs with loopy and vectorized approaches    
     ...: out1 =  vtk_to_numpy(loopy_app().GetData())
     ...: out2 =  vtk_to_numpy(vectorized_app().GetData())
     ...: print np.allclose(out1, out2)
     ...: 
True

In [156]: %timeit loopy_app()
1 loops, best of 3: 923 ms per loop

In [157]: %timeit vectorized_app()
10 loops, best of 3: 67.3 ms per loop

In [158]: 923/67.3
Out[158]: 13.714710252600298

13x+ 加速在那里提出的矢量化的一个!