Mercurial Hosting > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
575:13df64a9ff9d | 576:0eff0471f9cb |
---|---|
606 [float(n) for n in line2.split(' ')]]) | 606 [float(n) for n in line2.split(' ')]]) |
607 | 607 |
608 @staticmethod | 608 @staticmethod |
609 def fromPointList(points): | 609 def fromPointList(points): |
610 t = Trajectory() | 610 t = Trajectory() |
611 for p in points: | 611 if isinstance(points[0], list) or isinstance(points[0], tuple): |
612 t.addPosition(p) | 612 for p in points: |
613 t.addPositionXY(p[0],p[1]) | |
614 else: | |
615 for p in points: | |
616 t.addPosition(p) | |
613 return t | 617 return t |
614 | 618 |
615 def __len__(self): | 619 def __len__(self): |
616 return len(self.positions[0]) | 620 return len(self.positions[0]) |
617 | 621 |
743 'Returns the sum of the distances between each successive point' | 747 'Returns the sum of the distances between each successive point' |
744 displacement = 0 | 748 displacement = 0 |
745 for i in xrange(self.length()-1): | 749 for i in xrange(self.length()-1): |
746 displacement += Point.distanceNorm2(self.__getitem__(i),self.__getitem__(i+1)) | 750 displacement += Point.distanceNorm2(self.__getitem__(i),self.__getitem__(i+1)) |
747 return displacement | 751 return displacement |
752 | |
753 def computeCumulativeDistances(self): | |
754 '''Computes the distance from each point to the next and the cumulative distance up to the point | |
755 Can be accessed through getDistance(idx) and getCumulativeDistance(idx)''' | |
756 self.distances = [] | |
757 self.cumulativeDistances = [] | |
758 p1 = self[0] | |
759 cumulativeDistance = 0. | |
760 for i in xrange(self.length()-1): | |
761 p2 = self[i+1] | |
762 self.distances.append(Point.distanceNorm2(p1,p2)) | |
763 cumulativeDistance += self.distances[-1] | |
764 self.cumulativeDistances.append(cumulativeDistance) | |
765 p1 = p2 | |
766 | |
767 def getDistance(self,i): | |
768 '''Return the distance between points i and i+1''' | |
769 if i < self.length()-1: | |
770 return self.distances[i] | |
771 else: | |
772 print('Index {} beyond trajectory length {}-1'.format(i, self.length())) | |
773 | |
774 def getCumulativeDistance(self, i): | |
775 '''Return the cumulative distance between the beginning and point i+1''' | |
776 if i < self.length()-1: | |
777 return self.cumulativeDistances[i] | |
778 else: | |
779 print('Index {} beyond trajectory length {}-1'.format(i, self.length())) | |
748 | 780 |
749 def similarOrientation(self, refDirection, cosineThreshold, minProportion = 0.5): | 781 def similarOrientation(self, refDirection, cosineThreshold, minProportion = 0.5): |
750 '''Indicates whether the minProportion (<=1.) (eg half) of the trajectory elements (vectors for velocity) | 782 '''Indicates whether the minProportion (<=1.) (eg half) of the trajectory elements (vectors for velocity) |
751 have a cosine with refDirection is smaller than cosineThreshold''' | 783 have a cosine with refDirection is smaller than cosineThreshold''' |
752 count = 0 | 784 count = 0 |