Source code for k1lib._perlin

# AUTOGENERATED FILE! PLEASE DON'T EDIT HERE. EDIT THE SOURCE NOTEBOOKS INSTEAD
import numpy as np, k1lib
plt = k1lib.dep.plt
mpl = k1lib.dep.mpl
from typing import Any, List, Union, Tuple
__all__ = ["perlin3d"]
def interpolant(t):                                                              # interpolant
    return t*t*t*(t*(t*6 - 15) + 10)                                             # interpolant
[docs]def perlin3d(shape=(100, 100, 100), res=(2, 2, 2), tileable=(False, False, False), interpolant=interpolant): # perlin3d """Generate a 3D numpy array of perlin noise. Not my code! All credits go to the author of this library: https://github.com/pvigier/perlin-numpy :param shape: The shape of the generated array (tuple of three ints). This must be a multiple of res. :param res: The number of periods of noise to generate along each axis (tuple of three ints). Note shape must be a multiple of res. :param tileable: If the noise should be tileable along each axis (tuple of three bools). Defaults to (False, False, False). :param interpolant: The interpolation function, defaults to t*t*t*(t*(t*6 - 15) + 10). :return: A numpy array of shape shape with the generated noise. :raises ValueError: If shape is not a multiple of res.""" # perlin3d delta = (res[0] / shape[0], res[1] / shape[1], res[2] / shape[2]) # perlin3d d = (shape[0] // res[0], shape[1] // res[1], shape[2] // res[2]) # perlin3d grid = np.mgrid[0:res[0]:delta[0],0:res[1]:delta[1],0:res[2]:delta[2]] # perlin3d grid = np.mgrid[0:res[0]:delta[0],0:res[1]:delta[1],0:res[2]:delta[2]] # perlin3d grid = grid.transpose(1, 2, 3, 0) % 1 # perlin3d # Gradients # perlin3d theta = 2*np.pi*np.random.rand(res[0] + 1, res[1] + 1, res[2] + 1) # perlin3d phi = 2*np.pi*np.random.rand(res[0] + 1, res[1] + 1, res[2] + 1) # perlin3d gradients = np.stack( # perlin3d (np.sin(phi)*np.cos(theta), np.sin(phi)*np.sin(theta), np.cos(phi)), # perlin3d axis=3 # perlin3d ) # perlin3d if tileable[0]: # perlin3d gradients[-1,:,:] = gradients[0,:,:] # perlin3d if tileable[1]: # perlin3d gradients[:,-1,:] = gradients[:,0,:] # perlin3d if tileable[2]: # perlin3d gradients[:,:,-1] = gradients[:,:,0] # perlin3d gradients = gradients.repeat(d[0], 0).repeat(d[1], 1).repeat(d[2], 2) # perlin3d g000 = gradients[ :-d[0], :-d[1], :-d[2]] # perlin3d g100 = gradients[d[0]: , :-d[1], :-d[2]] # perlin3d g010 = gradients[ :-d[0],d[1]: , :-d[2]] # perlin3d g110 = gradients[d[0]: ,d[1]: , :-d[2]] # perlin3d g001 = gradients[ :-d[0], :-d[1],d[2]: ] # perlin3d g101 = gradients[d[0]: , :-d[1],d[2]: ] # perlin3d g011 = gradients[ :-d[0],d[1]: ,d[2]: ] # perlin3d g111 = gradients[d[0]: ,d[1]: ,d[2]: ] # perlin3d # Ramps # perlin3d n000 = np.sum(np.stack((grid[:,:,:,0] , grid[:,:,:,1] , grid[:,:,:,2] ), axis=3) * g000, 3) # perlin3d n100 = np.sum(np.stack((grid[:,:,:,0]-1, grid[:,:,:,1] , grid[:,:,:,2] ), axis=3) * g100, 3) # perlin3d n010 = np.sum(np.stack((grid[:,:,:,0] , grid[:,:,:,1]-1, grid[:,:,:,2] ), axis=3) * g010, 3) # perlin3d n110 = np.sum(np.stack((grid[:,:,:,0]-1, grid[:,:,:,1]-1, grid[:,:,:,2] ), axis=3) * g110, 3) # perlin3d n001 = np.sum(np.stack((grid[:,:,:,0] , grid[:,:,:,1] , grid[:,:,:,2]-1), axis=3) * g001, 3) # perlin3d n101 = np.sum(np.stack((grid[:,:,:,0]-1, grid[:,:,:,1] , grid[:,:,:,2]-1), axis=3) * g101, 3) # perlin3d n011 = np.sum(np.stack((grid[:,:,:,0] , grid[:,:,:,1]-1, grid[:,:,:,2]-1), axis=3) * g011, 3) # perlin3d n111 = np.sum(np.stack((grid[:,:,:,0]-1, grid[:,:,:,1]-1, grid[:,:,:,2]-1), axis=3) * g111, 3) # perlin3d # Interpolation # perlin3d t = interpolant(grid) # perlin3d n00 = n000*(1-t[:,:,:,0]) + t[:,:,:,0]*n100 # perlin3d n10 = n010*(1-t[:,:,:,0]) + t[:,:,:,0]*n110 # perlin3d n01 = n001*(1-t[:,:,:,0]) + t[:,:,:,0]*n101 # perlin3d n11 = n011*(1-t[:,:,:,0]) + t[:,:,:,0]*n111 # perlin3d n0 = (1-t[:,:,:,1])*n00 + t[:,:,:,1]*n10 # perlin3d n1 = (1-t[:,:,:,1])*n01 + t[:,:,:,1]*n11 # perlin3d return ((1-t[:,:,:,2])*n0 + t[:,:,:,2]*n1) # perlin3d