-
Notifications
You must be signed in to change notification settings - Fork 3
/
read_gt_mesh.py
76 lines (64 loc) · 2.31 KB
/
read_gt_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
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
import os
import numpy as np
from collections import defaultdict
def load_obj(obj_file):
"""
Load vertices and faces from an OBJ file.
Args:
obj_file (str): Path to the OBJ file.
Returns:
verts (np.ndarray): Array of vertex coordinates.
faces (np.ndarray): Array of face indices.
"""
verts = []
faces = []
with open(obj_file, 'r') as f:
for line in f:
if line.startswith('v '):
# Vertex line
vert = [float(x) for x in line.split()[1:4]]
verts.append(vert)
elif line.startswith('f '):
# Face line
face_indices = line.split()[1:4]
face = []
for index in face_indices:
vertex_index = int(index.split('/')[0]) - 1
face.append(vertex_index)
faces.append(face)
verts = np.array(verts)
faces = np.array(faces)
return verts, faces
def load_obj_mtl(obj_file, mtl_file):
# Load the OBJ file
vertices = []
faces = []
with open(obj_file, 'r') as f:
for line in f:
if line.startswith('v '):
vertices.append([float(x) for x in line.split()[1:4]])
elif line.startswith('f '):
faces.append([int(x) - 1 for x in line.split()[1:4]])
# Load the MTL file
materials = defaultdict(lambda: {'Kd': [1, 1, 1]})
current_material = None
with open(mtl_file, 'r') as f:
for line in f:
if line.startswith('newmtl '):
current_material = line.split()[1]
elif line.startswith('Kd '):
materials[current_material]['Kd'] = [float(x) for x in line.split()[1:4]]
# Combine vertices and materials
vertex_colors = []
for face in faces:
material = materials[current_material]
vertex_colors.extend([material['Kd']] * 3)
vertices = np.array(vertices)
faces = np.array(faces)
vertex_colors = np.array(vertex_colors)
return vertices, faces, vertex_colors
# Example usage
obj_file = '/data3/zhangshuai/DG-Mesh/data/dg-mesh/bird/mesh_gt/bluebird_animated0.obj'
mtl_file = '/data3/zhangshuai/DG-Mesh/data/dg-mesh/bird/mesh_gt/bluebird_animated0.mtl'
vertices, faces = load_obj(obj_file)
print(vertices)