diff trafficintelligence/moving.py @ 1078:8cc3feb1c1c5

merged
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 20 Jul 2018 16:26:57 -0400
parents 3939ae415be0
children 845d694af7b8
line wrap: on
line diff
--- a/trafficintelligence/moving.py	Fri Jul 20 16:24:30 2018 -0400
+++ b/trafficintelligence/moving.py	Fri Jul 20 16:26:57 2018 -0400
@@ -80,7 +80,7 @@
     @classmethod
     def union(cls, interval1, interval2):
         '''Smallest interval comprising self and interval2'''
-        return cls(min(interval1.first, interval2.first), max(interval2.last, interval2.last))
+        return cls(min(interval1.first, interval2.first), max(interval1.last, interval2.last))
         
     @classmethod
     def intersection(cls, interval1, interval2):
@@ -1159,6 +1159,29 @@
         self.setNObjects(nObjects) # a feature has None for nObjects
         self.features = None
         # compute bounding polygon from trajectory
+        
+    @staticmethod
+    def cropedTimeInterval(obj, value, after = True):
+        newTimeInterval = TimeInterval(obj.getFirstInstant(), min(value, obj.getLastInstant())) if after else TimeInterval(max(obj.getFirstInstant(), value), obj.getLastInstant())
+        if obj.positions is not None :
+            newPositions = obj.positions[slice(newTimeInterval.first, newTimeInterval.last+1)]
+        else:
+            newPositions = None
+        if obj.velocities is not None :
+            newVelocities = obj.velocities[slice(newTimeInterval.first, newTimeInterval.last+1)]
+        else:
+            newVelocities = None
+        if obj.features is not None :
+            newFeatures = [f.cropedTimeInterval(value, after) for f in obj.features]
+        else:
+            newFeatures = None
+        res = MovingObject(obj.getNum(), newTimeInterval, newPositions, newVelocities, obj.geometry, obj.userType, obj.nObjects)
+        res.features = newFeatures
+        res.featureNumbers = obj.featureNumbers
+        if hasattr(obj, 'projectedPositions'):
+            res.projectedPositions = obj.projectedPositions[slice(newTimeInterval.first, newTimeInterval.last+1)]
+        return res
+
 
     @staticmethod
     def aggregateTrajectories(features, interval = None, aggFunc = mean):
@@ -1201,7 +1224,7 @@
         commonTimeInterval = obj1.commonTimeInterval(obj2)
         if commonTimeInterval.empty():
             #print('The two objects\' time intervals do not overlap: obj1 {} and obj2 {}'.format(obj1.getTimeInterval(), obj2.getTimeInterval()))
-            emptyInterval = TimeInterval(min(obj1.getLastInstant(),obj2.getLastInstant()), max(obj1.getFirstInstant(),obj2.getFirstInstant()))
+            emptyInterval = TimeInterval(min(obj1.getLastInstant(),obj2.getLastInstant()), max(obj1.getFirstInstant(),obj2.getFirstInstant())+1)
             if obj1.existsAtInstant(emptyInterval.last):
                 firstObject = obj2
                 secondObject = obj1
@@ -1214,7 +1237,7 @@
             featurePositions = Trajectory()
             featureVelocities = Trajectory()
             p = firstObject.getPositionAtInstant(emptyInterval.first)+v
-            for t in range(emptyInterval.first+1, emptyInterval.last):
+            for t in range(emptyInterval.first+1, emptyInterval.last+1):
             	positions.addPosition(p)
             	velocities.addPosition(v)
             	featurePositions.addPosition(p)
@@ -1231,6 +1254,7 @@
                     print('Issue, new created feature has no num id')
             if obj1.hasFeatures() and obj2.hasFeatures():
                 newObject.features = obj1.getFeatures()+obj2.getFeatures()+[MovingObject(newFeatureNum, TimeInterval(emptyInterval.first+1, emptyInterval.last-1), featurePositions, featureVelocities)]
+                newObject.updatePositions()
         else: # time intervals overlap
             newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval())
             newObject = MovingObject(newNum, newTimeInterval, nObjects = 1) # hypothesis is that it's the same object being reunited
@@ -1348,6 +1372,7 @@
             self.nObjects = nObjects
         else:
             print('Number of objects represented by object {} must be greater or equal to 1 ({})'.format(self.getNum(), nObjects))
+            self.nObjects = None
         
     def setFeatures(self, features, featuresOrdered = False):
         '''Sets the features in the features field based on featureNumbers