Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 27:44689029a86f
updated segmentIntersection and other
author | Nicolas Saunier <nico@confins.net> |
---|---|
date | Sat, 05 Dec 2009 15:40:28 -0500 |
parents | 54d9cb0c902b |
children | 9ae709a2e8d0 |
comparison
equal
deleted
inserted
replaced
26:54d9cb0c902b | 27:44689029a86f |
---|---|
22 self.first=first | 22 self.first=first |
23 self.last=last | 23 self.last=last |
24 | 24 |
25 def __str__(self): | 25 def __str__(self): |
26 return '%d %d'%(self.first, self.last) | 26 return '%d %d'%(self.first, self.last) |
27 | |
28 def __iter__(self): | |
29 self.iterInstantNum = 0 | |
30 return self | |
31 | |
32 def next(self): | |
33 if self.iterInstantNum >= self.length(): | |
34 raise StopIteration | |
35 else: | |
36 self.iterInstantNum += 1 | |
37 return self.first+self.iterInstantNum | |
38 | 27 |
39 def empty(self): | 28 def empty(self): |
40 ''' | 29 ''' |
41 >>> Interval().empty() | 30 >>> Interval().empty() |
42 True | 31 True |
73 | 62 |
74 def intersection(self, interval2): | 63 def intersection(self, interval2): |
75 '''Largest interval comprising self and interval2''' | 64 '''Largest interval comprising self and interval2''' |
76 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) | 65 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) |
77 | 66 |
78 def TimeInterval(Interval): | 67 |
68 class TimeInterval(Interval): | |
79 '''Temporal interval''' | 69 '''Temporal interval''' |
80 | 70 |
81 def __init__(self, first=0, last=-1): | 71 def __init__(self, first=0, last=-1): |
82 Interval.__init__(self, first, last, False) | 72 Interval.__init__(self, first, last, False) |
73 | |
74 def __getitem__(self, i): | |
75 if not self.empty(): | |
76 return self.first+i | |
77 | |
78 def __iter__(self): | |
79 self.iterInstantNum = 0 | |
80 return self | |
81 | |
82 def next(self): | |
83 if self.iterInstantNum >= self.length(): | |
84 raise StopIteration | |
85 else: | |
86 self.iterInstantNum += 1 | |
87 return self[self.iterInstantNum] | |
83 | 88 |
84 def length(self): | 89 def length(self): |
85 '''Returns the length of the interval | 90 '''Returns the length of the interval |
86 | 91 |
87 >>> TimeInterval(0,1).length() | 92 >>> TimeInterval(0,1).length() |
230 | 235 |
231 def getInstantPassingLane(self, p1, p2): | 236 def getInstantPassingLane(self, p1, p2): |
232 '''Returns the instant(s) the object passes from one side of the segment to the other | 237 '''Returns the instant(s) the object passes from one side of the segment to the other |
233 empty list if not''' | 238 empty list if not''' |
234 instants = [] | 239 instants = [] |
235 lane = [[p1[0],p2[0]], [p1[1],p2[1]]] | |
236 | |
237 # refaire tout en points, marche pas | |
238 # self.positions[i] self.positions[i+1] | |
239 | 240 |
240 for i in xrange(self.length()-1): | 241 for i in xrange(self.length()-1): |
241 p = utils.segmentIntersection([self.positions[0][i:i+1],self.positions[1][i:i+1]], lane) | 242 p = utils.segmentIntersection(self.positions[i], self.positions[i+1], p1, p2) |
242 if p: # interpolate the instant | 243 if p: |
243 if self.positions[0][i] != self.positions[0][i+1]: | 244 if self.positions[i].x != self.positions[i+1].x: |
244 ratio = (p[0]-self.positions[0][i])/(self.positions[0][i+1]-self.positions[0][i]) | 245 ratio = (p.x-self.positions[i].x)/(self.positions[i+1].x-self.positions[i].x) |
245 elif self.positions[1][i] != self.positions[1][i+1]: | 246 elif self.positions[i].y != self.positions[i+1].y: |
246 ratio = (p[1]-self.positions[1][i])/(self.positions[1][i+1]-self.positions[1][i]) | 247 ratio = (p.y-self.positions[i].y)/(self.positions[i+1].y-self.positions[i].y) |
247 else: | 248 else: |
248 ratio = 0 | 249 ratio = 0 |
249 instants.append(self.timeInterval[i]*(1-ratio)+ratio*self.timeInterval[i+1]) | 250 instants.append(self.timeInterval[i]*(1-ratio)+ratio*self.timeInterval[i+1]) |
250 return instants | 251 return instants |
252 | |
251 # def computeVelocities(self): | 253 # def computeVelocities(self): |
252 | 254 |
253 # need for a class representing the indicators, their units, how to print them in graphs... | 255 # need for a class representing the indicators, their units, how to print them in graphs... |
254 class TemporalIndicator: | 256 class TemporalIndicator: |
255 '''Class for temporal indicators | 257 '''Class for temporal indicators |