diff python/events.py @ 662:72174e66aba5

corrected bug that increased TTC by 1 frame and structure to store collision points and crossing zones
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 18 May 2015 17:17:06 +0200
parents dc70d9e711f5
children 455f9b93819c
line wrap: on
line diff
--- a/python/events.py	Mon May 18 13:53:25 2015 +0200
+++ b/python/events.py	Mon May 18 17:17:06 2015 +0200
@@ -98,13 +98,15 @@
         if roaduserNum1 is not None and roaduserNum2 is not None:
             self.roadUserNumbers = set([roaduserNum1, roaduserNum2])
         elif roadUser1 is not None and roadUser2 is not None:
-            self.roadUserNumbers = set(roadUser1.getNum(), roadUser2.getNum())
+            self.roadUserNumbers = set([roadUser1.getNum(), roadUser2.getNum()])
         else:
             self.roadUserNumbers = None
         self.categoryNum = categoryNum
         self.indicators = {}
         self.interactionInterval = None
-        self.collisionPoints = None # distionary for collison points with different prediction methods
+         # list for collison points and crossing zones
+        self.collisionPoints = None
+        self.crossingZones = None
 
     def getRoadUserNumbers(self):
         return self.roadUserNumbers
@@ -180,7 +182,8 @@
             speedDifferentials[instant] = deltav.norm2()
             if collisionCourseDotProducts[instant] > 0:
                 interactionInstants.append(instant)
-            collisionCourseAngles[instant] = arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant]))
+            if distances[instant] != 0 and speedDifferentials[instant] != 0:
+                collisionCourseAngles[instant] = arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant]))
 
         if len(interactionInstants) >= 2:
             self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1])
@@ -201,8 +204,6 @@
 
     def computeCrossingsCollisions(self, predictionParameters, collisionDistanceThreshold, timeHorizon, computeCZ = False, debug = False, timeInterval = None, nProcesses = 1, usePrototypes=False, route1= (-1,-1), route2=(-1,-1), prototypes={}, secondStepPrototypes={}, nMatching={}, objects=[], noiseEntryNums=[], noiseExitNums=[], minSimilarity=0.1, mostMatched=None, useDestination=True, useSpeedPrototype=True, acceptPartialLength=30, step=1):
         '''Computes all crossing and collision points at each common instant for two road users. '''
-        self.collisionPoints={}
-        self.crossingZones={}
         TTCs = {}
         if usePrototypes:
             route1= getRoute(self.roadUser1,prototypes,objects,noiseEntryNums,noiseExitNums,useDestination)
@@ -212,19 +213,19 @@
             commonTimeInterval = timeInterval
         else:
             commonTimeInterval = self.timeInterval
-        self.collisionPoints[predictionParameters.name], crossingZones = predictionParameters.computeCrossingsCollisions(self.roadUser1, self.roadUser2, collisionDistanceThreshold, timeHorizon, computeCZ, debug, commonTimeInterval, nProcesses,usePrototypes,route1,route2,prototypes,secondStepPrototypes,nMatching,objects,noiseEntryNums,noiseExitNums,minSimilarity,mostMatched,useDestination,useSpeedPrototype,acceptPartialLength, step)
-        if computeCZ:
-            self.crossingZones[predictionParameters.name] = crossingZones
+        self.collisionPoints, crossingZones = predictionParameters.computeCrossingsCollisions(self.roadUser1, self.roadUser2, collisionDistanceThreshold, timeHorizon, computeCZ, debug, commonTimeInterval, nProcesses,usePrototypes,route1,route2,prototypes,secondStepPrototypes,nMatching,objects,noiseEntryNums,noiseExitNums,minSimilarity,mostMatched,useDestination,useSpeedPrototype,acceptPartialLength, step)
         for i, cp in self.collisionPoints.iteritems():
             TTCs[i] = prediction.SafetyPoint.computeExpectedIndicator(cp)
-        # add probability of collision, and probability of successful evasive action
         self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[7], TTCs, mostSevereIsMax=False))
         
+        # crossing zones and pPET
         if computeCZ:
+            self.crossingZones[predictionParameters.name] = crossingZones
             pPETs = {}
             for i, cz in self.crossingZones.iteritems():
                 pPETs[i] = prediction.SafetyPoint.computeExpectedIndicator(cz)
             self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[9], pPETs, mostSevereIsMax=False))
+        # TODO add probability of collision, and probability of successful evasive action
 
     def computePET(self, collisionDistanceThreshold):
         # TODO add crossing zone
@@ -238,7 +239,7 @@
         self.interactionType = interactionType
 
     def getCrossingZones(self, predictionMethodName):
-        if self.hasattr(self, 'crossingZones'):
+        if self.crossingZones is not None:
             return self.crossingZones[predictionMethodName]
         else:
             return None
@@ -277,16 +278,19 @@
     else:
         return None
 
-def aggregateSafetyPoints(interactions, pointType = 'collision'):
+def aggregateSafetyPoints(interactions, predictionMethodName = None, pointType = 'collision'):
     '''Put all collision points or crossing zones in a list for display'''
+    if predictionMethodName is None and len(interactions)>0:
+        predictionMethodName = interactions[0].collisionPoints.keys()[0]
+
     allPoints = []
     if pointType == 'collision':
         for i in interactions:
-            for points in i.collisionPoints.values():
+            for points in i.collisionPoints[predictionMethodName].values():
                 allPoints += points
     elif pointType == 'crossing':
         for i in interactions:
-            for points in i.crossingZones.values():
+            for points in i.crossingZones[predictionMethodName].values():
                 allPoints += points
     else:
         print('unknown type of point '+pointType)