Mercurial > hg > nsaunier > traffic-intelligence
comparison python/moving.py @ 79:5d487f183fe2
added method to test points in polygons and tests
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 11 Mar 2011 18:12:29 -0500 |
| parents | d3e1a7cf3375 |
| children | 41da2cdcd91c |
comparison
equal
deleted
inserted
replaced
| 78:99e807c29753 | 79:5d487f183fe2 |
|---|---|
| 137 return sqrt(self.norm2Squared()) | 137 return sqrt(self.norm2Squared()) |
| 138 | 138 |
| 139 def aslist(self): | 139 def aslist(self): |
| 140 return [self.x, self.y] | 140 return [self.x, self.y] |
| 141 | 141 |
| 142 def inPolygon(self, poly): | |
| 143 '''Returns if the point x, y is inside the polygon. | |
| 144 The polygon is defined by the ordered list of points in poly | |
| 145 | |
| 146 taken from http://www.ariel.com.au/a/python-point-int-poly.html''' | |
| 147 | |
| 148 n = len(poly); | |
| 149 counter = 0; | |
| 150 | |
| 151 p1 = poly[0]; | |
| 152 for i in range(n+1): | |
| 153 p2 = poly[i % n]; | |
| 154 if self.y > min(p1.y,p2.y): | |
| 155 if self.y <= max(p1.y,p2.y): | |
| 156 if self.x <= max(p1.x,p2.x): | |
| 157 if p1.y != p2.y: | |
| 158 xinters = (self.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x; | |
| 159 if p1.x == p2.x or self.x <= xinters: | |
| 160 counter+=1; | |
| 161 p1=p2 | |
| 162 return (counter%2 == 1); | |
| 163 | |
| 164 | |
| 142 @staticmethod | 165 @staticmethod |
| 143 def dot(p1, p2): | 166 def dot(p1, p2): |
| 144 return p1.x*p2.x+p1.y*p2.y | 167 return p1.x*p2.x+p1.y*p2.y |
| 145 | 168 |
| 146 @staticmethod | 169 @staticmethod |
| 156 '''Class for trajectories | 179 '''Class for trajectories |
| 157 i.e. a temporal sequence of positions | 180 i.e. a temporal sequence of positions |
| 158 | 181 |
| 159 the class is iterable.''' | 182 the class is iterable.''' |
| 160 | 183 |
| 161 def __init__(self, positions): | 184 def __init__(self, positions=None): |
| 162 self.positions = positions | 185 self.positions = positions |
| 163 | 186 |
| 164 @staticmethod | 187 @staticmethod |
| 165 def load(line1, line2): | 188 def load(line1, line2): |
| 166 return Trajectory([[float(n) for n in line1.split(' ')], | 189 return Trajectory([[float(n) for n in line1.split(' ')], |
| 192 else: | 215 else: |
| 193 self.positions[0].append(x) | 216 self.positions[0].append(x) |
| 194 self.positions[1].append(y) | 217 self.positions[1].append(y) |
| 195 | 218 |
| 196 def addPosition(self, p): | 219 def addPosition(self, p): |
| 197 self.addPosition(p.x, p.y) | 220 self.addPositionXY(p.x, p.y) |
| 198 | 221 |
| 199 def draw(self, options = ''): | 222 def draw(self, options = ''): |
| 200 from matplotlib.pylab import plot | 223 from matplotlib.pylab import plot |
| 201 plot(self.positions[0], self.positions[1], options) | 224 plot(self.positions[0], self.positions[1], options) |
| 202 | 225 |
| 256 return Trajectory([self.positions[0][inter.first:inter.last], | 279 return Trajectory([self.positions[0][inter.first:inter.last], |
| 257 self.positions[1][inter.first:inter.last]]) | 280 self.positions[1][inter.first:inter.last]]) |
| 258 else: | 281 else: |
| 259 return None | 282 return None |
| 260 | 283 |
| 261 def getTrajectoryInPolygon(self, polygon): | 284 def getTrajectoryInPolygon1(self, polygon): |
| 262 'Returns the set of points inside the polygon' | 285 'Returns the set of points inside the polygon' |
| 263 # use shapely polygon contains | 286 t = Trajectory() |
| 264 pass | 287 for i in xrange(self.length()): |
| 288 p = self.__getitem__(i) | |
| 289 if p.inPolygon(polygon): | |
| 290 t.addPosition(p) | |
| 291 if t.length()>0: | |
| 292 return t | |
| 293 else: | |
| 294 return None | |
| 295 | |
| 296 # version 2: use shapely polygon contains | |
| 265 | 297 |
| 266 ################## | 298 ################## |
| 267 # Moving Objects | 299 # Moving Objects |
| 268 ################## | 300 ################## |
| 269 | 301 |
