Example #1
0
    def test_update(self):
        obj = object()
        # In [(-10, -10), (0, 0)] quadrant
        bbox = BoundingBox([Vec2(-3, -3), Vec2(-1, -1)])
        self.tree.insert(bbox, obj)

        # In [(0, 0), (10, 10)] quadrant
        new_bbox = BoundingBox([Vec2(1, 1), Vec2(4, 4)])
        self.tree.update(new_bbox, obj)

        self.assertEqual(self._get_nodes_data(self.tree), {
            "level": 0, "nodes": [
                {"level": 1},
                {"level": 1, "nodes": [
                    {"level": 2},
                    {"level": 2},
                    {"level": 2},
                    {"level": 2, "nodes": [
                        {"level": 3},
                        {"level": 3},
                        {"level": 3},
                        {"level": 3, "objects": [obj]}
                    ]},
                ]},
                {"level": 1},
                {"level": 1},
            ]
        })
 def test_corner_intersection(self):
     b1 = BoundingBox([Vec2(0, 0), Vec2(2, 2)])
     b2 = BoundingBox([Vec2(2, 2), Vec2(5, 7)])
     self.assertFalse(intersects(b1, b2))
     self.assertTrue(intersects(b1, b2, border=True))
     self.assertFalse(contains(b1, b2))
     self.assertFalse(contains(b2, b1))
Example #3
0
    def test_insert_two_quadrants(self):
        bbox = BoundingBox([Vec2(1, -1), Vec2(4, 4)])
        self.tree.insert(bbox, bbox)

        self.assertEqual(self._get_nodes_data(self.tree), {
            "level": 0, "nodes": [
                {"level": 1},
                {"level": 1, "nodes": [
                    {"level": 2},
                    {"level": 2},
                    {"level": 2},
                    {"level": 2, "nodes": [
                        {"level": 3},
                        {"level": 3},
                        {"level": 3},
                        {"level": 3, "objects": [bbox]}
                    ]},
                ]},
                {"level": 1, "nodes": [
                    {"level": 2, "nodes": [
                        {"level": 3, "objects": [bbox]},
                        {"level": 3},
                        {"level": 3},
                        {"level": 3},
                    ]},
                    {"level": 2},
                    {"level": 2},
                    {"level": 2},
                ]},
                {"level": 1},
            ]
        })
 def test_border_intersection(self):
     bbox = BoundingBox([Vec2(0, 0), Vec2(2, 2)])
     pol = Polygon([Vec2(0, 0), Vec2(1, 0), Vec2(1, -2)])
     self.assertFalse(intersects(bbox, pol))
     self.assertTrue(intersects(bbox, pol, border=True))
     self.assertFalse(contains(pol, bbox))
     self.assertFalse(contains(bbox, pol))
 def test_bbox_in_circle_border(self):
     # Egyptian triangle (3, 4, 5)
     c = Circle(Vec2(0, 0), 5)
     r = BoundingBox([Vec2(-3, -4), Vec2(3, 4)])
     self.assertTrue(intersects(c, r))
     self.assertTrue(contains(c, r))
     self.assertFalse(contains(r, c))
 def test_circle_in_bbox_border(self):
     # contains have no need for `border` attributes. It always includes
     # border points.
     c = Circle(Vec2(0, 0), 4)
     r = BoundingBox.from_center(Vec2(0, 0), 8, 8)
     self.assertTrue(intersects(c, r))
     self.assertTrue(contains(r, c))
     self.assertFalse(contains(c, r))
Example #7
0
    def test_remove(self):
        # In [(0, 0), (10, 10)] quadrant
        bbox = BoundingBox([Vec2(1, 1), Vec2(4, 4)])
        self.tree.insert(bbox, bbox)
        self.tree.remove(bbox)

        self.assertEqual(self._get_nodes_data(self.tree), {
            "level": 0})
 def test_bbox_in_pol(self):
     bbox = BoundingBox([Vec2(0, 0), Vec2(2, 2)])
     pol = Polygon(
         [Vec2(-1, -1),
          Vec2(-1, 5),
          Vec2(5, 5),
          Vec2(7, 4),
          Vec2(3, -1)])
     self.assertTrue(intersects(bbox, pol))
     self.assertTrue(contains(pol, bbox))
     self.assertFalse(contains(bbox, pol))
Example #9
0
    def test_query(self):
        x = Circle(Vec2(30, 30), 10)
        y = Circle(Vec2(-10, -10), 10)
        z = Circle(Vec2(0, 0), 10)
        self.tree.insert(x, x)
        self.tree.insert(y, y)
        self.tree.insert(z, z)

        # Helper to ignore results order
        o = lambda x: list(sorted(x, key=lambda x: x.center.x))

        # Contains circle
        results = self.tree.query(
            BoundingBox.from_center(Vec2(30, 30), 22, 22))
        self.assertEqual(results, [x])
        # Intersect circle
        results = self.tree.query(
            BoundingBox.from_center(Vec2(20, 40), 10, 10))
        self.assertEqual(results, [x])
        # Is contained in circle
        results = self.tree.query(
            BoundingBox.from_center(Vec2(30, 30), 10, 10))
        self.assertEqual(results, [x])

        # Contains 2 circles
        results = self.tree.query(
            BoundingBox.from_center(Vec2(-5, -5), 32, 32))
        self.assertEqual(o(results), o([y, z]))
        # Intersect 2 circles
        results = self.tree.query(
            BoundingBox.from_center(Vec2(-10, 0), 2, 2))
        self.assertEqual(o(results), o([y, z]))
        # Contained in 2 circles
        results = self.tree.query(
            BoundingBox.from_center(Vec2(-5, -5), 2, 2))
        self.assertEqual(o(results), o([y, z]))

        # Contained in 1 of circles
        results = self.tree.query(
            BoundingBox.from_center(Vec2(-10, -10), 2, 2))
        self.assertEqual(results, [y])

        # Contain all circles
        results = self.tree.query(
            BoundingBox.from_center(Vec2(0, 0), 80, 80))
        self.assertEqual(o(results), o([x, y, z]))
Example #10
0
    def test_insert_single_quadrant(self):
        # In [(0, 0), (10, 10)] quadrant
        bbox = BoundingBox([Vec2(1, 1), Vec2(4, 4)])
        self.tree.insert(bbox, bbox)

        self.assertEqual(self._get_nodes_data(self.tree), {
            "level": 0, "nodes": [
                {"level": 1},
                {"level": 1, "nodes": [
                    {"level": 2},
                    {"level": 2},
                    {"level": 2},
                    {"level": 2, "nodes": [
                        {"level": 3},
                        {"level": 3},
                        {"level": 3},
                        {"level": 3, "objects": [bbox]}
                    ]},
                ]},
                {"level": 1},
                {"level": 1},
            ]
        })
 def test_intersection(self):
     c = Circle(Vec2(0, 0), 5)
     r = BoundingBox.from_center(Vec2(0, 6), 4, 4)
     self.assertTrue(intersects(c, r))
     self.assertFalse(contains(c, r))
     self.assertFalse(contains(r, c))
 def test_intersects(self):
     b1 = BoundingBox([Vec2(0, 0), Vec2(3, 4)])
     b2 = BoundingBox([Vec2(2, 2), Vec2(5, 7)])
     self.assertTrue(intersects(b1, b2))
     self.assertFalse(contains(b1, b2))
     self.assertFalse(contains(b2, b1))
 def test_no_intersection(self):
     c = Circle(Vec2(0, 0), 2)
     r = BoundingBox.from_center(Vec2(10, 10), 2, 2)
     self.assertFalse(intersects(c, r))
     self.assertFalse(contains(c, r))
     self.assertFalse(contains(r, c))
 def test_contains(self):
     b1 = BoundingBox.from_center(Vec2(0, 0), 4, 4)
     b2 = BoundingBox.from_center(Vec2(0, 0), 2, 2)
     self.assertTrue(intersects(b1, b2))
     self.assertTrue(contains(b1, b2))
     self.assertFalse(contains(b2, b1))
 def test_no_intersection(self):
     b1 = BoundingBox([Vec2(0, 0), Vec2(2, 2)])
     b2 = BoundingBox([Vec2(3, 3), Vec2(4, 4)])
     self.assertFalse(intersects(b1, b2))
     self.assertFalse(contains(b1, b2))
     self.assertFalse(contains(b2, b1))
 def test_corner_intersection(self):
     # Egyptian triangle (3, 4, 5)
     c = Circle(Vec2(0, 0), 5)
     r = BoundingBox([Vec2(3, 4), Vec2(5, 6)])
     self.assertFalse(intersects(c, r))
     self.assertTrue(intersects(c, r, border=True))
 def test_border_intersection(self):
     c = Circle(Vec2(0, 0), 2)
     r = BoundingBox.from_center(Vec2(0, 4), 4, 4)
     self.assertFalse(intersects(c, r))
     self.assertTrue(intersects(c, r, border=True))
 def test_bbox_in_circle(self):
     c = Circle(Vec2(0, 0), 10)
     r = BoundingBox.from_center(Vec2(0, 0), 2, 2)
     self.assertTrue(intersects(c, r))
     self.assertTrue(contains(c, r))
     self.assertFalse(contains(r, c))
 def test_no_intersection(self):
     bbox = BoundingBox([Vec2(0, 0), Vec2(2, 2)])
     pol = Polygon([Vec2(5, 5), Vec2(3, 4), Vec2(7, 5)])
     self.assertFalse(intersects(bbox, pol))
     self.assertFalse(contains(bbox, pol))
     self.assertFalse(contains(pol, bbox))