ó
 m[c           @` sj   d  Z  d d l m Z m Z m Z m Z d d l Z d d l m Z d d l	 Z
 d e f d „  ƒ  YZ d S(   u   
Tools for triangular grids.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   Triangulationt   TriAnalyzerc           B` sb   e  Z d  Z d „  Z e d „  ƒ Z e d „ Z d e d „ Z e	 e	 d „ Z
 e d d „ ƒ Z RS(	   u§  
    Define basic tools for triangular mesh analysis and improvement.

    A TriAnalizer encapsulates a :class:`~matplotlib.tri.Triangulation`
    object and provides basic tools for mesh analysis and mesh improvement.

    Parameters
    ----------
    triangulation : :class:`~matplotlib.tri.Triangulation` object
        The encapsulated triangulation to analyze.

    Attributes
    ----------
    `scale_factors`

    c         C` s+   t  | t ƒ s t d ƒ ‚ n  | |  _ d  S(   Nu   Expected a Triangulation object(   t
   isinstanceR   t
   ValueErrort   _triangulation(   t   selft   triangulation(    (    s6   lib/python2.7/site-packages/matplotlib/tri/tritools.pyt   __init__   s    c         C` st   |  j  j ƒ  } t j t j | ƒ d |  j  j j ƒd k } d t j |  j  j | ƒ d t j |  j  j | ƒ f S(   ur  
        Factors to rescale the triangulation into a unit square.

        Returns *k*, tuple of 2 scale factors.

        Returns
        -------
        k : tuple of 2 floats (kx, ky)
            Tuple of floats that would rescale the triangulation :
            ``[triangulation.x * kx, triangulation.y * ky]``
            fits exactly inside a unit square.

        t	   minlengthi    i   (	   R   t   get_masked_trianglest   npt   bincountt   ravelt   xt   sizet   ptpt   y(   R	   t   compressed_trianglest	   node_used(    (    s6   lib/python2.7/site-packages/matplotlib/tri/tritools.pyt   scale_factors#   s
    c         C` sþ  | r |  j  \ } } n d \ } } t j |  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 j | d d … d f d | d d … d f d ƒ } t j | d d … d f d | d d … d f d ƒ } t j | d d … d f d | d d … d f d ƒ } | | | d }	 |	 | | |	 | | |	 | | |	 }
 |
 d k } t j	 | ƒ r…| j
 d } t j | d t j ƒ} t j | | <| | | } | | d	 t j |
 | ƒ | | <n | | | d	 t j |
 ƒ } | | | d	 | |	 } | | } |  j j } | d k rä| St j j | d
 | ƒSd S(   u§  
        Returns 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 :attr:`scale_factors` (Only if *rescale* is True, which is
        its default value).

        Parameters
        ----------
        rescale : boolean, optional
            If True, a rescaling will be internally performed (based on
            :attr:`scale_factors`, so that the (unmasked) triangles fit
            exactly inside a unit square mesh. Default is True.

        Returns
        -------
        circle_ratios : 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.

        g      ð?Ni   i    i   g      à?g        t   dtypeg      @t   mask(   g      ð?g      ð?(   R   R   t   vstackR   R   R   t   Tt	   trianglest   sqrtt   anyt   shapet   emptyt   float64t   infR   t   Nonet   mat   array(   R	   t   rescalet   kxt   kyt   ptst   tri_ptst   at   bt   ct   st   prodt	   bool_flatt   ntrit   circum_radiust   abct	   in_radiust   circle_ratioR   (    (    s6   lib/python2.7/site-packages/matplotlib/tri/tritools.pyt   circle_ratios8   s:    <<<;;;*!
g{®Gáz„?c         C` s;  |  j  j j d } |  j | ƒ | k  } |  j  j } | d k rX t j | d t ƒ} n  t j	 |  j  j
 ƒ } t j | d t j ƒ} d } xš | d k r't j | d d ƒd k | @}	 t j |	 | ƒ }
 |
 | B} t j |
 ƒ } d | |
 d d … f <d | |
 <t j | d k d | | ƒ } qŽ Wt j j | t ƒ S(   u“  
        Eliminates 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 :func:`matplotlib.tri.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, optional
            Border triangles with incircle/circumcircle radii ratio r/R will
            be removed if r/R < *min_circle_ratio*. Default value: 0.01
        rescale : boolean, optional
            If True, a rescaling will first be internally performed (based on
            :attr:`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. Default
            (and recommended) value is True.

        Returns
        -------
        new_mask : array-like of booleans
            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.
        i    R   iÿÿÿÿt   axisi   N(   R   R   R   R6   R   R#   R   t   zerost   boolt   copyt	   neighborst   aranget   int32t   mint   logical_andt   sumt   whereR$   t   filledt   True(   R	   t   min_circle_ratioR&   R1   t   mask_bad_ratiot   current_maskt   valid_neighborst   renum_neighborst   naddt	   wavefrontt
   added_mask(    (    s6   lib/python2.7/site-packages/matplotlib/tri/tritools.pyt   get_flat_tri_mask}   s&    2

c         C` s  |  j  j } |  j  j ƒ  } |  j  j j d } |  j | | ƒ } t j t j | ƒ d |  j  j	 j
 ƒd k } |  j  j	 | } |  j  j | }	 |  j | ƒ }
 |
 | } | sÔ | sÁ | | |	 f S| | |	 |
 f Sn) | sê | | |	 | f S| | |	 | |
 f Sd S(   u8  
        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.

        Parameters
        ----------
        return_tri_renum : boolean, optional
            Indicates whether a renumbering table to translate the triangle
            numbers from the encapsulated triangulation numbering into the
            new (compressed) renumbering will be returned.
        return_node_renum : boolean, optional
            Indicates whether a renumbering table to translate the nodes
            numbers from the encapsulated triangulation numbering into the
            new (compressed) renumbering will be returned.

        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 : array-like of integers
            renumbering table to translate the triangle numbers from the
            encapsulated triangulation into the new (compressed) renumbering.
            -1 for masked triangles (deleted from *compressed_triangles*).
            Returned only if *return_tri_renum* is True.
        node_renum : array-like of integers
            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*). Returned only if *return_node_renum* is True.

        i    R   N(   R   R   R   R   R   t   _total_to_compress_renumR   R   R   R   R   R   (   R	   t   return_tri_renumt   return_node_renumt   tri_maskR   R1   t	   tri_renumt	   node_maskt   compressed_xt   compressed_yt
   node_renum(    (    s6   lib/python2.7/site-packages/matplotlib/tri/tritools.pyt   _get_compressed_triangulationË   s(    )
	
		c         C` s±   | d k r t j |  ƒ } n  |  d k	 r— t j | d t j ƒ} t j | d t j ƒj |  d d ƒ} t j t j | d ƒ d t j ƒ| | <| St j | d t j ƒSd S(   uT  
        Parameters
        ----------
        mask : 1d boolean array or None
            mask
        n : integer
            length of the mask. Useful only id mask can be None

        Returns
        -------
        renum : integer array
            array so that (`valid_array` being a compressed array
            based on a `masked_array` with mask *mask*) :

                  - For all i such as mask[i] = False:
                    valid_array[renum[i]] = masked_array[i]
                  - For all i such as mask[i] = True:
                    renum[i] = -1 (invalid value)

        R   R7   i    N(   R#   R   R   t   onesR=   R<   t   compress(   R   t   nt   renumt   valid(    (    s6   lib/python2.7/site-packages/matplotlib/tri/tritools.pyRM     s    ((N(   t   __name__t
   __module__t   __doc__R   t   propertyR   RC   R6   RL   t   FalseRV   t   staticmethodR#   RM   (    (    (    s6   lib/python2.7/site-packages/matplotlib/tri/tritools.pyR      s   	ENF(   R^   t
   __future__R    R   R   R   t   sixt   matplotlib.triR   t   numpyR   t   objectR   (    (    (    s6   lib/python2.7/site-packages/matplotlib/tri/tritools.pyt   <module>   s
   "