Mercurial Hosting > traffic-intelligence
changeset 1018:d7afc59f6966
work in progress
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 04 Jun 2018 17:46:52 -0400 |
parents | 89cbc4056c13 |
children | 5d2f6afae35b |
files | python/moving.py python/tests/moving.txt |
diffstat | 2 files changed, 40 insertions(+), 28 deletions(-) [+] |
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,
--- a/python/tests/moving.txt Mon Jun 04 17:42:35 2018 -0400 +++ b/python/tests/moving.txt Mon Jun 04 17:46:52 2018 -0400 @@ -199,6 +199,10 @@ >>> t.differentiate().empty() True +>>> o1 = MovingObject.generate(1, Point(1., 2.), Point(1., 1.), TimeInterval(0,10)) +>>> o2 = MovingObject.generate(2, Point(14., 14.), Point(1., 0.), TimeInterval(14,20)) + + >>> o1 = MovingObject.generate(1, Point(0., 2.), Point(0., 1.), TimeInterval(0,2)) >>> o1.classifyUserTypeSpeedMotorized(0.5, np.median) >>> userTypeNames[o1.getUserType()]