
    lj$@                    2   d dl mZ d dlmZ d dlmZ d dlmZ d dlZd dl	Z
d dlmZ d dlT d dlmZ d d	lmZmZ d dlT d d
lmZmZ d dlmZmZ d dlmZ d dlmZ erd dlmZ d dl m!Z!m"Z" ddgZ# G d de          Z$ G d de$          Z% G d de$          Z&dS )    )annotations)Iterable)Path)TYPE_CHECKINGN)Image)*)OpenGLMobject)integer_interpolateinterpolate)_Data	_Uniforms)change_to_rgba_arrayget_full_raster_image_path)listify)normalize_along_axis)Point3D_ArrayVector3D_ArrayOpenGLSurfaceOpenGLTexturedSurfacec                       e Zd ZdZdej        dfdej        dfdej        dfdej        dfgZdZd	d	d	d	d	ed	d
ddddde	j
        dd	fd$ fdZd Zd Zd Zd Zd%dZd&dZd'dZd ZefdZd  Zd! Zd" Zd# Z xZS )(r   a@  Creates a Surface.

    Parameters
    ----------
    uv_func
        The function that defines the surface.
    u_range
        The range of the ``u`` variable: ``(u_min, u_max)``.
    v_range
        The range of the ``v`` variable: ``(v_min, v_max)``.
    resolution
        The number of samples taken of the surface.
    axes
        Axes on which the surface is to be drawn. Optional
        parameter used when coloring a surface by z-value.
    color
        Color of the surface. Defaults to grey.
    colorscale
        Colors of the surface. Optional parameter used when
        coloring a surface by values. Passing a list of
        colors and an axes will color the surface by z-value.
        Passing a list of tuples in the form ``(color, pivot)``
        allows user-defined pivots where the color transitions.
    colorscale_axis
        Defines the axis on which the colorscale is applied
        (0 = x, 1 = y, 2 = z), default is z-axis (2).
    opacity
        Opacity of the surface from 0 being fully transparent
        to 1 being fully opaque. Defaults to 1.
    point   du_pointdv_pointcolor)   surfaceN   g      ?g333333?g?   gh㈵>TkwargsAnyc                   || _         ||nd| _        ||nd| _        ||nd| _        || _        || _        || _        || _        || _        d | _	         t                      j        d||	|
|||nd||d| |                                  d S )Nr   r    )e   r%   r   )r   opacityglossshadowshader_folderrender_primitive
depth_test )passed_uv_funcu_rangev_range
resolutionaxes
colorscalecolorscale_axisprefered_creation_axisepsilontriangle_indicessuper__init__compute_triangle_indices)selfuv_funcr.   r/   r0   r1   r   r2   r3   r&   r'   r(   r4   r5   r*   r+   r)   r!   	__class__s                     ^/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/mobject/opengl/opengl_surface.pyr8   zOpenGLSurface.__init__D   s    2 &")"5ww6")"5ww6 )3(>**J	$.&<#  $ 		
+8+D--)-!		
 		
 		
 		
 		
 	%%'''''    c                F    | j         r|                      ||          S ||dfS )Ng        )r-   )r:   uvs      r=   r;   zOpenGLSurface.uv_funcy   s/     	-&&q!,,,1c{r>   c                   	
  j         } j        \  }}t          j        g  j        |R  }t          j        g  j        |R  
g }d j        dfd j        ffD ]m\  	t          j        	
fd|D                       }t          j         fdd|          }|	                    |
                    ||z  |f                     n                     t          j        |                     d S )Nr   r   r   c                2    g | ]fd D             S )c                $    g | ]}z   |z   gS r,   r,   ).0rA   dudvr@   s     r=   
<listcomp>z8OpenGLSurface.init_points.<locals>.<listcomp>.<listcomp>   s&     C C Ca!b&!b&!1 C C Cr>   r,   )rF   r@   rG   rH   r/   s    @r=   rI   z-OpenGLSurface.init_points.<locals>.<listcomp>   s5    UUU C C C C C C7 C C CUUUr>   c                     j         |  S N)r;   )pr:   s    r=   <lambda>z+OpenGLSurface.init_points.<locals>.<lambda>   s    |t|Q7G r>   r   )dimr0   nplinspacer.   r/   r5   arrayapply_along_axisappendreshape
set_pointsvstack)r:   rN   nunvr.   point_listsuv_grid
point_gridrG   rH   r/   s   `       @@@r=   init_pointszOpenGLSurface.init_points   s   hB+0t|0R000+0t|0R000 a01dl2CD 	C 	CFBhUUUUUUWUUUVVG,-G-G-G-GGTTJz1127C.AABBBB
 		+../////r>   c                   | j         \  }}|dk    s|dk    r"t          j        dt                    | _        d S t          j        ||z                                ||f          }t          j        d|dz
  z  |dz
  z  t                    }|d dd df                                         |dd d<   |dd d df                                         |dd d<   |d ddd f                                         |dd d<   |d ddd f                                         |dd d<   |dd d df                                         |dd d<   |dd dd f                                         |d	d d<   || _        d S )
Nr   dtype   r    r   r   r      )r0   rO   zerosintr6   arangerT   flatten)r:   rW   rX   
index_gridindicess        r=   r9   z&OpenGLSurface.compute_triangle_indices   s    B77bAgg$&HQc$:$:$:D!FYrBw''//R99
(1Q<262#>>>"3B38,44661"2338,44661"3B38,44661"3B38,44661"2338,44661"2338,44661 'r>   c                    | j         S rK   )r6   r:   s    r=   get_triangle_indicesz"OpenGLSurface.get_triangle_indices   s    $$r>   return2tuple[Point3D_Array, Point3D_Array, Point3D_Array]c                z    | j         }t          |          dz  }|d |         ||d|z           |d|z  d          fS )Nr   r   )pointslen)r:   ro   ks      r=   $get_surface_points_and_nudged_pointsz2OpenGLSurface.get_surface_points_and_nudged_points   sI     KK1bqbz6!a!e),fQUWWo==r>   r   c                    |                                  \  }}}t          j        ||z
  | j        z  ||z
  | j        z            }t	          |d          S )Nr    )rr   rO   crossr5   r   )r:   s_points	du_points	dv_pointsnormalss        r=   get_unit_normalszOpenGLSurface.get_unit_normals   s[    )-)R)R)T)T&)Y(!T\1!T\1
 
 $GQ///r>   c           
     @    t          |t                    sJ  j        dk    rdk    r                     |            S |j        \                       t          j         fd|                                D                                   S )Nr   r    c           
     n    g | ]1}                     |                                d f          2S )r   )axis)get_partial_points_arraycopy)rF   arrar|   brW   rX   r:   s     r=   rI   z:OpenGLSurface.pointwise_become_partial.<locals>.<listcomp>   s]     	 	 	  11

R! 2  	 	 	r>   )	
isinstancer   r4   match_pointsr0   rU   rO   rV   rr   )r:   smobjectr   r   r|   rW   rX   s   ` ```@@r=   pointwise_become_partialz&OpenGLSurface.pointwise_become_partial   s    (M22222<.D66a1ffh'''K$BI	 	 	 	 	 	 	 	 	  (LLNN	 	 	 	
 	
 	
 r>   c                   t          |          dk    r|S |d d         \  }}|                    |          }||         dz
  }t          d||          \  }	}
t          d||          \  }}|dk    rNt          ||	         ||	dz            |
          |d |	<   t          ||         ||dz            |          ||dz   d <   n|d|d         f}t          |d d |	f         |d d |	dz   f         |
                              |          |d d d |	f<   t          |d d |f         |d d |dz   f         |                              |          |d d |dz   d f<   |                    ||z  g|dd          R           S )Nr   r   r    )rp   rT   r
   r   )r:   ro   r   r   r0   r|   rW   rX   	max_indexlower_indexlower_residueupper_indexupper_residueshapes                 r=   r}   z&OpenGLSurface.get_partial_points_array   s   v;;!MBQBB
++t$q(	%8Iq%I%I"]%8Iq%I%I"]199#.{#{Q'$ $F<K< 
 )4{#{Q') )F;?$$%% JqM*E&1qqq+~&qqq+/)*' ' genn	 111l{l?#
 ,7qqq+~&qqq+/)*, , genn	 111kAo'''(
 ~~rBw8ABB88999r>   c                   | j         t          t          t                    dz                      }| j        fd}|                    |           t          d          D ]}|d d         |         |d d<   | S )Nr   c                L    t          j        d| z                              S Nr   )rO   dot)indexro   tri_isvects    r=   	index_dotz9OpenGLSurface.sort_faces_back_to_front.<locals>.index_dot   s#    6&E	!23T:::r>   )key)r6   listrangerp   ro   sort)r:   r   rh   r   rq   ro   r   s    `   @@r=   sort_faces_back_to_frontz&OpenGLSurface.sort_faces_back_to_front   s    &uS[[A-..//	; 	; 	; 	; 	; 	; 	; 	###q 	1 	1A!!$Q$<0F14a4LLr>   c                X   |                                  \  }}}t          j        t          |          | j                  }d| j        vr`||d<   ||d<   ||d<   | j        r5t          | d          s|                     |          | _	        | j	        |d<   n| 
                    |           |S )zCalled by parent Mobject to calculate and return
        the shader data.

        Returns
        -------
        shader_dtype
            An array containing the shader data (vertices and
            color of each vertex)
        r^   ro   r   r   r   color_by_valr   )rr   rO   rc   rp   shader_dtypelocked_data_keysr2   hasattr_get_color_by_valuer   fill_in_shader_color_info)r:   ru   rv   rw   shader_datas        r=   get_shader_datazOpenGLSurface.get_shader_data   s     *.)R)R)T)T&)Yhs8}}D4EFFF4000#+K &/K
#&/K
# <t^44 K(,(@(@(J(JD%'+'8G$$..{;;;r>   c                4    |                      |dd           |S )aL  Fills in the shader color data when the surface
        is all one color.

        Parameters
        ----------
        shader_data
            The vertices of the surface.

        Returns
        -------
        shader_dtype
            An array containing the shader data (vertices and
            color of each vertex)
        r   rgbasread_data_to_shaderr:   r   s     r=   r   z'OpenGLSurface.fill_in_shader_color_info  s"     	  gw???r>   c                   t          | j        d                   t          t          fv r&d | j        D             d | j        D             g\  }}n]| j        }| j        j        d         }| j        j        d         }||z
  t          |          dz
  z  }t          j        |||z   |          }g }|D ]D}| j        	                    |          | j
                 }	|	|d         k    r/|                    t          |d         | j                             c|	|d         k    r/|                    t          |d         | j                             t          |          D ]n\  }
}||	k    rc|	||
dz
           z
  ||
         ||
dz
           z
  z  }t          t!          |d          d          }t#          ||
dz
           ||
         |          } no|                    t          || j                             F|S )a
  Matches each vertex to a color associated to it's z-value.

        Parameters
        ----------
        s_points
           The vertices of the surface.

        Returns
        -------
        List
            A list of colors matching the vertex inputs.
        r   c                    g | ]\  }}|S r,   r,   rF   ijs      r=   rI   z5OpenGLSurface._get_color_by_value.<locals>.<listcomp>9      ///tq!///r>   c                    g | ]\  }}|S r,   r,   r   s      r=   rI   z5OpenGLSurface._get_color_by_value.<locals>.<listcomp>:  r   r>   r    )startstopstepra   )typer2   r   tupler1   z_rangerp   rO   re   point_to_coordsr3   rS   color_to_rgbar&   	enumeratemaxmininterpolate_color)r:   ru   
new_colorspivots	pivot_min	pivot_maxpivot_frequencyreturn_colorsr   
axis_valuer   pivotcolor_index
temp_colors                 r=   r   z!OpenGLSurface._get_color_by_value*  s$    "##e}44//t/////t///"J
 J	)!,I	)!,I(94Z19LMOY0$  F  	N 	NE22599$:NOJVAY&&$$]:a=$,%O%OPPPPvbz))$$]:b>4<%P%PQQQQ )& 1 1  HAuz))'1F1q5M'A"1Iq1u5' '*#k1*=*=q&A&A%6&q1u-&qM'& &

  * $$]:t|%L%LMMMMr>   c                *    |                                  S rK   )rk   rj   s    r=   get_shader_vert_indicesz%OpenGLSurface.get_shader_vert_indices`  s    ((***r>   )r!   r"   )rl   rm   )rl   r   rK   )__name__
__module____qualname____doc__rO   float32r   r)   GREYmoderngl	TRIANGLESr8   r;   r\   r9   rk   rr   ry   r   r}   OUTr   r   r   r   r   __classcell__r<   s   @r=   r   r      s        @ 
"*d#	RZ&	RZ&	"*d#	L M    !+-3( 3( 3( 3( 3( 3( 3(j  0 0 0*( ( ($% % %> > > >0 0 0 0   2: : :B -0      0  $4 4 4l+ + + + + + +r>   c                  *     e Zd Zdd fd
Zd Z xZS )OpenGLSurfaceGroupN)r0   c               h    |dn|| _          t                      j        ddd i|  | j        |  d S )NrC   r;   r,   )r0   r7   r8   add)r:   r0   parametric_surfacesr!   r<   s       r=   r8   zOpenGLSurfaceGroup.__init__e  sK    $.$6&&J000000%&&&&r>   c                    d S rK   r,   rj   s    r=   r\   zOpenGLSurfaceGroup.init_pointsj  s    r>   )r   r   r   r8   r\   r   r   s   @r=   r   r   d  sT        8< ' ' ' ' ' ' '
      r>   r   c                       e Zd Zdej        dfdej        dfdej        dfdej        dfdej        dfgZd	Z e            Z e            Z	 e
            Z	 	 	 d"d# fdZd$dZ fdZd Zd Zd%dZd fd 	Zd! Z xZS )&r   r   r   r   r   	im_coords)r   r&   )r    textured_surfaceNRGBA
uv_surfacer   
image_filestr | Path | npt.NDArraydark_image_file
str | Path
image_modestr | Iterable[str]r)   c                4   i | _         t          |t                    st          d          t          |t          j                  rt          |          }t          |t          t          f          r|gdz  }|\  }}| 	                    ||          | 	                    |p||          d}	|rd| _
        || _        |j        | _        |j        | _        |j        | _        |j        | _        | j        j        | _         t#                      j        dd|	i| d S )Nz(uv_surface must be of type OpenGLSurfacer   )LightTextureDarkTexturetexture_pathsr,   )uniformsr   r   	ExceptionrO   ndarrayr   strr   get_image_from_filenum_texturesr   r;   r.   r/   r0   r'   r7   r8   )r:   r   r   r   r   r)   r!   image_mode_lightimage_mode_darkr   r<   s             r=   r8   zOpenGLTexturedSurface.__init__{  s8    *m44 	HFGGGj"*-- 	:-j99J j3+.. 	*$)J,6)/ 44    33-: 	
 	
  	" !D$!)!)!)$/_*
??}??????r>   r   rl   Image.Imagec                n    t          |          }t          j        |                              |          S rK   )r   r   openconvert)r:   r   r   s      r=   r   z)OpenGLTexturedSurface.get_image_from_file  s/    
 0
;;
z*%%--j999r>   c                    t                                                       t          j        d          | _        t          j        d          | _        d S )N)r   r   r$   )r7   	init_datarO   rc   r   r&   )r:   r<   s    r=   r   zOpenGLTexturedSurface.init_data  s?    &))x''r>   c                    | j         j        \  }|                     | j         j                   t	          j        fdt	          j        dd|          D                       | _        d S )Nc                J    g | ]}t          j        d d          D ]}||g S )r    r   )rO   rP   )rF   r@   rA   rX   s      r=   rI   z5OpenGLTexturedSurface.init_points.<locals>.<listcomp>  sR       Q2..   A   r>   r   r    )r   r0   rU   ro   rO   rQ   rP   r   )r:   rW   rX   s     @r=   r\   z!OpenGLTexturedSurface.init_points  ss    +B.///   Q2..  
 
r>   c                b    t          j        | j        j        d d df         g          | _        d S r   )rO   rQ   r   r   r&   rj   s    r=   init_colorsz!OpenGLTexturedSurface.init_colors  s,    x!6qqq!t!< =>>r>   Tc                    |                      |          D ]2}t          j        d t          |          D                       |_        3| S )Nc                    g | ]}|gS r,   r,   )rF   os     r=   rI   z5OpenGLTexturedSurface.set_opacity.<locals>.<listcomp>  s    #B#B#BAQC#B#B#Br>   )
get_familyrO   rQ   r   r&   )r:   r&   recursemobs       r=   set_opacityz!OpenGLTexturedSurface.set_opacity  sN    ??7++ 	D 	DC(#B#B1A1A#B#B#BCCCKKr>   r    c                    t                                          ||||           | j        }|j        |d d <   |dk    r|dk    r| S |j        \  }}|                     |||||df|          |d d <   | S )Nr   r    r   )r7   r   r   r0   r}   )	r:   	tsmobjectr   r   r|   r   rW   rX   r<   s	           r=   r   z.OpenGLTexturedSurface.pointwise_become_partial  s    ((Aq$???N	 *	!!!66a1ffK%B44Y1r2qkSWXX	!!!r>   c                b    |                      |dd           |                      |dd           |S )Nr&   r   r   r   s     r=   r   z/OpenGLTexturedSurface.fill_in_shader_color_info  s8      iCCC  k;GGGr>   )Nr   N)
r   r   r   r   r   r   r   r   r)   r   )r   r   r   r   rl   r   )T)r   r   r   rO   r   r   r)   r   r   r&   r   r   r8   r   r   r\   r   r   r   r   r   r   s   @r=   r   r   n  sL       	"*d#	RZ&	RZ&	bj$'	BJ%L 'MIeggG9;;L '+*0$('@ '@ '@ '@ '@ '@ '@R: : : :( ( ( ( (
	
 	
 	
? ? ?   
           r>   )'
__future__r   collections.abcr   pathlibr   typingr   r   numpyrO   PILr   manim.constants#manim.mobject.opengl.opengl_mobjectr	   manim.utils.bezierr
   r   manim.utils.colormanim.utils.config_opsr   r   manim.utils.imagesr   r   manim.utils.iterablesr   manim.utils.space_opsr   numpy.typingnptmanim.typingr   r   __all__r   r   r   r,   r>   r=   <module>r     s   " " " " " " $ $ $ $ $ $                                  = = = = = = ? ? ? ? ? ? ? ?     3 3 3 3 3 3 3 3 O O O O O O O O ) ) ) ) ) ) 6 6 6 6 6 6 ;::::::::3
4E+ E+ E+ E+ E+M E+ E+ E+P
       c c c c cM c c c c cr>   