Exemplo n.º 1
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()
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()
    plotter.update(pause=0.01)

Exemplo n.º 3
0
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)

    plotter = NetworkPlotter(network, figsize=(10, 7))
    plotter.defaults['vertex.fontsize'] = 8
    plotter.draw_vertices(text='key', radius=0.2)
    plotter.draw_edges()
    plotter.show()
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()
Exemplo n.º 5
0
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()
Exemplo n.º 6
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()
Exemplo n.º 7
0
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()
plotter.update()

# Callback for dynamic visualization


def plot_iterations(X, radius=0.005):

    for i in network.vertices():
        x, y, z = X[i, :]
        network.set_vertex_attributes(i, 'xyz', [x, y, z])

    plotter.update_vertices(radius)
    plotter.update_edges()
    plotter.update(pause=0.01)
Exemplo n.º 8
0
                       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()
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'
    for key in network.vertices_where({'is_fixed': True})
})
plotter.draw_edges()
plotter.update(pause=1.0)
Exemplo n.º 10
0
# 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 = {}
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]
Exemplo n.º 11
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()
Exemplo n.º 12
0
        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()

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

    if testrun == 3:
Exemplo n.º 13
0
        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()
Exemplo n.º 14
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()
Exemplo n.º 15
0
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)


drx_numpy(structure=structure,
Exemplo n.º 16
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()