forked from sudhir5595/Mesh_Neural_Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.py
70 lines (53 loc) · 2.73 KB
/
input.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
import numpy as np
import open3d as o3d
def find_neighbor(faces, faces_contain_this_vertex, vf1, vf2, except_face):
for i in (faces_contain_this_vertex[vf1] & faces_contain_this_vertex[vf2]):
if i != except_face:
face = faces[i].tolist()
face.remove(vf1)
face.remove(vf2)
return i
return except_face
def input_stl(path):
try:
triangular_mesh = o3d.io.read_triangle_mesh(path)
parameters = {}
parameters["normals"] = np.array(triangular_mesh.triangle_normals)
triangular_mesh.remove_duplicated_vertices()
parameters["vertices"] = np.array(triangular_mesh.vertices)
parameters["num_tri"] = int(parameters["normals"].shape[0])
parameters["triangles"] = np.array(triangular_mesh.triangles)
parameters["centroids"] = np.zeros((parameters["num_tri"],3))
parameters["corners"] = np.zeros((parameters["num_tri"], 9))
faces_contain_this_vertex = []
for i in range(parameters["vertices"].shape[0]):
faces_contain_this_vertex.append(set([]))
for i in range(parameters["num_tri"]):
[v1, v2, v3] = parameters["triangles"][i]
x1, y1, z1 = parameters["vertices"][v1]
x2, y2, z2 = parameters["vertices"][v2]
x3, y3, z3 = parameters["vertices"][v3]
Gx = (x1 + x2 + x3) / 3
Gy = (y1 + y2 + y3) / 3
Gz = (z1 + z2 + z3) / 3
parameters["centroids"][i] = [Gx, Gy, Gz]
c1 = parameters["vertices"][v1] - parameters["centroids"][i]
c2 = parameters["vertices"][v2] - parameters["centroids"][i]
c3 = parameters["vertices"][v3] - parameters["centroids"][i]
parameters['corners'][i] = np.concatenate((c1, c2, c3))
faces_contain_this_vertex[v1].add(i)
faces_contain_this_vertex[v2].add(i)
faces_contain_this_vertex[v3].add(i)
parameters["neigh_index"] = []
for i in range(parameters['num_tri']):
[v1, v2, v3] = parameters["triangles"][i]
n1 = find_neighbor(parameters["triangles"], faces_contain_this_vertex, v1, v2, i)
n2 = find_neighbor(parameters["triangles"], faces_contain_this_vertex, v2, v3, i)
n3 = find_neighbor(parameters["triangles"], faces_contain_this_vertex, v3, v1, i)
parameters["neigh_index"].append([n1, n2, n3])
parameters["neigh_index"] = np.array(parameters["neigh_index"])
#parameters["neigh_index"].sort(axis = 1)
return parameters
except:
return False
#o = input_stl('/home/prathmesh/Desktop/SoC-2020/ModelNet10_stl/ModelNet10/bathtub/train/bathtub_0096.stl')