a
    ëÝEbN)  ã                   @   s6   d Z ddlZddlmZ ddlmZ G dd„ dƒZdS )z
Tools for triangular grids.
é    N)Ú_api)ÚTriangulationc                   @   sL   e Zd ZdZdd„ Zedd„ ƒZddd„Zdd
d„Zdd„ Z	e
dd„ ƒZdS )ÚTriAnalyzera€  
    Define basic tools for triangular mesh analysis and improvement.

    A TriAnalyzer encapsulates a `.Triangulation` object and provides basic
    tools for mesh analysis and mesh improvement.

    Attributes
    ----------
    scale_factors

    Parameters
    ----------
    triangulation : `~matplotlib.tri.Triangulation`
        The encapsulated triangulation to analyze.
    c                 C   s   t jt|d� || _d S )N)Útriangulation)r   Zcheck_isinstancer   Ú_triangulation)Úselfr   © r   úW/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/matplotlib/tri/tritools.pyÚ__init__   s    zTriAnalyzer.__init__c                 C   sT   | j  ¡ }tjt |¡| j jjd�dk}dt | j j| ¡ dt | j j| ¡ fS )a-  
        Factors to rescale the triangulation into a unit square.

        Returns
        -------
        (float, float)
            Scaling factors (kx, ky) so that the triangulation
            ``[triangulation.x * kx, triangulation.y * ky]``
            fits exactly inside a unit square.
        ©Z	minlengthr   é   )	r   Úget_masked_trianglesÚnpÚbincountÚravelÚxÚsizeZptpÚy)r   Úcompressed_trianglesZ	node_usedr   r   r	   Úscale_factors    s    
ÿÿÿzTriAnalyzer.scale_factorsTc                 C   s:  |r| j \}}nd\}}t | jj| | jj| g¡j}|| jj }|dd…ddd…f |dd…ddd…f  }|dd…ddd…f |dd…ddd…f  }|dd…ddd…f |dd…ddd…f  }t |dd…df |dd…df ¡}t |dd…df |dd…df ¡}t |dd…df |dd…df ¡}|| | d }	|	|| |	  || |	  || |	  }
|
dk}t 	|¡�rÖ|j
d }tj|tjd�}tj||< || | }||  d	t |
|  ¡  || < n|| | d	t |
¡  }|| | d	| |	  }|| }| jj}|du �r&|S tjj||d
�S dS )aN  
        Return a measure of the triangulation triangles flatness.

        The ratio of the incircle radius over the circumcircle radius is a
        widely used indicator of a triangle flatness.
        It is always ``<= 0.5`` and ``== 0.5`` only for equilateral
        triangles. Circle ratios below 0.01 denote very flat triangles.

        To avoid unduly low values due to a difference of scale between the 2
        axis, the triangular mesh can first be rescaled to fit inside a unit
        square with `scale_factors` (Only if *rescale* is True, which is
        its default value).

        Parameters
        ----------
        rescale : bool, default: True
            If True, internally rescale (based on `scale_factors`), so that the
            (unmasked) triangles fit exactly inside a unit square mesh.

        Returns
        -------
        masked array
            Ratio of the incircle radius over the circumcircle radius, for
            each 'rescaled' triangle of the encapsulated triangulation.
            Values corresponding to masked triangles are masked out.

        )ç      ð?r   Nr   r   é   g      à?g        ©Zdtypeg      @)Úmask)r   r   Zvstackr   r   r   ÚTÚ	trianglesÚhypotÚanyÚshapeÚemptyZfloat64ÚinfÚsqrtr   ÚmaÚarray)r   ÚrescaleZkxÚkyZptsZtri_ptsÚaÚbÚcÚsÚprodZ	bool_flatÚntriZcircum_radiusÚabcZ	in_radiusZcircle_ratior   r   r   r	   Úcircle_ratios2   s>    
ÿ,,,$$$(

ÿ
zTriAnalyzer.circle_ratiosç{®Gáz„?c                 C   sÔ   | j jjd }|  |¡|k }| j j}|du r:tj|td�}t | j j	¡}tj
|tjd�}d}|dkrÆtj|dd�dk| @ }	|	|@ }
|
|B }t |
¡}d||
dd…f< d||
< t |dkd|| ¡}q\tj |d¡S )a  
        Eliminate excessively flat border triangles from the triangulation.

        Returns a mask *new_mask* which allows to clean the encapsulated
        triangulation from its border-located flat triangles
        (according to their :meth:`circle_ratios`).
        This mask is meant to be subsequently applied to the triangulation
        using `.Triangulation.set_mask`.
        *new_mask* is an extension of the initial triangulation mask
        in the sense that an initially masked triangle will remain masked.

        The *new_mask* array is computed recursively; at each step flat
        triangles are removed only if they share a side with the current mesh
        border. Thus no new holes in the triangulated domain will be created.

        Parameters
        ----------
        min_circle_ratio : float, default: 0.01
            Border triangles with incircle/circumcircle radii ratio r/R will
            be removed if r/R < *min_circle_ratio*.
        rescale : bool, default: True
            If True, first, internally rescale (based on `scale_factors`) so
            that the (unmasked) triangles fit exactly inside a unit square
            mesh.  This rescaling accounts for the difference of scale which
            might exist between the 2 axis.

        Returns
        -------
        array of bool
            Mask to apply to encapsulated triangulation.
            All the initially masked triangles remain masked in the
            *new_mask*.

        Notes
        -----
        The rationale behind this function is that a Delaunay
        triangulation - of an unstructured set of points - sometimes contains
        almost flat triangles at its border, leading to artifacts in plots
        (especially for high-resolution contouring).
        Masked with computed *new_mask*, the encapsulated
        triangulation would contain no more unmasked border triangles
        with a circle ratio below *min_circle_ratio*, thus improving the
        mesh quality for subsequent plots or interpolation.
        r   Nr   éÿÿÿÿr   )ZaxisT)r   r   r   r-   r   r   ZzerosÚboolÚcopyZ	neighborsÚarangeÚint32ÚminÚsumÚwherer"   Zfilled)r   Zmin_circle_ratior$   r+   Zmask_bad_ratioZcurrent_maskZvalid_neighborsZrenum_neighborsZnaddZ	wavefrontZ
added_maskr   r   r	   Úget_flat_tri_masku   s&    0
ÿzTriAnalyzer.get_flat_tri_maskc           	      C   sœ   | j j}| j  ¡ }| j jjd }|dur6|  | ¡}ntj|tjd�}tj	t 
|¡| j jjd�dk}| j j| }| j j| }|  |¡}|| }|||||fS )a”  
        Compress (if masked) the encapsulated triangulation.

        Returns minimal-length triangles array (*compressed_triangles*) and
        coordinates arrays (*compressed_x*, *compressed_y*) that can still
        describe the unmasked triangles of the encapsulated triangulation.

        Returns
        -------
        compressed_triangles : array-like
            the returned compressed triangulation triangles
        compressed_x : array-like
            the returned compressed triangulation 1st coordinate
        compressed_y : array-like
            the returned compressed triangulation 2nd coordinate
        tri_renum : int array
            renumbering table to translate the triangle numbers from the
            encapsulated triangulation into the new (compressed) renumbering.
            -1 for masked triangles (deleted from *compressed_triangles*).
        node_renum : int array
            renumbering table to translate the point numbers from the
            encapsulated triangulation into the new (compressed) renumbering.
            -1 for unused points (i.e. those deleted from *compressed_x* and
            *compressed_y*).

        r   Nr   r   )r   r   r   r   r   Ú_total_to_compress_renumr   r2   r3   r   r   r   r   r   )	r   Ztri_maskr   r+   Z	tri_renumZ
valid_nodeZcompressed_xZcompressed_yZ
node_renumr   r   r	   Ú_get_compressed_triangulationÀ   s$    
ÿÿ
ÿz)TriAnalyzer._get_compressed_triangulationc                 C   s:   t jt  | ¡dt jd�}t  | ¡}t j|t jd�|| < |S )aØ  
        Parameters
        ----------
        valid : 1D bool array
            Validity mask.

        Returns
        -------
        int array
            Array so that (`valid_array` being a compressed array
            based on a `masked_array` with mask ~*valid*):

            - For all i with valid[i] = True:
              valid_array[renum[i]] = masked_array[i]
            - For all i with valid[i] = False:
              renum[i] = -1 (invalid value)
        r/   r   )r   Úfullr   r3   r5   r2   )ZvalidZrenumZn_validr   r   r	   r8   ñ   s    
z$TriAnalyzer._total_to_compress_renumN)T)r.   T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   Úpropertyr   r-   r7   r9   Ústaticmethodr8   r   r   r   r	   r      s   

C
K1r   )r>   Únumpyr   Z
matplotlibr   Zmatplotlib.trir   r   r   r   r   r	   Ú<module>   s   