How to create a list of indices for a list of 3D vertices in Python?

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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)
</code>
<code>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) </code>
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 :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>4 547 1181 1335 867
4 1181 2243 182 1335
4 2243 787 2275 182
4 787 547 867 2275
</code>
<code>4 547 1181 1335 867 4 1181 2243 182 1335 4 2243 787 2275 182 4 787 547 867 2275 </code>
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)?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật