Aaron Otillar
Aaron Otillar

Reputation: 31

OpenSimplex and PyQt5: Why is my function taking so long?

I am trying to implement Simplex Noise using PyQt5 and Python to create Proceduraly Generated Height maps for simulation purposes. I have chosen PyQt5 because that is the GUI I am most familiar with. Here is my code for the Height Map Generation using the OpenSimplex Library.

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

And here is the PyQt5 Code that I am using to Draw this HeightMap

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)

When I time the draw_noise_grid() I get almost 1.5 seconds for this to even render, and half the time it breaks python and it shuts down. My question what am I doing wrong to cause this to render incredibly slow? Or do I need to choose a new GUI? Any help would be greatly appreciated!

Upvotes: 2

Views: 288

Answers (1)

Christophe Roussy
Christophe Roussy

Reputation: 17029

I have not tested it, but my comment seems to be the answer.

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

It says:

stable but slow.

If you want something fast you would need to use something in C via a lib like numpy or scipy ... So either contribute to the project by providing faster code or wait for an enhancement.

Or write your own implementation.

Upvotes: 1

Related Questions