def save_mc_simplify_obj(outdir, catname, modname, pred, gt, holefill=True): #save pred.obj outdir_cat = os.path.join(outdir, catname) if not os.path.exists(outdir_cat): os.mkdir(outdir_cat) objfile = os.path.join(outdir_cat, modname+'.obj') if holefill: shape_layers_cpu, pred = holefill_cpu(pred.cpu().numpy().astype('bool')) voxels = pred.astype('bool') else: voxels = pred.cpu().numpy().astype('bool') n_x, n_y, n_z = voxels.shape voxels = np.pad(voxels, (1, 1), 'constant', constant_values=(0, 0)) vertices, triangles = libmcubes.marching_cubes(voxels, 0.5) vertices = (vertices-0.5)/ n_x - 0.5 mesh = trimesh.Trimesh(vertices, triangles, vertex_normals=None, process=False) if len(triangles)>200000: nfaces = int(0.05 * len(triangles)) elif len(triangles)>=100000 and len(triangles)<200000: nfaces = int(0.1 * len(triangles)) else: nfaces = int(0.15 * len(triangles)) mesh = libsimplify.simplify_mesh(mesh, nfaces) if len(mesh.faces>20000): mesh = libsimplify.simplify_mesh(mesh, 20000) mesh.export(objfile) print('holefill is ', holefill,objfile)
def run_fuse(self): """ Run fusion. """ assert os.path.exists(self.options.depth_dir) common.makedir(self.options.out_dir) files = self.read_directory(self.options.depth_dir) timer = common.Timer() Rs = self.get_views() for filepath in files: # As rendering might be slower, we wait for rendering to finish. # This allows to run rendering and fusing in parallel (more or less). depths = common.read_hdf5(filepath) timer.reset() tsdf = self.fusion(depths, Rs) tsdf = tsdf[0] vertices, triangles = libmcubes.marching_cubes(-tsdf, 0) vertices /= self.options.resolution vertices -= 0.5 tsdf_file = os.path.join(self.options.tsdf_dir, '0.hf5') off_file = os.path.join(self.options.out_dir, ntpath.basename(filepath)[:-3]) common.write_hdf5(tsdf_file, tsdf) libmcubes.export_off(vertices, triangles, off_file) print('[Data] wrote %s (%f seconds)' % (off_file, timer.elapsed()))
def save_volume_obj(outdir, catname, modname, pred, gt, holefill=True, save_gt_obj=False): #save pred.obj outdir_cat = os.path.join(outdir, catname) if not os.path.exists(outdir_cat): os.mkdir(outdir_cat) if holefill: t0 = time.time() print('fill hole before', pred.sum().item(), pred.shape) shape_layers_cpu, pred = holefill_cpu(pred.cpu().numpy().astype('bool')) shape_layers_cpu = torch.from_numpy(shape_layers_cpu.astype(np.int32)).to(torch.int16) print('fill hole cpu after', pred.sum(), time.time()-t0) print('fill hole cpu consuem %f s'%(time.time()-t0)) voxels = pred.astype('bool') else: voxels = pred.cpu().numpy().astype('bool') n_x, n_y, n_z = voxels.shape voxels = np.pad(voxels, (1, 1), 'constant', constant_values=(0, 0)) objfile = os.path.join(outdir_cat, modname+'_pred.obj') vertices, triangles = libmcubes.marching_cubes(voxels, 0.5) vertices = (vertices-0.5)/ n_x - 0.5 mesh = trimesh.Trimesh(vertices, triangles, vertex_normals=None, process=False) #mesh.export(objfile) objfile = os.path.join(outdir_cat, modname+'_pred_simplify.obj') mesh = libsimplify.simplify_mesh(mesh, 10000) mesh.export(objfile) if save_gt_obj: #save gt.obj objfile = os.path.join(outdir_cat, modname+'_gt.obj') voxels = gt.cpu().numpy().astype('bool') n_x, n_y, n_z = voxels.shape voxels = np.pad(voxels, (1, 1), 'constant', constant_values=(0, 0)) vertices, triangles = libmcubes.marching_cubes(voxels, 0.5) vertices = (vertices-0.5)/ n_x - 0.5 mesh = trimesh.Trimesh(vertices, triangles, vertex_normals=None, process=False) #mesh.export(objfile) objfile = os.path.join(outdir_cat, modname+'_gt_simplify.obj') mesh = libsimplify.simplify_mesh(mesh, 10000) mesh.export(objfile)