U
    ?h@1                     @   sb  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
dZdd Zdd Z	e
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
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
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d Zd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          	   
   C   s:   d}|dkrd}n|dkr"t dt| |||||||d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) array
        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`
    FZlorensenTr   z/method should be either 'lewiner' or 'lorensen')use_classicr   )
ValueError_marching_cubes_lewiner)	volumelevelr   r	   r
   r   r   r   r    r   Y/var/www/html/venv/lib/python3.8/site-packages/skimage/measure/_marching_cubes_lewiner.pymarching_cubes	   s    u  r   c                 C   s  t | tjr| jdkrtd| jd dk sH| jd dk sH| jd dk rPtdt| tj} |dkr|d|  | 	   }n(t
|}||  k s|| 	 krtd	t|dkrtd
t|}|dk rtdt|}t }|dk	r|j| jkstdtj}	|	| |||||\}
}}}t|
s,tdt|
}
t|}d|_|dkr\t|}n|dksvtd| dt|ds|
tj|  }
|r|
|||fS tj}||
tj|||S d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   ZascentzIncorrect input z( in `gradient_direction`, see docstring.)r   r   r   )
isinstancenpZndarrayndimr   shapeZascontiguousarrayZfloat32minmaxfloatlenintbool_get_mc_lutsr   r   RuntimeErrorZfliplrZarray_equalZr_Zremove_degenerate_facesZastype)r   r   r   r	   r
   r   r   r   LfuncZverticesfacesZnormalsvaluesZfunr   r   r   r      sZ    *  





r   c                 C   s0   | \}}t |d}tj|dd}||_|S )Nzutf-8int8)Zdtype)base64decodebytesencoder   Z
frombufferr   )argsr   textZbytsarr   r   r   	_to_array   s
    r1   r*   c                6   C   s  t tdst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3t_8tj8S )z) Kind of lazy obtaining of the luts.
    THE_LUTS)9hasattrmclutsr   ZLutProviderEDGETORELATIVEPOSXEDGETORELATIVEPOSYEDGETORELATIVEPOSZr1   ZCASESCLASSICZCASESZTILING1ZTILING2Z	TILING3_1Z	TILING3_2Z	TILING4_1Z	TILING4_2ZTILING5ZTILING6_1_1ZTILING6_1_2Z	TILING6_2Z	TILING7_1Z	TILING7_2Z	TILING7_3ZTILING7_4_1ZTILING7_4_2ZTILING8ZTILING9ZTILING10_1_1ZTILING10_1_1_ZTILING10_1_2Z
TILING10_2ZTILING10_2_ZTILING11ZTILING12_1_1ZTILING12_1_1_ZTILING12_1_2Z
TILING12_2ZTILING12_2_Z
TILING13_1ZTILING13_1_Z
TILING13_2ZTILING13_2_Z
TILING13_3ZTILING13_3_Z
TILING13_4ZTILING13_5_1ZTILING13_5_2ZTILING14ZTEST3ZTEST4ZTEST6ZTEST7ZTEST10ZTEST12ZTEST13ZSUBCONFIG13r2   r   r   r   r   r$      sn                                      r$   c                 C   s   | | }|dddddf |dddddf  }|dddddf |dddddf  }~t ||d jddd  d S )aG  Compute surface area, given vertices and triangular faces.

    Parameters
    ----------
    verts : (V, 3) array of floats
        Array containing (x, y, z) 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   )Zaxisr   g       @)r   crosssum)Zvertsr(   Zactual_vertsabr   r   r   mesh_surface_area  s
    ,,r<   )N)r+   numpyr    r   r4   r   r   r   r1   arrayr5   r6   r7   r$   r<   r   r   r   r   <module>   s$       JTTT