コード例 #1
0
ファイル: _network.py プロジェクト: elidim/compas
    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()
コード例 #2
0
# 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'))

    plotter = NetworkPlotter(network, figsize=(10, 8))

    plotter.draw_vertices(radius=0.1, picker=10)
    plotter.draw_edges()

    default = [
        plotter.defaults['vertex.facecolor'] for key in network.vertices()
    ]
    highlight = '#ff0000'

    def on_pick(event):
        index = event.ind[0]

        colors = default[:]
        colors[index] = highlight

        plotter.vertexcollection.set_facecolor(colors)
        plotter.update()
コード例 #3
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()
コード例 #4
0
ファイル: planarity.py プロジェクト: tclim/compas
    crossings = network_find_crossings(network)

    print(network_count_crossings(network))
    print(crossings)
    print(len(crossings))

    ecolor = {}
    for e1, e2 in crossings:
        ecolor[e1] = '#ff0000'
        ecolor[e2] = '#ff0000'

    plotter = NetworkPlotter(network, figsize=(10, 7))

    plotter.draw_vertices()
    plotter.draw_edges(color=ecolor)
    plotter.show()

    # embedding = network.copy()

    # fix = (1, 12)

    # if network_embed_in_plane(embedding, fix=fix):

    #     plotter = NetworkPlotter(embedding, figsize=(10, 7))

    #     plotter.draw_lines([{'start': network.vertex_coordinates(u, 'xy'),
    #                           'end': network.vertex_coordinates(v, 'xy'),
    #                           'color': '#cccccc'} for u, v in network.edges()])

    #     plotter.draw_vertices(radius=0.3,
コード例 #5
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)
コード例 #6
0
# 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},
                   width={(u, v): 5.0
                          for u, v in edges})

# show the plot
plotter.show()
コード例 #7
0
ファイル: dr_python.py プロジェクト: philianeles/compas
            '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,
コード例 #8
0
edges = [(key_index[u], key_index[v]) for u, v in network.edges()]

C = connectivity_matrix(edges, rtype='list')
C = matlab.double(C)

# compute coordinate differences in Matlab

# # using an engine function
# uv = matlab.engine.mtimes(C, xyz)

# using workspace data
matlab.engine.workspace['C'] = C
matlab.engine.workspace['xyz'] = xyz

uv = matlab.engine.eval('C * xyz')

# compute edge lengths in Python

l = normrow(uv)
l = l.flatten().tolist()

# plot results as edge labels

plotter = NetworkPlotter(network, figsize=(10, 7), fontsize=6)

plotter.draw_vertices()
plotter.draw_edges(text={(u, v): '%.1f' % l[index]
                         for index, (u, v) in enumerate(network.edges())})

plotter.show()
コード例 #9
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 = {(u, v): str(index) for index, (u, v) 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()
コード例 #10
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()
コード例 #11
0
ファイル: _planarity.py プロジェクト: elidim/compas
if __name__ == '__main__':

    import compas

    from compas.datastructures import Network
    from compas.datastructures import network_is_planar
    from compas.datastructures import network_find_crossings
    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()
コード例 #12
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()
コード例 #13
0
ファイル: plotter.py プロジェクト: ytakzk/compas_assembly
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
    --------
    .. code-block:: python

        plotter = AssemblyPlotter(assembly, tight=True, figsize=(12, 8))

        plotter.draw_vertices()
        plotter.draw_blocks()

        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)

    def draw_vertices(self, *args, **kwargs):
        """Draw the vertices of an assembly.
        """
        return self.assembly_plotter.draw_vertices(*args, **kwargs)

    def draw_edges(self, *args, **kwargs):
        """Draw the edges of an assembly.
        """
        self.assembly_plotter.draw_edges(*args, **kwargs)

    def draw_blocks(self,
                    keys=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.

        """
        keys = keys or list(self.assembly.vertices())

        facecolordict = valuedict(keys, facecolor, self.block_plotter.defaults['face.facecolor'])
        edgecolordict = valuedict(keys, edgecolor, self.block_plotter.defaults['face.edgecolor'])
        edgewidthdict = valuedict(keys, edgewidth, self.block_plotter.defaults['face.edgewidth'])
        textcolordict = valuedict(keys, textcolor, self.block_plotter.defaults['face.textcolor'])
        fontsizedict  = valuedict(keys, fontsize,  self.block_plotter.defaults['face.fontsize'])

        polygons = []
        for key, attr in self.assembly.vertices(True):
            block = self.assembly.blocks[key]
            xyz = block.get_vertices_attributes('xyz')
            box = bounding_box_xy(xyz)
            polygons.append({
                'points': box,
                'edgecolor': edgecolordict[key],
                'edgewidth': edgewidthdict[key],
                'facecolor': facecolordict[key]
            })
        self.draw_polygons(polygons)