def cac_normal(v_np, f_np): V_igl = igl.eigen.MatrixXd(v_np.astype('float64')) F_igl = igl.eigen.MatrixXi(f_np.astype('intc')) VertexNormal = igl.eigen.MatrixXd() igl.per_vertex_normals(V_igl, F_igl, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA, VertexNormal) return np.array(VertexNormal)
def flip_energy(smov): ''' Compute energy associated to morph triangles deviating from the sphere's normal as an angle. The angle is 0 for perfect alignment, and pi for inverted triangles. ''' global Fmov morph_normals = igl.eigen.MatrixXd() igl.per_vertex_normals(smov, Fmov, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA, morph_normals) sphere_normals = smov.rowwiseNormalized() dot = np.einsum('ij,ij->i', e2p(morph_normals), e2p(sphere_normals)) ang = np.power(np.arccos(dot), 2) E = np.sum(ang) return E
def initialize(self): self.vertices = igl.eigen.MatrixXd() self.faces = igl.eigen.MatrixXi() try: if not igl.read_triangle_mesh(self.mesh_path, self.vertices, self.faces): print("failed to read mesh\n") except: traceback.print_exc(file=sys.stdout) sys.exit(-1) self.face_normals = igl.eigen.MatrixXd() igl.per_face_normals(self.vertices, self.faces, self.face_normals) self.vertex_normals = igl.eigen.MatrixXd() igl.per_vertex_normals(self.vertices, self.faces, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA, self.vertex_normals) self.vertex_data = e2p(self.vertices).astype(dtype=np.float32, order='C') self.index_data = e2p(self.faces).astype(dtype=np.uint32, order='C') self.face_normal_data = e2p(self.face_normals).astype(dtype=np.float32, order='C') self.vertex_normal_data = e2p(self.vertex_normals).astype( dtype=np.float32, order='C') self.num_faces = self.index_data.shape[0] self.num_vertices = self.vertex_data.shape[0] self.center = np.mean(self.vertex_data, axis=0) self.max_vals = np.max(self.vertex_data, axis=0) self.min_vals = np.min(self.vertex_data, axis=0) self.extents = self.max_vals - self.min_vals print("min = %s, max = %s, extents = %s" % (self.min_vals, self.max_vals, self.extents)) self.vertex_data = (self.vertex_data - self.center) / self.extents self.vertex_byte_count = ArrayDatatype.arrayByteCount(self.vertex_data) self.vertex_normal_byte_count = ArrayDatatype.arrayByteCount( self.vertex_normal_data) self.index_byte_count = ArrayDatatype.arrayByteCount(self.index_data)
elif key == ord('3'): viewer.data.set_normals(N_corners) return True return False # Load a mesh in OFF format igl.readOFF(TUTORIAL_SHARED_PATH + "fandisk.off", V, F) # Compute per-face normals N_faces = igl.eigen.MatrixXd() igl.per_face_normals(V, F, N_faces) # Compute per-vertex normals N_vertices = igl.eigen.MatrixXd() igl.per_vertex_normals(V, F, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA, N_vertices) # Compute per-corner normals, |dihedral angle| > 20 degrees --> crease N_corners = igl.eigen.MatrixXd() igl.per_corner_normals(V, F, 20, N_corners) # Plot the mesh viewer = igl.viewer.Viewer() viewer.callback_key_pressed = key_pressed viewer.core.show_lines = False viewer.data.set_mesh(V, F) viewer.data.set_normals(N_faces) print("Press '1' for per-face normals.") print("Press '2' for per-vertex normals.") print("Press '3' for per-corner normals.") viewer.launch()
elif key == ord('.'): viewer.core.lighting_factor += 0.1 elif key == ord(','): viewer.core.lighting_factor -= 0.1 else: return False viewer.core.lighting_factor = min(max(viewer.core.lighting_factor, 0.0), 1.0) return True print("Press 1 to turn off Ambient Occlusion\nPress 2 to turn on Ambient Occlusion\nPress . to turn up lighting\nPress , to turn down lighting") # Load a surface mesh igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F) # Calculate vertex normals igl.per_vertex_normals(V, F, N) # Compute ambient occlusion factor using embree igl.embree.ambient_occlusion(V, F, V, N, 500, AO) AO = 1.0 - AO # Plot the generated mesh viewer.data().set_mesh(V, F) key_down(viewer, ord('2'), 0) viewer.callback_key_down = key_down viewer.data().show_lines = False viewer.core.lighting_factor = 0.0 viewer.launch()
print("Press [space] to toggle showing surface.") print("Press '.'/',' to push back/pull forward slicing plane.") # Load mesh: (V,T) tet-mesh of convex hull, F contains original surface triangles igl.readMESH(TUTORIAL_SHARED_PATH + "bunny.mesh", V, T, F) # Call to point_mesh_squared_distance to determine bounds sqrD = igl.eigen.MatrixXd() I = igl.eigen.MatrixXi() C = igl.eigen.MatrixXd() igl.point_mesh_squared_distance(V, V, F, sqrD, I, C) max_distance = math.sqrt(sqrD.maxCoeff()) # Precompute signed distance AABB tree tree.init(V, F) # Precompute vertex, edge and face normals igl.per_face_normals(V, F, FN) igl.per_vertex_normals(V, F, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE, FN, VN) igl.per_edge_normals(V, F, igl.PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM, FN, EN, E, EMAP) # Plot the generated mesh update_visualization(viewer) viewer.callback_key_down = key_down viewer.data().show_lines = False viewer.launch()
viewer.core().lighting_factor -= 0.1 else: return False viewer.core().lighting_factor = min( max(viewer.core().lighting_factor, 0.0), 1.0) return True print( "Press 1 to turn off Ambient Occlusion\nPress 2 to turn on Ambient Occlusion\nPress . to turn up lighting\nPress , to turn down lighting" ) # Load a surface mesh igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F) # Calculate vertex normals igl.per_vertex_normals(V, F, N) # Compute ambient occlusion factor using embree igl.embree.ambient_occlusion(V, F, V, N, 500, AO) AO = 1.0 - AO # Plot the generated mesh viewer.data().set_mesh(V, F) key_down(viewer, ord('2'), 0) viewer.callback_key_down = key_down viewer.data().show_lines = False viewer.core().lighting_factor = 0.0 viewer.launch()
update_visualization(viewer) return True print("Press [space] to toggle showing surface.") print("Press '.'/',' to push back/pull forward slicing plane.") # Load mesh: (V,T) tet-mesh of convex hull, F contains original surface triangles igl.readMESH(TUTORIAL_SHARED_PATH + "bunny.mesh", V, T, F) # Call to point_mesh_squared_distance to determine bounds sqrD = igl.eigen.MatrixXd() I = igl.eigen.MatrixXi() C = igl.eigen.MatrixXd() igl.point_mesh_squared_distance(V, V, F, sqrD, I, C) max_distance = math.sqrt(sqrD.maxCoeff()) # Precompute signed distance AABB tree tree.init(V, F) # Precompute vertex, edge and face normals igl.per_face_normals(V, F, FN) igl.per_vertex_normals(V, F, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE, FN, VN) igl.per_edge_normals(V, F, igl.PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM, FN, EN, E, EMAP) # Plot the generated mesh update_visualization(viewer) viewer.callback_key_down = key_down viewer.core.show_lines = False viewer.launch()
print("mesh_path = %s\n" % mesh_path) vertices = igl.eigen.MatrixXd() faces = igl.eigen.MatrixXi() try: if not igl.read_triangle_mesh(mesh_path, vertices, faces): print("failed to read mesh\n") except: traceback.print_exc(file=sys.stdout) sys.exit(-1) face_normals = igl.eigen.MatrixXd() igl.per_face_normals(vertices, faces, face_normals) vertex_normals = igl.eigen.MatrixXd() igl.per_vertex_normals(vertices, faces, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA, vertex_normals) vertex_data = e2p(vertices).flatten('C').astype(dtype=np.float32, order='C') index_data = e2p(faces).flatten('C').astype(dtype=np.uint32, order='C') face_normal_data = e2p(face_normals).flatten('C').astype(dtype=np.float32, order='C') vertex_normal_data = e2p(vertex_normals).flatten('C').astype( dtype=np.float32, order='C') num_faces = len(index_data) / 3 num_vertices = len(vertex_data) / 3 print("#vertices = %d and #faces = %d" % (num_vertices, num_faces)) print("vertices =\n") for i in range(10):
v = igl.eigen.MatrixXd() f = igl.eigen.MatrixXi() fn = igl.eigen.MatrixXd() vn = igl.eigen.MatrixXd() doublearea = igl.eigen.MatrixXd() mesh_location = '../mesh_processing/data/bunny.obj' mesh = MESH() #mesh.v = igl.eigen.MatrixXd([[-1,-1,0.9],[1, -1, 1],[1, 1, 1.2],[-1, 1, 1],[-2, -2, 1], [2,1, 1]]) #mesh.f = igl.eigen.MatrixXd([[0,2,1],[0, 3,2], [4,3,0], [1,2,5]]).castint() read_file = igl.readOBJ(mesh_location, mesh.v, mesh.f) igl.per_face_normals(mesh.v, mesh.f, mesh.fn) igl.per_vertex_normals(mesh.v, mesh.f, mesh.vn) igl.doublearea(mesh.v, mesh.f, mesh.doublearea) mesh.fn = np.array(mesh.fn) mesh.vn = np.array(mesh.vn) f = 0.5 z = -2 sensor = np.array([f, 0, z]) lighting = np.array([-f, 0, z]) sensor_normal = np.array([0, 0, 1]) lighting_normal = np.array([0, 0, 1]) opt = OPT() opt.normal = 'n'
VertexNormal = igl.eigen.MatrixXd() write_full_obj( V, F, np.array([]), np.array([]), np.array([]), np.array([]), vtx_color, objdir + '/ori_color_crop' + '.obj', ) import os if not os.path.exists(objdir + '/Target_corr' + '.obj'): igl.per_vertex_normals(V_igl, F_igl, igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA, VertexNormal) # 1. 将裁减图转化到原图坐标,并设置为左下角为原点 write_full_obj( V, F, np.array([]), np.array([]), np.array([]), np.array([]), vtx_color, objdir + '/ori_color_crop' + '.obj', ) target_V = scaleToOriCoodi_bottomleft(V, BB, 192) write_full_obj( target_V,