14
14
15
15
"""Mesh data utilities."""
16
16
import matplotlib .pyplot as plt
17
- import modules
18
17
from mpl_toolkits import mplot3d # pylint: disable=unused-import
19
18
from mpl_toolkits .mplot3d .art3d import Poly3DCollection
20
19
import networkx as nx
@@ -88,53 +87,67 @@ def _face_model_map_fn(example):
88
87
example ['faces' ] = tf .cast (
89
88
tf .gather (face_permutation , example ['faces' ]), tf .int64 )
90
89
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
+
91
98
# 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 )
93
100
example ['vertices_mask' ] = tf .ones_like (
94
101
example ['vertices' ][..., 0 ], dtype = tf .float32 )
95
102
example ['faces_mask' ] = tf .ones_like (example ['faces' ], dtype = tf .float32 )
96
103
return example
97
104
return ds .map (_face_model_map_fn )
98
105
99
106
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."""
102
109
vertex_list = []
103
110
flat_vertices_list = []
104
111
flat_vertices_indices = {}
105
112
flat_triangles = []
106
113
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 :
122
132
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 )
134
140
135
141
return np .array (flat_vertices_list , dtype = np .float32 ), flat_triangles
136
142
137
143
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
+
138
151
def write_obj (vertices , faces , file_path , transpose = True , scale = 1. ):
139
152
"""Write vertices and faces to obj."""
140
153
if transpose :
@@ -286,10 +299,8 @@ def quantize_process_mesh(vertices, faces, tris=None, quantization_bits=8):
286
299
return vertices , faces , tris
287
300
288
301
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."""
293
304
294
305
# Transpose so that z-axis is vertical.
295
306
vertices = vertices [:, [2 , 0 , 1 ]]
@@ -316,6 +327,13 @@ def load_process_mesh(mesh_obj_path, quantization_bits=8):
316
327
}
317
328
318
329
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
+
319
337
def plot_meshes (mesh_list ,
320
338
ax_lims = 0.3 ,
321
339
fig_size = 4 ,
0 commit comments