diff python/moving.py @ 576:0eff0471f9cb

added functions to use trajectories as alignments
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 28 Aug 2014 16:42:13 -0400
parents 13df64a9ff9d
children d0abd2ee17b9
line wrap: on
line diff
--- a/python/moving.py	Wed Aug 13 10:51:16 2014 -0400
+++ b/python/moving.py	Thu Aug 28 16:42:13 2014 -0400
@@ -608,8 +608,12 @@
     @staticmethod
     def fromPointList(points):
         t = Trajectory()
-        for p in points:
-            t.addPosition(p)
+        if isinstance(points[0], list) or isinstance(points[0], tuple):
+            for p in points:
+                t.addPositionXY(p[0],p[1])
+        else:
+            for p in points:
+                t.addPosition(p)
         return t
 
     def __len__(self):
@@ -746,6 +750,34 @@
             displacement += Point.distanceNorm2(self.__getitem__(i),self.__getitem__(i+1))
         return displacement
 
+    def computeCumulativeDistances(self):
+        '''Computes the distance from each point to the next and the cumulative distance up to the point
+        Can be accessed through getDistance(idx) and getCumulativeDistance(idx)'''
+        self.distances = []
+        self.cumulativeDistances = []
+        p1 = self[0]
+        cumulativeDistance = 0.
+        for i in xrange(self.length()-1):
+            p2 = self[i+1]
+            self.distances.append(Point.distanceNorm2(p1,p2))
+            cumulativeDistance += self.distances[-1]
+            self.cumulativeDistances.append(cumulativeDistance)
+            p1 = p2
+
+    def getDistance(self,i):
+        '''Return the distance between points i and i+1'''
+        if i < self.length()-1:
+            return self.distances[i]
+        else:
+            print('Index {} beyond trajectory length {}-1'.format(i, self.length()))
+
+    def getCumulativeDistance(self, i):
+        '''Return the cumulative distance between the beginning and point i+1'''
+        if i < self.length()-1:
+            return self.cumulativeDistances[i]
+        else:
+            print('Index {} beyond trajectory length {}-1'.format(i, self.length()))
+
     def similarOrientation(self, refDirection, cosineThreshold, minProportion = 0.5):
         '''Indicates whether the minProportion (<=1.) (eg half) of the trajectory elements (vectors for velocity) 
         have a cosine with refDirection is smaller than cosineThreshold'''