comparison python/moving.py @ 995:349cd5e73f79

work in progress
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 21 May 2018 22:56:58 -0400
parents 8118c6b77d7c
children add667153087
comparison
equal deleted inserted replaced
994:8118c6b77d7c 995:349cd5e73f79
1131 self.userType = userType 1131 self.userType = userType
1132 self.features = None 1132 self.features = None
1133 # compute bounding polygon from trajectory 1133 # compute bounding polygon from trajectory
1134 1134
1135 @staticmethod 1135 @staticmethod
1136 def aggregateTrajectory(features, aggFunc = np.mean):
1137 'Computes the aggregate trajectory from list of MovingObject features'
1138 return None
1139
1140 @staticmethod
1136 def generate(num, p, v, timeInterval): 1141 def generate(num, p, v, timeInterval):
1137 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) 1142 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length()))
1138 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities) 1143 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities)
1139 1144
1140 @staticmethod 1145 @staticmethod
1141 def concatenate(obj1, obj2, num = None): 1146 def concatenate(obj1, obj2, num = None, computePositions = False):
1142 '''Concatenates two objects supposed to overlap temporally ''' 1147 '''Concatenates two objects supposed to overlap temporally '''
1143 if num is None: 1148 if num is None:
1144 newNum = obj1.getNum() 1149 newNum = obj1.getNum()
1145 else: 1150 else:
1146 newNum = num 1151 newNum = num
1170 1175
1171 1176
1172 else: 1177 else:
1173 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) 1178 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval())
1174 # positions 1179 # positions
1175 positions = Trajectory() 1180 if computePositions: # TODO it would probably be better to recompute from all features, if features are available, in other method
1176 for t in newTimeInterval: 1181 positions = Trajectory()
1177 nTotal = 0.
1178 p = Point(0.,0.)
1179 for obj in [obj1, obj2]:
1180 if obj.existsAtInstant(t):
1181 if obj.hasFeatures():
1182 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)])
1183 else:
1184 n = 1.
1185 p += obj.getPositionAtInstant(t).__mul__(n)
1186 nTotal += n
1187 assert nTotal>0, 'there should be at least one point for each instant'
1188 positions.addPosition(p.divide(nTotal))
1189 # velocities: if any
1190 if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'):
1191 velocities = Trajectory()
1192 for t in newTimeInterval: 1182 for t in newTimeInterval:
1193 nTotal = 0. 1183 nTotal = 0.
1194 p = Point(0.,0.) 1184 p = Point(0.,0.)
1195 for obj in [obj1, obj2]: 1185 for obj in [obj1, obj2]:
1196 if obj.existsAtInstant(t): 1186 if obj.existsAtInstant(t):
1197 if obj.hasFeatures(): 1187 if obj.hasFeatures():
1198 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) 1188 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)])
1199 else: 1189 else:
1200 n = 1. 1190 n = 1.
1201 p += obj.getVelocityAtInstant(t).__mul__(n) 1191 p += obj.getPositionAtInstant(t).__mul__(n)
1202 nTotal += n 1192 nTotal += n
1203 assert n>0, 'there should be at least one point for each instant' 1193 assert nTotal>0, 'there should be at least one point for each instant'
1204 velocities.addPosition(p.divide(nTotal)) 1194 positions.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
1205 else: 1213 else:
1214 positions = None
1206 velocities = None 1215 velocities = None
1207 # TODO object envelop (polygon) 1216 # TODO object envelop (polygon)
1208 # user type 1217 # user type
1209 if obj1.getUserType() != obj2.getUserType(): 1218 if obj1.getUserType() != obj2.getUserType():
1210 print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()])) 1219 print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()]))
1211 1220
1212 newObject = MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType()) 1221 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
1213 if obj1.hasFeatures() and obj2.hasFeatures(): 1224 if obj1.hasFeatures() and obj2.hasFeatures():
1214 newObject.features = obj1.getFeatures()+obj2.getFeatures() 1225 newObject.features = obj1.getFeatures()+obj2.getFeatures()
1215 return newObject 1226 return newObject
1216 1227
1217 def getObjectInTimeInterval(self, inter): 1228 def getObjectInTimeInterval(self, inter):