2013-05-09 61 views
1

如何從mayavi中的delaunay過濾器提取三角形?提取三角形在mayavi中形成delaunay過濾器

我想提取三角形就像matplotlib確實

import numpy as np 
import matplotlib.delaunay as triang 
from enthought.mayavi import mlab 

x = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2]) 
y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2]) 
z = np.zeros(9) 
#matplotlib 
centers, edges, triangles_index, neig = triang.delaunay(x,y) 

#mayavi 
vtk_source = mlab.pipeline.scalar_scatter(x, y, z, figure=False) 
delaunay = mlab.pipeline.delaunay2d(vtk_source) 

我想提取從Mayavi的德勞內過濾器的三角形來獲得變量@triangle_index和@centers(就像matplotlib)

的只是我發現事情是這樣的 http://docs.enthought.com/mayavi/mayavi/auto/example_delaunay_graph.html

,但只獲得了邊緣,並codificated比matplotlib

不同3210

回答

1

要獲得三角形指數:

poly = delaunay.outputs[0] 
tindex = poly.polys.data.to_array().reshape(-1, 4)[:, 1:] 

poly是POLYDATA對象,poly.polys是存儲索引信息的CellArray對象。 有關CellArray細節:http://www.vtk.org/doc/nightly/html/classvtkCellArray.html

讓每一個外接圓的中心,你需要循環每一個三角形和計算中心:

centers = [] 
for i in xrange(poly.number_of_cells): 
    cell = poly.get_cell(i) 
    points = cell.points.to_array()[:, :-1].tolist() 
    center = [0, 0] 
    points.append(center) 
    cell.circumcircle(*points) 
    centers.append(center) 

centers = np.array(centers) 

cell.circumcircle()是靜態函數,所以你需要通過所有的將三角形的點作爲參數,中心數據將通過修改第四個參數返回。

下面是完整的代碼:

import numpy as np 
from enthought.mayavi import mlab 

x = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2]) 
y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2]) 
z = np.zeros(9) 

vtk_source = mlab.pipeline.scalar_scatter(x, y, z, figure=False) 
delaunay = mlab.pipeline.delaunay2d(vtk_source) 

poly = delaunay.outputs[0] 
tindex = poly.polys.data.to_array().reshape(-1, 4)[:, 1:] 

centers = [] 
for i in xrange(poly.number_of_cells): 
    cell = poly.get_cell(i) 
    points = cell.points.to_array()[:, :-1].tolist() 
    center = [0, 0] 
    points.append(center) 
    cell.circumcircle(*points) 
    centers.append(center) 

centers = np.array(centers) 

print centers 
print tindex 

輸出是:

[[ 1.5 0.5] 
[ 1.5 0.5] 
[ 0.5 1.5] 
[ 0.5 0.5] 
[ 0.5 0.5] 
[ 0.5 1.5] 
[ 1.5 1.5] 
[ 1.5 1.5]] 
[[5 4 2] 
[4 1 2] 
[7 6 4] 
[4 3 1] 
[3 0 1] 
[6 3 4] 
[8 7 4] 
[8 4 5]] 

的結果可能不一樣matplotlib.delaunay,因爲有許多可能的解決方案。