def plot_network(network): """ Compas Network Plotter """ plotter=NetworkPlotter(network) plotter.draw_nodes(radius=0.001, text='key', fontsize=15.0, facecolor=(0, 0, 0)) plotter.draw_edges() plotter.show()
def plot(self, vertexcolor=None, edgecolor=None, vertexsize=None, edgewidth=None, vertextext=None, edgetext=None): """Plot a 2D representation of the network. Parameters ---------- vertexcolor : dict, optional A dictionary mapping vertex identifiers to colors. edgecolor : dict, optional A dictionary mapping edge identifiers to colors. vertexsize : dict, optional A dictionary mapping vertex identifiers to sizes. edgewidth : dict, optional A dictionary mapping edge identifiers to widths. vertextext : dict, optional A dictionary mappping vertex identifiers to labels. edgetext : dict, optional A dictionary mappping edge identifiers to labels. Examples -------- .. plot:: :include-source: import compas from compas.datastructures import Network network = Network.from_obj(compas.get('lines.obj')) network.plot() """ from compas_plotters import NetworkPlotter plotter = NetworkPlotter(self) plotter.draw_vertices(facecolor=vertexcolor, radius=vertexsize, text=vertextext) plotter.draw_edges(color=edgecolor, width=edgewidth, text=edgetext) plotter.show()
import compas from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('grid_irregular.obj')) node = next(network.nodes()) nbrs = network.neighbors(node) facecolor = {node: (255, 0, 0)} for nbr in nbrs: facecolor[nbr] = (0, 0, 255) edgecolor = {} for nbr in nbrs: edgecolor[node, nbr] = (0, 255, 0) edgecolor[nbr, node] = (0, 255, 0) plotter = NetworkPlotter(network, figsize=(12, 7.5)) plotter.draw_nodes(facecolor=facecolor) plotter.draw_edges(color=edgecolor, width={edge: 2.0 for edge in edgecolor}) plotter.show()
import compas from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('grid_irregular.obj')) text = {node: str(node) for node in network.nodes()} plotter = NetworkPlotter(network, figsize=(12, 7.5)) plotter.draw_nodes(text=text, radius=0.2) plotter.draw_edges() plotter.show()
cloads = Array2D(loads, 'double') cq = Array1D(q, 'double') cfixed = Array1D(fixed, 'int') cfree = Array1D(free, 'int') lib.fd.argtypes = [ ctypes.c_int, ctypes.c_int, ctypes.c_int, cvertices.ctype, cedges.ctype, cloads.ctype, cq.ctype, cfixed.ctype, cfree.ctype ] lib.fd(ctypes.c_int(len(vertices)), ctypes.c_int(len(edges)), ctypes.c_int(len(fixed)), cvertices.cdata, cedges.cdata, cloads.cdata, cq.cdata, cfixed.cdata, cfree.cdata) xyz = cvertices.pydata for key, attr in network.vertices(True): attr['x'] = float(xyz[key][0]) attr['y'] = float(xyz[key][1]) attr['z'] = float(xyz[key][2]) zmax = max(network.get_vertices_attribute('z')) plotter = NetworkPlotter(network, figsize=(10, 7)) plotter.draw_vertices(facecolor={ key: i_to_red(attr['z'] / zmax) for key, attr in network.vertices(True) }) plotter.draw_edges() plotter.show()
from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) a = network.split_edge(0, 22) b = network.split_edge(2, 30) c = network.split_edge(17, 21) d = network.split_edge(28, 16) lines = [] for u, v in network.edges(): lines.append({ 'start': network.vertex_coordinates(u, 'xy'), 'end' : network.vertex_coordinates(v, 'xy'), 'arrow': 'end', 'width': 4.0, 'color': '#00ff00' }) plotter = NetworkPlotter(network) plotter.draw_vertices(radius=0.2, facecolor={key: '#ff0000' for key in (a, b, c, d)}, text={key: key for key in network.vertices()}) plotter.draw_edges(color={(u, v): '#cccccc' for u, v in network.edges()}) plotter.draw_lines(lines) plotter.show()
# ============================================================================== if __name__ == '__main__': import compas from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) network.add_edge(6, 15) if not network_is_planar(network): crossings = network_find_crossings(network) else: crossings = [] print(crossings) plotter = NetworkPlotter(network) plotter.draw_vertices(radius=0.15, text={key: key for key in network.vertices()}) plotter.draw_edges( color={edge: '#ff0000' for edges in crossings for edge in edges}) plotter.show()
width={uv: 5.0 for uv in edges}) plotter.update() # callback for the pick event def onpick(event): index = event.ind[0] shortest_path_to(index) # path to the sample file DATA = os.path.join(os.path.dirname(__file__), '..', 'data') FILE = os.path.join(DATA, 'grid_irregular.obj') # load a network from an OBJ file network = Network.from_obj(FILE) # define the starting point start = 21 # create plotter # draw the original configuration # register the pick callback # show the viewer plotter = NetworkPlotter(network, figsize=(10, 7)) plotter.draw_vertices(facecolor={start: '#ff0000'}, radius=0.15, picker=10) plotter.draw_edges() plotter.register_listener(onpick) plotter.show()
} start = 21 end = 11 path = shortest_path(adjacency, start, end) edges = [] for i in range(len(path) - 1): u = path[i] v = path[i + 1] if v not in network.edge[u]: u, v = v, u edges.append([u, v]) plotter = NetworkPlotter(network, figsize=(10, 8), fontsize=6) plotter.draw_vertices( text={key: key for key in network.vertices()}, facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15) plotter.draw_edges(color={(u, v): '#ff0000' for u, v in edges}, width={(u, v): 5.0 for u, v in edges}) plotter.show()
components.append(list(visited)) return components # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": import compas from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('grid_irregular.obj')) components = connected_components(network.adjacency) key_color = vertex_coloring(network.adjacency) colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00'] plotter = NetworkPlotter(network, figsize=(10, 7)) plotter.draw_vertices( facecolor={key: colors[key_color[key]] for key in network.vertices()}) plotter.draw_edges() plotter.show()
# Main # ============================================================================== if __name__ == '__main__': import compas from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) network.add_edge(6, 15) if not network_is_planar(network): crossings = network_find_crossings(network) else: crossings = [] print(crossings) plotter = NetworkPlotter(network, figsize=(8, 5)) plotter.defaults['node.fontsize'] = 6 plotter.draw_nodes(radius=0.15, text={key: key for key in network.nodes()}) plotter.draw_edges( color={edge: '#ff0000' for edges in crossings for edge in edges}) plotter.show()
try: ad = network.get_edge_attribute((fkey, nbr), 'angle_diff') if ad: continue except: network.add_edge(fkey, nbr, attr_dict={'angle_diff': angle_diff}) # # ========================================================================== # # color up # # ========================================================================== anglemax = max(network.get_edges_attribute('angle_diff')) print('angle diff max', anglemax) colors = {} for u, v, attr in network.edges(True): angle_diff = attr['angle_diff'] color = i_to_rgb(angle_diff / anglemax) colors[(u, v)] = color # # ========================================================================== # # Set up Plotter # # ========================================================================== plotter = NetworkPlotter(network, figsize=(12, 9)) # plotter.draw_faces(facecolor=colors) plotter.draw_vertices(radius=0.01) plotter.draw_edges(color=colors) plotter.show()
structure = Network.from_obj(compas.get('lines.obj')) structure.update_default_vertex_attributes({'is_fixed': False, 'P': [1, 1, 0]}) structure.update_default_edge_attributes({'E': 10, 'A': 1, 'ct': 't'}) structure.vertices_attributes(['is_fixed', 'B'], [True, [0, 0, 0]], structure.leaves()) lines = [] for u, v in structure.edges(): lines.append({ 'start': structure.vertex_coordinates(u, 'xy'), 'end': structure.vertex_coordinates(v, 'xy'), 'color': '#cccccc' }) plotter = NetworkPlotter(structure, figsize=(10, 7)) plotter.draw_vertices(facecolor={ key: '#ff0000' for key in structure.vertices_where({'is_fixed': True}) }) plotter.draw_lines(lines) plotter.draw_edges() def callback(X, k_i): for key in structure.vertices(): x, y, z = X[k_i[key], :] structure.vertex_attributes(key, 'xyz', [x, y, z]) plotter.update_edges() plotter.update(pause=0.01)
# make network from sample file network = Network.from_obj(FILE) # specify start and end start = 21 end = 11 # compute the shortest path taking into account the edge weights path = shortest_path(network.adjacency, start, end) # convert the path to network edges edges = [(v, u) if not network.has_edge(u, v) else (u, v) for u, v in pairwise(path)] # make a plotter plotter = NetworkPlotter(network, figsize=(10, 7)) # set default font sizes plotter.defaults['vertex.fontsize'] = 6 plotter.defaults['edge.fontsize'] = 6 # draw the vertices plotter.draw_vertices( text='key', facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15) # draw the edges plotter.draw_edges(color={(u, v): '#ff0000' for u, v in edges},
if __name__ == '__main__': import compas_ags from compas_plotters import NetworkPlotter form = FormDiagram.from_obj(compas_ags.get('paper/fink.obj')) lines = [] for u, v in form.edges(): lines.append({ 'start': form.vertex_coordinates(u), 'end': form.vertex_coordinates(v), 'color': '#cccccc', 'width': 0.5, }) form.identify_fixed() vcolor = {key: '#ff0000' for key in form.fixed()} vlabel = {key: key for key in form.vertices()} elabel = {key: str(index) for index, key in enumerate(form.edges())} plotter = NetworkPlotter(form, figsize=(10.0, 7.0), fontsize=8) plotter.draw_lines(lines) plotter.draw_vertices(facecolor=vcolor, text=vlabel, radius=0.3) plotter.draw_edges(text=elabel) plotter.show()
import random import compas from compas.datastructures import Network from compas.utilities import pairwise from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('grid_irregular.obj')) plotter = NetworkPlotter(network, figsize=(12, 7.5)) nodecolor = (0, 255, 0) edgecolor = (0, 255, 0) edgewidth = 3 * plotter.defaults['edge.width'] node_color = {} edge_color = {} edge_width = {} start = random.choice(list(network.leaves())) goal = random.choice(list(network.leaves())) nodes = network.shortest_path(start, goal) for u, v in pairwise(nodes): node_color[v] = nodecolor edge_color[u, v] = edge_color[v, u] = edgecolor edge_width[u, v] = edge_width[v, u] = edgewidth node_color[start] = (255, 0, 0) node_color[goal] = (0, 0, 255) plotter.draw_nodes(facecolor=node_color) plotter.draw_edges(color=edge_color, width=edge_width)
import compas from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) plotter = NetworkPlotter(network, figsize=(12, 7.5)) plotter.draw_nodes() plotter.draw_edges() plotter.show()
attr['x'] += damping * (cx - x) attr['y'] += damping * (cy - y) attr['z'] += damping * (cz - z) if callback: callback(k, callback_args) # ============================================================================== # Main # ============================================================================== if __name__ == "__main__": import compas from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('grid_irregular.obj')) fixed = network.leaves() network_smooth_centroid(network, fixed=fixed) plotter = NetworkPlotter(network, figsize=(8, 5)) plotter.draw_nodes(facecolor={key: '#ff0000' for key in fixed}) plotter.draw_edges() plotter.show()
attr['z'] = xyz[key][2] # make a network network = Network.from_obj(FILE) # identify the fixed vertices leaves = network.vertices_where({'vertex_degree': 1}) network.set_vertices_attribute('is_fixed', True, keys=leaves) # assign random prescribed force densities to the edges for uv in network.edges(): network.set_edge_attribute(uv, 'qpre', 1.0 * random.randint(1, 7)) # make a plotter for (dynamic) visualization plotter = NetworkPlotter(network, figsize=(10, 7)) # plot the starting configuration plotter.draw_vertices(facecolor={ key: '#000000' for key in network.vertices_where({'is_fixed': True}) }) plotter.draw_edges() plotter.update(pause=1.0) # run the DR network_dr(network, kmax=50, callback=callback) # plot the final configuration plotter.draw_vertices(facecolor={ key: '#000000'
import compas from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('lines_noleaves.obj')) plotter = NetworkPlotter(network, figsize=(12, 7.5)) corners = list(network.nodes_where({'degree': 2})) # network.smooth(fixed=corners) plotter.draw_nodes(facecolor={node: (255, 0, 0) for node in corners}) plotter.draw_edges() plotter.show()
network = Network.from_vertices_and_edges(vertices=vertices, edges=edges) network.update_default_vertex_attributes({ 'is_fixed': False, 'P': [1, -2, 0], 'EIx': EI, 'EIy': EI }) network.update_default_edge_attributes({'E': 50, 'A': 1, 'l0': L / n}) network.set_vertices_attributes(['B', 'is_fixed'], [[0, 0, 0], True], keys=pins) network.attributes['beams'] = {'beam': {'nodes': list(range(n))}} # Plotter plotter = NetworkPlotter(network, figsize=(10, 7)) # Initial configuration lines = [] for u, v in network.edges(): lines.append({ 'start': network.vertex_coordinates(u, 'xy'), 'end': network.vertex_coordinates(v, 'xy'), 'color': '#cccccc', 'width': 1.0 }) plotter.draw_lines(lines) plotter.draw_vertices(radius=0.005, facecolor={key: '#ff0000' for key in pins}) plotter.draw_edges()
weight[(0, 18)] = 100 weight[(18, 0)] = 100 # specify start and end start = 21 end = 19 # compute the shortest path taking into account the edge weights path = dijkstra_path(network.adjacency, weight, start, end) # convert the path to network edges edges = [(v, u) if not network.has_edge(u, v) else (u, v) for u, v in pairwise(path)] # make a plotter plotter = NetworkPlotter(network, figsize=(8, 5)) # set default font sizes plotter.defaults['vertex.fontsize'] = 6 plotter.defaults['edge.fontsize'] = 6 # draw the vertices plotter.draw_vertices( text='key', facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15) # set the edge widths and colors color = {} width = {}
from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) u, v = network.get_any_edge() a = network.split_edge(u, v) lines = [] for u, v in network.edges(): lines.append({ 'start': network.vertex_coordinates(u, 'xy'), 'end' : network.vertex_coordinates(v, 'xy'), 'arrow': 'end', 'width': 4.0, 'color': '#00ff00' }) plotter = NetworkPlotter(network) plotter.draw_lines(lines) plotter.draw_vertices( radius=0.2, text={key: key for key in network.vertices()}, facecolor={key: '#ff0000' for key in (a,)} ) plotter.draw_edges() plotter.show()
vertices = network.get_vertices_attributes(('x', 'y', 'z')) edges = list(network.edges()) fixed = network.vertices_where({'is_fixed': True}) loads = network.get_vertices_attributes(('px', 'py', 'pz')) qpre = network.get_edges_attribute('qpre') fpre = network.get_edges_attribute('fpre') lpre = network.get_edges_attribute('lpre') linit = network.get_edges_attribute('linit') E = network.get_edges_attribute('E') radius = network.get_edges_attribute('radius') # make a plotter for (dynamic) visualization # and define a callback function # for plotting the intermediate configurations plotter = NetworkPlotter(network, figsize=(10, 7), fontsize=6) def callback(k, xyz, crits, args): print(k) plotter.update_vertices() plotter.update_edges() plotter.update(pause=0.001) for key, attr in network.vertices(True): attr['x'] = xyz[key][0] attr['y'] = xyz[key][1] attr['z'] = xyz[key][2] # plot the lines of the original configuration of the network # as a reference
plotter.show() # ============================================================================== # Main # ============================================================================== if __name__ == '__main__': import compas from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('lines.obj')) plotter = NetworkPlotter(network, figsize=(10, 7)) plotter.defaults['vertex.fontsize'] = 8 network.delete_vertex(17) plotter.draw_vertices(text='key', radius=0.2) plotter.draw_edges() plotter.show() vertices = {44: [0.0, 0.0, 0.0], 38: [1.0, 0.0, 0.0], 2: [2.0, 0.0, 0.0]} edges = [(44, 38), (38, 2)] network = Network.from_vertices_and_edges(vertices, edges) print(network)
class AssemblyPlotter(Plotter): """An ``AssemblyPlotter`` combines the functionality of a ``NetworkPlotter`` and a ``MeshPlotter`` and uses the same set of axes for all drawing output. Parameters ---------- assembly : Assembly The assembly data structure. Notes ----- For all other relevant parameters, see ``Plotter``. Examples -------- >>> """ def __init__(self, assembly, **kwargs): super(AssemblyPlotter, self).__init__(**kwargs) self.assembly = assembly self.assembly_plotter = NetworkPlotter(self.assembly, axes=self.axes) self.block_plotter = MeshPlotter(None, axes=self.axes) self.blockcollection = None def draw_nodes(self, *args, **kwargs): """Draw the nodes of an assembly. Parameters ---------- Examples -------- >>> """ return self.assembly_plotter.draw_nodes(*args, **kwargs) def draw_edges(self, *args, **kwargs): """Draw the edges of an assembly. """ self.assembly_plotter.draw_edges(*args, **kwargs) def clear_blocks(self): if self.blockcollection: self.blockcollection.remove() def draw_blocks(self, nodes=None, facecolor=None, edgecolor=None, edgewidth=None, textcolor=None, fontsize=None): """Draw the blocks of an assembly. Notes ----- The blocks are drawn as the boundaing boxes of their vertices. """ nodes = nodes or list(self.assembly.nodes()) node_facecolor = valuedict( nodes, facecolor, self.block_plotter.defaults['face.facecolor']) node_edgecolor = valuedict( nodes, edgecolor, self.block_plotter.defaults['face.edgecolor']) node_edgewidth = valuedict( nodes, edgewidth, self.block_plotter.defaults['face.edgewidth']) polygons = [] for node in nodes: block = self.assembly.node_attribute(node, 'block') for face in block.faces(): polygons.append({ 'points': block.face_coordinates(face), 'edgecolor': node_edgecolor[node], 'edgewidth': node_edgewidth[node], 'facecolor': node_facecolor[node] }) collection = self.draw_polygons(polygons) self.blockcollection = collection return collection
widths = default_linewidths[:] colors[start] = highlight_color for u, v in pairwise(nodes): colors[v] = highlight_color widths[edge_index[u, v]] = highlight_width plotter.nodecollection.set_facecolor(colors) plotter.edgecollection.set_linewidths(widths) plotter.update() network = Network.from_obj(compas.get('grid_irregular.obj')) goal = choice(list(network.leaves())) index_node = network.index_key() edge_index = network.uv_index() edge_index.update({(v, u): index for (u, v), index in edge_index.items()}) plotter = NetworkPlotter(network, figsize=(10, 8)) plotter.draw_nodes(radius=0.1, picker=10) plotter.draw_edges() default_colors = [(1, 1, 1) for key in network.nodes()] highlight_color = (1, 0, 0) default_colors[goal] = highlight_color default_linewidths = [1.0 for key in network.edges()] highlight_width = 3.0 plotter.nodecollection.set_facecolor(default_colors) plotter.register_listener(on_pick) plotter.show()
def __init__(self, assembly, **kwargs): super(AssemblyPlotter, self).__init__(**kwargs) self.assembly = assembly self.assembly_plotter = NetworkPlotter(self.assembly, axes=self.axes) self.block_plotter = MeshPlotter(None, axes=self.axes) self.blockcollection = None
'EIy': EI }) structure.update_default_edge_attributes({'E': 50, 'A': 1, 'l0': L / n}) structure.get_vertices_attributes(['B', 'is_fixed'], [[0, 0, 0], True], structure.leaves()) structure.attributes['beams'] = {'beam': {'nodes': list(range(n))}} lines = [] for u, v in structure.edges(): lines.append({ 'start': structure.vertex_coordinates(u, 'xy'), 'end': structure.vertex_coordinates(v, 'xy'), 'color': '#cccccc' }) plotter = NetworkPlotter(structure, figsize=(8, 5)) plotter.draw_vertices(radius=0.005, facecolor={ i: '#ff0000' for i in structure.vertices_where({'is_fixed': True}) }) plotter.draw_lines(lines) plotter.draw_edges() def callback(X, k_i): for key in structure.vertices(): x, y, z = X[k_i[key], :] structure.set_vertex_attributes(key, 'xyz', [x, y, z]) plotter.update_edges()
end = choice(leaves) # construc an adjacency dict # add weight to the edges corresponding to their length # compute the shortest path adjacency = {key: network.vertex_neighbors(key) for key in network.vertices()} weight = {(u, v): network.edge_length(u, v) for u, v in network.edges()} weight.update({(v, u): weight[(u, v)] for u, v in network.edges()}) path = dijkstra_path(adjacency, weight, start, end) # visualize the result plotter = NetworkPlotter(network, figsize=(10, 8), fontsize=6) edges = [] for u, v in pairwise(path): if v not in network.edge[u]: u, v = v, u edges.append([u, v]) plotter.draw_vertices( text={key: key for key in (start, end)}, facecolor={key: '#ff0000' for key in (path[0], path[-1])}, radius=0.15) plotter.draw_edges(color={(u, v): '#ff0000'