changeset 1148:eb88d2984637

corrected interaction classification
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 12 May 2020 01:16:54 -0400
parents 8c0ec7e1eb8e
children 392db62ea1da
files trafficintelligence/events.py trafficintelligence/tests/events.txt
diffstat 2 files changed, 46 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/trafficintelligence/events.py	Wed May 06 00:58:21 2020 -0400
+++ b/trafficintelligence/events.py	Tue May 12 01:16:54 2020 -0400
@@ -221,10 +221,13 @@
 
     def categorize(self, velocityAngleTolerance, parallelAngleTolerance):
         '''Computes the interaction category by instant
-        velocityAngleTolerance and parallelAngleTolerance in radian'''
+        velocityAngleTolerance and parallelAngleTolerance in radian
+        velocityAngleTolerance: indicates the angle threshold for rear and head on (180-velocityAngleTolerance), as well as the maximum collision course angle for head on
+        velocityAngleTolerance: indicates the angle between velocity vector (average for parallel) and position vector'''
         parallelAngleToleranceCosine = np.cos(parallelAngleTolerance)
         self.categories = {}
         collisionCourseDotProducts = self.getIndicator(Interaction.indicatorNames[0])
+        collisionCourseAngles = self.getIndicator(Interaction.indicatorNames[1])
         velocityAngles = self.getIndicator(Interaction.indicatorNames[4])
         for instant in self.timeInterval:
             if velocityAngles[instant] < velocityAngleTolerance: # parallel or rear end
@@ -234,12 +237,10 @@
                     self.categories[instant] = Interaction.categories["parallel"]
                 else:
                     self.categories[instant] = Interaction.categories["rearend"]
-            elif velocityAngles[instant] > np.pi - velocityAngleTolerance: # head on
-                if collisionCourseDotProducts[instant] > 0:
-                    self.categories[instant] = Interaction.categories["headon"]
-            else:
-                if collisionCourseDotProducts[instant] > 0:
-                    self.categories[instant] = Interaction.categories["side"]
+            elif velocityAngles[instant] > np.pi - velocityAngleTolerance and collisionCourseAngles[instant] < velocityAngleTolerance: # head on
+                self.categories[instant] = Interaction.categories["headon"]
+            elif collisionCourseDotProducts[instant] > 0:
+                self.categories[instant] = Interaction.categories["side"]
 
     def computeCrossingsCollisions(self, predictionParameters, collisionDistanceThreshold, timeHorizon, computeCZ = False, debug = False, timeInterval = None):
         '''Computes all crossing and collision points at each common instant for two road users. '''
--- a/trafficintelligence/tests/events.txt	Wed May 06 00:58:21 2020 -0400
+++ b/trafficintelligence/tests/events.txt	Tue May 12 01:16:54 2020 -0400
@@ -32,3 +32,41 @@
 True
 >>> inter.getIndicator(Interaction.indicatorNames[1])[6] # doctest:+ELLIPSIS
 3.1415...
+
+>>> from collections import Counter
+>>> from numpy import pi
+>>> o1 = MovingObject.generate(0, Point(0,0), Point(1,0), TimeInterval(0,100))
+>>> o2 = MovingObject.generate(0, Point(100,1), Point(-1,0), TimeInterval(0,100))
+>>> inter12 = Interaction(roadUser1 = o1, roadUser2 = o2)
+>>> inter12.computeIndicators()
+>>> inter12.categorize(pi*20/180, pi*60/180)
+>>> Counter(inter12.categories.values()).most_common()[0][0] # head on
+0
+>>> inter12.categories[max(inter12.categories.keys())] # then side
+2
+>>> o3 = MovingObject.generate(0, Point(0,2), Point(1,0), TimeInterval(0,100))
+>>> inter13 = Interaction(roadUser1 = o1, roadUser2 = o3)
+>>> inter13.computeIndicators()
+>>> inter13.categorize(pi*20/180, pi*60/180)
+>>> Counter(inter13.categories.values()).most_common()[0][0] # parallel
+3
+>>> len(Counter(inter13.categories.values()))
+1
+>>> o4 = MovingObject.generate(0, Point(100,20), Point(-1,0), TimeInterval(0,100))
+>>> inter14 = Interaction(roadUser1 = o1, roadUser2 = o4)
+>>> inter14.computeIndicators()
+>>> inter14.categorize(pi*20/180, pi*60/180)
+>>> Counter(inter14.categories.values()).most_common()[0][0] # side
+2
+>>> inter12.categories[0] # first head one
+0
+>>> inter12.categories[max(inter12.categories.keys())] # then side
+2
+>>> o5 = MovingObject.generate(0, Point(50,50), Point(0,-1), TimeInterval(0,100))
+>>> inter15 = Interaction(roadUser1 = o1, roadUser2 = o5)
+>>> inter15.computeIndicators()
+>>> inter15.categorize(pi*20/180, pi*60/180)
+>>> Counter(inter15.categories.values()).most_common()[0][0] # side
+2
+>>> len(Counter(inter15.categories.values()))
+1