§
    ‡ÛgO)  ã                   óD   — d Z ddlZddlmZ ddlmZ  G d„ d¦  «        ZdS )z
Tools for triangular grids.
é    N)Ú_api)ÚTriangulationc                   óZ   — e Zd ZdZd„ Zed„ ¦   «         Zdd„Zdd„Zd„ Z	e
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                 óJ   — t          j        t          |¬¦  «         || _        d S )N)Útriangulation)r   Úcheck_isinstancer   Ú_triangulation)Úselfr   s     úN/granary/edev/quantum/lib/python3.11/site-packages/matplotlib/tri/_tritools.pyÚ__init__zTriAnalyzer.__init__   s&   € ÝÔ�m¸=ÐIÑIÔIÐIØ+ˆÔÐÐó    c                 óF  — | j                              ¦   «         }t          j        t          j        |¦  «        | j         j        j        ¬¦  «        dk    }dt          j        | j         j        |         ¦  «        z  dt          j        | j         j        |         ¦  «        z  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.
        ©Ú	minlengthr   é   )	r
   Úget_masked_trianglesÚnpÚbincountÚravelÚxÚsizeÚptpÚy)r   Úcompressed_trianglesÚ	node_useds      r   Úscale_factorszTriAnalyzer.scale_factors    s˜   € ð  $Ô2×GÒGÑIÔIÐÝ”[¥¤Ð*>Ñ!?Ô!?Ø+/Ô+>Ô+@Ô+EðGñ Gô GØJKòLˆ	à•B”F˜4Ô.Ô0°Ô;Ñ<Ô<Ñ<Ø•B”F˜4Ô.Ô0°Ô;Ñ<Ô<Ñ<ð>ð 	>r   Tc                 ó˜  — |r| j         \  }}nd\  }}t          j        | j        j        |z  | j        j        |z  g¦  «        j        }|| j        j                 }|dd…ddd…f         |dd…ddd…f         z
  }|dd…ddd…f         |dd…ddd…f         z
  }|dd…ddd…f         |dd…ddd…f         z
  }t          j        |dd…df         |dd…df         ¦  «        }t          j        |dd…df         |dd…df         ¦  «        }t          j        |dd…df         |dd…df         ¦  «        }||z   |z   dz  }	|	||z   |	z
  z  ||z   |	z
  z  ||z   |	z
  z  }
|
dk    }t          j	        |¦  «        rq|j
        d         }t          j        |t          j        ¬¦  «        }t          j        ||<   ||z  |z  }||          d	t          j        |
|          ¦  «        z  z  || <   n ||z  |z  d	t          j        |
¦  «        z  z  }||z  |z  d	|z  |	z  z  }||z  }| j        j        }|€|S t          j                             ||¬
¦  «        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        ©Údtypeg      @)Úmask)r   r   Úvstackr
   r   r   ÚTÚ	trianglesÚhypotÚanyÚshapeÚemptyÚfloat64ÚinfÚsqrtr#   ÚmaÚarray)r   ÚrescaleÚkxÚkyÚptsÚtri_ptsÚaÚbÚcÚsÚprodÚ	bool_flatÚntriÚcircum_radiusÚabcÚ	in_radiusÚcircle_ratior#   s                     r   Úcircle_ratioszTriAnalyzer.circle_ratios2   s¯  € ð: ð 	"ØÔ)‰HˆR��à!‰HˆR�ÝŒi˜Ô,Ô.¨rÑ1ØÔ,Ô.¨rÑ1ð3ñ 4ô 4Ü45ð 	à�dÔ)Ô3Ô4ˆà�A�A�A�q˜!˜!˜!�GÔ˜w q q q¨!¨Q¨Q¨Q wÔ/Ñ/ˆØ�A�A�A�q˜!˜!˜!�GÔ˜w q q q¨!¨Q¨Q¨Q wÔ/Ñ/ˆØ�A�A�A�q˜!˜!˜!�GÔ˜w q q q¨!¨Q¨Q¨Q wÔ/Ñ/ˆÝŒH�Q�q�q�q˜!�t”W˜a    1 œgÑ&Ô&ˆÝŒH�Q�q�q�q˜!�t”W˜a    1 œgÑ&Ô&ˆÝŒH�Q�q�q�q˜!�t”W˜a    1 œgÑ&Ô&ˆàˆq‰S�‰U�C‰KˆØ�!�A‘#�a‘%‰y˜!˜A™#˜a™%Ñ  ! A¡# a¡%Ñ(ˆà˜R’Zˆ	ÝŒ6�)ÑÔð 
	:à”= Ô#ˆDÝœH Tµ´Ð<Ñ<Ô<ˆMÝ')¤vˆM˜)Ñ$Ø�A‘#�a‘%ˆCØ(+¨Y¨J¬Ø•B”G˜D ) Ô,Ñ-Ô-Ñ-ñ)/ˆM˜9˜*Ñ%Ð%ð ˜q™S ™U s­2¬7°4©=¬=Ñ'8Ñ9ˆMØ�q‘S˜‘U˜s =Ñ0°Ñ2Ñ3ˆ	Ø  Ñ.ˆØÔ"Ô'ˆØˆ<ØÐå”5—;’;˜|°$�;Ñ7Ô7Ð7r   ç{®Gáz„?c                 ó`  — | j         j        j        d         }|                      |¦  «        |k     }| j         j        }|€t          j        |t          ¬¦  «        }t          j        | j         j	        ¦  «        }t          j
        |t
          j        ¬¦  «        }d}|dk    rpt          j        |d¬¦  «        dk    | z  }	|	|z  }
|
|z  }t          j        |
¦  «        }d||
dd…f<   d||
<   t          j        |dk    d||         ¦  «        }|dk    °pt
          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   )ÚaxisT)r
   r&   r)   r@   r#   r   ÚzerosÚboolÚcopyÚ	neighborsÚarangeÚint32ÚminÚsumÚwherer.   Úfilled)r   Úmin_circle_ratior0   r;   Úmask_bad_ratioÚcurrent_maskÚvalid_neighborsÚrenum_neighborsÚnaddÚ	wavefrontÚ
added_masks              r   Úget_flat_tri_maskzTriAnalyzer.get_flat_tri_masku   s;  € ð` Ô"Ô,Ô2°1Ô5ˆØ×+Ò+¨GÑ4Ô4Ð7GÒGˆàÔ*Ô/ˆØÐÝœ8 DµÐ5Ñ5Ô5ˆLÝœ' $Ô"5Ô"?Ñ@Ô@ˆÝœ) Dµ´Ð9Ñ9Ô9ˆØˆØ�aŠiˆiõ œ °aÐ8Ñ8Ô8¸BÒ>À<À-ÑOˆIð # ^Ñ3ˆJØ%¨Ñ4ˆLÝ”6˜*Ñ%Ô%ˆDð .0ˆO˜J¨¨¨˜MÑ*Ø*,ˆO˜JÑ'Ý œh ¸"Ò'<¸bØ'6°Ô'GñIô IˆOð �aŠiˆiõ  Œu�|Š|˜L¨$Ñ/Ô/Ð/r   c                 óò  — | j         j        }| j                              ¦   «         }| j         j        j        d         }|�|                      | ¦  «        }n t          j        |t          j        ¬¦  «        }t          j	        t          j
        |¦  «        | j         j        j        ¬¦  «        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   rI   rJ   r   r   r   r   r   )	r   Útri_maskr   r;   Ú	tri_renumÚ
valid_nodeÚcompressed_xÚcompressed_yÚ
node_renums	            r   Ú_get_compressed_triangulationz)TriAnalyzer._get_compressed_triangulationÀ   sþ   € ð8 Ô&Ô+ˆØ#Ô2×GÒGÑIÔIÐØÔ"Ô,Ô2°1Ô5ˆØÐØ×5Ò5°x°iÑ@Ô@ˆIˆIåœ	 $­b¬hÐ7Ñ7Ô7ˆIõ ”k¥"¤(Ð+?Ñ"@Ô"@Ø,0Ô,?Ô,AÔ,FðHñ Hô HØKLòMˆ
àÔ*Ô,¨ZÔ8ˆØÔ*Ô,¨ZÔ8ˆØ×2Ò2°:Ñ>Ô>ˆ
ð  *Ð*>Ô?Ðà$ l°LÀ)Øðð 	r   c                 óÚ   — t          j        t          j        | ¦  «        dt           j        ¬¦  «        }t          j        | ¦  «        }t          j        |t           j        ¬¦  «        || <   |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)
        rC   r!   )r   Úfullr   rJ   rL   rI   )ÚvalidÚrenumÚn_valids      r   rY   z$TriAnalyzer._total_to_compress_renumñ   sP   € õ& ”�œ ™œ¨µ"´(Ð;Ñ;Ô;ˆÝ”&˜‘-”-ˆÝ”y µ´Ð9Ñ9Ô9ˆˆe‰Øˆr   N)T)rA   T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r@   rW   r`   ÚstaticmethodrY   © r   r   r   r      s¥   € € € € € ðð ð ,ð ,ð ,ð ð>ð >ñ „Xð>ð"A8ð A8ð A8ð A8ðFI0ð I0ð I0ð I0ðV/ð /ð /ðb ðð ñ „\ðð ð r   r   )ri   Únumpyr   Ú
matplotlibr   Úmatplotlib.trir   r   rl   r   r   ú<module>rp      s}   ððð ð Ð Ð Ð à Ð Ð Ð Ð Ð Ø (Ð (Ð (Ð (Ð (Ð (ð|ð |ð |ð |ð |ñ |ô |ð |ð |ð |r   