Example #1
0
    def __init__(self,
                 minCorner=Vector2(0.0, 0.0),
                 size=Vector2(1.0, 1.0),
                 resolution=(1, 1),
                 cellSize=None):
        '''Grid constructor.

        @param  minCorner       A 2-tuple-like instace of floats.  The position, in world space,
                                of the "bottom-left" corner of the domain.  (Minimum x- and y-
                                values.
        @param  size            A 2-tuple-like instace of floats.  The span of the domain (in world
                                space.)  The maximum values of the domain are minCorner[0] + size[0]
                                and minCorner[1] + size[1], respectively.
        @param  resolution      A 2-tuple like instance of ints.  The number of cells in the domain in
                                both the x- and y-directions.  This will imply a cell size.
        '''
        RectDomain.__init__(self, minCorner, size)
        self.resolution = resolution  # tuple (x, y)  - int
        # size of each cell in the world grid
        if (cellSize is None):
            self.cellSize = Vector2(size[0] / float(resolution[0]),
                                    size[1] / float(resolution[1]))
        else:
            self.cellSize = cellSize
            assert (np.abs(self.cellSize[0] * self.resolution[0] -
                           self.size[0]) < 0.0001
                    and np.abs(self.cellSize[1] * self.resolution[1] -
                               self.size[1]) < 0.0001)
Example #2
0
 def copyDomain(self, grid):
     '''Copies the grid domain parameters from the provided grid.minCorner
     
     @param  grid            An instance of an AbstractGrid.  If provided, the previous parameters
                             are ignored and the values are copied from the provided grid.
     '''
     RectDomain.copyDomain(self, grid)
     self.resolution = copy.deepcopy(grid.resolution)
     self.cellSize = copy.deepcopy(grid.cellSize)
Example #3
0
    def __init__(self, minCorner, size):
        '''Constructor.

        @param  minCorner       A 2-tuple-like instance of floats.  The position, in world space,
                                of the "bottom-left" corner of the domain.  (Minimum x- and y-
                                values.
        @param  size            A 2-tuple-like instace of floats.  The span of the domain (in world
                                space.)  The maximum values of the domain are minCorner[0] + size[0]
                                and minCorner[1] + size[1], respectively.
        '''
        RectDomain.__init__(self, minCorner, size)
Example #4
0
    def rectDomain(self):
        '''Returns the RectDomain data for this AbstractGrid.

        @returns        An instance of a RectDomain including this grid's minimum corner
                        position and world size.
        '''
        return RectDomain(self.minCorner, self.size)
Example #5
0
def makeDomain(domainX, domainY, cellSize=None):
    '''Defines a rectangular domain from the given specifications.

    If cellSize is not specified, the domain is a RectDomain, otherwise, it
    is an Abstract Grid.

    @param      domainX         A two-tuple like object of floats.  The range of the
                                test region on the x-axis as [minX, maxX]
    @param      domainY         A two-tuple like object of floats.  The range of the
                                test region on the y-axis as [minY, maxY]
    @param      cellSize        A float.  The size of the cells for the discretized
                                domain.
    @returns    An instance of RectDomain or AbstractGrid, depending on cellSize.
                If cellSize is not defined, then a RectDomain is constructed, otherwise
                an AbstractGrid.
    '''
    minCorner = Vector2(domainX[0], domainY[0])
    size = Vector2(domainX[1] - domainX[0], domainY[1] - domainY[0])
    if (cellSize is None):
        return RectDomain(minCorner, size)
    else:
        rX = int(np.floor(size[0] / cellSize))  # explicit truncation
        rY = int(np.floor(size[1] / cellSize))  # explicit truncation
        size = Vector2(rX * cellSize, rY * cellSize)
        return AbstractGrid(minCorner, size, (rX, rY))
Example #6
0
 def readConfig(self, file):
     '''Reads the common TaskWidget parameters from the file'''
     AnalysisTask.readConfig(self, file)
     line = file.readline()
     tokens = line.split('~')
     assert (len(tokens) == 2)
     names = tokens[0].split(',')
     tokens = tokens[1].split()
     assert (len(tokens) == len(names) * 4)
     rects = []
     while (tokens):
         minX, minY, w, h = map(lambda x: float(x), tokens[:4])
         rects.append(RectDomain((minX, minY), (w, h)))
         tokens = tokens[4:]
     self._rects = zip(names, rects)
Example #7
0
    def intersection(self, grid):
        '''Computes the intersection of this Grid with another Grid or domain.

        If there is no intersection between the two domains, None is returned, regardless
        of input type.
        
        The nature of the intersection depends on the type of domain provided.  If the
        input is an abstract RectDomain, then the intersection returned is, in turn,
        a RectDomain in continuous space.

        If the input is a derivative of an AbstractGrid, the grids must be aligned because
        the intersection is defined in terms of overlapping cells and the return type
        is a pair of 2-tuples, the minCorner and maxCorner (in grid coordinates) in THIS
        grid of the intersection.  To determine the coordinates in the provided grid's
        coordinates, use a GridTransform.        

        @param      grid        The grid/domain against which the intersection is performed.
        @returns    It depends on the input.  See the notes above.
        '''
        if (isinstance(grid, AbstractGrid)):
            assert (self.isAligned(grid))
            if (not self.intersects(grid)):
                return None
            xform = GridTransform(
                grid, self)  # grid cell coords in this grid's coords
            gmc = xform((0, 0))
            gMC = xform(grid.resolution)
            X = [gmc[0], gMC[0], 0, self.resolution[0]]
            X.sort()
            Y = [gmc[1], gMC[1], 0, self.resolution[1]]
            Y.sort()
            minCorner = (X[1], Y[1])
            maxCorner = (X[2], Y[2])
            return minCorner, maxCorner
        elif (isinstance(grid, RectDomain)):
            return RectDomain.intersection(self, grid)
        else:
            raise ValueError, 'Grids can only be intersected with RectDomain and its subclasses'