changeset 83:41da2cdcd91c

re-arranged trajectory intersections
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 18 Mar 2011 15:06:16 -0400
parents 76735aeab807
children 731df2fa0010
files python/moving.py
diffstat 1 files changed, 23 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/python/moving.py	Thu Mar 17 23:06:49 2011 -0400
+++ b/python/moving.py	Fri Mar 18 15:06:16 2011 -0400
@@ -5,7 +5,7 @@
 
 from math import sqrt, hypot;
 
-from shapely.geometry import Polygon
+#from shapely.geometry import Polygon
 
 __metaclass__ = type
 
@@ -274,6 +274,25 @@
     def wiggliness(self):
         return self.cumulatedDisplacement()/float(Point.distanceNorm2(self.__getitem__(0),self.__getitem__(self.length()-1)))
 
+    def getIntersections(self, p1, p2):
+        '''Returns a list of the indices at which the trajectory 
+        intersects with the segment of extremities p1 and p2 the list is empty if there is no crossing'''
+        indices = []
+
+        for i in xrange(self.length()-1):
+            q1=self.__getitem__(i)
+            q2=self.__getitem__(i+1)
+            p = utils.segmentIntersection(q1, q2, p1, p2)
+            if p:
+                if q1.x != q2.x:
+                    ratio = (p.x-q1.x)/(q2.x-q1.x)
+                elif q1.y != q2.y:
+                    ratio = (p.y-q1.y)/(q2.y-q1.y)
+                else:
+                    ratio = 0
+                indices.append(i+ratio)
+        return indices
+
     def getTrajectoryInInterval(self, inter):
         if inter.first >=0 and inter.last<= self.length():
             return Trajectory([self.positions[0][inter.first:inter.last],
@@ -364,23 +383,12 @@
     def draw(self, options = ''):
         self.positions.draw(options)
 
-    def getInstantPassingLane(self, p1, p2):
+    def getInstantsCrossingLane(self, p1, p2):
         '''Returns the instant(s)
         at which the object passes from one side of the segment to the other
         empty list if there is no crossing'''
-        instants = []
-
-        for i in xrange(self.length()-1):
-            p = utils.segmentIntersection(self.positions[i], self.positions[i+1], p1, p2)
-            if p:
-                if self.positions[i].x != self.positions[i+1].x:
-                    ratio = (p.x-self.positions[i].x)/(self.positions[i+1].x-self.positions[i].x)
-                elif self.positions[i].y != self.positions[i+1].y:
-                    ratio = (p.y-self.positions[i].y)/(self.positions[i+1].y-self.positions[i].y)
-                else:
-                    ratio = 0
-                instants.append(self.timeInterval[i]*(1-ratio)+ratio*self.timeInterval[i+1])
-        return instants
+        indices = self.positions.getIntersections(p1, p2)
+        return [t+self.getFirstInstant() for t in indices]
 
     @staticmethod
     def collisionCourseDotProduct(movingObject1, movingObject2, instant):