Beispiel #1
0
    def covers( self, this, that ):
        """Return true if the given quadrants does intersects each other."""

        # Convert quadrants ((x,y),w) as box ((a,b),(c,d)).
        this_box = as_box(this)
        that_box = as_box(that)

        # Convert boxes as list of edges.
        this_segments = tuple(utils.tour(as_rect(this)))
        that_segments = tuple(utils.tour(as_rect(that)))

        # If at least one of the segment of "this" intersects with "that".
        intersects = any( geometry.segment_intersection(s0,s1) for s0 in this_segments for s1 in that_segments )

        # Transform nested list of segments in flat list of points without any duplicates.
        this_points = as_rect(this)
        that_points = as_rect(that)

        # If all the points of "this" are inside "that".
        # Note: what we would want to test here is if ALL the points are comprised,
        #       as the case where at least one is already tested by the "intersects" stage.
        #       But we use an "any" anyway, because it is sufficient in this case and
        #       that testing all the points takes more time.
        this_in = any( geometry.in_box(p,this_box) for p in that_points )
        that_in = any( geometry.in_box(p,that_box) for p in this_points )

        return intersects or this_in or that_in
Beispiel #2
0
    def covers(self, this, that):
        """Return true if the given quadrants does intersects each other."""

        # Convert quadrants ((x,y),w) as box ((a,b),(c,d)).
        this_box = as_box(this)
        that_box = as_box(that)

        # Convert boxes as list of edges.
        this_segments = tuple(utils.tour(as_rect(this)))
        that_segments = tuple(utils.tour(as_rect(that)))

        # If at least one of the segment of "this" intersects with "that".
        intersects = any(
            geometry.segment_intersection(s0, s1) for s0 in this_segments
            for s1 in that_segments)

        # Transform nested list of segments in flat list of points without any duplicates.
        this_points = as_rect(this)
        that_points = as_rect(that)

        # If all the points of "this" are inside "that".
        # Note: what we would want to test here is if ALL the points are comprised,
        #       as the case where at least one is already tested by the "intersects" stage.
        #       But we use an "any" anyway, because it is sufficient in this case and
        #       that testing all the points takes more time.
        this_in = any(geometry.in_box(p, this_box) for p in that_points)
        that_in = any(geometry.in_box(p, that_box) for p in this_points)

        return intersects or this_in or that_in
Beispiel #3
0
    def status( self, point, quadrant ):
        """Return Status.Empty if the given point can be appended in the given quadrant."""

        assert(point is not None)
        assert(len(point) == 2)
        assert(quadrant is not None)
        assert(len(quadrant) == 2)

        box = as_box( quadrant )

        # if the point lies inside the given quadrant
        if geometry.in_box( point, box):
            if self.residents[quadrant]:
                # external: a quadrant that already contains a point
                assert( not self.children[quadrant] )
                return self.Status.Leaf
            elif self.children[quadrant]:
                # internal: a quadrant that contains other quadrants
                return self.Status.Node
            else:
                # empty: there is not point yet in this quadrant
                return self.Status.Empty
        else:
            # point is out of the quadrant
            return self.Status.Out
Beispiel #4
0
    def query( self, query_quad, at_quad = None ):
        """Return all the points (currently attached to the quad tree) that are located within the query_quad quadrant."""
        if not at_quad:
            at_quad = self.root

        query_box = as_box(query_quad)

        # If we ask for a quadrant that intersects with the current one.
        if self.covers( query_quad, at_quad ):
            # If the current quadrant contains sub-quadrants.
            if len(self.children[at_quad]) > 0:
                # Then go explore them.
                points = []
                for quad in self.children[at_quad]:
                    points += self.query(query_quad,quad)
                return points
            else:
                # Else, just return the point within the current quadrant.
                resident = self.residents[at_quad]
                if resident:
                    if geometry.in_box(resident,query_box):
                        # In a list, because we will concatenate.
                        return [resident]
        # If there is no intersection, there is no points.
        return []
Beispiel #5
0
    def status(self, point, quadrant):
        """Return Status.Empty if the given point can be appended in the given quadrant."""

        assert (point is not None)
        assert (len(point) == 2)
        assert (quadrant is not None)
        assert (len(quadrant) == 2)

        box = as_box(quadrant)

        # if the point lies inside the given quadrant
        if geometry.in_box(point, box):
            if self.residents[quadrant]:
                # external: a quadrant that already contains a point
                assert (not self.children[quadrant])
                return self.Status.Leaf
            elif self.children[quadrant]:
                # internal: a quadrant that contains other quadrants
                return self.Status.Node
            else:
                # empty: there is not point yet in this quadrant
                return self.Status.Empty
        else:
            # point is out of the quadrant
            return self.Status.Out
Beispiel #6
0
    def query(self, query_quad, at_quad=None):
        """Return all the points (currently attached to the quad tree) that are located within the query_quad quadrant."""
        if not at_quad:
            at_quad = self.root

        query_box = as_box(query_quad)

        # If we ask for a quadrant that intersects with the current one.
        if self.covers(query_quad, at_quad):
            # If the current quadrant contains sub-quadrants.
            if len(self.children[at_quad]) > 0:
                # Then go explore them.
                points = []
                for quad in self.children[at_quad]:
                    points += self.query(query_quad, quad)
                return points
            else:
                # Else, just return the point within the current quadrant.
                resident = self.residents[at_quad]
                if resident:
                    if geometry.in_box(resident, query_box):
                        # In a list, because we will concatenate.
                        return [resident]
        # If there is no intersection, there is no points.
        return []