def ags_inputs(network): """ Post-Processing Function: saves edg_dic_GT and ver_dic_GT files to be used in AGS """ ags_net=Network() # make a new network new_key_lis=range(len(network.node)) old_key_lis=list(network.nodes()) map_key_dic=dict(zip(old_key_lis, new_key_lis)) # {old_key:new_key} for ver in map_key_dic: ver_coor=network.node ags_net.add_node(key=map_key_dic[ver], x=ver_coor[ver]['x'], y=ver_coor[ver]['y'], z=ver_coor[ver]['z']) for edg in network.edges(): u=map_key_dic[edg[0]] v=map_key_dic[edg[1]] if (u, v) not in ags_net.edges() and (v, u) not in ags_net.edges(): ags_net.add_edge(u, v) ver_dic={} edg_dic={} for key in ags_net.node: ver_dic[key]=ags_net.node_coordinates(key) for ind_edg, edg in enumerate(ags_net.edges()): edg_dic[ind_edg]=edg # save the dictionaries to the Source folder with open(os.path.join(BASEDIR, 'ver_dic_GT.p'), 'wb') as fp: pickle.dump(ver_dic, fp, protocol=2) with open(os.path.join(BASEDIR, 'edg_dic_GT.p'), 'wb') as fp: pickle.dump(edg_dic, fp, protocol=2) return ags_net
def network_smoothen_lines(lines, fixed): net = Network.from_lines(lines) fixed = [] for vk in net.vertex: if net.vertex_degree(vk) == 1: fixed.append(vk) smooth_network_centroid(net, fixed) print net return net.to_lines()
def make_new_network (orig_net, edg_lis): """ makes a new newtork according to new edges """ new_net=Network() for edg in edg_lis: coor0=orig_net.node_coordinates(edg[0]) coor1=orig_net.node_coordinates(edg[1]) if edg[0] not in new_net.node: new_net.add_node(edg[0], {'x': coor0[0], 'y': coor0[1], 'z': coor0[2]}) if edg[1] not in new_net.node: new_net.add_node(edg[1], {'x': coor1[0], 'y': coor1[1], 'z': coor1[2]}) new_net.add_edge(edg[0], edg[1]) return new_net
def network_from_bmesh(bmesh, add_faces=False): """ Create a Network datastructure from a Blender mesh. Parameters: bmesh (obj): Blender mesh object. add_faces (bool): Add the bmesh faces to the Network. Returns: obj: Network object. """ vertices, edges, faces = bmesh_data(bmesh) network = Network.from_vertices_and_edges(vertices, edges) if add_faces: for c, face in enumerate(faces): network.add_face(face, fkey=c) return network
def make_network(ver_dic, edg_dic): """ make "compas" network using ver_dic and edg_dic """ net=Network() for ind, ver in ver_dic.items(): net.add_node(ind, {'x': ver[0], 'y': ver[1], 'z': ver[2]}) for edg in edg_dic.values(): net.add_edge(edg[0], edg[1]) return net
def fd_network(net_data): network = Network.from_data(net_data) k_i = network.key_index() xyz = network.get_vertices_attributes(('x', 'y', 'z')) loads = network.get_vertices_attributes(('px', 'py', 'pz')) q = network.get_edges_attribute('q') fixed = [k_i[k] for k in network if network.vertex[k]['is_anchor']] edges = [(k_i[u], k_i[v]) for u, v in network.edges_iter()] xyz, q, f, l, r = fd(xyz, edges, fixed, q, loads, rtype='list') for key in network: index = k_i[key] network.vertex[key]['x'] = xyz[index][0] network.vertex[key]['y'] = xyz[index][1] network.vertex[key]['z'] = xyz[index][2] return network.to_lines()
def network_from_ground_truss(self): """ generate compas network of ground truss and saves it in dic_attr['gt_net'] (the vertices are the same as the mesh vertices) """ mesh = self.dic_attr['mesh'] bars = self.dic_attr['bars'] gt_net = Network() for v_key, v_attr in mesh.vertex.items(): gt_net.add_node(v_key, v_attr) for edg in bars: gt_net.add_edge(edg[0], edg[1]) self.dic_attr['gt_net'] = gt_net
def make_network_from_face(network, key_lis): """ makes a network from list of vertex keys of a face used in "find_inner_face_corners" and "find_outer_face_corners" """ net=Network() for key in key_lis: xyz=(network.node[key]['x'], network.node[key]['y'], network.node[key]['z']) net.add_node(key, {'x': xyz[0], 'y': xyz[1], 'z': xyz[2]}) for edg in network.edges(): if edg[0] in key_lis and edg[1] in key_lis: net.add_edge(edg[0], edg[1]) return net
def rotate_dual(force_net, ANG): """ rotates the force_net coordinates 90 deg counterclockwise """ # save the rotated verteces in ver_coor_90_dic ver_coor_90_dic={} AX=[0.0, 0.0, 1.0] # normal to the plane of rotation ORG=[0.0, 0.0, 0.0] for key in force_net.node: coor=force_net.node_coordinates(key) pt=rotate_points([coor], ANG, AX, ORG) ver_coor_90_dic[key]=np.round_(pt[0], 3).tolist() # make new rotated dual network force_90_net=Network() for key, coor in ver_coor_90_dic.items(): force_90_net.add_node(key, {'x': coor[0], 'y': coor[1], 'z': coor[2]}) for edg in force_net.edges(): force_90_net.add_edge(edg[0], edg[1]) return force_90_net
def graph_from_strip_overlap(mesh): """Create the strip overlap graph of a (coarse) quad mesh. A graph node is a quad face strip and a graph edge is a face crossed by two quad face strips. The strip of each edge must be previously stored as an edge attribute. Parameters ---------- mesh : Mesh A (coarse) quad mesh. Returns ------- graph : graph The strip overlap graph. """ graph = Network() # add vertices strips_to_faces = strip_to_faces_dict(mesh) for strip, faces in strips_to_faces.items(): if strip == -1: continue x, y, z = centroid_points([mesh.face_centroid(fkey) for fkey in faces]) graph.add_vertex(key=strip, attr_dict={'x': x, 'y': y, 'z': z}) # add edges edges = [] faces_to_strips = faces_to_strips_dict(mesh) for strip_1, strip_2 in faces_to_strips.values(): # no duplicates if (strip_1, strip_2) not in edges and (strip_2, strip_1) not in edges: edges.append((strip_1, strip_2)) for u, v in edges: graph.add_edge(u, v) return graph
] edges_0 = [ [6, 3], [3, 0], [0, 5], [5, 4], [4, 1], [1, 6], [6, 2], ] # graph = Network.from_vertices_and_edges(vertices_0, edges_0) # key_color = try_colour_graph_with_two_colours(graph) # if key_color is not None: # colors = ['#ff0000', '#0000ff', '#00ff00'] # plotter = NetworkPlotter(graph, figsize=(10, 7)) # plotter.draw_vertices(facecolor={key: colors[key_color[key]] for key in graph.vertices()}) # plotter.draw_edges() # plotter.show() import time graph = Network() for i in range(7): graph.add_vertex(attr_dict={'x': i, 'y': 0, 'z': 0}) graph.add_edge(0, 1) graph.add_edge(2, 1) graph.add_edge(3, 4) t0 = time.time() print graph_disjointed_parts(graph) t1 = time.time() print t1 - t0
def mesh_multiple_strip_collapse(cls, mesh, strips_to_collapse): boundary_vertices = mesh.vertices_on_boundary() boundary_corner_vertices = [ vkey for vkey in boundary_vertices if len(mesh.vertex_neighbors(vkey)) == 2 ] edges_to_strips = edges_to_strips_dict(mesh) edges_to_collapse = [ edge for edge, strip in edges_to_strips.items() if strip in strips_to_collapse ] faces_to_collapse = [ fkey for fkey, strips in faces_to_strips_dict(mesh).items() if strips[0] in strips_to_collapse or strips[1] in strips_to_collapse ] # get groups of vertices to merge via graph of edges to collapse graph = Network() for vkey in mesh.vertices(): x, y, z = mesh.vertex_coordinates(vkey) graph.add_vertex(key=vkey, attr_dict={'x': x, 'y': y, 'z': z}) for u, v in edges_to_collapse: graph.add_edge(u, v) parts = graph_disjointed_parts(graph) # refine boundaries to avoid collapse to_subdivide = [] for boundary in mesh_boundaries(mesh): boundary_edges = [(boundary[i], boundary[i + 1]) for i in range(len(boundary) - 1)] boundary_edges_to_collapse = [ edge for edge in edges_to_collapse if edge in boundary_edges or edge[::-1] in boundary_edges ] if len(boundary_edges) - len(boundary_edges_to_collapse) < 3: for edge in boundary_edges: if edge not in boundary_edges_to_collapse and edge[:: -1] not in boundary_edges_to_collapse: if edge not in to_subdivide and edge[:: -1] not in to_subdivide: to_subdivide.append(edge) # refine pole points to avoid collapse poles = {u: [] for u, v in mesh.edges() if u == v} for u, v in edges_to_collapse: for pole in poles: if pole in mesh.halfedge[u] and pole in mesh.halfedge[v]: poles[pole].append((u, v)) for pole, pole_edges_to_collapse in poles.items(): vertex_faces = list(set(mesh.vertex_faces(pole))) if not mesh.is_vertex_on_boundary(pole): if len(vertex_faces) - len(pole_edges_to_collapse) < 3: for fkey in vertex_faces: face_vertices = copy.copy(mesh.face_vertices(fkey)) face_vertices.remove(pole) face_vertices.remove(pole) u, v = face_vertices if (u, v) not in pole_edges_to_collapse and ( v, u) not in pole_edges_to_collapse: if (u, v) not in to_subdivide and ( v, u) not in to_subdivide: to_subdivide.append((u, v)) # delete faces for fkey in faces_to_collapse: mesh.delete_face(fkey) for u, v in to_subdivide: face_strip_subdivide(cls, mesh, u, v) # merge vertices vertices_to_change = {} for part in parts: if len(part) > 1: # merge at the location of the unique two-valent boundary vertex ... xyz = None for vkey in part: if vkey in boundary_corner_vertices and xyz is None: xyz = mesh.vertex_coordinates(vkey) elif vkey in boundary_corner_vertices and xyz is not None: xyz = None break # ... or the unique boundary vertex ... if xyz is None: for vkey in part: if vkey in boundary_vertices and xyz is None: xyz = mesh.vertex_coordinates(vkey) elif vkey in boundary_vertices and xyz is not None: xyz = None break # ... or at the centroid if xyz is None: xyz = centroid_points( [mesh.vertex_coordinates(vkey) for vkey in part]) x, y, z = xyz new_vkey = mesh.add_vertex(attr_dict={'x': x, 'y': y, 'z': z}) vertices_to_change.update({vkey: new_vkey for vkey in part}) for fkey in list(mesh.faces()): face_vertices = [ vertices_to_change[vkey] if vkey in vertices_to_change else vkey for vkey in mesh.face_vertices(fkey) ] mesh.delete_face(fkey) mesh.add_face(face_vertices, fkey) mesh.cull_vertices() return mesh
'ry': 0.0, 'rz': 0.0, } dea = { 'qpre': 1.0, 'fpre': 0.0, 'lpre': 0.0, 'linit': 0.0, 'E': 0.0, 'radius': 0.0, } lines = compas.get_data('lines.obj') network = Network.from_obj(lines) network.update_default_vertex_attributes(dva) network.update_default_edge_attributes(dea) for key, attr in network.vertices(True): attr['is_fixed'] = network.vertex_degree(key) == 1 count = 1 for u, v, attr in network.edges(True): attr['qpre'] = count count += 1 res = drx(network) print(res)
if vkey_1 == vkey_2: if (vkey_2 == polyedge_2[0] or vkey_2 == polyedge_2[-1] ) and not mesh.is_vertex_on_boundary(vkey_2): continue if mesh.is_vertex_on_boundary( polyedge_1[0]) and mesh.is_vertex_on_boundary( polyedge_1[1]) and mesh.is_vertex_on_boundary( polyedge_2[0] ) and mesh.is_vertex_on_boundary( polyedge_2[1]): continue idx_0 = init_polyedges.index(polyedge_1) idx_1 = init_polyedges.index(polyedge_2) edges.append([idx_0, idx_1]) graph = Network.from_vertices_and_edges(vertices, edges) adjacency = graph.adjacency key_to_colour = vertex_coloring(adjacency) colours = [] for key, colour in key_to_colour.items(): if colour not in colours: colours.append(colour) for colour in colours: rs.AddLayer(name=str(colour)) if rs.IsLayer(str(0)): rs.LayerColor(str(0), [255, 0, 0])
div = 10 factor = 1.0 tol = 0.01 steps = 10000 deg = pi / 180 du = 0.02 dr = 15 * deg target = get_objects(0)[0] Xt = array(bezier_curve_interpolate(target, div * n)) # Network ds = L / m xyz = [[i * ds, 0, 0] for i in range(n)] uv = [[i, i + 1] for i in range(m)] network = Network.from_vertices_and_edges(xyz, uv) vertices = network.vertices() edges = network.edges() network.set_vertices_attributes(vertices, {'EIx': E * I, 'EIy': E * I}) network.set_edges_attributes(edges, {'E': E, 'A': A, 'l0': ds}) network.set_vertices_attributes([0, 1, m - 1, m], {'B': [0, 0, 0]}) network.beams = {'beam': {'nodes': list(range(n))}} # Manual dofs = 0, 0, 65 * deg, 0.6, 0, 135 * deg #Xs = update(dofs, network, Xt, div, factor, tol, steps, ds, refresh=100, bmesh=True, plot=True) # Optimise xa, za = Xt[0, 0], Xt[0, 2]
if not any([b in network.halfedge[key] for key in colored_with_current]): key_to_color[b] = current_color colored_with_current.append(b) for key in colored_with_current[1:]: uncolored.remove(key) current_color += 1 return key_to_color # ============================================================================== # Debugging # ============================================================================== if __name__ == "__main__": import compas from compas.datastructures.network import Network from compas.visualization.plotters.networkplotter import NetworkPlotter network = Network.from_obj(compas.get_data('grid_irregular.obj')) key_color = network_vertex_coloring(network) colors = ['#ff0000', '#00ff00', '#0000ff'] plotter = NetworkPlotter(network) plotter.draw_vertices(facecolor={key: colors[key_color[key]] for key in network}) plotter.draw_edges() plotter.show()
import compas.cad.blender as compas_blender from compas.datastructures.network import Network from compas.numerical.methods.forcedensity import fd __author__ = ['Tom Van Mele', ] __copyright__ = 'Copyright 2017, BRG - ETH Zurich', __license__ = 'MIT' __email__ = '*****@*****.**' # make a network from sample data network = Network.from_obj(compas.get_data('saddle.obj')) compas_blender.delete_objects_all() compas_blender.draw_network(network, type="lines") # set default vertex and edge attributes dva = {'is_anchor': False, 'px': 0.0, 'py': 0.0, 'pz': 0.0} dea = {'q': 1.0} network.update_default_vertex_attributes(dva) network.update_default_edge_attributes(dea) # identify *anchored* vertices for key in network:
return True # ============================================================================== # Debugging # ============================================================================== if __name__ == '__main__': import compas from compas.datastructures.network import Network from compas.visualization.plotters.networkplotter import NetworkPlotter network = Network.from_obj(compas.get_data('fink.obj')) embedding = network.copy() fix = (1, 12) if embed_network_in_plane(embedding, fix=fix): plotter = NetworkPlotter(embedding) plotter.draw_xlines([{ 'start': network.vertex_coordinates(u, 'xy'), 'end': network.vertex_coordinates(v, 'xy'), 'color': '#cccccc' } for u, v in network.edges()])
return CtQC def CitQCi(): pass def CitQCf(): pass # ============================================================================== # Debugging # ============================================================================== if __name__ == "__main__": import compas from compas.datastructures.network import Network network = Network.from_obj(compas.get_data('lines.obj')) key_index = network.key_index() edges = [(key_index[u], key_index[v]) for u, v in network.edges()] n = network.number_of_vertices() C = connectivity_matrix(edges, n) print C
__author__ = ['Tom Van Mele', ] __copyright__ = 'Copyright 2017, BRG - ETH Zurich', __license__ = 'MIT' __email__ = '*****@*****.**' # connect to the MATLAB COM automation server matlab = MatlabClient() # make a network from line geometry guids = compas_rhino.select_lines() lines = compas_rhino.get_line_coordinates(guids) network = Network.from_lines(lines) # vertex coordinates xyz = matlab.matrix_from_list(network.get_vertices_attributes('xyz')) # connectivity matrix m, n = network.number_of_edges(), network.number_of_vertices() key_index = network.key_index() C = [[0] * n for _ in range(m)] for i, (u, v) in enumerate(network.edges()): j, k = key_index[u], key_index[v] C[i][j] = -1