Skip to content

Commit d25484a

Browse files
tomwardiosaran-t
authored andcommitted
Enable loading and processing OBJ meshes from memory.
Also remove dependency on modules from data_utils to make it more hermetic. PiperOrigin-RevId: 406120785
1 parent 5f6b112 commit d25484a

File tree

1 file changed

+52
-34
lines changed

1 file changed

+52
-34
lines changed

polygen/data_utils.py

+52-34
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
"""Mesh data utilities."""
1616
import matplotlib.pyplot as plt
17-
import modules
1817
from mpl_toolkits import mplot3d # pylint: disable=unused-import
1918
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
2019
import networkx as nx
@@ -88,53 +87,67 @@ def _face_model_map_fn(example):
8887
example['faces'] = tf.cast(
8988
tf.gather(face_permutation, example['faces']), tf.int64)
9089

90+
def _dequantize_verts(verts, n_bits):
91+
min_range = -0.5
92+
max_range = 0.5
93+
range_quantize = 2**n_bits - 1
94+
verts = tf.cast(verts, tf.float32)
95+
verts = verts * (max_range - min_range) / range_quantize + min_range
96+
return verts
97+
9198
# Vertices are quantized. So convert to floats for input to face model
92-
example['vertices'] = modules.dequantize_verts(vertices, quantization_bits)
99+
example['vertices'] = _dequantize_verts(vertices, quantization_bits)
93100
example['vertices_mask'] = tf.ones_like(
94101
example['vertices'][..., 0], dtype=tf.float32)
95102
example['faces_mask'] = tf.ones_like(example['faces'], dtype=tf.float32)
96103
return example
97104
return ds.map(_face_model_map_fn)
98105

99106

100-
def read_obj(obj_path):
101-
"""Read vertices and faces from .obj file."""
107+
def read_obj_file(obj_file):
108+
"""Read vertices and faces from already opened file."""
102109
vertex_list = []
103110
flat_vertices_list = []
104111
flat_vertices_indices = {}
105112
flat_triangles = []
106113

107-
with open(obj_path) as obj_file:
108-
for line in obj_file:
109-
tokens = line.split()
110-
if not tokens:
111-
continue
112-
line_type = tokens[0]
113-
# We skip lines not starting with v or f.
114-
if line_type == 'v':
115-
vertex_list.append([float(x) for x in tokens[1:]])
116-
elif line_type == 'f':
117-
triangle = []
118-
for i in range(len(tokens) - 1):
119-
vertex_name = tokens[i + 1]
120-
if vertex_name in flat_vertices_indices:
121-
triangle.append(flat_vertices_indices[vertex_name])
114+
for line in obj_file:
115+
tokens = line.split()
116+
if not tokens:
117+
continue
118+
line_type = tokens[0]
119+
# We skip lines not starting with v or f.
120+
if line_type == 'v':
121+
vertex_list.append([float(x) for x in tokens[1:]])
122+
elif line_type == 'f':
123+
triangle = []
124+
for i in range(len(tokens) - 1):
125+
vertex_name = tokens[i + 1]
126+
if vertex_name in flat_vertices_indices:
127+
triangle.append(flat_vertices_indices[vertex_name])
128+
continue
129+
flat_vertex = []
130+
for index in six.ensure_str(vertex_name).split('/'):
131+
if not index:
122132
continue
123-
flat_vertex = []
124-
for index in six.ensure_str(vertex_name).split('/'):
125-
if not index:
126-
continue
127-
# obj triangle indices are 1 indexed, so subtract 1 here.
128-
flat_vertex += vertex_list[int(index) - 1]
129-
flat_vertex_index = len(flat_vertices_list)
130-
flat_vertices_list.append(flat_vertex)
131-
flat_vertices_indices[vertex_name] = flat_vertex_index
132-
triangle.append(flat_vertex_index)
133-
flat_triangles.append(triangle)
133+
# obj triangle indices are 1 indexed, so subtract 1 here.
134+
flat_vertex += vertex_list[int(index) - 1]
135+
flat_vertex_index = len(flat_vertices_list)
136+
flat_vertices_list.append(flat_vertex)
137+
flat_vertices_indices[vertex_name] = flat_vertex_index
138+
triangle.append(flat_vertex_index)
139+
flat_triangles.append(triangle)
134140

135141
return np.array(flat_vertices_list, dtype=np.float32), flat_triangles
136142

137143

144+
def read_obj(obj_path):
145+
"""Open .obj file from the path provided and read vertices and faces."""
146+
147+
with open(obj_path) as obj_file:
148+
return read_obj_file(obj_file)
149+
150+
138151
def write_obj(vertices, faces, file_path, transpose=True, scale=1.):
139152
"""Write vertices and faces to obj."""
140153
if transpose:
@@ -286,10 +299,8 @@ def quantize_process_mesh(vertices, faces, tris=None, quantization_bits=8):
286299
return vertices, faces, tris
287300

288301

289-
def load_process_mesh(mesh_obj_path, quantization_bits=8):
290-
"""Load obj file and process."""
291-
# Load mesh
292-
vertices, faces = read_obj(mesh_obj_path)
302+
def process_mesh(vertices, faces, quantization_bits=8):
303+
"""Process mesh vertices and faces."""
293304

294305
# Transpose so that z-axis is vertical.
295306
vertices = vertices[:, [2, 0, 1]]
@@ -316,6 +327,13 @@ def load_process_mesh(mesh_obj_path, quantization_bits=8):
316327
}
317328

318329

330+
def load_process_mesh(mesh_obj_path, quantization_bits=8):
331+
"""Load obj file and process."""
332+
# Load mesh
333+
vertices, faces = read_obj(mesh_obj_path)
334+
return process_mesh(vertices, faces, quantization_bits)
335+
336+
319337
def plot_meshes(mesh_list,
320338
ax_lims=0.3,
321339
fig_size=4,

0 commit comments

Comments
 (0)