def plot_tri_2(ax, points, tri):
edges = collect_edges(tri)
x = np.array([])
y = np.array([])
z = np.array([])
for (i,j) in edges:
x = np.append(x, [points[i, 0], points[j, 0], np.nan])
y = np.append(y, [points[i, 1], points[j, 1], np.nan])
z = np.append(z, [points[i, 2], points[j, 2], np.nan])
ax.plot3D(x, y, z, color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
def collect_edges(tri):
edges = set()
def sorted_tuple(a,b):
return (a,b) if a < b else (b,a)
# Add edges of tetrahedron (sorted so we don't add an edge twice, even if it comes in reverse order).
for (i0, i1, i2, i3) in tri.simplices:
edges.add(sorted_tuple(i0,i1))
edges.add(sorted_tuple(i0,i2))
edges.add(sorted_tuple(i0,i3))
edges.add(sorted_tuple(i1,i2))
edges.add(sorted_tuple(i1,i3))
edges.add(sorted_tuple(i2,i3))
return edges
尝试
mayavi.mlab.triangular_mesh()
blender BPY可以做到这一点,BGE python也可以。在
在blender.org网站在
也可以使用matplotlib的三维绘图(不需要mayavi包)。在
下面的代码是这样一个函数的初始简单实现。在
使用下面的测试代码调用此函数的结果如下所示:
^{pr2}$上面的代码很慢,因为绘图是在循环中完成的。此外,它单独作用于每个单纯形,因此边被渲染多次。 下面是一个更有效的实现,它使用辅助函数
collect_edges
只获取每条边一次,并在plot函数中使用np.nan
值在一个plot命令中绘制边缘段。在使用新函数运行上面的测试代码的结果给出了一个相同的数字,但是在我的机器上运行时间提高了x80倍(300毫秒与3.6毫秒相比)。在
相关问题 更多 >
编程相关推荐