示例#1
0
def testPedestrian():
    '''Test against legitimate pedestrian data'''
    # pedestrian domain
    minCorner = Vector2( 0.0, -6 )
    domainSize = Vector2( 2.4, 12 )
    pedDomain = Grid.RectDomain( minCorner, domainSize )
    # grid domain
    minCorner = Vector2( 0.0, -2 )
    domainSize = Vector2( 2.4, 4 )
    resolution = Vector2( domainSize.x / CELL_SIZE, domainSize.y / CELL_SIZE)
    gridDomain = Grid.AbstractGrid( minCorner, domainSize, resolution )

    # load pedestrian data
    pedFile = '/projects/crowd/fund_diag/paper/pre_density/experiment/Inputs/Corridor_onewayDB/uo-065-240-240_combined_MB.txt'
    try:
        data = loadTrajectory ( pedFile )
    except ValueError:
        print "Unable to recognize the data in the file: %s" % ( pedFile )
        return
    grids = []

    sig = Signals.PedestrianSignal( pedDomain )
    print gridDomain
    
    while ( True ):
        try:
            sig.setData( data )
        except StopIteration:
            break
        grid = gridDomain.getDataGrid() 
        kernel.convolve( sig, grid )
##        grid.cells /= ( CELL_SIZE * CELL_SIZE )

        print "Frame %d has min/max values: %f, %f" % ( sig.index, grid.minVal(), grid.maxVal() )        
        grids.append( grid )
##        break

    data.setNext( 0 )    
    visGrids( grids, data )
示例#2
0
    def getDomainSignal(self, convolveDomain, k, doReflection):
        '''Returns signal to support the domain covered by the given grid.
        
        It is assumed that data in the signal has the same \emph{resolution} as
        the grid, but may span a different region.  If the convolution domain is
        completely enclosed in the signal, then simply a sub-portion of the signal
        is returned.  If the domain extends beyond that of the signal, then the
        signal is reflected over those boundaries.  If the reflection still does
        not fill the region, then zeros are used to fill the rest of the domain.

        @param      convolveDomain      An instance of RectDomain, which corresponds to
                                        the convolution domain.  It is an M x N grid.
        @param      k                   The number of cells larger than the grid data is
                                        needed (related to convolution kernel size).
        @param      doReflection        A boolean reporting whether or not to reflect around
                                        the signal domain.
        @returns    An M+k x N+k numpy array.  Representing the signal in the domain.
        @raises     AttributeError if the data for the signal has not been set.
        '''
        baseIntersection = convolveDomain.intersection(self.data)
        if (baseIntersection == None):
            raise SignalDataError, "Trying to compute density in a region without signal"
        minPt, maxPt = baseIntersection
        if (minPt[0] != 0 or minPt[1] != 0
                or maxPt[0] != convolveDomain.resolution[0]
                or maxPt[1] != convolveDomain.resolution[1]):
            raise SignalDataError, "The entire convolution domain must lie within the signal domain"

        expand = convolveDomain.cellSize * k
        supportCorner = convolveDomain.minCorner - expand
        supportSize = convolveDomain.size + (2 * expand)
        supportResolution = (convolveDomain.resolution[0] + 2 * k,
                             convolveDomain.resolution[1] + 2 * k)
        supportDomain = Grid.AbstractGrid(supportCorner, supportSize,
                                          supportResolution)
        result = np.zeros(supportDomain.resolution,
                          dtype=convolveDomain.cells.dtype)

        # what section of signal domain overlaps support domain
        intersection = self.data.intersection(supportDomain)
        if (intersection != None):
            # the field only gets filled in if there is no intersection
            sigMin, sigMax = intersection
            xform = Grid.GridTransform(self.data, supportDomain)
            supMin = xform(sigMin)
            supMax = xform(sigMax)
            result[supMin[0]:supMax[0],
                   supMin[1]:supMax[1]] = self.data.cells[sigMin[0]:sigMax[0],
                                                          sigMin[1]:sigMax[1]]

            # if do reflection
            if (doReflection):
                # Reflection only necessary if the support domain isn't full of signal
                if (supMin[0] > 0 or supMin[1] > 0
                        or supMax[0] < supportResolution[0]
                        or supMax[1] < supportResolution[1]):
                    sigWidth = self.data.resolution[0]
                    sigHeight = self.data.resolution[1]
                    supWidth = supportDomain.resolution[0]
                    supHeight = supportDomain.resolution[1]

                    if (supMin[0] > 0):
                        # reflect left
                        rWidth = min(supMin[0], sigWidth)
                        reflKernel = self.data.cells[:rWidth, sigMin[1]:
                                                     sigMax[1]][::-1, :]
                        L = supMin[0] - rWidth
                        R = supMin[0]
                        result[L:R, supMin[1]:supMax[1]] = reflKernel

                        if (supMin[1] > 0):
                            #reflect bottom-left
                            rHeight = min(supMin[1], sigHeight - sigMin[1])
                            reflKernel = self.data.cells[:rWidth, :
                                                         rHeight][::-1, ::-1]
                            result[L:R,
                                   supMin[1] - rHeight:supMin[1]] = reflKernel

                        if (supMax[1] < supHeight):
                            #reflect top-left
                            rHeight = min(sigHeight, supHeight - supMax[1])
                            reflKernel = self.data.cells[:rWidth,
                                                         sigHeight - rHeight:
                                                         sigHeight][::-1, ::-1]
                            result[L:R,
                                   supMax[1]:supMax[1] + rHeight] = reflKernel

                    if (supMax[0] < supWidth):
                        # reflect right
                        rWidth = min(supWidth - supMax[0], sigWidth)
                        reflKernel = self.data.cells[
                            -rWidth:, sigMin[1]:sigMax[1]][::-1, :]
                        L = supMax[0]
                        R = supMax[0] + rWidth
                        result[L:R, supMin[1]:supMax[1]] = reflKernel

                        if (supMin[1] > 0):
                            # reflect bottom-right
                            rHeight = min(supMin[1], sigHeight - sigMin[1])
                            reflKernel = self.data.cells[
                                -rWidth:, :rHeight][::-1, ::-1]
                            result[L:R,
                                   supMin[1] - rHeight:supMin[1]] = reflKernel

                        if (supMax[1] < supHeight):
                            # reflect top
                            rHeight = min(sigHeight, supHeight - supMax[1])
                            reflKernel = self.data.cells[
                                -rWidth:,
                                sigHeight - rHeight:sigHeight][::-1, ::-1]
                            result[L:R,
                                   supMax[1]:supMax[1] + rHeight] = reflKernel

                    if (supMin[1] > 0):
                        # reflect bottom
                        rHeight = min(supMin[1], sigHeight - sigMin[1])
                        reflKernel = self.data.cells[
                            sigMin[0]:sigMax[1], :rHeight][:, ::-1]
                        result[supMin[0]:supMax[0],
                               supMin[1] - rHeight:supMin[1]] = reflKernel

                    if (supMax[1] < supHeight):
                        # reflect top
                        rHeight = min(sigHeight, supHeight - supMax[1])
                        reflKernel = self.data.cells[
                            sigMin[0]:sigMax[1],
                            sigHeight - rHeight:sigHeight][:, ::-1]
                        result[supMin[0]:supMax[0],
                               supMax[1]:supMax[1] + rHeight] = reflKernel
        return result