comparison trafficintelligence/moving.py @ 1222:69b531c7a061

added methods to reset trajectories and change object coordinates (including features)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 20 Jun 2023 15:42:19 -0400
parents 8a626226793e
children d478d3122804 eb3936809ea5
comparison
equal deleted inserted replaced
1221:5a207c838323 1222:69b531c7a061
871 self.positions[1][i] = y 871 self.positions[1][i] = y
872 872
873 def setPosition(self, i, p): 873 def setPosition(self, i, p):
874 self.setPositionXY(i, p.x, p.y) 874 self.setPositionXY(i, p.x, p.y)
875 875
876 def reset(self, x, y):
877 for i in range(self.__len__()):
878 self.positions[0][i] = x
879 self.positions[1][i] = y
880
876 def addPositionXY(self, x, y): 881 def addPositionXY(self, x, y):
877 self.positions[0].append(x) 882 self.positions[0].append(x)
878 self.positions[1].append(y) 883 self.positions[1].append(y)
879 884
880 def addPosition(self, p): 885 def addPosition(self, p):
881 self.addPositionXY(p.x, p.y) 886 self.addPositionXY(p.x, p.y)
882 887
883 def duplicateLastPosition(self): 888 def duplicateLastPosition(self):
884 self.positions[0].append(self.positions[0][-1]) 889 self.positions[0].append(self.positions[0][-1])
885 self.positions[1].append(self.positions[1][-1]) 890 self.positions[1].append(self.positions[1][-1])
891
892 def agg(self, aggFunc = mean):
893 return Point.agg(self, aggFunc)
886 894
887 @staticmethod 895 @staticmethod
888 def _plot(positions, options = '', withOrigin = False, lastCoordinate = None, timeStep = 1, objNum = None, **kwargs): 896 def _plot(positions, options = '', withOrigin = False, lastCoordinate = None, timeStep = 1, objNum = None, **kwargs):
889 if lastCoordinate is None: 897 if lastCoordinate is None:
890 plot(positions[0][::timeStep], positions[1][::timeStep], options, **kwargs) 898 plot(positions[0][::timeStep], positions[1][::timeStep], options, **kwargs)
949 957
950 def __mul__(self, alpha): 958 def __mul__(self, alpha):
951 '''Returns a new trajectory of the same length''' 959 '''Returns a new trajectory of the same length'''
952 return Trajectory([[alpha*x for x in self.getXCoordinates()], 960 return Trajectory([[alpha*x for x in self.getXCoordinates()],
953 [alpha*y for y in self.getYCoordinates()]]) 961 [alpha*y for y in self.getYCoordinates()]])
962
963 def filterMovingWindow(self, halfWidth, mode = 'valid'):
964 '''Returns a new Trajectory obtained after the smoothing of the input by a moving average'''
965 return Trajectory([utils.filterMovingWindow(self.positions[0], halfWidth, mode),
966 utils.filterMovingWindow(self.positions[1], halfWidth, mode)])
954 967
955 def differentiate(self, doubleLastPosition = False): 968 def differentiate(self, doubleLastPosition = False):
956 diff = Trajectory() 969 diff = Trajectory()
957 for i in range(1, self.length()): 970 for i in range(1, self.length()):
958 diff.addPosition(self[i]-self[i-1]) 971 diff.addPosition(self[i]-self[i-1])
1763 return self.positions.getXCoordinates() 1776 return self.positions.getXCoordinates()
1764 1777
1765 def getYCoordinates(self): 1778 def getYCoordinates(self):
1766 return self.positions.getYCoordinates() 1779 return self.positions.getYCoordinates()
1767 1780
1781 def setStationary(self):
1782 '''Resets the position to the mean of existing positions and sets speeds to 0
1783 And does the same to the features
1784 TODO: other options (provide x, y, what to do with features?)'''
1785 meanPosition = self.positions.agg(mean)
1786 self.positions.reset(meanPosition.x, meanPosition.y)
1787 self.velocities.reset(0,0)
1788 if self.hasFeatures():
1789 for f in self.getFeatures():
1790 f.setStationary()
1791
1768 def plot(self, options = '', withOrigin = False, timeStep = 1, withFeatures = False, withIds = False, **kwargs): 1792 def plot(self, options = '', withOrigin = False, timeStep = 1, withFeatures = False, withIds = False, **kwargs):
1769 if withIds: 1793 if withIds:
1770 objNum = self.getNum() 1794 objNum = self.getNum()
1771 else: 1795 else:
1772 objNum = None 1796 objNum = None