forked from Conchylicultor/DeepLearningOnGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
147 lines (115 loc) · 4.23 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Some utilities functions used in multiple scripts
Use python 3 (but should be working with python 2)
"""
import os, sys
import re
import numpy as np
def extractMesh(meshFilename):
"""
Extract the mesh informations from the file
Args:
meshFilename: path of the mesh file (.off format only !!)
Returns:
vertices: Array of the x,y,z position of each mesh points
faces: Array of the vertex indexes of each triangle
"""
# Open the file
print('Open file ', meshFilename)
meshFile = open(meshFilename, 'r')
lines = meshFile.readlines()
meshFile.close()
# Initialisation and global information
meshCount = lines[1].split()
vertexCount = int(meshCount[0])
faceCount = int(meshCount[1])
edgeCount = int(meshCount[2])
print('Mesh: ', vertexCount, ' vertices, ', faceCount, ' faces, ', edgeCount, ' edges')
vertices = []
faces = []
# For each line of the file
for line in lines[2:]: # Skip the first two lines (OFF and number of vertices)
words = line.split()
if len(words) == 3: # Read points
# Save each point coordinates in an array
vertices.append([float(words[0]), float(words[1]), float(words[2])])
elif len(words) == 4: # Read triangles >> vertex
faces.append([int(words[1]), int(words[2]), int(words[3])])
if len(vertices) != vertexCount:
print('Error: Number of vertices does not matches')
if len(faces) != faceCount:
print('Error: Number of faces does not matches')
return vertices, faces
def meshToPointCloud(meshVertices, meshFaces):
"""
Compute the point clouds informations from the mesh informations
Args:
vertices: Array of the x,y,z position of each mesh points
faces: Array of the vertex indexes of each triangle
Returns:
A point cloud (list of coordinates)
"""
Xin = np.zeros((len(meshFaces), 3))
for i, face in enumerate(meshFaces):
# Compute the centroid of the triangle
A = np.array(meshVertices[face[0]])
B = np.array(meshVertices[face[1]])
C = np.array(meshVertices[face[2]]) # Triangle coordinates
center = (A + B + C)/3
Xin[i,:] = center
return Xin
def sortFiles(filesList, startFrom=None):
"""
Generate an ordered list of files to compute
Args:
filesList: The file list to sort (will be modified)
startFrom: To start from a particular file (avoid recomputing the beginning each times)
Returns:
The sorted list
"""
filesList = naturalSort(filesList) # Alphabetical order
startIndex = 0
if startFrom != None:
startIndex = filesList.index(startFrom) # Raise an exception if not found
return filesList[startIndex:]
def naturalSort(l):
"""
Return the list sorted correctly (take into account the numerical values
instead of just the alphabetical order)
Args:
l: The file list to sort (will be modified)
Returns:
The sorted list
"""
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key = alphanum_key)
def loadLabelList(filename):
"""
Load the labels informations from the segmentation file
Args:
filename: path of the label file
Returns:
Array of the label of each node
"""
assert filename.endswith('.seg') or filename.endswith('.txt'), 'Wrong file format'
labelList = []
labelsFile = open(filename, 'r')
lines = labelsFile.readlines()
labelsFile.close()
# Extract rows
for line in lines:
labelList.append(int(line))
return np.asarray(labelList)
def saveLabelList(labelList, filename):
"""
Save the labels informations on a seg file (or signal file)
Args:
labelsFilename: Array of the label of each node
filename: path of the label file
"""
assert filename.endswith('.seg') or filename.endswith('.txt'), 'Wrong file format'
segFile = open(filename, "w")
for label in labelList:
segFile.write('%d\n' % label)
segFile.close()