diff python/moving.py @ 258:d90be3c02267

reasonably efficient computation of collision points and crossing zones
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 24 Jul 2012 12:37:47 -0400
parents dc1faa7287bd
children 8ab76b95ee72
line wrap: on
line diff
--- a/python/moving.py	Tue Jul 24 03:27:29 2012 -0400
+++ b/python/moving.py	Tue Jul 24 12:37:47 2012 -0400
@@ -1,10 +1,10 @@
 #! /usr/bin/env python
 '''Libraries for moving objects, trajectories...'''
 
-import utils;
-import cvutils;
+import utils
+import cvutils
 
-from math import sqrt;
+from math import sqrt
 
 #from shapely.geometry import Polygon
 
@@ -295,25 +295,31 @@
     from numpy import matrix
     from numpy.linalg import linalg, det
 
-    dp1 = p2-p1#[s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]]
-    dp2 = p4-p3#[s2[0][1]-s2[0][0], s2[1][1]-s2[1][0]]
-
-    A = matrix([[dp1.y, -dp1.x],
-                [dp2.y, -dp2.x]])
-    B = matrix([[dp1.y*p1.x-dp1.x*p1.y],
-                [dp2.y*p3.x-dp2.x*p3.y]])
-
-    if linalg.det(A) == 0:#crossProduct(ds1, ds2) == 0:
+    if (Interval(p1.x,p2.x, True).intersection(Interval(p3.x,p4.x, True)).empty()
+        or Interval(p1.y,p2.y, True).intersection(Interval(p3.y,p4.y, True)).empty()):
         return None
     else:
-        intersection = linalg.solve(A,B)
-        if (utils.inBetween(p1.x, p2.x, intersection[0,0])
-            and utils.inBetween(p3.x, p4.x, intersection[0,0])
-            and utils.inBetween(p1.y, p2.y, intersection[1,0])
-            and utils.inBetween(p3.y, p4.y, intersection[1,0])):
-            return Point(intersection[0,0], intersection[1,0])
+        dp1 = p2-p1#[s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]]
+        dp2 = p4-p3#[s2[0][1]-s2[0][0], s2[1][1]-s2[1][0]]
+
+        A = matrix([[dp1.y, -dp1.x],
+                    [dp2.y, -dp2.x]])
+        B = matrix([[dp1.y*p1.x-dp1.x*p1.y],
+                    [dp2.y*p3.x-dp2.x*p3.y]])
+
+        if linalg.det(A) == 0:#crossProduct(ds1, ds2) == 0:
+            return None
         else:
-            return None
+            intersection = linalg.solve(A,B)
+            if (utils.inBetween(p1.x, p2.x, intersection[0,0])
+                and utils.inBetween(p3.x, p4.x, intersection[0,0])
+                and utils.inBetween(p1.y, p2.y, intersection[1,0])
+                and utils.inBetween(p3.y, p4.y, intersection[1,0])):
+                return Point(intersection[0,0], intersection[1,0])
+            else:
+                return None
+
+# TODO: implement a better algorithm for intersections of sets of segments http://en.wikipedia.org/wiki/Line_segment_intersection
 
 class Trajectory:
     '''Class for trajectories
@@ -408,11 +414,11 @@
     
     def xBounds(self):
         # look for function that does min and max in one pass
-        return [min(self.getXCoordinates()), max(self.getXCoordinates())]
+        return Interval(min(self.getXCoordinates()), max(self.getXCoordinates()))
     
     def yBounds(self):
         # look for function that does min and max in one pass
-        return [min(self.getYCoordinates()), max(self.getYCoordinates())]
+        return Interval(min(self.getYCoordinates()), max(self.getYCoordinates()))
     
     def add(self, traj2):
         '''Returns a new trajectory of the same length'''