a
    ij                  $   @   sh  d Z dZddlZddlZddlmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ dd	lmZmZmZ eefZddlZd
d ZG dd dejZejejejejejejej ej!ej"ej#ej$ej%ej&ej'ej(ej)ej*ej+i	Z,ejejejejejejej!ej ej#ej"ej%ej$ej'ej&ej)ej(ej+ej*dejdejdejdej dej"dej$dej(dej*dej&iZ-dS )a  Lists/tuples as data-format for storage

Note:
    This implementation is *far* less efficient than using Numpy
    to support lists/tuples, as the code here is all available in
    C-level code there.  This implementation is required to allow
    for usage without numpy installed.
lists    N)_types)_arrayconstants)constanterror)ERROR_ON_COPY)formathandler)bytesunicodeas_8_bitc                 C   s&   t s| S dd }t| dd|_|S dS )zNDecorator which raises informative error if we try to copy while ERROR_ON_COPYc                 _   s   t d|jjf d S )Nz%s passed, cannot copy with ERROR_ON_COPY set, please use an array type which has native data-pointer support (e.g. numpy or ctypes arrays))r   Z	CopyError	__class____name__)selfvalueargsnamed r   Q/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/OpenGL/arrays/lists.pyraiseErrorOnCopy   s
    z%err_on_copy.<locals>.raiseErrorOnCopyr   r   N)r   getattrr   )funcr   r   r   r   err_on_copy   s
    r   c                   @   s   e Zd ZdZedddZeejZ	e
Z
dZeedd Zedd	 Zed
d Zedd ZedddZedd Zedd ZeedddZeedddZeed ddZed!ddZdS )"ListHandlera  Storage of array data in Python lists/arrays

    This mechanism, unlike multi-dimensional arrays, is not necessarily
    uniform in type or dimension, so we have to do a lot of extra checks
    to make sure that we get a correctly-structured array.  That, as
    well as the need to copy the arrays in Python code, makes this a far
    less efficient implementation than the numpy implementation, which
    does all the same things, but does them all in C code.

    Note: as an *output* format, this format handler produces ctypes
        arrays, not Python lists, this is done for convenience in coding
        the implementation, mostly.
    Nc              
   C   sb   zt |W S  ttfy\ } z4| ||}t t |}|f|_|W  Y d }~S d }~0 0 d S )N)ctypesbyref	TypeErrorAttributeErrorasArrayZc_void_p	addressofZ_temporary_array_)r   instancetypeCodeerrarrayppr   r   r   
from_param8   s    zListHandler.from_paramTc                 C   s
   t |S )zCGiven value in a known data-pointer type, return void_p for pointer)r   r   )clsr   r   r   r   voidDataPointerF   s    zListHandler.voidDataPointerc                 C   s    t | }|D ]}||9 }q| S )z#Return array of zeros in given size)GL_TYPE_TO_ARRAY_MAPPING)r%   dimsr    typedimr   r   r   zerosL   s    
zListHandler.zerosc              
   C   s|   zt |g}W n. tttfy< } zg W  Y d}~S d}~0 0 d}|D ]0}| |}|durF||krFtd||f |qFdS )zCalculate total dimension-set of the elements in x

        This is *extremely* messy, as it has to track nested arrays
        where the arrays could be different sizes on all sorts of
        levels...
        Nz+Non-uniform array encountered: %s versus %s)lenr   r   
ValueErrordimsOf)r%   x_r!   ZchildDimensionchildZnewDimensionr   r   r   r.   T   s"    
zListHandler.dimsOfc                 C   s8   t |j}|dur|S td|jtt  |f dS )z=Given a value, guess OpenGL type of the corresponding pointerNzADon't know GL type for array of type %r, known types: %s
value:%s)ARRAY_TO_GL_TYPE_MAPPINGget_type_r   listkeys)r%   r   resultr   r   r   arrayToGLTypeo   s    
zListHandler.arrayToGLTypec                 C   s4   d}|  |D ] }t|dd}|dur||9 }q|S )z6Given a data-value, calculate dimensions for the array   _length_Ntypesr   )r%   r   r    r(   baselengthr   r   r   	arraySize   s    
zListHandler.arraySizec                 c   s6   |}|dur2|V  t |dd}t|ttfrd}qdS )z.Produce iterable producing all composite typesNr4   )r   
isinstancer	   r
   )r%   r   Z	dimObjectr   r   r   r<      s    zListHandler.typesc                 c   s.   |  |D ]}t|dd}|dur
|V  q
dS )z"Produce iterable of all dimensionsr:   Nr;   )r%   r   r=   r>   r   r   r   r(      s    zListHandler.dimsc                    s   du rt dt }t|ttfr fdd|D }|r |d ddd D ]}||9 }qV|t|9 }| }||dd< |S n||S dS )zConvert given value to a ctypes array value of given typeCode

        This does a *lot* of work just to get the data into the correct
        format.  It's not going to be anywhere near as fast as a numpy
        or similar approach!
        Nz0Haven't implemented type-inference for lists yetc                    s   g | ]}  |qS r   )r   ).0itemr%   r    r   r   
<listcomp>       z'ListHandler.asArray.<locals>.<listcomp>r   )NotImplementedErrorr'   r@   r5   tuple
dimensionsr,   )r%   r   r    Z	arrayTypeZsubItemsr*   r7   r   rC   r   r      s    	
zListHandler.asArrayc                 C   s   t | |d S )z-Determine unit size of an array (if possible)rF   rH   r(   r%   r   r    r   r   r   unitSize   s    zListHandler.unitSizec                 C   s   t | |S )z<Determine dimensions of the passed array value (if possible)rJ   rK   r   r   r   rI      s    zListHandler.dimensionsc                 C   s
   t |S )zCGiven a data-value, calculate number of bytes required to represent)r   ZsizeofrK   r   r   r   arrayByteCount   s    zListHandler.arrayByteCount)N)N)N)N)N)N)r   
__module____qualname____doc__r   r$   staticmethodr   r   ZdataPointerHANDLED_TYPESZisOutputclassmethodr&   r+   r.   r8   r?   r<   r(   r   rL   rI   rM   r   r   r   r   r   )   s@   	



	
	
r   fdiIhHbBs).rP   ZREGISTRY_NAMEr   Z_ctypesZOpenGL.raw.GLr   ZOpenGL.arraysr   ZGL_1_1ZOpenGLr   r   ZOpenGL._configflagsr   r   ZOpenGL._bytesr	   r
   r   r5   rH   rR   operatorr   ZFormatHandlerr   ZGLdoubleZ	GL_DOUBLEZGLfloatZGL_FLOATZGLintZGL_INTZGLuintZGL_UNSIGNED_INTZGLshortZGL_SHORTZGLushortZGL_UNSIGNED_SHORTZGLcharZGL_CHARZGLbyteZGL_BYTEZGLubyteZGL_UNSIGNED_BYTEr2   r'   r   r   r   r   <module>   sT   	  