Mercurial Hosting > traffic-intelligence
changeset 1010:c90c4682c67e
concatenateWith,addFeatures,delFeatures,updatePosition, (new functions) ; concatenate (update) : from line 1145 to 1320
author | Wendlasida |
---|---|
date | Fri, 25 May 2018 15:14:27 -0400 |
parents | add667153087 |
children | 4f0312bee393 |
files | python/moving.py |
diffstat | 1 files changed, 120 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/python/moving.py Fri May 25 14:23:56 2018 -0400 +++ b/python/moving.py Fri May 25 15:14:27 2018 -0400 @@ -1143,7 +1143,106 @@ return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities) @staticmethod - def concatenate(obj1, obj2, num = None, computePositions = False): + def concatenateWith(obj1, ObjectList=[], num = None): #Concatenate with a list of MovingObjects + 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() : + newObject.features = list(obj1.features) + else: + def getKey(item): + return 0 if not obj1.commonTimeInterval(item).empty() else max(obj1.getFirstInstant(),item.getFirstInstant())-min(obj1.getLastInstant(),item.getLastInstant()) + Order = min(ObjectList , key=getKey) + newObject = MovingObject.concatenate(obj1, Order, num) + ObjectList.remove(Order) + 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 and i not in FeatureList] + if len(obj1.features)!=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 newTimeInterval: + 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()) + return newObject + + + def updatePosition(self): + if self.features is not None: + print 'On update ici' + 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 + + @staticmethod + def concatenate(obj1, obj2, num = None): '''Concatenates two objects supposed to overlap temporally ''' if num is None: newNum = obj1.getNum() @@ -1171,14 +1270,30 @@ 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 return MovingObject.concatenate(MovingObject.concatenate(obj1, newObject),obj2) else: newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) # positions - if computePositions: # TODO it would probably be better to recompute from all features, if features are available, in other method - positions = Trajectory() + 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)) + # velocities: if any + if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'): + velocities = Trajectory() for t in newTimeInterval: nTotal = 0. p = Point(0.,0.) @@ -1188,30 +1303,11 @@ n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) else: n = 1. - p += obj.getPositionAtInstant(t).__mul__(n) + p += obj.getVelocityAtInstant(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 obj1.getVelocities() is not None and obj2.getVelocities() is not None: - velocities = 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.getVelocityAtInstant(t).__mul__(n) - nTotal += n - assert n>0, 'there should be at least one point for each instant' - velocities.addPosition(p.divide(nTotal)) - else: - velocities = None + velocities.addPosition(p.divide(nTotal)) else: - positions = None velocities = None # TODO object envelop (polygon) # user type @@ -1219,8 +1315,6 @@ 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 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