Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 663:65a867942eee
modified concatenate to take into account features if available
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 19 May 2015 16:58:26 +0200 |
parents | 72174e66aba5 |
children | 455f9b93819c |
comparison
equal
deleted
inserted
replaced
662:72174e66aba5 | 663:65a867942eee |
---|---|
1008 newNum = num | 1008 newNum = num |
1009 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) | 1009 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) |
1010 # positions | 1010 # positions |
1011 positions = Trajectory() | 1011 positions = Trajectory() |
1012 for t in newTimeInterval: | 1012 for t in newTimeInterval: |
1013 nTotal = 0. | |
1013 p = Point(0.,0.) | 1014 p = Point(0.,0.) |
1014 n = 0. | 1015 for obj in [obj1, obj2]: |
1015 if obj1.existsAtInstant(t): | 1016 if obj.existsAtInstant(t): |
1016 p += obj1.getPositionAtInstant(t) | 1017 if obj.hasFeatures(): |
1017 n += 1. | 1018 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) |
1018 if obj2.existsAtInstant(t): | 1019 else: |
1019 p += obj2.getPositionAtInstant(t) | 1020 n = 1. |
1020 n += 1. | 1021 p += obj.getPositionAtInstant(t).multiply(n) |
1021 assert n>0, 'there should be at least one point for each instant' | 1022 nTotal += n |
1022 positions.addPosition(p.divide(n)) | 1023 assert nTotal>0, 'there should be at least one point for each instant' |
1024 positions.addPosition(p.divide(nTotal)) | |
1023 # velocities: if any | 1025 # velocities: if any |
1024 if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'): | 1026 if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'): |
1025 velocities = Trajectory() | 1027 velocities = Trajectory() |
1026 for t in newTimeInterval: | 1028 for t in newTimeInterval: |
1029 nTotal = 0. | |
1027 p = Point(0.,0.) | 1030 p = Point(0.,0.) |
1028 n = 0. | 1031 for obj in [obj1, obj2]: |
1029 if obj1.existsAtInstant(t): | 1032 if obj.existsAtInstant(t): |
1030 p += obj1.getVelocityAtInstant(t) | 1033 if obj.hasFeatures(): |
1031 n += 1. | 1034 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) |
1032 if obj2.existsAtInstant(t): | 1035 else: |
1033 p += obj2.getVelocityAtInstant(t) | 1036 n = 1. |
1034 n += 1. | 1037 p += obj.getVelocityAtInstant(t).multiply(n) |
1038 nTotal += n | |
1035 assert n>0, 'there should be at least one point for each instant' | 1039 assert n>0, 'there should be at least one point for each instant' |
1036 velocities.addPosition(p.divide(n)) | 1040 velocities.addPosition(p.divide(nTotal)) |
1037 else: | 1041 else: |
1038 velocities = None | 1042 velocities = None |
1039 # TODO object envelop (polygon) | 1043 # TODO object envelop (polygon) |
1040 # user type | 1044 # user type |
1041 if obj1.getUserType() != obj2.getUserType(): | 1045 if obj1.getUserType() != obj2.getUserType(): |
1042 print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()])) | 1046 print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()])) |
1043 | 1047 |
1044 return MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType()) | 1048 newObject = MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType()) |
1049 if obj1.hasFeatures() and obj2.hasFeatures(): | |
1050 newObject.features = obj1.getFeatures()+obj2.getFeatures() | |
1051 return newObject | |
1045 | 1052 |
1046 def getObjectInTimeInterval(self, inter): | 1053 def getObjectInTimeInterval(self, inter): |
1047 '''Returns a new object extracted from self, | 1054 '''Returns a new object extracted from self, |
1048 restricted to time interval inter''' | 1055 restricted to time interval inter''' |
1049 intersection = TimeInterval.intersection(inter, self.getTimeInterval()) | 1056 intersection = TimeInterval.intersection(inter, self.getTimeInterval()) |