comparison python/moving.py @ 83:41da2cdcd91c

re-arranged trajectory intersections
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 18 Mar 2011 15:06:16 -0400
parents 5d487f183fe2
children f88a19695bba
comparison
equal deleted inserted replaced
82:76735aeab807 83:41da2cdcd91c
3 3
4 import utils; 4 import utils;
5 5
6 from math import sqrt, hypot; 6 from math import sqrt, hypot;
7 7
8 from shapely.geometry import Polygon 8 #from shapely.geometry import Polygon
9 9
10 __metaclass__ = type 10 __metaclass__ = type
11 11
12 #class MovingObject: 12 #class MovingObject:
13 13
272 return displacement 272 return displacement
273 273
274 def wiggliness(self): 274 def wiggliness(self):
275 return self.cumulatedDisplacement()/float(Point.distanceNorm2(self.__getitem__(0),self.__getitem__(self.length()-1))) 275 return self.cumulatedDisplacement()/float(Point.distanceNorm2(self.__getitem__(0),self.__getitem__(self.length()-1)))
276 276
277 def getIntersections(self, p1, p2):
278 '''Returns a list of the indices at which the trajectory
279 intersects with the segment of extremities p1 and p2 the list is empty if there is no crossing'''
280 indices = []
281
282 for i in xrange(self.length()-1):
283 q1=self.__getitem__(i)
284 q2=self.__getitem__(i+1)
285 p = utils.segmentIntersection(q1, q2, p1, p2)
286 if p:
287 if q1.x != q2.x:
288 ratio = (p.x-q1.x)/(q2.x-q1.x)
289 elif q1.y != q2.y:
290 ratio = (p.y-q1.y)/(q2.y-q1.y)
291 else:
292 ratio = 0
293 indices.append(i+ratio)
294 return indices
295
277 def getTrajectoryInInterval(self, inter): 296 def getTrajectoryInInterval(self, inter):
278 if inter.first >=0 and inter.last<= self.length(): 297 if inter.first >=0 and inter.last<= self.length():
279 return Trajectory([self.positions[0][inter.first:inter.last], 298 return Trajectory([self.positions[0][inter.first:inter.last],
280 self.positions[1][inter.first:inter.last]]) 299 self.positions[1][inter.first:inter.last]])
281 else: 300 else:
362 return self.positions.getYCoordinates() 381 return self.positions.getYCoordinates()
363 382
364 def draw(self, options = ''): 383 def draw(self, options = ''):
365 self.positions.draw(options) 384 self.positions.draw(options)
366 385
367 def getInstantPassingLane(self, p1, p2): 386 def getInstantsCrossingLane(self, p1, p2):
368 '''Returns the instant(s) 387 '''Returns the instant(s)
369 at which the object passes from one side of the segment to the other 388 at which the object passes from one side of the segment to the other
370 empty list if there is no crossing''' 389 empty list if there is no crossing'''
371 instants = [] 390 indices = self.positions.getIntersections(p1, p2)
372 391 return [t+self.getFirstInstant() for t in indices]
373 for i in xrange(self.length()-1):
374 p = utils.segmentIntersection(self.positions[i], self.positions[i+1], p1, p2)
375 if p:
376 if self.positions[i].x != self.positions[i+1].x:
377 ratio = (p.x-self.positions[i].x)/(self.positions[i+1].x-self.positions[i].x)
378 elif self.positions[i].y != self.positions[i+1].y:
379 ratio = (p.y-self.positions[i].y)/(self.positions[i+1].y-self.positions[i].y)
380 else:
381 ratio = 0
382 instants.append(self.timeInterval[i]*(1-ratio)+ratio*self.timeInterval[i+1])
383 return instants
384 392
385 @staticmethod 393 @staticmethod
386 def collisionCourseDotProduct(movingObject1, movingObject2, instant): 394 def collisionCourseDotProduct(movingObject1, movingObject2, instant):
387 'A positive result indicates that the road users are getting closer' 395 'A positive result indicates that the road users are getting closer'
388 deltap = movingObject1.getPositionAtInstant(instant)-movingObject2.getPositionAtInstant(instant) 396 deltap = movingObject1.getPositionAtInstant(instant)-movingObject2.getPositionAtInstant(instant)