-
Notifications
You must be signed in to change notification settings - Fork 17
/
scasim.py
96 lines (68 loc) · 2.35 KB
/
scasim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from math import sqrt, atan, acos, asin, tan, cos, sin, pi, atan2, fabs
def inverse_gnomonic(sc, center_x, center_y, distance, unit_size=1):
res = []
for x,y,d in sc:
x = (x - center_x) * unit_size / distance
y = (y - center_y) * unit_size / distance
rho = sqrt(x**2 + y**2)
c = atan(rho)
# FIXME At point 0,0 we get NaNs. (Unlikely to happen with
# real data.)
lat = asin(y * sin(c) / rho) * 180/pi
lon = atan2(x * sin(c), rho * cos(c)) * 180/pi
res.append((lon, lat, d))
return res
def which_min(*l):
mi = 0
for i,e in enumerate(l):
if e<l[mi]:
mi = i
return mi
def scasim(s, t, center_x, center_y, distance, unit_size, modulator=0.83):
if len(s) == 0:
return t.duration(), None
if len(t) == 0:
return s.duration(), None
s = inverse_gnomonic(s, center_x, center_y, distance, unit_size)
t = inverse_gnomonic(t, center_x, center_y, distance, unit_size)
# Prepare matrix:
m, n = len(s), len(t)
d = map(lambda i:[0]*(n+1), xrange(m+1))
acc = 0
for i in xrange(1,m+1):
acc += s[i-1][2]
d[i][0] = acc
acc = 0
for j in xrange(1,n+1):
acc += t[j-1][2]
d[0][j] = acc
# Compute similarity:
for i in xrange(n):
for j in xrange(m):
slon = s[j][0] / (180/pi)
tlon = t[i][0] / (180/pi)
slat = s[j][1] / (180/pi)
tlat = t[i][1] / (180/pi)
angle = acos(sin(slat) * sin(tlat) +
cos(slat) * cos(tlat) * cos(slon - tlon)) * (180/pi)
mixer = modulator**angle
cost = (abs(t[i][2] - s[j][2]) * mixer +
(t[i][2] + s[j][2]) * (1.0 - mixer))
ops = (d[j][i+1] + s[j][2],
d[j+1][i] + t[i][2],
d[j][i] + cost)
mi = which_min(*ops)
d[j+1][i+1] = ops[mi]
return d[-1][-1]
if __name__ == '__main__':
# Each tuple is a fixation with elements x-coordinate,
# y-coordinate, duration:
sc1 = [(100, 100, 100),
(200, 200, 100),
(300, 100, 100)]
sc2 = [(100, 200, 100),
(200, 300, 100),
(300, 300, 100)]
print scasim(sc1, sc2, 1024/2.0, 768/2.0, 60.0, 1/30.0, 0.8)