
    ljT=                       d Z ddlmZ dgZddlmZmZ ddlmZ ddl	Z
ddlmZ ddlmZmZmZmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZ ddlmZ ddl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)  G d de          Z*dS )zHA camera that can be positioned and oriented in three-dimensional space.    )annotationsThreeDCamera)CallableIterable)AnyN)Mobject)get_3d_vmob_end_corner"get_3d_vmob_end_corner_unit_normalget_3d_vmob_start_corner$get_3d_vmob_start_corner_unit_normal)VMobject)ValueTracker)FloatRGBA_ArrayMatrixMNPoint3DPoint3D_ArrayPoint3DLike   )config)Camera)*)Point)get_shaded_rgb)extract_mobject_family_members)rotation_about_zrotation_matrixc            
      |    e Zd Zddddez  dez  z   dez  z   ddd	d
ez  d	df
dQ fdZedRd            Z	e	j
        dSd!            Z	dT fd$ZdUd&ZdVd+Z	 dWdXd-ZdYd.ZdZ fd1Zd[d2Zd[d3Zd[d4Zd[d5Zd[d6Zd\d8Zd\d9Zd\d:Zd\d;Zd\d<Zd]d=Zd^d?Zd^d@Zd_dCZd`dDZda fdGZ ddHdIdbdMZ!dcdNZ"dcdOZ#dcdPZ$ xZ%S )dr   g      4@g?g      @	      
   TFr   i   focal_distancefloatshading_factordefault_distancelight_source_start_pointr   should_apply_shadingboolexponential_projectionphithetagammazoomkwargsr   c                   t          |                    dt                    d          | _         t	                      j        di | || _        || _        || _        |	| _	        |
| _
        || _        || _        || _        t          | j                  | _        || _        || _        dt"          d         z  | _        t'          | j                  | _        t'          | j                  | _        t'          | j                  | _        t'          | j	                  | _        t'          | j
                  | _        i | _        t5                      | _        |                                  dS )zInitializes the ThreeDCamera

        Parameters
        ----------
        *kwargs
            Any keyword argument of Camera.
        frame_centerr   )stroke_width   frame_widthN )r   getORIGIN_frame_centersuper__init__r"   r*   r+   r,   r-   r$   r%   r&   light_sourcer'   r)   r   max_allowable_normr   phi_trackertheta_trackerfocal_distance_trackergamma_trackerzoom_trackerfixed_orientation_mobjectssetfixed_in_frame_mobjectsreset_rotation_matrix)selfr"   r$   r%   r&   r'   r)   r*   r+   r,   r-   r.   	__class__s               V/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/camera/three_d_camera.pyr9   zThreeDCamera.__init__(   s>   * #6::nf#E#ETUVVV""6""",

	, 0(@%!$"?@@$8!&<#"#f]&;";'11)$*55&243F&G&G#)$*55(33PR'58UU$""$$$$$    returnr   c                &    | j         j        d         S )Nr   )r7   pointsrE   s    rG   r0   zThreeDCamera.frame_centerT   s    !(++rH   pointNonec                :    | j                             |           d S N)r7   move_torE   rM   s     rG   r0   zThreeDCamera.frame_centerX   s    ""5)))))rH   mobjectsIterable[Mobject]c                d    |                                    t                      j        |fi | d S rP   )rD   r8   capture_mobjects)rE   rS   r.   rF   s      rG   rV   zThreeDCamera.capture_mobjects\   s:    ""$$$  44V44444rH   list[ValueTracker]c                B    | j         | j        | j        | j        | j        gS )zA list of :class:`ValueTrackers <.ValueTracker>` of phi, theta, focal_distance,
        gamma and zoom.

        Returns
        -------
        list
            list of ValueTracker objects
        )r<   r=   r>   r?   r@   rL   s    rG   get_value_trackerszThreeDCamera.get_value_trackers`   s,     '
 	
rH   vmobjectr   rgbasr   c                    | j         s|S |j        r|                                dk    r| j        j        d         }t          |          dk     r|                    dd          }nt          j        |d d                   }t          |dd df         t          |          t          |          |          |dd df<   t          |dd df         t          |          t          |          |          |dd df<   |S |S )Nr   r   )axisr2   r!   )r'   shade_in_3dget_num_pointsr:   rK   lenrepeatnparrayr   r   r   r	   r
   )rE   rZ   r[   light_source_pointshaded_rgbass        rG   modified_rgbaszThreeDCamera.modified_rgbasq   s    ( 	L 	 X%<%<%>%>%B%B!%!2!9!!<5zzA~~$||AA|66!xbqb	22"0QU#(224X>>"	# #LBQB #1QU#&x0028<<"	# #LBQB  rH   
backgroundc                T    |                      ||                    |                    S rP   )rf   get_stroke_rgbas)rE   rZ   rg   s      rG   ri   zThreeDCamera.get_stroke_rgbas   s(    
 ""8X-F-Fz-R-RSSSrH   c                R    |                      ||                                          S rP   )rf   get_fill_rgbas)rE   rZ   s     rG   rk   zThreeDCamera.get_fill_rgbas   s&     ""8X-D-D-F-FGGGrH   argslist[Mobject]c                     t                      j        |i |}|                                 dfd}t          ||          S )Nmobr   rI   r#   c                    t          | d          r| j        st          j        S t          j        |                                 j                  d         }|S )Nr^   r   )hasattrr^   rb   infdotget_z_index_reference_pointT)ro   distance
rot_matrixs     rG   z_keyz3ThreeDCamera.get_mobjects_to_display.<locals>.z_key   sQ    C// CO v !fS%D%D%F%F
UUVWXHOrH   )key)ro   r   rI   r#   )r8   get_mobjects_to_displayget_rotation_matrixsorted)rE   rl   r.   rS   rx   rw   rF   s        @rG   rz   z$ThreeDCamera.get_mobjects_to_display   se     35772DCFCC--//
	 	 	 	 	 	 hE****rH   c                4    | j                                         S )zReturns the Polar angle (the angle off Z_AXIS) phi.

        Returns
        -------
        float
            The Polar angle in radians.
        )r<   	get_valuerL   s    rG   get_phizThreeDCamera.get_phi   s     ))+++rH   c                4    | j                                         S )zReturns the Azimuthal i.e the angle that spins the camera around the Z_AXIS.

        Returns
        -------
        float
            The Azimuthal angle in radians.
        )r=   r~   rL   s    rG   	get_thetazThreeDCamera.get_theta   s     !++---rH   c                4    | j                                         S )zReturns focal_distance of the Camera.

        Returns
        -------
        float
            The focal_distance of the Camera in MUnits.
        )r>   r~   rL   s    rG   get_focal_distancezThreeDCamera.get_focal_distance   s     *44666rH   c                4    | j                                         S )a   Returns the rotation of the camera about the vector from the ORIGIN to the Camera.

        Returns
        -------
        float
            The angle of rotation of the camera about the vector
            from the ORIGIN to the Camera in radians
        )r?   r~   rL   s    rG   	get_gammazThreeDCamera.get_gamma   s     !++---rH   c                4    | j                                         S )zReturns the zoom amount of the camera.

        Returns
        -------
        float
            The zoom amount of the camera.
        )r@   r~   rL   s    rG   get_zoomzThreeDCamera.get_zoom   s      **,,,rH   valuec                :    | j                             |           dS )zSets the polar angle i.e the angle between Z_AXIS and Camera through ORIGIN in radians.

        Parameters
        ----------
        value
            The new value of the polar angle in radians.
        N)r<   	set_valuerE   r   s     rG   set_phizThreeDCamera.set_phi   s!     	""5)))))rH   c                :    | j                             |           dS )zSets the azimuthal angle i.e the angle that spins the camera around Z_AXIS in radians.

        Parameters
        ----------
        value
            The new value of the azimuthal angle in radians.
        N)r=   r   r   s     rG   	set_thetazThreeDCamera.set_theta   !     	$$U+++++rH   c                :    | j                             |           dS )zSets the focal_distance of the Camera.

        Parameters
        ----------
        value
            The focal_distance of the Camera.
        N)r>   r   r   s     rG   set_focal_distancezThreeDCamera.set_focal_distance   s!     	#--e44444rH   c                :    | j                             |           dS )zSets the angle of rotation of the camera about the vector from the ORIGIN to the Camera.

        Parameters
        ----------
        value
            The new angle of rotation of the camera.
        N)r?   r   r   s     rG   	set_gammazThreeDCamera.set_gamma   r   rH   c                :    | j                             |           dS )zSets the zoom amount of the camera.

        Parameters
        ----------
        value
            The zoom amount of the camera.
        N)r@   r   r   s     rG   set_zoomzThreeDCamera.set_zoom  s!     	##E*****rH   c                8    |                                  | _        dS )zySets the value of self.rotation_matrix to
        the matrix corresponding to the current position of the camera
        N)generate_rotation_matrixr   rL   s    rG   rD   z"ThreeDCamera.reset_rotation_matrix  s      $<<>>rH   r   c                    | j         S )zReturns the matrix corresponding to the current position of the camera.

        Returns
        -------
        np.array
            The matrix corresponding to the current position of the camera.
        )r   rL   s    rG   r{   z ThreeDCamera.get_rotation_matrix  s     ##rH   c                X   |                                  }|                                 }|                                 }t          | dt          z  z
            t          | t                    t          |          g}t          j        d          }|D ]}t          j	        ||          }|S )zGenerates a rotation matrix based off the current position of the camera.

        Returns
        -------
        np.array
            The matrix corresponding to the current position of the camera.
        Z   r2   )
r   r   r   r   DEGREESr   RIGHTrb   identityrs   )rE   r*   r+   r,   matricesresultmatrixs          rG   r   z%ThreeDCamera.generate_rotation_matrix  s     llnn    eVb7l233SD%((U##

 Q 	, 	,FVFF++FFrH   rK   r   c                   | j         }|                                 }|                                 }|                                 }||z
  }t	          j        ||j                  }|dddf         }dD ]c}| j        r/t	          j        ||z            }|dk     }	||||	         z
  z  ||	<   n|||z
  z  }d|||z
  dk     <   |dd|fxx         ||z  z  cc<   d|S )a&  Applies the current rotation_matrix as a projection
        matrix to the passed array of points.

        Parameters
        ----------
        points
            The list of points to project.

        Returns
        -------
        np.array
            The points after projecting.
        Nr   )r   r!   r   i@B )	r0   r   r   r{   rb   rs   ru   r)   exp)
rE   rK   r0   r"   r-   rw   zsifactorlt0s
             rG   project_pointszThreeDCamera.project_points1  s    (0022}}--//
,&
--AAAqD\ 	* 	*A* 
:
 ^ 3441f,C0HIs'>B+>?49+q01111a4LLLFTM)LLLLrH   c                f    |                      |                    d                    dddf         S )a  Applies the current rotation_matrix as a projection
        matrix to the passed point.

        Parameters
        ----------
        point
            The point to project.

        Returns
        -------
        np.array
            The point after projection.
        )r!   r2   r   N)r   reshaperR   s     rG   project_pointzThreeDCamera.project_pointV  s1     ""5==#8#899!QQQ$??rH   mobjectr   c                
   t                                          ||          }|| j        v }|| j        v }|r|S |r4| j        |         } |            }|                     |          }|||z
  z   S |                     |          S rP   )r8   transform_points_pre_displayrA   rC   r   r   )	rE   r   rK   fixed_orientationfixed_in_framecenter_funccenter
new_centerrF   s	           rG   r   z)ThreeDCamera.transform_points_pre_displayf  s    
 55gvFF#t'FF D$@@ 	M 	/9'BK []]F++F33JZ&011&&v...rH   N)use_static_center_funcr   r   r   Callable[[], Point3D] | Nonec                   dd}|D ]=}|r|}n|r ||          }n|j         }|                                D ]}|| j        |<   >dS )a  This method allows the mobject to have a fixed orientation,
        even when the camera moves around.
        E.G If it was passed through this method, facing the camera, it
        will continue to face the camera even as the camera moves.
        Highly useful when adding labels to graphs and the like.

        Parameters
        ----------
        *mobjects
            The mobject whose orientation must be fixed.
        use_static_center_func
            Whether or not to use the function that takes the mobject's
            center as centerpoint, by default False
        center_func
            The function which returns the centerpoint
            with respect to which the mobject will be oriented, by default None
        r   r   rI   Callable[[], Point3D]c                6    |                                  fdS )Nc                      S rP   r4   )rM   s   rG   <lambda>z]ThreeDCamera.add_fixed_orientation_mobjects.<locals>.get_static_center_func.<locals>.<lambda>  s    5 rH   )
get_center)r   rM   s    @rG   get_static_center_funczKThreeDCamera.add_fixed_orientation_mobjects.<locals>.get_static_center_func  s     &&((E === rH   N)r   r   rI   r   )r   
get_familyrA   )rE   r   r   rS   r   r   funcsubmobs           rG   add_fixed_orientation_mobjectsz+ThreeDCamera.add_fixed_orientation_mobjectsy  s    4	! 	! 	! 	!   	? 	?G *"' *--g66)!,,.. ? ?:>/77?	? 	?rH   c                ^    t          |          D ]}| j                            |           dS )a  This method allows the mobject to have a fixed position,
        even when the camera moves around.
        E.G If it was passed through this method, at the top of the frame, it
        will continue to be displayed at the top of the frame.

        Highly useful when displaying Titles or formulae or the like.

        Parameters
        ----------
        **mobjects
            The mobject to fix in frame.
        N)r   rC   addrE   rS   r   s      rG   add_fixed_in_frame_mobjectsz(ThreeDCamera.add_fixed_in_frame_mobjects  s?     6h?? 	6 	6G(,,W5555	6 	6rH   c                L    t          |          D ]}|| j        v r| j        |= dS )aU  If a mobject was fixed in its orientation by passing it through
        :meth:`.add_fixed_orientation_mobjects`, then this undoes that fixing.
        The Mobject will no longer have a fixed orientation.

        Parameters
        ----------
        mobjects
            The mobjects whose orientation need not be fixed any longer.
        N)r   rA   r   s      rG   !remove_fixed_orientation_mobjectsz.ThreeDCamera.remove_fixed_orientation_mobjects  s?     6h?? 	= 	=G$9993G<	= 	=rH   c                p    t          |          D ]%}|| j        v r| j                            |           &dS )a>  If a mobject was fixed in frame by passing it through
        :meth:`.add_fixed_in_frame_mobjects`, then this undoes that fixing.
        The Mobject will no longer be fixed in frame.

        Parameters
        ----------
        mobjects
            The mobjects which need not be fixed in frame any longer.
        N)r   rC   remover   s      rG   remove_fixed_in_frame_mobjectsz+ThreeDCamera.remove_fixed_in_frame_mobjects  sK     6h?? 	= 	=G$666,33G<<<	= 	=rH   )r"   r#   r$   r#   r%   r#   r&   r   r'   r(   r)   r(   r*   r#   r+   r#   r,   r#   r-   r#   r.   r   )rI   r   )rM   r   rI   rN   )rS   rT   r.   r   rI   rN   )rI   rW   )rZ   r   r[   r   rI   r   )F)rZ   r   rg   r(   rI   r   )rZ   r   rI   r   )rl   r   r.   r   rI   rm   )rI   r#   )r   r#   rI   rN   )rI   rN   )rI   r   )rK   r   rI   r   )rM   r   rI   r   )r   r   rK   r   rI   r   )rS   r   r   r(   r   r   rI   rN   )rS   r   rI   rN   )&__name__
__module____qualname__DOWNLEFTOUTr   r9   propertyr0   setterrV   rY   rf   ri   rk   rz   r   r   r   r   r   r   r   r   r   r   rD   r{   r   r   r   r   r   r   r   r   __classcell__)rF   s   @rG   r   r   '   s        !% #"%01D1t80Cb3h0N%)',W}*% *% *% *% *% *% *%X , , , X, * * * *5 5 5 5 5 5
 
 
 
"   : !T T T T TH H H H
+ + + + + + , , , ,. . . .7 7 7 7	. 	. 	. 	.- - - -* * * *, , , ,5 5 5 5, , , ,+ + + +? ? ? ?$ $ $ $   *# # # #J@ @ @ @ / / / / / /, (-48	&? &? &? &? &? &?P6 6 6 6 = = = == = = = = = = =rH   )+__doc__
__future__r   __all__collections.abcr   r   typingr   numpyrb   manim.mobject.mobjectr   #manim.mobject.three_d.three_d_utilsr	   r
   r   r   &manim.mobject.types.vectorized_mobjectr   manim.mobject.value_trackerr   manim.typingr   r   r   r   r    r   camera.camerar   	constants!mobject.types.point_cloud_mobjectr   utils.colorr   utils.familyr   utils.space_opsr   r   r   r4   rH   rG   <module>r      s   N N " " " " " "
 / . . . . . . .           ) ) ) ) ) )            < ; ; ; ; ; 4 4 4 4 4 4                    " " " " " "     5 5 5 5 5 5 ( ( ( ( ( ( 9 9 9 9 9 9 ? ? ? ? ? ? ? ?d= d= d= d= d=6 d= d= d= d= d=rH   