comparison python/moving.py @ 231:249d65ff6c35

merged modifications for windows
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 02 Jul 2012 23:49:39 -0400
parents c31722fcc9de
children e0988a8ace0c
comparison
equal deleted inserted replaced
230:bc4ea09b1743 231:249d65ff6c35
101 self.boundingPolygon = boundingPolygon 101 self.boundingPolygon = boundingPolygon
102 102
103 def empty(self): 103 def empty(self):
104 return self.timeInterval.empty() or not self.boudingPolygon 104 return self.timeInterval.empty() or not self.boudingPolygon
105 105
106 def getId(self):
107 return self.num
108
106 def getFirstInstant(self): 109 def getFirstInstant(self):
107 return self.timeInterval.first 110 return self.timeInterval.first
108 111
109 def getLastInstant(self): 112 def getLastInstant(self):
110 return self.timeInterval.last 113 return self.timeInterval.last
136 return Point(self.x-other.x, self.y-other.y) 139 return Point(self.x-other.x, self.y-other.y)
137 140
138 def multiply(self, alpha): 141 def multiply(self, alpha):
139 return Point(self.x*alpha, self.y*alpha) 142 return Point(self.x*alpha, self.y*alpha)
140 143
141 def draw(self, options = 'o'): 144 def draw(self, options = 'o', **kwargs):
142 from matplotlib.pylab import plot 145 from matplotlib.pylab import plot
143 plot([self.x], [self.y], options) 146 plot([self.x], [self.y], options, **kwargs)
144 147
145 def norm2Squared(self): 148 def norm2Squared(self):
146 '''2-norm distance (Euclidean distance)''' 149 '''2-norm distance (Euclidean distance)'''
147 return self.x*self.x+self.y*self.y 150 return self.x*self.x+self.y*self.y
148 151
153 def aslist(self): 156 def aslist(self):
154 return [self.x, self.y] 157 return [self.x, self.y]
155 158
156 def astuple(self): 159 def astuple(self):
157 return (self.x, self.y) 160 return (self.x, self.y)
161
162 def asint(self):
163 return Point(int(self.x), int(self.y))
158 164
159 def project(self, homography): 165 def project(self, homography):
160 from numpy.core.multiarray import array 166 from numpy.core.multiarray import array
161 projected = cvutils.projectArray(homography, array([[self.x], [self.y]])) 167 projected = cvutils.projectArray(homography, array([[self.x], [self.y]]))
162 return Point(projected[0], projected[1]) 168 return Point(projected[0], projected[1])
217 return FlowVector(self.position+other.position, self.velocity+other.velocity) 223 return FlowVector(self.position+other.position, self.velocity+other.velocity)
218 224
219 def multiply(self, alpha): 225 def multiply(self, alpha):
220 return FlowVector(self.position.multiply(alpha), self.velocity.multiply(alpha)) 226 return FlowVector(self.position.multiply(alpha), self.velocity.multiply(alpha))
221 227
222 def draw(self, options = ''): 228 def draw(self, options = '', **kwargs):
223 from matplotlib.pylab import plot 229 from matplotlib.pylab import plot
224 plot([self.position.x, self.position.x+self.velocity.x], [self.position.y, self.position.y+self.velocity.y], options) 230 plot([self.position.x, self.position.x+self.velocity.x], [self.position.y, self.position.y+self.velocity.y], options, **kwargs)
225 self.position.draw(options+'x') 231 self.position.draw(options+'x', **kwargs)
226 232
227 @staticmethod 233 @staticmethod
228 def similar(f1, f2, maxDistance2, maxDeltavelocity2): 234 def similar(f1, f2, maxDistance2, maxDeltavelocity2):
229 return (f1.position-f2.position).norm2Squared()<maxDistance2 and (f1.velocity-f2.velocity).norm2Squared()<maxDeltavelocity2 235 return (f1.position-f2.position).norm2Squared()<maxDistance2 and (f1.velocity-f2.velocity).norm2Squared()<maxDeltavelocity2
230 236
299 305
300 def addPosition(self, p): 306 def addPosition(self, p):
301 self.addPositionXY(p.x, p.y) 307 self.addPositionXY(p.x, p.y)
302 308
303 @staticmethod 309 @staticmethod
304 def _draw(positions, options = '', withOrigin = False, lastCoordinate = None): 310 def _draw(positions, options = '', withOrigin = False, lastCoordinate = None, **kwargs):
305 from matplotlib.pylab import plot 311 from matplotlib.pylab import plot
306 if lastCoordinate == None: 312 if lastCoordinate == None:
307 plot(positions[0], positions[1], options) 313 plot(positions[0], positions[1], options, **kwargs)
308 elif 0 <= lastCoordinate <= len(positions[0]): 314 elif 0 <= lastCoordinate <= len(positions[0]):
309 plot(positions[0][:lastCoordinate], positions[1][:lastCoordinate], options) 315 plot(positions[0][:lastCoordinate], positions[1][:lastCoordinate], options, **kwargs)
310 if withOrigin: 316 if withOrigin:
311 plot([positions[0][0]], [positions[1][0]], 'ro') 317 plot([positions[0][0]], [positions[1][0]], 'ro', **kwargs)
312 318
313 def project(self, homography): 319 def project(self, homography):
314 from numpy.core.multiarray import array 320 from numpy.core.multiarray import array
315 projected = cvutils.projectArray(homography, array(self.positions)) 321 projected = cvutils.projectArray(homography, array(self.positions))
316 return Trajectory(projected) 322 return Trajectory(projected)
317 323
318 def draw(self, options = '', withOrigin = False): 324 def draw(self, options = '', withOrigin = False, **kwargs):
319 Trajectory._draw(self.positions, options, withOrigin) 325 Trajectory._draw(self.positions, options, withOrigin,**kwargs)
320 326
321 def drawAt(self, lastCoordinate, options = '', withOrigin = False): 327 def drawAt(self, lastCoordinate, options = '', withOrigin = False, **kwargs):
322 Trajectory._draw(self.positions, options, withOrigin, lastCoordinate) 328 Trajectory._draw(self.positions, options, withOrigin, lastCoordinate, **kwargs)
323 329
324 def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False): 330 def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, **kwargs):
325 from matplotlib.pylab import plot 331 from matplotlib.pylab import plot
326 imgPositions = [[x*nPixelsPerUnitDistance for x in self.positions[0]], 332 imgPositions = [[x*nPixelsPerUnitDistance for x in self.positions[0]],
327 [-x*nPixelsPerUnitDistance+imageHeight for x in self.positions[1]]] 333 [-x*nPixelsPerUnitDistance+imageHeight for x in self.positions[1]]]
328 Trajectory._draw(imgPositions, options, withOrigin) 334 Trajectory._draw(imgPositions, options, withOrigin, **kwargs)
329 335
330 def getXCoordinates(self): 336 def getXCoordinates(self):
331 return self.positions[0] 337 return self.positions[0]
332 338
333 def getYCoordinates(self): 339 def getYCoordinates(self):
487 return self.positions.getXCoordinates() 493 return self.positions.getXCoordinates()
488 494
489 def getYCoordinates(self): 495 def getYCoordinates(self):
490 return self.positions.getYCoordinates() 496 return self.positions.getYCoordinates()
491 497
492 def draw(self, options = '', withOrigin = False): 498 def draw(self, options = '', withOrigin = False, **kwargs):
493 self.positions.draw(options, withOrigin) 499 self.positions.draw(options, withOrigin, **kwargs)
494 500
495 def drawWorldOnImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False): 501 def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, **kwargs):
496 self.positions.drawWorldOnImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin) 502 self.positions.drawOnWorldImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin, **kwargs)
497 503
498 def getInstantsCrossingLane(self, p1, p2): 504 def getInstantsCrossingLane(self, p1, p2):
499 '''Returns the instant(s) 505 '''Returns the instant(s)
500 at which the object passes from one side of the segment to the other 506 at which the object passes from one side of the segment to the other
501 empty list if there is no crossing''' 507 empty list if there is no crossing'''
619 if len(indices) >= minNInstants: 625 if len(indices) >= minNInstants:
620 values = sorted(values[indices], reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values 626 values = sorted(values[indices], reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values
621 return mean(values[:minNInstants]) 627 return mean(values[:minNInstants])
622 else: 628 else:
623 return None 629 return None
630
631 # functions to aggregate discretized maps of indicators
632 # TODO add values in the cells between the positions (similar to discretizing vector graphics to bitmap)
624 633
625 def indicatorMap(indicatorValues, trajectory, squareSize): 634 def indicatorMap(indicatorValues, trajectory, squareSize):
626 '''Returns a dictionary 635 '''Returns a dictionary
627 with keys for the indices of the cells (squares) 636 with keys for the indices of the cells (squares)
628 in which the trajectory positions are located 637 in which the trajectory positions are located