标题可能看起来有点令人困惑,但这是场景。我在混合器中编写一个简单的导出器,我有以下信息,我正在尝试导出一个立方体,我有面和构成面的边,以及哪个顶点构成边。我试图以顺时针顺序分配顶点以创建一个立方体。基本上,顺时针,我的意思是顶部和底部顶点进入附加图像的模式。
例如,当抓取立方体的所有面并检查构成面的边缘而不是构成该面的顶点时,我得到以下内容,每个圆括号为边,两个顶点构成:
face 1 - (0, 1) (1, 2) (2, 3) (0, 3)
face 2 - 4, 7) (6, 7) (5, 6) (4, 5)
face 3 - (0, 4) (4, 5) (1, 5) (0, 1)
face 4 - (1, 5) (5, 6) (2, 6) (1, 2)
face 5 - (2, 6) (6, 7) (3, 7) (2, 3)
face 6 - (0, 4) (0, 3) (3, 7) (4, 7)
基本上目标是有6行顶点以创建立方体,例如0,1,2,3将创建立方体的顶部4,5,6,7将创建底部。但是,我需要订单以顺时针方式从侧面的左上角开始,这是我卡住的地方。有人能告诉我如何做到这一点或举例说明吗?提前谢谢!
答案 0 :(得分:0)
好的,我不确定,但下面是一些我认为是你正在寻找的python代码。给定边缘,它会尝试将所有面部定位在一致的情况下。办法。请注意,给定面的方向是这样的:如果放置多维数据集以便只有该面可见,则顶点按顺时针顺序列出。
FACE_EDGES = [
[(0, 1), (1, 2), (2, 3), (0, 3)],
[(4, 7), (6, 7), (5, 6), (4, 5)],
[(0, 4), (4, 5), (1, 5), (0, 1)],
[(1, 5), (5, 6), (2, 6), (1, 2)],
[(2, 6), (6, 7), (3, 7), (2, 3)],
[(0, 4), (0, 3), (3, 7), (4, 7)]
]
# gets vertices of a face, but may be "clockwise" or
# "counterclockwise"
def get_vertices_from_edges(edges):
verts = [edges[0][0]]
for i in range(3):
for a, b in edges:
if a == verts[-1] and b not in verts:
verts.append(b)
break
if b == verts[-1] and a not in verts:
verts.append(a)
break
return verts
def get_face_vertices(face_edges):
face_verts = []
for edges in face_edges:
face_verts.append(get_vertices_from_edges(edges))
# orientations of vertices may not be correct yet
orient_order = range(6) # the order in which to fix orientations of faces
if face_verts[0][0] not in face_verts[1]:
# make sure the first two faces touch each other, so that all
# subsequent faces will share an edge with a previously
# oriented face
orient_order[1] = 2
orient_order[2] = 1
oriented_edges = set()
for face_index in orient_order:
verts = face_verts[face_index]
edges = zip(verts, verts[-1:] + verts[:-1])
# make it so that if face A and face B share an edge, they
# orient the shared edge in opposite ways
needs_reverse = False
for a, b in edges:
if (a, b) in oriented_edges:
needs_reverse = True
break
if needs_reverse:
verts.reverse()
edges = zip(verts, verts[-1:] + verts[:-1])
for a, b in edges:
oriented_edges.add((a, b))
return face_verts
print get_face_vertices(FACE_EDGES)
# [[0, 1, 2, 3], [4, 7, 6, 5], [0, 4, 5, 1], [1, 5, 6, 2], [2, 6, 7, 3], [3, 7, 4, 0]]