Mercurial Hosting > traffic-intelligence
changeset 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 |
files | python/moving.py python/tests/moving.txt |
diffstat | 2 files changed, 45 insertions(+), 2 deletions(-) [+] |
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'''
--- a/python/tests/moving.txt Wed Aug 13 10:51:16 2014 -0400 +++ b/python/tests/moving.txt Thu Aug 28 16:42:13 2014 -0400 @@ -96,6 +96,17 @@ >>> t1.differentiate() (1.000000,2.000000) (2.000000,5.000000) +>>> t1.computeCumulativeDistances() +>>> t1.getDistance(0) +2.23606797749979 +>>> t1.getDistance(1) +5.385164807134504 +>>> t1.getCumulativeDistance(1) +7.6212327846342935 +>>> t1.getCumulativeDistance(2) +Index 2 beyond trajectory length 3-1 + + >>> from utils import LCSS >>> lcss = LCSS(lambda x,y: Point.distanceNorm2(x,y) <= 0.1) >>> Trajectory.lcss(t1, t1, lcss)