gpt4 book ai didi

python - 在 Python 中结合 Perlin 噪声和泊松圆盘采样

转载 作者:行者123 更新时间:2023-12-05 07:21:28 26 4
gpt4 key购买 nike

我正在尝试在二维网格上复制逼真的植被放置。为此,我使用泊松圆盘采样(Bridson 算法)植被放置和perlin 噪声 来确定每个区域的植被密度。

当我排除 perlin 噪声 并保持恒定的最小距离时,我获得了理想的结果。但是,当我通过 perlin 噪声 改变最小距离时,结果没有意义。

我做错了什么?

python 3.4.4。我试图查看来自 here 的伪代码, 我环顾四周 StackOverflow , 甚至 here .我还从 [github] ( https://github.com/emulbreh/bridson) 复制了代码并稍作修改。

但我似乎无法理解我的错误。

主要.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)

泊松分布

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]

我期望这样的结果:

image taken from https://imgur.com/LKOsjh2

我几乎可以看到 perlin 噪声图。顺便说一句,这是来自 1st link在上面。

但是我得到这样的输出:

image taken from https://imgur.com/j4W6Hup

灰度图是关联生成的柏林噪声。

我知道有更有效的做事方式。不过,我打算坚持使用 Python。

最佳答案

我看到的一个问题是 k=3。如果你看Bridson's original article ,他建议 k=30。如果 k 太低,则说明您没有进行足够的测试以查看候选点是否接近现有点。正如您在输出中看到的那样,这很容易导致意外的结 block 。另一个问题是,通用 Bridson 算法假定 r 为静态值,但使用 Perlin 时出现的聚集是因为您随噪声值改变 r。必须对 Bridson 算法进行相当大的修改,因为 r 决定了像元大小。

关于python - 在 Python 中结合 Perlin 噪声和泊松圆盘采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56910591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com