-
Notifications
You must be signed in to change notification settings - Fork 1
/
linear_poisson.py
248 lines (189 loc) · 6.99 KB
/
linear_poisson.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import sys
import random
import time
from math import sqrt, ceil, floor, pi, cos
# This code requires Polygon
# http://www.cs.man.ac.uk/~toby/alan/software/
# http://pypi.python.org/pypi/Polygon
from Polygon import Polygon
from Polygon.Shapes import Circle
import sample
import time
# radius of the disks to be added. Points (aka centers) will be spaced by 2R.
R = 0.01
start_time = time.time()
if len(sys.argv) > 1:
try:
R = float(sys.argv[1])
except:
sys.stderr.write('Usage: %s <disk radius>\n'%(sys.argv[0]))
sys.exit(1)
# Some constants
Rsquared = R**2
fourRsquared = 4 * Rsquared
grid_size = int(ceil(sqrt(2.0) / R))
grid_spacing = 1 / float(grid_size)
neighbor_offset = int(ceil(2 * R / grid_spacing))
inf = float('inf')
# This test is only available in python 3.0 and up
def isinf(v):
return v == inf
# The grid is stored as a dictionary from (i, j) index of the grid square.
# The meaning of values in the grid:
# no entry - a grid square that has not been requested yet
# (p, t, r) - point p, time of arrival t, valid region r
# if t == infinity and r is True, then the point was accepted
# if t == infinity and r is False, then that grid square was
# covered by other points
grid = {}
bucket = set()
def unwrap((i, j)):
# wrap around
i = (i + grid_size) % grid_size
j = (j + grid_size) % grid_size
return (i, j)
def fetch_grid((i, j)):
'''on demand generation of empty grid squares.'''
(i, j) = unwrap((i, j))
if (i, j) not in grid:
xloc = i * grid_spacing
yloc = j * grid_spacing
r = Polygon([(xloc, yloc),
(xloc + grid_spacing, yloc),
(xloc + grid_spacing, yloc + grid_spacing),
(xloc, yloc + grid_spacing)])
p = sample.sample(r)
t = random.expovariate(r.area())
grid[i, j] = (p, t, r)
return grid[(i, j)]
def sq_distance_point_to_grid((px, py), (i, j)):
xlo = i * grid_spacing
xhi = (i + 1) * grid_spacing
ylo = j * grid_spacing
yhi = (j + 1) * grid_spacing
xdiff = min(abs(px - xlo), abs(px - xhi)) if not xlo < px < xhi else 0
ydiff = min(abs(py - ylo), abs(py - yhi)) if not ylo < py < yhi else 0
return xdiff**2 + ydiff**2
def neighbors((i, j)):
'''the neighbor grid squares of the point at (i, j) the grid.'''
p = fetch_grid((i, j))[0]
nlist = []
for offset_i in range(-neighbor_offset, neighbor_offset + 1):
for offset_j in range(-neighbor_offset, neighbor_offset + 1):
if offset_i == 0 and offset_j == 0:
continue
# this check purposefully relies on sq_distance_point_to_grid() not calling unwrap()
if sq_distance_point_to_grid(p, (i + offset_i, j + offset_j)) > fourRsquared:
continue
nlist.append((i + offset_i, j + offset_j))
return nlist
def disk((x, y)):
''' the exclusion disk, twice the radius'''
# because we're using an approximate disk, we use a just slightly
# larger R so that the minimum internal radius of the polygon is R.
Radjust = R / cos(pi / 32)
return Circle(2 * Radjust, (x, y), 32)
def too_close((x, y), (z, w)):
return ((x - z)**2 + (y - w)**2) <= fourRsquared
def closest_p(p, n):
# find closest p to grid n in wrapped space
closest_d = sq_distance_point_to_grid(p, n)
pclose = p
for off_x in [-1, 0, 1]:
for off_y in [-1, 0, 1]:
off_dist = sq_distance_point_to_grid((p[0] + off_x, p[1] + off_y), n)
if off_dist < closest_d:
closest_d = off_dist
pclose = (p[0] + off_x, p[1] + off_y)
return pclose
def update_valid(n, p):
n = unwrap(n)
# this grid square must have been created already in locally_early()
q, t, r = grid[n]
# no change if this square is already output or covered
if isinf(t):
return False
p = closest_p(p, n)
r = r - disk(p)
r.simplify()
# update
grid[n] = (q, t, r)
# Did p invalidate q?
if too_close(q, p):
A = r.area()
if A == 0:
# q's square has been covered
grid[n] = (q, inf, False)
else:
# generate a new q
q = sample.sample(r)
t = t + random.expovariate(A)
grid[n] = (q, t, r)
return True
return False
def check_nearby((i, j)):
''' find all nearby points that have become locally early.'''
# These loops have to be over all grid squares that *might* have a
# point that has (i,j) as a neighbor.
for offset_i in range(-neighbor_offset, neighbor_offset + 1):
for offset_j in range(-neighbor_offset, neighbor_offset + 1):
p, t, r = fetch_grid((i + offset_i, j + offset_j))
p = closest_p(p, (i, j))
# this check purposefully relies on sq_distance_point_to_grid() not calling unwrap()
if sq_distance_point_to_grid(p, (i, j)) > fourRsquared:
continue
if locally_early((i + offset_i, j + offset_j)):
bucket.add((i + offset_i, j + offset_j))
def accept((i, j)):
(i, j) = unwrap((i, j))
p, t, r = grid[(i, j)]
# update all valid regions for our neighbors
updated_neighbors = [n for n in neighbors((i, j)) if update_valid(n, p)]
# mark p for i,j as accepted - setting region to True indicates
# that this point should be output.
grid[(i, j)] = (p, inf, True)
# These checks must be after p's time is set to inf.
check_nearby((i, j))
for n in updated_neighbors:
check_nearby(n)
def locally_early((i, j)):
(i, j) = unwrap((i, j))
if (i, j) in bucket:
# already marked
return True
p, t, r = fetch_grid((i, j))
if isinf(t):
# already in output or grid square is covered
return False
for n in neighbors((i, j)):
np, nt, nr = fetch_grid(n)
if nt < t:
return False
return True
def traverse():
'''Traverse the grid, looking for points to add. When one is
found, empty the bucket before continuing.'''
for i in range(grid_size):
for j in range(grid_size):
if locally_early((i, j)):
bucket.add((i, j))
while len(bucket) > 0:
accept(bucket.pop())
def distchk(p, q):
p = closest_p(p, (floor(q[0] / grid_spacing), floor(q[1] / grid_spacing)))
d = (p[0] - q[0])**2 + (p[1] - q[1])**2
assert d > fourRsquared
return "%f %f sqrt %f %f"%(d, fourRsquared, sqrt(d), 2 * R)
# for reproducibility & debugging
# random.seed(0)
traverse()
output = set([p for p, t, r in grid.values() if isinf(t) and r == True])
print R, len(output), sample.sample_count, float(sample.sample_count) / len(output), grid_size * grid_size, len(output) / (time.time() - start_time)
# Every grid square should be completely covered
for p, t, r in grid.values():
assert r == False or r == True
# validate
for p in output:
for q in output:
if p != q:
distchk(p, q)