class MapMerger: def __init__(self): self.graph_generator = GraphGenerator() self.graph1 = self.graph_generator.generate_circular( 15, 15, 3 * 15 - 6, 3 * 15 - 6, 50) self.graph1.polar_sort_all_graph_edges() self.nx_graph1 = self.graph1.get_nx_graph() self.graph_generator.remove_random_edges(3, 7) self.graph_generator.change_all_nodes_position(2) self.graph2 = self.graph_generator.graph self.nx_graph2 = self.graph2.get_nx_graph() part_one = [deepcopy(node) for node in self.graph1.nodes] part_two = [deepcopy(node) for node in self.graph2.nodes] edges = [] for node in part_one: node.index = node.index * 10 + 0 for node in part_two: node.index = node.index * 10 + 1 for node1 in part_one: node1_prev_index = node1.index // 10 node1_adjacent_faces = Polygons( polygons=self.graph1.get_node_adjacent_faces(node1_prev_index)) turning_function1 = node1_adjacent_faces.get_all_polygons_turning_function( ) for node2 in part_two: node2_prev_index = node2.index // 10 node2_adjacent_faces = Polygons( polygons=self.graph2.get_node_adjacent_faces( node2_prev_index)) turning_function2 = node2_adjacent_faces.get_all_polygons_turning_function( ) edges.append( Edge( node1.index, node2.index, self._turning_functions_similarity( turning_function1, turning_function2))) print(edges) self.bipartite_graph = BipartiteGraph(part_one, part_two, edges) self.bipartite_nx_graph = self.bipartite_graph.get_maximum_weighted_matching( ) def _turning_functions_similarity(self, turning_function1, turning_function2): length = min(len(turning_function1), len(turning_function2)) ret = 360 * length for i in range(length): ret -= abs(turning_function1[i] - turning_function2[i]) return ret def show(self): pos1 = nx.get_node_attributes(self.nx_graph1, 'pos') pos2 = nx.get_node_attributes(self.nx_graph2, 'pos') pos3 = nx.get_node_attributes(self.bipartite_nx_graph, 'pos') plt.figure(1) nx.draw(self.nx_graph1, pos1, with_labels=True) plt.figure(2) nx.draw(self.nx_graph2, pos2, with_labels=True) plt.figure(3) nx.draw(self.bipartite_nx_graph, pos3, with_labels=True) plt.show()
import networkx as nx from matplotlib import pyplot as plt from graph_node import Node from vector import Vector from math import atan2 from graph_generator import GraphGenerator from graph import Graph graph_generator = GraphGenerator() graph1 = graph_generator.generate_circular(15, 15, 3 * 15 - 6, 3 * 15 - 6, 50) nx_graph1 = graph1.get_nx_graph() graph_generator.remove_random_edges(3, 7) graph_generator.change_all_nodes_position(2) graph2 = graph_generator.graph nx_graph2 = graph2.get_nx_graph() node1, node2, node3, node4 = Node(0, 10, 10), Node(1, 10, 20), Node(2, 20, 20), Node(3, 20, 10) # graph = Graph([node1, node2, node3, node4], [(0, 1), (1, 2), (2, 3), (3, 0)]) # nx_graph1.add_node(node1.index, pos=(node1.x, node1.y)) # nx_graph1.add_node(node2.index, pos=(node2.x, node2.y)) # nx_graph1.add_node(node3.index, pos=(node3.x, node3.y)) # nx_graph1.add_node(node4.index, pos=(node4.x, node4.y)) # nx_graph1.add_edge(1, 2) # nx_graph1.add_edge(2, 3) # nx_graph1.add_edge(3, 4)