Mercurial Hosting > traffic-intelligence
comparison python/utils.py @ 152:74b1fc68d4df
re-organized code to avoid cyclic python module dependencies
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 06 Sep 2011 18:44:23 -0400 |
parents | 2bf5b76320c0 |
children | f03fe3d6d0c8 |
comparison
equal
deleted
inserted
replaced
151:4af774bb186d | 152:74b1fc68d4df |
---|---|
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 ''' Generic utilities.''' | 2 ''' Generic utilities.''' |
3 | 3 |
4 #from numpy import * | 4 #from numpy import * |
5 #from pylab import * | 5 #from pylab import * |
6 | |
7 import moving | |
8 | 6 |
9 __metaclass__ = type | 7 __metaclass__ = type |
10 | 8 |
11 commentChar = '#' | 9 commentChar = '#' |
12 | 10 |
117 eg 1.23 at 0 decimal is 2, at 1 decimal is 1.3''' | 115 eg 1.23 at 0 decimal is 2, at 1 decimal is 1.3''' |
118 from math import ceil,pow | 116 from math import ceil,pow |
119 tens = pow(10,nDecimals) | 117 tens = pow(10,nDecimals) |
120 return ceil(v*tens)/tens | 118 return ceil(v*tens)/tens |
121 | 119 |
122 def segmentIntersection(p1, p2, p3, p4): | 120 def inBetween(bound1, bound2, x): |
123 '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise''' | 121 return bound1 <= x <= bound2 or bound2 <= x<= bound1 |
124 from numpy import matrix | |
125 from numpy.linalg import linalg, det | |
126 | |
127 dp1 = p2-p1#[s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]] | |
128 dp2 = p4-p3#[s2[0][1]-s2[0][0], s2[1][1]-s2[1][0]] | |
129 | |
130 A = matrix([[dp1.y, -dp1.x], | |
131 [dp2.y, -dp2.x]]) | |
132 B = matrix([[dp1.y*p1.x-dp1.x*p1.y], | |
133 [dp2.y*p3.x-dp2.x*p3.y]]) | |
134 | |
135 if linalg.det(A) == 0:#crossProduct(ds1, ds2) == 0: | |
136 return None | |
137 else: | |
138 intersection = linalg.solve(A,B) | |
139 if (moving.Interval(p1.x, p2.x, True).contains(intersection[0,0]) | |
140 and moving.Interval(p3.x, p4.x, True).contains(intersection[0,0]) | |
141 and moving.Interval(p1.y, p2.y, True).contains(intersection[1,0]) | |
142 and moving.Interval(p3.y, p4.y, True).contains(intersection[1,0])): | |
143 return moving.Point(intersection[0,0], intersection[1,0]) | |
144 else: | |
145 return None | |
146 | 122 |
147 def crossProduct(l1, l2): | 123 def crossProduct(l1, l2): |
148 return l1[0]*l2[1]-l1[1]*l2[0] | 124 return l1[0]*l2[1]-l1[1]*l2[0] |
149 | 125 |
150 def filterMovingWindow(input, halfWidth): | 126 def filterMovingWindow(input, halfWidth): |