diff 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
line wrap: on
line diff
--- a/trafficintelligence/moving.py	Mon Jun 19 22:37:45 2023 -0400
+++ b/trafficintelligence/moving.py	Tue Jun 20 15:42:19 2023 -0400
@@ -873,6 +873,11 @@
     def setPosition(self, i, p):
         self.setPositionXY(i, p.x, p.y)
 
+    def reset(self, x, y):
+        for i in range(self.__len__()):
+            self.positions[0][i] = x
+            self.positions[1][i] = y
+
     def addPositionXY(self, x, y):
         self.positions[0].append(x)
         self.positions[1].append(y)
@@ -884,6 +889,9 @@
         self.positions[0].append(self.positions[0][-1])
         self.positions[1].append(self.positions[1][-1])
 
+    def agg(self, aggFunc = mean):
+        return Point.agg(self, aggFunc)
+
     @staticmethod
     def _plot(positions, options = '', withOrigin = False, lastCoordinate = None, timeStep = 1, objNum = None, **kwargs):
         if lastCoordinate is None:
@@ -952,6 +960,11 @@
         return Trajectory([[alpha*x for x in self.getXCoordinates()],
                            [alpha*y for y in self.getYCoordinates()]])
 
+    def filterMovingWindow(self, halfWidth, mode = 'valid'):
+        '''Returns a new Trajectory obtained after the smoothing of the input by a moving average'''
+        return Trajectory([utils.filterMovingWindow(self.positions[0], halfWidth, mode),
+                           utils.filterMovingWindow(self.positions[1], halfWidth, mode)])
+
     def differentiate(self, doubleLastPosition = False):
         diff = Trajectory()
         for i in range(1, self.length()):
@@ -1765,6 +1778,17 @@
     def getYCoordinates(self):
         return self.positions.getYCoordinates()
 
+    def setStationary(self):
+        '''Resets the position to the mean of existing positions and sets speeds to 0
+        And does the same to the features
+        TODO: other options (provide x, y, what to do with features?)'''
+        meanPosition = self.positions.agg(mean)
+        self.positions.reset(meanPosition.x, meanPosition.y)
+        self.velocities.reset(0,0)
+        if self.hasFeatures():
+            for f in self.getFeatures():
+                f.setStationary()
+        
     def plot(self, options = '', withOrigin = False, timeStep = 1, withFeatures = False, withIds = False, **kwargs):
         if withIds:
             objNum = self.getNum()