Example #1
0
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()
Example #2
0
    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()
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()
    plotter.update(pause=0.01)


drx_numpy(structure=structure,
          tol=0.01,
          refresh=20,
          factor=30,
Example #4
0
    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()
Example #5
0
# ==============================================================================

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()
Example #6
0
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()
Example #7
0
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
Example #8
0
            'start': network.vertex_coordinates(u, 'xy'),
            'end': network.vertex_coordinates(v, 'xy'),
            'color': '#cccccc',
            'width': 0.5
        })

    plotter.draw_lines(lines)

    # draw the vertices and edges in the starting configuration
    # and pause for a second before starting the dynamic visualization

    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 dynamic relaxation

    xyz, q, f, l, r = dr(vertices,
                         edges,
                         fixed,
                         loads,
                         qpre,
                         fpre,
                         lpre,
                         linit,
                         E,
                         radius,
Example #9
0
# 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 = {}
text = {}
for uv in network.edges():
    if uv in edges:
        color[uv] = '#ff0000'
        width[uv] = 5.0
    elif weight[uv] >= 100:
        color[uv] = '#00ff00'
        width[uv] = 5.0
        # text[uv] = weight[uv]

# draw the edges
plotter.draw_edges(color=color, width=width, text=text)

# show the plot
plotter.show()
Example #10
0
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()
Example #11
0
            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()

    # ==========================================================================
    # testrun 3
    # ==========================================================================

    if testrun == 3:
        import compas

        from compas.datastructures import Network
        from compas.topology import dijkstra_distances
        from compas_plotters import NetworkPlotter
        from compas.utilities import i_to_red
Example #12
0
        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()
Example #13
0
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()
Example #14
0
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'
                          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()
Example #15
0
        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 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, callback=callback)

    # plot the final configuration

    fmax = max(network.get_edges_attribute('f'))

    plotter.draw_vertices(facecolor={
        key: '#000000'
        for key in network.vertices_where({'is_fixed': True})
    })
    plotter.draw_edges(color={(u, v): i_to_rgb(attr['f'] / fmax)