o
    môjz3  ã                   @   sN   d Z ddlZddlmZ ddlmZ ddlZG dd„ dƒZG dd„ deƒZ	dS )	z'
Mesh refinement for triangular grids.
é    N)Ú_api)ÚTriangulationc                   @   s   e Zd ZdZdd„ ZdS )Ú
TriRefinera{  
    Abstract base class for classes implementing mesh refinement.

    A TriRefiner encapsulates a Triangulation object and provides tools for
    mesh refinement and interpolation.

    Derived classes must implement:

    - ``refine_triangulation(return_tri_index=False, **kwargs)`` , where
      the optional keyword arguments *kwargs* are defined in each
      TriRefiner concrete implementation, and which returns:

      - a refined triangulation,
      - optionally (depending on *return_tri_index*), for each
        point of the refined triangulation: the index of
        the initial triangulation triangle to which it belongs.

    - ``refine_field(z, triinterpolator=None, **kwargs)``, where:

      - *z* array of field values (to refine) defined at the base
        triangulation nodes,
      - *triinterpolator* is an optional `~matplotlib.tri.TriInterpolator`,
      - the other optional keyword arguments *kwargs* are defined in
        each TriRefiner concrete implementation;

      and which returns (as a tuple) a refined triangular mesh and the
      interpolated values of the field at the refined triangulation nodes.
    c                 C   s   t jt|d� || _d S )N)Útriangulation)r   Úcheck_isinstancer   Ú_triangulation©Úselfr   © r
   úl/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/matplotlib/tri/_trirefine.pyÚ__init__*   s   
zTriRefiner.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r
   r
   r   r      s    r   c                       sB   e Zd ZdZ‡ fdd„Zddd„Zdd	d
„Zeddd„ƒZ‡  Z	S )ÚUniformTriRefinerzÆ
    Uniform mesh refinement by recursive subdivisions.

    Parameters
    ----------
    triangulation : `~matplotlib.tri.Triangulation`
        The encapsulated triangulation (to be refined)
    c                    s   t ƒ  |¡ d S ©N)Úsuperr   r   ©Ú	__class__r
   r   r   =   s   zUniformTriRefiner.__init__Fé   c                 C   s   | j }|jjd }tj|tjd�}t|ƒD ]
}|  ||¡\}}q|jjd }|j}|r~tj	|dtjd�}	| j j
}
|
du rLt |d¡ dd¡|	|< ||	fS |
| }t || d¡ dd¡|	||dd…f < t ||  d¡ dd¡|	|| dd…f < ||	fS |S )aÞ  
        Compute a uniformly refined triangulation *refi_triangulation* of
        the encapsulated :attr:`triangulation`.

        This function refines the encapsulated triangulation by splitting each
        father triangle into 4 child sub-triangles built on the edges midside
        nodes, recursing *subdiv* times.  In the end, each triangle is hence
        divided into ``4**subdiv`` child triangles.

        Parameters
        ----------
        return_tri_index : bool, default: False
            Whether an index table indicating the father triangle index of each
            point is returned.
        subdiv : int, default: 3
            Recursion level for the subdivision.
            Each triangle is divided into ``4**subdiv`` child triangles;
            hence, the default results in 64 refined subtriangles for each
            triangle of the initial triangulation.

        Returns
        -------
        refi_triangulation : `~matplotlib.tri.Triangulation`
            The refined triangulation.
        found_index : int array
            Index of the initial triangulation containing triangle, for each
            point of *refi_triangulation*.
            Returned only if *return_tri_index* is set to True.
        r   ©ÚdtypeéÿÿÿÿNr   )r   Ú	trianglesÚshapeÚnpÚarangeÚint32ÚrangeÚ_refine_triangulation_onceÚxÚfullÚmaskÚrepeatÚreshape)r	   Úreturn_tri_indexÚsubdivZrefi_triangulationÚntriÚ	ancestorsÚ_Ú	refi_nptsZrefi_trianglesÚfound_indexÚtri_maskZancestor_maskr
   r
   r   Úrefine_triangulation@   s@   
ÿÿÿù
ÿþÿþz&UniformTriRefiner.refine_triangulationNc                 C   sb   |du rt j | j|¡}ntjt jj|d� |}| j|dd�\}}|j|j	|j
|d�d }||fS )aÅ  
        Refine a field defined on the encapsulated triangulation.

        Parameters
        ----------
        z : (npoints,) array-like
            Values of the field to refine, defined at the nodes of the
            encapsulated triangulation. (``n_points`` is the number of points
            in the initial triangulation)
        triinterpolator : `~matplotlib.tri.TriInterpolator`, optional
            Interpolator used for field interpolation. If not specified,
            a `~matplotlib.tri.CubicTriInterpolator` will be used.
        subdiv : int, default: 3
            Recursion level for the subdivision.
            Each triangle is divided into ``4**subdiv`` child triangles.

        Returns
        -------
        refi_tri : `~matplotlib.tri.Triangulation`
             The returned refined triangulation.
        refi_z : 1D array of length: *refi_tri* node count.
             The returned interpolated field (at *refi_tri* nodes).
        N)ÚtriinterpolatorT)r'   r&   )Ú	tri_indexr   )Ú
matplotlibÚtriÚCubicTriInterpolatorr   r   r   ÚTriInterpolatorr.   Ú_interpolate_multikeysr!   Úy)r	   Úzr/   r'   ÚinterpZrefi_trir,   Zrefi_zr
   r
   r   Úrefine_field…   s"   ÿ
ÿ
ÿ
ÿÿzUniformTriRefiner.refine_fieldc           $   
   C   sð  | j }| j}| j}| j}t |¡d }t |¡d }|dur;t |¡}t |¡|fkr;tdt |¡› dt |¡› �ƒ‚t |dk¡}d| | d }	||	 }
t 	|
¡}t 	|
¡}||d|…< ||d|…< t 
tj|tjd�d¡}t tjdtjd�|¡}|||f }||k}|| }|| }||||f  ||||d	 d f   d
 }||||f  ||||d	 d f   d
 }|||d…< |||d…< |}tj|dgtjd�}|}tdƒD ]%}||k}t |¡}|| }tj|tjd�| |dd…|f |< ||7 }qØt |¡}|| }|| }t ||dd…f t |tjdtjd�¡ ¡}tj|d	d�} || }!||| f |||!f< tj|d dgtjd�}"t |dd…df |dd…df |dd…df g¡j|"ddd…dd…f< t |dd…d	f |dd…d	f |dd…df g¡j|"d	dd…dd…f< t |dd…df |dd…df |dd…d	f g¡j|"ddd…dd…f< t |dd…df |dd…d	f |dd…df g¡j|"ddd…dd…f< t|||"ƒ}#| jdu�ré|# t | jd¡¡ |du �rð|#S |#t |d¡fS )aë  
        Refine a `.Triangulation` by splitting each triangle into 4
        child-masked_triangles built on the edges midside nodes.

        Masked triangles, if present, are also split, but their children
        returned masked.

        If *ancestors* is not provided, returns only a new triangulation:
        child_triangulation.

        If the array-like key table *ancestor* is given, it shall be of shape
        (ntri,) where ntri is the number of *triangulation* masked_triangles.
        In this case, the function returns
        (child_triangulation, child_ancestors)
        child_ancestors is defined so that the 4 child masked_triangles share
        the same index as their father: child_ancestors.shape = (4 * ntri,).
        r   NzNIncompatible shapes provide for triangulation.masked_triangles and ancestors: z and r   r   é   r   é   g      à?)Úaxisé   )r!   r6   Ú	neighborsr   r   r   ÚasarrayÚ
ValueErrorÚsumÚzerosÚtiler   r   r$   Úemptyr   Úlogical_notÚabsÚouterÚonesÚargminÚvstackÚTr   r#   Úset_mask)$r   r)   r!   r6   r>   r   Únptsr(   ÚbordersZ	added_ptsr+   Zrefi_xZrefi_yZ
edge_elemsZedge_apexesZedge_neighborsZmask_mastersZmastersZapex_mastersZx_addZy_addZnew_pt_cornerZnew_pt_midsideZcum_sumZimidZmask_st_locZn_masters_locZelem_masters_locZmask_slavesZslavesZslaves_mastersZ
diff_tableZslave_masters_apexZslaves_apexZchild_trianglesZchild_triangulationr
   r
   r   r    «   sÀ   
þþÿ

ÿÿÿÿ
ÿÿ

ÿÿþþþþþþþþ
z,UniformTriRefiner._refine_triangulation_once)Fr   )Nr   r   )
r   r   r   r   r   r.   r9   Ústaticmethodr    Ú__classcell__r
   r
   r   r   r   /   s    

E&r   )
r   Únumpyr   r1   r   Zmatplotlib.tri._triangulationr   Zmatplotlib.tri._triinterpolater   r   r
   r
   r
   r   Ú<module>   s    #