diff python/moving.py @ 1013:d6f121ded971

Moving pull request
author Wendlasida
date Fri, 01 Jun 2018 17:29:01 -0400
parents 01db14e947e4
children 026f6b3b122c
line wrap: on
line diff
--- a/python/moving.py	Fri Jun 01 10:47:49 2018 -0400
+++ b/python/moving.py	Fri Jun 01 17:29:01 2018 -0400
@@ -1154,16 +1154,13 @@
     def generate(num, p, v, timeInterval):
         positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) 
         return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities)
-        
-
-
 
     @staticmethod
     def generateWithFeatures(num, features, userType):
     	newObjectTMP = MovingObject.concatenateWith(features[0],features[1:],num)
-	newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = userType)
-	newObject.features = [copy.deepcopy(f) for f in features]
-	return newObject
+    	newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = userType)
+    	newObject.features = [copy.deepcopy(f) for f in features]
+    	return newObject
 
     @staticmethod
     def concatenateWith(obj1, ObjectList=[], num = None): #Concatenate with a list of MovingObjects
@@ -1180,95 +1177,23 @@
     		newObject = MovingObject.concatenateWith(newObject,ObjectList)
     	return newObject
 
-    '''@staticmethod
-    def addFeatures(obj1, FeatureList=[], num = None): #Add features to an object
-    	if len(FeatureList)==0: #We return a clone
-    		newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) 
-    		if obj1.features is not None:
-    			newObject.features = list(obj1.features)
-	else :
-		newObjectTMP = MovingObject.concatenateWith(obj1,FeatureList,num)
-		newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = newObjectTMP.getUserType())
-		if obj1.hasFeatures() and newObjectTMP.hasFeatures():
-			newObject.features = obj1.features + newObjectTMP.features
-		
-	return newObject'''
-	
-
-    @staticmethod
-    def delFeatures(obj1, FeatureList=[], num = None):
-    	if obj1.features is not None:
-    		if len(FeatureList)>0:
-    			tmp = [ i for i in (obj1.features if obj1.features is not None else []) if i not in FeatureList]
-    			if len(tmp)==0:
-    				newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType())
-    				newObject.features = [MovingObject(obj1.getNum() if num is None else num, newInterval, obj1.positions, obj1.velocities, userType = obj1.getUserType())]
-    			elif len(obj1.features if obj1.features is not None else [])!=len(tmp):
-    				newInterval = TimeInterval(min([i.getFirstInstant() for i in tmp]), max([i.getLastInstant() for i in tmp]))
-		    		positions = Trajectory()
-		    		for t in newInterval:
-					nTotal = 0.
-					p = Point(0.,0.)
-					for obj in tmp:
-					    if obj.existsAtInstant(t):
-						if obj.hasFeatures():
-						    n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)])
-						else:
-						    n = 1.
-						p += obj.getPositionAtInstant(t).__mul__(n)
-						nTotal += n
-					if nTotal==0:
-						positions.addPosition(obj1.getPositionAtInstant(t))
-					else:
-						positions.addPosition(p.divide(nTotal))
-				if hasattr(obj1, 'velocities'):
-					velocities = Trajectory()
-					for t in newInterval:
-						nTotal = 0.
-						p = Point(0.,0.)
-						for obj in tmp:
-							if obj.existsAtInstant(t):
-							    if obj.hasFeatures():
-								n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)])
-							    else:
-								n = 1.
-							    p += obj.getVelocityAtInstant(t).__mul__(n)
-							    nTotal += n
-						if nTotal==0:
-							velocities.addPosition(obj1.getVelocityAtInstant(t))
-						else:
-							velocities.addPosition(p.divide(nTotal))
-				else:
-					velocities = None
-				
-				newObject = MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType())
-				newObject.features = tmp+[MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType())]
-			else:
-				newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) 
-		    		if obj1.features is not None:
-		    			newObject.features = list(obj1.features)
-    	else:
-    		newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType())
-    	return newObject
-		
-
     def updatePosition(self):
     	if self.features is not None:
 	    	positions = Trajectory()
 	    	for t in self.getTimeInterval():
 	    		nTotal = 0.
-			p = Point(0.,0.)
-			for obj in self.features:
-				if obj.existsAtInstant(t):
-					if obj.hasFeatures():
-						n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)])
-					else:
-						n = 1.
-					p += obj.getPositionAtInstant(t).__mul__(n)
-					nTotal += n
-			assert nTotal>0, 'there should be at least one point for each instant'
-                	positions.addPosition(p.divide(nTotal))
-                self.positions = positions
+	    		p = Point(0.,0.)
+	    		for obj in self.features:
+	    			if obj.existsAtInstant(t):
+	    				if obj.hasFeatures():
+	    					n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)])
+	    				else:
+	    					n = 1.
+	    				p += obj.getPositionAtInstant(t).__mul__(n)
+	    				nTotal += n
+	    		assert nTotal>0, 'there should be at least one point for each instant'
+	    		positions.addPosition(p.divide(nTotal))
+	    	self.positions = positions
 
     @staticmethod
     def concatenate(obj1, obj2, num = None):
@@ -1297,9 +1222,8 @@
             	velocities.addPositionXY(vitessex,vitessey)
             	px+=vitessex
             	py+=vitessey
-
-	    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 
+            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)
                      
         else:
@@ -1307,18 +1231,18 @@
             # positions
             positions = Trajectory()
             for t in newTimeInterval:
-                nTotal = 0.
-                p = Point(0.,0.)
-                for obj in [obj1, obj2]:
-                    if obj.existsAtInstant(t):
-                        if obj.hasFeatures():
-                            n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)])
-                        else:
-                            n = 1.
-                        p += obj.getPositionAtInstant(t).__mul__(n)
-                        nTotal += n
-                assert nTotal>0, 'there should be at least one point for each instant'
-                positions.addPosition(p.divide(nTotal))
+            	nTotal = 0.
+            	p = Point(0.,0.)
+            	for obj in [obj1, obj2]:
+            		if obj.existsAtInstant(t):
+            			if obj.hasFeatures():
+		                	n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)])
+		            	else:
+		                	n = 1.
+		            	p += obj.getPositionAtInstant(t).__mul__(n)
+		            	nTotal += n
+            	assert nTotal>0, 'there should be at least one point for each instant'
+            	positions.addPosition(p.divide(nTotal))
             # velocities: if any
             if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'):
                 velocities = Trajectory()
@@ -2055,7 +1979,7 @@
     axis('equal')
 
 
-if __name__ == "__main__":
+'''if __name__ == "__main__":
     import doctest
     import unittest
     suite = doctest.DocFileSuite('tests/moving.txt')
@@ -2066,3 +1990,4 @@
     if shapelyAvailable: 
         suite = doctest.DocFileSuite('tests/moving_shapely.txt')
         unittest.TextTestRunner().run(suite)
+            for t in newTimeInterval:'''