if grid[gp]: return True for cell in grid.square_iter(gp, 2): if cell and sqr_dist(cell, p) <= r_sqr: return True return False #Create the grid cell_size = r/sqrt(2) inv_cell_size = 1 / cell_size r_sqr = r*r grid = Grid2D((int(ceil(width/cell_size)), int(ceil(height/cell_size)))) process_list = RandomQueue() sample_points = [] #generate the first point put_point((rand(width), rand(height))) #generate other points from points in queue. while not process_list.empty(): p = process_list.pop() for i in xrange(k): q = generate_random_around(p, r) if in_rectangle(q) and not in_neighbourhood(q): put_point(q) return sample_points
def sample_poisson_3d(width, height, depth, r_grid, k): #Convert rectangle (the one to be sampled) coordinates to # coordinates in the grid. def grid_coordinates(p): x, y, z = p return int_point_3d((x*inv_cell_size, y*inv_cell_size, z*inv_cell_size)) # Puts a sample point in all the algorithm's relevant containers. def put_point(p): process_list.push(p) sample_points.append(p) grid[grid_coordinates(p)].append(p) # Generates a point randomly selected around # the given point, between r and 2*r units away. def generate_random_around(p, r): x, y, z = p rr = uniform(r, 2*r) rs = uniform(0, 2*pi) rt = uniform(0, 2*pi) return rr*sin(rs)*cos(rt) + x, rr*sin(rs)*sin(rt) + y, rr*cos(rs) + z # Is the given point in the rectangle to be sampled? def in_rectangle(p): x, y, z = p return 0 <= x < width and 0 <= y < height and 0 <= z < depth def in_neighbourhood(p, r): gp = grid_coordinates(p) r_sqr = r*r for cell in grid.square_iter(gp, 2): for q in cell: if sqr_dist_3d(q, p) <= r_sqr: return True return False r_min, r_max = r_grid.min_max() #Create the grid cell_size = r_max/sqrt(2) inv_cell_size = 1 / cell_size r_max_sqr = r_max*r_max grid = ListGrid3D(( int(ceil(width/cell_size)), int(ceil(height/cell_size)), int(ceil(depth/cell_size)))) process_list = RandomQueue() sample_points = [] #generate the first point put_point((rand(width), rand(height), rand(depth))) #generate other points from points in queue. while not process_list.empty(): p = process_list.pop() r = r_grid[int_point_3d(p)] for i in range(k): q = generate_random_around(p, r) if in_rectangle(q) and not in_neighbourhood(q, r): put_point(q) return sample_points
def sample_poisson_uniform(width, height, r, k): #Convert rectangle (the one to be sampled) coordinates to # coordinates in the grid. def grid_coordinates(p): x, y = p return (int(x*inv_cell_size), int(y*inv_cell_size)) # Puts a sample point in all the algorithm's relevant containers. def put_point(p): process_list.push(p) sample_points.append(p) grid[grid_coordinates(p)] = p # Generates a point randomly selected around # the given point, between r and 2*r units away. def generate_random_around(p, r): x, y = p rr = uniform(r, 2*r) rt = uniform(0, 2*pi) return rr*sin(rt) + x, rr*cos(rt) + y # Is the given point in the rectangle to be sampled? def in_rectangle(p): x, y = p return 0 <= x < width and 0 <= y < height def in_neighbourhood(p): gp = gx, gy = grid_coordinates(p) if grid[gp]: return True for cell in grid.square_iter(gp, 2): if cell and sqr_dist(cell, p) <= r_sqr: return True return False #Create the grid cell_size = r/sqrt(2) inv_cell_size = 1 / cell_size r_sqr = r*r grid = Grid2D((int(ceil(width/cell_size)), int(ceil(height/cell_size)))) process_list = RandomQueue() sample_points = [] #generate the first point put_point((rand(width), rand(height))) #generate other points from points in queue. while not process_list.empty(): p = process_list.pop() for i in range(k): q = generate_random_around(p, r) if in_rectangle(q) and not in_neighbourhood(q): put_point(q) return sample_points
if grid[gp]: return True for cell in grid.square_iter(gp, 2): if cell and sqr_dist(cell, p) <= r_sqr: return True return False #Create the grid cell_size = r / sqrt(2) inv_cell_size = 1 / cell_size r_sqr = r * r grid = Grid2D( (int(ceil(width / cell_size)), int(ceil(height / cell_size)))) process_list = RandomQueue() sample_points = [] #generate the first point put_point((rand(width), rand(height))) #generate other points from points in queue. while not process_list.empty(): p = process_list.pop() for i in xrange(k): q = generate_random_around(p, r) if in_rectangle(q) and not in_neighbourhood(q): put_point(q) return sample_points