我正在尝试在2d网格上复制现实的植被位置。为此,我使用泊松圆盘采样(Bridson algorithm
)植被位置和perlin noise
来确定每个区域的植被密度。
当我排除perlin noise
并保持恒定的最小距离时,我会获得理想的结果。但是,当我通过perlin noise
更改最小距离时,结果没有意义。
我做错了什么?
Python 3.4.4。我尝试查看来自here的伪代码,已经查看了StackOverflow,甚至here。 我还从[github](https://github.com/emulbreh/bridson)复制了代码,并做了一些改动。
但是我似乎无法理解我的错误。
main.py
import subprocess as sp
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import toimage
import noise
from Poisson import poisson_disc_samples
def generate_perlin_poisson(width, height):
# Perlin Noise
print('Perlin Noise')
shape = (height, width)
scale = 100.0
octaves = 6
persistence = 0.5
lacunarity = 2.0
world = np.zeros(shape)
for i in range(shape[0]):
for j in range(shape[1]):
world[i][j] = noise.pnoise2(i / scale,
j / scale,
octaves=octaves,
persistence=persistence,
lacunarity=lacunarity,
repeatx=shape[0],
repeaty=shape[1],
base=0)
toimage(world).show()
min_rad = 1
max_rad = 5
z = np.interp(world, (np.amin(world), np.amax(world)), (min_rad, max_rad))
# # Notepad PrintOut
# fileName = 'perlin_world.txt'
# programName = "notepad.exe"
# with open(fileName, 'w') as f:
# for row in range(z.shape[0]):
# # print(row, z[row])
# f.write(str(z[row].tolist()) + '\n')
#
# sp.Popen([programName, fileName])
# Bridson Poisson Disc Sampling
print('Bridson Poisson Disc Sampling')
plt.scatter(*zip(*poisson_disc_samples(width=height, height=width, r_max=max_rad, r_min=min_rad, r_array=z)), c='g', alpha=0.6, lw=0)
plt.show()
print('Completed.')
if __name__ == '__main__':
width, height = 256, 256
generate_perlin_poisson(width, height)
Poisson.py
from random import random
from math import cos, sin, floor, sqrt, pi, ceil
def euclidean_distance(a, b):
dx = a[0] - b[0]
dy = a[1] - b[1]
return sqrt(dx * dx + dy * dy)
def poisson_disc_samples(width, height, r_max, r_min, k=3, r_array=[], distance=euclidean_distance, random=random):
tau = 2 * pi
cellsize = r_max / sqrt(2)
grid_width = int(ceil(width / cellsize))
grid_height = int(ceil(height / cellsize))
grid = [None] * (grid_width * grid_height)
def grid_coords(p):
return int(floor(p[0] / cellsize)), int(floor(p[1] / cellsize))
def fits(p, gx, gy, r):
yrange = list(range(max(gy - 2, 0), min(gy + 3, grid_height)))
for x in range(max(gx - 2, 0), min(gx + 3, grid_width)):
for y in yrange:
g = grid[x + y * grid_width]
if g is None:
continue
r = r_array[int(floor(g[0]))][int(floor(g[1]))]
if distance(p, g) <= r: # too close
return False
return True
p = width * random(), height * random()
queue = [p]
grid_x, grid_y = grid_coords(p)
grid[grid_x + grid_y * grid_width] = p
z_max = width * height * 8
z = 0
while queue:
qindex = int(random() * len(queue)) # select random point from queue
qx, qy = queue.pop(qindex)
r = r_array[int(floor(qx))][int(floor(qy))]
# print('min_dist:', r)
z += 1
if z > z_max:
print('max iteration exceeded')
break
for _ in range(k):
alpha = tau * random()
d = r * sqrt(3 * random() + 1)
px = qx + d * cos(alpha)
py = qy + d * sin(alpha)
if not (0 <= px < width and 0 <= py < height):
continue
p = (px, py)
grid_x, grid_y = grid_coords(p)
if not fits(p, grid_x, grid_y, r):
continue
queue.append(p)
grid[grid_x + grid_y * grid_width] = p
return [p for p in grid if p is not None]
我期望这样的结果:
我几乎可以看到Perlin噪声图。顺便说一下,这是从上方的1st link上来的。
但是我得到这样的输出:
灰度图是相关的产生的珀林噪声。
我知道有更有效的做事方法。我打算坚持使用Python。
答案 0 :(得分:0)
我看到的一个问题是k = 3。如果您查看Bridson's original article,他建议k = 30。如果k太小,则说明您的测试不足以查看候选点是否接近现有点。正如您在输出中看到的那样,这很容易导致意外的成簇。另一个问题是,通用Bridson算法为r假定了一个静态值,但是使用Perlin获得的结块是因为您随噪声值改变了r。由于r驱动像元大小,因此必须对Bridson算法进行相当大的修改。