我有这组 3D 顶点。每条线保存 2 个不同点的坐标,因此每条线就像 x1,y1,z1,x2,y2,z2。
0.142000E+03 -.600000E+03 0.262790E+04 -.142000E+03 -.600000E+03 0.262790E+04
-.984000E+02 -.599999E+03 0.258430E+04 0.984000E+02 -.599999E+03 0.258430E+04
0.984000E+02 -.599999E+03 0.258430E+04 0.984000E+02 -.599999E+03 0.238750E+04
0.984000E+02 -.599999E+03 0.258430E+04 -.984000E+02 -.599999E+03 0.258430E+04
-.984000E+02 -.599999E+03 0.238750E+04 0.984000E+02 -.599999E+03 0.238750E+04
-.142000E+03 0.600000E+03 0.262800E+04 0.142000E+03 0.600000E+03 0.262800E+04
0.984000E+02 0.599999E+03 0.258430E+04 -.984000E+02 0.599999E+03 0.258430E+04
0.142000E+03 0.600000E+03 0.262800E+04 0.142000E+03 0.600000E+03 0.234400E+04
我想首先分离每个三元组的顶点,然后将所有三元组合并到一个列表 A 中。然后,我想从这个列表中创建一个由唯一三元组组成的列表 B,并为每个三元组提供一个索引。从那里,我想根据唯一的三元组列表 B 创建 A 中每个元素的索引列表 C。我本质上是想制作一个 .off 文件,其中索引组构成一张脸。
我在Python中尝试了这段代码:
import re
def takeNumFaces(filepath):
with open(filepath, 'r') as file:
lines = file.readlines()
second_line = lines[1].strip()
numbers = second_line.split()
faces = int(numbers[1])
return(faces)
def takeNumPoints(filepath):
with open(filepath, 'r') as file:
lines = file.readlines()
second_line = lines[1].strip()
numbers = second_line.split()
points = int(numbers[2])
return(points)
geofile = "C:/mygeofile.geo"
geofaces = takeNumFaces(geofile)
facepoints = takeNumPoints(geofile)
#print(geofaces, facepoints)
def gatherVertices(filepath):
vertices = []
unique_vert = []
index = 0
with open(filepath, 'r') as file:
lines = file.readlines()
for line in lines[2 : 2*geofaces + 2]:
parts = re.findall(r'[-0]\.\d+E[+-]\d{2}', line)
# print(parts)
for i in range(0, len(parts), 3):
vertex = [parts[i], parts[i+1], parts[i+2]]
#print(vertex)
vertices.append(vertex)
return vertices
vertices = gatherVertices(geofile)
#print(vertices)
unique_vertices = set(tuple(vertex) for vertex in vertices)
unique_vertices_list = [list(vertex) for vertex in unique_vertices]
# print(len(unique_vertices), len(vertices))
# for vertex in unique_vertices_list:
# print(vertex)
def getVertexIndices(vertices1, vertices2):
# Convert vertices2 to tuples (since unique_vertices_list is already list of lists)
unique_vertices_tuples = [tuple(vertex) for vertex in vertices2]
# Create a mapping of vertices to indices
vertex_to_index = {tuple(vertex): index for index, vertex in enumerate(unique_vertices_tuples)}
# print(vertex_to_index)
# Get the index of each vertex in vertices1 in vertices2
index_list = [vertex_to_index[tuple(vertex)] for vertex in vertices1]
return index_list
index_list = getVertexIndices(vertices, unique_vertices_list)
# for index in index_list:
# print(index)
# print(len(index_list))
# num = 2000
# if num in index_list:
# print(f"{num} is in the list")
# else:
# print(f"{num} is not in the list")
def groupFaces(indices):
faces = [indices[i:i+4] for i in range(0, len(indices), 4)]
return faces
face_list = groupFaces(index_list)
# for face in face_list:
# print(face)
off_file = "C:/myPrivateFilePath"
object_dimension = 3
groupsize = facepoints // object_dimension
def write_off(offpath):
with open(offpath, 'w') as file:
file.write("OFF\n")
file.write(f"{len(vertices)} {geofaces} 0\n")
for vertex in vertices:
file.write(" ".join(vertex) + "\n")
for face in face_list:
line = f"{groupsize} " + " ".join(map(str, face)) + "\n"
file.write(line)
write_off(off_file)
我设法制作了一个三元组列表,例如 [0.142000E+03, -.600000E+03, 0.262790E+04], [-.142000E+03, -.600000E+03, 0.262790E+04] 等。出于某种原因,我的索引被打乱了,我得到了一个以 :
开头的 index_list4 547 1181 1335 867
4 1181 2243 182 1335
4 2243 787 2275 182
4 787 547 867 2275
所以第一组甚至不是由前 4 个顶点三元组组成的。我应该获取顶点中每个元素(顶点三元组)的索引,然后将其索引(根据 unique_vertices_list)提供给index_list。
如何确保顶点三元组的索引按顺序出现(即顶点的元素 0 是唯一列表的元素 0,因此 index_list 的元素 0 是 0)?
看起来使用
set
来获取唯一的顶点是问题的核心。 set
未订购。这是另一个讨论这个问题的问题。答案中有很多您可能不需要的细节,但您可能可以从最上面的答案中获取代码来开始。