Matplotlib 从mayavi中的delaunay过滤器提取三角形

Matplotlib 从mayavi中的delaunay过滤器提取三角形,matplotlib,delaunay,mayavi,Matplotlib,Delaunay,Mayavi,如何从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) #matplotl

如何从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 delaunay过滤器中提取三角形,以获得变量@triangle_index和@centers(就像matplotlib一样)

我唯一发现的就是这个


但仅获取边,并且编码方式与matplotlib不同,以获取三角形索引:

poly = delaunay.outputs[0]
tindex = poly.polys.data.to_array().reshape(-1, 4)[:, 1:]
poly
是一个PolyData对象,
poly.poly
是一个存储索引信息的对象。 有关CellArray的详细信息:

要获得每个外接圆的圆心,需要循环每个三角形并计算圆心:

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.expectCircle()
是一个静态函数,因此需要将三角形的所有点作为参数传递,修改第四个参数将返回中心数据

以下是完整的代码:

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不同,因为有许多可能的解决方案。

要获取三角形索引:

poly = delaunay.outputs[0]
tindex = poly.polys.data.to_array().reshape(-1, 4)[:, 1:]
poly
是一个PolyData对象,
poly.poly
是一个存储索引信息的对象。 有关CellArray的详细信息:

要获得每个外接圆的圆心,需要循环每个三角形并计算圆心:

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.expectCircle()
是一个静态函数,因此需要将三角形的所有点作为参数传递,修改第四个参数将返回中心数据

以下是完整的代码:

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不同,因为有许多可能的解决方案