diff trafficintelligence/moving.py @ 1252:fe35473acee3

adding method to compute PET using polygon for the outline of a vehicle (bird eye view of the vehicle)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 22 Mar 2024 14:33:25 -0400
parents 77fbd0e2ba7d
children ef68d4ba7dae
line wrap: on
line diff
--- a/trafficintelligence/moving.py	Fri Mar 15 17:05:54 2024 -0400
+++ b/trafficintelligence/moving.py	Fri Mar 22 14:33:25 2024 -0400
@@ -4,7 +4,7 @@
 import copy
 from math import sqrt, atan2, cos, sin, inf
 
-from numpy import median, mean, array, arange, zeros, ones, hypot, NaN, std, floor, ceil, float32, argwhere, minimum,  issubdtype, integer as npinteger, percentile
+from numpy import median, mean, array, arange, zeros, ones, hypot, NaN, std, floor, ceil, float32, argwhere, minimum,  issubdtype, integer as npinteger, percentile, full
 from matplotlib.pyplot import plot, text, arrow
 from scipy.spatial.distance import cdist
 from scipy.signal import savgol_filter
@@ -1938,21 +1938,35 @@
                 self.prototypeSimilarities.append(similarities/minimum(arange(1., len(similarities)+1), proto.getMovingObject().length()*ones(len(similarities))))
 
     @staticmethod
-    def computePET(obj1, obj2, collisionDistanceThreshold):
+    def computePET(obj1, obj2, collisionDistanceThreshold = None, useBoundingPoly = False):
         '''Post-encroachment time based on distance threshold
 
         Returns the smallest time difference when the object positions are within collisionDistanceThreshold
         and the instants at which each object is passing through its corresponding position'''
-        positions1 = [p.astuple() for p in obj1.getPositions()]
-        positions2 = [p.astuple() for p in obj2.getPositions()]
-        n1 = len(positions1)
-        n2 = len(positions2)
+        n1 = int(obj1.length())
+        n2 = int(obj2.length())
         pets = zeros((n1, n2))
         for i,t1 in enumerate(obj1.getTimeInterval()):
             for j,t2 in enumerate(obj2.getTimeInterval()):
                 pets[i,j] = abs(t1-t2)
-        distances = cdist(positions1, positions2, metric = 'euclidean')
-        smallDistances = (distances <= collisionDistanceThreshold)
+        if useBoundingPoly:
+            polygons1 = [pointsToShapely(obj1.getBoundingPolygon(i)) for i in obj1.getTimeInterval()]
+            polygons2 = [pointsToShapely(obj2.getBoundingPolygon(i)) for i in obj2.getTimeInterval()]
+            overlaps = []
+            for poly2 in polygons2:
+                prep(poly2)
+            for poly1 in polygons1:
+                prep(poly1)
+                overlaps.append([poly1.overlaps(poly2) for poly2 in polygons2])
+            smallDistances = array(overlaps)
+        elif collisionDistanceThreshold is not None:
+            positions1 = [p.astuple() for p in obj1.getPositions()]
+            positions2 = [p.astuple() for p in obj2.getPositions()]
+            distances = cdist(positions1, positions2, metric = 'euclidean')
+            smallDistances = (distances <= collisionDistanceThreshold)
+        else:
+            smallDistances = array([])
+        
         if smallDistances.any():
             smallPets = pets[smallDistances]
             petIdx = smallPets.argmin()