diff python/moving.py @ 1018:d7afc59f6966

work in progress
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 04 Jun 2018 17:46:52 -0400
parents 9b53be469a36
children 5d2f6afae35b
line wrap: on
line diff
--- a/python/moving.py	Mon Jun 04 17:42:35 2018 -0400
+++ b/python/moving.py	Mon Jun 04 17:46:52 2018 -0400
@@ -1163,7 +1163,7 @@
     	return newObject
 
     @staticmethod
-    def concatenateWith(obj1, ObjectList=[], num = None): #Concatenate with a list of MovingObjects
+    def concatenateWith(obj1, ObjectList=[], num = None):
     	if len(ObjectList)==0:
     		newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType())
     		if obj1.hasFeatures() :
@@ -1196,32 +1196,39 @@
 	    	self.positions = positions
 
     @staticmethod
-    def concatenate(obj1, obj2, num = None):
-        '''Concatenates two objects supposed to overlap temporally '''
+    def concatenate(obj1, obj2, num = None, computePositions = False):
+        '''Concatenates two objects, whether overlapping temporally or not
+
+        Positions will be recomputed only if computePositions is True
+        Otherwise, only featureNumbers and/or features will be merged'''
         if num is None:
             newNum = obj1.getNum()
         else:
             newNum = num
         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()))
-            positions = Trajectory()
+            #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()))
             if obj1.existsAtInstant(emptyInterval.last):
-            	vitessex=(obj1.getPositionAtInstant(emptyInterval.last).x-obj2.getPositionAtInstant(emptyInterval.first).x)/(emptyInterval.last-emptyInterval.first)
-            	vitessey=(obj1.getPositionAtInstant(emptyInterval.last).y-obj2.getPositionAtInstant(emptyInterval.first).y)/(emptyInterval.last-emptyInterval.first)
-            	px,py=obj2.getPositionAtInstant(emptyInterval.first)
-            else :
-            	vitessex=(obj2.getPositionAtInstant(emptyInterval.last).x-obj1.getPositionAtInstant(emptyInterval.first).x)/(emptyInterval.last-emptyInterval.first)
-            	vitessey=(obj2.getPositionAtInstant(emptyInterval.last).y-obj1.getPositionAtInstant(emptyInterval.first).y)/(emptyInterval.last-emptyInterval.first)
-            	px,py=obj1.getPositionAtInstant(emptyInterval.first)
-            positions = Trajectory()
-            velocities = Trajectory()
-            for t in emptyInterval:
-            	positions.addPositionXY(px,py)
-            	velocities.addPositionXY(vitessex,vitessey)
-            	px+=vitessex
-            	py+=vitessey
+                firstObject = obj2
+                secondObject = obj1
+            else:
+                firstObject = obj1
+                secondObject = obj2
+            v = (secondObject.getPositionAtInstant(emptyInterval.last)-firstObject.getPositionAtInstant(emptyInterval.first)).divide(emptyInterval.length())
+            positions = copy.deepcopy(firstObject.getPositions())
+            velocities = copy.deepcopy(firstObject.getPositions())
+            featurePositions = Trajectory()
+            featureVelocities = Trajectory()
+            #newFeature = MovingObject(-1, TimeInterval(emptyInterval.first+1, emptyInterval.last-1))# what feature number to choose?
+            p = firstObject.getPositionAtInstant(emptyInterval.first)+v
+            for t in range(emptyInterval.first+1, emptyInterval.last):
+            	positions.addPosition(p)
+            	velocities.addPosition(v)
+            	featurePositions.addPosition(p)
+            	featureVelocities.addPosition(v)
+            	p=p+v
+            # continue
             newObject = MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType())
             newObject.features = [MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType())] #In case there is features to add when we recursively call concatenate 
             return MovingObject.concatenate(MovingObject.concatenate(obj1, newObject),obj2)
@@ -1260,15 +1267,16 @@
                     velocities.addPosition(p.divide(nTotal))
             else:
                 velocities = None
-            # TODO object envelop (polygon)
-            # user type
-            if obj1.getUserType() != obj2.getUserType():
-                print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()]))
+        # user type
+        if obj1.getUserType() != obj2.getUserType():
+            print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()]))
 
-            newObject = MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType())
-            if obj1.hasFeatures() and obj2.hasFeatures():
-                newObject.features = obj1.getFeatures()+obj2.getFeatures()
-            return newObject
+        newObject = MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType())
+        if hasattr(obj1, 'featureNumbers') and hasattr(obj2, 'featureNumbers'):
+            newObject.featureNumbers = obj1.featureNumbers+obj2.featureNumbers
+        if obj1.hasFeatures() and obj2.hasFeatures():
+            newObject.features = obj1.getFeatures()+obj2.getFeatures()
+        return newObject
 
     def getObjectInTimeInterval(self, inter):
         '''Returns a new object extracted from self,