a
    ij6)                  
   @   s  d Z ddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlZddlZddlmZ ddlZdd	lmZ d
ZG dd de	jejZeeej_dd Zej D ]>\ZZeeZeej e< eeje ksJ ej!d eksJ qz
[[[W n& e"y& Z# zW Y dZ#[#n
dZ#[#0 0 dd Z$eejdd Zeej%dd Z%dZ&dd Z'dd Z(eej)dd Z)eej*dd Z*eej+dd  Z+eej,d!d" Z,dS )#a  Implementation of GLU Nurbs structure and callback methods

Same basic pattern as seen with the gluTess* functions, just need to
add some bookkeeping to the structure class so that we can keep the
Python function references alive during the calling process.
    )GLU)platform
converterswrapper)	glustruct)lazy)arrayserrorN)PLATFORM)_configflags)GLUnurbsgluNewNurbsRenderergluNurbsCallbackgluNurbsCallbackDatagluNurbsCallbackDataEXTgluNurbsCurvegluNurbsSurfacegluPwlCurvec                   @   s  e Zd ZdZeejZi Ze	j
ede	je	jede	jee	je	jedee	je	jedee	jee	je	jedee	je	jedee	jee	je	jedee	je	jedee	jee	je	jedee	je	jedee	jee	je	jede	jedee	je	jede	jiZ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de	jde	jde	jdiZdd Zdd
dZdd Z dd Z!dS )r   a  GLU Nurbs structure with oor and callback storage support

    IMPORTANT NOTE: the texture coordinate callback receives a raw ctypes
    data-pointer, as without knowing what type of evaluation is being done
    (1D or 2D) we cannot safely determine the size of the array to convert
    it.  This is a limitation of the C implementation.  To convert to regular
    data-pointer, just call yourNurb.ptrAsArray( ptr, size, arrays.GLfloatArray )
    with the size of data you expect.
    N_justOOR_vec3_vec4_texc                    s    fdd}|S )z#Just do OOR on the last argument...c                     s$   | d d  | d f }  |  S )N)originalObject)argsfunctionself Q/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/OpenGL/GLU/glunurbs.pygetOORh   s    z!GLUnurbs._justOOR.<locals>.getOORr   )r   r   r    r   r   r   r   f   s    zGLUnurbs._justOOR   c                    s    fdd}|S )zBConvert first arg to size-element array, do OOR on arg2 if presentc                     sD    | d tj}t| dkr8| d } ||S  |S d S )Nr      )Z
ptrAsArrayr   GLfloatArraylenr   )r   vecoorr   r   sizer   r   r%   n   s
    
zGLUnurbs._vec3.<locals>.vecr   )r   r   r(   r%   r   r'   r   r   l   s    zGLUnurbs._vec3c                 C   s   |  |dS )zSize-4 vector version...   )r   )r   r   r   r   r   r   v   s    zGLUnurbs._vec4c                    s    fdd}|S )zTexture coordinate callback

        NOTE: there is no way for *us* to tell what size the array is, you will
        get back a raw data-point, not an array, as you do for all other callback
        types!!!
        c                     s8   t | dkr(| d } | d |S  | d S d S )Nr"   r   )r$   r   )r   r&   r   r   r   r&      s    zGLUnurbs._tex.<locals>.oorr   )r   r   r&   r   r   r   r   y   s    zGLUnurbs._tex)r!   )"__name__
__module____qualname____doc__r
   ZfunctionTypeForr   ZFUNCTION_TYPECALLBACK_FUNCTION_REGISTRARS_simpleZGLU_NURBS_BEGINGLenumZGLU_NURBS_BEGIN_DATActypesPOINTERZGLvoidZGLU_NURBS_VERTEXZGLfloatZGLU_NURBS_VERTEX_DATAZGLU_NURBS_NORMALZGLU_NURBS_NORMAL_DATAZGLU_NURBS_COLORZGLU_NURBS_COLOR_DATAZGLU_NURBS_TEXTURE_COORDZGLU_NURBS_TEXTURE_COORD_DATAZGLU_NURBS_ENDZGLU_NURBS_END_DATAZGLU_NURBS_ERRORCALLBACK_TYPESZWRAPPER_METHODSr   r   r   r   r   r   r   r   r      sz   	+

r   c                 C   s6   t tj}tttj| g|_|jd | ks2J |S )z4Get gluNurbsCallback function with set last arg-typer   )	r   ZcopyBaseFunctionr/   r   r1   r2   r   r0   argtypes)funcTyperesultr   r   r   _callbackWithType   s    r7   r   c                 C   s   |  ||S )z,Dispatch to the nurb's addCallback operation)ZaddCallback)nurbwhichZCallBackFuncr   r   r   r      s    r   c                 C   s   |  }|d }|S )zAReturn a new nurbs renderer for the system (dereferences pointer)r   r   )baseFunctionZnewSetnewr   r   r   r      s    r   c                 C   s   | || |S z6Note the Python object for use as userData by the nurbZ
noteObjectr:   r8   ZuserDatar   r   r   r      s    
r      c                 C   sb   | dk rt d|| f nB| tkr:t d|t| f n$|d|  k r^t d||d|  f dS )zCheck that order is valid...r"   z%s should be 1 or more, is %sz%s should be %s or less, is %s   z<Knotcount must be at least 2x %s is %s should be at least %sN)r	   GLUError	MAX_ORDER)order	knotCountnamer   r   r   
checkOrder   s    
rF   c                 C   sB   t | r>| d }| dd D ] }||k rtd|||f qdS )z'Check that knots are in ascending orderr   r"   Nz"%s has decreasing knot %s after %s)r$   r	   rA   )knotsrE   Zknotnextr   r   r   
checkKnots   s    rI   c                 C   s   | || |S r<   r=   r>   r   r   r   r      s    
r   c           
   
   C   s   t j|}t j|}t j|}zt j|\}}W n. tyf } ztdW Y d}~n
d}~0 0 || }	tj	rt
|	|d t|d | ||||||	|S )zaPythonic version of gluNurbsCurve

    Calculates knotCount, stride, and order automatically
    z"Need a 2-dimensional control arrayNzorder of NURBS curvezknots of NURBS curver   r#   asArray	arraySize
dimensions
ValueErrorr	   rA   r   ZERROR_CHECKINGrF   rI   )
r:   r8   rG   controltyperD   lengthsteperrrC   r   r   r   r      s     
r   c                 C   s*  t j|}t j|}t j|}t j|}t j|}zt j|\}}	}
W n. ty } ztdW Y d}~n
d}~0 0 || }||	 }|	|
 }|
}tj	rt
||d t
||d t|d t|d || ||  ||	 ks
td|||||||||||| |||||||||||}|S )zcPythonic version of gluNurbsSurface

    Calculates knotCount, stride, and order automatically
    z"Need a 3-dimensional control arrayNzsOrder of NURBS surfaceztOrder of NURBS surfacezsKnots of NURBS surfaceztKnots of NURBS surfacezInvalid NURB structurerJ   )r:   r8   ZsKnotsZtKnotsrO   rP   Z
sKnotCountZ
tKnotCountrQ   widthrR   rS   ZsOrderZtOrderZsStrideZtStrider6   r   r   r   r      sB     



r   c                 C   s`   t j|}|tjkrd}n|tjkr,d}ntd| t j|}t|| }| |||||S )zgluPwlCurve -- piece-wise linear curve within GLU context

    data -- the data-array
    type -- determines number of elements/data-point
    r@   r!   zUnrecognised type constant: %s)	r   r#   rK   r/   ZGLU_MAP1_TRIM_2ZGLU_MAP_TRIM_3rN   rL   int)r:   r8   datarP   Zdivisorr(   r   r   r   r     s    

r   )-r-   Z
OpenGL.rawr   r/   ZOpenGLr   r   r   Z
OpenGL.GLUr   ZOpenGL.lazywrapperr   Z_lazyr   r	   r1   weakrefZOpenGL.platformr
   r   __all__Z	GLUStructr   r2   r   restyper7   r3   itemscr5   cbr.   r4   	NameErrorrS   r   r   rB   rF   rI   r   r   r   r   r   r   r   r   <module>   sN   k	







)