U
    EvfT                     @   s  d Z ddlZddlZddlmZ ddlmZmZm	Z
mZmZ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 d
d Zdd Zdd ZG dd dejZdDddZ G dd dej!Z"dEddZ#dFddZ$dGddZ%dHddZ&dIdd Z'G d!d" d"eZ(dJd#d$Z)G d%d& d&eZ*G d'd( d(e*Z+d)d* Z,dKd+d,Z-dLd-d.Z.G d/d0 d0eZ/G d1d2 d2eZ0dMd4d5Z1G d6d7 d7eZ2dNd8d9Z3d:d; Z4d<d= Z5d>d? Z6d@dA Z7dOdBdCZ8dS )Pzx
Module containing 3D artist code and functions to convert 2D
artists into 3D versions which can be added to an Axes3D.
    N)contextmanager)artistcbookcolorslinestextpath)LineCollectionPolyCollectionPatchCollectionPathCollection)	Normalize)Patch   )proj3dc                 C   s    | d d } | dkr| d } | S )z?Return the given angle normalized to -180 < *a* <= 180 degrees.ih      ar   r   >/tmp/pip-unpacked-wheel-7vhvci0g/mpl_toolkits/mplot3d/art3d.py_norm_angle   s    r   c                 C   s    | d d } | dkr| d } | S )z=Return the given angle normalized to -90 < *a* <= 90 degrees.r   Z   r   r   r   r   r   _norm_text_angle"   s    r   c                 C   st   | dkrt dS | dkr$t dS | dkr6t dS | dkrHt dS t | rht| d	krht | S td
dS )a  
    Return a direction vector.

    Parameters
    ----------
    zdir : {'x', 'y', 'z', None, 3-tuple}
        The direction. Possible values are:

        - 'x': equivalent to (1, 0, 0)
        - 'y': equivalent to (0, 1, 0)
        - 'z': equivalent to (0, 0, 1)
        - *None*: equivalent to (0, 0, 0)
        - an iterable (x, y, z) is converted to a NumPy array, if not already

    Returns
    -------
    x, y, z : array-like
        The direction vector.
    x)r   r   r   y)r   r   r   z)r   r   r   N)r   r   r      z2'x', 'y', 'z', None or vector of length 3 expected)nparrayiterablelen
ValueErrorzdirr   r   r   get_dir_vector*   s    




r$   c                       s^   e Zd ZdZdddZdd Zd fd
d	Zdd ZdddZe	j
dd ZdddZ  ZS )Text3Da  
    Text object with 3D position and direction.

    Parameters
    ----------
    x, y, z : float
        The position of the text.
    text : str
        The text string to display.
    zdir : {'x', 'y', 'z', None, 3-tuple}
        The direction of the text. See `.get_dir_vector` for a description of
        the values.

    Other Parameters
    ----------------
    **kwargs
         All other parameters are passed on to `~matplotlib.text.Text`.
   r    r   c                 K   s&   t jj| |||f| | || d S N)mtextText__init__set_3d_properties)selfr   r   r   r   r#   kwargsr   r   r   r*   `   s    zText3D.__init__c                 C   s   | j | j| jfS )z*Return the (x, y, z) position of the text.)_x_y_zr,   r   r   r   get_position_3dd   s    zText3D.get_position_3dNc                    s8   t  |dd  | |d  |dk	r4t|| _dS )ax  
        Set the (*x*, *y*, *z*) position of the text.

        Parameters
        ----------
        xyz : (float, float, float)
            The position in 3D space.
        zdir : {'x', 'y', 'z', None, 3-tuple}
            The direction of the text. If unspecified, the *zdir* will not be
            changed. See `.get_dir_vector` for a description of the values.
        N   )superZset_positionset_zr$   _dir_vec)r,   xyzr#   	__class__r   r   set_position_3dh   s    zText3D.set_position_3dc                 C   s   || _ d| _dS )zl
        Set the *z* position of the text.

        Parameters
        ----------
        z : float
        TN)r0   stale)r,   r   r   r   r   r5   y   s    zText3D.set_zc                 C   s   || _ t|| _d| _dS )aB  
        Set the *z* position and direction of the text.

        Parameters
        ----------
        z : float
            The z-position in 3D space.
        zdir : {'x', 'y', 'z', 3-tuple}
            The direction of the text. Default: 'z'.
            See `.get_dir_vector` for a description of the values.
        TN)r0   r$   r6   r;   )r,   r   r#   r   r   r   r+      s    
zText3D.set_3d_propertiesc              	   C   s   t | j| j| jf}t||| j g| jj	}|d d |d d  }|d d |d d  }t
t
||}tj| |d d |d d t|d tj| | W 5 Q R X d| _d S )Nr   r   )r.   r/   Z	_rotationF)r   r   r.   r/   r0   r   proj_trans_pointsr6   axesMmathdegreesatan2r   _setattr_cmr   r(   r)   drawr;   )r,   rendererZ
position3dZprojZdxZdyZangler   r   r   rC      s     zText3D.drawc                 C   s   d S r'   r   r,   rD   r   r   r   get_tightbbox   s    zText3D.get_tightbbox)r   r   r   r&   r   )N)r   r   )N)__name__
__module____qualname____doc__r*   r2   r:   r5   r+   r   allow_rasterizationrC   rF   __classcell__r   r   r8   r   r%   L   s   


r%   r   c                 C   s   t | _| || dS )a  
    Convert a `.Text` to a `.Text3D` object.

    Parameters
    ----------
    z : float
        The z-position in 3D space.
    zdir : {'x', 'y', 'z', 3-tuple}
        The direction of the text. Default: 'z'.
        See `.get_dir_vector` for a description of the values.
    N)r%   r9   r+   )objr   r#   r   r   r   text_2d_to_3d   s    rN   c                       sL   e Zd ZdZ fddZdddZdd	 Zd
d Zej	 fddZ
  ZS )Line3Da  
    3D line object.

    .. note:: Use `get_data_3d` to obtain the data associated with the line.
            `~.Line2D.get_data`, `~.Line2D.get_xdata`, and `~.Line2D.get_ydata` return
            the x- and y-coordinates of the projected 2D-line, not the x- and y-data of
            the 3D-line. Similarly, use `set_data_3d` to set the data, not
            `~.Line2D.set_data`, `~.Line2D.set_xdata`, and `~.Line2D.set_ydata`.
    c                    s(   t  jg g f|| | ||| dS )aO  

        Parameters
        ----------
        xs : array-like
            The x-data to be plotted.
        ys : array-like
            The y-data to be plotted.
        zs : array-like
            The z-data to be plotted.
        *args, **kwargs :
            Additional arguments are passed to `~matplotlib.lines.Line2D`.
        N)r4   r*   set_data_3d)r,   xsyszsargsr-   r8   r   r   r*      s    zLine3D.__init__r   r   c                 C   sH   |   }|  }t| }t|t|}t||||| _	d| _
dS )a  
        Set the *z* position and direction of the line.

        Parameters
        ----------
        zs : float or array of floats
            The location along the *zdir* axis in 3D space to position the
            line.
        zdir : {'x', 'y', 'z'}
            Plane to plot line orthogonal to. Default: 'z'.
            See `.get_dir_vector` for a description of the values.
        TN)Z	get_xdataZ	get_ydatar   Z_to_unmasked_float_arrayZravelr   broadcast_tor    juggle_axes_verts3dr;   )r,   rS   r#   rQ   rR   r   r   r   r+      s    zLine3D.set_3d_propertiesc                 G   sP   t |dkr|d }td|D ] \}}t|st| dq|| _d| _dS )ag  
        Set the x, y and z data

        Parameters
        ----------
        x : array-like
            The x-data to be plotted.
        y : array-like
            The y-data to be plotted.
        z : array-like
            The z-data to be plotted.

        Notes
        -----
        Accepts x, y, z arguments or a single array-like (x, y, z)
        r   r   r7   z must be a sequenceTN)r    zipr   r   RuntimeErrorrW   r;   )r,   rT   namer7   r   r   r   rP      s    
zLine3D.set_data_3dc                 C   s   | j S )z
        Get the current data

        Returns
        -------
        verts3d : length-3 tuple or array-like
            The current data as a tuple or array-like.
        )rW   r1   r   r   r   get_data_3d   s    	zLine3D.get_data_3dc                    sH   | j \}}}t|||| jj\}}}| || t | d| _d S )NF)	rW   r   Zproj_transformr=   r>   set_datar4   rC   r;   )r,   rD   Zxs3dZys3dZzs3drQ   rR   rS   r8   r   r   rC     s
    zLine3D.draw)r   r   )rG   rH   rI   rJ   r*   r+   rP   r[   r   rK   rC   rL   r   r   r8   r   rO      s   

rO   c                 C   s   t | _| || dS )aA  
    Convert a `.Line2D` to a `.Line3D` object.

    Parameters
    ----------
    zs : float
        The location along the *zdir* axis in 3D space to position the line.
    zdir : {'x', 'y', 'z'}
        Plane to plot line orthogonal to. Default: 'z'.
        See `.get_dir_vector` for a description of the values.
    N)rO   r9   r+   )linerS   r#   r   r   r   line_2d_to_3d  s    r^   c                    sH   t |t| }| jddd}dd t||D } fdd|D }|S )zConvert a path to a 3D segment.FZsimplifyZcurvesc                 S   s"   g | ]\\\}}}}|||fqS r   r   .0r   r   coder   r   r   r   
<listcomp>*  s     z'_path_to_3d_segment.<locals>.<listcomp>c                    s    g | ]\}}}t ||| qS r   rV   ra   r   r   r   r"   r   r   rc   +  s     )r   rU   r    iter_segmentsrX   )r   rS   r#   pathsegssegseg3dr   r"   r   _path_to_3d_segment%  s
    rj   c                    sP   t |st |t| }nt|t| kr4td fddt| |D }|S )z6Convert paths from a collection object to 3D segments.z-Number of z-coordinates does not match paths.c                    s   g | ]\}}t || qS r   )rj   ra   r   Zpathzr"   r   r   rc   8  s   z)_paths_to_3d_segments.<locals>.<listcomp>)r   r   rU   r    r!   rX   )pathsrS   r#   Zsegsr   r"   r   _paths_to_3d_segments/  s    

rm   c                    sj   t |t| }| jddd}dd t||D }|rVt| \}} fdd|D }ng }g }|t|fS )z/Convert a path to a 3D segment with path codes.Fr_   c                 S   s&   g | ]\\\}}}}|||f|fqS r   r   r`   r   r   r   rc   B  s     z2_path_to_3d_segment_with_codes.<locals>.<listcomp>c                    s    g | ]\}}}t ||| qS r   rd   re   r"   r   r   rc   E  s     )r   rU   r    rf   rX   list)r   rS   r#   rg   Z	seg_codesrh   codesri   r   r"   r   _path_to_3d_segment_with_codes=  s    rp   c                    sT   t |t| } fddt| |D }|r:t| \}}n
g g  }}t|t|fS )zP
    Convert paths from a collection object to 3D segments with path codes.
    c                    s   g | ]\}}t || qS r   )rp   rk   r"   r   r   rc   R  s   z4_paths_to_3d_segments_with_codes.<locals>.<listcomp>)r   rU   r    rX   rn   )rl   rS   r#   Zsegments_codessegmentsro   r   r"   r    _paths_to_3d_segments_with_codesL  s    

rr   c                       s0   e Zd ZdZdd Z fddZdd Z  ZS )Line3DCollectionz#
    A collection of 3D lines.
    c                 C   s   || _ d| _dS z&Set the position to use for z-sorting.TN
_sort_zposr;   r,   valr   r   r   set_sort_zpos`  s    zLine3DCollection.set_sort_zposc                    s   || _ t g  dS )z"
        Set 3D segments.
        N)_segments3dr4   set_segments)r,   rq   r8   r   r   r{   e  s    zLine3DCollection.set_segmentsc                    sT    fdd j D }dd |D }t | d}|D ]\}}}t|t|}q6|S )zB
        Project the points according to renderer matrix.
        c                    s   g | ]}t | jjqS r   )r   r<   r=   r>   )ra   Zpointsr1   r   r   rc   p  s   z5Line3DCollection.do_3d_projection.<locals>.<listcomp>c                 S   s    g | ]\}}}t ||gqS r   )r   column_stack)ra   rQ   rR   rS   r   r   r   rc   r  s     g    eA)rz   r	   r{   min)r,   Zxyslistsegments_2dZminzrQ   rR   rS   r   r1   r   do_3d_projectionl  s    
z!Line3DCollection.do_3d_projection)rG   rH   rI   rJ   ry   r{   r   rL   r   r   r8   r   rs   [  s   rs   c                 C   s$   t |  ||}t| _| | dS )z<Convert a `.LineCollection` to a `.Line3DCollection` object.N)rm   	get_pathsrs   r9   r{   )colrS   r#   
segments3dr   r   r   line_collection_2d_to_3d|  s    r   c                       sB   e Zd ZdZddd fdd
Zddd	Zd
d Zdd Z  ZS )Patch3Dz
    3D patch object.
    r   r   rS   r#   c                   s   t  j|| | || dS )aO  
        Parameters
        ----------
        verts :
        zs : float
            The location along the *zdir* axis in 3D space to position the
            patch.
        zdir : {'x', 'y', 'z'}
            Plane to plot patch orthogonal to. Default: 'z'.
            See `.get_dir_vector` for a description of the values.
        N)r4   r*   r+   )r,   rS   r#   rT   r-   r8   r   r   r*     s    zPatch3D.__init__r   c                    s.   t |t|} fddt||D | _dS )a  
        Set the *z* position and direction of the patch.

        Parameters
        ----------
        verts :
        zs : float
            The location along the *zdir* axis in 3D space to position the
            patch.
        zdir : {'x', 'y', 'z'}
            Plane to plot patch orthogonal to. Default: 'z'.
            See `.get_dir_vector` for a description of the values.
        c                    s"   g | ]\\}}}t ||| qS r   rd   re   r"   r   r   rc     s   
z-Patch3D.set_3d_properties.<locals>.<listcomp>N)r   rU   r    rX   
_segment3d)r,   vertsrS   r#   r   r"   r   r+     s    
zPatch3D.set_3d_propertiesc                 C   s   | j S r'   )_path2dr1   r   r   r   get_path  s    zPatch3D.get_pathc           	      C   sN   | j }t| \}}}t|||| jj\}}}}tt	||g| _
t|S r'   )r   rX   r   proj_transform_clipr=   r>   mpathPathr   r|   r   r}   	r,   srQ   rR   rS   vxsvysvzsvisr   r   r   r     s    
zPatch3D.do_3d_projection)r   r   )	rG   rH   rI   rJ   r*   r+   r   r   rL   r   r   r8   r   r     s
   
r   c                   @   s2   e Zd ZdZdddddZddd	Zd
d ZdS )PathPatch3Dz
    3D PathPatch object.
    r   r   r   c                K   s    t j| f| | ||| dS )aa  
        Parameters
        ----------
        path :
        zs : float
            The location along the *zdir* axis in 3D space to position the
            path patch.
        zdir : {'x', 'y', 'z', 3-tuple}
            Plane to plot path patch orthogonal to. Default: 'z'.
            See `.get_dir_vector` for a description of the values.
        N)r   r*   r+   )r,   r   rS   r#   r-   r   r   r   r*     s    zPathPatch3D.__init__r   c                 C   s    t j| |j||d |j| _dS )a  
        Set the *z* position and direction of the path patch.

        Parameters
        ----------
        path :
        zs : float
            The location along the *zdir* axis in 3D space to position the
            path patch.
        zdir : {'x', 'y', 'z', 3-tuple}
            Plane to plot path patch orthogonal to. Default: 'z'.
            See `.get_dir_vector` for a description of the values.
        r   N)r   r+   Zverticesro   _code3d)r,   r   rS   r#   r   r   r   r+     s    zPathPatch3D.set_3d_propertiesc           	      C   sR   | j }t| \}}}t|||| jj\}}}}tt	||g| j
| _t|S r'   )r   rX   r   r   r=   r>   r   r   r   r|   r   r   r}   r   r   r   r   r     s    
zPathPatch3D.do_3d_projectionN)r   r   )rG   rH   rI   rJ   r*   r+   r   r   r   r   r   r     s   
r   c                 C   s4   |   }|  }||}t|r*|d S tg S )z2Return a list of vertices for the path of a patch.r   )get_patch_transformr   Zto_polygonsr    r   r   )patchtransr   polygonsr   r   r   _get_patch_verts  s    
r   c                 C   s    t | }t| _| ||| dS )z*Convert a `.Patch` to a `.Patch3D` object.N)r   r   r9   r+   )r   r   r#   r   r   r   r   patch_2d_to_3d  s    r   c                 C   s2   |   }|  }||}t| _| ||| dS )z2Convert a `.PathPatch` to a `.PathPatch3D` object.N)r   r   Ztransform_pathr   r9   r+   )Z	pathpatchr   r#   r   r   r   r   r   r   pathpatch_2d_to_3d  s
    
r   c                       sv   e Zd ZdZdddd fdd
Zdd	 Zd
d Zdd Zdd Z fddZ	dd Z
 fddZ fddZ  ZS )Patch3DCollectionz%
    A collection of 3D patches.
    r   r   TrS   r#   
depthshadec                   s$   || _ t j|| | || dS )a  
        Create a collection of flat 3D patches with its normal vector
        pointed in *zdir* direction, and located at *zs* on the *zdir*
        axis. 'zs' can be a scalar or an array-like of the same length as
        the number of patches in the collection.

        Constructor arguments are the same as for
        :class:`~matplotlib.collections.PatchCollection`. In addition,
        keywords *zs=0* and *zdir='z'* are available.

        Also, the keyword argument *depthshade* is available to indicate
        whether to shade the patches in order to give the appearance of depth
        (default is *True*). This is typically desired in scatter plots.
        N)_depthshader4   r*   r+   r,   rS   r#   r   rT   r-   r8   r   r   r*     s    zPatch3DCollection.__init__c                 C   s   | j S r'   r   r1   r   r   r   get_depthshade  s    z Patch3DCollection.get_depthshadec                 C   s   || _ d| _dS z
        Set whether depth shading is performed on collection members.

        Parameters
        ----------
        depthshade : bool
            Whether to shade the patches in order to give the appearance of
            depth.
        TNr   r;   r,   r   r   r   r   set_depthshade  s    
z Patch3DCollection.set_depthshadec                 C   s   || _ d| _dS rt   ru   rw   r   r   r   ry   %  s    zPatch3DCollection.set_sort_zposc                 C   s`   |    |  }t|dkr(|j\}}ng }g }t||t||| _td| _	d| _
d| _dS )a  
        Set the *z* positions and direction of the patches.

        Parameters
        ----------
        zs : float or array of floats
            The location or locations to place the patches in the collection
            along the *zdir* axis.
        zdir : {'x', 'y', 'z'}
            Plane to plot patches orthogonal to.
            All patches must have the same direction.
            See `.get_dir_vector` for a description of the values.
        r   NT)update_scalarmappableget_offsetsr    TrV   r   
atleast_1d
_offsets3dslice_z_markers_idx_vzsr;   r,   rS   r#   offsetsrQ   rR   r   r   r   r+   *  s    
z#Patch3DCollection.set_3d_propertiesc                    s`   | j \}}}t|||| jj\}}}}|| _t t	||g |j
dkrVt|S tjS d S Nr   )r   r   r   r=   r>   r   r4   set_offsetsr   r|   sizer}   nan)r,   rQ   rR   rS   r   r   r   r   r8   r   r   r   F  s    

z"Patch3DCollection.do_3d_projectionc                 C   sD   | j d k	r| jrt|| j n|}t|dkr6|| j }t|| jS Nr   r   r   _zalphar    r   mcolorsto_rgba_array_alphar,   Zcolor_arrayr   r   r   "_maybe_depth_shade_and_sort_colorsR  s    
z4Patch3DCollection._maybe_depth_shade_and_sort_colorsc                    s   |  t  S r'   r   r4   get_facecolorr1   r8   r   r   r   \  s    zPatch3DCollection.get_facecolorc                    s&   t | jdr|  S | t  S Nfacer   Z
_str_equal_edgecolorsr   r   r4   get_edgecolorr1   r8   r   r   r   _  s    zPatch3DCollection.get_edgecolor)rG   rH   rI   rJ   r*   r   r   ry   r+   r   r   r   r   rL   r   r   r8   r   r     s   
r   c                       s   e Zd ZdZdddd fdd
Z fdd	Zd
d Zdd Zd! fdd	Z fddZ	dd Z
dd Zdd Ze fddZdd Z fddZ fdd Z  ZS )"Path3DCollectionz#
    A collection of 3D paths.
    r   r   Tr   c                   s0   || _ d| _t j|| | || d| _dS )a  
        Create a collection of flat 3D paths with its normal vector
        pointed in *zdir* direction, and located at *zs* on the *zdir*
        axis. 'zs' can be a scalar or an array-like of the same length as
        the number of paths in the collection.

        Constructor arguments are the same as for
        :class:`~matplotlib.collections.PathCollection`. In addition,
        keywords *zs=0* and *zdir='z'* are available.

        Also, the keyword argument *depthshade* is available to indicate
        whether to shade the patches in order to give the appearance of depth
        (default is *True*). This is typically desired in scatter plots.
        FN)r   _in_drawr4   r*   r+   _offset_zorderedr   r8   r   r   r*   m  s
    zPath3DCollection.__init__c              
      s>   |   , tj| dd t | W 5 Q R X W 5 Q R X d S )NT)r   )_use_zordered_offsetr   rB   r4   rC   rE   r8   r   r   rC     s    
zPath3DCollection.drawc                 C   s   || _ d| _dS rt   ru   rw   r   r   r   ry     s    zPath3DCollection.set_sort_zposc                 C   s   |    |  }t|dkr(|j\}}ng }g }t||t||| _| j| _	t
| j| _| j\}}}td| _d| _d| _dS )a  
        Set the *z* positions and direction of the paths.

        Parameters
        ----------
        zs : float or array of floats
            The location or locations to place the paths in the collection
            along the *zdir* axis.
        zdir : {'x', 'y', 'z'}
            Plane to plot paths orthogonal to.
            All paths must have the same direction.
            See `.get_dir_vector` for a description of the values.
        r   r   NT)r   r   r    r   rV   r   r   r   _sizes_sizes3dr   _linewidths_linewidths3dr   r   r   r;   r   r   r   r   r+     s    
z"Path3DCollection.set_3d_properties      R@c                    s   t  || | js|| _d S r'   )r4   	set_sizesr   r   )r,   ZsizesZdpir8   r   r   r     s    zPath3DCollection.set_sizesc                    s$   t  | | js t| j| _d S r'   )r4   set_linewidthr   r   r   r   r   )r,   Zlwr8   r   r   r     s    zPath3DCollection.set_linewidthc                 C   s   | j S r'   r   r1   r   r   r   r     s    zPath3DCollection.get_depthshadec                 C   s   || _ d| _dS r   r   r   r   r   r   r     s    
zPath3DCollection.set_depthshadec           	      C   s   | j \}}}t|||| jj\}}}}t|d d d  }| _|| _t	| j
dkrb| j
| | _t	| jdkr|| j| | _t| t||f || }|| }|| }t||f| _|jrt|S tjS )Nr   r   )r   r   r   r=   r>   r   Zargsortr   r   r    r   r   r   r   r   r   r|   r   r   r}   r   )	r,   rQ   rR   rS   r   r   r   r   Zz_markers_idxr   r   r   r     s     
z!Path3DCollection.do_3d_projectionc                 #   s>   | j d krd V  n(| j}t | j  z
d V  W 5 || _X d S r'   )r   Z_offsetsr4   r   )r,   Z
old_offsetr8   r   r   r     s    

z%Path3DCollection._use_zordered_offsetc                 C   sD   | j d k	r| jrt|| j n|}t|dkr6|| j }t|| jS r   r   r   r   r   r   r     s    
z3Path3DCollection._maybe_depth_shade_and_sort_colorsc                    s   |  t  S r'   r   r1   r8   r   r   r     s    zPath3DCollection.get_facecolorc                    s&   t | jdr|  S | t  S r   r   r1   r8   r   r   r     s    zPath3DCollection.get_edgecolor)r   )rG   rH   rI   rJ   r*   rC   ry   r+   r   r   r   r   r   r   r   r   r   r   rL   r   r   r8   r   r   h  s   -!
r   Tc                 C   sD   t | trt| _d| _nt | tr(t| _|| _d| _| 	|| dS )a6  
    Convert a `.PatchCollection` into a `.Patch3DCollection` object
    (or a `.PathCollection` into a `.Path3DCollection` object).

    Parameters
    ----------
    zs : float or array of floats
        The location or locations to place the patches in the collection along
        the *zdir* axis. Default: 0.
    zdir : {'x', 'y', 'z'}
        The axis in which to place the patches. Default: "z".
        See `.get_dir_vector` for a description of the values.
    depthshade
        Whether to shade the patches to give a sense of depth. Default: *True*.

    NF)

isinstancer   r   r9   r   r   r   r   r   r+   )r   rS   r#   r   r   r   r   patch_collection_2d_to_3d  s    

r   c                       s   e Zd ZdZdddd fdd
ZejejejdZ	d	d
 Z
dd Zd" fdd	Zdd Zdd Zdd Zdd Z fddZ fddZdd Zdd Zd d! Z  ZS )#Poly3DCollectiona  
    A collection of 3D polygons.

    .. note::
        **Filling of 3D polygons**

        There is no simple definition of the enclosed surface of a 3D polygon
        unless the polygon is planar.

        In practice, Matplotlib fills the 2D projection of the polygon. This
        gives a correct filling appearance only for planar polygons. For all
        other polygons, you'll find orientations in which the edges of the
        polygon intersect in the projection. This will lead to an incorrect
        visualization of the 3D area.

        If you need filled areas, it is recommended to create them via
        `~mpl_toolkits.mplot3d.axes3d.Axes3D.plot_trisurf`, which creates a
        triangulation and thus generates consistent surfaces.
    averageFN)zsortshadelightsourcec          
         s   |rlt |}|dd}|dk	r0t||||d< |dd}	|	dk	rTt|	|||d< |dkrl|	dkrltdt j|f|| t|tjr|j	dkrtdnt
dd |D rtd| | d| _dS )	a"  
        Parameters
        ----------
        verts : list of (N, 3) array-like
            The sequence of polygons [*verts0*, *verts1*, ...] where each
            element *verts_i* defines the vertices of polygon *i* as a 2D
            array-like of shape (N, 3).
        zsort : {'average', 'min', 'max'}, default: 'average'
            The calculation method for the z-order.
            See `~.Poly3DCollection.set_zsort` for details.
        shade : bool, default: False
            Whether to shade *facecolors* and *edgecolors*. When activating
            *shade*, *facecolors* and/or *edgecolors* must be provided.

            .. versionadded:: 3.7

        lightsource : `~matplotlib.colors.LightSource`, optional
            The lightsource to use when *shade* is True.

            .. versionadded:: 3.7

        *args, **kwargs
            All other parameters are forwarded to `.PolyCollection`.

        Notes
        -----
        Note that this class does a bit of magic with the _facecolors
        and _edgecolors properties.
        
facecolorsN
edgecolorszCYou must provide facecolors, edgecolors, or both for shade to work.r   z)verts must be a list of (N, 3) array-likec                 s   s    | ]}t t|d kV  qdS )r3   N)r    r   shape)ra   Zvertr   r   r   	<genexpr>}  s     z,Poly3DCollection.__init__.<locals>.<genexpr>)_generate_normalsget_shade_colorsr!   r4   r*   r   r   ndarrayndimany	set_zsort_codes3d)
r,   r   r   r   r   rT   r-   normalsr   r   r8   r   r   r*   H  s8        


zPoly3DCollection.__init__)r   r}   maxc                 C   s   | j | | _d| _d| _dS )a  
        Set the calculation method for the z-order.

        Parameters
        ----------
        zsort : {'average', 'min', 'max'}
            The function applied on the z-coordinates of the vertices in the
            viewer's coordinate system, to determine the z-order.
        NT)_zsort_functions
_zsortfuncrv   r;   )r,   r   r   r   r   r     s    
zPoly3DCollection.set_zsortc                 C   s   t |rt|j\}}}ng g g   }}}tt |}t||||g| _dtdd |D }tt	|dd |dd | _
dS )zOptimize points for projection.r   c                 S   s   g | ]}t |qS r   )r    )ra   segmentr   r   r   rc     s     z/Poly3DCollection.get_vector.<locals>.<listcomp>Nr   r   )r   )r    r   Z	row_stackr   onesr   _vecZcumsummapr   
_segslices)r,   r   rQ   rR   rS   r   indicesr   r   r   
get_vector  s    zPoly3DCollection.get_vectorTc                    s"   |  | t g d || _dS )a  
        Set 3D vertices.

        Parameters
        ----------
        verts : list of (N, 3) array-like
            The sequence of polygons [*verts0*, *verts1*, ...] where each
            element *verts_i* defines the vertices of polygon *i* as a 2D
            array-like of shape (N, 3).
        closed : bool, default: True
            Whether the polygon should be closed by adding a CLOSEPOLY
            connection at the end.
        FN)r   r4   	set_verts_closed)r,   r   closedr8   r   r   r     s    
zPoly3DCollection.set_vertsc                 C   s   | j |dd || _dS )z Set 3D vertices with path codes.F)r   N)r   r   )r,   r   ro   r   r   r   set_verts_and_codes  s    z$Poly3DCollection.set_verts_and_codesc                 C   sF   |    d | _| d t| | _t| | _t| | _	d| _
d S )Nr   T)r   rv   r   r
   r   _facecolor3dr   _edgecolor3dZ	get_alphaZ_alpha3dr;   r1   r   r   r   r+     s    
z"Poly3DCollection.set_3d_propertiesc                 C   s   || _ d| _dS rt   ru   rw   r   r   r   ry     s    zPoly3DCollection.set_sort_zposc                    s   j dk	r.    jr  j _ jr. j _t	 j
 jj\fdd jD } j} j}t|t|kr|jt|dd}t|t|krt|dkr|}n|jt|dd}|r
t fddtt|||D dd	 d
d}t| \}} _ _}n g }td _td _g } jdk	rX fdd|D }t || nt | j t jt|kr j _ jdk	rtdgdg jgdgg}	t	|	 jj}
|
d d S jdkrtS tj S dS )z<
        Perform the 3D projection for this object.
        Nc                    s"   g | ]} | | | fqS r   r   )ra   sl)txstystzsr   r   rc     s     z5Poly3DCollection.do_3d_projection.<locals>.<listcomp>r   )axisc                 3   s<   | ]4\}\\}}}}}  |t||g|||fV  qd S r'   )r   r   r|   )ra   idxrQ   rR   rS   ZfcZecr1   r   r   r     s   z4Poly3DCollection.do_3d_projection.<locals>.<genexpr>c                 S   s   | d S r   r   r   r   r   r   <lambda>      z3Poly3DCollection.do_3d_projection.<locals>.<lambda>T)keyreverser      c                    s   g | ]} j | qS r   )r   )ra   r   r1   r   r   rc     s     r   r3   )!Z_Ar   Z_face_is_mappedZ_facecolorsr   Z_edge_is_mappedr   r   r   Z_proj_transform_vecr   r=   r>   r   r    repeatsorted	enumeraterX   _facecolors2d_edgecolors2dr   emptyr   r
   r   r   r   rv   r   r   r}   r   )r,   ZxyzlistZcfaceZcedgeZz_segments_2d_r~   Zidxsro   ZzvecZztransr   )r,   r   r   r   r   r     sV    

 
z!Poly3DCollection.do_3d_projectionc                    s   t  | t| | _d S r'   )r4   set_facecolorr
   r   r   r,   r   r8   r   r   r    s    zPoly3DCollection.set_facecolorc                    s   t  | t| | _d S r'   )r4   set_edgecolorr
   r   r   r	  r8   r   r   r
    s    zPoly3DCollection.set_edgecolorc              
   C   s|   t j| | zt| j| j| _W n ttt	fk
r>   Y nX zt| j
| j| _W n ttt	fk
rp   Y nX d| _d S )NT)r   ZArtist	set_alphar   r   r   r   AttributeError	TypeError
IndexErrorr   r   r;   )r,   alphar   r   r   r    s      
 
zPoly3DCollection.set_alphac                 C   s&   t | ds | j | j_|   | jS )Nr  )hasattrr=   get_projr>   r   r  r1   r   r   r   r   +  s    
zPoly3DCollection.get_facecolorc                 C   s&   t | ds | j | j_|   | jS )Nr  )r  r=   r  r>   r   r  r1   r   r   r   r   3  s    
zPoly3DCollection.get_edgecolor)T)rG   rH   rI   rJ   r*   r   r   r}   r   r   r   r   r   r   r+   ry   r   r  r
  r  r   r   rL   r   r   r8   r   r   3  s(   ;Er   c                 C   s2   t |  ||\}}t| _| || |   dS )a  
    Convert a `.PolyCollection` into a `.Poly3DCollection` object.

    Parameters
    ----------
    zs : float or array of floats
        The location or locations to place the polygons in the collection along
        the *zdir* axis. Default: 0.
    zdir : {'x', 'y', 'z'}
        The axis in which to place the patches. Default: 'z'.
        See `.get_dir_vector` for a description of the values.
    N)rr   r   r   r9   r   r+   )r   rS   r#   Zsegments_3dro   r   r   r   poly_collection_2d_to_3d<  s      r  c                 C   sL   |dkr|| |fS |dkr$| ||fS |d dkr>t | |||S | ||fS dS )z
    Reorder coordinates so that 2D *xs*, *ys* can be plotted in the plane
    orthogonal to *zdir*. *zdir* is normally 'x', 'y' or 'z'. However, if
    *zdir* starts with a '-' it is interpreted as a compensation for
    `rotate_axes`.
    r   r   r   -N)rotate_axesrQ   rR   rS   r#   r   r   r   rV   P  s    

rV   c                 C   s2   |dkr||| fS |dkr$|| |fS | ||fS dS )z
    Reorder coordinates so that the axes are rotated with *zdir* along
    the original z axis. Prepending the axis with a '-' does the
    inverse transform, so *zdir* can be 'x', '-x', 'y', '-y', 'z' or '-z'.
    )r   z-y)z-xr   Nr   r  r   r   r   r  a  s
    

r  c                 C   s   t | dkst |dkr"tdS tt|t|}d||d  }tt| t |df}t	|ddddf |dddf | gS )z7Modify the alphas of the color list according to depth.r   r   r   gffffff?r   Nr   )
r    r   zerosr   r}   r   rU   r   r   r|   )r   rS   normZsatsZrgbar   r   r   r   o  s    
r   c           	      C   s:  t | tjr|| jd }d|d d| d   }}}| d|ddf | d|ddf  }| d|ddf | d|ddf  }ntt| df}tt| df}t| D ]\}}t|}d|d d| d   }}}||ddf ||ddf  ||ddf< ||ddf ||ddf  ||ddf< qt||S )aA  
    Compute the normals of a list of polygons, one normal per polygon.

    Normals point towards the viewer for a face with its vertices in
    counterclockwise order, following the right hand rule.

    Uses three points equally spaced around the polygon. This method assumes
    that the points are in a plane. Otherwise, more than one shade is required,
    which is not supported.

    Parameters
    ----------
    polygons : list of (M_i, 3) array-like, or (..., M, 3) array-like
        A sequence of polygons to compute normals for, which can have
        varying numbers of vertices. If the polygons all have the same
        number of vertices and array is passed, then the operation will
        be vectorized.

    Returns
    -------
    normals : (..., 3) array
        A normal vector estimated for the polygon.
    r   r   r3   .N)r   r   r   r   r  r    r  Zcross)	r   ni1i2Zi3Zv1Zv2Zpoly_iZpsr   r   r   r   }  s    
$&,.r   c              	      s   |dkrt jddd}tjdd" |tjj|ddd	 |j }W 5 Q R X t| }| rt 	d
d t 	ddj
 fdd}d|| < t | } | dddf }||ddtjf |  }||dddf< nt|  }|S )z
    Shade *color* using normal vectors given by *normals*,
    assuming a *lightsource* (using default position if not given).
    *color* can also be an array of the same length as *normals*.
    N   g-x3@)ZazdegZaltdegignore)invalidr   T)r   Zkeepdimsr   g333333?c                    s    | S r'   r   r   Zin_normZout_normr   r   r    s    z_shade_colors.<locals>.normr   r   )r   ZLightSourcer   ZerrstateZlinalgr  	directionisnanr   r   Zinverser   ZnewaxisZ
asanyarraycopy)colorr   r   r   maskr  r  r   r   r  r   r     s$    

r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   T)r   r   )N)9rJ   r?   Znumpyr   
contextlibr   Z
matplotlibr   r   r   r   r   r   r(   r   r   Zmatplotlib.collectionsr	   r
   r   r   Zmatplotlib.colorsr   Zmatplotlib.patchesr   r&   r   r   r   r$   r)   r%   rN   ZLine2DrO   r^   rj   rm   rp   rr   rs   r   r   r   r   r   r   r   r   r   r   r  rV   r  r   r   r   r   r   r   r   <module>   sN    "[
]





!
2/


k 1
  
+