
    ljP                    n   d Z ddlmZ ddgZddlZddlZddlZddl	Z	ddl
mZmZ ddlmZ ddlmZmZmZ ddlZddlZdd	lmZ dd
lmZmZ ddlT ddlmZ ddlm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+ ddl,m-Z- erddl.mZ/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 e:j;        de:j<        ej=        j<        e:j>        ej=        j>        e:j?        ej=        j?        iZ@eAj;        deAj<        ejB        j<        eAjC        ejB        jC        eAjD        ejB        jD        iZE G d d          ZF G d d          ZGdS )zLA camera converts the mobjects contained in a Scene into an array of pixels.    )annotationsCamera"BackgroundColoredVMobjectDisplayerN)CallableIterable)reduce)TYPE_CHECKINGAnySelf)Image)configlogger)*)Mobject)PMobject)VMobject)
ManimColorParsableManimColorcolor_to_int_rgba)extract_mobject_family_members)get_full_raster_image_path)list_difference_update)cross2dAbstractImageMobject)FloatRGBA_ArrayFloatRGBALike_Array
ManimFloatManimInt
PixelArrayPoint3DPoint3D_Arrayc                  >   e Zd ZdZdedddddddddddddfdd%Zdd'Zedd)            Zej	        dd+            Zedd,            Z
e
j	        dd.            Z
dd2Zdd5Zddd8Zdd9Z	 ddd=Z	 dddBZ	 dddCZ	 dddDZddGZddHZddJZddKZ	 	 dddRZddSZddTZddUZddWZddZZdd[Zdd^Zdd`ZddcZ dddZ!ddgZ"ddhZ#	 dddiZ$	 dddkZ%ddlZ&ddnZ'ddpZ(ddsZ)ddyZ*dd|Z+ddZ,ddZ-ddZ.ddZ/ddZ0ddZ1ddZ2ddZ3ddZ4ddZ5ddZ6ddZ7dS )r   av  Base camera class.

    This is the object which takes care of what exactly is displayed
    on screen at any given moment.

    Parameters
    ----------
    background_image
        The path to an image that should be the background image.
        If not set, the background is filled with :attr:`self.background_color`
    background
        What :attr:`background` is set to. By default, ``None``.
    pixel_height
        The height of the scene in pixels.
    pixel_width
        The width of the scene in pixels.
    kwargs
        Additional arguments (``background_color``, ``background_opacity``)
        to be set.
    NRGBA   uint8g{Gz?Tbackground_image
str | Noneframe_centerr!   
image_modestr
n_channelsintpixel_array_dtypecairo_line_width_multiplefloatuse_z_indexbool
backgroundPixelArray | Nonepixel_height
int | Nonepixel_widthframe_heightfloat | Noneframe_width
frame_ratebackground_colorParsableManimColor | Nonebackground_opacitykwargsr
   returnNonec                   || _         || _        || _        || _        || _        || _        || _        || _        d | _        |	t          d         }	|	| _
        |
t          d         }
|
| _        |t          d         }|| _        |t          d         }|| _        |t          d         }|| _        |%t          j        t          d                   | _        nt          j        |          | _        |t          d         | _        n|| _        t          d         | _        t)          j        | j                  j        | _        i | _        |                                  |                                  |                                  d S )Nr5   r7   r8   r:   r;   r<   r>   )r'   r)   r*   r,   r.   r/   r1   r3   %background_colored_vmobject_displayerr   r5   r7   r8   r:   r;   r   parse_background_color_background_opacitymax_allowable_normnpiinfomaxrgb_max_valpixel_array_to_cairo_contextinit_backgroundresize_frame_shapereset)selfr'   r)   r*   r,   r.   r/   r1   r3   r5   r7   r8   r:   r;   r<   r>   r?   s                    N/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/camera/camera.py__init__zCamera.__init__S   s   & !1($$!2)B&&$  	2 !.1L( /K&!.1L( /K&-J$#1;1A)*2 2D"" &0%56F%G%GD"%.45I.JD$$'9D$ #)"78D$:;;?FH) 	!!!

    memoc                8    d | _         t          j        |           S N)canvascopy)rP   rT   s     rQ   __deepcopy__zCamera.__deepcopy__   s     yrS   r   c                    | j         S rV   )rE   rP   s    rQ   r<   zCamera.background_color   s    %%rS   colorc                <    || _         |                                  d S rV   )rE   rM   )rP   r\   s     rQ   r<   zCamera.background_color   s"    !&rS   c                    | j         S rV   )rF   r[   s    rQ   r>   zCamera.background_opacity   s    ''rS   alphac                <    || _         |                                  d S rV   )rF   rM   )rP   r_   s     rQ   r>   zCamera.background_opacity   s"    #( rS   mobjectr   Ltype[VMobject] | type[PMobject] | type[AbstractImageMobject] | type[Mobject]c                    ddl m} t          | j        t          | j        || j        t          d i| _        | j        D ]}t          ||          r|c S t          d| d          )aI  Return the type of mobject, if it is a type that can be rendered.

        If `mobject` is an instance of a class that inherits from a class that
        can be rendered, return the super class.  For example, an instance of a
        Square is also an instance of VMobject, and these can be rendered.
        Therefore, `type_or_raise(Square())` returns True.

        Parameters
        ----------
        mobject
            The object to take the type of.

        Notes
        -----
        For a list of classes that can currently be rendered, see :meth:`display_funcs`.

        Returns
        -------
        Type[:class:`~.Mobject`]
            The type of mobjects, if it can be rendered.

        Raises
        ------
        :exc:`TypeError`
            When mobject is not an instance of a class that can be rendered.
           r   c                    | S rV    )batchpas     rQ   <lambda>z&Camera.type_or_raise.<locals>.<lambda>   s    u rS   zDisplaying an object of class z is not supported)mobject.types.image_mobjectr   r   $display_multiple_vectorized_mobjectsr   %display_multiple_point_cloud_mobjectsdisplay_multiple_image_mobjectsr   display_funcs
isinstance	TypeError)rP   ra   r   _types       rQ   type_or_raisezCamera.type_or_raise   s    : 	GFFFFF
 d?d@ $"F,,	
 	 ' 	 	E'5)) QQQQRRRrS   
new_height	new_widthc                    || _         || _        |                                  |                                  |                                  dS )a/  This method resets the height and width
        of a single pixel to the passed new_height and new_width.

        Parameters
        ----------
        new_height
            The new height of the entire scene in pixels
        new_width
            The new width of the entire scene in pixels
        N)r7   r5   rM   rN   rO   )rP   rs   rt   s      rQ   reset_pixel_shapezCamera.reset_pixel_shape   sI     %&!!!

rS   r   fixed_dimensionc                    | j         }| j        }| j        }| j        }||z  }|dk    r||z  }n||z  }|| _        || _        dS )a  
        Changes frame_shape to match the aspect ratio
        of the pixels, where fixed_dimension determines
        whether frame_height or frame_width
        remains fixed while the other changes accordingly.

        Parameters
        ----------
        fixed_dimension
            If 0, height is scaled with respect to width
            else, width is scaled with respect to height.
        r   N)r5   r7   r8   r:   )rP   rw   r5   r7   r8   r:   aspect_ratios          rQ   rN   zCamera.resize_frame_shape   se     (&(&"\1a&5LL&5K(&rS   c                   | j         }| j        }| j        t          | j                  }t	          j        |                              | j                  }t          j	        |          d|d|f         | _
        | j
                            | j                  | _
        dS t          | j        | j                  }t          j        ||| j        f| j                  | _
        || j
        ddddf<   dS )zInitialize the background.
        If self.background_image is the path of an image
        the image is set as background; else, the default
        background color fills the background.
        Ndtype)r5   r7   r'   r   r   openconvertr*   rH   arrayr3   astyper.   r   r<   r>   zerosr,   )rP   heightwidthpathimagebackground_rgbas         rQ   rM   zCamera.init_background  s     "  ,-d.CDDDJt$$,,T_==E !huoogvgvvo>DO"o44T5KLLDOOO/%' O !h0,  DO %4DOAAAqqqD!!!rS   pixel_array PixelArray | list | tuple | NoneImage.Imagec                J    || j         }t          j        || j                  S )ag  Returns an image from the passed
        pixel array, or from the current frame
        if the passed pixel array is none.

        Parameters
        ----------
        pixel_array
            The pixel array from which to get an image, by default None

        Returns
        -------
        PIL.Image.Image
            The PIL image of the array.
        Nmode)r   r   	fromarrayr*   rP   r   s     rQ   	get_imagezCamera.get_image-  s)    " *K{AAAArS   FPixelArray | list | tupleconvert_from_floatsr    c                f     t          j        |          }|rt          j         fdd|          }|S )a  Converts a pixel array from values that have floats in then
        to proper RGB values.

        Parameters
        ----------
        pixel_array
            Pixel array to convert.
        convert_from_floats
            Whether or not to convert float values to ints, by default False

        Returns
        -------
        np.array
            The new, converted pixel array.
        c                H    | j         z                      j                  S rV   )rK   r   r.   )frP   s    rQ   ri   z,Camera.convert_pixel_array.<locals>.<lambda>W  s     1t//778NOO rS   rd   )rH   r   apply_along_axis)rP   r   r   retvals   `   rQ   convert_pixel_arrayzCamera.convert_pixel_arrayB  sG    $ +&& 	(OOOO F
 rS   c                    |                      ||          }t          | d          r| j        j        |j        k    s	|| _        dS |ddddddf         | j        ddddddf<   dS )aH  Sets the pixel array of the camera to the passed pixel array.

        Parameters
        ----------
        pixel_array
            The pixel array to convert and then set as the camera's pixel array.
        convert_from_floats
            Whether or not to convert float values to proper RGB values, by default False
        r   N)r   hasattrr   shape)rP   r   r   converted_arrays       rQ   set_pixel_arrayzCamera.set_pixel_array]  s     '+&>&>,'
 '
 D-((	A &/*???+:D )8111aaa(@DQQQ111W%%%rS   c                <    |                      ||          | _        dS )aP  Sets the background to the passed pixel_array after converting
        to valid RGB values.

        Parameters
        ----------
        pixel_array
            The pixel array to set the background to.
        convert_from_floats
            Whether or not to convert floats values to proper RGB valid ones, by default False
        N)r   r3   )rP   r   r   s      rQ   set_backgroundzCamera.set_backgroundu  s      22;@STTrS   coords_to_colors_func"Callable[[np.ndarray], np.ndarray]c                    t          j        d           |                                 }t          j        |d|          }t          j        d           |                     |d          S )a  
        Makes a pixel array for the background by using coords_to_colors_func to determine each pixel's color. Each input
        pixel's color. Each input to coords_to_colors_func is an (x, y) pair in space (in ordinary space coordinates; not
        pixel coordinates), and each output is expected to be an RGBA array of 4 floats.

        Parameters
        ----------
        coords_to_colors_func
            The function whose input is an (x,y) pair of coordinates and
            whose return values must be the colors for that point

        Returns
        -------
        np.array
            The pixel array which can then be passed to set_background.
        zStarting set_backgroundrd   zEnding set_backgroundT)r   )r   infoget_coords_of_all_pixelsrH   r   r   )rP   r   coordsnew_backgrounds       rQ   make_background_from_funcz Camera.make_background_from_func  se    & 	-.....00,-BAvNN+,,,''D'QQQrS   c                V    |                      |                     |                     dS )a!  
        Sets the background to a pixel array using coords_to_colors_func to determine each pixel's color. Each input
        pixel's color. Each input to coords_to_colors_func is an (x, y) pair in space (in ordinary space coordinates; not
        pixel coordinates), and each output is expected to be an RGBA array of 4 floats.

        Parameters
        ----------
        coords_to_colors_func
            The function whose input is an (x,y) pair of coordinates and
            whose return values must be the colors for that point
        N)r   r   )rP   r   s     rQ   set_background_from_funczCamera.set_background_from_func  s-     	D::;PQQRRRRRrS   r   c                :    |                      | j                   | S )zResets the camera's pixel array
        to that of the background

        Returns
        -------
        Camera
            The camera object after setting the pixel array.
        )r   r3   r[   s    rQ   rO   zCamera.reset  s     	T_---rS   c                0    |                      |           d S rV   )r   )rP   r3   s     rQ   set_frame_to_backgroundzCamera.set_frame_to_background  s    Z(((((rS   mobjectsIterable[Mobject]include_submobjectsexcluded_mobjectslist | Nonelist[Mobject]c                    |r?t          || j        d          }|r&t          || j                  }t          ||          }t          |          S )a  Used to get the list of mobjects to display
        with the camera.

        Parameters
        ----------
        mobjects
            The Mobjects
        include_submobjects
            Whether or not to include the submobjects of mobjects, by default True
        excluded_mobjects
            Any mobjects to exclude, by default None

        Returns
        -------
        list
            list of mobjects
        T)r1   only_those_with_points)r1   )r   r1   r   list)rP   r   r   r   all_excludeds        rQ   get_mobjects_to_displayzCamera.get_mobjects_to_display  sr    .  	J5 ,'+  H
 ! J=% $ 0      2(LIIH~~rS   c           
        | j         }| j        }| j        }t          t          j        |                                d         |d         |dz  z
  k     |                                d         |d         |dz  z   k    |                                d         |d         |dz  z   k    |	                                d         |d         |dz  z
  k     g           S )a  Checks whether the passed mobject is in
        frame or not.

        Parameters
        ----------
        mobject
            The mobject for which the checking needs to be done.

        Returns
        -------
        bool
            True if in frame, False otherwise.
        r   rd      )
r)   r8   r:   r   opor_	get_right
get_bottomget_leftget_top)rP   ra   fcfhfws        rQ   is_in_framezCamera.is_in_frame  s     F!!##A&Aa7""$$Q'"Q%"q&.8  ""1%1Q6!!!$r!urAv~5	
 
 
 	
rS   c                      | j         |gfi |S )a=  Capture mobjects by storing it in :attr:`pixel_array`.

        This is a single-mobject version of :meth:`capture_mobjects`.

        Parameters
        ----------
        mobject
            Mobject to capture.

        kwargs
            Keyword arguments to be passed to :meth:`get_mobjects_to_display`.

        )capture_mobjects)rP   ra   r?   s      rQ   capture_mobjectzCamera.capture_mobject   s!     %t$gY99&999rS   c                     | j         |fi |}t          j        || j                  D ].\  }} | j        |         t          |          | j                   /dS )a  Capture mobjects by printing them on :attr:`pixel_array`.

        This is the essential function that converts the contents of a Scene
        into an array, which is then converted to an image or video.

        Parameters
        ----------
        mobjects
            Mobjects to capture.

        kwargs
            Keyword arguments to be passed to :meth:`get_mobjects_to_display`.

        Notes
        -----
        For a list of classes that can currently be rendered, see :meth:`display_funcs`.

        N)r   itgroupbyrr   rn   r   r   )rP   r   r?   
group_typegroups        rQ   r   zCamera.capture_mobjects  su    4 04/CCFCC!#Hd6H!I!I 	J 	JJ*Dz*4;;8HIIII	J 	JrS   cairo.Context | Nonec                R    | j                             t          |          d          S )a/  Returns the cached cairo context of the passed
        pixel array if it exists, and None if it doesn't.

        Parameters
        ----------
        pixel_array
            The pixel array to check.

        Returns
        -------
        cairo.Context
            The cached cairo context.
        N)rL   getidr   s     rQ   get_cached_cairo_contextzCamera.get_cached_cairo_context3  s#     044R__dKKKrS   ctxcairo.Contextc                4    || j         t          |          <   dS )zCaches the passed Pixel array into a Cairo Context

        Parameters
        ----------
        pixel_array
            The pixel array to cache
        ctx
            The context to cache it into.
        N)rL   r   )rP   r   r   s      rQ   cache_cairo_contextzCamera.cache_cairo_contextC  s     >A)"[//:::rS   c                   |                      |          }|r|S | j        }| j        }| j        }| j        }| j        }t          j                            |j	        t          j
        ||          }t          j        |          }	|	                    ||           |	                    t          j        ||z  dd||z   |dz  |d         ||z  z  z
  |dz  |d         ||z  z  z                        |                     ||	           |	S )a  Returns the cairo context for a pixel array after
        caching it to self.pixel_array_to_cairo_context
        If that array has already been cached, it returns the
        cached version instead.

        Parameters
        ----------
        pixel_array
            The Pixel array to get the cairo context of.

        Returns
        -------
        cairo.Context
            The cairo context of the pixel array.
        r   rd   r   )r   r7   r5   r:   r8   r)   cairoImageSurfacecreate_for_datadataFORMAT_ARGB32Contextscale
set_matrixMatrixr   )
rP   r   
cached_ctxpwphr   r   r   surfacer   s
             rQ   get_cairo_contextzCamera.get_cairo_contextO  s&     22;??
 	$44	
 
 mG$$		"bLbr'
a2a5BG,,a2a5BG,, 		
 		
 		
 	  c222
rS   	vmobjectslist[VMobject]c                    t          |          dk    rdS t          j        |d           }|D ]4\  }}|r|                     ||           |                     ||           5dS )zDisplays multiple VMobjects in the pixel_array

        Parameters
        ----------
        vmobjects
            list of VMobjects to display
        pixel_array
            The pixel array
        r   Nc                *    |                                  S rV   get_background_image)vms    rQ   ri   z=Camera.display_multiple_vectorized_mobjects.<locals>.<lambda>  s    R=T=T=V=V rS   )lenr   r   -display_multiple_background_colored_vmobjects1display_multiple_non_background_colored_vmobjects)rP   r   r   batch_image_pairsr   rg   s         rQ   rk   z+Camera.display_multiple_vectorized_mobjects|  s     y>>QFJy2V2VWW- 	 	LE5 BB5+VVVVFF   		 	rS   Iterable[VMobject]c                f    |                      |          }|D ]}|                     ||           dS )a  Displays multiple VMobjects in the cairo context, as long as they don't have
        background colors.

        Parameters
        ----------
        vmobjects
            list of the VMobjects
        pixel_array
            The Pixel array to add the VMobjects to.
        N)r   display_vectorized)rP   r   r   r   vmobjects        rQ   r   z8Camera.display_multiple_non_background_colored_vmobjects  sI     $$[11! 	3 	3H##Hc2222	3 	3rS   r   r   c                    |                      ||           |                     ||d           |                     ||           |                     ||           | S )a  Displays a VMobject in the cairo context

        Parameters
        ----------
        vmobject
            The Vectorized Mobject to display
        ctx
            The cairo context to use.

        Returns
        -------
        Camera
            The camera object
        Tr3   )set_cairo_context_pathapply_stroke
apply_fill)rP   r   r   s      rQ   r   zCamera.display_vectorized  sd     	##C222#xD999X&&&#x(((rS   c                   |                      ||j                  }t          |          dk    r| S |                                 |                    |          }|D ]}|                    |          }|                                 |d         } |j        |dd           |D ]0\  }}	}
} |j        g |	dd         |
dd         |dd         R   1|	                    |d         |d                   r|
                                 | S )a)  Sets a path for the cairo context with the vmobject passed

        Parameters
        ----------
        ctx
            The cairo context
        vmobject
            The VMobject

        Returns
        -------
        Camera
            Camera object after setting cairo_context_path
        r   Nrd   )transform_points_pre_displaypointsr   new_pathgen_subpaths_from_points_2d#gen_cubic_bezier_tuples_from_pointsnew_sub_pathmove_tocurve_toconsider_points_equals_2d
close_path)rP   r   r   r   subpathssubpathquadsstart_p0p1p2p3s               rQ   r   zCamera.set_cairo_context_path  s1    228X_MM v;;!K77?? 	! 	!G@@IIEAJECKrr###( 8 8RR7b!f7r"1"v72A27777711'!*gbkJJ !   rS   rgbasr   c                   t          |          dk    r+ |j        g |d         ddd         |d         d         R   n|                                }|                     ||          }t	          j        t          j        d |D               }t          j	        ddt          |                    }t          ||d	          D ]$\  }} |j        |g|ddd         |d         R   %|                    |           | S )
a\  Sets the color of the cairo context

        Parameters
        ----------
        ctx
            The cairo context
        rgbas
            The RGBA array with which to color the context.
        vmobject
            The VMobject with which to set the color.

        Returns
        -------
        Camera
            The camera object
        r   r   rd   Nr      c              3  *   K   | ]}|d d         V  d S )Nrd   rf   ).0points     rQ   	<genexpr>z1Camera.set_cairo_context_color.<locals>.<genexpr>  s*      2Q2Q5!92Q2Q2Q2Q2Q2QrS   Tstrict)r   set_source_rgba!get_gradient_start_and_end_pointsr   r   LinearGradientr   chainrH   linspacezipadd_color_stop_rgba
set_source)	rP   r   r  r   r   patoffsetsrgbaoffsets	            rQ   set_cairo_context_colorzCamera.set_cairo_context_color  s   & u::??  C>q!%R%>%(1+>>>>>??AAF66xHHF&2Q2Q&2Q2Q2Q(RSCk!QE

33G #E74 @ @ @ G Gf''FaeeFd1gFFFFFNN3rS   c                    |                      ||                     |          |           |                                 | S )zFills the cairo context

        Parameters
        ----------
        ctx
            The cairo context
        vmobject
            The VMobject

        Returns
        -------
        Camera
            The camera object.
        )r   get_fill_rgbasfill_preserve)rP   r   r   s      rQ   r   zCamera.apply_fill  sA     	$$S$*=*=h*G*GRRRrS   c                   |                     |          }|dk    r| S |                     ||                     ||          |           |                    || j        z  | j        | j        z  z             |j        t          j        k    r%|	                    t          |j                            |j        t          j        k    r%|                    t          |j                            |                                 | S )a  Applies a stroke to the VMobject in the cairo context.

        Parameters
        ----------
        ctx
            The cairo context
        vmobject
            The VMobject
        background
            Whether or not to consider the background when applying this
            stroke width, by default False

        Returns
        -------
        Camera
            The camera object with the stroke applied.
        r   r   )get_stroke_widthr   get_stroke_rgbasset_line_widthr/   r:   
joint_typeLineJointTypeAUTOset_line_joinLINE_JOIN_MAP	cap_styleCapStyleTypeset_line_capCAP_STYLE_MAPstroke_preserve)rP   r   r   r3   r   s        rQ   r   zCamera.apply_stroke  s   ( ))*55A::K$$!!(z!BB	
 	
 	

 	,-$"224	
 	
 	
 -"444mH,?@AAA!222]8+=>???rS   r   c                ,    |                     |          S )a  Gets the RGBA array for the stroke of the passed
        VMobject.

        Parameters
        ----------
        vmobject
            The VMobject
        background
            Whether or not to consider the background when getting the stroke
            RGBAs, by default False

        Returns
        -------
        np.ndarray
            The RGBA array of the stroke.
        )r&  )rP   r   r3   s      rQ   r&  zCamera.get_stroke_rgbas9  s    & ((444rS   c                *    |                                 S )zReturns the RGBA array of the fill of the passed VMobject

        Parameters
        ----------
        vmobject
            The VMobject

        Returns
        -------
        np.array
            The RGBA Array of the fill of the VMobject
        )r"  )rP   r   s     rQ   r"  zCamera.get_fill_rgbasN  s     &&(((rS   r   c                F    | j         t          |           | _         | j         S )a"  Returns the background_colored_vmobject_displayer
        if it exists or makes one and returns it if not.

        Returns
        -------
        BackgroundColoredVMobjectDisplayer
            Object that displays VMobjects that have the same color
            as the background.
        )rC   r   r[   s    rQ   )get_background_colored_vmobject_displayerz0Camera.get_background_colored_vmobject_displayer]  s+     5=2488 6 99rS   
cvmobjectsc                n    |                                  } |j        | }|                     ||           | S )a0  Displays multiple vmobjects that have the same color as the background.

        Parameters
        ----------
        cvmobjects
            List of Colored VMobjects
        pixel_array
            The pixel array.

        Returns
        -------
        Camera
            The camera object.
        )r5  displayoverlay_rgba_array)rP   r6  r   	displayercvmobject_pixel_arrays        rQ   r   z4Camera.display_multiple_background_colored_vmobjectso  sA    " BBDD	 1	 1: >-BCCCrS   	pmobjectsIterable[PMobject]c           	         |D ]=}|                      ||j        |j        |                     |j                  |           >dS )zDisplays multiple PMobjects by modifying the passed pixel array.

        Parameters
        ----------
        pmobjects
            List of PMobjects
        pixel_array
            The pixel array to modify.
        N)display_point_cloudr   r  adjusted_thicknessstroke_width)rP   r<  r   pmobjects       rQ   rl   z,Camera.display_multiple_point_cloud_mobjects  s^     " 	 	H$$''(=>>   	 	rS   rB  r   r   r"   	thicknessc                *   t          |          dk    rdS |                     ||          }|                     ||          }|j        d         }| j        |z                      | j                  }t          |          }|t          |          z  }	t          j        |g|	z            	                    ||f          }| 
                    |          }
||
         }||
         }| j        }| j        }t          j        d|gd          }|	                    d          }t          j        ||          dddf         }|                    d          }|	                    ||z  |f          }|||<   |	                    |||f          |ddddf<   dS )a  Displays a PMobject by modifying the pixel array suitably.

        TODO: Write a description for the rgbas argument.

        Parameters
        ----------
        pmobject
            Point Cloud Mobject
        points
            The points to display in the point cloud mobject
        rgbas

        thickness
            The thickness of each point of the PMobject
        pixel_array
            The pixel array to modify.

        r   Nrd   r   r-   r{   )rd   r   )r   points_to_pixel_coordsthickened_coordinatesr   rK   r   r.   rH   r   reshapeon_screen_pixelsr5   r7   dot)rP   rB  r   r  rC  r   pixel_coordsrgba_len
target_lenfactoron_screen_indicesr   r   	flattenerindicesnew_pas                   rQ   r?  zCamera.display_point_cloud  s   4 v;;!F228VDD11,	JJ$Q'!E)11$2HII&&
s5zz)%6)**22J3IJJ 11,??#$56'(HaWE222	%%f--	&y11!!!Q$7..''$$b2gx%899w"NNBH+=>>AAAqqqDrS   image_mobjectsIterable[AbstractImageMobject]c                <    |D ]}|                      ||           dS )zDisplays multiple image mobjects by modifying the passed pixel_array.

        Parameters
        ----------
        image_mobjects
            list of ImageMobjects
        pixel_array
            The pixel array to modify.
        N)display_image_mobject)rP   rR  r   image_mobjects       rQ   rm   z&Camera.display_multiple_image_mobjects  s:     , 	C 	CM&&}kBBBB	C 	CrS   rV  r   
np.ndarrayc                D   t          j        |                                d          }t          j        ddg|j        dgd|j        g|j        |j        gg          }|                     ||j                  	                    t          j
                  }t          j        t          d |D              t          d |D              g          }|z  ||z  }t          d |D              t          d |D              f}fdd	D             fd
t          d          D             }t          j                            |d          }	t          j        |	          }
||
         }|	|
         }|dk    rdS ||
dz
  dz           }||
dz
  dz           }t#          t%          ||                    |z  }t#          t%          ||                    |z  }t          ||          }|dk     rdS g }t'          |d          D ]U\  \  }}\  }}|                    ||dddd| |z  | |z  g           |                    ddd||d| |z  | |z  g           Vt          j        |t          j                  }|                    d          	                    t          j                  }	 t          j                            ||          }n# t          j        j        $ r Y dS w xY w|                    |t           j        j        ||j                  }t          j        t          j        | j        | j        f          d          }|                     ||d         |d         |d         |d         z   |d         |d         z   f           | !                    ||           dS )a  Display an :class:`~.ImageMobject` by changing the ``pixel_array`` suitably.

        Parameters
        ----------
        image_mobject
            The :class:`~.ImageMobject` to display.
        pixel_array
            The pixel array to put the :class:`~.ImageMobject` in.
        r$   r   r   c                    g | ]\  }}|S rf   rf   r  xys      rQ   
<listcomp>z0Camera.display_image_mobject.<locals>.<listcomp>      666DAqa666rS   c                    g | ]\  }}|S rf   rf   rZ  s      rQ   r]  z0Camera.display_image_mobject.<locals>.<listcomp>  r^  rS   c                    g | ]\  }}|S rf   rf   rZ  s      rQ   r]  z0Camera.display_image_mobject.<locals>.<listcomp>
      2221!222rS   c                    g | ]\  }}|S rf   rf   rZ  s      rQ   r]  z0Camera.display_image_mobject.<locals>.<listcomp>  ra  rS   c                     g | ]
}|         S rf   rf   )r  itarget_coordss     rQ   r]  z0Camera.display_image_mobject.<locals>.<listcomp>  s    CCCM!,CCCrS   )r   r   r  rd   c                >    g | ]}|d z   dz           |         z
  S )r   r%   rf   )r  rd  ordered_verticess     rQ   r]  z0Camera.display_image_mobject.<locals>.<listcomp>  s2    WWW!1q5A+.1A!1DDWWWrS   r%   r   )axisNg      ?Tr  r{      )sizemethodr   resample)box)"r   r   get_pixel_arrayrH   r   r   r   points_to_subpixel_coordsr   r   int64minrJ   rangelinalgnormargmaxabsr   r  appendfloat64rG  solveLinAlgError	transform	TransformPERSPECTIVEresampling_algorithmr   r5   r7   pasteoverlay_PIL_image)rP   rV  r   	sub_imageoriginal_coordsint_target_coordsshift_vectortarget_sizesidesside_lengths_in_pixelslongest_side_indexlongest_sidelongest_side_length_in_pixelsprevious_side	next_sideh1h2"height_from_longest_side_in_pixelshomography_matrixr[  r\  XYAbtransform_coefficients
full_imagerg  re  s                              @@rQ   rU  zCamera.display_image_mobject  s3    OM$A$A$C$C&QQQ	(A!$I$%)"23	
 
 66=/
 
 *00:: x66$5666766$56667
 
 	%\)22 1222322 12223
 DCCClCCCWWWWeTUhhWWW!#A!>!>Y'=>>/0(>?Q(R%(A--F1A5:;-1Q67	 }55669VVy11225RR-0R[[*-33F !-NNN 	I 	INFQFQ$$aAq!QQQ%GHHH$$aAq!QQQ%GHHHHH&bj999##A&&--bj99	%'Y__Q%:%:""y$ 	 	 	 FF	
 ''?.'"7	 ( 
 
	 _Hd')9:;;
 
 

 	QQQ+a.0Q+a.0	 	 	
 	
 	
 	{J77777s   ( K	 	K! K!	new_arrayc                X    |                      ||                     |                     dS )zOverlays an RGBA array on top of the given Pixel array.

        Parameters
        ----------
        pixel_array
            The original pixel array to modify.
        new_array
            The new pixel array to overlay.
        N)r  r   )rP   r   r  s      rQ   r9  zCamera.overlay_rgba_arrayU  s,     	{DNN9,E,EFFFFFrS   r   r   c                    t          j        t          j        |                     |          |          d          |ddddf<   dS )zOverlays a PIL image on the passed pixel array.

        Parameters
        ----------
        pixel_array
            The Pixel array
        image
            The Image to overlay.
        r&   r{   N)rH   r   r   alpha_compositer   )rP   r   r   s      rQ   r  zCamera.overlay_PIL_imagec  sP     H!$.."="=uEE
 
 
AAAqqqDrS   c                v   t          j        || j        k              s|S t          j        t           j        j        d|          }|| j        k    }||ddf         }||         }t          j        |                    t          |          df          |j	        d         d          }| j        |z  |z  }|||<   |S )a  If any of the points in the passed array are out of
        the viable range, they are adjusted suitably.

        Parameters
        ----------
        points
            The points to adjust

        Returns
        -------
        np.array
            The adjusted points.
        r   N)
rH   anyrG   r   rs  rt  repeatrG  r   r   )rP   r   normsviolator_indices	violatorsviolator_normsreshaped_normsrescaleds           rQ   adjust_out_of_range_pointsz!Camera.adjust_out_of_range_pointsr  s     vft6677 	M#BINAv>> 4#::+QQQ./	/0""C$7$7#;<<LO
 

 *Y6G#+ rS   c                z    t          j        t          j        |                    st          j        d          }|S )N)r   r  )rH   allisfiniter   rP   ra   r   s      rQ   r   z#Camera.transform_points_pre_display  s6     vbk&))** 	& Xf%%FrS   npt.NDArray[ManimFloat]c                Z   |                      ||          }|| j        z
  }t          j        t	          |          df          }| j        }| j        }| j        }| j        }||z  }	|dz  }
||z  }|dz  }|dz  }|d d df         |	z  |
z   |d d df<   |d d df         |z  |z   |d d df<   |S )Nrd   r   r   r   )	r   r)   rH   r   r   r5   r7   r8   r:   )rP   ra   r   shifted_pointsresultr5   r7   r8   r:   
width_mult	width_addheight_mult
height_adds                rQ   ro  z Camera.points_to_subpixel_coords  s     227FCC$"333v;;*++(&(& ;.
!O	"\1!A%
r%aaad+j89Dqqq!t%aaad+k9JFqqq!trS   npt.NDArray[ManimInt]c                h    |                      ||                              t          j                  S rV   )ro  r   rH   rp  r  s      rQ   rE  zCamera.points_to_pixel_coords  s*    
 --gv>>EEbhOOOrS   rJ  c           	         t          t          j        |dddf         dk    |dddf         | j        k     |dddf         dk    |dddf         | j        k     g          S )a$  Returns array of pixels that are on the screen from a given
        array of pixel_coordinates

        Parameters
        ----------
        pixel_coords
            The pixel coords to check.

        Returns
        -------
        np.array
            The pixel coords on screen.
        Nr   r   )r   r   and_r7   r5   )rP   rJ  s     rQ   rH  zCamera.on_screen_pixels  sz     GQQQT"a'QQQT"T%55QQQT"a'QQQT"T%66	
 
 	
rS   c                    t          j        t          d         t          d                   }t          j        | j        | j                  }||z  }d|dz
  |z  z   S )a/  Computes the adjusted stroke width for a zoomed camera.

        Parameters
        ----------
        thickness
            The stroke width of a mobject.

        Returns
        -------
        float
            The adjusted stroke width that reflects zooming in with
            the camera.
        r5   r7   r   )r   addr   r5   r7   )rP   rC  big_sumthis_sumrM  s        rQ   r@  zCamera.adjusted_thickness  sT     ~ 6}8MNN&!2D4DEE8#IMV+++rS   c                    t          |          }t          t          | dz  dz   |dz  dz                       }t          j        t          t          j        ||                              S )zDetermine a list of vectors used to nudge
        two-dimensional pixel coordinates.

        Parameters
        ----------
        thickness

        Returns
        -------
        np.array

        rd   r   )r-   r   rr  rH   r   r   product)rP   rC  _ranges      rQ   get_thickening_nudgeszCamera.get_thickening_nudges  s^     	NN	eYJ!Oa/a!1CDDEExRZ7788999rS   c                    |                      |          }t          j        fd|D                       j        }                    |dz  df          S )aQ  Returns thickened coordinates for a passed array of pixel coords and
        a thickness to thicken by.

        Parameters
        ----------
        pixel_coords
            Pixel coordinates
        thickness
            Thickness

        Returns
        -------
        np.array
            Array of thickened pixel coords.
        c                    g | ]}|z   S rf   rf   )r  nudgerJ  s     rQ   r]  z0Camera.thickened_coordinates.<locals>.<listcomp>  s     J J J%!5 J J JrS   rd   )r  rH   r   rj  rG  )rP   rJ  rC  nudgesrj  s    `   rQ   rF  zCamera.thickened_coordinates  s`    $ ++I66x J J J J6 J J JKK ##TQYN333rS   c                ,   t          j        | j        | j        g          }t          j        | j        | j        g          }t          j        | j        | j        g          ddd                             ddd          }||z  |z  }||dz  z
  }|dz  }|S )zReturns the cartesian coordinates of each pixel.

        Returns
        -------
        np.ndarray
            The array of cartesian coordinates.
        Nr   r   rd   r   )r   r   )rH   r   r:   r8   r7   r5   rP  	transpose)rP   full_space_dimsfull_pixel_dimsuncentered_pixel_coordsuncentered_space_coordscentered_space_coordss         rQ   r   zCamera.get_coords_of_all_pixels  s     (D$4d6G#HII(D$4d6G#HII #%*d.?AQ-R"S"SDbD#

)Aq!

 	  $o5# !8?Q;N O !6 ?$$rS   )"r'   r(   r)   r!   r*   r+   r,   r-   r.   r+   r/   r0   r1   r2   r3   r4   r5   r6   r7   r6   r8   r9   r:   r9   r;   r9   r<   r=   r>   r9   r?   r
   r@   rA   )rT   r
   r@   r   )r@   r   )r\   r   r@   rA   )r@   r0   )r_   r0   r@   rA   )ra   r   r@   rb   )rs   r0   rt   r0   r@   rA   )r   )rw   r-   r@   rA   r@   rA   rV   )r   r   r@   r   )F)r   r   r   r2   r@   r    )r   r   r   r2   r@   rA   )r   r   r@   r    )r   r   r@   rA   )r@   r   )r3   r    r@   rA   )TN)r   r   r   r2   r   r   r@   r   )ra   r   r@   r2   )ra   r   r?   r
   r@   rA   )r   r   r?   r
   r@   rA   )r   r    r@   r   )r   r    r   r   r@   rA   )r   r    r@   r   )r   r   r   r    r@   rA   )r   r   r   r    r@   rA   )r   r   r   r   r@   r   )r   r   r   r   r@   r   )r   r   r  r   r   r   r@   r   )r   r   r   r   r3   r2   r@   r   )r   r   r3   r2   r@   r   )r   r   r@   r   )r@   r   )r6  r   r   r    r@   r   )r<  r=  r   r    r@   rA   )rB  r   r   r"   r  r   rC  r0   r   r    r@   rA   )rR  rS  r   r    r@   rA   )rV  r   r   rW  r@   rA   )r   rW  r  rW  r@   rA   )r   rW  r   r   r@   rA   )r   rW  r@   rW  )ra   r   r   r"   r@   r"   )ra   r   r   r"   r@   r  )ra   r   r   r"   r@   r  )rJ  rW  r@   r    )rC  r0   r@   r0   )rC  r0   r@   r    )rJ  rW  rC  r0   r@   r    )r@   r    )8__name__
__module____qualname____doc__ORIGINrR   rY   propertyr<   setterr>   rr   rv   rN   rM   r   r   r   r   r   r   rO   r   r   r   r   r   r   r   r   rk   r   r   r   r   r   r   r&  r"  r5  r   rl   r?  rm   rU  r9  r  r  r   ro  rE  rH  r@  r  rF  r   rf   rS   rQ   r   r   =   s#        . (, & !(+/ (,#'"&%)$(#'6:+/!L L L L L\    & & & X&     ( ( ( X(    -S -S -S -S^   "' ' ' ' '24 4 4 46 ?CB B B B B, SX    8 SXA A A A A2 SXU U U U U R R R R4S S S S 
 
 
 
) ) ) ) %))-	# # # # #J
 
 
 
6: : : : J J J JFL L L L 
A 
A 
A 
A+ + + +Z   03 3 3 3"   *       D   B   ( JO' ' ' ' 'T 6;5 5 5 5 5*) ) ) ): : : :$   6   *3? 3? 3? 3?jC C C C"n8 n8 n8 n8`G G G G
 
 
 
   :      4P P P P
 
 
 
0, , , ,(: : : :"4 4 4 40% % % % % %rS   c                  F    e Zd ZdZddZddZ	 dddZddZd dZd!dZ	dS )"r   zAuxiliary class that handles displaying vectorized mobjects with
    a set background image.

    Parameters
    ----------
    camera
        Camera object to use.
    camerar   c                    || _         i | _        t          j        |j                  | _        |                                  d S rV   )r  file_name_to_pixel_array_maprH   r   r   reset_pixel_array)rP   r  s     rQ   rR   z+BackgroundColoredVMobjectDisplayer.__init__B  s>    CE)8F$677     rS   r@   rA   c                &    d| j         d d d d f<   d S )Nr   )r   r[   s    rQ   r  z4BackgroundColoredVMobjectDisplayer.reset_pixel_arrayH  s!    !"AAArS   r$   background_arrayr    rt   r0   rs   r   r+   c                    t          j        |          }|                    |          }|                    ||f          }t	          j        |          S )a  Resizes the pixel array representing the background.

        Parameters
        ----------
        background_array
            The pixel
        new_width
            The new width of the background
        new_height
            The new height of the background
        mode
            The PIL image mode, by default "RGBA"

        Returns
        -------
        np.array
            The numpy pixel array of the resized background.
        )r   r   r~   resizerH   r   )rP   r  rt   rs   r   r   resized_images          rQ   resize_background_arrayz:BackgroundColoredVMobjectDisplayer.resize_background_arrayK  sK    2  011d##i%<==x&&&rS   r   c                    |j         dd         \  }}|j         d         dk    rdnd}|                     ||||          S )ae  Resizes the background array to match the passed pixel array.

        Parameters
        ----------
        background_array
            The prospective pixel array.
        pixel_array
            The pixel array whose width and height should be matched.

        Returns
        -------
        np.array
            The resized background array.
        Nrd   r%   r$   RGB)r   r  )rP   r  r   r   r   r   s         rQ    resize_background_array_to_matchzCBackgroundColoredVMobjectDisplayer.resize_background_array_to_matchi  sO    " $)"1"-$*1-22vv++,<eVTRRRrS   r    Image.Image | pathlib.Path | strc                z   t          |          }|| j        v r| j        |         S t          |t                     r#t          |          }t	          j        |          }t          j        |          }| j        }t          j	        |j
        |j
        k              s|                     ||          }|| j        |<   |S )a  Gets the background array that has the passed file_name.

        Parameters
        ----------
        image
            The background image or its file name.

        Returns
        -------
        np.ndarray
            The pixel array of the image.
        )r+   r  ro   r   r   r}   rH   r   r   r  r   r  )rP   r   	image_key	full_path
back_arrayr   s         rQ   get_background_arrayz7BackgroundColoredVMobjectDisplayer.get_background_array~  s     JJ	9994Y??eS!! 	*2599IJy))EXe__
&vk':+;;<< 	X>>z;WWJ7A))4rS   r6  r   r4   c                   t          j        |d           }d}|D ]\  }}|                     |          }| j        }| j                            ||           t          j        ||                    d          z  dz  | j        j	                  }||}nt          j
        ||          }|                                  |S )zDisplays the colored VMobjects.

        Parameters
        ----------
        *cvmobjects
            The VMobjects

        Returns
        -------
        np.array
            The pixel array with the `cvmobjects` displayed.
        c                *    |                                  S rV   r   )cvs    rQ   ri   z<BackgroundColoredVMobjectDisplayer.display.<locals>.<lambda>  s    b>U>U>W>W rS   Nr0      r{   )r   r   r  r   r  r   rH   r   r   r.   maximumr  )	rP   r6  r   
curr_arrayr   rg   r  r   r  s	            rQ   r8  z*BackgroundColoredVMobjectDisplayer.display  s     Jz3W3WXX
- 	% 	%LE5#88??*KKII   !K$6$6w$?$??#Ek3  I !&

Z
I>>
""$$$$rS   N)r  r   r  )r$   )
r  r    rt   r0   rs   r0   r   r+   r@   r    )r  r    r   r    r@   r    )r   r  r@   r    )r6  r   r@   r4   )
r  r  r  r  rR   r  r  r  r  r8  rf   rS   rQ   r   r   8  s         ! ! ! !# # # # ' ' ' ' '<S S S S*   >     rS   )Hr  
__future__r   __all__rX   	itertoolsr   operatorr   pathlibcollections.abcr   r   	functoolsr   typingr	   r
   r   r   numpyrH   PILr   manim._configr   r   manim.constantsmanim.mobject.mobjectr   'manim.mobject.types.point_cloud_mobjectr   &manim.mobject.types.vectorized_mobjectr   manim.utils.colorr   r   r   manim.utils.familyr   manim.utils.imagesr   manim.utils.iterablesr   manim.utils.space_opsr   numpy.typingnpt!manim.mobject.types.image_mobjectr   manim.typingr   r   r   r   r    r!   r"   r)  r*  ROUNDLineJoinBEVELMITERr,  r.  LineCapBUTTSQUAREr0  r   r   rf   rS   rQ   <module>r	     s   R R " " " " " "9
:           . . . . . . . .       + + + + + + + + + +            ( ( ( ( ( ( ( (     ) ) ) ) ) ) < < < < < < ; ; ; ; ; ; O O O O O O O O O O = = = = = = 9 9 9 9 9 9 8 8 8 8 8 8 ) ) ) ) ) ) FFFFFF                  ---	 t+u})-	v% v% v% v% v% v% v% v%v'D D D D D D D D D DrS   