[0, 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0], [3, 1, 0], [0, 1, 0], [0, 0.5, 0], [0, 0.25, 0], [4, 0, 0], [4, 1, 0], ] faces = [[0, 1, 2, 3, 4, 5, 6, 7], [3, 8, 9, 4]] sources = [1, 2, 6, 7] mesh = Mesh.from_vertices_and_faces(vertices, faces) #quadrangulate_face(mesh, 0, sources) quadrangulate_mesh(mesh, sources) # for vkey in mesh.vertices(): # print 'vkey', vkey, mesh.vertex_faces(vkey) # for fkey in mesh.faces(): # print 'fkey', fkey, mesh.face_vertices(fkey) plotter = MeshPlotter(mesh) plotter.draw_vertices(text='key') plotter.draw_edges() plotter.draw_faces(text='key') plotter.show()
def from_skeleton(cls, lines, radius=1): network = Network.from_lines(lines) tube_extremities = {} nodes = [] for vkey in network.vertices(): if len(network.vertex_neighbors(vkey)) > 1: points = [ network.edge_point(vkey, nbr, t=float(radius) / network.edge_length(vkey, nbr)) for nbr in network.vertex_neighbors(vkey) ] faces = convex_hull(points) mesh = cls.from_vertices_and_faces(points, faces) meshes = [] for fkey in mesh.faces(): vertices = [ mesh.edge_midpoint(u, v) for u, v in mesh.face_halfedges(fkey) ] faces = [[0, 1, 2]] meshes.append(cls.from_vertices_and_faces(vertices, faces)) for vkey_2 in mesh.vertices(): tops = [] bottoms = [] n = normalize_vector( subtract_vectors(mesh.vertex_coordinates(vkey_2), network.vertex_coordinates(vkey))) for i in range(len(mesh.vertex_neighbors(vkey_2))): pt_0 = mesh.edge_midpoint( vkey_2, mesh.vertex_neighbors(vkey_2, ordered=True)[i - 1]) bottoms.append(pt_0) pt_1 = mesh.edge_midpoint( vkey_2, mesh.vertex_neighbors(vkey_2, ordered=True)[i]) pt_2 = midpoint_line([pt_0, pt_1]) pt_2 = add_vectors( scale_vector(n, distance_point_point(pt_0, pt_1)), pt_2) tops.append(pt_2) vertices = [pt_0, pt_2, pt_1] faces = [[0, 1, 2]] meshes.append( cls.from_vertices_and_faces(vertices, faces)) for i in range(len(tops)): vertices = [tops[i - 1], tops[i], bottoms[i]] faces = [[0, 1, 2]] meshes.append( cls.from_vertices_and_faces(vertices, faces)) #print network.vertex_neighbors(vkey), network.vertex_neighbors(vkey)[vkey_2] tube_extremities[( vkey, network.vertex_neighbors(vkey)[vkey_2])] = tops mesh = meshes_join_and_weld(meshes) #dense_mesh = trimesh_subdivide_loop(mesh, k = 3) nodes.append(mesh) return nodes[0] meshes_2 = [] for u, v in network.edges(): if len(network.vertex_neighbors(u)) > 1 and len( network.vertex_neighbors(v)) > 1: #print len(tube_extremities[(u, v)]) #print len(tube_extremities[(v, u)]) if len(tube_extremities[(u, v)]) == len(tube_extremities[(v, u)]): n = len(tube_extremities[(u, v)]) l = network.edge_length(u, v) - 2 * radius m = math.floor(l / radius) + 1 pt_uv = tube_extremities[(u, v)] pt_vu = list(reversed(tube_extremities[(v, u)])) dmin = -1 imin = None for i in range(n): distance = sum([ distance_point_point(pt_uv[j], pt_vu[i + j - len(pt_vu)]) for j in range(n) ]) if dmin < 0 or distance < dmin: dmin = distance imin = i pt_vu = [pt_vu[imin + j - len(pt_vu)] for j in range(n)] array = [pt_uv] for i in range(int(m)): polygon = [] for j in range(int(n)): u = pt_uv[j] v = pt_vu[j] polygon.append( add_vectors( scale_vector(u, (float(m) - 1 - float(i)) / float(m - 1)), scale_vector(v, float(i) / float(m - 1)))) array.append(polygon) array.append(pt_vu) #print len(array), len(array[0]), len(array[1]), len(array[2]), len(array[3]) for i in range(int(n)): for j in range(int(m)): vertices = [ array[i - 1][j - 1], array[i - 1][j], array[i][j] ] faces = [[0, 1, 2]] meshes_2.append( Mesh.from_vertices_and_faces(vertices, faces)) vertices, faces = join_and_weld_meshes(meshes_2) #meshes_2 = rs.AddMesh(vertices, faces) meshes = [] for node in nodes: vertices, faces = node.to_vertices_and_faces() meshes.append(rs.AddMesh(vertices, faces))
def from_quad_mesh(cls, quad_mesh, collect_strips=True, collect_polyedges=True, attribute_density=True): """Build coarse quad mesh from quad mesh with density and child-parent element data. Parameters ---------- quad_mesh : QuadMesh A quad mesh. attribute_density : bool, optional Keep density data of dense quad mesh and inherit it as aatribute. Returns ---------- coarse_quad_mesh : CoarseQuadMesh A coarse quad mesh with density data. """ polyedges = quad_mesh.singularity_polyedge_decomposition() # vertex data vertices = { vkey: quad_mesh.vertex_coordinates(vkey) for vkey in quad_mesh.vertices() } coarse_vertices_children = { vkey: vkey for polyedge in polyedges for vkey in [polyedge[0], polyedge[-1]] } coarse_vertices = { vkey: quad_mesh.vertex_coordinates(vkey) for vkey in coarse_vertices_children } # edge data coarse_edges_children = {(polyedge[0], polyedge[-1]): polyedge for polyedge in polyedges} singularity_edges = [(x, y) for polyedge in polyedges for u, v in pairwise(polyedge) for x, y in [(u, v), (v, u)]] # face data faces = { fkey: quad_mesh.face_vertices(fkey) for fkey in quad_mesh.faces() } adj_edges = {(f1, f2) for f1 in quad_mesh.faces() for f2 in quad_mesh.face_neighbors(f1) if f1 < f2 and quad_mesh.face_adjacency_halfedge(f1, f2) not in singularity_edges} coarse_faces_children = {} for i, connected_faces in enumerate( connected_components(adjacency_from_edges(adj_edges))): mesh = Mesh.from_vertices_and_faces( vertices, [faces[face] for face in connected_faces]) coarse_faces_children[i] = [ vkey for vkey in reversed(mesh.boundaries()[0]) if mesh.vertex_degree(vkey) == 2 ] coarse_quad_mesh = cls.from_vertices_and_faces(coarse_vertices, coarse_faces_children) # attribute relation child-parent element between coarse and dense quad meshes coarse_quad_mesh.data['attributes'][ 'vertex_coarse_to_dense'] = coarse_vertices_children coarse_quad_mesh.data['attributes']['edge_coarse_to_dense'] = { u: {} for u in coarse_quad_mesh.vertices() } for (u, v), polyedge in coarse_edges_children.items(): coarse_quad_mesh.data['attributes']['edge_coarse_to_dense'][u][ v] = polyedge coarse_quad_mesh.data['attributes']['edge_coarse_to_dense'][v][ u] = list(reversed(polyedge)) # collect strip and polyedge attributes if collect_strips: coarse_quad_mesh.collect_strips() if collect_polyedges: coarse_quad_mesh.collect_polyedges() # store density attribute from input dense quad mesh if attribute_density: coarse_quad_mesh.set_strips_density(1) for skey in coarse_quad_mesh.strips(): u, v = coarse_quad_mesh.strip_edges(skey)[0] d = len( coarse_edges_children.get((u, v), coarse_edges_children.get((v, u), []))) coarse_quad_mesh.set_strip_density(skey, d) # store quad mesh and use as polygonal mesh coarse_quad_mesh.set_quad_mesh(quad_mesh) coarse_quad_mesh.set_polygonal_mesh(quad_mesh.copy()) return coarse_quad_mesh