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()
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()
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()
plotter = NetworkPlotter(network, figsize=(10, 7)) # Initial configuration lines = [] for u, v in network.edges(): lines.append({ 'start': network.node_coordinates(u, 'xy'), 'end': network.node_coordinates(v, 'xy'), 'color': '#cccccc', 'width': 1.0}) plotter.draw_lines(lines) plotter.draw_nodes(radius=0.005, facecolor={key: '#ff0000' for key in pins}) plotter.draw_edges() plotter.update() # Callback for dynamic visualization def plot_iterations(X, radius=0.005): for i, attr in network.nodes(True): attr['x'] = X[i, 0] attr['y'] = X[i, 1] attr['z'] = X[i, 2] plotter.update_nodes(radius) plotter.update_edges()
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()
lsys = "A-A++A-A-A-A++A-A++A-A++A-A-A-A++A-A-A-A++A-A-A-A++A-A++A-A++A-A-A-A++A-A++A-A++A-A-A-A++A-A++A-A++A-A-A-A++A-A-A-A++A-A-A-A++A-A++A-A++A-A-A-A++A" network = Network() step = 1. pt = Point(0, 0, 0) v = Vector(1, 0, 0) def draw(network, point, vector, s, step, last_node=None): for i, c in enumerate(s): if c == 'A': point = point + vector * step a = network.add_node(x=point.x, y=point.y, z=point.z) if last_node: network.add_edge(last_node, a) last_node = a elif c == '-': R = Rotation.from_axis_and_angle((0, 0, 1), math.radians(60)) vector.transform(R) elif c == '+': R = Rotation.from_axis_and_angle((0, 0, 1), math.radians(-60)) vector.transform(R) draw(network, pt, v, lsys, step) plotter = NetworkPlotter(network, figsize=(12, 7.5)) plotter.draw_nodes(radius=0.1) plotter.draw_edges() plotter.show()
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
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()
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()
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) 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()
import compas from compas.datastructures import Network from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('grid_irregular.obj')) plotter = NetworkPlotter(network, figsize=(12, 7.5)) plotter.draw_nodes(facecolor={node: (255, 0, 0) for node in network.leaves()}) plotter.draw_edges() plotter.show()
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_nodes(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' for u, v in edges}, width={(u, v): 3.0 for u, v in edges}, text={(u, v): '{:.1f}'.format(weight[(u, v)]) for u, v in network.edges()}) plotter.show()