
    lj(                       d Z ddlmZ ddgZddlmZ ddlZddlm	Z	m
Z
 ddlmZmZ d	d
lmZ d	dlmZ d	dlmZmZ d	dlmZmZ ddlmZ ddlmZmZ ddlmZmZ erd	dl m!Z!  G d de          Z" G d de"          Z# G d de"          Z$dS )zQAnimations that try to transform Mobjects while keeping track of identical parts.    )annotationsTransformMatchingShapesTransformMatchingTex)TYPE_CHECKINGN)OpenGLGroupOpenGLMobject)OpenGLVGroupOpenGLVMobject   )config)RendererType)GroupMobject)VGroupVMobject   )AnimationGroup)FadeInFadeOut)FadeTransformPieces	Transform)Scenec                  l     e Zd ZdZ	 	 	 dd fdZddZddZedd            Zedd            Z	 xZ
S )TransformMatchingAbstractBasea(  Abstract base class for transformations that keep track of matching parts.

    Subclasses have to implement the two static methods
    :meth:`~.TransformMatchingAbstractBase.get_mobject_parts` and
    :meth:`~.TransformMatchingAbstractBase.get_mobject_key`.

    Basically, this transformation first maps all submobjects returned
    by the ``get_mobject_parts`` method to certain keys by applying the
    ``get_mobject_key`` method. Then, submobjects with matching keys
    are transformed into each other.

    Parameters
    ----------
    mobject
        The starting :class:`~.Mobject`.
    target_mobject
        The target :class:`~.Mobject`.
    transform_mismatches
        Controls whether submobjects without a matching key are transformed
        into each other by using :class:`~.Transform`. Default: ``False``.
    fade_transform_mismatches
        Controls whether submobjects without a matching key are transformed
        into each other by using :class:`~.FadeTransform`. Default: ``False``.
    key_map
        Optional. A dictionary mapping keys belonging to some of the starting mobject's
        submobjects (i.e., the return values of the ``get_mobject_key`` method)
        to some keys belonging to the target mobject's submobjects that should
        be transformed although the keys don't match.
    kwargs
        All further keyword arguments are passed to the submobject transformations.


    Note
    ----
    If neither ``transform_mismatches`` nor ``fade_transform_mismatches``
    are set to ``True``, submobjects without matching keys in the starting
    mobject are faded out in the direction of the unmatched submobjects in
    the target mobject, and unmatched submobjects in the target mobject
    are faded in from the direction of the unmatched submobjects in the
    start mobject.

    FNmobjectr   target_mobjecttransform_mismatchesboolfade_transform_mismatcheskey_mapdict | Nonec                z   t          |t                    rt          }nAt          |t                    rt          }n$t          |t
                    rt          }nt          }|                     |          }|                     |          }	|i } |            }
 |            }t          |          
                    |	          D ]8}|
                    ||                    |                    |	|                    9t          |
|fi |g} |            } |            }|                                D ]o\  }}||v rf||	v rb|                    ||                    |                    |	|                    |                    |d            |	                    |d            pt          |          dk    r!|                    t#          ||fi |            |            } |            }t          |                              |	          D ]}|                    ||                    t          |	                              |          D ]}|                    |	|                    |                                }|r+d|vrd|d<   |                    t          ||fi |           nh|r"|                    t#          ||fi |           nD|                    t)          |fd|i|           |                    t+          |fd|i|            t-                      j        |  ||g| _        || _        d S )Nr   $replace_mobject_with_target_in_sceneTtarget_position)
isinstancer
   r	   r   r   r   r   r   get_shape_mapsetintersectionaddr   itemspoplenappendr   
differencecopyr   r   super__init__	to_removeto_add)selfr   r   r   r   r    kwargs
group_type
source_map
target_maptransform_sourcetransform_targetkeyanimskey_mapped_sourcekey_mapped_targetkey1key2fade_sourcefade_targetfade_target_copy	__class__s                        c/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/animation/transform_matching_parts.pyr1   z&TransformMatchingAbstractBase.__init__F   s    g~.. 	%JJ// 	$JJ** 	JJJ''00
''77
?G
 &:<<%:<<z??//
;; 	2 	2C  C111  C1111+-=HHHHI 'JLL&JLL!--// 	+ 	+JD$z!!dj&8&8!%%j&6777!%%j&6777tT***tT*** !!A%%LL#$57HSSFSS   !jll jllz??--j99 	- 	-COOJsO,,,,z??--j99 	- 	-COOJsO,,,,&++-- 
	5VCCAE=>LL;FFvFFGGGG& 	LL,[+PPPPQQQQLLTTkTVTTUUULL'OOOOO   	%  !#34$    returndictc                   i }|                      |          D ]t}|                     |          }||vr>t          d         t          j        k    rt                      ||<   nt                      ||<   ||                             |           u|S )Nrenderer)get_mobject_partsget_mobject_keyr   r   OPENGLr	   r   r)   )r4   r   	shape_mapsmr;   s        rE   r&   z+TransformMatchingAbstractBase.get_shape_map   s    	((11 	# 	#B&&r**C)##*%)<<<%1^^IcNN%+XXIcNcNr""""rF   scener   Nonec                    | j         D ]}|                    d           |                    | j                    |j        | j          |                    | j                   d S )Nr   )
animationsinterpolateremover   r2   r)   r3   )r4   rP   anims      rE   clean_up_from_scenez1TransformMatchingAbstractBase.clean_up_from_scene   si    O 	  	 DQT\"""dn%%		$+rF   c                     t          d          NzTo be implemented in subclass.NotImplementedErrorr   s    rE   rK   z/TransformMatchingAbstractBase.get_mobject_parts       !"BCCCrF   c                     t          d          rY   rZ   r\   s    rE   rL   z-TransformMatchingAbstractBase.get_mobject_key   r]   rF   FFN
r   r   r   r   r   r   r   r   r    r!   )r   r   rG   rH   )rP   r   rG   rQ   )r   r   )__name__
__module____qualname____doc__r1   r&   rW   staticmethodrK   rL   __classcell__rD   s   @rE   r   r      s        ) )^ &+*/#G% G% G% G% G% G% G%R
 
 
 
    D D D \D D D D \D D D D DrF   r   c                  \     e Zd ZdZ	 	 	 dd fdZedd            Zedd            Z xZS )r   a  An animation trying to transform groups by matching the shape
    of their submobjects.

    Two submobjects match if the hash of their point coordinates after
    normalization (i.e., after translation to the origin, fixing the submobject
    height at 1 unit, and rounding the coordinates to three decimal places)
    matches.

    See also
    --------
    :class:`~.TransformMatchingAbstractBase`

    Examples
    --------

    .. manim:: Anagram

        class Anagram(Scene):
            def construct(self):
                src = Text("the morse code")
                tar = Text("here come dots")
                self.play(Write(src))
                self.wait(0.5)
                self.play(TransformMatchingShapes(src, tar, path_arc=PI/2))
                self.wait(0.5)

    FNr   r   r   r   r   r   r    r!   c                F     t                      j        ||f|||d| d S N)r   r   r    r0   r1   r4   r   r   r   r   r    r5   rD   s          rE   r1   z TransformMatchingShapes.__init__   O     		
 "6&?	
 	
 	
 	
 	
 	
 	
rF   rG   list[Mobject]c                *    |                                  S N)family_members_with_pointsr\   s    rE   rK   z)TransformMatchingShapes.get_mobject_parts   s    11333rF   intc                &   |                                   |                                  |                     d           t          j        | j        d          dz   }t          |                                          }|                                  |S )Nr   )height   g        )	
save_statecenterr'   nproundpointshashtobytesrestore)r   rounded_pointsresults      rE   rL   z'TransformMatchingShapes.get_mobject_key   s~    1'.!44s:n,,..//rF   r_   r`   r   r   rG   rn   )r   r   rG   rr   	ra   rb   rc   rd   r1   re   rK   rL   rf   rg   s   @rE   r   r      s         @ &+*/#
 
 
 
 
 
 
$ 4 4 4 \4    \    rF   c                  \     e Zd ZdZ	 	 	 dd fdZedd            Zedd            Z xZS )r   a  A transformation trying to transform rendered LaTeX strings.

    Two submobjects match if their ``tex_string`` matches.

    See also
    --------
    :class:`~.TransformMatchingAbstractBase`

    Examples
    --------

    .. manim:: MatchingEquationParts

        class MatchingEquationParts(Scene):
            def construct(self):
                variables = VGroup(MathTex("a"), MathTex("b"), MathTex("c")).arrange_submobjects().shift(UP)

                eq1 = MathTex("{{x}}^2", "+", "{{y}}^2", "=", "{{z}}^2")
                eq2 = MathTex("{{a}}^2", "+", "{{b}}^2", "=", "{{c}}^2")
                eq3 = MathTex("{{a}}^2", "=", "{{c}}^2", "-", "{{b}}^2")

                self.add(eq1)
                self.wait(0.5)
                self.play(TransformMatchingTex(Group(eq1, variables), eq2))
                self.wait(0.5)
                self.play(TransformMatchingTex(eq2, eq3))
                self.wait(0.5)

    FNr   r   r   r   r   r   r    r!   c                F     t                      j        ||f|||d| d S rj   rk   rl   s          rE   r1   zTransformMatchingTex.__init__	  rm   rF   rG   rn   c                    t          | t          t          t          t          f          rd | j        D             S t          | d          sJ | j        S )Nc                L    g | ]!}t                               |          D ]}|"S  )r   rK   ).0sps      rE   
<listcomp>z:TransformMatchingTex.get_mobject_parts.<locals>.<listcomp>  sM       -??BB      rF   
tex_string)r%   r   r   r   r	   submobjectshasattrr\   s    rE   rK   z&TransformMatchingTex.get_mobject_parts  sb    gv{LIJJ 	'  ,    7L11111&&rF   strc                    | j         S rp   )r   r\   s    rE   rL   z$TransformMatchingTex.get_mobject_key'  s    !!rF   r_   r`   r   )r   r   rG   r   r   rg   s   @rE   r   r      s         D &+*/#
 
 
 
 
 
 
$ 	' 	' 	' \	' " " " \" " " " "rF   )%rd   
__future__r   __all__typingr   numpyrx   #manim.mobject.opengl.opengl_mobjectr   r   .manim.mobject.opengl.opengl_vectorized_mobjectr	   r
   _configr   	constantsr   mobject.mobjectr   r    mobject.types.vectorized_mobjectr   r   compositionr   fadingr   r   	transformr   r   scene.scener   r   r   r   r   rF   rE   <module>r      s   W W " " " " " "$&<
=                 J J J J J J J J W W W W W W W W       $ $ $ $ $ $ , , , , , , , , ? ? ? ? ? ? ? ? ' ' ' ' ' ' # # # # # # # # 5 5 5 5 5 5 5 5 $######OD OD OD OD ODN OD OD ODd; ; ; ; ;; ; ; ;|?" ?" ?" ?" ?"8 ?" ?" ?" ?" ?"rF   