Mercurial Hosting > traffic-intelligence
changeset 67:ded58c424783
added indicator computation and modified severity indicator constructor
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Sun, 07 Nov 2010 04:21:26 -0500 |
parents | 56fe4ef1377e |
children | 846fa9dc47de |
files | python/event.py python/moving.py python/ubc_utils.py |
diffstat | 3 files changed, 43 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/python/event.py Sun Nov 07 01:34:43 2010 -0500 +++ b/python/event.py Sun Nov 07 04:21:26 2010 -0500 @@ -35,6 +35,21 @@ else: return None + def computeIndicators(self): + collisionCourseDotProduct = [0]*self.timeInterval.length() + collisionCourseCosine = {} + distances = [0]*self.timeInterval.length() + for i,instant in enumerate(self.timeInterval): + deltap = self.movingObject1.getPositionAtInstant(instant)-self.movingObject2.getPositionAtInstant(instant) + deltav = self.movingObject2.getVelocityAtInstant(instant)-self.movingObject1.getVelocityAtInstant(instant) + collisionCourseDotProduct[i] = moving.Point.dot(deltap, deltav) + distances[i] = deltap.norm2() + if collisionCourseDotProduct[i] > 0: + collisionCourseCosine[instant] = collisionCourseDotProduct[i]/(distances[i]*deltav.norm2()) + self.indicators = [moving.SeverityIndicator('Collision Course Dot Product', collisionCourseDotProduct, self.timeInterval), + moving.SeverityIndicator('Distances', distances, self.timeInterval), + moving.SeverityIndicator('Collision Course Cosine', collisionCourseCosine)] + class Crossing(moving.STObject): '''Class for the event of a street crossing
--- a/python/moving.py Sun Nov 07 01:34:43 2010 -0500 +++ b/python/moving.py Sun Nov 07 04:21:26 2010 -0500 @@ -66,7 +66,7 @@ return self def next(self): - if self.iterInstantNum >= self.length(): + if self.iterInstantNum >= self.length()-1: raise StopIteration else: self.iterInstantNum += 1 @@ -139,6 +139,10 @@ return [self.x, self.y] @staticmethod + def dot(p1, p2): + return p1.x*p2.x+p1.y*p2.y + + @staticmethod def distanceNorm2(p1, p2): return (p1-p2).norm2() @@ -307,6 +311,12 @@ def getVelocityAt(self, i): return self.velocities[i] + def getPositionAtInstant(self, i): + return self.positions[i-self.getFirstInstant()] + + def getVelocityAtInstant(self, i): + return self.velocities[i-self.getFirstInstant()] + def getXCoordinates(self): return self.positions.getXCoordinates() @@ -334,7 +344,19 @@ instants.append(self.timeInterval[i]*(1-ratio)+ratio*self.timeInterval[i+1]) return instants - # def computeVelocities(self): + @staticmethod + def collisionCourseDotProduct(movingObject1, movingObject2, instant): + 'A positive result indicates that the road users are getting closer' + deltap = movingObject1.getPositionAtInstant(instant)-movingObject2.getPositionAtInstant(instant) + deltav = movingObject2.getVelocityAtInstant(instant)-movingObject1.getVelocityAtInstant(instant) + return moving.Point.dot(deltap, deltav) + + @staticmethod + def collisionCourseCosine(movingObject1, movingObject2, instant): + 'A positive result indicates that the road users are getting closer' + deltap = movingObject1.getPositionAtInstant(instant)-movingObject2.getPositionAtInstant(instant) + deltav = movingObject2.getVelocityAtInstant(instant)-movingObject1.getVelocityAtInstant(instant) + return moving.Point.dot(deltap, deltav)/(deltap.norm2()*deltav.norm2()) def plotRoadUsers(objects, colors): '''Colors is a PlottingPropertyValues instance''' @@ -366,9 +388,8 @@ field mostSevereIsMax is True if the most severe value taken by the indicator is the maximum''' - def __init__(self, name, values, mostSevereIsMax=True, ignoredValue = None): - # , timeInterval=None # implement later - TemporalIndicator.__init__(self, name, values, timeInterval=None) + def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, ignoredValue = None): + TemporalIndicator.__init__(self, name, values, timeInterval) self.mostSevereIsMax = mostSevereIsMax self.ignoredValue = ignoredValue
--- a/python/ubc_utils.py Sun Nov 07 01:34:43 2010 -0500 +++ b/python/ubc_utils.py Sun Nov 07 04:21:26 2010 -0500 @@ -140,13 +140,12 @@ inter = Interaction(interactionNum, TimeInterval(parsedLine[1],parsedLine[2]), parsedLine[3], parsedLine[4], categoryNum = parsedLine[5]) indicatorFrameNums = [int(n) for n in lines[1].split(' ')] - indicators = [] + inter.indicators = [] for indicatorNum,line in enumerate(lines[2:]): values = {} for i,v in enumerate([float(n) for n in line.split(' ')]): values[indicatorFrameNums[i]] = v - indicators.append(SeverityIndicator(severityIndicatorNames[indicatorNum], values, mostSevereIsMax[indicatorNum], ignoredValue[indicatorNum])) - inter.indicators = indicators + inter.indicators.append(SeverityIndicator(severityIndicatorNames[indicatorNum], values, None, mostSevereIsMax[indicatorNum], ignoredValue[indicatorNum])) interactions.append(inter) interactionNum+=1