python - 多面体的 Delaunay 三角化(Python)

标签 python matplotlib scipy delaunay polyhedra

我正在尝试获取 Delaunay Triangulation python 中的多面体,以便我可以计算质心。我看到有一个 Delaunay scipy.spatial 中的函数,它在 n 维空间中工作。问题是文档显示了 2D 使用,但没有给我指示如何处理更高维度。能够将这个对象分解成一个数组可能会为我解决这个问题,但我不知道该怎么做。

我遇到的问题是我不知道如何验证它在输出对象时是否正常工作。我在谷歌上找不到任何关于如何绘制多面体或如何使用 scipy 吐回的对象的信息。

如果我这样做

import numpy as np
from scipy.spatial import Delaunay

points = np.array([[0,0,0],[1,0,0],[1,1,0],[1,0,1],[1,1,1],[0,1,0],[0,1,1],[0,0,1]])
Delaunay(points)

我真的很想能够取回这些四面体的坐标,以便计算多面体的质心。如果我也能画出镶嵌多面体的图形,那就太好了。我在 MATLAB 中看到我可以使用一个名为 trimesn 的函数来做到这一点,我从 matplotlib 中找到了一个,但它似乎真的很不一样,而且它的文档也不是很好。

from matplotlib.collections import TriMesh TriMesh.__doc__ 

u'\n      Class for the efficient drawing of a triangular mesh using\n   
Gouraud shading.\n\n    A triangular mesh is a
:class:`~matplotlib.tri.Triangulation`\n    object.\n    '

最佳答案

tess = Delaunay(pts) 返回的是 Delanauy 类的对象。您可以将四面体检查为 tess.simplices。它有不同的属性和方法。例如,在 2D 中,它可以为您绘制三角剖分、凸包和 Voronoi 镶嵌。

关于最终四面体集合的可视化,我没有找到直接的方法,但我设法获得了一个工作脚本。检查下面的代码。

from __future__ import division
import numpy as np
from scipy.spatial import Delaunay
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
from itertools import combinations


def plot_tetra(tetra, pts, color="green", alpha=0.1, lc="k", lw=1):
    combs = combinations(tetra, 3)
    for comb in combs:
        X = pts[comb, 0]
        Y = pts[comb, 1]
        Z = pts[comb, 2]
        verts = [zip(X, Y, Z)]
        triangle = Poly3DCollection(verts, facecolors=color, alpha=0.1)
        lines = Line3DCollection(verts, colors=lc, linewidths=lw)
        ax.add_collection3d(triangle)
        ax.add_collection3d(lines)

pts = np.array([
            [0,0,0],
            [1,0,0],
            [1,1,0],
            [1,0,1],
            [1,1,1],
            [0,1,0],
            [0,1,1],
            [0,0,1]])
tess = Delaunay(pts)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for k, tetra in enumerate(tess.simplices):
    color = plt.cm.Accent(k/(tess.nsimplex - 1))
    plot_tetra(tetra, pts, color=color, alpha=0.1, lw=0.5, lc="k")
ax.scatter(pts[:, 0], pts[:, 1], pts[:, 2], c='k')
plt.savefig("Delaunay.png", dpi=600)
plt.show()

生成的图像是

enter image description here

关于python - 多面体的 Delaunay 三角化(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34820373/

相关文章:

python - 基于其他列在 DataFrame 中创建新列

python - Matplotlib Boxplot 和 pandas 数据框数据类型

python - Scipy Normaltest是如何使用的?

matplotlib - 在 julia 中使用 matplotlib 的补丁

matplotlib - 累积直方图的最后一个点是 y=0

python - 为什么我不能为整数规划设置 SciPy 的约束优化?

python - 如何在 Python 中可视化 3D delaunay 三角剖分?

python - 将用户输入传递给节日

python - 如何处理多个关键字参数?

python - 在考虑执行流程时,异常的反义词是什么?