
    P/Phz3                     h    d Z ddlZddlmZ ddlmZ ddlZ G d d          Z G d de          Z	dS )	z'
Mesh refinement for triangular grids.
    N)_api)Triangulationc                       e Zd ZdZd ZdS )
TriRefinera{  
    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 implement:

    - ``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 an optional `~matplotlib.tri.TriInterpolator`,
      - 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                 J    t          j        t          |           || _        d S )N)triangulation)r   check_isinstancer   _triangulation)selfr   s     Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/matplotlib/tri/_trirefine.py__init__zTriRefiner.__init__*   s&    m=IIII+    N)__name__
__module____qualname____doc__r    r   r   r   r      s-         :, , , , ,r   r   c                   J     e Zd ZdZ fdZd	dZd
dZedd            Z xZ	S )UniformTriRefinerz
    Uniform mesh refinement by recursive subdivisions.

    Parameters
    ----------
    triangulation : `~matplotlib.tri.Triangulation`
        The encapsulated triangulation (to be refined)
    c                 J    t                                          |           d S N)superr   )r   r   	__class__s     r   r   zUniformTriRefiner.__init__=   s!    '''''r   F   c                    | j         }|j        j        d         }t          j        |t          j                  }t          |          D ]}|                     ||          \  }}|j        j        d         }|j        }|rt          j	        |dt          j                  }	| j         j
        }
|
-t          j        |d                              dd          |	|<   n|
|         }t          j        ||         d                              dd          |	||ddf         <   t          j        ||          d                              dd          |	|| ddf         <   ||	fS |S )a  
        Compute a 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, recursing *subdiv* times.  In the end, each triangle is hence
        divided into ``4**subdiv`` child triangles.

        Parameters
        ----------
        return_tri_index : bool, default: False
            Whether an index table indicating the father triangle index of each
            point is returned.
        subdiv : int, default: 3
            Recursion level for the subdivision.
            Each triangle is divided into ``4**subdiv`` child triangles;
            hence, the default results in 64 refined subtriangles for each
            triangle of the initial triangulation.

        Returns
        -------
        refi_triangulation : `~matplotlib.tri.Triangulation`
            The refined triangulation.
        found_index : int array
            Index of the initial triangulation containing triangle, for each
            point of *refi_triangulation*.
            Returned only if *return_tri_index* is set to True.
        r   dtypeNr   )r
   	trianglesshapenparangeint32range_refine_triangulation_oncexfullmaskrepeatreshape)r   return_tri_indexsubdivrefi_triangulationntri	ancestors_	refi_nptsrefi_trianglesfound_indextri_maskancestor_masks               r   refine_triangulationz&UniformTriRefiner.refine_triangulation@   s   < "0!+1!4 Id"(333	v 	/ 	/A,0,K,K"I-/ -/)		&(.q1	+5  	& ')Rrx@@@K*/H.0i	89/; /;;B72q>> N++ !) 3 "	)M*B*+!- !--4WR^^ N=!!!+;<  !#	)]N*C*+!- !--4WR^^ NM>111+<=  &{22%%r   Nc                 ,   |&t           j                            | j        |          }n't	          j        t           j        j        |           |}|                     |d          \  }}|                    |j	        |j
        |          d         }||fS )a  
        Refine a field defined on the encapsulated triangulation.

        Parameters
        ----------
        z : (npoints,) array-like
            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 : `~matplotlib.tri.TriInterpolator`, optional
            Interpolator used for field interpolation. If not specified,
            a `~matplotlib.tri.CubicTriInterpolator` will be used.
        subdiv : int, default: 3
            Recursion level for the subdivision.
            Each triangle is divided into ``4**subdiv`` child triangles.

        Returns
        -------
        refi_tri : `~matplotlib.tri.Triangulation`
             The returned refined triangulation.
        refi_z : 1D array of length: *refi_tri* node count.
             The returned interpolated field (at *refi_tri* nodes).
        N)triinterpolatorT)r,   r+   )	tri_indexr   )
matplotlibtriCubicTriInterpolatorr
   r   r	   TriInterpolatorr6   _interpolate_multikeysr&   y)r   zr8   r,   interprefi_trir3   refi_zs           r   refine_fieldzUniformTriRefiner.refine_field   s    0 "^88#Q( (FF !*."@2AC C C C$F $ 9 9D !: !2 !2+..J
k / ; ;;<>r   c           
      	   | j         }| j        }| j        }| j        }t	          j        |          d         }t	          j        |          d         }|ft	          j        |          }t	          j        |          |fk    r9t          dt	          j        |           dt	          j        |                     t	          j        |dk              }d|z  |z   dz  }	||	z   }
t	          j	        |
          }t	          j	        |
          }||d|<   ||d|<   t	          j
        t	          j        |t          j                  d          }t	          j        t	          j        dt          j                  |          }|||f         }||k    }||         }||         }||||f                  ||||d	z   dz  f                  z   d
z  }||||f                  ||||d	z   dz  f                  z   d
z  }|||d<   |||d<   |}t	          j        |dgt          j                  }|}t          d          D ]Y}||k    }t	          j        |          }||         }t	          j        |t          j                  |z   |dd|f         |<   ||z  }Zt	          j        |          }||         }||         }t	          j        ||ddf         t	          j        |t	          j        dt          j                            z
            }t	          j        |d	          } ||         }!||| f         |||!f<   t	          j        |dz  dg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        -|#                    t	          j        | j        d                     ||#S |#t	          j        |d          fS )a  
        Refine a `.Triangulation` by splitting each triangle into 4
        child-masked_triangles built on the edges midside nodes.

        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,).
        r   NzNIncompatible shapes provide for triangulation.masked_triangles and ancestors: z and r   r      r      g      ?)axis   )r&   r?   	neighborsr   r!   r    asarray
ValueErrorsumzerostiler"   r#   r)   emptyr$   logical_notabsouteronesargminvstackTr   r(   set_mask)$r   r/   r&   r?   rJ   r   nptsr.   borders	added_ptsr1   refi_xrefi_y
edge_elemsedge_apexesedge_neighborsmask_mastersmastersapex_mastersx_addy_addnew_pt_cornernew_pt_midsidecum_sumimidmask_st_locn_masters_locelem_masters_locmask_slavesslavesslaves_masters
diff_tableslave_masters_apexslaves_apexchild_triangleschild_triangulations$                                       r   r%   z,UniformTriRefiner._refine_triangulation_once   s   ( OO "+	!+	x{{1~x	""1% 
9--Ix	""tg-- Gx	**G G13)1D1DG GH H H &b))tVg%!+	9$	)$$)$$ uuuu WRYt28<<<a@@
i	!28 < < <dCC":{#:;"^3 \*"<09Wl2349W|A~&::;<=@CD9Wl2349W|A~&::;<=@CDtuutuu " 4)28<<<!HH 	% 	%D</KF;//M&{38:	RX9/ 9/ 9/1899N111d7#$45}$GG n\22K('4VInaaa&78HVRWQbh-G-G-GHHI J J
Yz:::!+..<../0v{*+ (DFA;bh???#%9!!!Q$1!5111a4 ." $# $##$ 	1aaa  $&9!!!Q$1!5111a4 ." $# $##$ 	1aaa  $&9!!!Q$1!5111a4 ." $# $##$ 	1aaa  $&9111a4 .A"6111a4 ." $# $##$ 	1aaa  ,FFOLL )((=3Eq)I)IJJJ&&&	)Q(?(???r   )Fr   )Nr   r   )
r   r   r   r   r   r6   rD   staticmethodr%   __classcell__)r   s   @r   r   r   /   s         ( ( ( ( (C& C& C& C&J$  $  $  $ L G@ G@ G@ \G@ G@ G@ G@ G@r   r   )
r   numpyr!   r:   r   matplotlib.tri._triangulationr   matplotlib.tri._triinterpolater   r   r   r   r   <module>rz      s               7 7 7 7 7 7 % % % % ,  ,  ,  ,  ,  ,  ,  ,FD@ D@ D@ D@ D@
 D@ D@ D@ D@ D@r   