
    hO)                     <    S r SSKrSSKJr  SSKJr   " S S5      rg)z
Tools for triangular grids.
    N)_api)Triangulationc                   X    \ rS rSrSrS r\S 5       rSS jrSS jr	S r
\S 5       rS	rg
)TriAnalyzer   aT  
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                 B    [         R                  " [        US9  Xl        g )N)triangulation)r   check_isinstancer   _triangulation)selfr	   s     J/var/www/html/env/lib/python3.13/site-packages/matplotlib/tri/_tritools.py__init__TriAnalyzer.__init__   s    m=I+    c                    U R                   R                  5       n[        R                  " [        R                  " U5      U R                   R
                  R                  S9S:g  nS[        R                  " U R                   R
                  U   5      -  S[        R                  " U R                   R                  U   5      -  4$ )z
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_factorsTriAnalyzer.scale_factors    s      $22GGI[[*>!?+/+>+>+@+@+E+EGJKL	BFF4..00;<<BFF4..00;<<> 	>r   c                    U(       a  U R                   u  p#OSu  p#[        R                  " U R                  R                  U-  U R                  R
                  U-  /5      R                  nX@R                  R                     nUSS2SSS24   USS2SSS24   -
  nUSS2SSS24   USS2SSS24   -
  nUSS2SSS24   USS2SSS24   -
  n[        R                  " USS2S4   USS2S4   5      n[        R                  " USS2S4   USS2S4   5      n[        R                  " USS2S4   USS2S4   5      nXg-   U-   S-  n	XU-   U	-
  -  Xh-   U	-
  -  Xx-   U	-
  -  n
U
S:H  n[        R                  " U5      (       aq  UR                  S   n[        R                  " U[        R                  S9n[        R                  X'   Xg-  U-  nX)    S	[        R                  " X)    5      -  -  X) '   O!Xg-  U-  S	[        R                  " U
5      -  -  nXg-  U-  S	U-  U	-  -  nX-  nU R                  R                  nUc  U$ [        R                   R#                  UUS
9$ )a  
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_ratiosTriAnalyzer.circle_ratios2   sU   : ))HR!HRii,,..r1,,..r13 445A 	))334Aq!Gwq!Qw//Aq!Gwq!Qw//Aq!Gwq!Qw//HHQq!tWa1g&HHQq!tWa1g&HHQq!tWa1g&SUCKA#a%y!#a% !#a%(RZ	66)==#DHHT<M')vvM$#a%C(+JBGGD,--)/M*% SUs2774='89MSUs=023	 .""''<55;;|$;77r   c                    U R                   R                  R                  S   nU R                  U5      U:  nU R                   R                  nUc  [
        R                  " U[        S9n[
        R                  " U R                   R                  5      n[
        R                  " U[
        R                  S9nSnUS:w  ak  [
        R                  " USS9S:H  U) -  n	X-  n
X-  n[
        R                  " U
5      nSXjSS24'   SXz'   [
        R                  " US:H  SXv   5      nUS:w  a  Mk  [
        R                  R!                  US5      $ )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,   rC   r&   r   zerosboolcopy	neighborsarangeint32minsumwherer1   filled)r   min_circle_ratior3   r>   mask_bad_ratiocurrent_maskvalid_neighborsrenum_neighborsnadd	wavefront
added_masks              r   get_flat_tri_maskTriAnalyzer.get_flat_tri_masku   s*   ` "",,2215++G47GG**//88D5L''$"5"5"?"?@))D9ai a8B><-OI #3J%4L66*%D .0OM**,O' hh"'<b'6'GIO ai  uu||L$//r   c                 F   U R                   R                  nU R                   R                  5       nU R                   R                  R                  S   nUb  U R                  U) 5      nO#[        R                  " U[        R                  S9n[        R                  " [        R                  " U5      U R                   R                  R                  S9S:g  nU R                   R                  U   nU R                   R                  U   nU R                  U5      nX   nX&XtU4$ )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   r$   r   )r   r&   r   r)   r,   _total_to_compress_renumr   rL   rM   r   r   r   r   r   )	r   tri_maskr   r>   	tri_renum
valid_nodecompressed_xcompressed_y
node_renums	            r   _get_compressed_triangulation)TriAnalyzer._get_compressed_triangulation   s   8 &&++#22GGI"",,221555xi@I		$bhh7I kk"((+?"@,0,?,?,A,A,F,FHKLM
**,,Z8**,,Z822:>
  *?$L 	r   c                     [         R                  " [         R                  " U 5      S[         R                  S9n[         R                  " U 5      n[         R
                  " U[         R                  S9X'   U$ )ah  
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)
rF   r$   )r   fullr   rM   rO   rL   )validrenumn_valids      r   r]   $TriAnalyzer._total_to_compress_renum   sJ    & "((;&&-yy9r   )r   N)T)g{Gz?T)__name__
__module____qualname____firstlineno____doc__r   propertyr   rC   rZ   rd   staticmethodr]   __static_attributes__ r   r   r   r      sH     , > >"A8FI0V/b  r   r   )rp   numpyr   
matplotlibr   matplotlib.trir   r   rt   r   r   <module>rx      s!      (| |r   