
    Mi0                     n    S r SSKrSSKJr  Su  rrrS rSS jr	SS jr
SS	 jrS
 rSS jrS rS rS rg)z]
rendering.py
--------------

Functions to convert trimesh objects to pyglet/opengl objects.
    N   )util)r   r      c                    [         R                  " U S5      (       a  [        U 40 UD6$ [         R                  " U S5      (       a  [        U 40 UD6$ [         R                  " U S5      (       a"  [	        U R
                  4SU R                  0UD6$ [         R                  " U S5      (       a  [	        U 40 UD6$ [         R                  " U S5      (       a  [        U R                  " S0 UD640 UD6$ [        S5      e)	a+  
Try to convert various geometry objects to the constructor
args for a pyglet indexed vertex list.

Parameters
------------
obj : Trimesh, Path2D, Path3D, (n,2) float, (n,3) float
  Object to render

Returns
------------
args : tuple
  Args to be passed to pyglet indexed vertex list
  constructor.
TrimeshPath
PointCloudcolorsndarray	VoxelGridz'Geometry passed is not a viewable type! )	r   is_instance_namedmesh_to_vertexlistpath_to_vertexlistpoints_to_vertexlistverticesr
   as_boxes
ValueError)geometrykwargss     k/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/rendering.pyconvert_to_vertexlistr      s      h	22!(5f55			&	1	1 "(5f55			,	7	7#H$5$5XhooXQWXX			)	4	4#H777			+	6	6!("3"3"=f"=HHHBCC    c           	         [        U R                  R                  5      S:X  ag  U R                  R                  S   S:X  aJ  [        R                  " U R                  [        R
                  " [        U R                  5      5      45      nOU R                  n[        U R                  S5      (       Ga   [        U5      nU R                  nU R                  nU R                  R                  nU R                  R                  n	[        U	S5      (       a  U	R                  SL n
O"[        U	S5      (       a  U	R                  SL n
OSn
Ub  U
(       d  [        U5      U:w  a  [        U	R                  U5      nGOUR                  S   S:  a  USS2SS24   nSUR!                  [        R"                  5      R%                  S	5      R'                  5       4nGO+U(       au  [        U R                  5      U:  a\  U R(                  nUR                  n[        U5      nUR                  nUR                  n[        UR                  R*                  U5      nO[        U R                  5      S
-  n[        R,                  " U R.                  S5      nX@R                     n[        R0                  " U[        R2                  S9n[        R,                  " U R                  R4                  S5      R%                  S5      n[        X5      nU[6        UUR%                  S	5      R'                  5       SUR%                  S	5      R'                  5       4SUR%                  S	5      R'                  5       4U4nU$ )a  
Convert a Trimesh object to arguments for an
indexed vertex list constructor.

Parameters
-------------
mesh : trimesh.Trimesh
  Mesh to be rendered
group : str
  Rendering group for the vertex list
smooth : bool
  Should we try to smooth shade the mesh
smooth_threshold : int
  Maximum number of faces to smooth shade

Returns
--------------
args : (7,) tuple
  Args for vertex list constructor

   r   uvimageNbaseColorTextureTz
t2f/static   )r   r    dtype)r   r   
v3f/staticz
n3f/static)lenr   shapenpcolumn_stackzeroshasattrvisualvertex_normalsfacesr   materialr   r   colors_to_gl
main_colorastypefloat64reshapetolistsmooth_shadedvertex_colorstileface_normalsarangeint64face_colorsGL_TRIANGLES)meshgroupsmoothsmooth_thresholdr   vertex_countnormalsr,   r   r-   no_imagecolor_glr
   argss                 r   r   r   4   s   . 4==1$)<)<Q)?1)D??DMM288C<N3O#PQ==t{{D!!8}%%

 [[^^ ;;''8W%%~~-HX12200D8HH :SW%< $H$7$7FH xx{Q2A2Y$bii

&;&C&CB&G&N&N&PQH	C

O&66 ##??8}'' ; ;\J 4::*''$++V4JJ'		,bhh700&9AA'J5
 	b  "	x''+2245	wr*1134D Kr   c                 d   U R                   nU R                   Vs/ s H(  n[        R                  " UR	                  U5      5      PM*     nn[        R
                  " U5      n[        U5      n[        R                  " US5      (       aF  UR                  S5      n[        R                  " U[        R                  " [        U5      5      45      n[        R                  " U5      R                  5       nU R                  n	U	b}  [        R                  " [!        XPR                  5       V
Vs/ s HF  u  p[        R"                  " [        U
5      S45      U-  R%                  [        R&                  5      PMH     snn
5      n	[)        XS9nU[*        UUSUR                  S5      4U4nU$ s  snf s  snn
f )a%  
Convert a Path3D object to arguments for a
pyglet indexed vertex list constructor.

Parameters
-------------
path : trimesh.path.Path3D object
  Mesh to be rendered
group : str
  Rendering group for the vertex list

Returns
--------------
args : (7,) tuple
  Args for vertex list constructor
r   r   r   )countr#   r   )r   entitiesr   stack_linesdiscretevstack_emptyr$   is_shaper2   r&   r'   r(   r8   r3   r
   vstackziponesr0   uint8r.   GL_LINES)pathr=   r   r   estackedlinesrG   indexr
   sc	gl_colorsrD   s                 r   r   r      sb   $ }}H @D}}M}!t

8 45}GMg&EJE }}Xw''g&U(< =>IIe##%E [[F  55DA #a&!%)11"((;5
 V1I 		u}}R()D KA Ns   /F'*AF,
c                 
   [         R                  " U [         R                  S9n [        R                  " U S5      (       a6  [         R
                  " U [         R                  " [        U 5      5      45      n O'[        R                  " U S5      (       d  [        S5      e[         R                  " [        U 5      5      R                  5       n[        U 5      [        UUSU R                  S5      4[        U[        U 5      5      4nU$ )aJ  
Convert a numpy array of 3D points to args for
a vertex list constructor.

Parameters
-------------
points : (n, 3) float
  Points to be rendered
colors : (n, 3) or (n, 4) float
  Colors for each point
group : str
  Rendering group for the vertex list

Returns
--------------
args : (7,) tuple
  Args for vertex list constructor
r!   rF   )r   r    zPointcloud must be (n,3)!r#   r   )r&   
asanyarrayr1   r   rL   r'   r(   r$   r   r8   r3   	GL_POINTSr2   r.   )pointsr
   r=   r   rV   rD   s         r   r   r      s    & ]]64F}}VW%%&"((3v;*?!@A]]67++455IIc&k"))+E 	F	v~~b)*VS[)D Kr   c                    [         R                  " U 5      n [        U5      nSSSS.nU R                  R                  U;   a  X R                  R                     nOSnUbT  [
        R                  " XS45      (       a7  SU R                  S    U S3nU R                  S	5      R                  5       nXE4$ Ubo  U R                  S
;   a_  [         R                  " XR                  4U R                  S9U -  R                  S	5      R                  5       nSU R                   U S3nXE4$ [         R                  " / SQUS45      R                  S	5      R                  5       nSnXE4$ )a  
Given a list of colors (or None) return a GL-acceptable
list of colors.

Parameters
------------
colors: (count, (3 or 4)) float
  Input colors as an array

Returns
---------
colors_type : str
  Color type
colors_gl : (count,) list
  Colors to pass to pyglet
fB)r_   iuN)r    r   rX   r   z/staticr   ))r    )r   r!   )        rc   rc   z
c3f/static)r&   r[   intr"   kindr   rL   r%   r2   r3   rO   sizer6   )r
   rG   colors_dtypesr"   colors_typerY   s         r   r.   r.      sH   $ ]]6"FJECc2M||M)ll//0T]]66?CC&,,q/*5'9NN2&--/	" !!! 
	v|||; WWe[[)>GWR[VX 	 &++ugW5 !!	 GGOeQZ8@@DKKM	"!!r   c                    SSK n[        U S5      (       a  U R                  nO[        U S5      (       a  U R                  nOgUc  gU(       a  SSKJn  U" U5      n[        R                  " 5        nUR                  USS9  UR                  S5        UR                  R                  S	US
9nSSS5        WR                  5       nU$ ! , (       d  f       N = f)a  
Convert a trimesh.visual.texture.Material object into
a pyglet-compatible texture object.

Parameters
--------------
material : trimesh.visual.texture.Material
  Material to be converted
upsize: bool
  If True, will upscale textures to their nearest power
  of two resolution to avoid weirdness

Returns
---------------
texture : pyglet.image.Texture
  Texture loaded into pyglet form
r   Nr   r   r   )power_resizepng)formatz.png)filenamefile)pygletr)   r   r   visual.texturerj   r   BytesIOsaveseekloadget_texture)r-   upsizero   imgrj   r_   gl_imagetextures           r   material_to_texturerz   %  s    $  x!!nn	-	.	.'' { 03 
15!	q	<<$$f1$= 
 ""$GN 
s   0<C
Cc                     SSK Jn  UR                  S-  " [        R                  " U [        R
                  S9R                  R                  5       6 $ )a  
Convert a numpy row-major homogeneous transformation matrix
to a flat column-major GLfloat transformation.

Parameters
-------------
matrix : (4,4) float
  Row-major homogeneous transform

Returns
-------------
glmatrix : (16,) gl.GLfloat
  Transform in pyglet format
r   gl   r!   )ro   r}   GLfloatr&   arrayfloat32Travel)matrixr}   s     r   matrix_to_glr   Z  s:      JJObhhvRZZ@BBHHJKKr   c                     SSK Jn  [        R                  " U 5      n [	        U5      S:  a  [        R
                  " X5      n UR                  [	        U 5      -  " U 6 nU$ )zO
Convert an array and an optional set of args into a
flat vector of gl.GLfloat
r   r|   )ro   r}   r&   r   r$   appendr   )r   rD   r}   vectors       r   vector_to_glr   o  sI    
 HHUOE
4y1}		%&jj3u:%.FMr   c                 .   SSK Jn  [        U R                  R	                  [
        R                  5      S-  5      n[        U5      S:X  d   e[        USS2S4   5      nX#R                  U4X#R                  U4X#R                  U4X#R                  U4/nU$ )a  
Convert trimesh.scene.lighting.Light objects into
args for gl.glLightFv calls

Parameters
--------------
light : trimesh.scene.lighting.Light
  Light object to be converted to GL
transform : (4, 4) float
  Transformation matrix of light
lightN : int
  Result of gl.GL_LIGHT0, gl.GL_LIGHT1, etc

Returns
--------------
multiarg : [tuple]
  List of args to pass to gl.glLightFv eg:
  [gl.glLightfb(*a) for a in multiarg]
r   r|   g     o@r   Nr    )ro   r}   r   colorr0   r&   r1   r$   GL_POSITIONGL_SPECULAR
GL_DIFFUSE
GL_AMBIENT)light	transformlightNr}   gl_colorgl_positionrD   s          r   light_to_glr   }  s    (  EKK..rzz:UBCHx=A y!Q/0K 
-	*	)	)	D Kr   )NTi`  )N)NN)T)__doc__numpyr&    r   r\   rQ   r;   r   r   r   r   r.   rz   r   r   r   r   r   r   <module>r      sV      %. !	8\ DF[|5p$N1"h2jL*$r   