Mercurial > hg > nsaunier > traffic-intelligence
comparison python/moving.py @ 67:ded58c424783
added indicator computation and modified severity indicator constructor
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Sun, 07 Nov 2010 04:21:26 -0500 |
| parents | 56fe4ef1377e |
| children | 846fa9dc47de |
comparison
equal
deleted
inserted
replaced
| 66:56fe4ef1377e | 67:ded58c424783 |
|---|---|
| 64 def __iter__(self): | 64 def __iter__(self): |
| 65 self.iterInstantNum = 0 | 65 self.iterInstantNum = 0 |
| 66 return self | 66 return self |
| 67 | 67 |
| 68 def next(self): | 68 def next(self): |
| 69 if self.iterInstantNum >= self.length(): | 69 if self.iterInstantNum >= self.length()-1: |
| 70 raise StopIteration | 70 raise StopIteration |
| 71 else: | 71 else: |
| 72 self.iterInstantNum += 1 | 72 self.iterInstantNum += 1 |
| 73 return self[self.iterInstantNum] | 73 return self[self.iterInstantNum] |
| 74 | 74 |
| 135 '2-norm distance (Euclidean distance)' | 135 '2-norm distance (Euclidean distance)' |
| 136 return sqrt(self.norm2Squared()) | 136 return sqrt(self.norm2Squared()) |
| 137 | 137 |
| 138 def aslist(self): | 138 def aslist(self): |
| 139 return [self.x, self.y] | 139 return [self.x, self.y] |
| 140 | |
| 141 @staticmethod | |
| 142 def dot(p1, p2): | |
| 143 return p1.x*p2.x+p1.y*p2.y | |
| 140 | 144 |
| 141 @staticmethod | 145 @staticmethod |
| 142 def distanceNorm2(p1, p2): | 146 def distanceNorm2(p1, p2): |
| 143 return (p1-p2).norm2() | 147 return (p1-p2).norm2() |
| 144 | 148 |
| 304 def getPositionAt(self, i): | 308 def getPositionAt(self, i): |
| 305 return self.positions[i] | 309 return self.positions[i] |
| 306 | 310 |
| 307 def getVelocityAt(self, i): | 311 def getVelocityAt(self, i): |
| 308 return self.velocities[i] | 312 return self.velocities[i] |
| 313 | |
| 314 def getPositionAtInstant(self, i): | |
| 315 return self.positions[i-self.getFirstInstant()] | |
| 316 | |
| 317 def getVelocityAtInstant(self, i): | |
| 318 return self.velocities[i-self.getFirstInstant()] | |
| 309 | 319 |
| 310 def getXCoordinates(self): | 320 def getXCoordinates(self): |
| 311 return self.positions.getXCoordinates() | 321 return self.positions.getXCoordinates() |
| 312 | 322 |
| 313 def getYCoordinates(self): | 323 def getYCoordinates(self): |
| 332 else: | 342 else: |
| 333 ratio = 0 | 343 ratio = 0 |
| 334 instants.append(self.timeInterval[i]*(1-ratio)+ratio*self.timeInterval[i+1]) | 344 instants.append(self.timeInterval[i]*(1-ratio)+ratio*self.timeInterval[i+1]) |
| 335 return instants | 345 return instants |
| 336 | 346 |
| 337 # def computeVelocities(self): | 347 @staticmethod |
| 348 def collisionCourseDotProduct(movingObject1, movingObject2, instant): | |
| 349 'A positive result indicates that the road users are getting closer' | |
| 350 deltap = movingObject1.getPositionAtInstant(instant)-movingObject2.getPositionAtInstant(instant) | |
| 351 deltav = movingObject2.getVelocityAtInstant(instant)-movingObject1.getVelocityAtInstant(instant) | |
| 352 return moving.Point.dot(deltap, deltav) | |
| 353 | |
| 354 @staticmethod | |
| 355 def collisionCourseCosine(movingObject1, movingObject2, instant): | |
| 356 'A positive result indicates that the road users are getting closer' | |
| 357 deltap = movingObject1.getPositionAtInstant(instant)-movingObject2.getPositionAtInstant(instant) | |
| 358 deltav = movingObject2.getVelocityAtInstant(instant)-movingObject1.getVelocityAtInstant(instant) | |
| 359 return moving.Point.dot(deltap, deltav)/(deltap.norm2()*deltav.norm2()) | |
| 338 | 360 |
| 339 def plotRoadUsers(objects, colors): | 361 def plotRoadUsers(objects, colors): |
| 340 '''Colors is a PlottingPropertyValues instance''' | 362 '''Colors is a PlottingPropertyValues instance''' |
| 341 from matplotlib.pyplot import figure, axis | 363 from matplotlib.pyplot import figure, axis |
| 342 figure() | 364 figure() |
| 364 class SeverityIndicator(TemporalIndicator): | 386 class SeverityIndicator(TemporalIndicator): |
| 365 '''Class for severity indicators | 387 '''Class for severity indicators |
| 366 field mostSevereIsMax is True | 388 field mostSevereIsMax is True |
| 367 if the most severe value taken by the indicator is the maximum''' | 389 if the most severe value taken by the indicator is the maximum''' |
| 368 | 390 |
| 369 def __init__(self, name, values, mostSevereIsMax=True, ignoredValue = None): | 391 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, ignoredValue = None): |
| 370 # , timeInterval=None # implement later | 392 TemporalIndicator.__init__(self, name, values, timeInterval) |
| 371 TemporalIndicator.__init__(self, name, values, timeInterval=None) | |
| 372 self.mostSevereIsMax = mostSevereIsMax | 393 self.mostSevereIsMax = mostSevereIsMax |
| 373 self.ignoredValue = ignoredValue | 394 self.ignoredValue = ignoredValue |
| 374 | 395 |
| 375 def getMostSevereValue(self, minNInstants=1): | 396 def getMostSevereValue(self, minNInstants=1): |
| 376 from matplotlib.mlab import find | 397 from matplotlib.mlab import find |
