Mercurial > hg > nsaunier > traffic-intelligence
comparison python/moving.py @ 1018:d7afc59f6966
work in progress
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 04 Jun 2018 17:46:52 -0400 |
| parents | 9b53be469a36 |
| children | 5d2f6afae35b |
comparison
equal
deleted
inserted
replaced
| 1017:89cbc4056c13 | 1018:d7afc59f6966 |
|---|---|
| 1161 newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = userType) | 1161 newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = userType) |
| 1162 newObject.features = [copy.deepcopy(f) for f in features] | 1162 newObject.features = [copy.deepcopy(f) for f in features] |
| 1163 return newObject | 1163 return newObject |
| 1164 | 1164 |
| 1165 @staticmethod | 1165 @staticmethod |
| 1166 def concatenateWith(obj1, ObjectList=[], num = None): #Concatenate with a list of MovingObjects | 1166 def concatenateWith(obj1, ObjectList=[], num = None): |
| 1167 if len(ObjectList)==0: | 1167 if len(ObjectList)==0: |
| 1168 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | 1168 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) |
| 1169 if obj1.hasFeatures() : | 1169 if obj1.hasFeatures() : |
| 1170 newObject.features = list(obj1.features) | 1170 newObject.features = list(obj1.features) |
| 1171 else: | 1171 else: |
| 1194 assert nTotal>0, 'there should be at least one point for each instant' | 1194 assert nTotal>0, 'there should be at least one point for each instant' |
| 1195 positions.addPosition(p.divide(nTotal)) | 1195 positions.addPosition(p.divide(nTotal)) |
| 1196 self.positions = positions | 1196 self.positions = positions |
| 1197 | 1197 |
| 1198 @staticmethod | 1198 @staticmethod |
| 1199 def concatenate(obj1, obj2, num = None): | 1199 def concatenate(obj1, obj2, num = None, computePositions = False): |
| 1200 '''Concatenates two objects supposed to overlap temporally ''' | 1200 '''Concatenates two objects, whether overlapping temporally or not |
| 1201 | |
| 1202 Positions will be recomputed only if computePositions is True | |
| 1203 Otherwise, only featureNumbers and/or features will be merged''' | |
| 1201 if num is None: | 1204 if num is None: |
| 1202 newNum = obj1.getNum() | 1205 newNum = obj1.getNum() |
| 1203 else: | 1206 else: |
| 1204 newNum = num | 1207 newNum = num |
| 1205 commonTimeInterval = obj1.commonTimeInterval(obj2) | 1208 commonTimeInterval = obj1.commonTimeInterval(obj2) |
| 1206 if commonTimeInterval.empty(): | 1209 if commonTimeInterval.empty(): |
| 1207 print('The two objects\' time intervals do not overlap: obj1 {} and obj2 {}'.format(obj1.getTimeInterval(), obj2.getTimeInterval())) | 1210 #print('The two objects\' time intervals do not overlap: obj1 {} and obj2 {}'.format(obj1.getTimeInterval(), obj2.getTimeInterval())) |
| 1208 emptyInterval = TimeInterval(min(obj1.getLastInstant(),obj2.getLastInstant()) , max(obj1.getFirstInstant(),obj2.getFirstInstant())) | 1211 emptyInterval = TimeInterval(min(obj1.getLastInstant(),obj2.getLastInstant()), max(obj1.getFirstInstant(),obj2.getFirstInstant())) |
| 1209 positions = Trajectory() | |
| 1210 if obj1.existsAtInstant(emptyInterval.last): | 1212 if obj1.existsAtInstant(emptyInterval.last): |
| 1211 vitessex=(obj1.getPositionAtInstant(emptyInterval.last).x-obj2.getPositionAtInstant(emptyInterval.first).x)/(emptyInterval.last-emptyInterval.first) | 1213 firstObject = obj2 |
| 1212 vitessey=(obj1.getPositionAtInstant(emptyInterval.last).y-obj2.getPositionAtInstant(emptyInterval.first).y)/(emptyInterval.last-emptyInterval.first) | 1214 secondObject = obj1 |
| 1213 px,py=obj2.getPositionAtInstant(emptyInterval.first) | 1215 else: |
| 1214 else : | 1216 firstObject = obj1 |
| 1215 vitessex=(obj2.getPositionAtInstant(emptyInterval.last).x-obj1.getPositionAtInstant(emptyInterval.first).x)/(emptyInterval.last-emptyInterval.first) | 1217 secondObject = obj2 |
| 1216 vitessey=(obj2.getPositionAtInstant(emptyInterval.last).y-obj1.getPositionAtInstant(emptyInterval.first).y)/(emptyInterval.last-emptyInterval.first) | 1218 v = (secondObject.getPositionAtInstant(emptyInterval.last)-firstObject.getPositionAtInstant(emptyInterval.first)).divide(emptyInterval.length()) |
| 1217 px,py=obj1.getPositionAtInstant(emptyInterval.first) | 1219 positions = copy.deepcopy(firstObject.getPositions()) |
| 1218 positions = Trajectory() | 1220 velocities = copy.deepcopy(firstObject.getPositions()) |
| 1219 velocities = Trajectory() | 1221 featurePositions = Trajectory() |
| 1220 for t in emptyInterval: | 1222 featureVelocities = Trajectory() |
| 1221 positions.addPositionXY(px,py) | 1223 #newFeature = MovingObject(-1, TimeInterval(emptyInterval.first+1, emptyInterval.last-1))# what feature number to choose? |
| 1222 velocities.addPositionXY(vitessex,vitessey) | 1224 p = firstObject.getPositionAtInstant(emptyInterval.first)+v |
| 1223 px+=vitessex | 1225 for t in range(emptyInterval.first+1, emptyInterval.last): |
| 1224 py+=vitessey | 1226 positions.addPosition(p) |
| 1227 velocities.addPosition(v) | |
| 1228 featurePositions.addPosition(p) | |
| 1229 featureVelocities.addPosition(v) | |
| 1230 p=p+v | |
| 1231 # continue | |
| 1225 newObject = MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType()) | 1232 newObject = MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType()) |
| 1226 newObject.features = [MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType())] #In case there is features to add when we recursively call concatenate | 1233 newObject.features = [MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType())] #In case there is features to add when we recursively call concatenate |
| 1227 return MovingObject.concatenate(MovingObject.concatenate(obj1, newObject),obj2) | 1234 return MovingObject.concatenate(MovingObject.concatenate(obj1, newObject),obj2) |
| 1228 else: | 1235 else: |
| 1229 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) | 1236 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) |
| 1258 nTotal += n | 1265 nTotal += n |
| 1259 assert nTotal>0, 'there should be at least one point for each instant' | 1266 assert nTotal>0, 'there should be at least one point for each instant' |
| 1260 velocities.addPosition(p.divide(nTotal)) | 1267 velocities.addPosition(p.divide(nTotal)) |
| 1261 else: | 1268 else: |
| 1262 velocities = None | 1269 velocities = None |
| 1263 # TODO object envelop (polygon) | 1270 # user type |
| 1264 # user type | 1271 if obj1.getUserType() != obj2.getUserType(): |
| 1265 if obj1.getUserType() != obj2.getUserType(): | 1272 print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()])) |
| 1266 print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()])) | 1273 |
| 1267 | 1274 newObject = MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType()) |
| 1268 newObject = MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType()) | 1275 if hasattr(obj1, 'featureNumbers') and hasattr(obj2, 'featureNumbers'): |
| 1269 if obj1.hasFeatures() and obj2.hasFeatures(): | 1276 newObject.featureNumbers = obj1.featureNumbers+obj2.featureNumbers |
| 1270 newObject.features = obj1.getFeatures()+obj2.getFeatures() | 1277 if obj1.hasFeatures() and obj2.hasFeatures(): |
| 1271 return newObject | 1278 newObject.features = obj1.getFeatures()+obj2.getFeatures() |
| 1279 return newObject | |
| 1272 | 1280 |
| 1273 def getObjectInTimeInterval(self, inter): | 1281 def getObjectInTimeInterval(self, inter): |
| 1274 '''Returns a new object extracted from self, | 1282 '''Returns a new object extracted from self, |
| 1275 restricted to time interval inter''' | 1283 restricted to time interval inter''' |
| 1276 intersection = TimeInterval.intersection(inter, self.getTimeInterval()) | 1284 intersection = TimeInterval.intersection(inter, self.getTimeInterval()) |
