Mercurial > hg > nsaunier > traffic-intelligence
comparison python/moving.py @ 623:ce7133cbcdf3
implemented function to concatenate objects
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 23 Dec 2014 14:56:13 -0500 |
| parents | dc8490726d06 |
| children | bac66bd536c5 |
comparison
equal
deleted
inserted
replaced
| 622:dc8490726d06 | 623:ce7133cbcdf3 |
|---|---|
| 192 return Point(self.y, -self.x) | 192 return Point(self.y, -self.x) |
| 193 | 193 |
| 194 def multiply(self, alpha): | 194 def multiply(self, alpha): |
| 195 'Warning, returns a new Point' | 195 'Warning, returns a new Point' |
| 196 return Point(self.x*alpha, self.y*alpha) | 196 return Point(self.x*alpha, self.y*alpha) |
| 197 | |
| 198 def divide(self, alpha): | |
| 199 'Warning, returns a new Point' | |
| 200 return Point(self.x/alpha, self.y/alpha) | |
| 197 | 201 |
| 198 def plot(self, options = 'o', **kwargs): | 202 def plot(self, options = 'o', **kwargs): |
| 199 from matplotlib.pylab import plot | 203 from matplotlib.pylab import plot |
| 200 plot([self.x], [self.y], options, **kwargs) | 204 plot([self.x], [self.y], options, **kwargs) |
| 201 | 205 |
| 946 def generate(p, v, timeInterval): | 950 def generate(p, v, timeInterval): |
| 947 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) | 951 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) |
| 948 return MovingObject(timeInterval = timeInterval, positions = positions, velocities = velocities) | 952 return MovingObject(timeInterval = timeInterval, positions = positions, velocities = velocities) |
| 949 | 953 |
| 950 @staticmethod | 954 @staticmethod |
| 951 def concatenate(obj1, obj2): | 955 def concatenate(obj1, obj2, num = None): |
| 952 ''' ''' | 956 '''Concatenates two objects supposed to overlap temporally ''' |
| 953 pass | 957 commonTimeInterval = obj1.commonTimeInterval(obj2) |
| 958 if commonTimeInterval.empty(): | |
| 959 print('The two objects\' time intervals do not overlap: obj1 {} and obj2 {}'.format(obj1.getTimeInterval(), obj2.getTimeInterval())) | |
| 960 return None | |
| 961 else: | |
| 962 if num == None: | |
| 963 newNum = obj1.getNum() | |
| 964 else: | |
| 965 newNum = num | |
| 966 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) | |
| 967 # positions | |
| 968 positions = Trajectory() | |
| 969 for t in newTimeInterval: | |
| 970 p = Point(0.,0.) | |
| 971 n = 0. | |
| 972 if obj1.existsAtInstant(t): | |
| 973 p += obj1.getPositionAtInstant(t) | |
| 974 n += 1. | |
| 975 if obj2.existsAtInstant(t): | |
| 976 p += obj2.getPositionAtInstant(t) | |
| 977 n += 1. | |
| 978 assert n>0, 'there should be at least one point for each instant' | |
| 979 positions.addPosition(p.divide(n)) | |
| 980 # velocities: if any | |
| 981 if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'): | |
| 982 velocities = Trajectory() | |
| 983 for t in newTimeInterval: | |
| 984 p = Point(0.,0.) | |
| 985 n = 0. | |
| 986 if obj1.existsAtInstant(t): | |
| 987 p += obj1.getVelocityAtInstant(t) | |
| 988 n += 1. | |
| 989 if obj2.existsAtInstant(t): | |
| 990 p += obj2.getVelocityAtInstant(t) | |
| 991 n += 1. | |
| 992 assert n>0, 'there should be at least one point for each instant' | |
| 993 velocities.addPosition(p.divide(n)) | |
| 994 else: | |
| 995 velocities = None | |
| 996 # TODO object envelop (polygon) | |
| 997 # user type | |
| 998 if obj1.getUserType() != obj2.getUserType(): | |
| 999 print('The two moving objects have different user types: obj1 {} obj2 {}'.format(userTypeNames[obj1.getUserType()], userTypeNames[obj2.getUserType()])) | |
| 1000 | |
| 1001 return MovingObject(newNum, newTimeInterval, positions, velocities, userType = obj1.getUserType()) | |
| 954 | 1002 |
| 955 def getObjectInTimeInterval(self, inter): | 1003 def getObjectInTimeInterval(self, inter): |
| 956 '''Returns a new object extracted from self, | 1004 '''Returns a new object extracted from self, |
| 957 restricted to time interval inter''' | 1005 restricted to time interval inter''' |
| 958 intersection = TimeInterval.intersection(inter, self.getTimeInterval()) | 1006 intersection = TimeInterval.intersection(inter, self.getTimeInterval()) |
