【问题标题】:Delaunay Triangulation simplices - scipyDelaunay三角剖分单纯形 - scipy
【发布时间】:2020-10-17 21:07:09
【问题描述】:

我正在阅读有关 Delaunay (scipy) 的信息并遇到了代码:

import numpy as np
points = np.array([[0, 0], [0, 1.1], [1, 0], [1, 1]])

from scipy.spatial import Delaunay
tri = Delaunay(points)

import matplotlib.pyplot as plt
plt.triplot(points[:,0], points[:,1], tri.simplices.copy())
plt.plot(points[:,0], points[:,1], 'o')
plt.show()

据我了解,单纯形是三角形向更高维度的推广。

我不明白下面代码的含义,希望帮助理解它:

# Point indices and coordinates for the two triangles forming the triangulation:

tri.simplices
array([[3, 2, 0],
       [3, 1, 0]], dtype=int32)

points[tri.simplices]
array([[[ 1. ,  1. ],
        [ 1. ,  0. ],
        [ 0. ,  0. ]],
       [[ 1. ,  1. ],
        [ 0. ,  1.1],
        [ 0. ,  0. ]]])

Triangle 0 is the only neighbor of triangle 1, and it’s opposite to vertex 1 of triangle 1:


tri.neighbors[1]
# array([-1,  0, -1], dtype=int32)

points[tri.simplices[1,1]]
array([ 0. ,  1.1])

谢谢!

【问题讨论】:

    标签: python-3.x scipy delaunay scipy-spatial


    【解决方案1】:

    此代码从包含两个三角形的四个顶点创建一个 Delaunay 三角剖分。三角剖分如下所示:

    代码首先定义数组中的四个顶点:

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

    接下来,scipy 为这些点构造一个 Delaunay 三角剖分:

    from scipy.spatial import Delaunay
    tri = Delaunay(points)
    

    现在,tri.simplices 包含 Delaunay 三角剖分中的三角形列表(在此 2D 情况下)。每个三角形都表示为三个整数:每个值表示原始点数组中的一个索引。

    tri.simplices
    array([[3, 2, 0],
           [3, 1, 0]], dtype=int32)
    

    所以 [3,2,0] 是顶点 3 (1,1)、顶点 2 (1,0) 和顶点 0 (0,0) 之间的三角形。下面的代码连接点和tri数据结构来计算每个三角形顶点的坐标,消除了间接性:

    points[tri.simplices]
    array([[[ 1. ,  1. ],
            [ 1. ,  0. ],
            [ 0. ,  0. ]],
           [[ 1. ,  1. ],
            [ 0. ,  1.1],
            [ 0. ,  0. ]]])
    

    tri.neighbors 数组包含有关哪些三角形彼此相邻的信息。

    tri.neighbors[1]
    # array([-1,  0, -1], dtype=int32)
    

    回想一下三角形 1(位置 1 的三单纯形)有顶点 [3,1,0]。三角形 0 是与顶点 1 相对的相邻三角形 1,这就是结果在第二个元素中具有值 0 的原因(对应于 [3,1,0] 中的 1)。没有与顶点 3 相对的三角形(即,沿顶点 0 和 1 之间的边连接)或顶点 0 相对的三角形,因此邻居数组在这些位置包含 -1。

    最后,是这段代码吗?

    points[tri.simplices[1,1]]
    array([ 0. ,  1.1])
    

    回顾上面的tri.simplices数据结构,单纯形1的位置1包含一个值1(即[3,1,0]。这条线只是查找顶点1的坐标。

    最后一点,返回的单纯形中的顶点顺序不需要与这个原始示例匹配,并且可能因版本而异。这是与以下评论中的观察结果相匹配的最近运行,该观察结果与原始顶点顺序不一致(在原始文档中提供):

    【讨论】:

    • tri.neighbors[0] 的输出是array([ 1, -1, -1], dtype=int32)。但是根据图/图,输出不应该是array([-1, 1, -1], dtype=int32),因为三角形1是相邻的三角形0并且与顶点2(三角形0)相对的是顶点1(三角形1)?根据输出,我不明白顶点 1 与顶点 3 的对面如何?
    • 当我实际运行这段代码时,我碰巧得到位置 0 的三角形顶点以不同的顺序列出。我会用一些细节更新答案。
    猜你喜欢
    • 2016-05-19
    • 2019-02-16
    • 2016-02-08
    • 2014-01-21
    • 2021-12-19
    • 2019-06-22
    • 2015-01-07
    • 2019-04-18
    • 2012-03-04
    相关资源
    最近更新 更多