【问题标题】:Combining Perlin Noise and Poisson Disc Sampling in Python在 Python 中结合 Perlin 噪声和泊松圆盘采样
【发布时间】:2019-11-16 12:47:02
【问题描述】:

我正在尝试在 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)

泊松.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]

我期望的结果是这样的:

我几乎可以将柏林噪声图可视化。顺便说一句,这是来自上面的1st link

但我得到这样的输出:

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

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

【问题讨论】:

    标签: python poisson perlin-noise


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 2015-09-26
      相关资源
      最近更新 更多