Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 25:28e546861263
added Point class and modified trajectory accordingly
author | Nicolas Saunier <nico@confins.net> |
---|---|
date | Sat, 05 Dec 2009 12:08:25 -0500 |
parents | 5f2921ad4f7e |
children | 54d9cb0c902b |
comparison
equal
deleted
inserted
replaced
24:6fb59cfb201e | 25:28e546861263 |
---|---|
94 return self.timeInterval.first() | 94 return self.timeInterval.first() |
95 | 95 |
96 def getLastInstant(self): | 96 def getLastInstant(self): |
97 return self.timeInterval.first() | 97 return self.timeInterval.first() |
98 | 98 |
99 class Point: | |
100 def __init__(self, x, y): | |
101 self.x = x | |
102 self.y = y | |
103 | |
104 def __str__(self): | |
105 return '(%f,%f)'%(self.x,self.y) | |
106 | |
107 def __repr__(self): | |
108 return str(self) | |
109 | |
110 def __sub__(self, other): | |
111 ''' | |
112 >>> Point(3,4)-Point(1,7) | |
113 (2.000000,-3.000000) | |
114 ''' | |
115 return Point(self.x-other.x, self.y-other.y) | |
116 | |
99 class Trajectory: | 117 class Trajectory: |
100 '''Class for trajectories | 118 '''Class for trajectories |
101 i.e. a temporal sequence of positions | 119 i.e. a temporal sequence of positions |
102 | 120 |
103 the class is iterable.''' | 121 the class is iterable.''' |
104 | 122 |
105 def __init__(self, positions = None): | 123 def __init__(self, line1, line2): |
106 # self.iterInstantNum = 0 | 124 self.positions = [[float(n) for n in line1.split(' ')], |
107 self.positions = positions | 125 [float(n) for n in line2.split(' ')]] |
108 | 126 |
109 def __str__(self): | 127 def __str__(self): |
110 return ' '.join(map(utils.printPoint, self.positions[0], self.positions[1])) | 128 return ' '.join(map(utils.printPoint, self.positions[0], self.positions[1])) |
111 | 129 |
112 def __getitem__(self, i): | 130 def __getitem__(self, i): |
113 return [self.positions[0][i], self.positions[1][i]] | 131 return Point(self.positions[0][i], self.positions[1][i]) |
114 | 132 |
115 def __iter__(self): | 133 def __iter__(self): |
116 self.iterInstantNum = 0 | 134 self.iterInstantNum = 0 |
117 return self | 135 return self |
118 | 136 |
121 raise StopIteration | 139 raise StopIteration |
122 else: | 140 else: |
123 self.iterInstantNum += 1 | 141 self.iterInstantNum += 1 |
124 return self[self.iterInstantNum-1] | 142 return self[self.iterInstantNum-1] |
125 | 143 |
126 def addPosition(self, point): | 144 def addPosition(self, p): |
127 if not self.positions: | 145 if not self.positions: |
128 self.positions = [[point[0]],[point[1]]] | 146 self.positions = [[p.x],[p.y]] |
129 else: | 147 else: |
130 self.positions[0].append(point[0]) | 148 self.positions[0].append(p.x) |
131 self.positions[1].append(point[1]) | 149 self.positions[1].append(p.y) |
132 | 150 |
133 def draw(self): | 151 def draw(self, options = ''): |
134 from matplotlib.pylab import plot | 152 from matplotlib.pylab import plot |
135 plot(self.positions[0], self.positions[1]) | 153 plot(self.positions[0], self.positions[1], options) |
136 | 154 |
137 def length(self): | 155 def length(self): |
138 return len(self.positions[0]) | 156 return len(self.positions[0]) |
139 | 157 |
140 def getXCoordinates(self): | 158 def getXCoordinates(self): |
183 return self.positions.getXCoordinates() | 201 return self.positions.getXCoordinates() |
184 | 202 |
185 def getYCoordinates(self): | 203 def getYCoordinates(self): |
186 return self.positions.getYCoordinates() | 204 return self.positions.getYCoordinates() |
187 | 205 |
188 def draw(self): | 206 def draw(self, options = ''): |
189 self.positions.draw() | 207 self.positions.draw(options) |
190 | 208 |
191 def getInstantPassingLane(self, p1, p2): | 209 def getInstantPassingLane(self, p1, p2): |
192 '''Returns the instant(s) the object passes from one side of the segment to the other | 210 '''Returns the instant(s) the object passes from one side of the segment to the other |
193 None if does not''' | 211 empty list if not''' |
194 # parallel | 212 instants = [] |
195 | 213 lane = [[p1[0],p2[0]], [p1[1],p2[1]]] |
214 | |
215 # refaire tout en points, marche pas | |
216 # self.positions[i] self.positions[i+1] | |
217 | |
218 for i in xrange(self.length()-1): | |
219 p = utils.segmentIntersection([self.positions[0][i:i+1],self.positions[1][i:i+1]], lane) | |
220 if p: # interpolate the instant | |
221 if self.positions[0][i] != self.positions[0][i+1]: | |
222 ratio = (p[0]-self.positions[0][i])/(self.positions[0][i+1]-self.positions[0][i]) | |
223 elif self.positions[1][i] != self.positions[1][i+1]: | |
224 ratio = (p[1]-self.positions[1][i])/(self.positions[1][i+1]-self.positions[1][i]) | |
225 else: | |
226 ratio = 0 | |
227 instants.append(self.timeInterval[i]*(1-ratio)+ratio*self.timeInterval[i+1]) | |
228 return instants | |
196 # def computeVelocities(self): | 229 # def computeVelocities(self): |
197 | 230 |
198 # need for a class representing the indicators, their units, how to print them in graphs... | 231 # need for a class representing the indicators, their units, how to print them in graphs... |
199 class TemporalIndicator: | 232 class TemporalIndicator: |
200 '''Class for temporal indicators | 233 '''Class for temporal indicators |