U
    ÑtœdRE  ã                   @   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!ƒ�rêt"| #¡ ƒ}nFt |||fƒ�rdd„ t$|ƒD ƒ}n"t%|dƒ�r$t"|ƒ}nt"i  #¡ ƒ}t&||ƒ}|D �]’\‰}ˆ�r>|d k�r\�q>| |¡}t ˆt'ƒ�r€dd„ ˆD ƒ‰t%ˆdƒ�s”t
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�rðt,ˆˆƒ}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�rÒt6 7¡ d+ })t8|)ƒ})|d k�rðt,ˆˆƒ}%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�rètJ|-|,ƒ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"   