Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 288:e0d41c7f53d4
updated class/method descriptions
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 29 Jan 2013 10:36:17 -0500 |
parents | f2cf16ad798f |
children | df58d361f19e |
comparison
equal
deleted
inserted
replaced
287:66691c06928c | 288:e0d41c7f53d4 |
---|---|
9 #from shapely.geometry import Polygon | 9 #from shapely.geometry import Polygon |
10 | 10 |
11 __metaclass__ = type | 11 __metaclass__ = type |
12 | 12 |
13 class Interval: | 13 class Interval: |
14 '''Generic Interval''' | 14 '''Generic interval: a subset of real numbers (not iterable)''' |
15 def __init__(self, first=0, last=-1, revert = False): | 15 def __init__(self, first=0, last=-1, revert = False): |
16 'Warning, do not revert if last<first, it contradicts the definition of empty' | |
17 if revert and last<first: | 16 if revert and last<first: |
18 self.first=last | 17 self.first=last |
19 self.last=first | 18 self.last=first |
20 else: | 19 else: |
21 self.first=first | 20 self.first=first |
42 | 41 |
43 def contains(self, instant): | 42 def contains(self, instant): |
44 return (self.first<=instant and self.last>=instant) | 43 return (self.first<=instant and self.last>=instant) |
45 | 44 |
46 def inside(self, interval2): | 45 def inside(self, interval2): |
47 'indicates if the temporal interval of self is comprised in interval2' | 46 '''Indicates if the temporal interval of self is comprised in interval2''' |
48 return (self.first >= interval2.first) and (self.last <= interval2.last) | 47 return (self.first >= interval2.first) and (self.last <= interval2.last) |
49 | 48 |
50 def union(self, interval2): | 49 def union(self, interval2): |
51 '''Smallest interval comprising self and interval2''' | 50 '''Smallest interval comprising self and interval2''' |
52 return Interval(min(self.first, interval2.first), max(self.last, interval2.last)) | 51 return Interval(min(self.first, interval2.first), max(self.last, interval2.last)) |
73 inter = inter.union(i) | 72 inter = inter.union(i) |
74 return inter | 73 return inter |
75 | 74 |
76 | 75 |
77 class TimeInterval(Interval): | 76 class TimeInterval(Interval): |
78 '''Temporal interval based on frame numbers (hence the modified length method) | 77 '''Temporal interval: set of instants at fixed time step, between first and last, included |
79 may be modified directly by setting first and last''' | 78 |
79 For example: based on frame numbers (hence the modified length method) | |
80 It may be modified directly by setting first and last''' | |
80 | 81 |
81 def __init__(self, first=0, last=-1): | 82 def __init__(self, first=0, last=-1): |
82 Interval.__init__(self, first, last, False) | 83 Interval.__init__(self, first, last, False) |
83 | 84 |
84 def __getitem__(self, i): | 85 def __getitem__(self, i): |
105 # with methods to create intersection, unions... | 106 # with methods to create intersection, unions... |
106 # ''' | 107 # ''' |
107 # We will use the polygon class of Shapely | 108 # We will use the polygon class of Shapely |
108 | 109 |
109 class STObject: | 110 class STObject: |
110 '''Class for spatio-temporal object | 111 '''Class for spatio-temporal object, i.e. with temporal and spatial existence |
111 i.e. with temporal and spatial existence | |
112 (time interval and bounding polygon for positions (e.g. rectangle)). | 112 (time interval and bounding polygon for positions (e.g. rectangle)). |
113 It does not mean that the object is defined | 113 |
114 It may not mean that the object is defined | |
114 for all time instants within the time interval''' | 115 for all time instants within the time interval''' |
115 | 116 |
116 def __init__(self, num = None, timeInterval = None, boundingPolygon = None): | 117 def __init__(self, num = None, timeInterval = None, boundingPolygon = None): |
117 self.num = num | 118 self.num = num |
118 self.timeInterval = timeInterval | 119 self.timeInterval = timeInterval |
166 def norm2Squared(self): | 167 def norm2Squared(self): |
167 '''2-norm distance (Euclidean distance)''' | 168 '''2-norm distance (Euclidean distance)''' |
168 return self.x*self.x+self.y*self.y | 169 return self.x*self.x+self.y*self.y |
169 | 170 |
170 def norm2(self): | 171 def norm2(self): |
171 '2-norm distance (Euclidean distance)' | 172 '''2-norm distance (Euclidean distance)''' |
172 return sqrt(self.norm2Squared()) | 173 return sqrt(self.norm2Squared()) |
173 | 174 |
174 def norm1(self): | 175 def norm1(self): |
175 return abs(self.x)+abs(self.y) | 176 return abs(self.x)+abs(self.y) |
176 | 177 |
233 def plotAll(points, color='r'): | 234 def plotAll(points, color='r'): |
234 from matplotlib.pyplot import scatter | 235 from matplotlib.pyplot import scatter |
235 scatter([p.x for p in points],[p.y for p in points], c=color) | 236 scatter([p.x for p in points],[p.y for p in points], c=color) |
236 | 237 |
237 class NormAngle: | 238 class NormAngle: |
238 'alternate encoding of a point, by its norm and orientation' | 239 '''Alternate encoding of a point, by its norm and orientation''' |
239 | 240 |
240 def __init__(self, norm, angle): | 241 def __init__(self, norm, angle): |
241 self.norm = norm | 242 self.norm = norm |
242 self.angle = angle | 243 self.angle = angle |
243 | 244 |
326 return None | 327 return None |
327 | 328 |
328 # TODO: implement a better algorithm for intersections of sets of segments http://en.wikipedia.org/wiki/Line_segment_intersection | 329 # TODO: implement a better algorithm for intersections of sets of segments http://en.wikipedia.org/wiki/Line_segment_intersection |
329 | 330 |
330 class Trajectory: | 331 class Trajectory: |
331 '''Class for trajectories | 332 '''Class for trajectories: temporal sequence of positions |
332 i.e. a temporal sequence of positions | 333 |
333 | 334 The class is iterable''' |
334 the class is iterable.''' | |
335 | 335 |
336 def __init__(self, positions=None): | 336 def __init__(self, positions=None): |
337 if positions != None: | 337 if positions != None: |
338 self.positions = positions | 338 self.positions = positions |
339 else: | 339 else: |
524 'truck'] | 524 'truck'] |
525 | 525 |
526 userType2Num = utils.inverseEnumeration(userTypeNames) | 526 userType2Num = utils.inverseEnumeration(userTypeNames) |
527 | 527 |
528 class MovingObject(STObject): | 528 class MovingObject(STObject): |
529 '''Class for moving objects | 529 '''Class for moving objects: a spatio-temporal object |
530 i.e. with a trajectory and a geometry (volume) (constant) | 530 with a trajectory and a geometry (constant volume over time) and a usertype (e.g. road user) |
531 and a usertype (e.g. road user) | |
532 ''' | 531 ''' |
533 | 532 |
534 def __init__(self, num = None, timeInterval = None, positions = None, geometry = None, userType = None): | 533 def __init__(self, num = None, timeInterval = None, positions = None, geometry = None, userType = None): |
535 STObject.__init__(self, num, timeInterval) | 534 STObject.__init__(self, num, timeInterval) |
536 self.positions = positions | 535 self.positions = positions |
635 suite = doctest.DocFileSuite('tests/moving.txt') | 634 suite = doctest.DocFileSuite('tests/moving.txt') |
636 #suite = doctest.DocTestSuite() | 635 #suite = doctest.DocTestSuite() |
637 unittest.TextTestRunner().run(suite) | 636 unittest.TextTestRunner().run(suite) |
638 #doctest.testmod() | 637 #doctest.testmod() |
639 #doctest.testfile("example.txt") | 638 #doctest.testfile("example.txt") |
640 |