Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 91:daa05fae1a70
modified the type of the result of interval lengths to float, added comments
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 09 Jun 2011 16:03:17 -0400 |
parents | f84293ad4611 |
children | a5ef9e40688e |
comparison
equal
deleted
inserted
replaced
90:f84293ad4611 | 91:daa05fae1a70 |
---|---|
28 def empty(self): | 28 def empty(self): |
29 return self.first > self.last | 29 return self.first > self.last |
30 | 30 |
31 def length(self): | 31 def length(self): |
32 '''Returns the length of the interval''' | 32 '''Returns the length of the interval''' |
33 return max(0,self.last-self.first) | 33 return float(max(0,self.last-self.first)) |
34 | 34 |
35 def getList(self): | 35 def getList(self): |
36 return [self.first, self.last] | 36 return [self.first, self.last] |
37 | 37 |
38 def contains(self, instant): | 38 def contains(self, instant): |
50 '''Largest interval comprising self and interval2''' | 50 '''Largest interval comprising self and interval2''' |
51 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) | 51 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) |
52 | 52 |
53 | 53 |
54 class TimeInterval(Interval): | 54 class TimeInterval(Interval): |
55 '''Temporal interval | 55 '''Temporal interval based on frame numbers (hence the modified length method) |
56 may be modified directly by setting first and last''' | 56 may be modified directly by setting first and last''' |
57 | 57 |
58 def __init__(self, first=0, last=-1): | 58 def __init__(self, first=0, last=-1): |
59 Interval.__init__(self, first, last, False) | 59 Interval.__init__(self, first, last, False) |
60 | 60 |
73 self.iterInstantNum += 1 | 73 self.iterInstantNum += 1 |
74 return self[self.iterInstantNum] | 74 return self[self.iterInstantNum] |
75 | 75 |
76 def length(self): | 76 def length(self): |
77 '''Returns the length of the interval''' | 77 '''Returns the length of the interval''' |
78 return max(0,self.last-self.first+1) | 78 return float(max(0,self.last-self.first+1)) |
79 | 79 |
80 # class BoundingPolygon: | 80 # class BoundingPolygon: |
81 # '''Class for a polygon bounding a set of points | 81 # '''Class for a polygon bounding a set of points |
82 # with methods to create intersection, unions... | 82 # with methods to create intersection, unions... |
83 # ''' | 83 # ''' |
270 # sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]]) | 270 # sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]]) |
271 # return sqrt(sq) | 271 # return sqrt(sq) |
272 return [hypot(x,y) for x,y in zip(self.positions[0], self.positions[1])] | 272 return [hypot(x,y) for x,y in zip(self.positions[0], self.positions[1])] |
273 | 273 |
274 def cumulatedDisplacement(self): | 274 def cumulatedDisplacement(self): |
275 'Returns the sum of the distances between each successive point' | |
275 displacement = 0 | 276 displacement = 0 |
276 for i in xrange(self.length()-1): | 277 for i in xrange(self.length()-1): |
277 displacement += Point.distanceNorm2(self.__getitem__(i),self.__getitem__(i+1)) | 278 displacement += Point.distanceNorm2(self.__getitem__(i),self.__getitem__(i+1)) |
278 return displacement | 279 return displacement |
279 | 280 |
280 def wiggliness(self): | 281 def wiggliness(self): |
281 return self.cumulatedDisplacement()/float(Point.distanceNorm2(self.__getitem__(0),self.__getitem__(self.length()-1))) | 282 return self.cumulatedDisplacement()/float(Point.distanceNorm2(self.__getitem__(0),self.__getitem__(self.length()-1))) |
282 | 283 |
283 def getIntersections(self, p1, p2): | 284 def getIntersections(self, p1, p2): |
284 '''Returns a list of the indices at which the trajectory | 285 '''Returns a list of the indices at which the trajectory |
285 intersects with the segment of extremities p1 and p2 the list is empty if there is no crossing''' | 286 intersects with the segment of extremities p1 and p2 |
287 the list is empty if there is no crossing''' | |
286 indices = [] | 288 indices = [] |
287 | 289 |
288 for i in xrange(self.length()-1): | 290 for i in xrange(self.length()-1): |
289 q1=self.__getitem__(i) | 291 q1=self.__getitem__(i) |
290 q2=self.__getitem__(i+1) | 292 q2=self.__getitem__(i+1) |