1

我正在尝试使用 PyQt5 和 Python 实现 Simplex Noise 以创建程序生成的高度图以用于模拟目的。我选择了 PyQt5,因为那是我最熟悉的 GUI。这是我使用 OpenSimplex 库生成高度图的代码。

def noise_map(width,height):
    height = height
    width = width
    noise_height_map = [[0 for x in range(width)] for y in range(height)]
    for y in range(height):
        for x in range(width):
            nx = x
            ny = y
            noise_height_map[y][x] = noise(nx, ny)
    return noise_height_map

这是我用来绘制这个高度图的 PyQt5 代码

class Example(QWidget):

def __init__(self):
    super().__init__()

    self.size = self.size()
    self.map = pickle.load( open( "noise.p", "rb" ) )
    self.gen = OpenSimplex()
    self.width = 300
    self.height = 300
    self.initUI()

def initUI(self):
    self.setGeometry(300, 300, self.width, self.height)
    self.setWindowTitle('Points')
    self.show()

def paintEvent(self, e):
    start_time = time.time()
    qp = QPainter()
    qp.begin(self)
    #self.draw_random_grid(qp)
    self.draw_noise_grid(qp)
    #self.draw_noise_grid_v2(qp)
    qp.end()
    end_time= time.time()
    print('Paint Event:',end_time - start_time)

def noise(self, nx, ny):
    # Rescale from -1.0:+1.0 to 0.0:1.0
    return self.simplex.noise2d(nx, ny)
           #/ 2.0 + 0.5

def noise_map(self, width, height):
    height = height
    width = width
    noise_map = [[0 for x in range(width)] for y in range(height)]
    for y in range(height):
        for x in range(width):
            nx = x
            ny = y
            noise_map[y][x] = self.noise(nx, ny)

    return noise_map

def draw_noise_grid(self, qp):
    start_time = time.time()
    column_index = 0
    row_index = 0
    col = QColor(0, 0, 0)

    for row in range(self.width):
        for column in range(self.height):
            value =(self.map[row][column] * 255)
            col.setHsv(0, 0, value, 255)
            qp.fillRect(column_index, row_index, 10, 10, col)


            column_index += 10
            if column_index >= 1000:
                column_index = 0

        row_index += 10
        if row_index >= 1000:
            row_index = 0

    end_time = time.time()
    print('Draw Grid:',end_time - start_time)

当我对 draw_noise_grid() 计时时,我得到了将近 1.5 秒的时间来渲染它,而一半的时间它会破坏 python 并关闭。我的问题是我做错了什么导致它变得异常缓慢?还是我需要选择一个新的 GUI?任何帮助将不胜感激!

4

1 回答 1

0

我没有测试过,但我的评论似乎是答案。

https://github.com/lmas/opensimplex#status

它说:

稳定但缓慢。

如果你想要快速的东西,你需要通过像 numpy 或 scipy 这样的库在 C 中使用一些东西......所以要么通过提供更快的代码为项目做出贡献,要么等待增强。

或者编写自己的实现。

于 2018-02-12T09:42:46.490 回答