comparison 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
comparison
equal deleted inserted replaced
1251:2b1c8fe8f7e4 1252:fe35473acee3
2 '''Libraries for moving objects, trajectories...''' 2 '''Libraries for moving objects, trajectories...'''
3 3
4 import copy 4 import copy
5 from math import sqrt, atan2, cos, sin, inf 5 from math import sqrt, atan2, cos, sin, inf
6 6
7 from numpy import median, mean, array, arange, zeros, ones, hypot, NaN, std, floor, ceil, float32, argwhere, minimum, issubdtype, integer as npinteger, percentile 7 from numpy import median, mean, array, arange, zeros, ones, hypot, NaN, std, floor, ceil, float32, argwhere, minimum, issubdtype, integer as npinteger, percentile, full
8 from matplotlib.pyplot import plot, text, arrow 8 from matplotlib.pyplot import plot, text, arrow
9 from scipy.spatial.distance import cdist 9 from scipy.spatial.distance import cdist
10 from scipy.signal import savgol_filter 10 from scipy.signal import savgol_filter
11 11
12 try: 12 try:
1936 lcss.similarities(proto.getMovingObject().getPositions().asArray().T, self.getPositions().asArray().T) 1936 lcss.similarities(proto.getMovingObject().getPositions().asArray().T, self.getPositions().asArray().T)
1937 similarities = lcss.similarityTable[-1, :-1].astype(float) 1937 similarities = lcss.similarityTable[-1, :-1].astype(float)
1938 self.prototypeSimilarities.append(similarities/minimum(arange(1., len(similarities)+1), proto.getMovingObject().length()*ones(len(similarities)))) 1938 self.prototypeSimilarities.append(similarities/minimum(arange(1., len(similarities)+1), proto.getMovingObject().length()*ones(len(similarities))))
1939 1939
1940 @staticmethod 1940 @staticmethod
1941 def computePET(obj1, obj2, collisionDistanceThreshold): 1941 def computePET(obj1, obj2, collisionDistanceThreshold = None, useBoundingPoly = False):
1942 '''Post-encroachment time based on distance threshold 1942 '''Post-encroachment time based on distance threshold
1943 1943
1944 Returns the smallest time difference when the object positions are within collisionDistanceThreshold 1944 Returns the smallest time difference when the object positions are within collisionDistanceThreshold
1945 and the instants at which each object is passing through its corresponding position''' 1945 and the instants at which each object is passing through its corresponding position'''
1946 positions1 = [p.astuple() for p in obj1.getPositions()] 1946 n1 = int(obj1.length())
1947 positions2 = [p.astuple() for p in obj2.getPositions()] 1947 n2 = int(obj2.length())
1948 n1 = len(positions1)
1949 n2 = len(positions2)
1950 pets = zeros((n1, n2)) 1948 pets = zeros((n1, n2))
1951 for i,t1 in enumerate(obj1.getTimeInterval()): 1949 for i,t1 in enumerate(obj1.getTimeInterval()):
1952 for j,t2 in enumerate(obj2.getTimeInterval()): 1950 for j,t2 in enumerate(obj2.getTimeInterval()):
1953 pets[i,j] = abs(t1-t2) 1951 pets[i,j] = abs(t1-t2)
1954 distances = cdist(positions1, positions2, metric = 'euclidean') 1952 if useBoundingPoly:
1955 smallDistances = (distances <= collisionDistanceThreshold) 1953 polygons1 = [pointsToShapely(obj1.getBoundingPolygon(i)) for i in obj1.getTimeInterval()]
1954 polygons2 = [pointsToShapely(obj2.getBoundingPolygon(i)) for i in obj2.getTimeInterval()]
1955 overlaps = []
1956 for poly2 in polygons2:
1957 prep(poly2)
1958 for poly1 in polygons1:
1959 prep(poly1)
1960 overlaps.append([poly1.overlaps(poly2) for poly2 in polygons2])
1961 smallDistances = array(overlaps)
1962 elif collisionDistanceThreshold is not None:
1963 positions1 = [p.astuple() for p in obj1.getPositions()]
1964 positions2 = [p.astuple() for p in obj2.getPositions()]
1965 distances = cdist(positions1, positions2, metric = 'euclidean')
1966 smallDistances = (distances <= collisionDistanceThreshold)
1967 else:
1968 smallDistances = array([])
1969
1956 if smallDistances.any(): 1970 if smallDistances.any():
1957 smallPets = pets[smallDistances] 1971 smallPets = pets[smallDistances]
1958 petIdx = smallPets.argmin() 1972 petIdx = smallPets.argmin()
1959 distanceIndices = argwhere(smallDistances)[petIdx] 1973 distanceIndices = argwhere(smallDistances)[petIdx]
1960 return smallPets[petIdx], obj1.getFirstInstant()+distanceIndices[0], obj2.getFirstInstant()+distanceIndices[1] 1974 return smallPets[petIdx], obj1.getFirstInstant()+distanceIndices[0], obj2.getFirstInstant()+distanceIndices[1]