
    0-Ph82                        d dl Z d dlZddlmZ ddlmZ 	 dddddddd	d
Zd Zd Z	 ej
        d dgddgdd gd d gd dgddgdd gd d gd d gddgddgd d ggd          Z ej
        d d gd dgddgdd gd d gd dgddgdd gd d gd d gddgddggd          Z ej
        d d gd d gd d gd d gddgddgddgddgd dgd dgd dgd dggd          Zd Zd ZdS )    N   )_marching_cubes_lewiner_luts)_marching_cubes_lewiner_cy)      ?r   r   descentTlewiner)spacinggradient_direction	step_sizeallow_degeneratemethodmaskc          
      p    d}|dk    rd}n|dk    rt          d          t          | |||||||          S )a  Marching cubes algorithm to find surfaces in 3d volumetric data.

    In contrast with Lorensen et al. approach [2]_, Lewiner et
    al. algorithm is faster, resolves ambiguities, and guarantees
    topologically correct results. Therefore, this algorithm generally
    a better choice.

    Parameters
    ----------
    volume : (M, N, P) ndarray
        Input data volume to find isosurfaces. Will internally be
        converted to float32 if necessary.
    level : float, optional
        Contour value to search for isosurfaces in `volume`. If not
        given or None, the average of the min and max of vol is used.
    spacing : length-3 tuple of floats, optional
        Voxel spacing in spatial dimensions corresponding to numpy array
        indexing dimensions (M, N, P) as in `volume`.
    gradient_direction : string, optional
        Controls if the mesh was generated from an isosurface with gradient
        descent toward objects of interest (the default), or the opposite,
        considering the *left-hand* rule.
        The two options are:
        * descent : Object was greater than exterior
        * ascent : Exterior was greater than object
    step_size : int, optional
        Step size in voxels. Default 1. Larger steps yield faster but
        coarser results. The result will always be topologically correct
        though.
    allow_degenerate : bool, optional
        Whether to allow degenerate (i.e. zero-area) triangles in the
        end-result. Default True. If False, degenerate triangles are
        removed, at the cost of making the algorithm slower.
    method: {'lewiner', 'lorensen'}, optional
        Whether the method of Lewiner et al. or Lorensen et al. will be used.
    mask : (M, N, P) array, optional
        Boolean array. The marching cube algorithm will be computed only on
        True elements. This will save computational time when interfaces
        are located within certain region of the volume M, N, P-e.g. the top
        half of the cube-and also allow to compute finite surfaces-i.e. open
        surfaces that do not end at the border of the cube.

    Returns
    -------
    verts : (V, 3) array
        Spatial coordinates for V unique mesh vertices. Coordinate order
        matches input `volume` (M, N, P). If ``allow_degenerate`` is set to
        True, then the presence of degenerate triangles in the mesh can make
        this array have duplicate vertices.
    faces : (F, 3) array
        Define triangular faces via referencing vertex indices from ``verts``.
        This algorithm specifically outputs triangles, so each face has
        exactly three indices.
    normals : (V, 3) array
        The normal direction at each vertex, as calculated from the
        data.
    values : (V,) array
        Gives a measure for the maximum value of the data in the local region
        near each vertex. This can be used by visualization tools to apply
        a colormap to the mesh.

    See Also
    --------
    skimage.measure.mesh_surface_area
    skimage.measure.find_contours

    Notes
    -----
    The algorithm [1]_ is an improved version of Chernyaev's Marching
    Cubes 33 algorithm. It is an efficient algorithm that relies on
    heavy use of lookup tables to handle the many different cases,
    keeping the algorithm relatively easy. This implementation is
    written in Cython, ported from Lewiner's C++ implementation.

    To quantify the area of an isosurface generated by this algorithm, pass
    verts and faces to `skimage.measure.mesh_surface_area`.

    Regarding visualization of algorithm output, to contour a volume
    named `myvolume` about the level 0.0, using the ``mayavi`` package::

      >>>
      >> from mayavi import mlab
      >> verts, faces, _, _ = marching_cubes(myvolume, 0.0)
      >> mlab.triangular_mesh([vert[0] for vert in verts],
                              [vert[1] for vert in verts],
                              [vert[2] for vert in verts],
                              faces)
      >> mlab.show()

    Similarly using the ``visvis`` package::

      >>>
      >> import visvis as vv
      >> verts, faces, normals, values = marching_cubes(myvolume, 0.0)
      >> vv.mesh(np.fliplr(verts), faces, normals, values)
      >> vv.use().Run()

    To reduce the number of triangles in the mesh for better performance,
    see this `example
    <https://docs.enthought.com/mayavi/mayavi/auto/example_julia_set_decimation.html#example-julia-set-decimation>`_
    using the ``mayavi`` package.

    References
    ----------
    .. [1] Thomas Lewiner, Helio Lopes, Antonio Wilson Vieira and Geovan
           Tavares. Efficient implementation of Marching Cubes' cases with
           topological guarantees. Journal of Graphics Tools 8(2)
           pp. 1-15 (december 2003).
           :DOI:`10.1080/10867651.2003.10487582`
    .. [2] Lorensen, William and Harvey E. Cline. Marching Cubes: A High
           Resolution 3D Surface Construction Algorithm. Computer Graphics
           (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170).
           :DOI:`10.1145/37401.37422`
    FlorensenTr   z/method should be either 'lewiner' or 'lorensen')use_classicr   )
ValueError_marching_cubes_lewiner)	volumelevelr	   r
   r   r   r   r   r   s	            g/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/measure/_marching_cubes_lewiner.pymarching_cubesr   	   se    z K	9		JKKK"	 	 	 	    c                 D   t          | t          j                  r| j        dk    rt	          d          | j        d         dk     s"| j        d         dk     s| j        d         dk     rt	          d          t          j        | t          j                  } |-d|                                 | 	                                z   z  }nNt          |          }||                                 k     s|| 	                                k    rt	          d	          t          |          dk    rt	          d
          t          |          }|dk     rt	          d          t          |          }t                      }||j        | j        k    st	          d          t          j        }	 |	| |||||          \  }
}}}t          |
          st#          d          t          j        |
          }
t          j        |          }d|_        |dk    rt          j        |          }n|dk    st	          d| d          t          j        |d          s|
t          j        |         z  }
|r|
|||fS t          j        } ||
                    t          j                  |||          S )zdLewiner et al. algorithm for marching cubes. See
    marching_cubes_lewiner for documentation.

       z(Input volume should be a 3D numpy array.r      r   z#Input array must be at least 2x2x2.N      ?z/Surface level must be within volume data range.z'`spacing` must consist of three floats.zstep_size must be at least one.z)volume and mask must have the same shape.z(No surface found at the given iso value.)r   r   ascentzIncorrect input z( in `gradient_direction`, see docstring.)r   r   r   )
isinstancenpndarrayndimr   shapeascontiguousarrayfloat32minmaxfloatlenintbool_get_mc_lutsr   r   RuntimeErrorfliplrarray_equalr_remove_degenerate_facesastype)r   r   r	   r
   r   r   r   r   Lfuncverticesfacesnormalsvaluesfuns                  r   r   r      s     fbj)) EfkQ.>.>CDDD|Afl1o11V\!_q5H5H>???!&"*55F }vzz||fjjll23e6::<<56::<<#7#7NOOO
7||qBCCCII1}}:;;;{##K 	A zV\))HIII &4D'+tq)[$( ($HeWf x== GEFFF y""Hi  G EKY&& 	%  8++1   
 
 	
 >'9-- -beGn, H//(@s8??2:..wGGGr   c                     | \  }}t          j        |                    d                    }t          j        |d          }||_        |S )Nzutf-8int8)dtype)base64decodebytesencoder    
frombufferr#   )argsr#   textbytsars        r   	_to_arrayrE      sG    KE4dkk'2233D	t6	*	*	*BBHIr   r;   c                  
   t          t          d          st          j        g t          t
          t          t          t          j                  t          t          j	                  t          t          j
                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                  t          t          j                   t          t          j!                  t          t          j"                  t          t          j#                  t          t          j$                  t          t          j%                  t          t          j&                  t          t          j'                  t          t          j(                  t          t          j)                  t          t          j*                  t          t          j+                  t          t          j,                  t          t          j-                  t          t          j.                  t          t          j/                  t          t          j0                  t          t          j1                  t          t          j2                  t          t          j3                  t          t          j4                  t          t          j5                  t          t          j6                  t          t          j7                  R  t          _8        t          j8        S )z#Kind of lazy obtaining of the luts.THE_LUTS)9hasattrmclutsr   LutProviderEDGETORELATIVEPOSXEDGETORELATIVEPOSYEDGETORELATIVEPOSZrE   CASESCLASSICCASESTILING1TILING2	TILING3_1	TILING3_2	TILING4_1	TILING4_2TILING5TILING6_1_1TILING6_1_2	TILING6_2	TILING7_1	TILING7_2	TILING7_3TILING7_4_1TILING7_4_2TILING8TILING9TILING10_1_1TILING10_1_1_TILING10_1_2
TILING10_2TILING10_2_TILING11TILING12_1_1TILING12_1_1_TILING12_1_2
TILING12_2TILING12_2_
TILING13_1TILING13_1_
TILING13_2TILING13_2_
TILING13_3TILING13_3_
TILING13_4TILING13_5_1TILING13_5_2TILING14TEST3TEST4TEST6TEST7TEST10TEST12TEST13SUBCONFIG13rG    r   r   r,   r,      s[   6:&& 5
4@ 4
4
4
 4
 f)**	4

 fl##4
 fn%%4
 fn%%4
 f&''4
 f&''4
 f&''4
 f&''4
 fn%%4
 f())4
 f())4
 f&''4
  f&''!4
" f&''#4
$ f&''%4
& f())'4
( f()))4
* fn%%+4
, fn%%-4
. f)**/4
0 f*++14
2 f)**34
4 f'((54
6 f())74
8 fo&&94
: f)**;4
< f*++=4
> f)**?4
@ f'((A4
B f())C4
D f'((E4
F f())G4
H f'((I4
J f())K4
L f'((M4
N f())O4
P f'((Q4
R f)**S4
T f)**U4
V fo&&W4
X fl##Y4
Z fl##[4
\ fl##]4
^ fl##_4
` fm$$a4
b fm$$c4
d fm$$e4
f f())g4
 4
 4
l ?r   c                    | |         }|dddddf         |dddddf         z
  }|dddddf         |dddddf         z
  }~t          j        ||          dz                      d          dz                                  dz  S )a=  Compute surface area, given vertices and triangular faces.

    Parameters
    ----------
    verts : (V, 3) array of floats
        Array containing coordinates for V unique mesh vertices.
    faces : (F, 3) array of ints
        List of length-3 lists of integers, referencing vertex coordinates as
        provided in `verts`.

    Returns
    -------
    area : float
        Surface area of mesh. Units now [coordinate units] ** 2.

    Notes
    -----
    The arguments expected by this function are the first two outputs from
    `skimage.measure.marching_cubes`. For unit correct output, ensure correct
    `spacing` was passed to `skimage.measure.marching_cubes`.

    This algorithm works properly only if the ``faces`` provided are all
    triangles.

    See Also
    --------
    skimage.measure.marching_cubes

    Nr   r   r   )axisr   g       @)r    crosssum)vertsr6   actual_vertsabs        r   mesh_surface_arear   ;  s    > <LQQQ111WQQQ111W 55AQQQ111WQQQ111W 55A Xa^^q %%1%--499;;cAAr   )N)r=   numpyr     r   rI   r   r   r   rE   arrayrK   rL   rM   r,   r   r~   r   r   <module>r      s        4 4 4 4 4 4 ( ( ( ( ( (
 K  	K K K K K\OH OH OHd    RX1qeQqE1Q%!A!uaUAa5STUVRWYZ[\X]_`ab^cefghdikmstt RX1qeQqE1Q%!A!uaUAa5STUVRWYZ[\X]_`ab^cefghdikmstt RX1qeQqE1Q%!A!uaUAa5STUVRWYZ[\X]_`ab^cefghdikmstt 9 9 9x%B %B %B %B %Br   