ó
±éRc           @  sË   d  d l  m Z d  d l Z y e Wn& e k
 rL d  d l Z e j Z n Xd  d l m Z d  d l	 Z
 d  d l m Z d  d l m Z m Z d d g Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d S(
   iÿÿÿÿ(   t   print_functionN(   t   izip(   t   delaunay(   t   LinearInterpolatort   NNInterpolatort   Triangulationt   DuplicatePointWarningc           B  s   e  Z d  Z RS(   sB   Duplicate points were passed in to the triangulation routine.
    (   t   __name__t
   __module__t   __doc__(    (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyR      s   c           B  s‰   e  Z d  Z d „  Z d „  Z d „  Z d „  Z e j d „ Z	 e j d „ Z
 d d „ Z d e j d „ Z d e j d	 „ Z d
 „  Z RS(   s  A Delaunay triangulation of points in a plane.

    Triangulation(x, y)
    x, y -- the coordinates of the points as 1-D arrays of floats

    Let us make the following definitions:
        npoints = number of points input
        nedges = number of edges in the triangulation
        ntriangles = number of triangles in the triangulation

        point_id = an integer identifying a particular point (specifically, an
            index into x and y), range(0, npoints)
        edge_id = an integer identifying a particular edge, range(0, nedges)
        triangle_id = an integer identifying a particular triangle
            range(0, ntriangles)

    Attributes: (all should be treated as read-only to maintain consistency)
      x, y -- the coordinates of the points as 1-D arrays of floats.

      circumcenters -- (ntriangles, 2) array of floats giving the (x,y)
        coordinates of the circumcenters of each triangle (indexed by a
        triangle_id).

      edge_db -- (nedges, 2) array of point_id's giving the points forming
        each edge in no particular order; indexed by an edge_id.

      triangle_nodes -- (ntriangles, 3) array of point_id's giving the points
        forming each triangle in counter-clockwise order; indexed by a
        triangle_id.

      triangle_neighbors -- (ntriangles, 3) array of triangle_id's giving the
        neighboring triangle; indexed by a triangle_id.

        The value can also be -1 meaning that that edge is on the convex hull
        of the points and there is no neighbor on that edge. The values are
        ordered such that triangle_neighbors[tri, i] corresponds with the edge
        *opposite* triangle_nodes[tri, i]. As such, these neighbors are also
        in counter-clockwise order.

      hull -- list of point_id's giving the nodes which form the convex hull
        of the point set. This list is sorted in counter-clockwise order.

    Duplicate points.
      If there are no duplicate points, Triangulation stores the specified
      x and y arrays and there is no difference between the client's and
      Triangulation's understanding of point indices used in edge_db,
      triangle_nodes and hull.

      If there are duplicate points, they are removed from the stored
      self.x and self.y as the underlying delaunay code cannot deal with
      duplicates.  len(self.x) is therefore equal to len(x) minus the
      number of duplicate points.  Triangulation's edge_db, triangle_nodes
      and hull refer to point indices in self.x and self.y, for internal
      consistency within Triangulation and the corresponding Interpolator
      classes.  Client code must take care to deal with this in one of
      two ways:

      1. Ignore the x,y it specified in Triangulation's constructor and
         use triangulation.x and triangulation.y instead, as these are
         consistent with edge_db, triangle_nodes and hull.

      2. If using the x,y the client specified then edge_db,
         triangle_nodes and hull should be passed through the function
         to_client_point_indices() first.
    c         C  sW  t  j | d t  j ƒ|  _ t  j | d t  j ƒ|  _ |  j j |  j j k sf t |  j j ƒ d k ru t d ƒ ‚ n  |  j j |  _ |  j	 ƒ  } t | ƒ d k rt
 j d t ƒ t  j t  j t |  j ƒ ƒ | ƒ |  _ |  j |  j |  _ |  j |  j |  _ n	 d  |  _ |  j |  _ t |  j |  j ƒ \ |  _ |  _ |  _ |  _ |  j ƒ  |  _ d  S(   Nt   dtypei   s#   x,y must be equal-length 1-D arraysi    sB   Input data contains duplicate x,y points; some values are ignored.(   t   npt   asarrayt   float64t   xt   yt   shapet   lent
   ValueErrort	   old_shapet   _get_duplicate_point_indicest   warningst   warnR   t   deletet   aranget   j_uniquet   Nonet   _client_point_index_mapR   t   circumcenterst   edge_dbt   triangle_nodest   triangle_neighborst   _compute_convex_hullt   hull(   t   selfR   R   t
   duplicates(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyt   __init__Z   s"    0'	-c         C  sk   t  j d |  j |  j f ƒ } t  j t t  j |  j | ƒ d k t  j |  j | ƒ d k @g ƒ } | | S(   s‚   Return array of indices of x,y points that are duplicates of
        previous points. Indices are in no particular order.
        t   keysi    (   R   t   lexsortR   R   t   hstackt   Falset   diff(   R"   t   j_sortedt   mask_duplicates(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyR   ~   s    #c         C  sš  |  j  d k } i  } | j t t |  j | d d … d f d d … d f |  j | d d … d f d d … d f ƒ ƒ ƒ | j t t |  j | d d … d f d d … d f |  j | d d … d f d d … d f ƒ ƒ ƒ | j t t |  j | d d … d f d d … d f |  j | d d … d f d d … d f ƒ ƒ ƒ t | j ƒ  ƒ } x$ | r‹| j | j | d ƒ ƒ qhW| j ƒ  | S(   sÁ   Extract the convex hull from the triangulation information.

        The output will be a list of point_id's in counter-clockwise order
        forming the convex hull of the data set.
        iÿÿÿÿNi    i   i   (	   R   t   updatet   dictR   R   t   listt   popitemt   appendt   pop(   R"   t   bordert   edgesR!   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyR    �   s    646464	
c         C  s"   |  j  d k	 r |  j  | S| Sd S(   sÂ   Converts any array of point indices used within this class to
        refer to point indices within the (x,y) arrays specified in the
        constructor before duplicates were removed.
        N(   R   R   (   R"   t   array(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyt   to_client_point_indices§   s    c         C  sh   t  j | d t  j ƒ} | j |  j k r9 t d ƒ ‚ n  |  j d k	 rX | |  j } n  t |  | | ƒ S(   sâ   Get an object which can interpolate within the convex hull by
        assigning a plane to each triangle.

        z -- an array of floats giving the known function values at each point
          in the triangulation.
        R
   s#   z must be the same shape as x and yN(	   R   R   R   R   R   R   R   R   R   (   R"   t   zt   default_value(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyt   linear_interpolator±   s    c         C  sh   t  j | d t  j ƒ} | j |  j k r9 t d ƒ ‚ n  |  j d k	 rX | |  j } n  t |  | | ƒ S(   sÜ   Get an object which can interpolate within the convex hull by
        the natural neighbors method.

        z -- an array of floats giving the known function values at each point
          in the triangulation.
        R
   s#   z must be the same shape as x and yN(	   R   R   R   R   R   R   R   R   R   (   R"   R6   R7   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyt   nn_interpolatorÀ   s    c         C  s\  | d  k r= |  j d |  j d |  j d |  j d f } n  t j | t j ƒ \ } } } } t | t j j |  j ƒ ƒ } t | t j j |  j ƒ ƒ } t	 | t j
 j |  j ƒ ƒ } t	 | t j
 j |  j ƒ ƒ } t	 | | d | | d ƒ } | | d } | | d }	 t j | d | | | d | g |	 |	 d | |	 d | g g ƒ \ }
 } t j |  j |
 f ƒ } t j |  j | f ƒ } |  j | | ƒ } t j t |  j ƒ d f t j ƒ } |  j | d  d  … d f <|  j | d  d  … d f <d d l m } | | | ƒ \ } } } } t j | |
 | d | | d | d f ƒ } | | f S(   Ni    i   g       @i   i   iÿÿÿÿ(   t   lstsq(   R   R   R   R   R   R   t   mint   minimumt   reducet   maxt   maximumR4   R'   t	   __class__t   onesR   t
   numpy.dualR:   (   R"   R6   t   bboxt   minxt   maxxt   minyt   maxyt   Mt   midxt   midyt   xpt   ypt   x1t   y1t   newtrit   xy1R:   t   ct   rest   rankt   st   zp(    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyt   prep_extrapolatorÏ   s,    1!"+$1c         C  s(   |  j  | | ƒ \ } } | j | | ƒ S(   N(   RV   R9   (   R"   R6   RC   R7   RO   RU   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyt   nn_extrapolatorë   s    c         C  s(   |  j  | | ƒ \ } } | j | | ƒ S(   N(   RV   R8   (   R"   R6   RC   R7   RO   RU   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyt   linear_extrapolatorï   s    c         C  sh   i  } x[ |  j  D]P \ } } | j | t ƒ  ƒ } | j | ƒ | j | t ƒ  ƒ } | j | ƒ q W| S(   sµ   Return a graph of node_id's pointing to node_id's.

        The arcs of the graph correspond to the edges in the triangulation.

        {node_id: set([node_id, ...]), ...}
        (   R   t
   setdefaultt   sett   add(   R"   t   gt   it   jRT   (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyt
   node_graphó   s    N(   R   R   R	   R$   R   R    R5   R   t   nanR8   R9   R   RV   RW   RX   R_   (    (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyR      s   A	$			
(   t
   __future__R    R   RZ   t	   NameErrort   setst   Sett	   itertoolsR   t   numpyR   t   matplotlib._delaunayR   t   interpolateR   R   t   __all__t   RuntimeWarningR   t   objectR   (    (    (    sY   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/delaunay/triangulate.pyt   <module>   s   