def __init__(self, p1, p2): '''Constructor. @param p1 An instance of Vector2. The start point of the segment. @param p2 An instance of Vector2. The end point of the segment. ''' Segment.__init__(self, p1, p2)
def __init__( self, p1, p2 ): Segment.__init__( self, p1, p2 ) self.agents = [] self.coef = Vector3( 0, 0, 0 ) # coefficients to line's equation # dot it with the point (x, y, 1) to get signed distance self.computeCoefficients() self.flowCounts = [] # flow counts for each time step self.transferedAgents = [] # when an agent crosses previous line it gets added # to this line's transfer line self.nextLine = None
class Fov: def __init__(self, origin, angle, length): self.angle = angle self.origin = origin self.rseg = Segment(origin, length, self.angle) self.lseg = Segment(origin, length, self.rseg.angle + self.angle) def draw(self): self.rseg.draw() self.lseg.draw() def getFov(self): # pdb.set_trace() # return anglebtw(self.rseg.getVectorB(), self.lseg.getVectorB()) return self.rseg.findAngle(self.lseg)
def main(): '''Test the functionality''' pygame.init() map = FlameMap() SAMPLES = 300 samples = np.linspace(-5, 5, SAMPLES) X, Y = np.meshgrid(samples, samples) grid = np.dstack((X, Y)) if (True): print "Single segment" p1 = Vector2(-1.0, 1.0) p2 = Vector2(1.0, 0.0) seg = Segment(p1, p2) s = time.clock() dist = computeSegmentDistance(seg, grid) e = time.clock() print "\tTook %f seconds to compute %d distances" % (e - s, grid.size / 2) surface = map.colorOnSurface((dist.min(), dist.max()), dist.T[:, ::-1]) imgSeg = imgSpaceSegment(seg, (-5.0, 5.0), (-5.0, 5.0), SAMPLES, SAMPLES) pygame.draw.line(surface, (255, 255, 255), (imgSeg.p1.x, imgSeg.p1.y), (imgSeg.p2.x, imgSeg.p2.y)) pygame.image.save(surface, 'distFieldSeg.png') if (True): print "Obstacle" o = Obstacle() o.closed = True # create a hexagonal obstacle RADIUS = 2.0 RAD_SAMPLE = 12 for i in xrange(RAD_SAMPLE): theta = 2.0 * np.pi / RAD_SAMPLE * i x = np.cos(theta) * RADIUS y = np.sin(theta) * RADIUS o.vertices.append(Vector3(x, y, 0)) s = time.clock() dist = computeObstacleDistance(o, grid) e = time.clock() print "\tTook %f seconds to compute %d distances" % (e - s, grid.size / 2) ## print dist surface = map.colorOnSurface((dist.min(), dist.max()), dist.T[:, ::-1]) for seg in o.segments: imgSeg = imgSpaceSegment(seg, (-5.0, 5.0), (-5.0, 5.0), SAMPLES, SAMPLES) pygame.draw.line(surface, (255, 255, 255), (imgSeg.p1.x, imgSeg.p1.y), (imgSeg.p2.x, imgSeg.p2.y)) pygame.image.save(surface, 'distFieldObst.png')
def imgSpaceSegment(segment, xRange, yRange, xCount, yCount): '''Given the segment defined in world space, and the definition of the image space (specified by the xrange of values, the yrange of values and the number of cells in each direction) Computes the equivalent segment in that image space.''' xMin = xRange[0] xScale = xRange[1] - xRange[0] yMin = yRange[0] yScale = yRange[1] - yRange[0] col0 = int((segment.p1.x - xMin) / xScale * xCount) row0 = int((segment.p1.y - yMin) / yScale * yCount) col1 = int((segment.p2.x - xMin) / xScale * xCount) row1 = int((segment.p2.y - yMin) / yScale * yCount) return Segment(Vector2(col0, row0), Vector2(col1, row1))
def __str__( self ): s = Segment.__str__( self ) for agt in self.agents: s += '\n\t\t%s' % agt return s
def __init__(self, origin, angle, length): self.angle = angle self.origin = origin self.rseg = Segment(origin, length, self.angle) self.lseg = Segment(origin, length, self.rseg.angle + self.angle)
def main(): """Test the functionality""" from math import pi, exp import os, sys import optparse parser = optparse.OptionParser() # analysis to perform parser.add_option( "-d", "--density", help="Evaluate density.", action="store_true", dest='density', default=False ) parser.add_option( "-s", "--speed", help="Evaluate speed.", action="store_true", dest='speed', default=False ) parser.add_option( "-o", "--omega", help="Evaluate omega.", action="store_true", dest='omega', default=False ) parser.add_option( "-p", "--progress", help="Evaluate progress.", action="store_true", dest='progress', default=False ) parser.add_option( "-k", "--koshak", help="Evaluate koshak regions.", action="store_true", dest='koshak', default=False ) parser.add_option( "-i", "--include", help="Include all states", action="store_true", dest='includeAll', default=False ) # analysis domain - start, frame count, frame step parser.add_option( "-r", "--range", help="A triple of numbers: start frame, max frame count, frame step", nargs=3, action="store", dest='domain', type="int", default=(0, -1, 1) ) options, args = parser.parse_args() # input source file srcFile = sys.argv[1] pygame.init() CELL_SIZE = 0.2 MAX_AGENTS = -1 MAX_FRAMES = -1 FRAME_STEP = 1 FRAME_WINDOW = 1 START_FRAME = 0 EXCLUDE_STATES = () START_FRAME, MAX_FRAMES, FRAME_STEP = options.domain print "Command line:", START_FRAME, MAX_FRAMES, FRAME_STEP if ( True ): #increase the color bar specifications ColorMap.BAR_HEIGHT = 300 ColorMap.BAR_WIDTH = 30 ColorMap.FONT_SIZE = 20 timeStep = 1.0 outPath = '.' if ( True ): # This size doesn't work for 25k ## size = Vector2( 175.0, 120.0 ) ## minPt = Vector2( -75.0, -60.0 ) # this size DOES work for 25k size = Vector2( 215.0, 160.0 ) minPt = Vector2( -95.0, -80.0 ) res = (int( size.x / CELL_SIZE ), int( size.y / CELL_SIZE ) ) size = Vector2( res[0] * CELL_SIZE, res[1] * CELL_SIZE ) timeStep = 0.05 outPath = os.path.join( '/projects','tawaf','sim','jul2011','results' ) path = os.path.join( outPath, '{0}.scb'.format( srcFile ) ) print "Reading", path outPath = os.path.join( outPath, srcFile ) if ( not options.includeAll ): EXCLUDE_STATES = (1, 2, 3, 4, 5, 6, 7, 8, 9) domain = AbstractGrid( minPt, size, res ) print "Size:", size print "minPt:", minPt print "res:", res timeStep *= FRAME_STEP frameSet = FrameSet( path, START_FRAME, MAX_FRAMES, MAX_AGENTS, FRAME_STEP ) print "Total frames:", frameSet.totalFrames() grids = GridFileSequence( os.path.join( outPath, 'junk' ), Vector2(0,3.2), Vector2(-6., 6.)) colorMap = FlameMap() # output the parameters used to create the data # todo: R = 2.0 R = 1.5 def distFunc( dispX, dispY, radiusSqd ): """Constant distance function""" # This is the local density function provided by Helbing # using Gaussian, delta(in the equation) = radiusSqd return np.exp( -(dispX * dispX + dispY * dispY) / (2.0 * radiusSqd ) ) / ( 2.0 * np.pi * radiusSqd ) dfunc = lambda x, y: distFunc( x, y, R * R ) if ( options.density ): if ( not os.path.exists( os.path.join( outPath, 'dense' ) ) ): os.makedirs( os.path.join( outPath, 'dense' ) ) print "\tComputing density with R = %f" % R s = time.clock() kernel = Kernels.GaussianKernel( R, CELL_SIZE, False ) signal = Signals.PedestrianSignal( domain ) # signal domain is the same as convolution domain grids.convolveSignal( domain, kernel, signal, frameSet ) print "\t\tTotal computation time: ", (time.clock() - s), "seconds" print "\tComputing density images", s = time.clock() imageName = os.path.join( outPath, 'dense', 'dense' ) reader = GridFileSequenceReader( grids.outFileName + ".density" ) visualizeGFS( reader, colorMap, imageName, 'png', 1.0, grids.obstacles ) print "Took", (time.clock() - s), "seconds" if ( options.speed ): if ( not os.path.exists( os.path.join( outPath, 'speed' ) ) ): os.makedirs( os.path.join( outPath, 'speed' ) ) print "\tComputing speeds", s = time.clock() stats = grids.computeSpeeds( minPt, size, res, R, frameSet, timeStep, EXCLUDE_STATES, GridFileSequence.BLIT_SPEED ) stats.write( os.path.join( outPath, 'speed', 'stat.txt' ) ) stats.savePlot( os.path.join( outPath, 'speed', 'stat.png' ), 'Average speed per step' ) print "Took", (time.clock() - s), "seconds" print "\tComputing speed images", s = time.clock() imageName = os.path.join( outPath, 'speed', 'speed' ) # the limit: 0.5 means the color map is saturated from from minVal to 50% of the range reader = GridFileSequenceReader( grids.outFileName + ".speed" ) visualizeGFS( reader, colorMap, imageName, 'png', 0.75, grids.obstacles ) print "Took", (time.clock() - s), "seconds" if ( options.omega ): if ( not os.path.exists( os.path.join( outPath, 'omega' ) ) ): os.makedirs( os.path.join( outPath, 'omega' ) ) print "\tComputing omega", s = time.clock() stats = grids.computeAngularSpeeds( minPt, size, res, R, frameSet, timeStep, EXCLUDE_STATES, GridFileSequence.BLIT_SPEED, FRAME_WINDOW ) stats.write( os.path.join( outPath, 'omega', 'stat.txt' ) ) stats.savePlot( os.path.join( outPath, 'omega', 'stat.png'), 'Average radial velocity per step' ) print "Took", (time.clock() - s), "seconds" print "\tComputing omega images", s = time.clock() imageName = os.path.join( outPath, 'omega', 'omega' ) colorMap = RedBlueMap() reader = GridFileSequenceReader( grids.outFileName + ".omega" ) visualizeGFS( reader, colorMap, imageName, 'png', 1.0, grids.obstacles ) print "Took", (time.clock() - s), "seconds" if ( options.progress ): if ( not os.path.exists( os.path.join( outPath, 'progress' ) ) ): os.makedirs( os.path.join( outPath, 'progress' ) ) print "\tComputing progress", s = time.clock() stats = grids.computeProgress( minPt, size, res, R, frameSet, timeStep, EXCLUDE_STATES, FRAME_WINDOW ) stats.write( os.path.join( outPath, 'progress', 'stat.txt' ) ) stats.savePlot( os.path.join( outPath, 'progress', 'stat.png'), 'Average progress around Kaabah' ) print "Took", (time.clock() - s), "seconds" print "\tComputing progress images", s = time.clock() imageName = os.path.join( outPath, 'progress', 'progress' ) colorMap = FlameMap( (0.0, 1.0) ) reader = GridFileSequenceReader( grids.outFileName + ".progress" ) visualizeGFS( reader, colorMap, imageName, 'png', 1.0, grids.obstacles ) print "Took", (time.clock() - s), "seconds" if ( False ): if ( not os.path.exists( os.path.join( outPath, 'advec' ) ) ): os.makedirs( os.path.join( outPath, 'advec' ) ) lines = [ Segment( Vector2(0.81592, 5.12050), Vector2( 0.96233, -5.27461) ) ] print "\tComputing advection", s = time.clock() grids.computeAdvecFlow( minPt, size, res, dfunc, 3.0, R, frameSet, lines ) print "Took", (time.clock() - s), "seconds" print "\tComputing advection images", s = time.clock() imageName = os.path.join( outPath, 'advec', 'advec' ) reader = GridFileSequenceReader( grids.outFileName + ".advec" ) visualizeGFS( reader, colorMap, imageName, 'png', 1.0, grids.obstacles ) print "Took", (time.clock() - s), "seconds" if ( options.koshak ): if ( not os.path.exists( os.path.join( outPath, 'regionSpeed' ) ) ): os.makedirs( os.path.join( outPath, 'regionSpeed' ) ) print "\tComputing region speeds" s = time.clock() vertices = ( Vector2( -0.551530, 0.792406 ), Vector2( 3.736435, -58.246524 ), Vector2( 42.376927, -56.160723 ), Vector2( 5.681046, -6.353232 ), Vector2( 92.823337, -4.904953 ), Vector2( 5.376837, 6.823865 ), Vector2( 92.526405, 9.199321 ), Vector2( 88.517822, -48.850902 ), Vector2( 6.416100, 53.293737 ), Vector2( -5.906582, 6.230001 ), Vector2( -6.203514, 53.739135 ), Vector2( 62.833196, 57.896184 ), Vector2( 93.268736, 43.643444 ), Vector2( -41.686899, -61.322050 ), Vector2( -74.794826, -25.838665 ), Vector2( -75.388691, 49.582085 ) ) vIDs = ( (0, 3, 4, 6, 5), (5, 6, 12, 11, 8), (5, 8, 10, 9, 0), (0, 9, 10, 15, 14, 13), (0, 13, 1), (0, 1, 2, 3), (3, 2, 7, 4) ) polygons = [] for ids in vIDs: p = Polygon() p.closed = True for id in ids: p.vertices.append( vertices[id] ) polygons.append( p ) grids.computeRegionSpeed( frameSet, polygons, timeStep, EXCLUDE_STATES ) print "Took", (time.clock() - s), "seconds" # output image imagePath = os.path.join( outPath, 'regionSpeed', 'region' ) colorMap = TwoToneHSVMap( (0, 0.63, 0.96), (100, 0.53, 0.75 ) ) regionSpeedImages( grids.outFileName + ".region", imagePath, polygons, colorMap, minPt, size, res ) if ( False ): # flow lines if ( not os.path.exists( os.path.join( outPath, 'flow' ) ) ): os.makedirs( os.path.join( outPath, 'flow' ) ) lines = ( Segment( Vector2( 4.56230, -7.71608 ), Vector2( 81.49586, -4.55443 ) ), Segment( Vector2( 5.08924, 5.72094 ), Vector2( 82.28628, 8.61913 ) ), Segment( Vector2( 3.50842, 8.09218 ), Vector2( 2.71800, 51.30145 ) ), Segment( Vector2( -5.97654, 5.72094 ), Vector2( -8.87472, 51.56492 ) ), Segment( Vector2( -6.50348, -7.18914 ), Vector2( -40.75473, -53.56005 ) ), Segment( Vector2( -1.23406, -6.92567 ), Vector2( 1.13718, -51.18881 ) ), Segment( Vector2( 3.50842, -7.45261 ), Vector2( 44.08297, -45.65592 ) ) ) flow = computeFlowLines( Vector2( 0, 0 ), lines, frameSet ) flowFile = os.path.join( outPath, 'flow', 'flow.txt' ) file = open( flowFile, 'w' ) flow.write( file ) file.close() if ( False ): # Traces print "Rendering traces" s = time.clock() grids.renderTraces( minPt, size, res, frameSet, 5, 5, 'data/trace11_' ) print "Took", (time.clock() - s), "seconds"
from pyglet.window import Window import pyglet from primitives import Segment, Vector, Point from view import Fov import pdb import math window = Window(800, 600) x1, y1 = (400, 300) # parameters for segment a = Vector(x1, y1) length = 100 angle = 45 seg = Segment(a, length, angle) fieldofview = Fov(a, angle, length) print(fieldofview.getFov()) nAngle = 45 # seg.rotate(nAngle) def update(dt): global nAngle # pdb.set_trace() if nAngle == 360: nAngle = 45 seg.rotate(nAngle) nAngle += 45
def __init__( self, p1, p2 ): Segment.__init__( self, p1, p2 )