o
     Pj                     @   sr   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZ G dd dZdS )	zPerlin Noise calculating lib.    N)	lru_cache)DictIterableListOptionalTupleUnion)RandVec)hasherc                	   @   s   e Zd ZdZddedee fddZ	ddeeee	e
f d	eeee	e
f  d
efddZ	ddeeee	e
ef d	eeee	e
f  d
efddZeddd	dde
edf d	ee
edf  d
efddZdS )PerlinNoisez]Smooth random noise generator.

    read more https://en.wikipedia.org/wiki/Perlin_noise
       Noctavesseedc                 C   sX   |dkrt d|durt|ts|dkrt d|| _|r |ntdd| _i | _dS )a  Perlin Noise object initialization class.

            ex.: noise = PerlinNoise(n_dims=2, octaves=3.5, seed=777)

        Parameters:
            octaves : optional positive float, default = 1
                positive number of sub rectangles in each [0, 1] range
            seed : optional positive int, default = None
                specified seed

        Raises:
            ValueError: if seed is negative
        r   z&octaves expected to be positive numberNz+seed expected to be positive integer numberr   i )
ValueError
isinstanceintr   randomrandintr   cache)selfr   r    r   N/home/thesage/.local/lib/python3.10/site-packages/perlin_noise/perlin_noise.py__init__   s   
zPerlinNoise.__init__coordinates
tile_sizesreturnc                 C   s   |  ||S )zForward request to noise function.

        Parameters:
            coordinates: float or list of coordinates
            tile_sizes: optional tile sizes to repetative patterns

        Returns:
            noise_value
        )noise)r   r   r   r   r   r   __call__+   s   zPerlinNoise.__call__c                    s   t  ttttfstdt  ttfr g nt  durVt tr(gtdd D s5tdtt krAtddd t	 D  tfd	dD fd
d D  dd  D }t
 fddtj| D S )a  Get perlin noise value for given coordinates.

        Parameters:
            coordinates: float or list of coordinates
            tile_sizes: optional tile sizes to repetative patterns

        Returns:
            noise_value

        Raises:
            TypeError: if coordinates is not valid type
            ValueError: if tile_sizes have different length than coordinates
        z*coordinates must be int, float or iterableNc                 s   s    | ]}t |tV  qd S N)r   r   .0tiler   r   r   	<genexpr>Y       z$PerlinNoise.noise.<locals>.<genexpr>z%tile_sizes must be int or list of intz/tile_sizes must have same length as coordinatesc                 S   s   g | ]\}}|| qS r   r   )r    coorsr!   r   r   r   
<listcomp>`   s    z%PerlinNoise.noise.<locals>.<listcomp>c                 3   s    | ]}| j  V  qd S r   r   r   r   r   r   r"   a   r#   c                    s   g | ]}| j  qS r   r&   r    
coordinater'   r   r   r%   b   s    c                 S   s$   g | ]}t |t |d  fqS )r   )mathfloorr(   r   r   r   r%   d   s    c                    s   g | ]} | qS r   )get_from_cache_of_create_newget_weighted_val)r    r$   r   r   r   r   r   r%   i   s    )r   r   floatlisttuple	TypeErroralllenr   zipsum	itertoolsproduct)r   r   r   coor_bounding_boxr   r.   r   r   ;   s.   
zPerlinNoise.noised   F)maxsizetypedr$   .c                 C   s   t || jt|| S )zUse cached RandVec or creates new.

        Parameters:
            coors: Tuple of int vector coordinates
            tile_sizes: optional tile sizes to repetative patterns

        Returns:
            RandVec
        )r	   r   r
   )r   r$   r   r   r   r   r,   q   s   z(PerlinNoise.get_from_cache_of_create_new)r   Nr   )__name__
__module____qualname____doc__r/   r   r   r   r   r   r   r   r   r   r   r	   r,   r   r   r   r   r      s8    


6
r   )r@   r7   r*   r   	functoolsr   typingr   r   r   r   r   r   perlin_noise.rand_vecr	   perlin_noise.toolsr
   r   r   r   r   r   <module>   s     