def kagome_polyedge_colouring_2(self): polyedges = self.kagome_polyedge_data edge_to_polyedge_index = {vkey: {} for vkey in self.kagome.vertices()} for i, polyedge in enumerate(polyedges): for u, v in pairwise(polyedge): edge_to_polyedge_index[u][v] = i edge_to_polyedge_index[v][u] = i vertices = [ centroid_points( [self.kagome.vertex_coordinates(vkey) for vkey in polyedge]) for polyedge in polyedges ] edges = [] for idx, polyedge in enumerate(polyedges): for vkey in polyedge: for vkey_2 in self.kagome.vertex_neighbors(vkey): idx_2 = edge_to_polyedge_index[vkey][vkey_2] if idx_2 != idx and idx < idx_2 and (idx, idx_2) not in edges: edges.append((idx, idx_2)) polyedge_network = Network.from_vertices_and_edges(vertices, edges) key_to_colour = vertex_coloring(polyedge_network.adjacency) return [key_to_colour[key] for key in sorted(key_to_colour.keys())]
def strip_edge_network(mesh, skey): all_strip_vertices = list( set([vkey for edge in mesh.strip_edges(skey) for vkey in edge])) strip_edges = [(u, v) for u, v in mesh.strip_edges(skey) if u != v] # exception for poles strip_vertices = { vkey: mesh.vertex_coordinates(vkey) for vkey in all_strip_vertices } return Network.from_vertices_and_edges(strip_vertices, strip_edges)
def branches(self): """Get the branch polylines of the topological skeleton as polylines connecting singular points. Returns ------- list List of polylines as tuples of XYZ-coordinates. """ return network_polylines(Network.from_lines(self.lines()))
def decomposition_polylines(self): """Get all the branch polylines to form a decomposition of the Delaunay mesh. These branches include the ones between singularities, between singularities and boundaries and along boundaries. Additional branches for some fixes: the ones to further split boundaries that only have two splits. Returns ------- list List of polylines as list of point XYZ-coordinates. """ branches = self.branches_singularity_to_singularity( ) + self.branches_singularity_to_boundary() + self.branches_boundary() branches += self.branches_splitting_boundary_kinks() branches += self.branches_splitting_collapsed_boundaries() branches += self.branches_splitting_flipped_faces() self.polylines = network_polylines( Network.from_lines([(u, v) for polyline in branches for u, v in pairwise(polyline)]), splits=[ self.vertex_coordinates(vkey) for vkey in self.corner_vertices() ]) return self.polylines
def delete_strip(mesh, skey, preserve_boundaries=False): """Delete a strip. Parameters ---------- mesh : QuadMesh A quad mesh. skey : hashable A strip key. preserve_boundaries : bool A boolean whether to preserve boundaries that would be collapsed by refining strips without adding singularities. Returns ------- skey_to_skeys : dict, None If strip splits were applied to preserve boundaries, a dictionary of the initial strip key to the refining strip keys. None otherwise. """ if skey not in list(mesh.strips()): return 0 if preserve_boundaries: skey_to_skeys = split_strips(mesh, boundary_strip_preserve(mesh, [skey])) old_boundary_vertices = list(mesh.vertices_on_boundary()) # get strip data strip_edges = mesh.strip_edges(skey) strip_faces = mesh.strip_faces(skey) # collateral strip deletions collateral_deleted_strips = [] #print('strip_faces: ', strip_faces) for skey_2 in mesh.strips(): if skey_2 == skey: continue #print('strip_faces_2: ', mesh.strip_faces(skey_2), [mesh.strip_faces(skey_2) in strip_faces]) if all([fkey in strip_faces for fkey in mesh.strip_faces(skey_2)]): collateral_deleted_strips.append(skey_2) #print('collateral_deleted_strips: ', collateral_deleted_strips) # build network between vertices of the edges of the strip to delete to # get the disconnect parts of vertices to merge vertices = set([i for edge in strip_edges for i in edge]) # maps between old and new indices old_to_new = {vkey: i for i, vkey in enumerate(vertices)} new_to_old = {i: vkey for i, vkey in enumerate(vertices)} # network vertex_coordinates = [mesh.vertex_coordinates(vkey) for vkey in vertices] edges = [(old_to_new[u], old_to_new[v]) for u, v in strip_edges] network = Network.from_vertices_and_edges(vertex_coordinates, edges) # disconnected parts parts = network_disconnected_vertices(network) # delete strip faces for fkey in strip_faces: mesh.delete_face_in_strips(fkey) for fkey in strip_faces: mesh.delete_face(fkey) old_vkeys_to_new_vkeys = {} # merge strip edge vertices that are connected for part in parts: # move back from network vertices to mesh vertices vertices = [new_to_old[vkey] for vkey in part] # skip adding a vertex if all vertices of the part are disconnected if any(mesh.is_vertex_connected(vkey) for vkey in vertices): # get position based on disconnected vertices that used to be on # the boundary if any if any(not mesh.is_vertex_connected(vkey) for vkey in vertices): points = [ mesh.vertex_coordinates(vkey) for vkey in vertices if not mesh.is_vertex_connected(vkey) ] # or based on old boundary vertices if any elif any(vkey in old_boundary_vertices for vkey in vertices): points = [ mesh.vertex_coordinates(vkey) for vkey in vertices if vkey in old_boundary_vertices ] else: points = [mesh.vertex_coordinates(vkey) for vkey in vertices] # new vertex x, y, z = centroid_points(points) new_vkey = mesh.add_vertex(attr_dict={'x': x, 'y': y, 'z': z}) old_vkeys_to_new_vkeys.update( {old_vkey: new_vkey for old_vkey in vertices}) # replace the old vertices for old_vkey in vertices: mesh.substitute_vertex_in_strips(old_vkey, new_vkey) mesh_substitute_vertex_in_faces(mesh, old_vkey, new_vkey, mesh.vertex_faces(old_vkey)) # delete the old vertices for old_vkey in vertices: mesh.delete_vertex(old_vkey) # delete data of deleted strip and collateral deleted strips del mesh.data['attributes']['strips'][skey] for skey_2 in collateral_deleted_strips: del mesh.data['attributes']['strips'][skey_2] #print(old_vkeys_to_new_vkeys) #print(mesh.data['attributes']['face_pole']) if 'face_pole' in mesh.data['attributes']: for fkey, pole in mesh.data['attributes']['face_pole'].items(): if fkey in mesh.data['attributes']['face_pole']: #print(fkey, pole, mesh.data['attributes']['face_pole']) if pole == mesh.data['attributes']['face_pole'][fkey]: if pole in old_vkeys_to_new_vkeys: mesh.data['attributes']['face_pole'][ fkey] = old_vkeys_to_new_vkeys[pole] return old_vkeys_to_new_vkeys
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))