
    !iO)                     <    d Z ddlZddlmZ ddlmZ  G d d      Zy)z
Tools for triangular grids.
    N)_api)Triangulationc                   L    e Zd ZdZd Zed        Zd	dZd
dZd Z	e
d        Zy)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                 H    t        j                  t        |       || _        y )N)triangulation)r   check_isinstancer   _triangulation)selfr   s     K/var/www/html/venv/lib/python3.12/site-packages/matplotlib/tri/_tritools.py__init__zTriAnalyzer.__init__   s    m=I+    c                    | j                   j                         }t        j                  t        j                  |      | j                   j
                  j                        dk7  }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2GGI[[*>!?+/+>+>+@+@+E+EGJKL	BFF4..00;<<BFF4..00;<<> 	>r   c                    |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                  |      rw|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                   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   sn   : ))HR!HRii,,..r1,,..r13 445A 	d))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&qSUCK!A#a%y!A#a% !A#a%(RZ	66)==#DHHT<M')vvM)$A#a%C(+YJBGGD),--)/M9*% qSUs2774='89MqSUs=023	 .""''<55;;|$;77r   c                    | j                   j                  j                  d   }| j                  |      |k  }| j                   j                  }|t        j                  |t              }t        j                  | j                   j                        }t        j                  |t
        j                        }d}|dk7  rnt        j                  |d      dk(  | z  }	|	|z  }
|
|z  }t        j                  |
      }d||
ddf<   d||
<   t        j                  |dk(  d||         }|dk7  rnt
        j                  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   s4   ` "",,2215++G47GG**//88D5L''$"5"5"?"?@))D9ai a8B><-OI #^3J%4L66*%D .0OJM**,OJ' hh"'<b'6'GIO ai  uu||L$//r   c                 P   | j                   j                  }| j                   j                         }| j                   j                  j                  d   }|| j                  |       }n%t        j                  |t        j                        }t        j                  t        j                  |      | j                   j                  j                        dk7  }| j                   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   r!   r   )r
   r#   r   r&   r)   _total_to_compress_renumr   rH   rI   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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:>
  **>?$lL) 	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)
        rB   r!   )r   fullr   rI   rK   rH   )validrenumn_valids      r   rX   z$TriAnalyzer._total_to_compress_renum   sL    & "((;&&-yy9er   N)T)g{Gz?T)__name__
__module____qualname____doc__r   propertyr   r@   rV   r_   staticmethodrX    r   r   r   r      sH     , > >"A8FI0V/b  r   r   )rh   numpyr   
matplotlibr   matplotlib.trir   r   rk   r   r   <module>ro      s!      (| |r   