
    kjg*                        d dl mZ d dlmZ d dlZddlmZmZ ddl	m
Z
mZmZmZ 	 	 	 d$d%dZe G d d                      Zd&dZ G d  d!          Z G d" d#          ZdS )'    )annotations)	dataclassN   )Cell
build_tree)FuncPointValuedPointbinary_search_zero   '  fnr   pminr	   pmax	min_depthint	max_quadstolnp.ndarray | Nonereturnlist[list[Point]]c           	     F   t          j        |          }t          j        |          }|	||z
  dz  }nt          j        |          }t          d| |||||          }t          || |                                          }t          || |                                          S )u   Get the curve representing fn([x,y])=0 on pmin[0] ≤ x ≤ pmax[0] ∩ pmin[1] ≤ y ≤ pmax[1]
    Returns as a list of curves, where each curve is a list of pointsNi     )npasarrayr   TriangulatortriangulateCurveTracertrace)r   r   r   r   r   r   quadtree	triangless           N/home/agentuser/manim-venv/lib/python3.11/site-packages/isosurfaces/isoline.pyplot_isoliner#      s     :dD:dD
{d{d"joo!RtY	3GGHXr3//;;==Iy"c**00222    c                  T    e Zd ZU ded<   	 dZded<   dZded<   dZded<   d	Zd
ed<   dS )Trianglelist[ValuedPoint]verticesNzTriangle | NonenextzValuedPoint | Nonenext_bisect_pointprevFboolvisited)__name__
__module____qualname____annotations__r)   r*   r+   r-    r$   r"   r&   r&       si         b D    ,00000 D    Gr$   r&   ar
   bcdcenter-tuple[Triangle, Triangle, Triangle, Triangle]c                    t          | ||g          t          |||g          t          |||g          t          || |g          fS )zLa,b,c,d should be clockwise oriented, with center on the inside of that quad)r&   )r3   r4   r5   r6   r7   s        r"   four_trianglesr:   +   sR    
 	!Q  !Q  !Q  !Q  	 r$   c                  b    e Zd ZdZd$d
Zd%dZd&dZd'dZd'dZd(dZ	d)dZ
d*dZd+d!Zd,d"Zd#S )-r   aK  While triangulating, also compute the isolines.

    Divides each quad into 8 triangles from the quad's center. This simplifies
    adjacencies between triangles for the general case of multiresolution quadtrees.

    Based on Manson, Josiah, and Scott Schaefer. "Isosurfaces
    over simplicial partitions of multiresolution grids." Computer Graphics Forum.
    Vol. 29. No. 2. Oxford, UK: Blackwell Publishing Ltd, 2010.
    (https://people.engr.tamu.edu/schaefer/research/iso_simplicial.pdf), but this
    does not currently implement placing dual vertices based on the gradient.
    rootr   r   r   r   
np.ndarrayr   Nonec                L    g | _         i | _        || _        || _        || _        d S N)r!   hanging_nextr<   r   r   )selfr<   r   r   s       r"   __init__zTriangulator.__init__D   s*    )+35	r$   list[Triangle]c                D    |                      | j                   | j        S r@   )triangulate_insider<   r!   )rB   s    r"   r   zTriangulator.triangulateK   s     	***~r$   quadc                   |j         r|j         D ]}|                     |           |                     |j         d         |j         d                    |                     |j         d         |j         d                    |                     |j         d         |j         d                    |                     |j         d         |j         d                    d S d S )Nr   r   r      )childrenrF   triangulate_crossing_rowtriangulate_crossing_col)rB   rG   childs      r"   rF   zTriangulator.triangulate_insideO   s    = 	N / /''....))$-*:DM!<LMMM))$-*:DM!<LMMM))$-*:DM!<LMMM))$-*:DM!<LMMMMM	N 	Nr$   r3   r4   c                   |j         ra|j         rZ|                     |j         d         |j         d                    |                     |j         d         |j         d                    dS |j         rD|                     |j         d         |           |                     |j         d         |           dS |j         rD|                     ||j         d                    |                     ||j         d                    dS |                     |          }|                     |          }|j        |j        k     rV|                     |j        d         |j        d                   }t          |j        d         ||j        d         ||          }nU|                     |j        d         |j        d                   }t          |j        d         ||j        d         ||          }|                     |           dS )z<Quad b should be to the right (greater x values) than quad ar   r   rI   r   N)rJ   rK   get_face_dualdepthget_edge_dualr(   r:   add_four_trianglesrB   r3   r4   face_dual_aface_dual_b	edge_dualr!   s          r"   rK   z%Triangulator.triangulate_crossing_rowX   s   : 	/!* 	/))!*Q-AGGG))!*Q-AGGGGGZ 	/))!*Q-;;;))!*Q-;;;;;Z 	/))!QZ];;;))!QZ];;;;; ,,Q//K,,Q//Kw   ..qz!}ajmLL	*1:a=+qzRS}Vaclmm		 ..qz!}ajmLL	*1:a=+qzRS}Vaclmm	##I.....r$   c                   |j         ra|j         rZ|                     |j         d         |j         d                    |                     |j         d         |j         d                    dS |j         rD|                     |j         d         |           |                     |j         d         |           dS |j         rD|                     ||j         d                    |                     ||j         d                    dS |                     |          }|                     |          }|j        |j        k     rV|                     |j        d         |j        d                   }t          |j        d         ||j        d         ||          }nU|                     |j        d         |j        d                   }t          |j        d         ||j        d         ||          }|                     |           dS )zMostly a copy-paste of triangulate_crossing_row. For n-dimensions, want to pass a
        dir index into a shared triangulate_crossing_dir function insteadr   r   rI   r   N)rJ   rL   rO   rP   rQ   r(   r:   rR   rS   s          r"   rL   z%Triangulator.triangulate_crossing_colq   s    : 	/!* 	/))!*Q-AGGG))!*Q-AGGGGGZ 	/))!*Q-;;;))!*Q-;;;;;Z 	/))!QZ];;;))!QZ];;;;; ,,Q//K,,Q//Kw   ..qz!}ajmLL	*1:a=+qzRS}Vaclmm		 ..qz!}ajmLL	*1:a=+qzRS}Vaclmm	##I.....r$   r!   r8   c                    t          d          D ]7}|                     ||         ||dz   dz           ||dz   dz                      8| j                            |           d S )N   r   r   )rangenext_sandwich_trianglesr!   extend)rB   r!   is      r"   rR   zTriangulator.add_four_triangles   ss    q 	g 	gA((1y!a%17MyZ[^_Z_cdYdOeffffi(((((r$   tri1r&   tri2vposr
   vnegc                    |j         dcxk    r|j         k    sn d S t          ||| j        | j                  \  }}|sd S ||_        ||_        ||_        d S )Nr   )valr   r   r   r*   r)   r+   )rB   r^   r_   r`   ra   intersectionis_zeros          r"   set_nextzTriangulator.set_next   sm    x!''''tx''''F 24tw Q Qg 	F!-				r$   r5   c                   |j         d         }|j         d         }|j         d         }|j        dcxk    r|j        k    rn n|                     ||||           |j        dcxk    r|j        k    rn n|                     ||||           |j        |j        z   j                                        }|j        dcxk    r|j        k    rEn nB|| j        v r-|                     || j        |         ||           | j        |= dS || j        |<   dS |j        dcxk    r|j        k     rFn dS || j        v r-|                     | j        |         |||           | j        |= dS || j        |<   dS dS )u  Find the "next" triangle for the triangle b. See Triangle for a description of the curve orientation.

        We assume the triangles are oriented such that they share common vertices center←a[2]≡b[2]≡c[2]
        and x←a[1]≡b[0], y←b[1]≡c[0]r   r   r   N)r(   rc   rf   posdatatobytesrA   )rB   r3   r4   r5   r7   xyids           r"   r[   z$Triangulator.next_sandwich_triangles   s    AJqMJqM :""""QU"""""MM!Q***51""""
"""""MM!Q6***
 eaem!))++ 51T&&&a!22!61===%b)))()!"%%%Ua!%T&&&d/3Q1===%b)))()!"%%%  r$   p1p2c                   |j         dk    |j         dk    k    rt          j        ||| j                  S d}|                     |j        d|z
  z  |j        |z  z             }|                     |j        |z  |j        d|z
  z  z             }|dk    |dk    k    rt          j        ||| j                  S t          |j        |          }t          |j        |          }t          j        ||| j                  S )z(Returns the dual point on an edge p1--p2r   g{Gz?r   )rc   r
   midpointr   rh   intersectZero)rB   rn   ro   dtdf1df2v1v2s           r"   rQ   zTriangulator.get_edge_dual   s    FQJBFQJ'''B888 ggbfB'"&2+566ggbfrkBFa"f$5566!Gq!! 'B888 RVS))BRVS))B,RTW===r$   c                d    t          j        |j        d         |j        d         | j                  S )Nr   )r
   rq   r(   r   )rB   rG   s     r"   rO   zTriangulator.get_face_dual   s'    #DM!$4dmB6GQQQr$   N)r<   r   r   r   r   r=   r   r>   )r   rD   )rG   r   r   r>   )r3   r   r4   r   r   r>   )r!   r8   r   r>   )
r^   r&   r_   r&   r`   r
   ra   r
   r   r>   )r3   r&   r4   r&   r5   r&   r   r>   )rn   r
   ro   r
   r   r
   )rG   r   r   r
   )r.   r/   r0   __doc__rC   r   rF   rK   rL   rR   rf   r[   rQ   rO   r2   r$   r"   r   r   7   s        
 
      N N N N/ / / /2/ / / /4) ) ) )
   #* #* #* #*J> > > >,R R R R R Rr$   r   c                  2    e Zd ZU ded<   ddZddZddZdS )r   r'   active_curver!   rD   r   r   r   r=   r   r>   c                0    || _         || _        || _        d S r@   )r!   r   r   )rB   r!   r   r   s       r"   rC   zCurveTracer.__init__   s    "r$   r   c                    g }| j         D ]F}|j        s=|j        6g | _        |                     |           |                    | j                   Gd |D             S )Nc                &    g | ]}d  |D             S )c                    g | ]	}|j         
S r2   )rh   ).0vs     r"   
<listcomp>z0CurveTracer.trace.<locals>.<listcomp>.<listcomp>   s    &&&1&&&r$   r2   )r   curves     r"   r   z%CurveTracer.trace.<locals>.<listcomp>   s'    ;;;5&&&&&;;;r$   )r!   r-   r)   r|   march_triangleappend)rB   curvestriangles      r"   r   zCurveTracer.trace   sp    *, 	1 	1H# 1(A$&!##H---d/000;;F;;;;r$   r   r&   c                $   |}d}|j         |j         }||u rd}n|j         |D|j        s=|j        | j                            |j                   d|_        |j        }||j        =|r'| j                            | j        d                    d S d S )NFTr   )r+   r-   r*   r|   r   r)   )rB   r   start_triangleclosed_loops       r"   r   zCurveTracer.march_triangle   s    !m'}H>))"	 m'
 "8+;")5!(()CDDD#H}H	 "8+;"
  	;$$T%6q%9:::::	; 	;r$   N)r!   rD   r   r   r   r=   r   r>   )r   r   )r   r&   r   r>   )r.   r/   r0   r1   rC   r   r   r2   r$   r"   r   r      s^         ####   
< < < <; ; ; ; ; ;r$   r   )r   r   N)r   r   r   r	   r   r	   r   r   r   r   r   r   r   r   )r3   r
   r4   r
   r5   r
   r6   r
   r7   r
   r   r8   )
__future__r   dataclassesr   numpyr   cellr   r   pointr   r	   r
   r   r#   r&   r:   r   r   r2   r$   r"   <module>r      sH   " " " " " " ! ! ! ! ! !     " " " " " " " " ? ? ? ? ? ? ? ? ? ? ? ? !3 3 3 3 3*        	 	 	 	`R `R `R `R `R `R `R `RF"; "; "; "; "; "; "; "; "; ";r$   