Mercurial Hosting > traffic-intelligence
changeset 290:df58d361f19e
refactoring of Interval and TimeInterval using class methods (intersection, union)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 29 Jan 2013 18:23:47 -0500 |
parents | e56c34c1ebac |
children | 9f81218e497a |
files | python/moving.py python/prediction.py |
diffstat | 2 files changed, 26 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/python/moving.py Tue Jan 29 11:21:42 2013 -0500 +++ b/python/moving.py Tue Jan 29 18:23:47 2013 -0500 @@ -10,7 +10,7 @@ __metaclass__ = type -class Interval: +class Interval(object): '''Generic interval: a subset of real numbers (not iterable)''' def __init__(self, first=0, last=-1, revert = False): if revert and last<first: @@ -46,16 +46,18 @@ '''Indicates if the temporal interval of self is comprised in interval2''' return (self.first >= interval2.first) and (self.last <= interval2.last) - def union(self, interval2): + @classmethod + def union(cls, interval1, interval2): '''Smallest interval comprising self and interval2''' - return Interval(min(self.first, interval2.first), max(self.last, interval2.last)) + return cls(min(interval1.first, interval2.first), max(interval2.last, interval2.last)) - def intersection(self, interval2): + @classmethod + def intersection(cls, interval1, interval2): '''Largest interval comprised in both self and interval2''' - return Interval(max(self.first, interval2.first), min(self.last, interval2.last)) + return cls(max(interval1.first, interval2.first), min(interval1.last, interval2.last)) def distance(self, interval2): - if not self.intersection(interval2).empty(): + if not Interval.intersection(self, interval2).empty(): return 0 elif self.first > interval2.last: return self.first - interval2.last @@ -69,7 +71,7 @@ 'returns the smallest interval containing all intervals' inter = intervals[0] for i in intervals[1:]: - inter = inter.union(i) + inter = Interval.union(inter, i) return inter @@ -80,7 +82,11 @@ It may be modified directly by setting first and last''' def __init__(self, first=0, last=-1): - Interval.__init__(self, first, last, False) + super(TimeInterval, self).__init__(first, last, False) + + @staticmethod + def fromInterval(inter): + return TimeInterval(inter.first, inter.last) def __getitem__(self, i): if not self.empty(): @@ -107,7 +113,7 @@ # ''' # We will use the polygon class of Shapely -class STObject: +class STObject(object): '''Class for spatio-temporal object, i.e. with temporal and spatial existence (time interval and bounding polygon for positions (e.g. rectangle)). @@ -138,9 +144,9 @@ return self.timeInterval.contains(t) def commonTimeInterval(self, obj2): - return self.getTimeInterval().intersection(obj2.getTimeInterval()) + return TimeInterval.intersection(self.getTimeInterval(), obj2.getTimeInterval()) -class Point: +class Point(object): def __init__(self, x, y): self.x = x self.y = y @@ -235,7 +241,7 @@ from matplotlib.pyplot import scatter scatter([p.x for p in points],[p.y for p in points], c=color) -class NormAngle: +class NormAngle(object): '''Alternate encoding of a point, by its norm and orientation''' def __init__(self, norm, angle): @@ -274,7 +280,7 @@ return predictedPosition, predictedSpeedTheta -class FlowVector: +class FlowVector(object): '''Class to represent 4-D flow vectors, ie a position and a velocity''' def __init__(self, position, velocity): @@ -302,8 +308,7 @@ from numpy import matrix from numpy.linalg import linalg, det - if (Interval(p1.x,p2.x, True).intersection(Interval(p3.x,p4.x, True)).empty() - or Interval(p1.y,p2.y, True).intersection(Interval(p3.y,p4.y, True)).empty()): + if (Interval.intersection(Interval(p1.x,p2.x,True), Interval(p3.x,p4.x,True)).empty()) or (Interval.intersection(Interval(p1.y,p2.y,True), Interval(p3.y,p4.y,True)).empty()): return None else: dp1 = p2-p1#[s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]] @@ -328,7 +333,7 @@ # TODO: implement a better algorithm for intersections of sets of segments http://en.wikipedia.org/wiki/Line_segment_intersection -class Trajectory: +class Trajectory(object): '''Class for trajectories: temporal sequence of positions The class is iterable''' @@ -531,16 +536,16 @@ ''' def __init__(self, num = None, timeInterval = None, positions = None, geometry = None, userType = None): - STObject.__init__(self, num, timeInterval) + super(MovingObject, self).__init__(num, timeInterval) self.positions = positions self.geometry = geometry self.userType = userType # compute bounding polygon from trajectory - + def getObjectInTimeInterval(self, inter): '''Returns a new object extracted from self, restricted to time interval inter''' - intersection = inter.intersection(self.getTimeInterval()) + intersection = TimeInterval.intersection(inter, self.getTimeInterval()) if not intersection.empty(): trajectoryInterval = TimeInterval(intersection.first-self.getFirstInstant(), intersection.last-self.getFirstInstant()) obj = MovingObject(self.num, intersection, self.positions.getTrajectoryInInterval(trajectoryInterval), self.geometry, self.userType)
--- a/python/prediction.py Tue Jan 29 11:21:42 2013 -0500 +++ b/python/prediction.py Tue Jan 29 18:23:47 2013 -0500 @@ -204,7 +204,7 @@ t, p1, p2 = computeCollisionTime(et1, et2, collisionDistanceThreshold, timeHorizon) if t <= timeHorizon: - collisionPoints.append(SafetyPoint((p1+p2).multiply(0.5), currentInstant, et1.probability*et2.probability, t)) + collisionPoints.append(SafetyPoint((p1+p2).multiply(0.5), et1.probability*et2.probability, t)) else: # check if there is a crossing zone # TODO? zone should be around the points at which the traj are the closest # look for CZ at different times, otherwise it would be a collision @@ -249,7 +249,7 @@ commonTimeInterval = obj1.commonTimeInterval(obj2) for i in list(commonTimeInterval)[:-1]: # do not look at the 1 last position/velocities, often with errors print(obj1.num, obj2.num, i) - collisionPoints[i], crossingZones[i] = computeCrossingsCollisionsAtInstant(obj1, obj2, predictionParameters, collisionDistanceThreshold, timeHorizon, debug) + collisionPoints[i], crossingZones[i] = computeCrossingsCollisionsAtInstant(i, obj1, obj2, predictionParameters, collisionDistanceThreshold, timeHorizon, debug) return collisionPoints, crossingZones