【问题标题】:List all faces for all edges列出所有边的所有面
【发布时间】:2013-02-08 10:04:24
【问题描述】:

一个很简单的问题: 如何在 Python(或 Cython)中有效地计算以下数量。

给定 3D 多边形列表(多边形

下面的形式给出了一个多边形列表:

vertex = np.array([[0, 0, 0], [0, 0, 1], [0, 1, 0],[1, 0, 0],[0.5, 0.5, 0.5]], order = 'F').T
polygons = np.array([3, 0, 1, 2, 4, 1, 2, 3 ,4])

即多边形是一维数组,其中包含 [N,i1,i2,i3,i4,...] 形式的条目, N 是多边形中的顶点数,然后是顶点数组中顶点的 id 号(在上面的示例中,一个三角形有 3 个顶点 [0,1,2],一个多边形有 4 个顶点 [1,2] ,3,4]

我需要计算信息:所有边的列表和每条边的信息 哪些面包含这条边。

而且我需要快速完成:顶点的数量可能很大。

更新
多边形是闭合的,即多边形[4, 0, 1, 5, 7]表示有4个顶点,边是0-1, 1-5, 5-7, 7-0 事实上,脸是多边形的同义词。

【问题讨论】:

  • 你能定义你的案例中的边和面吗?

标签: python cython


【解决方案1】:

不知道这是不是最快的选择,很可能不是,但它确实有效。我认为最慢的部分是edges.index((v, polygon[i + 1])),我们必须在其中查找此边缘是否已在列表中。顶点数组并不是真正需要的,因为边是一对顶点索引。我使用 face_index 作为多边形索引的参考,因为你没有写出什么是面。

vertex = [[0,0,0], [0,0,1], [0,1,0],[1,0,0],[0.5,0.5,0.5]]
polygons = [3,0,1,2,4,1,2,3,4]
_polygons = polygons
edges = []
faces = []
face_index = 0

while _polygons:
    polygon = _polygons[1:_polygons[0] + 1]
    polygon.append(polygon[0])
    _polygons = _polygons[_polygons[0] + 1:]

    for i, v in enumerate(polygon[0:-1]):
        if not (v, polygon[i + 1]) in edges:
            edges.append((v, polygon[i + 1]))
            faces.append([face_index, ])
        else:
            faces[edges.index((v, polygon[i + 1]))].append(face_index)
    face_index += 1

edges = map(lambda edge, face: (edge, face), edges, faces)

print edges

<<< [((0, 1), [0]), ((1, 2), [0, 1]), ((2, 0), [0]), ((2, 3), [1]), ((3, 4), [1]), ((4, 1), [1])]

您可以通过删除线 polygon.append(polygon[0]) 并手动将多边形的第一个顶点附加到多边形中的顶点列表来使其更快,这应该不是问题。 我的意思是将polygons = [3,0,1,2,4,1,2,3,4] 更改为polygons = [3,0,1,2,0,4,1,2,3,4,1]

PS 尝试使用PEP8。这是一种代码类型。它说您应该在可迭代对象中的每个逗号后放置一个空格,以便于阅读。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    相关资源
    最近更新 更多