Ë
    G8¿cN)  ã                   ó<   — 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     ú9/usr/lib/python3/dist-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ˆ	à”B—F‘F˜4×.Ñ.×0Ñ0°Ñ;Ó<Ñ<Ø”B—F‘F˜4×.Ñ.×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  € ñ: Ø×)Ñ)‰HˆR‘à!‰HˆRÜi‰i˜×,Ñ,×.Ñ.¨rÑ1Ø×,Ñ,×.Ñ.¨rÑ1ð3ó 4ß45±Að 	àd×)Ñ)×3Ñ3Ñ4ˆà’Aqš!GÑ˜w¢q¨!ªQ wÑ/Ñ/ˆØ’Aqš!GÑ˜w¢q¨!ªQ wÑ/Ñ/ˆØ’Aqš!GÑ˜w¢q¨!ªQ wÑ/Ñ/ˆÜH‰HQ’q˜!t‘W˜a¢ 1 ™gÓ&ˆÜH‰HQ’q˜!t‘W˜a¢ 1 ™gÓ&ˆÜH‰HQ’q˜!t‘W˜a¢ 1 ™gÓ&ˆàˆq‰S‰UC‰KˆØ!A‘#a‘%‰y˜!˜A™#˜a™%Ñ  ! A¡# a¡%Ñ(ˆà˜R‘Zˆ	Ü6‰6)Ôà—=‘= Ñ#ˆDÜŸH™H T´·±Ô<ˆMÜ')§v¡vˆM˜)Ñ$ØA‘#a‘%ˆCØ(+¨Y¨J©Ø”B—G‘G˜D ) Ñ,Ó-Ñ-ñ)/ˆM˜9˜*Ò%ð ˜q™S ™U s¬2¯7©7°4«=Ñ'8Ñ9ˆMØq‘S˜‘U˜s =Ñ0°Ñ2Ñ3ˆ	Ø  Ñ.ˆØ×"Ñ"×'Ñ'ˆØˆ<ØÐä—5‘5—;‘;˜|°$;Ó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Ñ2°1Ñ5ˆØ×+Ñ+¨GÓ4Ð7GÑGˆà×*Ñ*×/Ñ/ˆØÐÜŸ8™8 D´Ô5ˆLÜŸ'™' $×"5Ñ"5×"?Ñ"?Ó@ˆÜŸ)™) D´·±Ô9ˆØˆØaŠiô Ÿ™ °aÔ8¸BÑ>À<À-ÑOˆIð # ^Ñ3ˆJØ%¨Ñ4ˆLÜ—6‘6˜*Ó%ˆDð .0ˆO˜Jª˜MÑ*Ø*,ˆO˜JÑ'Ü Ÿh™h ¸"Ñ'<¸bØ'6°Ñ'GóIˆOð a‹iô  u‰u|‰|˜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Ñ2°1Ñ5ˆØÐØ×5Ñ5°x°iÓ@‰IäŸ	™	 $¬b¯h©hÔ7ˆIô —k‘k¤"§(¡(Ð+?Ó"@Ø,0×,?Ñ,?×,AÑ,A×,FÑ,Fô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)
        rB   r!   )r   Úfullr   rI   rK   rH   )ÚvalidÚrenumÚn_valids      r   rX   z$TriAnalyzer._total_to_compress_renumñ   sL   € ô& —‘œŸ™ ›¨´"·(±(Ô;ˆÜ—&‘&˜“-ˆÜ—y‘y ´·±Ô9ˆˆe‰Øˆr   N)T)g{®Gáz„?T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r@   rV   r_   ÚstaticmethodrX   © r   r   r   r      sH   „ ñò ,ð ñ>ó ð>ó"A8óFI0òV/ðb ñó ñr   r   )rh   Únumpyr   Ú
matplotlibr   Úmatplotlib.trir   r   rk   r   r   ú<module>ro      s!   ðñó å Ý (÷|ò |r   