ó
 ‰\c           @   s  d  d l  Z  d  d l Z d  d l Z d d l m Z d d l m Z e  j d k r_ e j	 Z
 n	 e j Z
 d d d d e e 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 S(   iÿÿÿÿNi   (   t   _marching_cubes_lewiner_luts(   t   _marching_cubes_lewiner_cyi   g      ð?t   descentc         C   sg  t  |  t j ƒ s" |  j d k r1 t d ƒ ‚ n  |  j d d k  sj |  j d d k  sj |  j d d k  ry t d ƒ ‚ n  t j |  t j ƒ }  | d k r· d |  j	 ƒ  |  j
 ƒ  } n? t | ƒ } | |  j	 ƒ  k  sç | |  j
 ƒ  k rö t d ƒ ‚ n  t | ƒ d k rt d	 ƒ ‚ n  t | ƒ } | d k  r>t d
 ƒ ‚ n  t | ƒ } t ƒ  } t j } | |  | | | | ƒ \ }	 }
 } } t |	 ƒ s›t d ƒ ‚ n  t j |	 ƒ }	 t j | ƒ } d |
 _ | d k ràt j |
 ƒ }
 n | d k sÿt d | ƒ ‚ n  t j | d ƒ s%|	 t j | }	 n  | r;|	 |
 | | f St j } | |	 j t j ƒ |
 | | ƒ Sd S(   s¡  
    Lewiner marching cubes algorithm to find surfaces in 3d volumetric data.

    In contrast to ``marching_cubes_classic()``, this algorithm is faster,
    resolves ambiguities, and guarantees topologically correct results.
    Therefore, this algorithm generally a better choice, unless there
    is a specific need for the classic algorithm.

    Parameters
    ----------
    volume : (M, N, P) array
        Input data volume to find isosurfaces. Will internally be
        converted to float32 if necessary.
    level : float
        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
        Voxel spacing in spatial dimensions corresponding to numpy array
        indexing dimensions (M, N, P) as in `volume`.
    gradient_direction : string
        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
        Step size in voxels. Default 1. Larger steps yield faster but
        coarser results. The result will always be topologically correct
        though.
    allow_degenerate : bool
        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.
    use_classic : bool
        If given and True, the classic marching cubes by Lorensen (1987)
        is used. This option is included for reference purposes. Note
        that this algorithm has ambiguities and is not guaranteed to
        produce a topologically correct result. The results with using
        this option are *not* generally the same as the
        ``marching_cubes_classic()`` function.

    Returns
    -------
    verts : (V, 3) array
        Spatial coordinates for V unique mesh vertices. Coordinate order
        matches input `volume` (M, N, P).
    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.

    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 # doctest: +SKIP
      >>> verts, faces, normals, values = marching_cubes_lewiner(myvolume, 0.0) # doctest: +SKIP
      >>> mlab.triangular_mesh([vert[0] for vert in verts],
      ...                      [vert[1] for vert in verts],
      ...                      [vert[2] for vert in verts],
      ...                      faces) # doctest: +SKIP
      >>> mlab.show() # doctest: +SKIP

    Similarly using the ``visvis`` package::

      >>> import visvis as vv # doctest: +SKIP
      >>> verts, faces, normals, values = marching_cubes_lewiner(myvolume, 0.0) # doctest: +SKIP
      >>> vv.mesh(np.fliplr(verts), faces, normals, values) # doctest: +SKIP
      >>> vv.use().Run() # doctest: +SKIP

    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

    See Also
    --------
    skimage.measure.marching_cubes_classic
    skimage.measure.mesh_surface_area
    i   s(   Input volume should be a 3D numpy array.i    i   i   s#   Input array must be at least 2x2x2.g      à?s/   Surface level must be within volume data range.s'   `spacing` must consist of three floats.s   step_size must be at least one.s(   No surface found at the given iso value.iÿÿÿÿR   t   ascents:   Incorrect input %s in `gradient_direction`, see docstring.N(   iÿÿÿÿi   (   i   i   i   (   t
   isinstancet   npt   ndarrayt   ndimt
   ValueErrort   shapet   ascontiguousarrayt   float32t   Nonet   mint   maxt   floatt   lent   intt   boolt   _get_mc_lutsR   t   marching_cubest   RuntimeErrort   fliplrt   array_equalt   r_t   remove_degenerate_facest   astype(   t   volumet   levelt   spacingt   gradient_directiont	   step_sizet   allow_degeneratet   use_classict   Lt   funct   verticest   facest   normalst   valuest   fun(    (    sF   lib/python2.7/site-packages/skimage/measure/_marching_cubes_lewiner.pyt   marching_cubes_lewiner   sF    j"9$		$		c         C   sC   |  \ } } t  | j d ƒ ƒ } t j | d d ƒ} | | _ | S(   Ns   utf-8t   dtypet   int8(   t   base64decodet   encodeR   t
   frombufferR	   (   t   argsR	   t   textt   bytst   ar(    (    sF   lib/python2.7/site-packages/skimage/measure/_marching_cubes_lewiner.pyt	   _to_array²   s
    	i    R+   c        5   C   sq  t  t d ƒ sjt j 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 ƒ ƒ3 t _8 n  t j8 S(   s)    Kind of lazy obtaining of the luts.
    t   THE_LUTS(9   t   hasattrt   mclutsR   t   LutProvidert   EDGETORELATIVEPOSXt   EDGETORELATIVEPOSYt   EDGETORELATIVEPOSZR3   t   CASESCLASSICt   CASESt   TILING1t   TILING2t	   TILING3_1t	   TILING3_2t	   TILING4_1t	   TILING4_2t   TILING5t   TILING6_1_1t   TILING6_1_2t	   TILING6_2t	   TILING7_1t	   TILING7_2t	   TILING7_3t   TILING7_4_1t   TILING7_4_2t   TILING8t   TILING9t   TILING10_1_1t   TILING10_1_1_t   TILING10_1_2t
   TILING10_2t   TILING10_2_t   TILING11t   TILING12_1_1t   TILING12_1_1_t   TILING12_1_2t
   TILING12_2t   TILING12_2_t
   TILING13_1t   TILING13_1_t
   TILING13_2t   TILING13_2_t
   TILING13_3t   TILING13_3_t
   TILING13_4t   TILING13_5_1t   TILING13_5_2t   TILING14t   TEST3t   TEST4t   TEST6t   TEST7t   TEST10t   TEST12t   TEST13t   SUBCONFIG13R4   (    (    (    sF   lib/python2.7/site-packages/skimage/measure/_marching_cubes_lewiner.pyR   Å   s(    	00$$$$$$$$$$$$$(   i   (   g      ð?g      ð?g      ð?(   t   syst   base64t   numpyR   t    R    R6   R   t   version_infot   decodebytesR,   t   decodestringR   t   Truet   FalseR)   R3   t   arrayR8   R9   R:   R   (    (    (    sF   lib/python2.7/site-packages/skimage/measure/_marching_cubes_lewiner.pyt   <module>   s   	 	~~~