U
    tdRE                     @   s   d Z ddlmZmZmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ d	d
lmZ d	dlmZ d	dlmZ d	dlmZ d	dlmZ d	dlmZ dZ e Z!G dd deeZ"G dd de"Z#dS )a  
Drawing routines to draw graphs.

This module contains routines to draw graphs on:

  - Cairo surfaces (L{DefaultGraphDrawer})
  - Matplotlib axes (L{MatplotlibGraphDrawer})

It also contains routines to send an igraph graph directly to
(U{Cytoscape<http://www.cytoscape.org>}) using the
(U{CytoscapeRPC plugin<http://gforge.nbic.nl/projects/cytoscaperpc/>}), see
L{CytoscapeGraphDrawer}. L{CytoscapeGraphDrawer} can also fetch the current
network from Cytoscape and convert it to igraph format.
    )atan2cospisintan)warn)convex_hull	VertexSeq)Configuration)AbstractGraphDrawer)TextAlignment)Point   )AbstractCairoDrawer)CairoArrowEdgeDrawer)CairoPolygonDrawer)CairoTextDrawer)
find_cairo)CairoVertexDrawer)CairoGraphDrawerc                   @   s   e Zd ZdZdd ZdS )AbstractCairoGraphDrawerzBAbstract base class for graph drawers that draw on a Cairo canvas.c                 C   s   t | || t|  dS )a  Constructs the graph drawer and associates it to the given
        Cairo context and the given L{BoundingBox}.

        @param context: the context on which we will draw
        @param bbox:    the bounding box within which we will draw.
                        Can be anything accepted by the constructor
                        of L{BoundingBox} (i.e., a 2-tuple, a 4-tuple
                        or a L{BoundingBox} object).
        N)r   __init__r   )selfcontextbbox r   S/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/drawing/cairo/graph.pyr   -   s    
z!AbstractCairoGraphDrawer.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r   *   s   r   c                       s2   e Zd ZdZdeeef fdd	Zdd Z  Z	S )r   a  Class implementing the default visualisation of a graph.

    The default visualisation of a graph draws the nodes on a 2D plane
    according to a given L{Layout}, then draws a straight or curved
    edge between nodes connected by edges. This is the visualisation
    used when one invokes the L{plot()} function on a L{Graph} object.

    See L{Graph.__plot__()} for the keyword arguments understood by
    this drawer.Nc                    s$   t  || || _|| _|| _dS )ai  Constructs the graph drawer and associates it to the given
        Cairo context and the given L{BoundingBox}.

        @param context: the context on which we will draw
        @param bbox:    the bounding box within which we will draw.
                        Can be anything accepted by the constructor
                        of L{BoundingBox} (i.e., a 2-tuple, a 4-tuple
                        or a L{BoundingBox} object).
        @param vertex_drawer_factory: a factory method that returns an
                        L{AbstractCairoVertexDrawer} instance bound to a
                        given Cairo context. The factory method must take
                        four parameters: the Cairo context, the bounding
                        box of the drawing area, the palette to be
                        used for drawing colored vertices, and the graph layout.
                        The default vertex drawer is L{CairoVertexDrawer}.
        @param edge_drawer_factory: a factory method that returns an
                        L{AbstractCairoEdgeDrawer} instance bound to a
                        given Cairo context. The factory method must take
                        two parameters: the Cairo context and the palette
                        to be used for drawing colored edges. You can use
                        any of the actual L{AbstractEdgeDrawer}
                        implementations here to control the style of
                        edges drawn by igraph. The default edge drawer is
                        L{CairoArrowEdgeDrawer}.
        @param label_drawer_factory: a factory method that returns a
                        L{CairoTextDrawer} instance bound to a given Cairo
                        context. The method must take one parameter: the
                        Cairo context. The default label drawer is
                        L{CairoTextDrawer}.
        N)superr   vertex_drawer_factoryedge_drawer_factorylabel_drawer_factory)r   r   r   r"   r#   r$   	__class__r   r   r   I   s    &zCairoGraphDrawer.__init__c           9   	      s  |rt dt |dd }|d kr*td|| _| j}| }| j}|dd }| |dd ||dd}zt	|}W n t
k
r   |g}Y nX t|dk r|| q| j|}j||d	d
d |dd }	|	s|	d krTd|krTd| krT| dk rTddlm}	 |dd}
|
dkr:d}
t|
}
|	|d |
d|d< | |||}| ||}| |}||j|||j|| ||}| ||}d|kr|d }ddlm}m} t |t!rt"|# }nFt |||frdd t$|D }n"t%|dr$t"|}nt"i # }t&||}|D ]\}r>|d kr\q>||}t t'rdd D t%dst
dfddt(fddD D }dt)fdd|D  fd d|D }t|d!krZt*|d  t*|d"   }}|| +  }t*|d"  |d }|| || || || || || g}n*t*d#d t,| D    fd$d|D }|-|d |d" |d! |d% d&  |j.|d' |/  |j-|  |0  q>|j|d krt,}nfd(d|D }|r|j1}n|j2}|D ]2\}} |j3\}!}"|! |"  }#}$|| |#|$ q|j|d krnt,}%nfd)d|D }%|j4}|5d" |%D ]\}&}'}(||'|&|( q|d*})|)d krt67 d+ })t8|)})|d krt,}%nfd,d|D }%|%D ]4\}&}(|&j9d kr"q|:|&j;t<j=t<j> |?|&j@ |j-|&jA  |&j9|_B|&jCr |D \}*}+},}-}*}*|,d- |-d-  },}-|&jC|&jE d- }.|&jFd!tG  }/|(d |.tH|/  }0|(d" |.tI|/  }1|,dkrtJ|-|,d!tG  }2ntGd- }2tG|2 }3|/d!tG |2 ks|/|2kr.|0|,7 }0|1tK|/|, 8 }1n|/|2krx|/|3krxz|0|-tK|/ 7 }0W n tLk
rl   Y nX |1|-8 }1nj|/|3kr|/|3d!|2  kr|0|,8 }0|1tK|/|, 7 }1n4z|0|-tK|/ 8 }0W n tLk
r   Y nX |1|-7 }1|jM|0|, |1|- |+ |)d. n<|(\}0}1|&jEd- }4|0|4 |1|4 |0|4 |1|4 f|_|j4|)d. q|j|d kr\t,}nfd/d|D }|D ]:\}} | j9d krqt|:| j;t<j=t<j> |?| j@ |j-| jA  | j9|_B|j3\}!}"|! |"  }#}$|N| |#|$\\}5}6\}7}8|D \}*}+},}-}*}*|,d- },|-d- }-|7tOjPkr2|5|,8 }5n|7tOjQkrF|5|,7 }5|8tOjRkrd|6|-|+d-  8 }6n|8tOjSkrx|6|-7 }6|7|_T|8|_U|5|, |6|- |5|, |6|- f|_|j4|)d. qtd S )0NzOPositional arguments to plot functions are ignored and will be deprecated soon.r   z bbox is required for Cairo plotspalettelayoutmarginr      keep_aspect_ratioF)r+   	autocurveZedge_curvedZcurvedi'  )r,   Tg      ?)	attributedefaultmark_groups)VertexClusteringVertexCoverc                 s   s   | ]\}}||fV  qd S Nr   ).0colorgroupr   r   r   	<genexpr>   s     z(CairoGraphDrawer.draw.<locals>.<genexpr>__iter__c                 S   s   g | ]
}|j qS r   )index)r3   vertexr   r   r   
<listcomp>   s     z)CairoGraphDrawer.draw.<locals>.<listcomp>z&group membership list must be iterablec                    s   g | ]} | qS r   r   r3   i)r5   r   r   r:      s     c                    s   g | ]} | qS r   r   r3   idxr(   r   r   r:      s     g      ?c                 3   s   | ]} | j V  qd S r2   )sizer=   )vertex_builderr   r   r6      s     c                    s   g | ]} | qS r   r   r=   r?   r   r   r:      s        r   c                 S   s    g | ]}t |tt| qS r   )sumfloatlen)r3   coordsr   r   r   r:      s     c                    s   g | ]}t |   qS r   )r   Ztowards)r3   point)centercorner_radiusr   r   r:      s      g      ?)rI   c                 3   s   | ]}|  | fV  qd S r2   r   r;   edge_builderesr   r   r6     s     c                 3   s$   | ]}| |  | fV  qd S r2   r   r;   )r(   rA   vsr   r   r6     s    Zwrap_labelszplotting.wrap_labelsc                 3   s   | ]}|  | fV  qd S r2   r   r;   )r(   rA   r   r   r6   0  s     g       @)wrapc                 3   s   | ]}|  | fV  qd S r2   r   r;   rK   r   r   r6     s     )Vr   DeprecationWarningpop
ValueErrorr   Zis_directedr   Zensure_layoutgetlist	TypeErrorrE   extendZcontractZfit_intoZedge_attributesZecountZigraphr,   rD   r"   r#   r$   ZVisualVertexBuilderrN   ZVisualEdgeBuilderrM   Z_determine_vertex_orderZ_determine_edge_orderZigraph.clusteringr0   r1   
isinstancedictiteritems	enumeratehasattrr   r	   r   maxr   
normalizedzipZset_source_rgbaZ	draw_pathZfill_preservestrokeZdraw_directed_edgeZdraw_undirected_edgetupledrawZset_line_widthr
   instanceboollabelZselect_font_faceZfontcairoZFONT_SLANT_NORMALZFONT_WEIGHT_NORMALZset_font_sizeZ
label_sizeZlabel_colortextZ
label_distZtext_extentsr@   Zlabel_angler   r   r   r   r   	ExceptionZdraw_atZget_label_positionr   RIGHTLEFTZBOTTOMZTOPhalignvalign)9r   graphargskwdsr   Zdirectedr   r'   r)   r,   r.   Zvertex_drawerZedge_drawerZlabel_drawerZvertex_orderZ
edge_orderr/   r0   r1   Z
group_iterZpolygon_drawerZcolor_idr4   ZhullpolygonabcnZedge_coord_iterZdrawer_methodedgeZvisual_edgesrcdestZ
src_vertexZdest_vertexZvertex_coord_iterr9   Zvisual_vertexrF   rO   _ZybwhZradiusalphaZcxcybetagammaZ	half_sizexyrk   rl   r   )rH   rI   rL   rM   r5   r(   rA   rN   r   rb   t   s   







$*$







  



  
  
zCairoGraphDrawer.draw)
r   r   r   r    r   r   r   r   rb   __classcell__r   r   r%   r   r   >   s   +r   N)$r    mathr   r   r   r   r   warningsr   Zigraph._igraphr   r	   Zigraph.configurationr
   Zigraph.drawing.baseclassesr   Zigraph.drawing.textr   Zigraph.drawing.utilsr   baser   ru   r   rp   r   rg   r   utilsr   r9   r   __all__rf   r   r   r   r   r   r   <module>   s"   