diff python/moving.py @ 623:ce7133cbcdf3

implemented function to concatenate objects
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 23 Dec 2014 14:56:13 -0500
parents dc8490726d06
children bac66bd536c5
line wrap: on
line diff
--- a/python/moving.py	Mon Dec 22 16:29:34 2014 -0500
+++ b/python/moving.py	Tue Dec 23 14:56:13 2014 -0500
@@ -195,6 +195,10 @@
         'Warning, returns a new Point'
         return Point(self.x*alpha, self.y*alpha)
 
+    def divide(self, alpha):
+        'Warning, returns a new Point'
+        return Point(self.x/alpha, self.y/alpha)
+
     def plot(self, options = 'o', **kwargs):
         from matplotlib.pylab import plot
         plot([self.x], [self.y], options, **kwargs)
@@ -948,9 +952,53 @@
         return MovingObject(timeInterval = timeInterval, positions = positions, velocities = velocities)
 
     @staticmethod
-    def concatenate(obj1, obj2):
-        ''' '''
-        pass
+    def concatenate(obj1, obj2, num = None):
+        '''Concatenates two objects supposed to overlap temporally '''
+        commonTimeInterval = obj1.commonTimeInterval(obj2)
+        if commonTimeInterval.empty():
+            print('The two objects\' time intervals do not overlap: obj1 {} and obj2 {}'.format(obj1.getTimeInterval(), obj2.getTimeInterval()))
+            return None
+        else:
+            if num == None:
+                newNum = obj1.getNum()
+            else:
+                newNum = num
+            newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval())
+            # positions
+            positions = Trajectory()
+            for t in newTimeInterval:
+                p = Point(0.,0.)
+                n = 0.
+                if obj1.existsAtInstant(t):
+                    p += obj1.getPositionAtInstant(t)
+                    n += 1.
+                if obj2.existsAtInstant(t):
+                    p += obj2.getPositionAtInstant(t)
+                    n += 1.
+                assert n>0, 'there should be at least one point for each instant'
+                positions.addPosition(p.divide(n))
+            # velocities: if any
+            if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'):
+                velocities = Trajectory()
+                for t in newTimeInterval:
+                    p = Point(0.,0.)
+                    n = 0.
+                    if obj1.existsAtInstant(t):
+                        p += obj1.getVelocityAtInstant(t)
+                        n += 1.
+                    if obj2.existsAtInstant(t):
+                        p += obj2.getVelocityAtInstant(t)
+                        n += 1.
+                    assert n>0, 'there should be at least one point for each instant'
+                    velocities.addPosition(p.divide(n))
+            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()]))
+
+            return MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType())
 
     def getObjectInTimeInterval(self, inter):
         '''Returns a new object extracted from self,