Mercurial > hg > nsaunier > traffic-intelligence
comparison python/moving.py @ 630:69a98f84f3eb
corrected major issue with pPET, only for CVDirect prediction for now
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 17 Feb 2015 02:21:31 +0100 |
| parents | 82e9f78a4714 |
| children | 2d1d33ae1c69 |
comparison
equal
deleted
inserted
replaced
| 629:0a5e89d6fc62 | 630:69a98f84f3eb |
|---|---|
| 577 and utils.inBetween(p3.y, p4.y, inter.y)): | 577 and utils.inBetween(p3.y, p4.y, inter.y)): |
| 578 return inter | 578 return inter |
| 579 else: | 579 else: |
| 580 return None | 580 return None |
| 581 | 581 |
| 582 def segmentLineIntersection(p1, p2, p3, p4): | |
| 583 '''Indicates if the line going through p1 and p2 intersects inside p3, p4''' | |
| 584 inter = intersection(p1, p2, p3, p4) | |
| 585 if inter != None and utils.inBetween(p3.x, p4.x, inter.x) and utils.inBetween(p3.y, p4.y, inter.y)): | |
| 586 return inter | |
| 587 else: | |
| 588 return None | |
| 589 | |
| 590 | |
| 582 class Trajectory(object): | 591 class Trajectory(object): |
| 583 '''Class for trajectories: temporal sequence of positions | 592 '''Class for trajectories: temporal sequence of positions |
| 584 | 593 |
| 585 The class is iterable''' | 594 The class is iterable''' |
| 586 | 595 |
| 806 | 815 |
| 807 for i in xrange(self.length()-1): | 816 for i in xrange(self.length()-1): |
| 808 q1=self.__getitem__(i) | 817 q1=self.__getitem__(i) |
| 809 q2=self.__getitem__(i+1) | 818 q2=self.__getitem__(i+1) |
| 810 p = utils.segmentIntersection(q1, q2, p1, p2) | 819 p = utils.segmentIntersection(q1, q2, p1, p2) |
| 811 if p: | 820 if p != None: |
| 812 if q1.x != q2.x: | 821 if q1.x != q2.x: |
| 813 ratio = (p.x-q1.x)/(q2.x-q1.x) | 822 ratio = (p.x-q1.x)/(q2.x-q1.x) |
| 814 elif q1.y != q2.y: | 823 elif q1.y != q2.y: |
| 815 ratio = (p.y-q1.y)/(q2.y-q1.y) | 824 ratio = (p.y-q1.y)/(q2.y-q1.y) |
| 816 else: | 825 else: |
| 817 ratio = 0 | 826 ratio = 0 |
| 818 indices.append(i+ratio) | 827 indices.append(i+ratio) |
| 819 return indices | 828 return indices |
| 829 | |
| 830 def getLineIntersections(self, p1, p2): | |
| 831 '''Returns a list of the indices at which the trajectory | |
| 832 intersects with the segment of extremities p1 and p2 | |
| 833 the list is empty if there is no crossing''' | |
| 834 indices = [] | |
| 835 intersections = [] | |
| 836 | |
| 837 for i in xrange(self.length()-1): | |
| 838 q1=self.__getitem__(i) | |
| 839 q2=self.__getitem__(i+1) | |
| 840 p = utils.segmentLineIntersection(p1, p2, q1, q2) | |
| 841 if p != None: | |
| 842 if q1.x != q2.x: | |
| 843 ratio = (p.x-q1.x)/(q2.x-q1.x) | |
| 844 elif q1.y != q2.y: | |
| 845 ratio = (p.y-q1.y)/(q2.y-q1.y) | |
| 846 else: | |
| 847 ratio = 0 | |
| 848 indices.append(i+ratio) | |
| 849 intersections.append(p) | |
| 850 return indices, intersections | |
| 820 | 851 |
| 821 def getTrajectoryInInterval(self, inter): | 852 def getTrajectoryInInterval(self, inter): |
| 822 'Returns all position between index inter.first and index.last (included)' | 853 'Returns all position between index inter.first and index.last (included)' |
| 823 if inter.first >=0 and inter.last<= self.length(): | 854 if inter.first >=0 and inter.last<= self.length(): |
| 824 return Trajectory([self.positions[0][inter.first:inter.last+1], | 855 return Trajectory([self.positions[0][inter.first:inter.last+1], |
