Exemple #1
0
    def testUnion(self):
        ra = Rect(0, 0, 10, 10)
        rb = Rect(-10, -10, 1, 1)
        x, y, w, h = ra.union(rb).extent()
        self.assertEquals(x, -10)
        self.assertEquals(y, -10)
        self.assertEquals(w, 20)
        self.assertEquals(h, 20)

        for i in range(1000):
            a, b = G.rect(), G.rect()
            u = a.union(b)
            self.assertTrue(a.intersect(u).area() > 0)
            self.assertTrue(u.intersect(a).area() > 0)
            self.assertTrue(b.intersect(u).area() > 0)
            self.assertTrue(u.intersect(b).area() > 0)
            self.assertTrue(
                u.area() >= (max(a.area(), b.area())),
                "union area (iter %d) fail %f >= %f" %
                (i, u.area(), (max(a.area(), b.area()))))

            c, d = G.disjointPair()
            u2 = c.union(d)
            self.assertTrue(c.intersect(u2).area() > 0)
            self.assertTrue(u2.intersect(c).area() > 0)
            self.assertTrue(d.intersect(u2).area() > 0)
            self.assertTrue(u2.intersect(d).area() > 0)
            self.assertTrue(u2.area() > c.area())
            self.assertTrue(u2.area() > d.area())
            self.assertTrue(u2.area() >= (c.area() + d.area()))
Exemple #2
0
    def testIntersection(self):
        ra = Rect(0, 0, 10, 10)
        rb = Rect(5, 5, 15, 15)
        res = ra.intersect(rb)
        x, y, w, h = res.extent()
        self.assertEquals(x, 5)
        self.assertEquals(y, 5)
        self.assertEquals(w, 5)
        self.assertEquals(h, 5)
        self.assertEquals(res.area(), 25)

        rc = Rect(0, 0, 10, 10)
        rd = Rect(11, 11, 21, 21)
        res2 = rc.intersect(rd)
        self.assertEquals(res2.area(), 0)
        self.assertTrue(res2 is NullRect)

        for i in range(1000):
            a, b = G.intersectingPair()
            self.assertTrue(a.intersect(b).area() > 0.0)
            c, d = G.disjointPair()
            self.assertEquals(c.intersect(d).area(), 0)

        self.assertTrue(ra.intersect(NullRect) is NullRect)
        self.assertTrue(NullRect.intersect(ra) is NullRect)
Exemple #3
0
 def rect(self, size=10.0):
     x, y, w, h = rr(), rr(), random.uniform(0.0, size), random.uniform(
         0.0, size)
     r = Rect(x, y, x + w, y + h)
     assert (not r.swapped_x)
     assert (not r.swapped_y)
     return r
Exemple #4
0
    def _invariants(self, node, seen):
        idx = node.index

        self.assertTrue(idx not in seen)

        seen[idx] = True

        if node.holds_leaves():
            #print("node: %d, children: %r" % (node.index, [c.index for c in node.children()]))
            self.assertTrue(node.nchildren() == 0
                            or node.get_first_child().is_leaf())
            for c in node.children():
                #print(c.index)
                self.assertTrue(c.is_leaf())
                self.assertTrue(isinstance(c.leaf_obj(), TstO))
        else:
            for c in node.children():
                self.assertTrue(not c.is_leaf())
        self.assertEquals(idx, node.index)

        r = Rect(node.rect.x, node.rect.y, node.rect.xx, node.rect.yy)
        for c in node.children():
            assert r.does_contain(c.rect)

        self.assertEquals(idx, node.index)

        for c in node.children():
            if not c.is_leaf(): self._invariants(c, seen)

        self.assertEquals(idx, node.index)
Exemple #5
0
 def disjointWith(self, ra):
     ax, ay, aw, ah = ra.extent()
     w, h = rr(), rr()
     distsq = max(w * w + h * h, aw * aw + ah * ah)
     dist = 2.0 * math.sqrt(distsq) + random.uniform(0.1, 1.0)
     ang = random.uniform(0.0, 2.0 * math.pi)
     x = math.cos(ang) * dist
     y = math.sin(ang) * dist
     return Rect(ax + x, ay + y, ax + x + w, ay + y + h)
Exemple #6
0
 def add_transformation(self, transform_matrix):
     """Adds a transformation to all tiles"""
     self.rtree = RTree()
     for single_tile in self.single_tiles:
         single_tile.add_transformation(transform_matrix)
         bbox = single_tile.get_bbox()
         # pyrtree uses the (x_min, y_min, x_max, y_max) notation
         self.rtree.insert(single_tile,
                           Rect(bbox[0], bbox[2], bbox[1], bbox[3]))
Exemple #7
0
 def __init__(self, single_tiles, blend_type="NO_BLENDING"):
     """Receives a number of image paths, and for each a transformation matrix"""
     self.blend_type = self.BLEND_TYPE[blend_type]
     self.single_tiles = single_tiles
     # Create an RTree of the bounding boxes of the tiles
     self.rtree = RTree()
     for t in self.single_tiles:
         bbox = t.get_bbox()
         # pyrtree uses the (x_min, y_min, x_max, y_max) notation
         self.rtree.insert(t, Rect(bbox[0], bbox[2], bbox[1], bbox[3]))
Exemple #8
0
 def testCons(self):
     r = Rect(0, 0, 10, 10)
     self.assertTrue(r is not None)
     self.assertTrue(r is not NullRect)
Exemple #9
0
 def intersectingWith(self, ra):
     rb_x = random.uniform(ra.x, ra.xx)
     rb_y = random.uniform(ra.y, ra.yy)
     return Rect(rb_x, rb_y, rb_x + rr(), rb_y + rr())
Exemple #10
0
    def crop(self, from_x, from_y, to_x, to_y):
        if len(self.single_tiles) == 0:
            return None, None

        # Distinguish between the different types of blending
        if self.blend_type == 0:  # No blending
            res = np.zeros(
                (round(to_y + 1 - from_y), round(to_x + 1 - from_x)),
                dtype=np.uint8)
            # render only relevant parts, and stitch them together
            # filter only relevant tiles using rtree
            rect_res = self.rtree.query_rect(Rect(from_x, from_y, to_x, to_y))
            for rtree_node in rect_res:
                if not rtree_node.is_leaf():
                    continue
                t = rtree_node.leaf_obj()
                t_img, t_start_point, _ = t.crop(from_x, from_y, to_x, to_y)
                if t_img is not None:
                    res[t_start_point[1] - from_y:t_img.shape[0] +
                        (t_start_point[1] - from_y),
                        t_start_point[0] - from_x:t_img.shape[1] +
                        (t_start_point[0] - from_x)] = t_img

        elif self.blend_type == 1:  # Averaging
            # Do the calculation on a uint16 image (for overlapping areas), and convert to uint8 at the end
            res = np.zeros(
                (round(to_y + 1 - from_y), round(to_x + 1 - from_x)),
                dtype=np.uint16)
            res_mask = np.zeros(
                (round(to_y + 1 - from_y), round(to_x + 1 - from_x)),
                dtype=np.uint8)

            # render only relevant parts, and stitch them together
            # filter only relevant tiles using rtree
            rect_res = self.rtree.query_rect(Rect(from_x, from_y, to_x, to_y))
            for rtree_node in rect_res:
                if not rtree_node.is_leaf():
                    continue
                t = rtree_node.leaf_obj()
                t_img, t_start_point, t_mask = t.crop(from_x, from_y, to_x,
                                                      to_y)
                if t_img is not None:
                    res[t_start_point[1] - from_y:t_img.shape[0] +
                        (t_start_point[1] - from_y),
                        t_start_point[0] - from_x:t_img.shape[1] +
                        (t_start_point[0] - from_x)] += t_img
                    #t_start_point[0] - from_x: t_img.shape[1] + t_start_point[0] - from_x] += t_mask * 50
                    res_mask[t_start_point[1] - from_y:t_img.shape[0] +
                             (t_start_point[1] - from_y),
                             t_start_point[0] - from_x:t_img.shape[1] +
                             (t_start_point[0] - from_x)] += t_mask

            # Change the values of 0 in the mask to 1, to avoid division by 0
            res_mask[res_mask == 0] = 1
            res = res / res_mask
            res = res.astype(np.uint8)

        elif self.blend_type == 2:  # Linear averaging
            # Do the calculation on a uint32 image (for overlapping areas), and convert to uint8 at the end
            # For each pixel use the min-distance to an edge as a weight, and store the
            # average the outcome according to the weight
            res = np.zeros(
                (round(to_y + 1 - from_y), round(to_x + 1 - from_x)),
                dtype=np.uint32)
            res_weights = np.zeros(
                (round(to_y + 1 - from_y), round(to_x + 1 - from_x)),
                dtype=np.uint16)

            # render only relevant parts, and stitch them together
            # filter only relevant tiles using rtree
            rect_res = self.rtree.query_rect(Rect(from_x, from_y, to_x, to_y))
            for rtree_node in rect_res:
                if not rtree_node.is_leaf():
                    continue
                t = rtree_node.leaf_obj()
                t_img, t_start_point, t_weights = t.crop_with_distances(
                    from_x, from_y, to_x, to_y)
                if t_img is not None:
                    res[t_start_point[1] - from_y:t_img.shape[0] +
                        (t_start_point[1] - from_y),
                        t_start_point[0] - from_x:t_img.shape[1] +
                        (t_start_point[0] - from_x)] += t_img * t_weights
                    res_weights[t_start_point[1] - from_y:t_img.shape[0] +
                                (t_start_point[1] - from_y),
                                t_start_point[0] - from_x:t_img.shape[1] +
                                (t_start_point[0] - from_x)] += t_weights

            # Change the weights that are 0 to 1, to avoid division by 0
            res_weights[res_weights < 1] = 1
            res = res / res_weights
            res = res.astype(np.uint8)

        return res, (from_x, from_y)
Exemple #11
0
    x2 = rectangepoints[1][0]
    y2 = rectangepoints[1][1]
    # #print x1
    # wi=x2-x1
    # he=y2-y1
    #
    # p = plt.Rectangle(
    #  (x1, y1), wi, he,angle=0 ,fill=False
    #
    #  )
    # ax.add_patch(p)

    st = "tag" + str(i)
    i += 1
    #print st
    t.insert(st, Rect(x1, y1, x2, y2))
'''
querying
'''

point_res = t.query_point((2, 1025))
# rect_res = t.query_rect( Rect(0,1020,4,1040) )

rectx1 = 0
recty1 = 1020
rectx2 = 4
recty2 = 1040

tempx1 = -1000.0
tempy1 = 1030.0
tempx2 = -972.5
Exemple #12
0
from pyrtree import Rect, RTree

DATA_SIZE = 100

# PyRTree Insert and Search Test. This PyRTree is query only index.

obj = []
for x in range(DATA_SIZE):
    # Create objects with rectangles for objs, Rect(0, 0, 1, 1), Rect(1, 1, 2, 2); name obj0, obj1, ...
    obj.append([Rect(0 + x, 0 + x, 1 + x, 1 + x), 'obj' + x.__str__()])

rtree = RTree() # tree creation

# pyrtree uses the Rect (x_min, y_min, x_max, y_max) notation
for x in range(DATA_SIZE):
    rtree.insert(obj[x], obj[x][0]) # element insertion with a given box

# Query the tree
rect_res = rtree.query_rect(Rect(1, 1, 4, 4) )

# Traverse the query result
for rtree_node in rect_res:
    if not rtree_node.is_leaf():
        continue
    t = rtree_node.leaf_obj()
    print(t[0].x, t[0].y, t[0].xx, t[0].yy, t[1])

# Get the internal nodes which are at the deepest level of the tree. Each internal contains a number of leaf nodes
for nodes in rtree.get_last_level():
    print(nodes)
Exemple #13
0
 def rect_from_coords(self, x1, y1, x2, y2):
     r = Rect(x1, y1, x2, y2)
     assert (not r.swapped_x)
     assert (not r.swapped_y)
     return r