Skip to content

Commit c07c8cf

Browse files
committed
Refactor OBJ loader
For #102
1 parent 8c9ac0a commit c07c8cf

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

lacecore/_obj/loader.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,42 @@ def create_reader_and_config():
5656
return reader, config
5757

5858

59+
def _get_arity(shapes):
60+
if len(shapes) == 0:
61+
return 3
62+
63+
all_vertices_per_face = np.concatenate(
64+
[shape.mesh.numpy_num_face_vertices() for shape in shapes]
65+
)
66+
first_arity = all_vertices_per_face[0]
67+
if np.any(all_vertices_per_face < 3) or np.any(all_vertices_per_face > 4):
68+
raise ArityException(
69+
"OBJ Loader does not support arities greater than 4 or less than 3"
70+
)
71+
is_mixed_arity = ~np.all(all_vertices_per_face == first_arity)
72+
return tuple(all_vertices_per_face) if is_mixed_arity else all_vertices_per_face[0]
73+
74+
5975
def _finalize(reader, triangulate):
60-
attrib = reader.GetAttrib()
6176
shapes = reader.GetShapes()
62-
tinyobj_vertices = attrib.numpy_vertices().reshape(-1, 3)
63-
if len(shapes) > 0:
64-
all_vertices_per_face = np.concatenate(
65-
[shape.mesh.numpy_num_face_vertices() for shape in shapes]
66-
)
67-
first_arity = all_vertices_per_face[0]
68-
if np.any(all_vertices_per_face < 3) or np.any(all_vertices_per_face > 4):
69-
raise ArityException(
70-
"OBJ Loader does not support arities greater than 4 or less than 3"
71-
)
72-
is_mixed_arity = np.any(all_vertices_per_face != first_arity)
73-
all_faces = np.zeros((0, 3 if triangulate else first_arity), dtype=FACE_DTYPE)
74-
else:
75-
all_faces = np.zeros((0, 3), dtype=FACE_DTYPE)
7677

78+
arity = _get_arity(shapes)
79+
is_mixed_arity = isinstance(arity, tuple)
80+
first_arity = arity[0] if is_mixed_arity else arity
81+
82+
all_faces = np.zeros((0, 3 if triangulate else first_arity), dtype=FACE_DTYPE)
7783
segm = OrderedDict()
7884

7985
for shape in shapes:
80-
tinyobj_all_indices = shape.mesh.numpy_indices().reshape(-1, 3)[:, 0]
86+
these_face_indices = shape.mesh.numpy_indices().reshape(-1, 3)[:, 0]
8187
if is_mixed_arity and not triangulate:
8288
raise ArityException(
8389
"OBJ Loader does not support mixed arities with triangulate=False"
8490
)
8591
elif is_mixed_arity:
8692
these_vertices_per_face = shape.mesh.numpy_num_face_vertices()
8793
these_faces = np.zeros((0, 3), dtype=FACE_DTYPE)
88-
for this_face in unstack(tinyobj_all_indices, these_vertices_per_face):
94+
for this_face in unstack(these_face_indices, these_vertices_per_face):
8995
if len(this_face) == 3:
9096
these_faces = np.concatenate([these_faces, this_face.reshape(1, 3)])
9197
else:
@@ -97,7 +103,7 @@ def _finalize(reader, triangulate):
97103
]
98104
)
99105
else:
100-
these_faces = tinyobj_all_indices.reshape(-1, first_arity)
106+
these_faces = these_face_indices.reshape(-1, first_arity)
101107
if triangulate and first_arity == 4:
102108
# Triangulate ABCD as ABC + ACD.
103109
these_faces = these_faces[:, [[0, 1, 2], [0, 2, 3]]].reshape(-1, 3)
@@ -114,7 +120,12 @@ def _finalize(reader, triangulate):
114120
segm[name] = segm[name] + these_face_indices
115121

116122
group_map = GroupMap.from_dict(segm, len(all_faces))
117-
return Mesh(v=tinyobj_vertices, f=all_faces, face_groups=group_map)
123+
124+
return Mesh(
125+
v=reader.GetAttrib().numpy_vertices().reshape(-1, 3),
126+
f=all_faces,
127+
face_groups=group_map,
128+
)
118129

119130

120131
def load(mesh_path, triangulate=False):

0 commit comments

Comments
 (0)