comparison python/events.py @ 705:0ceee3b1a96d dev

cleanup crossing collisions and crossing zones in Interaction (not stored per type of prediction method)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 22 Jul 2015 13:46:28 -0400
parents 957126bfb456
children e395bffe1412
comparison
equal deleted inserted replaced
704:f83d125d0c55 705:0ceee3b1a96d
4 4
5 import moving, prediction, indicators, utils, cvutils 5 import moving, prediction, indicators, utils, cvutils
6 from base import VideoFilenameAddable 6 from base import VideoFilenameAddable
7 7
8 import numpy as np 8 import numpy as np
9 from numpy import arccos
10 9
11 import multiprocessing 10 import multiprocessing
12 import itertools 11 import itertools
13 12
14 13
180 for instant in self.timeInterval: 179 for instant in self.timeInterval:
181 deltap = self.roadUser1.getPositionAtInstant(instant)-self.roadUser2.getPositionAtInstant(instant) 180 deltap = self.roadUser1.getPositionAtInstant(instant)-self.roadUser2.getPositionAtInstant(instant)
182 v1 = self.roadUser1.getVelocityAtInstant(instant) 181 v1 = self.roadUser1.getVelocityAtInstant(instant)
183 v2 = self.roadUser2.getVelocityAtInstant(instant) 182 v2 = self.roadUser2.getVelocityAtInstant(instant)
184 deltav = v2-v1 183 deltav = v2-v1
185 velocityAngles[instant] = arccos(moving.Point.dot(v1, v2)/(v1.norm2()*v2.norm2())) 184 velocityAngles[instant] = np.arccos(moving.Point.dot(v1, v2)/(v1.norm2()*v2.norm2()))
186 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav) 185 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav)
187 distances[instant] = deltap.norm2() 186 distances[instant] = deltap.norm2()
188 speedDifferentials[instant] = deltav.norm2() 187 speedDifferentials[instant] = deltav.norm2()
189 if collisionCourseDotProducts[instant] > 0: 188 if collisionCourseDotProducts[instant] > 0:
190 interactionInstants.append(instant) 189 interactionInstants.append(instant)
191 if distances[instant] != 0 and speedDifferentials[instant] != 0: 190 if distances[instant] != 0 and speedDifferentials[instant] != 0:
192 collisionCourseAngles[instant] = arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant])) 191 collisionCourseAngles[instant] = np.arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant]))
193 192
194 if len(interactionInstants) >= 2: 193 if len(interactionInstants) >= 2:
195 self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1]) 194 self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1])
196 else: 195 else:
197 self.interactionInterval = moving.TimeInterval() 196 self.interactionInterval = moving.TimeInterval()
225 if len(TTCs) > 0: 224 if len(TTCs) > 0:
226 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[7], TTCs, mostSevereIsMax=False)) 225 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[7], TTCs, mostSevereIsMax=False))
227 226
228 # crossing zones and pPET 227 # crossing zones and pPET
229 if computeCZ: 228 if computeCZ:
230 self.crossingZones[predictionParameters.name] = crossingZones 229 self.crossingZones = crossingZones
231 pPETs = {} 230 pPETs = {}
232 for i, cz in self.crossingZones.iteritems(): 231 for i, cz in self.crossingZones.iteritems():
233 pPETs[i] = prediction.SafetyPoint.computeExpectedIndicator(cz) 232 pPETs[i] = prediction.SafetyPoint.computeExpectedIndicator(cz)
234 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[9], pPETs, mostSevereIsMax=False)) 233 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[9], pPETs, mostSevereIsMax=False))
235 # TODO add probability of collision, and probability of successful evasive action 234 # TODO add probability of collision, and probability of successful evasive action
246 if hasattr(self, 'collision'): 245 if hasattr(self, 'collision'):
247 return self.collision 246 return self.collision
248 else: 247 else:
249 return None 248 return None
250 249
251 def getCrossingZones(self, predictionMethodName): 250 def getCollisionPoints(self):
252 if self.crossingZones is not None: 251 return self.collisionPoints
253 return self.crossingZones[predictionMethodName] 252
254 else: 253 def getCrossingZones(self):
255 return None 254 return self.crossingZones
256
257 def getCollisionPoints(self, predictionMethodName):
258 if self.collisionPoints is not None:
259 return self.collisionPoints[predictionMethodName]
260 else:
261 return None
262
263 255
264 def createInteractions(objects, _others = None): 256 def createInteractions(objects, _others = None):
265 '''Create all interactions of two co-existing road users''' 257 '''Create all interactions of two co-existing road users'''
266 if _others is not None: 258 if _others is not None:
267 others = _others 259 others = _others
286 if i<len(interactions): 278 if i<len(interactions):
287 return interactions[i] 279 return interactions[i]
288 else: 280 else:
289 return None 281 return None
290 282
291 def aggregateSafetyPoints(interactions, predictionMethodName = None, pointType = 'collision'): 283 def aggregateSafetyPoints(interactions, pointType = 'collision'):
292 '''Put all collision points or crossing zones in a list for display''' 284 '''Put all collision points or crossing zones in a list for display'''
293 if predictionMethodName is None and len(interactions)>0:
294 predictionMethodName = interactions[0].collisionPoints.keys()[0]
295
296 allPoints = [] 285 allPoints = []
297 if pointType == 'collision': 286 if pointType == 'collision':
298 for i in interactions: 287 for i in interactions:
299 for points in i.collisionPoints[predictionMethodName].values(): 288 for points in i.collisionPoints.values():
300 allPoints += points 289 allPoints += points
301 elif pointType == 'crossing': 290 elif pointType == 'crossing':
302 for i in interactions: 291 for i in interactions:
303 for points in i.crossingZones[predictionMethodName].values(): 292 for points in i.crossingZones.values():
304 allPoints += points 293 allPoints += points
305 else: 294 else:
306 print('unknown type of point '+pointType) 295 print('unknown type of point: '+pointType)
307 return allPoints 296 return allPoints
308 297
309 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity): 298 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity):
310 '''Finds exemplar indicator time series for all interactions 299 '''Finds exemplar indicator time series for all interactions
311 Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) 300 Returns the prototype indices (in the interaction list) and the label of each indicator (interaction)