def update_visualization(viewer): global V, F, T, tree, FN, VN, EN, E, EMAP, max_distance, slice_z, overlay plane = igl.eigen.MatrixXd([ 0.0, 0.0, 1.0, -((1 - slice_z) * V.col(2).minCoeff() + slice_z * V.col(2).maxCoeff()) ]) V_vis = igl.eigen.MatrixXd() F_vis = igl.eigen.MatrixXi() # Extract triangle mesh slice through volume mesh and subdivide nasty triangles J = igl.eigen.MatrixXi() bary = igl.eigen.SparseMatrixd() igl.marching_tets(V, T, plane, V_vis, F_vis, J, bary) max_l = 0.03 while True: l = igl.eigen.MatrixXd() igl.edge_lengths(V_vis, F_vis, l) l /= (V_vis.colwiseMaxCoeff() - V_vis.colwiseMinCoeff()).norm() if l.maxCoeff() < max_l: break bad = l.rowwiseMaxCoeff() > max_l notbad = l.rowwiseMaxCoeff() <= max_l # TODO replace by ~ operator F_vis_bad = igl.eigen.MatrixXi() F_vis_good = igl.eigen.MatrixXi() igl.slice_mask(F_vis, bad, 1, F_vis_bad) igl.slice_mask(F_vis, notbad, 1, F_vis_good) igl.upsample(V_vis, F_vis_bad) F_vis = igl.cat(1, F_vis_bad, F_vis_good) # Compute signed distance S_vis = igl.eigen.MatrixXd() I = igl.eigen.MatrixXi() N = igl.eigen.MatrixXd() C = igl.eigen.MatrixXd() # Bunny is a watertight mesh so use pseudonormal for signing igl.signed_distance_pseudonormal(V_vis, V, F, tree, FN, VN, EN, EMAP, S_vis, I, C, N) # push to [0,1] range S_vis = 0.5 * (S_vis / max_distance) + 0.5 C_vis = igl.eigen.MatrixXd() # color without normalizing igl.parula(S_vis, False, C_vis) if overlay: append_mesh(C_vis, F_vis, V_vis, V, F, igl.eigen.MatrixXd([[0.8, 0.8, 0.8]])) viewer.data().clear() viewer.data().set_mesh(V_vis, F_vis) viewer.data().set_colors(C_vis) viewer.core.lighting_factor = overlay
def update_visualization(viewer): global V, F, T, tree, FN, VN, EN, E, EMAP, max_distance, slice_z, overlay plane = igl.eigen.MatrixXd([0.0, 0.0, 1.0, -((1 - slice_z) * V.col(2).minCoeff() + slice_z * V.col(2).maxCoeff())]) V_vis = igl.eigen.MatrixXd() F_vis = igl.eigen.MatrixXi() # Extract triangle mesh slice through volume mesh and subdivide nasty triangles J = igl.eigen.MatrixXi() bary = igl.eigen.SparseMatrixd() igl.slice_tets(V, T, plane, V_vis, F_vis, J, bary) max_l = 0.03 while True: l = igl.eigen.MatrixXd() igl.edge_lengths(V_vis, F_vis, l) l /= (V_vis.colwiseMaxCoeff() - V_vis.colwiseMinCoeff()).norm() if l.maxCoeff() < max_l: break bad = l.rowwiseMaxCoeff() > max_l notbad = l.rowwiseMaxCoeff() <= max_l # TODO replace by ~ operator F_vis_bad = igl.eigen.MatrixXi() F_vis_good = igl.eigen.MatrixXi() igl.slice_mask(F_vis, bad, 1, F_vis_bad) igl.slice_mask(F_vis, notbad, 1, F_vis_good) igl.upsample(V_vis, F_vis_bad) F_vis = igl.cat(1, F_vis_bad, F_vis_good) # Compute signed distance S_vis = igl.eigen.MatrixXd() I = igl.eigen.MatrixXi() N = igl.eigen.MatrixXd() C = igl.eigen.MatrixXd() # Bunny is a watertight mesh so use pseudonormal for signing igl.signed_distance_pseudonormal(V_vis, V, F, tree, FN, VN, EN, EMAP, S_vis, I, C, N) # push to [0,1] range S_vis = 0.5 * (S_vis / max_distance) + 0.5 C_vis = igl.eigen.MatrixXd() # color without normalizing igl.parula(S_vis, False, C_vis) if overlay: append_mesh(C_vis, F_vis, V_vis, V, F, igl.eigen.MatrixXd([[0.8, 0.8, 0.8]])) viewer.data.clear() viewer.data.set_mesh(V_vis, F_vis) viewer.data.set_colors(C_vis) viewer.core.lighting_factor = overlay
def reconstruct_npz(inname, outname): """ Recontruct a 3D shape by deforming a template :param inname: input path :return: None (but save reconstruction) """ if os.path.exists(outname): return with np.load(inname) as npl: V, F = npl['V'], npl['F'] V = pca_whiten(V) max_axis = np.argmax((np.max(V, axis=0) - np.min(V, axis=0))) V = V[:, np.roll(np.arange(3), 1 - max_axis)] # 1 means Y V *= 1.7 assert (np.max(V, axis=0) - np.min(V, axis=0))[1] > 1.69 while V.shape[0] < 1e4: eV, eF = p2e(V), p2e(F) NV, NF = igl.eigen.MatrixXd(), igl.eigen.MatrixXi() igl.upsample(eV, eF, NV, NF) V, F = e2p(NV), e2p(NF) input = trimesh.Trimesh(vertices=V, faces=F, process=False) scalefactor = 1.0 if global_variables.opt.scale: input, scalefactor = scale( input, global_variables.mesh_ref_LR ) #scale input to have the same volume as mesh_ref_LR if global_variables.opt.clean: input = clean(input) #remove points that doesn't belong to any edges test_orientation(input) inp_V = input.vertices if inp_V.shape[0] > 1e5: inp_V = inp_V[ np.random.choice(inp_V.shape[0], int(1e5), replace=False), :] final_points, final_loss = run(inp_V, scalefactor) npz_path = os.path.dirname(outname) if not os.path.exists(npz_path): os.makedirs(npz_path) np.savez(outname, V=final_points, l=final_loss)
def upsample(mesh): v = igl.eigen.MatrixXd(mesh.v.astype(np.double)) f = igl.eigen.MatrixXi(mesh.f) igl.upsample(v, f) mesh.v = np.array(v, dtype=np.float32, order='C') mesh.f = np.array(f, dtype=np.int32, order='C')