annotate python/event.py @ 263:c71540470057

reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 25 Jul 2012 22:06:51 -0400
parents 857bcd41e9a2
children dbe7e53334d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #! /usr/bin/env python
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2 '''Libraries for events
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3 Interactions, pedestrian crossing...'''
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
5 #import utils;
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
6 import moving
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
8 __metaclass__ = type
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
10 class Interaction(moving.STObject):
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
11 '''Class for an interaction between two road users
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
12 or a road user and an obstacle
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
13
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
14 link to the moving objects
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
15 '''
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
16
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
17 categories = {'headon': 0,
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
18 'rearend': 1,
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
19 'side': 2,
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
20 'parallel': 3}
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
21
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
22 def __init__(self, num = None, timeInterval = None, roaduserNum1 = None, roaduserNum2 = None, movingObject1 = None, movingObject2 = None, categoryNum = None):
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
23 moving.STObject.__init__(self, num, timeInterval)
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
24 self.roaduserNum1 = roaduserNum1
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
25 self.roaduserNum2 = roaduserNum2
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
26 self.movingObject1 = movingObject1
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
27 self.movingObject2 = movingObject2
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
28 self.categoryNum = categoryNum
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
29
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
30 def getIndicator(self, indicatorName):
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
31 if hasattr(self, 'indicators'):
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
32 for i in self.indicators:
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
33 if i.name == indicatorName:
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
34 return i
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
35 else:
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
36 return None
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
37
67
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
38 def computeIndicators(self):
93
19603b5fa578 added timeinterval computation for indicators based on dictionaries
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 67
diff changeset
39 '''Computes the collision course cosine only if the cosine is positive'''
94
7da14f07e1f9 bug introduced by conversion of interval length to int
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 93
diff changeset
40 collisionCourseDotProduct = [0]*int(self.timeInterval.length())
67
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
41 collisionCourseCosine = {}
95
857bcd41e9a2 bug introduced by conversion of interval length to int
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 94
diff changeset
42 distances = [0]*int(self.timeInterval.length())
67
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
43 for i,instant in enumerate(self.timeInterval):
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
44 deltap = self.movingObject1.getPositionAtInstant(instant)-self.movingObject2.getPositionAtInstant(instant)
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
45 deltav = self.movingObject2.getVelocityAtInstant(instant)-self.movingObject1.getVelocityAtInstant(instant)
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
46 collisionCourseDotProduct[i] = moving.Point.dot(deltap, deltav)
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
47 distances[i] = deltap.norm2()
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
48 if collisionCourseDotProduct[i] > 0:
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
49 collisionCourseCosine[instant] = collisionCourseDotProduct[i]/(distances[i]*deltav.norm2())
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
50 self.indicators = [moving.SeverityIndicator('Collision Course Dot Product', collisionCourseDotProduct, self.timeInterval),
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
51 moving.SeverityIndicator('Distances', distances, self.timeInterval),
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
52 moving.SeverityIndicator('Collision Course Cosine', collisionCourseCosine)]
ded58c424783 added indicator computation and modified severity indicator constructor
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 59
diff changeset
53
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
54 class Crossing(moving.STObject):
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
55 '''Class for the event of a street crossing
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
57 TODO: detecter passage sur la chaussee
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
58 identifier origines et destination (ou uniquement chaussee dans FOV)
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
59 carac traversee
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
60 detecter proximite veh (retirer si trop similaire simultanement
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
61 carac interaction'''
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
62
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
63 def __init__(self, roaduserNum = None, num = None, timeInterval = None):
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
64 moving.STObject.__init__(self, num, timeInterval)
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
65 self.roaduserNum = roaduserNum
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
66
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
67
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
68
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
69 if __name__ == "__main__":
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
70 import doctest
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
71 import unittest
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
72 #suite = doctest.DocFileSuite('tests/moving.txt')
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
73 suite = doctest.DocTestSuite()
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
74 unittest.TextTestRunner().run(suite)
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
75