/usr/share/pyshared/igraph/drawing/edge.py is in python-igraph 0.6.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | """
Drawers for various edge styles in graph plots.
"""
__all__ = ["AbstractEdgeDrawer", "AlphaVaryingEdgeDrawer",
"ArrowEdgeDrawer", "DarkToLightEdgeDrawer",
"LightToDarkEdgeDrawer", "TaperedEdgeDrawer"]
__license__ = "GPL"
try:
from cairo import LinearGradient
except ImportError:
# No cairo support is installed. Don't worry, there will
# be a fake Cairo module in igraph.drawing
pass
from igraph.drawing.colors import clamp
from math import atan2, cos, pi, sin
class AbstractEdgeDrawer(object):
"""Abstract edge drawer object from which all concrete edge drawer
implementations are derived."""
def __init__(self, context):
"""Constructs the edge drawer.
@param context: a Cairo context on which the edges will be drawn.
"""
self.context = context
@staticmethod
def _curvature_to_float(value):
"""Converts values given to the 'curved' edge style argument
in plotting calls to floating point values."""
if value is None or value is False:
return 0.0
if value is True:
return 0.5
return float(value)
def draw_directed_edge(self, edge, src_vertex, dest_vertex):
"""Draws a directed edge.
@param edge: the edge to be drawn. Visual properties of the edge
are defined by the attributes of this object.
@param src_vertex: the source vertex. Visual properties are given
again as attributes.
@param dest_vertex: the target vertex. Visual properties are given
again as attributes.
"""
raise NotImplementedError()
def draw_loop_edge(self, edge, vertex):
"""Draws a loop edge.
The default implementation draws a small circle.
@param edge: the edge to be drawn. Visual properties of the edge
are defined by the attributes of this object.
@param vertex: the vertex to which the edge is attached. Visual
properties are given again as attributes.
"""
ctx = self.context
ctx.set_source_rgba(*edge.color)
ctx.set_line_width(edge.width)
radius = vertex.size * 1.5
center_x = vertex.position[0] + cos(pi/4) * radius / 2.
center_y = vertex.position[1] - sin(pi/4) * radius / 2.
ctx.arc(center_x, center_y, radius/2., 0, pi * 2)
ctx.stroke()
def draw_undirected_edge(self, edge, src_vertex, dest_vertex):
"""Draws an undirected edge.
The default implementation of this method draws undirected edges
as straight lines. Loop edges are drawn as small circles.
@param edge: the edge to be drawn. Visual properties of the edge
are defined by the attributes of this object.
@param src_vertex: the source vertex. Visual properties are given
again as attributes.
@param dest_vertex: the target vertex. Visual properties are given
again as attributes.
"""
if src_vertex == dest_vertex: # TODO
return self.draw_loop_edge(edge, src_vertex)
ctx = self.context
ctx.set_source_rgba(*edge.color)
ctx.set_line_width(edge.width)
ctx.move_to(*src_vertex.position)
if edge.curved:
(x1, y1), (x2, y2) = src_vertex.position, dest_vertex.position
aux1 = (2*x1+x2) / 3.0 - edge.curved * 0.5 * (y2-y1), \
(2*y1+y2) / 3.0 + edge.curved * 0.5 * (x2-x1)
aux2 = (x1+2*x2) / 3.0 - edge.curved * 0.5 * (y2-y1), \
(y1+2*y2) / 3.0 + edge.curved * 0.5 * (x2-x1)
ctx.curve_to(aux1[0], aux1[1], aux2[0], aux2[1], *dest_vertex.position)
else:
ctx.line_to(*dest_vertex.position)
ctx.stroke()
class ArrowEdgeDrawer(AbstractEdgeDrawer):
"""Edge drawer implementation that draws undirected edges as
straight lines and directed edges as arrows.
"""
def draw_directed_edge(self, edge, src_vertex, dest_vertex):
if src_vertex == dest_vertex: # TODO
return self.draw_loop_edge(edge, src_vertex)
ctx = self.context
(x1, y1), (x2, y2) = src_vertex.position, dest_vertex.position
# Draw the edge
ctx.set_source_rgba(*edge.color)
ctx.set_line_width(edge.width)
ctx.move_to(x1, y1)
if edge.curved:
# Calculate the curve
aux1 = (2*x1+x2) / 3.0 - edge.curved * 0.5 * (y2-y1), \
(2*y1+y2) / 3.0 + edge.curved * 0.5 * (x2-x1)
aux2 = (x1+2*x2) / 3.0 - edge.curved * 0.5 * (y2-y1), \
(y1+2*y2) / 3.0 + edge.curved * 0.5 * (x2-x1)
ctx.curve_to(aux1[0], aux1[1], aux2[0], aux2[1], x2, y2)
x1, y1 = aux2
else:
# Draw the line
ctx.line_to(x2, y2)
# Determine where the edge intersects the circumference of the
# vertex shape.
x2, y2 = dest_vertex.shape.intersection_point(
x2, y2, x1, y1, dest_vertex.size)
ctx.stroke()
# Draw the arrowhead
angle = atan2(y2-y1, x2-x1)
arrow_size = 15. * edge.arrow_size
arrow_width = 10. / edge.arrow_width
aux_points = [
(x2 - arrow_size * cos(angle - pi/arrow_width),
y2 - arrow_size * sin(angle - pi/arrow_width)),
(x2 - arrow_size * cos(angle + pi/arrow_width),
y2 - arrow_size * sin(angle + pi/arrow_width)),
]
ctx.move_to(x2, y2)
ctx.line_to(*aux_points[0])
ctx.line_to(*aux_points[1])
ctx.line_to(x2, y2)
ctx.fill()
class TaperedEdgeDrawer(AbstractEdgeDrawer):
"""Edge drawer implementation that draws undirected edges as
straight lines and directed edges as tapered lines that are
wider at the source and narrow at the destination.
"""
def draw_directed_edge(self, edge, src_vertex, dest_vertex):
if src_vertex == dest_vertex: # TODO
return self.draw_loop_edge(edge, src_vertex)
# Determine where the edge intersects the circumference of the
# destination vertex.
src_pos, dest_pos = src_vertex.position, dest_vertex.position
dest_pos = dest_vertex.shape.intersection_point(
dest_pos[0], dest_pos[1], src_pos[0], src_pos[1],
dest_vertex.size
)
ctx = self.context
# Draw the edge
ctx.set_source_rgba(*edge.color)
ctx.set_line_width(edge.width)
angle = atan2(dest_pos[1]-src_pos[1], dest_pos[0]-src_pos[0])
arrow_size = src_vertex.size / 4.
aux_points = [
(src_pos[0] + arrow_size * cos(angle + pi/2),
src_pos[1] + arrow_size * sin(angle + pi/2)),
(src_pos[0] + arrow_size * cos(angle - pi/2),
src_pos[1] + arrow_size * sin(angle - pi/2))
]
ctx.move_to(*dest_pos)
ctx.line_to(*aux_points[0])
ctx.line_to(*aux_points[1])
ctx.line_to(*dest_pos)
ctx.fill()
class AlphaVaryingEdgeDrawer(AbstractEdgeDrawer):
"""Edge drawer implementation that draws undirected edges as
straight lines and directed edges by varying the alpha value
of the specified edge color between the source and the destination.
"""
def __init__(self, context, alpha_at_src, alpha_at_dest):
super(AlphaVaryingEdgeDrawer, self).__init__(context)
self.alpha_at_src = (clamp(float(alpha_at_src), 0., 1.), )
self.alpha_at_dest = (clamp(float(alpha_at_dest), 0., 1.), )
def draw_directed_edge(self, edge, src_vertex, dest_vertex):
if src_vertex == dest_vertex: # TODO
return self.draw_loop_edge(edge, src_vertex)
src_pos, dest_pos = src_vertex.position, dest_vertex.position
ctx = self.context
# Set up the gradient
lg = LinearGradient(src_pos[0], src_pos[1], dest_pos[0], dest_pos[1])
edge_color = edge.color[:3] + self.alpha_at_src
edge_color_end = edge_color[:3] + self.alpha_at_dest
lg.add_color_stop_rgba(0, *edge_color)
lg.add_color_stop_rgba(1, *edge_color_end)
# Draw the edge
ctx.set_source(lg)
ctx.set_line_width(edge.width)
ctx.move_to(*src_pos)
ctx.line_to(*dest_pos)
ctx.stroke()
class LightToDarkEdgeDrawer(AlphaVaryingEdgeDrawer):
"""Edge drawer implementation that draws undirected edges as
straight lines and directed edges by using an alpha value of
zero (total transparency) at the source and an alpha value of
one (full opacity) at the destination. The alpha value is
interpolated in-between.
"""
def __init__(self, context):
super(LightToDarkEdgeDrawer, self).__init__(context, 0.0, 1.0)
class DarkToLightEdgeDrawer(AlphaVaryingEdgeDrawer):
"""Edge drawer implementation that draws undirected edges as
straight lines and directed edges by using an alpha value of
one (full opacity) at the source and an alpha value of zero
(total transparency) at the destination. The alpha value is
interpolated in-between.
"""
def __init__(self, context):
super(DarkToLightEdgeDrawer, self).__init__(context, 1.0, 0.0)
|