forked from ivyn/3d-mesh-reconstruction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualisation.py
53 lines (45 loc) · 1.32 KB
/
visualisation.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
# Written by Pransu Dash, Ryan Leung, and Ivy Nguyen
import numpy as np
import open3d as o3d
from bpa import BPA
from bpa import Triangle
if __name__ == "__main__":
print("Load a ply point cloud, print it, and render it")
pcd = o3d.io.read_point_cloud("madara.ply")
pcd.estimate_normals(fast_normal_computation=False)
pcd.normalize_normals()
pcd.orient_normals_to_align_with_direction()
b = BPA(pcd)
print('making mesh')
triangles = b.make_mesh()
f = open("madara_r_4_3.ply", "w")
vertices = []
v2index = {}
index_counter = 0
faces = []
for t in triangles:
out = []
for vertex in t.vertices:
v = tuple(vertex)
if v2index.get(v):
out.append(v2index[v])
else:
v2index[v] = index_counter
index_counter += 1
out.append(v2index[v])
vertices.append(v)
faces.append(out)
f.write('ply\n')
f.write('format ascii 1.0\n')
f.write('element vertex ' + str(len(vertices)) + '\n')
f.write('property float32 x\n')
f.write('property float32 y\n')
f.write('property float32 z\n')
f.write('element face ' + str(len(faces)) + '\n')
f.write('property list uint8 int32 vertex_indices\n')
f.write('end_header\n')
for v in vertices:
f.write(str(v[0]) + " " + str(v[1]) + " " + str(v[2]) + "\n")
for face in faces:
f.write('3 ' + str(face[0]) + " " + str(face[1]) + " " + str(face[2]) + "\n")
f.close()