diff python/moving.py @ 627:82e9f78a4714

added test for location for trajectories
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 16 Feb 2015 08:41:14 +0100
parents 35155ac2a294
children 69a98f84f3eb
line wrap: on
line diff
--- a/python/moving.py	Sat Feb 14 19:18:14 2015 -0500
+++ b/python/moving.py	Mon Feb 16 08:41:14 2015 +0100
@@ -235,7 +235,7 @@
         projected = cvutils.projectArray(homography, array([[self.x], [self.y]]))
         return Point(projected[0], projected[1])
 
-    def inPolygonNoShapely(self, polygon):
+    def inPolygon(self, polygon):
         '''Indicates if the point x, y is inside the polygon
         (array of Nx2 coordinates of the polygon vertices)
 
@@ -793,10 +793,7 @@
         for p in self:
             if p.similarOrientation(refDirection, cosineThreshold):
                 count += 1
-        if count > lengthThreshold:
-            return True
-        else:
-            return False
+        return count >= lengthThreshold
 
     def wiggliness(self):
         return self.getCumulativeDistance(self.length()-1)/float(Point.distanceNorm2(self.__getitem__(0),self.__getitem__(self.length()-1)))
@@ -829,15 +826,6 @@
         else:
             return None
     
-    def getTrajectoryInPolygonNoShapely(self, polygon):
-        '''Returns the trajectory built with the set of points inside the polygon
-        (array of Nx2 coordinates of the polygon vertices)'''
-        traj = Trajectory()
-        for p in self:
-            if p.inPolygonNoShapely(polygon):
-                traj.addPosition(p)
-        return traj
-
     if shapelyAvailable:
         def getTrajectoryInPolygon(self, polygon):
             '''Returns the trajectory built with the set of points inside the (shapely) polygon'''
@@ -846,7 +834,26 @@
             for p in pointsInPolygon(points, polygon):
                     traj.addPositionXY(p.x, p.y)
             return traj
-    
+
+        def proportionInPolygon(self, polygon, minProportion = 0.5):
+            pointsIn = pointsInPolygon([p.asShapely() for p in self], polygon)
+            lengthThreshold = float(self.length())*minProportion
+            return len(pointsIn) >= lengthThreshold
+    else:
+        def getTrajectoryInPolygon(self, polygon):
+            '''Returns the trajectory built with the set of points inside the polygon
+            (array of Nx2 coordinates of the polygon vertices)'''
+            traj = Trajectory()
+            for p in self:
+                if p.inPolygon(polygon):
+                    traj.addPosition(p)
+            return traj
+
+        def proportionInPolygon(self, polygon, minProportion = 0.5):
+            pointsInPolygon = [p.inPolygon(polygon) for p in self]
+            lengthThreshold = float(self.length())*minProportion
+            return len(pointsInPolygon) >= lengthThreshold
+
     @staticmethod
     def lcss(t1, t2, lcss):
         return lcss.compute(t1, t2)