diff python/moving.py @ 248:571ba5ed22e2

added utils for bus processing
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 18 Jul 2012 02:54:22 -0400
parents bd8ab323c198
children 99173da7afae
line wrap: on
line diff
--- a/python/moving.py	Wed Jul 18 02:54:02 2012 -0400
+++ b/python/moving.py	Wed Jul 18 02:54:22 2012 -0400
@@ -4,7 +4,7 @@
 import utils;
 import cvutils;
 
-from math import sqrt, hypot;
+from math import sqrt;
 
 #from shapely.geometry import Polygon
 
@@ -48,13 +48,31 @@
         return (self.first >= interval2.first) and (self.last <= interval2.last)
 
     def union(self, interval2):
-        '''Largest interval comprising self and interval2'''
+        '''Smallest interval comprising self and interval2'''
         return TimeInterval(min(self.first, interval2.first), max(self.last, interval2.last))
         
     def intersection(self, interval2):
         '''Largest interval comprised in both self and interval2'''
         return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last))
 
+    def distance(self, interval2):
+        if not self.intersection(interval2).empty():
+            return 0
+        elif self.first > interval2.last:
+            return self.first - interval2.last
+        elif self.last < interval2.first:
+            return interval2.first - self.last
+        else:
+            return None
+
+
+def unionIntervals(intervals):
+    'returns the smallest interval containing all intervals'
+    inter = intervals[0]
+    for i in intervals[1:]:
+        inter = inter.union(i)
+    return inter
+
 
 class TimeInterval(Interval):
     '''Temporal interval based on frame numbers (hence the modified length method)
@@ -379,7 +397,8 @@
 #        def add(x, y): return x+y
 #        sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]])
 #        return sqrt(sq)
-        return [hypot(x,y) for x,y in zip(self.positions[0], self.positions[1])]
+        from numpy import hypot
+        return hypot(self.positions[0], self.positions[1])
 
     def cumulatedDisplacement(self):
         'Returns the sum of the distances between each successive point'
@@ -506,6 +525,9 @@
     def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, **kwargs):
         self.positions.drawOnWorldImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin, **kwargs)
 
+    def play(self, videoFilename, homography = None):
+        cvutils.displayTrajectories(videoFilename, [self], homography, self.getFirstInstant(), self.getLastInstant())
+
     def getInstantsCrossingLane(self, p1, p2):
         '''Returns the instant(s)
         at which the object passes from one side of the segment to the other