示例#1
0
文件: Grid.py 项目: nd9600/arc
    def parse_objects(self) -> List[Object]:
        objects = []
        '''
        Grid are
            * complete
            * connected
            * solid bodies
            * each square of the object has the same colour, despite noise or occlusion
        
        we loop over every square in the grid, and if it's not black:
            this will be the start of a new object - it can't be part of an existing object, we'd have processed it already
            if we've already seen it (see below), ignore it
            add it to a queue of positions we need to process for this object,
                record that we've seen it,
                add it to a list of positions that this object has
                find all the neighbouring squares that have the same colour, and for each one
                    if we've seen it already, ignore it
                    add to the "needs processing" queue
                make a new object that that has all of the above-processed position in it, and add that object to the grid's list
        '''
        seen_positions: Set[AbsolutePosition] = set()
        for y, row in enumerate(self.grid_array):
            for x, squareColour in enumerate(row):
                current_position = (x, y)
                if (squareColour ==
                        0  # todo: assumes background is always black
                        or current_position in seen_positions):
                    continue

                positions_for_this_object = []
                object_position_queue: Deque[AbsolutePosition] = deque()
                object_position_queue.append(current_position)
                while len(object_position_queue) > 0:
                    current_position = object_position_queue.pop()
                    positions_for_this_object.append(current_position)
                    seen_positions.add(current_position)

                    new_x = current_position[0]
                    new_y = current_position[1]
                    neighbourhood = self.get_neighbourhood(new_x, new_y)
                    neighbouring_squares_with_the_same_colour = list(
                        filter(
                            lambda pos: self.get_colour(pos[0], pos[1]) ==
                            squareColour, neighbourhood))
                    for position in neighbouring_squares_with_the_same_colour:
                        if position not in seen_positions:
                            object_position_queue.append(position)
                            seen_positions.add(position)

                unseen_object = Object.create_with_absolute_positions(
                    squareColour, positions_for_this_object)
                objects.append(unseen_object)

        return objects
示例#2
0
    def test_making_object_with_absolute_positions(self):
        absolute_positions = [
            (1, 0),
            (1, 1),
            (1, 2),
            (2, 2),
            (2, 3),
        ]
        o = Object.create_with_absolute_positions(1, absolute_positions, 0)
        self.assertEqual((1, 0), o.top_left_offset)

        self.assertEqual([
            (0, 0),
            (0, 1),
            (0, 2),
            (1, 2),
            (1, 3),
        ], o.relative_positions)