Ejemplo n.º 1
0
def computeSectorsAngles(G, commonVertex):

    angle_dict = dict()

    for currN in set(nx.all_neighbors(G, commonVertex)):

        v1 = G.node[commonVertex]
        v2 = G.node[currN]

        v1_x, v1_y = vertexmanager.getCoordinate(v1)
        v2_x, v2_y = vertexmanager.getCoordinate(v2)

        angle = getAngleOfLineBetweenTwoPoints(v1_x, v1_y, v2_x, v2_y)



        if(angle < 0):
            angle = 360-abs(angle)


        if(angle not in angle_dict.keys()):
            angle_dict[angle] = list()
        angle_dict[angle].append(currN)

    sorted_slopes = sorted(angle_dict.keys())

    sector_angle_dict = dict()

    for i in range(0, len(sorted_slopes)):

        first_index = i
        second_index = i+1

        if(second_index>=len(sorted_slopes)):
            second_index = 0

        first_slope = sorted_slopes[first_index]
        next_slope = sorted_slopes[second_index]


        v1_id = angle_dict[first_slope][0]
        v2_id = angle_dict[next_slope][0]

        center = G.node[commonVertex]
        v1 = G.node[v1_id]
        v2 = G.node[v2_id]

        angular_resolution = next_slope-first_slope

        if(angular_resolution < 0):
            angular_resolution = 360-abs(angular_resolution)

        if(angular_resolution not in sector_angle_dict.keys()):
            sector_angle_dict[angular_resolution] = list()


        sector_angle_dict[angular_resolution].append([v1_id,  v2_id])

    return sector_angle_dict
Ejemplo n.º 2
0
def monotone_draw(G, root, edge_length):
    """ take tree
  assign unique slope
  use tan-1 for slopes
  if path, may consider same slop
  run DFS
  """

    i = 0  # starting with zero angle

    vertexmanager.setCoordinate(G.node[root], 0.0, 0.0)

    for e in nx.dfs_edges(G, root):
        u, v = e
        slp = math.atan(i)

        x_u, y_u = vertexmanager.getCoordinate(G.node[u])

        x_v = x_u + math.cos(slp)
        y_v = y_u + math.sin(slp)

        vertexmanager.setCoordinate(G.node[v], x_v + edge_length,
                                    y_v + edge_length)

        i = i + 1

    return G
Ejemplo n.º 3
0
def rotate(G, angle):

    angle = math.radians(angle)

    for currVertex in nx.nodes(G):
        x, y = vertexmanager.getCoordinate(G.node[currVertex])
        x_rot = x * math.cos(angle) - y * math.sin(angle)
        y_rot = x * math.sin(angle) + y * math.cos(angle)
        vertexmanager.setCoordinate(G.node[currVertex], x_rot, y_rot)

    return G
Ejemplo n.º 4
0
def avg_edge_length(G):

    sum_edge_length = 0.0
    edge_count = len(G.edges())

    for edge in G.edges():

        s,t = edge

        s = G.node[s]
        t = G.node[t]

        x_source1, y_source1  = vertexmanager.getCoordinate(s)
        x_target1, y_target1 = vertexmanager.getCoordinate(t)

        curr_length = math.sqrt((x_source1 - x_target1)**2 + (y_source1 - y_target1)**2)

        sum_edge_length += curr_length

    avg_edge_len = sum_edge_length/edge_count
    return avg_edge_len
Ejemplo n.º 5
0
def scale(G, scaling_factor):

    all_pos = nx.get_node_attributes(G, "pos").values()

    coo_x = sorted([float(p.split(",")[0]) for p in all_pos])
    coo_y = sorted([float(p.split(",")[1]) for p in all_pos])
    min_x = float(coo_x[0])
    min_y = float(coo_y[0])

    for currVertex in nx.nodes(G):
        v = G.node[currVertex]
        v_x, v_y = vertexmanager.getCoordinate(v)
        v_x_scaled = v_x * scaling_factor
        v_y_scaled = v_y * scaling_factor
        vertexmanager.setCoordinate(v, v_x_scaled, v_y_scaled)

    return G
Ejemplo n.º 6
0
def dot_to_txt(input_file, output_file):
    G = nx_read_dot(input_file)
    print(nx.info(G))
    f = open(output_file, 'w')
    f.write(str(len(G.nodes())) + '\n')
    id_to_name = dict()
    name_to_id = dict()
    count = 0
    for node_id in G.nodes():
        node = G.node[node_id]
        name_to_id[node_id] = count
        count += 1
        x, y = vertexmanager.getCoordinate(node)
        f.write(str(x) + ' ' + str(y) + '\n')
    print(name_to_id)
    for edg in G.edges():
        f.write(str(name_to_id[edg[0]]) + " " + str(name_to_id[edg[1]]) + "\n")
    f.close()
Ejemplo n.º 7
0
SubG = nx_read_dot(subgraphpath)

commonVertices = set(set(G.nodes()) & set(SubG.nodes()))

avg_edge_length = avg_edge_length(G)

if len(crossings.count_crossings_single_graph(G)):
    print(graph_name + "  has crossings.")
    print("exiting")
    sys.exit()

v_counter = 0
for commonVertex in commonVertices:
    v_counter += 1

    translation_dx, translation_dy = vertexmanager.getCoordinate(
        G.node[commonVertex])
    translateGraph(G, -translation_dx, -translation_dy)

    a_counter = 0
    # # Extract subcomponents, draw, sacle and attach it to the widest sector
    for currN in set(nx.all_neighbors(SubG, commonVertex)):
        a_counter += 1

        # Compute sector angle and get the largest
        sector_angle_dict = computeSectorsAngles(G, commonVertex)
        sorted_angles = sorted(sector_angle_dict.keys())
        largest_sector_angle = sorted_angles[-1]

        # Get First vertex
        sector_vertices = sector_angle_dict[largest_sector_angle][0]
Ejemplo n.º 8
0
def remove_crossings(G):

    while len(crossings.count_crossings_single_graph(G)):

        crs = crossings.count_crossings_single_graph(G)

        current_crossing_edges = crs[0]

        G_copy = G.copy()

        # Removing edges to separate graph
        G_copy.remove_edges_from(current_crossing_edges)

        # Getting the smaller component to be scaled
        smaller_component_vertices = list([
            c for c in sorted(
                nx.connected_components(G_copy), key=len, reverse=True)
        ][-1])
        # print(smaller_component_vertices)

        main_edge = ""
        main_vertex = ""
        other_vertex = ""

        # Getting the edge connecting the main and the smaller component
        for curr_edge in current_crossing_edges:
            s = curr_edge[0]
            t = curr_edge[1]

            if s in smaller_component_vertices:
                main_vertex = t
                other_vertex = s
                main_edge = curr_edge
                break

            if t in smaller_component_vertices:
                main_vertex = s
                other_vertex = t
                main_edge = curr_edge
                break

        # print("main: "  + main_vertex)
        # print("other: " + other_vertex)
        # print(main_edge)

        # Translating the graph for better scaling
        translation_dx, translation_dy = vertexmanager.getCoordinate(
            G.node[main_vertex])
        translateGraph(G, -translation_dx, -translation_dy)

        subcomponet_vertices = smaller_component_vertices
        subcomponet_edges = G.subgraph(subcomponet_vertices).copy().edges()

        H = nx.Graph()

        H.add_nodes_from(list(subcomponet_vertices))
        H.add_node(main_vertex)
        nx.set_node_attributes(H, nx.get_node_attributes(G, 'pos'), 'pos')

        H.add_edges_from(list(subcomponet_edges))
        H.add_edge(main_vertex, other_vertex)
        # print(nx.info(H))

        # print(nx.get_node_attributes(H, 'pos'))
        scale(H, 0.5)
        # print(nx.get_node_attributes(H, 'pos'))

        nx.set_node_attributes(G, nx.get_node_attributes(H, 'pos'), 'pos')

        # print("changed")
    print("crossings:" + str(len(crossings.count_crossings_single_graph(G))))
    return G