示例#1
0
    def two_edges_are_one(self, edge_1, edge_2):
        """
        Checks if the two edges are actually a single edge.
        :return: True if edges are 100% the same one
        """

        eq1 = LineEquation(slope=edge_1[3].slope,
                           const=edge_1[3].const,
                           edge1=edge_1[0],
                           edge2=edge_1[1])
        eq2 = LineEquation(slope=edge_2[3].slope,
                           const=edge_2[3].const,
                           edge1=edge_2[0],
                           edge2=edge_2[1])


        # Check collision point
        collision_point = LineEquation.get_equation_collision_point(eq1, eq2)
        GLogger.log(logging.DEBUG, Utils.format_log_msg("Found collision point of both edges", point=collision_point, eq1=eq1, eq2=eq2))
        if collision_point == LINES_ALWAYS_MEET:
            # Lines have the same slope + const. Big change they are the same one.
            if LineEquation.check_collision_point(eq1, eq2):
                GLogger.log(logging.DEBUG, "Lines meet and intersect with each other - They are the same line")
                return True
            else:
                GLogger.log(logging.DEBUG, "Lines have the same parameters but we are not sure if they meet")
                return False
        return False
示例#2
0
    def test_trim_data(self, mock_utils):
        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        Utils.read_game_config_file(path.join("..", CONFIG_FILE_PATH))
        Utils.read_graph_config_file(path.join("..", GRAPH_CONFIG_PATH))
        data_handler = GameDataHandler(path.join("../", "graph_config.txt"),
                                       None)
        data_handler.graph.add_node(self.node_1_real.x,
                                    self.node_1_real.y,
                                    serial=self.node_1_real.serial_num)
        data_handler.graph.add_node(self.node_2_unreal.x,
                                    self.node_2_unreal.y,
                                    serial=self.node_2_unreal.serial_num)
        data_handler.graph.add_node(self.node_2_unreal_moved.x,
                                    self.node_2_unreal_moved.y,
                                    serial=self.node_2_unreal_moved.serial_num)
        data_handler.graph.add_node(self.node_3_unreal.x,
                                    self.node_3_unreal.y,
                                    serial=self.node_3_unreal.serial_num)
        data_handler.graph.add_node(self.node_3_unreal_moved.x,
                                    self.node_3_unreal_moved.y,
                                    serial=self.node_3_unreal_moved.serial_num)
        data_handler.graph.add_node(self.node_4_real.x,
                                    self.node_4_real.y,
                                    serial=self.node_4_real.serial_num)
        edge_1 = (self.node_1_real, self.node_2_unreal_moved, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_1_real,
                               edge2=self.node_2_unreal_moved))
        edge_2 = (self.node_2_unreal, self.node_3_unreal_moved, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_2_unreal,
                               edge2=self.node_3_unreal_moved))
        edge_3 = (self.node_3_unreal, self.node_4_real, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_3_unreal,
                               edge2=self.node_4_real))
        edge_4 = (self.node_1_real, self.node_4_real, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_1_real,
                               edge2=self.node_4_real))
        data_handler.extra_edges = [edge_1, edge_2, edge_3]

        data_handler.trim_data()
        self.assertEquals(len(data_handler.edges_to_add), 1)
        self.assertEquals(data_handler.edges_to_add[0][0].serial_num,
                          edge_4[0].serial_num)
        self.assertEquals(data_handler.edges_to_add[0][1].serial_num,
                          edge_4[1].serial_num)
示例#3
0
    def connect_edges(self, edge_1, edge_2):
        """
        The longest distance in an edge is the distance between the real nodes of that edge.
        We clean all existing connections and then connect the two nodes that are furthest from each other.
        :param edge_1, edge_2: An edge defined by a tuple of NodeObjects
        :returns the new edge created
        """
        # Cleaning all existing connection
        # self.log.debug("Connecting edges", edge1=edge_1, edge2=edge_2)
        if edge_1 in self.extra_edges:
            GLogger.log(logging.DEBUG, "Removing edge from extra edges", removed_edge=edge_1)
            self.extra_edges.remove(edge_1)
        if edge_2 in self.extra_edges:
            GLogger.log(logging.DEBUG, "Removing edge from extra edges", removed_edge=edge_2)
            self.extra_edges.remove(edge_2)

        if edge_1[0] is None or edge_1[1] is None or edge_2[0] is None or edge_2[1] is None:
            raise Exception("Cleaning Nodes failed - At least one of the nodes does not exist")

        self.clean_connection(edge_1[0], edge_1[1])
        self.clean_connection(edge_1[1], edge_1[0])
        if edge_2[0] != edge_1[0] or edge_2[1] != edge_2[1]:
            self.clean_connection(edge_2[0], edge_2[1])
            self.clean_connection(edge_2[1], edge_2[0])

        node_1_serial, node_2_serial = self.get_furthest_nodes(edge_1[0], edge_1[1], edge_2[0], edge_2[1])
        node_1 = self.graph.get_node_by_serial(node_1_serial)
        node_2 = self.graph.get_node_by_serial(node_2_serial)

        # connect the right nodes
        self.connect_nodes(node_1, node_2)
        if node_1.x < node_2.x:
            new_edge = (node_1, node_2, edge_1[2],  LineEquation(slope=edge_1[3].slope,
                                                                 const=edge_1[3].const,
                                                                 edge1=node_1,
                                                                 edge2=node_2))
        else:
            new_edge = (node_2, node_1, edge_1[2],  LineEquation(slope=edge_1[3].slope,
                                                                 const=edge_1[3].const,
                                                                 edge1=node_2,
                                                                 edge2=node_1))
        #self.edges_to_add.append(new_edge)
        return new_edge
示例#4
0
    def test_connect_edges_advanced(self, mock_get_node_by_serial, mock_clean,
                                    mock_connect, mock_utils):
        def mock_get_node(serial):
            for node in self.node_list:
                if node.serial_num == serial:
                    return node
            return None

        # Assemble
        mock_get_node_by_serial.side_effect = mock_get_node
        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        data_handler = GameDataHandler(None, None)

        edge_two_real_14 = (self.node_1_real, self.node_4_real,
                            self.node_1_real.slope(self.node_4_real),
                            LineEquation(slope=self.node_1_real.slope(
                                self.node_4_real),
                                         const=1,
                                         edge1=self.node_1_real,
                                         edge2=self.node_4_real))
        edge_left_real_13 = (self.node_1_real, self.node_3_unreal,
                             self.node_1_real.slope(self.node_3_unreal),
                             LineEquation(slope=self.node_1_real.slope(
                                 self.node_3_unreal),
                                          const=1,
                                          edge1=self.node_1_real,
                                          edge2=self.node_3_unreal))
        edge_left_real_12 = (self.node_1_real, self.node_2_unreal,
                             self.node_1_real.slope(self.node_2_unreal),
                             LineEquation(slope=self.node_1_real.slope(
                                 self.node_2_unreal),
                                          const=1,
                                          edge1=self.node_1_real,
                                          edge2=self.node_2_unreal))
        edge_right_real_24 = (self.node_2_unreal, self.node_4_real,
                              self.node_2_unreal.slope(self.node_4_real),
                              LineEquation(slope=self.node_2_unreal.slope(
                                  self.node_4_real),
                                           const=1,
                                           edge1=self.node_2_unreal,
                                           edge2=self.node_4_real))
        edge_two_unreal_23 = (self.node_2_unreal, self.node_3_unreal,
                              self.node_2_unreal.slope(self.node_3_unreal),
                              LineEquation(slope=self.node_2_unreal.slope(
                                  self.node_3_unreal),
                                           const=1,
                                           edge1=self.node_2_unreal,
                                           edge2=self.node_3_unreal))
        edge_two_unreal_13 = (self.node_1_unreal, self.node_3_unreal,
                              self.node_1_unreal.slope(self.node_3_unreal),
                              LineEquation(slope=self.node_1_unreal.slope(
                                  self.node_3_unreal),
                                           const=1,
                                           edge1=self.node_1_unreal,
                                           edge2=self.node_3_unreal))

        # Act + Assert

        # Case 1 - One edge is connected to two real nodes. Other edge only connects to one real node and the other
        #  to a fake node
        edge = data_handler.connect_edges(edge_two_real_14, edge_left_real_13)
        mock_connect.assert_called_with(self.node_1_real, self.node_4_real)
        self.assertEquals(edge_two_real_14[3].edge1, edge[3].edge1)
        self.assertEquals(edge_two_real_14[3].edge2, edge[3].edge2)

        # Case 2 - Both edges each connect to one real node and one fake node. Both real nodes are different
        edge = data_handler.connect_edges(edge_left_real_13,
                                          edge_right_real_24)
        self.assertEquals(edge_two_real_14[3].edge1, edge[3].edge1)
        self.assertEquals(edge_two_real_14[3].edge2, edge[3].edge2)

        # Case 3 - Both edges connect to the same real node. Both edge's other node is  different fake one
        edge = data_handler.connect_edges(edge_left_real_13, edge_left_real_12)
        mock_connect.assert_called_with(self.node_1_real, self.node_3_unreal)
        self.assertEquals(edge_left_real_13[3].edge1, edge[3].edge1)
        self.assertEquals(edge_left_real_13[3].edge2, edge[3].edge2)

        # Case 4 - One edge is connected to a real node and to a fake node. Other edge connects to two fake nodes.
        edge = data_handler.connect_edges(edge_left_real_12,
                                          edge_two_unreal_23)
        mock_connect.assert_called_with(self.node_1_real, self.node_3_unreal)
        self.assertEquals(edge_left_real_13[3].edge1, edge[3].edge1)
        self.assertEquals(edge_left_real_13[3].edge2, edge[3].edge2)

        # Case 5 - Both edge have two fake nodes.
        edge = data_handler.connect_edges(edge_two_unreal_13,
                                          edge_two_unreal_23)
        mock_connect.assert_called_with(self.node_1_unreal, self.node_3_unreal)
        self.assertEquals(edge_two_unreal_13[3].edge1, edge[3].edge1)
        self.assertEquals(edge_two_unreal_13[3].edge2, edge[3].edge2)
示例#5
0
    def test_two_edges_are_one(self, mock_utils):
        # Assemble
        node_1 = NodeObject(serial=1,
                            location={
                                'x': 100,
                                'y': 100
                            },
                            size=1,
                            real=True)
        node_2 = NodeObject(serial=2,
                            location={
                                'x': 200,
                                'y': 200
                            },
                            size=1,
                            real=True)
        node_3 = NodeObject(serial=3,
                            location={
                                'x': 300,
                                'y': 300
                            },
                            size=1,
                            real=True)
        node_4 = NodeObject(serial=4,
                            location={
                                'x': 400,
                                'y': 400
                            },
                            size=1,
                            real=True)
        node_5 = NodeObject(serial=4,
                            location={
                                'x': 100,
                                'y': 400
                            },
                            size=1,
                            real=True)

        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        data_handler = GameDataHandler(None, None)

        edge_35 = (node_3, node_5, node_3.slope(node_5),
                   LineEquation(slope=node_3.slope(node_5),
                                const=1,
                                edge1=node_3,
                                edge2=node_5))
        edge_24 = (node_2, node_4, node_2.slope(node_4),
                   LineEquation(slope=node_2.slope(node_4),
                                const=1,
                                edge1=node_2,
                                edge2=node_4))
        edge_12 = (node_1, node_2, node_1.slope(node_2),
                   LineEquation(slope=node_1.slope(node_2),
                                const=1,
                                edge1=node_1,
                                edge2=node_2))
        edge_13 = (node_1, node_3, node_1.slope(node_3),
                   LineEquation(slope=node_1.slope(node_3),
                                const=1,
                                edge1=node_1,
                                edge2=node_3))
        edge_34 = (node_3, node_4, node_3.slope(node_4),
                   LineEquation(slope=node_3.slope(node_4),
                                const=1,
                                edge1=node_3,
                                edge2=node_4))

        # Act

        res_miss = data_handler.two_edges_are_one(edge_35, edge_24)
        res_not_sure = data_handler.two_edges_are_one(edge_12, edge_34)
        res_hit = data_handler.two_edges_are_one(edge_13, edge_24)
        res_hit_same_point = data_handler.two_edges_are_one(edge_12, edge_13)
        res_same_edge = data_handler.two_edges_are_one(edge_12, edge_12)

        # Assert
        self.assertFalse(res_not_sure)
        self.assertFalse(res_miss)
        self.assertTrue(res_hit)
        self.assertTrue(res_hit_same_point)
        self.assertTrue(res_same_edge)