Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 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 |
comparison
equal
deleted
inserted
replaced
996:add667153087 | 1010:c90c4682c67e |
---|---|
1141 def generate(num, p, v, timeInterval): | 1141 def generate(num, p, v, timeInterval): |
1142 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) | 1142 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) |
1143 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities) | 1143 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities) |
1144 | 1144 |
1145 @staticmethod | 1145 @staticmethod |
1146 def concatenate(obj1, obj2, num = None, computePositions = False): | 1146 def concatenateWith(obj1, ObjectList=[], num = None): #Concatenate with a list of MovingObjects |
1147 if len(ObjectList)==0: | |
1148 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | |
1149 if obj1.hasFeatures() : | |
1150 newObject.features = list(obj1.features) | |
1151 else: | |
1152 def getKey(item): | |
1153 return 0 if not obj1.commonTimeInterval(item).empty() else max(obj1.getFirstInstant(),item.getFirstInstant())-min(obj1.getLastInstant(),item.getLastInstant()) | |
1154 Order = min(ObjectList , key=getKey) | |
1155 newObject = MovingObject.concatenate(obj1, Order, num) | |
1156 ObjectList.remove(Order) | |
1157 newObject = MovingObject.concatenateWith(newObject,ObjectList) | |
1158 return newObject | |
1159 | |
1160 @staticmethod | |
1161 def addFeatures(obj1, FeatureList=[], num = None): #Add features to an object | |
1162 if len(FeatureList)==0: #We return a clone | |
1163 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | |
1164 if obj1.features is not None: | |
1165 newObject.features = list(obj1.features) | |
1166 else : | |
1167 newObjectTMP = MovingObject.concatenateWith(obj1,FeatureList,num) | |
1168 newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = newObjectTMP.getUserType()) | |
1169 if obj1.hasFeatures() and newObjectTMP.hasFeatures(): | |
1170 newObject.features = obj1.features + newObjectTMP.features | |
1171 | |
1172 return newObject | |
1173 | |
1174 | |
1175 @staticmethod | |
1176 def delFeatures(obj1, FeatureList=[], num = None): | |
1177 if obj1.features is not None: | |
1178 if len(FeatureList)>0: | |
1179 tmp = [ i for i in obj1.features and i not in FeatureList] | |
1180 if len(obj1.features)!=len(tmp): | |
1181 newInterval = TimeInterval(min([i.getFirstInstant() for i in tmp]), max([i.getLastInstant() for i in tmp])) | |
1182 positions = Trajectory() | |
1183 for t in newInterval: | |
1184 nTotal = 0. | |
1185 p = Point(0.,0.) | |
1186 for obj in tmp: | |
1187 if obj.existsAtInstant(t): | |
1188 if obj.hasFeatures(): | |
1189 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | |
1190 else: | |
1191 n = 1. | |
1192 p += obj.getPositionAtInstant(t).__mul__(n) | |
1193 nTotal += n | |
1194 if nTotal==0: | |
1195 positions.addPosition(obj1.getPositionAtInstant(t)) | |
1196 else: | |
1197 positions.addPosition(p.divide(nTotal)) | |
1198 if hasattr(obj1, 'velocities'): | |
1199 velocities = Trajectory() | |
1200 for t in newTimeInterval: | |
1201 nTotal = 0. | |
1202 p = Point(0.,0.) | |
1203 for obj in tmp: | |
1204 if obj.existsAtInstant(t): | |
1205 if obj.hasFeatures(): | |
1206 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | |
1207 else: | |
1208 n = 1. | |
1209 p += obj.getVelocityAtInstant(t).__mul__(n) | |
1210 nTotal += n | |
1211 if nTotal==0: | |
1212 velocities.addPosition(obj1.getVelocityAtInstant(t)) | |
1213 else: | |
1214 velocities.addPosition(p.divide(nTotal)) | |
1215 else: | |
1216 velocities = None | |
1217 | |
1218 newObject = MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType()) | |
1219 newObject.features = tmp+[MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType())] | |
1220 else: | |
1221 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | |
1222 return newObject | |
1223 | |
1224 | |
1225 def updatePosition(self): | |
1226 if self.features is not None: | |
1227 print 'On update ici' | |
1228 positions = Trajectory() | |
1229 for t in self.getTimeInterval(): | |
1230 nTotal = 0. | |
1231 p = Point(0.,0.) | |
1232 for obj in self.features: | |
1233 if obj.existsAtInstant(t): | |
1234 if obj.hasFeatures(): | |
1235 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | |
1236 else: | |
1237 n = 1. | |
1238 p += obj.getPositionAtInstant(t).__mul__(n) | |
1239 nTotal += n | |
1240 assert nTotal>0, 'there should be at least one point for each instant' | |
1241 positions.addPosition(p.divide(nTotal)) | |
1242 self.positions = positions | |
1243 | |
1244 @staticmethod | |
1245 def concatenate(obj1, obj2, num = None): | |
1147 '''Concatenates two objects supposed to overlap temporally ''' | 1246 '''Concatenates two objects supposed to overlap temporally ''' |
1148 if num is None: | 1247 if num is None: |
1149 newNum = obj1.getNum() | 1248 newNum = obj1.getNum() |
1150 else: | 1249 else: |
1151 newNum = num | 1250 newNum = num |
1169 velocities.addPositionXY(vitessex,vitessey) | 1268 velocities.addPositionXY(vitessex,vitessey) |
1170 px+=vitessex | 1269 px+=vitessex |
1171 py+=vitessey | 1270 py+=vitessey |
1172 | 1271 |
1173 newObject = MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType()) | 1272 newObject = MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType()) |
1273 newObject.features = [MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType())] #In case there is features to add when we recursively call concatenate | |
1174 return MovingObject.concatenate(MovingObject.concatenate(obj1, newObject),obj2) | 1274 return MovingObject.concatenate(MovingObject.concatenate(obj1, newObject),obj2) |
1175 | 1275 |
1176 | 1276 |
1177 else: | 1277 else: |
1178 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) | 1278 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) |
1179 # positions | 1279 # positions |
1180 if computePositions: # TODO it would probably be better to recompute from all features, if features are available, in other method | 1280 positions = Trajectory() |
1181 positions = Trajectory() | 1281 for t in newTimeInterval: |
1282 nTotal = 0. | |
1283 p = Point(0.,0.) | |
1284 for obj in [obj1, obj2]: | |
1285 if obj.existsAtInstant(t): | |
1286 if obj.hasFeatures(): | |
1287 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | |
1288 else: | |
1289 n = 1. | |
1290 p += obj.getPositionAtInstant(t).__mul__(n) | |
1291 nTotal += n | |
1292 assert nTotal>0, 'there should be at least one point for each instant' | |
1293 positions.addPosition(p.divide(nTotal)) | |
1294 # velocities: if any | |
1295 if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'): | |
1296 velocities = Trajectory() | |
1182 for t in newTimeInterval: | 1297 for t in newTimeInterval: |
1183 nTotal = 0. | 1298 nTotal = 0. |
1184 p = Point(0.,0.) | 1299 p = Point(0.,0.) |
1185 for obj in [obj1, obj2]: | 1300 for obj in [obj1, obj2]: |
1186 if obj.existsAtInstant(t): | 1301 if obj.existsAtInstant(t): |
1187 if obj.hasFeatures(): | 1302 if obj.hasFeatures(): |
1188 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | 1303 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) |
1189 else: | 1304 else: |
1190 n = 1. | 1305 n = 1. |
1191 p += obj.getPositionAtInstant(t).__mul__(n) | 1306 p += obj.getVelocityAtInstant(t).__mul__(n) |
1192 nTotal += n | 1307 nTotal += n |
1193 assert nTotal>0, 'there should be at least one point for each instant' | 1308 assert nTotal>0, 'there should be at least one point for each instant' |
1194 positions.addPosition(p.divide(nTotal)) | 1309 velocities.addPosition(p.divide(nTotal)) |
1195 # velocities: if any | |
1196 if obj1.getVelocities() is not None and obj2.getVelocities() is not None: | |
1197 velocities = Trajectory() | |
1198 for t in newTimeInterval: | |
1199 nTotal = 0. | |
1200 p = Point(0.,0.) | |
1201 for obj in [obj1, obj2]: | |
1202 if obj.existsAtInstant(t): | |
1203 if obj.hasFeatures(): | |
1204 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | |
1205 else: | |
1206 n = 1. | |
1207 p += obj.getVelocityAtInstant(t).__mul__(n) | |
1208 nTotal += n | |
1209 assert n>0, 'there should be at least one point for each instant' | |
1210 velocities.addPosition(p.divide(nTotal)) | |
1211 else: | |
1212 velocities = None | |
1213 else: | 1310 else: |
1214 positions = None | |
1215 velocities = None | 1311 velocities = None |
1216 # TODO object envelop (polygon) | 1312 # TODO object envelop (polygon) |
1217 # user type | 1313 # user type |
1218 if obj1.getUserType() != obj2.getUserType(): | 1314 if obj1.getUserType() != obj2.getUserType(): |
1219 print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()])) | 1315 print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()])) |
1220 | 1316 |
1221 newObject = MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType()) | 1317 newObject = MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType()) |
1222 if hasattr(obj1, 'featureNumbers') and hasattr(obj2, 'featureNumbers'): | |
1223 newObject.featureNumbers = obj1.featureNumbers+obj2.featureNumbers | |
1224 if obj1.hasFeatures() and obj2.hasFeatures(): | 1318 if obj1.hasFeatures() and obj2.hasFeatures(): |
1225 newObject.features = obj1.getFeatures()+obj2.getFeatures() | 1319 newObject.features = obj1.getFeatures()+obj2.getFeatures() |
1226 return newObject | 1320 return newObject |
1227 | 1321 |
1228 def getObjectInTimeInterval(self, inter): | 1322 def getObjectInTimeInterval(self, inter): |