Exemplo n.º 1
0
    def test_getting_object_size(self):
        obj_a = Object(1, (0, 0), [
            (0, 0),
            (1, 0),
            (2, 0),
            (3, 0),
            (3, 1),
            (3, 2),
            (2, 2),
            (1, 2),
            (0, 2),
            (0, 1),
        ])
        self.assertEqual(10, ObjectRuntime.get_size(obj_a))

        obj_b = Object(8, (7, 3), [
            (10, 12),
        ])
        self.assertEqual(1, ObjectRuntime.get_size(obj_b))

        obj_c = Object(1, (2, 9), [
            (0, 0),
            (1, 0),
            (1, 1),
            (2, 2),
            (3, 3),
            (4, 4),
        ])
        self.assertEqual(6, ObjectRuntime.get_size(obj_c))
Exemplo n.º 2
0
    def test_what_points_object_contains_when_its_enclosed(self):
        obj_a = Object(2, (0, 0), [(0, 0)])

        obj_b = Object(1, (2, 2), [
            (0, 0),
            (1, 0),
            (2, 0),
            (3, 0),
            (3, 1),
            (3, 2),
            (2, 2),
            (1, 2),
            (0, 2),
            (0, 1),
        ])
        frame_model = ObjectRuntime.make_frame_model_from_objects(
            [obj_a, obj_b], 0)

        self.assertEqual(
            0,
            len(GeometryRuntime.points_contained_by_object(obj_a,
                                                           frame_model)))

        points_contained_by_b = GeometryRuntime.points_contained_by_object(
            obj_b, frame_model)
        self.assertEqual(2, len(points_contained_by_b))
        self.assertSequenceEqual([(3, 3), (4, 3)], points_contained_by_b)
Exemplo n.º 3
0
    def group_same_objects(self) -> Dict[ObjectKind, List[ObjectId]]:
        grouped_objects: Dict[ObjectKind, List[ObjectId]] = defaultdict(list)

        ungrouped_objects = list(self.objects.values())
        obj_a_index = 0
        while True:
            # we don't need to explicitly compare the last object,
            # it'll have already been compared to every other object
            current_object_is_last_object = (
                obj_a_index > 0  # it always needs to group the first object
                and obj_a_index >= len(ungrouped_objects) - 1)
            if current_object_is_last_object:
                break

            obj_a = ungrouped_objects[obj_a_index]

            # if an object is already been grouped, any other objects it's the same as will have been grouped too,
            # so we don't need to compare it
            if obj_a.id in grouped_objects.values():
                obj_a_index = obj_a_index + 1
                continue

            obj_kind = obj_a.get_object_kind()
            grouped_objects[obj_kind].append(obj_a.id)

            for obj_b_index in range(obj_a_index + 1, len(ungrouped_objects)):
                obj_b = ungrouped_objects[obj_b_index]
                if ObjectRuntime.are_objects_the_same_kind(obj_a, obj_b):
                    grouped_objects[obj_kind].append(obj_b.id)
            obj_a_index = obj_a_index + 1
        return dict(grouped_objects)
Exemplo n.º 4
0
    def test_making_frame_model_from_objects_with_non_enclosing_top_left_offsets(
            self):
        obj_a = Object(2, (0, 0), [(0, 0)])

        relative_positions = [
            (0, 0),
            (1, 0),
            (2, 0),
            (3, 0),
            (3, 1),
            (3, 2),
            (2, 2),
            (1, 2),
            (0, 2),
            (0, 1),
        ]
        obj_b = Object(1, (2, 2), relative_positions)
        frame_model = ObjectRuntime.make_frame_model_from_objects(
            [obj_a, obj_b], 0)
        self.assertEqual(5, frame_model.number_of_rows)
        self.assertEqual(6, frame_model.number_of_columns)

        self.assertEqual(0, frame_model.background_colour)

        self.assertSequenceEqual([
            [2, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 1, 1, 1, 1],
            [0, 0, 1, 0, 0, 1],
            [0, 0, 1, 1, 1, 1],
        ],
                                 frame_model.to_grid().grid_array)
Exemplo n.º 5
0
    def test_making_frame_model_from_one_object(self):
        obj = Object(
            7,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (0, 1),
                (1, 1),
                (1, 2),
            ],
        )

        frame_model = ObjectRuntime.make_frame_model_from_objects([obj], 0)
        self.assertEqual(3, frame_model.number_of_rows)
        self.assertEqual(2, frame_model.number_of_columns)

        self.assertEqual(0, frame_model.background_colour)

        self.assertSequenceEqual([
            [7, 7],
            [7, 7],
            [0, 7],
        ],
                                 frame_model.to_grid().grid_array)
Exemplo n.º 6
0
    def test_making_frame_model_from_one_object_inside_another(self):
        obj_a = Object(
            7,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 1),
                (2, 2),
                (1, 2),
                (0, 2),
                (0, 1),
            ],
        )

        obj_b = Object(
            5,
            (2, 2),
            [(0, 0)],
        )

        frame_model = ObjectRuntime.make_frame_model_from_objects(
            [obj_a, obj_b], 0)
        self.assertEqual(3, frame_model.number_of_rows)
        self.assertEqual(3, frame_model.number_of_columns)

        self.assertEqual(0, frame_model.background_colour)

        self.assertSequenceEqual([
            [7, 7, 7],
            [7, 5, 7],
            [7, 7, 7],
        ],
                                 frame_model.to_grid().grid_array)
Exemplo n.º 7
0
    def test_getting_the_biggest_object(self):
        obj_a = Object(1, (0, 0), [
            (0, 0),
            (1, 1),
            (2, 2),
        ])
        obj_b = Object(2, (0, 1), [
            (0, 0),
            (1, 1),
            (2, 2),
            (3, 3),
        ])
        obj_c = Object(3, (0, 2), [
            (0, 0),
            (1, 1),
        ])

        frame_model = ObjectRuntime.make_frame_model_from_objects(
            [obj_a, obj_b, obj_c], 0)

        # get list of objects
        # find id of biggest object
        #
        biggest_object = ListRuntime.map_list(
            lambda obj: (obj.id, ObjectRuntime.get_size(obj)),
            frame_model.objects_as_list())
        print("\n")
        print(biggest_object)

        new_frame_model = FrameModelRuntime.change_objects(
            frame_model,
            ListRuntime.filter_list(lambda obj: obj == biggest_object,
                                    frame_model.objects_as_list()))
        new_frame_model.to_grid().plot()
        self.assertEqual(1, len(list(new_frame_model.objects.values())))
        self.assertEqual(obj_b, list(new_frame_model.objects.values())[0])
Exemplo n.º 8
0
    def test_what_points_object_contains_when_its_not_enclosed(self):
        obj_a = Object(1, (1, 1), [
            (0, 0),
            (1, 0),
            (2, 0),
            (3, 0),
            (3, 2),
            (2, 2),
            (1, 2),
            (0, 2),
            (0, 1),
        ])
        obj_b = Object(2, (5, 5), [(0, 0)])

        frame_model = ObjectRuntime.make_frame_model_from_objects(
            [obj_a, obj_b], 0, False)
        points_contained_by_b = GeometryRuntime.points_contained_by_object(
            obj_a, frame_model)
        self.assertEqual(0, len(points_contained_by_b))
Exemplo n.º 9
0
    def test_what_points_object_contains_when_theres_another_object_in_the_way(
            self):
        obj_a = Object(1, (1, 1), [
            (0, 0),
            (1, 0),
            (2, 0),
            (3, 0),
            (3, 2),
            (3, 3),
            (2, 2),
            (1, 2),
            (0, 2),
            (0, 1),
        ])
        obj_b = Object(2, (5, 5), [(0, 0)])
        obj_c = Object(3, (4, 2), [(0, 0)])

        frame_model = ObjectRuntime.make_frame_model_from_objects(
            [obj_a, obj_b, obj_c], 0, False)
        points_contained_by_b = GeometryRuntime.points_contained_by_object(
            obj_a, frame_model)
        self.assertEqual(0, len(points_contained_by_b))
Exemplo n.º 10
0
    def test_is_point_enclosed_in_cropped_rectangle(self):
        relative_positions = [
            (0, 0),
            (1, 0),
            (2, 0),
            (3, 0),
            (3, 1),
            (3, 2),
            (2, 2),
            (1, 2),
            (0, 2),
            (0, 1),
        ]
        obj = Object(1, (0, 0), relative_positions)
        frame_model = ObjectRuntime.make_frame_model_from_objects([obj], 0)

        for pos in relative_positions:
            self.assertFalse(
                GeometryRuntime.is_point_fully_enclosed(pos, frame_model))
        self.assertTrue(
            GeometryRuntime.is_point_fully_enclosed((1, 1), frame_model))
        self.assertTrue(
            GeometryRuntime.is_point_fully_enclosed((2, 1), frame_model))
Exemplo n.º 11
0
    def test_is_point_enclosed_in_rectangle(self):
        obj_a = Object(2, (0, 0), [(0, 0)])

        relative_positions = [
            (0, 0),
            (1, 0),
            (2, 0),
            (3, 0),
            (3, 1),
            (3, 2),
            (2, 2),
            (1, 2),
            (0, 2),
            (0, 1),
        ]
        obj_b = Object(1, (2, 2), relative_positions)
        frame_model = ObjectRuntime.make_frame_model_from_objects(
            [obj_a, obj_b], 0)

        self.assertFalse(
            GeometryRuntime.is_point_fully_enclosed((0, 0), frame_model))
        for pos in obj_b.convert_to_absolute_positions():
            self.assertFalse(
                GeometryRuntime.is_point_fully_enclosed(pos, frame_model))

        self.assertFalse(
            GeometryRuntime.is_point_fully_enclosed((1, 0), frame_model))
        self.assertFalse(
            GeometryRuntime.is_point_fully_enclosed((1, 1), frame_model))
        self.assertFalse(
            GeometryRuntime.is_point_fully_enclosed((0, 1), frame_model))

        self.assertTrue(
            GeometryRuntime.is_point_fully_enclosed((3, 3), frame_model))
        self.assertTrue(
            GeometryRuntime.is_point_fully_enclosed((4, 3), frame_model))
Exemplo n.º 12
0
    def test_matching_one_object_out_of_three_across_frames(self):
        obj_1 = Object(
            1,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            1
        )
        obj_2 = Object(
            2,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            2
        )
        obj_3 = Object(
            1,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 2),
            ],
            0,
            3
        )

        obj_1b = Object(
            1,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            4
        )
        obj_2b = Object(
            3,
            (1, 1),
            [
                (0, 0),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            5
        )
        obj_3b = Object(
            1,
            (1, 1),
            [
                (0, 1),
                (1, 0),
                (2, 0),
                (2, 1),
            ],
            0,
            6
        )

        original_frame_model = FrameModel(
            5,
            5,
            0,
            [
                obj_1,
                obj_2,
                obj_3,
            ]
        )
        second_frame_model = FrameModel(
            5,
            5,
            0,
            [
                obj_1b,
                obj_2b,
                obj_3b,
            ]
        )

        self.assertSequenceEqual(
            [
                4,
                5,
                6
            ],
            list(second_frame_model.objects.keys())
        )

        second_frame_model_after_match = ObjectRuntime.match_objects_in_second_frame_to_those_in_first(
            original_frame_model, second_frame_model
        )
        matched_second_object = second_frame_model_after_match.objects[obj_1.id]

        self.assertSequenceEqual(
            [
                1,
                5,
                6
            ],
            list(second_frame_model_after_match.objects.keys())
        )
        self.assertEqual(
            obj_1.colour,
            matched_second_object.colour,
        )
        self.assertEqual(
            obj_1.top_left_offset,
            matched_second_object.top_left_offset,
        )
        self.assertSequenceEqual(
            obj_1.relative_positions,
            matched_second_object.relative_positions,
        )