comparison python/moving.py @ 16:9d6831cfe675

added tools for speed
author Nicolas Saunier <nico@confins.net>
date Wed, 18 Nov 2009 18:36:52 -0500
parents e7bbe8465591
children 5a21d2cfee44
comparison
equal deleted inserted replaced
14:e7bbe8465591 16:9d6831cfe675
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 '''Libraries for moving objects, trajectories...''' 2 '''Libraries for moving objects, trajectories...'''
3 3
4 import utils; 4 import utils;
5
6 from math import sqrt, hypot;
5 7
6 from shapely.geometry import Polygon 8 from shapely.geometry import Polygon
7 9
8 __metaclass__ = type 10 __metaclass__ = type
9 11
88 i.e. a temporal sequence of positions''' 90 i.e. a temporal sequence of positions'''
89 91
90 def __init__(self, positions = None): 92 def __init__(self, positions = None):
91 self.positions = positions 93 self.positions = positions
92 94
95 def __str__(self):
96 return ' '.join(map(utils.printPoint, self.positions[0], self.positions[1]))
97
93 def addPosition(self, point): 98 def addPosition(self, point):
94 if not self.positions: 99 if not self.positions:
95 self.positions = [[point[0]],[point[1]]] 100 self.positions = [[point[0]],[point[1]]]
96 else: 101 else:
97 self.positions[0].append(point[0]) 102 self.positions[0].append(point[0])
106 return [min(self.positions[0]), max(self.positions[0])] 111 return [min(self.positions[0]), max(self.positions[0])]
107 112
108 def yBounds(self): 113 def yBounds(self):
109 # look for function that does min and max in one pass 114 # look for function that does min and max in one pass
110 return [min(self.positions[1]), max(self.positions[1])] 115 return [min(self.positions[1]), max(self.positions[1])]
111 116
117 def norm(self):
118 '''Returns the list of the norms at each instant'''
119 # def add(x, y): return x+y
120 # sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]])
121 # return sqrt(sq)
122 return [hypot(x,y) for x,y in zip(self.positions[0], self.positions[1])]
112 123
113 class MovingObject(STObject): 124 class MovingObject(STObject):
114 '''Class for moving objects 125 '''Class for moving objects
115 i.e. with a trajectory and a geometry (volume) 126 i.e. with a trajectory and a geometry (volume)
116 and a type (e.g. road user) 127 and a type (e.g. road user)
117 ''' 128 '''
118 129
119 def __init__(self, num = None, timeInterval = None, trajectory = None, geometry = None, type = None): 130 def __init__(self, num = None, timeInterval = None, positions = None, geometry = None, type = None):
120 STObject.__init__(self, num, timeInterval) 131 STObject.__init__(self, num, timeInterval)
121 self.trajectory = trajectory 132 self.positions = positions
122 self.geometry = geometry 133 self.geometry = geometry
123 self.type = type 134 self.type = type
124 # compute bounding polygon from trajectory 135 # compute bounding polygon from trajectory
125 136
126 def draw(self): 137 def draw(self):