-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_mesh.py
38 lines (29 loc) · 1.22 KB
/
visualize_mesh.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
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
def read_obj(filename):
vertices = []
faces = []
with open(filename, 'r') as file:
for line in file:
if line.startswith('v '):
parts = line.split()
vertices.append([float(parts[1]), float(parts[2]), float(parts[3])])
elif line.startswith('f '):
parts = line.split()
faces.append([int(parts[1])-1, int(parts[2])-1, int(parts[3])-1])
return vertices, faces
def plot_mesh(vertices, faces):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
mesh = Poly3DCollection([[vertices[idx] for idx in face] for face in faces], alpha=0.5, edgecolor='k')
ax.add_collection3d(mesh)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.auto_scale_xyz([min(v[0] for v in vertices), max(v[0] for v in vertices)],
[min(v[1] for v in vertices), max(v[1] for v in vertices)],
[min(v[2] for v in vertices), max(v[2] for v in vertices)])
plt.show()
if __name__ == '__main__':
vertices, faces = read_obj('data/output_mesh.obj')
plot_mesh(vertices, faces)