ó
 m[c           @` sŒ   d  Z  d d l m Z m Z m Z m Z d d l Z d d l Z d d l	 m
 Z
 d d l Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(	   u'   
Mesh refinement for triangular grids.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   Triangulationt
   TriRefinerc           B` s   e  Z d  Z d „  Z RS(   uó  
    Abstract base class for classes implementing mesh refinement.

    A TriRefiner encapsulates a Triangulation object and provides tools for
    mesh refinement and interpolation.

    Derived classes must implements:

        - ``refine_triangulation(return_tri_index=False, **kwargs)`` , where
          the optional keyword arguments *kwargs* are defined in each
          TriRefiner concrete implementation, and which returns :

              - a refined triangulation
              - optionally (depending on *return_tri_index*), for each
                point of the refined triangulation: the index of
                the initial triangulation triangle to which it belongs.

        - ``refine_field(z, triinterpolator=None, **kwargs)`` , where:

              - *z* array of field values (to refine) defined at the base
                triangulation nodes
              - *triinterpolator* is a
                :class:`~matplotlib.tri.TriInterpolator` (optional)
              - the other optional keyword arguments *kwargs* are defined in
                each TriRefiner concrete implementation

          and which returns (as a tuple) a refined triangular mesh and the
          interpolated values of the field at the refined triangulation nodes.

    c         C` s+   t  | t ƒ s t d ƒ ‚ n  | |  _ d  S(   Nu   Expected a Triangulation object(   t
   isinstanceR   t
   ValueErrort   _triangulation(   t   selft   triangulation(    (    s7   lib/python2.7/site-packages/matplotlib/tri/trirefine.pyt   __init__-   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    s7   lib/python2.7/site-packages/matplotlib/tri/trirefine.pyR      s   t   UniformTriRefinerc           B` sG   e  Z d  Z d „  Z e d d „ Z d d d „ Z e d d „ ƒ Z	 RS(   uÚ   
    Uniform mesh refinement by recursive subdivisions.

    Parameters
    ----------
    triangulation : :class:`~matplotlib.tri.Triangulation`
                     The encapsulated triangulation (to be refined)
    c         C` s   t  j |  | ƒ d  S(   N(   R   R   (   R	   R
   (    (    s7   lib/python2.7/site-packages/matplotlib/tri/trirefine.pyR   A   s    i   c         C` s_  |  j  } | j j d } t j | d t j ƒ} x, t | ƒ D] } |  j | | ƒ \ } } q> W| j j d } | j } | rWt j	 | d t j ƒ}	 |  j  j
 }
 |
 d k rÕ t j | d ƒ j d d ƒ |	 | <nx |
 | } t j | | d ƒ j d d ƒ |	 | | d d … f <t j | | d ƒ j d d ƒ |	 | | d d … f <| |	 f S| Sd S(   uo  
        Computes an uniformly refined triangulation *refi_triangulation* of
        the encapsulated :attr:`triangulation`.

        This function refines the encapsulated triangulation by splitting each
        father triangle into 4 child sub-triangles built on the edges midside
        nodes, recursively (level of recursion *subdiv*).
        In the end, each triangle is hence divided into ``4**subdiv``
        child triangles.
        The default value for *subdiv* is 3 resulting in 64 refined
        subtriangles for each triangle of the initial triangulation.

        Parameters
        ----------
        return_tri_index : boolean, optional
            Boolean indicating whether an index table indicating the father
            triangle index of each point will be returned. Default value
            False.
        subdiv : integer, optional
            Recursion level for the subdivision. Defaults value 3.
            Each triangle will be divided into ``4**subdiv`` child triangles.

        Returns
        -------
        refi_triangulation : :class:`~matplotlib.tri.Triangulation`
            The returned refined triangulation
        found_index : array-like of integers
            Index of the initial triangulation containing triangle, for each
            point of *refi_triangulation*.
            Returned only if *return_tri_index* is set to True.

        i    t   dtypei   iÿÿÿÿN(   R   t	   trianglest   shapet   npt   aranget   int32t   ranget   _refine_triangulation_oncet   xt   onest   maskt   Nonet   repeatt   reshape(   R	   t   return_tri_indext   subdivt   refi_triangulationt   ntrit	   ancestorst   _t	   refi_nptst   refi_trianglest   found_indext   tri_maskt   ancestor_mask(    (    s7   lib/python2.7/site-packages/matplotlib/tri/trirefine.pyt   refine_triangulationD   s*    !			
)*
c         C` s›   | d k r' t j j |  j | ƒ } n* t | t j j ƒ sK t d ƒ ‚ n  | } |  j d | d t	 ƒ \ } } | j
 | j | j d | ƒd } | | f S(   u²  
        Refines a field defined on the encapsulated triangulation.

        Returns *refi_tri* (refined triangulation), *refi_z* (interpolated
        values of the field at the node of the refined triangulation).

        Parameters
        ----------
        z : 1d-array-like of length ``n_points``
            Values of the field to refine, defined at the nodes of the
            encapsulated triangulation. (``n_points`` is the number of points
            in the initial triangulation)
        triinterpolator : :class:`~matplotlib.tri.TriInterpolator`, optional
            Interpolator used for field interpolation. If not specified,
            a :class:`~matplotlib.tri.CubicTriInterpolator` will
            be used.
        subdiv : integer, optional
            Recursion level for the subdivision. Defaults to 3.
            Each triangle will be divided into ``4**subdiv`` child triangles.

        Returns
        -------
        refi_tri : :class:`~matplotlib.tri.Triangulation` object
                     The returned refined triangulation
        refi_z : 1d array of length: *refi_tri* node count.
                   The returned interpolated field (at *refi_tri* nodes)
        u!   Expected a TriInterpolator objectR   R   t	   tri_indexi    N(   R   t
   matplotlibt   trit   CubicTriInterpolatorR   R   t   TriInterpolatorR   R)   t   Truet   _interpolate_multikeysR   t   y(   R	   t   zt   triinterpolatorR   t   interpt   refi_triR&   t   refi_z(    (    s7   lib/python2.7/site-packages/matplotlib/tri/trirefine.pyt   refine_fieldŒ   s    		c   $      C` sÃ  |  j  } |  j } |  j } |  j } t j | ƒ d } t j | ƒ d } | d	 k	 r­ t j | ƒ } t j | ƒ | f k r­ t d j	 t j | ƒ t j | ƒ ƒ ƒ ‚ q­ n  t j
 | d k ƒ } d | | d }	 | |	 }
 t j |
 ƒ } t j |
 ƒ } | | | *| | | *t j t j t j | d t j ƒt j | d t j ƒt j | d t j ƒg ƒ ƒ } t j t j t j | d t j ƒt j | d t j ƒt j | d t j ƒd g ƒ ƒ } | | | f } | | k } | | } | | } | | | | f | | | | d d f d } | | | | f | | | | d d f d } | | | )| | | )| } t j | d g d t j ƒ} | } xs t d ƒ D]e } | | k } t j
 | ƒ } | | } t j | d t j ƒ| | d	 d	 … | f | <| | 7} q¡Wt j | ƒ } | | } | | } t j | | d	 d	 … f t j | t j d d t j ƒƒ ƒ } t j | d
 d ƒ}  | | }! | | |  f | | |! f <t j | d d g d t j ƒ}" t j | d	 d	 … d f | d	 d	 … d f | d	 d	 … d f g ƒ j |" d d	 d … d	 d	 … f <t j | d	 d	 … d f | d	 d	 … d f | d	 d	 … d f g ƒ j |" d d	 d … d	 d	 … f <t j | d	 d	 … d f | d	 d	 … d f | d	 d	 … d f g ƒ j |" d d	 d … d	 d	 … f <t j | d	 d	 … d f | d	 d	 … d f | d	 d	 … d f g ƒ j |" d d	 d … d	 d	 … f <t | | |" ƒ }# |  j d	 k	 r™|# j t j |  j d ƒ ƒ n  | d	 k r©|# S|# t j | d ƒ f Sd	 S(   u  
        This function refines a matplotlib.tri *triangulation* by splitting
        each triangle into 4 child-masked_triangles built on the edges midside
        nodes.
        The masked triangles, if present, are also split but their children
        returned masked.

        If *ancestors* is not provided, returns only a new triangulation:
        child_triangulation.

        If the array-like key table *ancestor* is given, it shall be of shape
        (ntri,) where ntri is the number of *triangulation* masked_triangles.
        In this case, the function returns
        (child_triangulation, child_ancestors)
        child_ancestors is defined so that the 4 child masked_triangles share
        the same index as their father: child_ancestors.shape = (4 * ntri,).

        i    uY   Incompatible shapes provide for triangulation.masked_triangles and ancestors: {0} and {1}iÿÿÿÿi   i   R   i   g      à?Nt   axisi   (   R   R1   t	   neighborsR   R   R   R   t   asarrayR   t   formatt   sumt   zerost   ravelt   vstackR   R   R   t   emptyR   t   logical_nott   abst   outert   argmint   TR   R   t   set_maskR   ($   R
   R"   R   R1   R9   R   t   nptsR!   t   borderst	   added_ptsR$   t   refi_xt   refi_yt
   edge_elemst   edge_apexest   edge_neighborst   mask_masterst   masterst   apex_masterst   x_addt   y_addt   new_pt_cornert   new_pt_midsidet   cum_sumt   imidt   mask_st_loct   n_masters_loct   elem_masters_loct   mask_slavest   slavest   slaves_masterst
   diff_tablet   slave_masters_apext   slaves_apext   child_trianglest   child_triangulation(    (    s7   lib/python2.7/site-packages/matplotlib/tri/trirefine.pyR   ·   s’    				'


!!!%

!!


*

(
"&8&8&8&8N(
   R   R   R   R   t   FalseR)   R   R7   t   staticmethodR   (    (    (    s7   lib/python2.7/site-packages/matplotlib/tri/trirefine.pyR   3   s   	H+(   R   t
   __future__R    R   R   R   t   sixt   numpyR   t   matplotlib.tri.triangulationR   t   matplotlib.tri.triinterpolateR+   t   objectR   R   (    (    (    s7   lib/python2.7/site-packages/matplotlib/tri/trirefine.pyt   <module>   s   "%