changeset 25:28e546861263

added Point class and modified trajectory accordingly
author Nicolas Saunier <nico@confins.net>
date Sat, 05 Dec 2009 12:08:25 -0500
parents 6fb59cfb201e
children 54d9cb0c902b
files python/moving.py python/ubc_utils.py
diffstat 2 files changed, 50 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/python/moving.py	Sat Dec 05 11:04:03 2009 -0500
+++ b/python/moving.py	Sat Dec 05 12:08:25 2009 -0500
@@ -96,21 +96,39 @@
     def getLastInstant(self):
         return self.timeInterval.first()
 
+class Point:
+    def __init__(self, x, y):
+        self.x = x
+        self.y = y
+
+    def __str__(self):
+        return '(%f,%f)'%(self.x,self.y)
+
+    def __repr__(self):
+        return str(self)
+
+    def __sub__(self, other):
+        '''
+        >>> Point(3,4)-Point(1,7)
+        (2.000000,-3.000000)
+        '''
+        return Point(self.x-other.x, self.y-other.y)
+
 class Trajectory:
     '''Class for trajectories
     i.e. a temporal sequence of positions
 
     the class is iterable.'''
 
-    def __init__(self, positions = None):
-#        self.iterInstantNum = 0
-        self.positions = positions
+    def __init__(self, line1, line2):
+        self.positions = [[float(n) for n in line1.split(' ')],
+                          [float(n) for n in line2.split(' ')]]
 
     def __str__(self):
         return ' '.join(map(utils.printPoint, self.positions[0], self.positions[1]))
 
     def __getitem__(self, i):
-        return [self.positions[0][i], self.positions[1][i]]        
+        return Point(self.positions[0][i], self.positions[1][i])
 
     def __iter__(self):
         self.iterInstantNum = 0
@@ -123,16 +141,16 @@
             self.iterInstantNum += 1
             return self[self.iterInstantNum-1]
 
-    def addPosition(self, point):
+    def addPosition(self, p):
         if not self.positions:
-            self.positions = [[point[0]],[point[1]]]
+            self.positions = [[p.x],[p.y]]
         else:
-            self.positions[0].append(point[0])
-            self.positions[1].append(point[1])
+            self.positions[0].append(p.x)
+            self.positions[1].append(p.y)
 
-    def draw(self):
+    def draw(self, options = ''):
         from matplotlib.pylab import plot
-        plot(self.positions[0], self.positions[1])
+        plot(self.positions[0], self.positions[1], options)
 
     def length(self):
         return len(self.positions[0])
@@ -185,14 +203,29 @@
     def getYCoordinates(self):
         return self.positions.getYCoordinates()
     
-    def draw(self):
-        self.positions.draw()
+    def draw(self, options = ''):
+        self.positions.draw(options)
 
     def getInstantPassingLane(self, p1, p2):
         '''Returns the instant(s) the object passes from one side of the segment to the other
-        None if does not'''
-        # parallel
-    
+        empty list if not'''
+        instants = []
+        lane = [[p1[0],p2[0]], [p1[1],p2[1]]]
+
+        # refaire tout en points, marche pas
+        # self.positions[i] self.positions[i+1]
+
+        for i in xrange(self.length()-1):
+            p = utils.segmentIntersection([self.positions[0][i:i+1],self.positions[1][i:i+1]], lane)
+            if p: # interpolate the instant
+                if self.positions[0][i] != self.positions[0][i+1]:
+                    ratio = (p[0]-self.positions[0][i])/(self.positions[0][i+1]-self.positions[0][i])
+                elif self.positions[1][i] != self.positions[1][i+1]:
+                    ratio = (p[1]-self.positions[1][i])/(self.positions[1][i+1]-self.positions[1][i])
+                else:
+                    ratio = 0
+                instants.append(self.timeInterval[i]*(1-ratio)+ratio*self.timeInterval[i+1])
+        return instants
     # def computeVelocities(self):
 
 # need for a class representing the indicators, their units, how to print them in graphs...
--- a/python/ubc_utils.py	Sat Dec 05 11:04:03 2009 -0500
+++ b/python/ubc_utils.py	Sat Dec 05 12:08:25 2009 -0500
@@ -33,11 +33,9 @@
         obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2]))
         add = True
         if (len(lines) >= 3):
-            obj.positions = Trajectory([[float(n) for n in lines[1].split(' ')],
-                                        [float(n) for n in lines[2].split(' ')]])
+            obj.positions = Trajectory(lines[1], lines[2])
             if (len(lines) >= 5):
-                obj.velocities = Trajectory([[float(n) for n in lines[3].split(' ')],
-                                           [float(n) for n in lines[4].split(' ')]])
+                obj.velocities = Trajectory(lines[3], lines[4])
 
         if len(lines) != 2:
             objects.append(obj)