I have this set of 3D vertices. Each line holds the coordinates of 2 different points, so each line is like 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
I wanted to first separate each triplet of vertices then combine all triplets into a list A. Then from this list I wanted to create a list B of unique triplets and give each of them an index. From there, I wanted create a list C of indexes of each element in A according to the unique triplet list B. I essentially wanted to make a .off file with groups of indices making a face.
I tried this code in 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("OFFn")
file.write(f"{len(vertices)} {geofaces} 0n")
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)
I managed to make a list of triplets like [0.142000E+03, -.600000E+03, 0.262790E+04], [-.142000E+03, -.600000E+03, 0.262790E+04] etc. But for some reason my indices were shuffled and I got an index_list beginning with :
4 547 1181 1335 867
4 1181 2243 182 1335
4 2243 787 2275 182
4 787 547 867 2275
So the first group isn’t even made with the first 4 vertex triplets. I was supposed to grab the index of each element (vertex triplet) in vertices then give its index (according to unique_vertices_list) to index_list.
How do I make sure that the vertex triplets’ indices appear in order (i.e. element 0 of vertices is element 0 of unique list, therefore element 0 of index_list is 0)?