diff python/moving.py @ 92:a5ef9e40688e

makes use of matplotlib function to test if point is in a polygon
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 12 Jun 2011 03:24:55 -0400
parents daa05fae1a70
children 19603b5fa578
line wrap: on
line diff
--- a/python/moving.py	Thu Jun 09 16:03:17 2011 -0400
+++ b/python/moving.py	Sun Jun 12 03:24:55 2011 -0400
@@ -143,7 +143,9 @@
         '''Returns if the point x, y is inside the polygon.
         The polygon is defined by the ordered list of points in poly
         
-        taken from http://www.ariel.com.au/a/python-point-int-poly.html'''
+        taken from http://www.ariel.com.au/a/python-point-int-poly.html
+
+        Use points_inside_poly from matplotlib.nxutils'''
 
         n = len(poly);
         counter = 0;
@@ -237,6 +239,10 @@
 
     def getYCoordinates(self):
         return self.positions[1]
+
+    def asArray(self):
+        from numpy.core.multiarray import array
+        return array(self.positions)
     
     def xBounds(self):
         # look for function that does min and max in one pass
@@ -308,13 +314,15 @@
         else:
             return None
     
-    def getTrajectoryInPolygon1(self, polygon):
-        'Returns the set of points inside the polygon'
+    def getTrajectoryInPolygon(self, polygon):
+        '''Returns the set of points inside the polygon
+        (array of Nx2 coordinates of the polygon vertices)'''
+        import matplotlib.nxutils as nx
         t = Trajectory()
+        result = nx.points_inside_poly(self.asArray().T, polygon)
         for i in xrange(self.length()):
-            p = self.__getitem__(i)
-            if p.inPolygon(polygon):
-                t.addPosition(p)
+            if result[i]:
+                t.addPositionXY(self.positions[0][i], self.positions[1][i])
         if t.length()>0:
             return t
         else: