
    hz3                     Z    S r SSKrSSKJr  SSKJr  SSKr " S S5      r " S S\5      r	g)	z'
Mesh refinement for triangular grids.
    N)_api)Triangulationc                       \ rS rSrSrS rSrg)
TriRefiner   a+  
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                 B    [         R                  " [        US9  Xl        g )N)triangulation)r   check_isinstancer   _triangulation)selfr	   s     K/var/www/html/env/lib/python3.13/site-packages/matplotlib/tri/_trirefine.py__init__TriRefiner.__init__*   s    m=I+    )r   N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__ r   r   r   r      s    :,r   r   c                   T   ^  \ rS rSrSrU 4S jrSS jrS	S jr\S
S j5       r	Sr
U =r$ )UniformTriRefiner/   z
Uniform mesh refinement by recursive subdivisions.

Parameters
----------
triangulation : `~matplotlib.tri.Triangulation`
    The encapsulated triangulation (to be refined)
c                 $   > [         TU ]  U5        g N)superr   )r   r	   	__class__s     r   r   UniformTriRefiner.__init__=   s    'r   c                    U R                   nUR                  R                  S   n[        R                  " U[        R
                  S9n[        U5       H  nU R                  X55      u  p5M     UR                  R                  S   nUR                  nU(       a  [        R                  " US[        R
                  S9n	U R                   R                  n
U
c,  [        R                  " US5      R                  SS5      X'   X94$ X   n[        R                  " X[   S5      R                  SS5      XUSS24   '   [        R                  " X[)    S5      R                  SS5      XU) SS24   '   X94$ U$ )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   dtypeN   )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_triangulation&UniformTriRefiner.refine_triangulation@   sp   < "00!++11!4 IId"((3	vA,0,K,K"-/)	  '((..q1	+55  '')Rrxx@K**//H.0ii	89/;;B72q> + &22 !) 3 "		)*B*+!--4WR^ =!+;<  !#		)N*C*+!--4WR^ M>1+<=  &22%%r   c                 6   Uc+  [         R                  R                  U R                  U5      nO/[        R
                  " [         R                  R                  US9  UnU R                  USS9u  pVUR                  UR                  UR                  US9S   nXW4$ )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).
)triinterpolatorT)r2   r1   )	tri_indexr   )
matplotlibtriCubicTriInterpolatorr   r   r
   TriInterpolatorr<   _interpolate_multikeysr,   y)r   zr?   r2   interprefi_trir9   refi_zs           r   refine_fieldUniformTriRefiner.refine_field   s    0 "^^88##Q(F !!*.."@"@2AC$F $ 9 9D !: !2..JJ

k / ;;<>r   c                 f	   U R                   nU R                  nU R                  nU R                  n[        R
                  " U5      S   n[        R
                  " U5      S   nUbj  [        R                  " U5      n[        R
                  " U5      U4:w  a9  [        S[        R
                  " U5       S[        R
                  " U5       35      e[        R                  " US:H  5      nSU-  U-   S-  n	Xi-   n
[        R                  " U
5      n[        R                  " U
5      nX+SU& X<SU& [        R                  " [        R                  " U[        R                  S9S5      n[        R                  " [        R                  " S[        R                  S9U5      nXMU4   nX:  nUU   nUU   nX%UU4      X%UUS	-   S-  4      -   S
-  nX5UU4      X5UUS	-   S-  4      -   S
-  nUXS& UXS& Un[        R                  " US/[        R                  S9nUn[        S5       HX  nUU:H  n[        R                  " U5      nUU   n[        R                  " U[        R                  S9U-   USS2U4   U'   UU-  nMZ     [        R                   " U5      nUU   nUU   n[        R"                  " UUSS24   [        R$                  " U[        R&                  " S[        R                  S95      -
  5      n[        R(                  " US	S9n UU   n!UUU 4   UUU!4'   [        R                  " US-  S/[        R                  S9n"[        R*                  " USS2S4   USS2S4   USS2S4   /5      R,                  U"SSS2SS24'   [        R*                  " USS2S	4   USS2S	4   USS2S4   /5      R,                  U"S	SS2SS24'   [        R*                  " USS2S4   USS2S4   USS2S	4   /5      R,                  U"SSS2SS24'   [        R*                  " USS2S4   USS2S	4   USS2S4   /5      R,                  U"SSS2SS24'   [/        XU"5      n#U R0                  b0  U#R3                  [        R                  " U R0                  S5      5        Uc  U#$ U#[        R                  " US5      4$ )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,   rF   	neighborsr%   r'   r&   asarray
ValueErrorsumzerostiler(   r)   r/   emptyr*   logical_notabsouteronesargminvstackTr   r.   set_mask)$r	   r5   r,   rF   rR   r%   nptsr4   borders	added_ptsr7   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+   ,UniformTriRefiner._refine_triangulation_once   s   ( OOOO "++	!++	xx{1~xx	"1% 

9-Ixx	"tg- Exx	*+5)1D0EGH H &&b)tVg%!+	$	)$)$ uu WWRYYt288<a@
ii		!288 <dC"{#:;"3 \*"<0Wl234W|A~&::;<=@CDWl234W|A~&::;<=@CDuu " 4)288<!HD</KFF;/M&{38:		RXX9/1899N1d7#$45}$G  nn\2K('4VVIna&78HHVRWWQbhh-GHI J
YYz:!+..<../0v{*+ ((DFA;bhh?#%99!Q$1!51a4 ." $##$1 	1a  $&99!Q$1!51a4 ." $##$1 	1a  $&99!Q$1!51a4 ." $##$1 	1a  $&991a4 .A"61a4 ." $##$1 	1a  ,FOL )((=3E3Eq)IJ&&&		)Q(???r   r   )Fr$   )Nr$   r   )r   r   r   r   r   r   r<   rK   staticmethodr+   r   __classcell__)r   s   @r   r   r   /   s2    (C&J$ L G@ G@r   r   )
r   numpyr'   rA   r   matplotlib.tri._triangulationr   matplotlib.tri._triinterpolater   r   r   r   r   <module>r      s3      7 % ,  ,FD@
 D@r   