Created
November 15, 2016 16:42
-
-
Save rougier/c168856c01159833ad06381388821fe6 to your computer and use it in GitHub Desktop.
Poisson disk sampling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def poisson_disk_sample(width=1.0, height=1.0, radius=0.025, k=30): | |
# References: Fast Poisson Disk Sampling in Arbitrary Dimensions | |
# Robert Bridson, SIGGRAPH, 2007 | |
def squared_distance(p0, p1): | |
return (p0[0]-p1[0])**2 + (p0[1]-p1[1])**2 | |
def random_point_around(p, k=1): | |
# WARNING: This is not uniform around p but we can live with it | |
R = np.random.uniform(radius, 2*radius, k) | |
T = np.random.uniform(0, 2*np.pi, k) | |
P = np.empty((k, 2)) | |
P[:, 0] = p[0]+R*np.sin(T) | |
P[:, 1] = p[1]+R*np.cos(T) | |
return P | |
def in_limits(p): | |
return 0 <= p[0] < width and 0 <= p[1] < height | |
def neighborhood(shape, index, n=2): | |
row, col = index | |
row0, row1 = max(row-n, 0), min(row+n+1, shape[0]) | |
col0, col1 = max(col-n, 0), min(col+n+1, shape[1]) | |
I = np.dstack(np.mgrid[row0:row1, col0:col1]) | |
I = I.reshape(I.size//2, 2).tolist() | |
I.remove([row, col]) | |
return I | |
def in_neighborhood(p): | |
i, j = int(p[0]/cellsize), int(p[1]/cellsize) | |
if M[i, j]: | |
return True | |
for (i, j) in N[(i, j)]: | |
if M[i, j] and squared_distance(p, P[i, j]) < squared_radius: | |
return True | |
return False | |
def add_point(p): | |
points.append(p) | |
i, j = int(p[0]/cellsize), int(p[1]/cellsize) | |
P[i, j], M[i, j] = p, True | |
# Here `2` corresponds to the number of dimension | |
cellsize = radius/np.sqrt(2) | |
rows = int(np.ceil(width/cellsize)) | |
cols = int(np.ceil(height/cellsize)) | |
# Squared radius because we'll compare squared distance | |
squared_radius = radius*radius | |
# Positions cells | |
P = np.zeros((rows, cols, 2), dtype=np.float32) | |
M = np.zeros((rows, cols), dtype=bool) | |
# Cache generation for neighborhood | |
N = {} | |
for i in range(rows): | |
for j in range(cols): | |
N[(i, j)] = neighborhood(M.shape, (i, j), 2) | |
points = [] | |
add_point((np.random.uniform(width), np.random.uniform(height))) | |
while len(points): | |
i = np.random.randint(len(points)) | |
p = points[i] | |
del points[i] | |
Q = random_point_around(p, k) | |
for q in Q: | |
if in_limits(q) and not in_neighborhood(q): | |
add_point(q) | |
return P[M] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment