| 1 |
""" |
| 2 |
Drawers for various edge styles in graph plots. |
| 3 |
""" |
| 4 |
|
| 5 |
from math import atan2, cos, pi, sin |
| 6 |
|
| 7 |
from igraph.drawing.baseclasses import AbstractEdgeDrawer |
| 8 |
from igraph.drawing.colors import clamp |
| 9 |
from igraph.drawing.metamagic import AttributeCollectorBase |
| 10 |
from igraph.drawing.utils import ( |
| 11 |
euclidean_distance, |
| 12 |
get_bezier_control_points_for_curved_edge, |
| 13 |
intersect_bezier_curve_and_circle, |
| 14 |
) |
| 15 |
|
| 16 |
from .utils import find_cairo |
| 17 |
|
| 18 |
__all__ = ( |
| 19 |
"AbstractCairoEdgeDrawer", |
| 20 |
"AlphaVaryingEdgeDrawer", |
| 21 |
"CairoArrowEdgeDrawer", |
| 22 |
"DarkToLightEdgeDrawer", |
| 23 |
"LightToDarkEdgeDrawer", |
| 24 |
"TaperedEdgeDrawer", |
| 25 |
) |
| 26 |
|
| 27 |
cairo = find_cairo() |
| 28 |
|
| 29 |
|
| 30 |
class AbstractCairoEdgeDrawer(AbstractEdgeDrawer): |
| 31 |
"""Cairo-specific abstract edge drawer object.""" |
| 32 |
|
| 33 |
def __init__(self, context, palette): |
| 34 |
"""Constructs the edge drawer. |
| 35 |
|
| 36 |
@param context: a Cairo context on which the edges will be drawn. |
| 37 |
@param palette: the palette that can be used to map integer color |
| 38 |
indices to colors when drawing edges |
| 39 |
""" |
| 40 |
self.context = context |
| 41 |
self.palette = palette |
| 42 |
self.VisualEdgeBuilder = self._construct_visual_edge_builder() |
| 43 |
|
| 44 |
def _construct_visual_edge_builder(self): |
| 45 |
"""Construct the visual edge builder that will collect the visual |
| 46 |
attributes of an edge when it is being drawn.""" |
| 47 |
|
| 48 |
class VisualEdgeBuilder(AttributeCollectorBase): |
| 49 |
"""Builder that collects some visual properties of an edge for |
| 50 |
drawing""" |
| 51 |
|
| 52 |
_kwds_prefix = "edge_" |
| 53 |
arrow_size = 1.0 |
| 54 |
arrow_width = 1.0 |
| 55 |
color = ("#444", self.palette.get) |
| 56 |
curved = (0.0, self._curvature_to_float) |
| 57 |
label = None |
| 58 |
label_color = ("black", self.palette.get) |
| 59 |
label_size = 12.0 |
| 60 |
font = "sans-serif" |
| 61 |
width = 1.0 |
| 62 |
|
| 63 |
return VisualEdgeBuilder |
| 64 |
|
| 65 |
def draw_loop_edge(self, edge, vertex): |
| 66 |
"""Draws a loop edge. |
| 67 |
|
| 68 |
The default implementation draws a small circle. |
| 69 |
|
| 70 |
@param edge: the edge to be drawn. Visual properties of the edge |
| 71 |
are defined by the attributes of this object. |
| 72 |
@param vertex: the vertex to which the edge is attached. Visual |
| 73 |
properties are given again as attributes. |
| 74 |
""" |
| 75 |
ctx = self.context |
| 76 |
ctx.set_source_rgba(*edge.color) |
| 77 |
ctx.set_line_width(edge.width) |
| 78 |
radius = vertex.size * 1.5 |
| 79 |
center_x = vertex.position[0] + cos(pi / 4) * radius / 2.0 |
| 80 |
center_y = vertex.position[1] - sin(pi / 4) * radius / 2.0 |
| 81 |
ctx.arc(center_x, center_y, radius / 2.0, 0, pi * 2) |
| 82 |
ctx.stroke() |
| 83 |
|
| 84 |
def draw_undirected_edge(self, edge, src_vertex, dest_vertex): |
| 85 |
"""Draws an undirected edge. |
| 86 |
|
| 87 |
The default implementation of this method draws undirected edges |
| 88 |
as straight lines. Loop edges are drawn as small circles. |
| 89 |
|
| 90 |
@param edge: the edge to be drawn. Visual properties of the edge |
| 91 |
are defined by the attributes of this object. |
| 92 |
@param src_vertex: the source vertex. Visual properties are given |
| 93 |
again as attributes. |
| 94 |
@param dest_vertex: the target vertex. Visual properties are given |
| 95 |
again as attributes. |
| 96 |
""" |
| 97 |
if src_vertex == dest_vertex: # TODO |
| 98 |
return self.draw_loop_edge(edge, src_vertex) |
| 99 |
|
| 100 |
ctx = self.context |
| 101 |
ctx.set_source_rgba(*edge.color) |
| 102 |
ctx.set_line_width(edge.width) |
| 103 |
ctx.move_to(*src_vertex.position) |
| 104 |
|
| 105 |
if edge.curved: |
| 106 |
(x1, y1), (x2, y2) = src_vertex.position, dest_vertex.position |
| 107 |
aux1, aux2 = get_bezier_control_points_for_curved_edge( |
| 108 |
x1, |
| 109 |
y1, |
| 110 |
x2, |
| 111 |
y2, |
| 112 |
edge.curved, |
| 113 |
) |
| 114 |
ctx.curve_to(aux1[0], aux1[1], aux2[0], aux2[1], *dest_vertex.position) |
| 115 |
else: |
| 116 |
ctx.line_to(*dest_vertex.position) |
| 117 |
|
| 118 |
ctx.stroke() |
| 119 |
|
| 120 |
|
| 121 |
class CairoArrowEdgeDrawer(AbstractCairoEdgeDrawer): |
| 122 |
"""Edge drawer implementation that draws undirected edges as |
| 123 |
straight lines and directed edges as arrows. |
| 124 |
""" |
| 125 |
|
| 126 |
def draw_directed_edge(self, edge, src_vertex, dest_vertex): |
| 127 |
if src_vertex == dest_vertex: # TODO |
| 128 |
return self.draw_loop_edge(edge, src_vertex) |
| 129 |
|
| 130 |
ctx = self.context |
| 131 |
(x1, y1), (x2, y2) = src_vertex.position, dest_vertex.position |
| 132 |
(x_src, y_src), (x_dest, y_dest) = src_vertex.position, dest_vertex.position |
| 133 |
|
| 134 |
# Draw the edge |
| 135 |
ctx.set_source_rgba(*edge.color) |
| 136 |
ctx.set_line_width(edge.width) |
| 137 |
ctx.move_to(x1, y1) |
| 138 |
|
| 139 |
if edge.curved: |
| 140 |
# Calculate the curve |
| 141 |
aux1, aux2 = get_bezier_control_points_for_curved_edge( |
| 142 |
x1, y1, x2, y2, edge.curved |
| 143 |
) |
| 144 |
|
| 145 |
# Coordinates of the control points of the Bezier curve |
| 146 |
xc1, yc1 = aux1 |
| 147 |
xc2, yc2 = aux2 |
| 148 |
|
| 149 |
# Determine where the edge intersects the circumference of the |
| 150 |
# vertex shape: Tip of the arrow |
| 151 |
x2, y2 = intersect_bezier_curve_and_circle( |
| 152 |
x_src, y_src, xc1, yc1, xc2, yc2, x_dest, y_dest, dest_vertex.size / 2.0 |
| 153 |
) |
| 154 |
|
| 155 |
# Calculate the arrow head coordinates |
| 156 |
angle = atan2(y_dest - y2, x_dest - x2) # navid |
| 157 |
arrow_size = 15.0 * edge.arrow_size |
| 158 |
arrow_width = 10.0 / edge.arrow_width |
| 159 |
aux_points = [ |
| 160 |
( |
| 161 |
x2 - arrow_size * cos(angle - pi / arrow_width), |
| 162 |
y2 - arrow_size * sin(angle - pi / arrow_width), |
| 163 |
), |
| 164 |
( |
| 165 |
x2 - arrow_size * cos(angle + pi / arrow_width), |
| 166 |
y2 - arrow_size * sin(angle + pi / arrow_width), |
| 167 |
), |
| 168 |
] |
| 169 |
|
| 170 |
# Midpoint of the base of the arrow triangle |
| 171 |
x_arrow_mid, y_arrow_mid = ( |
| 172 |
(aux_points[0][0] + aux_points[1][0]) / 2.0, |
| 173 |
(aux_points[0][1] + aux_points[1][1]) / 2.0, |
| 174 |
) |
| 175 |
|
| 176 |
# Vector representing the base of the arrow triangle |
| 177 |
x_arrow_base_vec, y_arrow_base_vec = ( |
| 178 |
(aux_points[0][0] - aux_points[1][0]), |
| 179 |
(aux_points[0][1] - aux_points[1][1]), |
| 180 |
) |
| 181 |
|
| 182 |
# Recalculate the curve such that it lands on the base of the arrow triangle |
| 183 |
aux1, aux2 = get_bezier_control_points_for_curved_edge( |
| 184 |
x1, y1, x_arrow_mid, y_arrow_mid, edge.curved |
| 185 |
) |
| 186 |
|
| 187 |
# Offset the second control point (aux2) such that it falls precisely |
| 188 |
# on the normal to the arrow base vector. Strictly speaking, |
| 189 |
# offset_length is the offset length divided by the length of the |
| 190 |
# arrow base vector. |
| 191 |
offset_length = (x_arrow_mid - aux2[0]) * x_arrow_base_vec + ( |
| 192 |
y_arrow_mid - aux2[1] |
| 193 |
) * y_arrow_base_vec |
| 194 |
offset_length /= ( |
| 195 |
euclidean_distance(0, 0, x_arrow_base_vec, y_arrow_base_vec) ** 2 |
| 196 |
) |
| 197 |
|
| 198 |
aux2 = ( |
| 199 |
aux2[0] + x_arrow_base_vec * offset_length, |
| 200 |
aux2[1] + y_arrow_base_vec * offset_length, |
| 201 |
) |
| 202 |
|
| 203 |
# Draw the curve from the first vertex to the midpoint of the base |
| 204 |
# of the arrow head |
| 205 |
ctx.curve_to(aux1[0], aux1[1], aux2[0], aux2[1], x_arrow_mid, y_arrow_mid) |
| 206 |
else: |
| 207 |
# Determine where the edge intersects the circumference of the |
| 208 |
# vertex shape. |
| 209 |
x2, y2 = dest_vertex.shape.intersection_point( |
| 210 |
x2, y2, x1, y1, dest_vertex.size |
| 211 |
) |
| 212 |
|
| 213 |
# Draw the arrowhead |
| 214 |
angle = atan2(y_dest - y2, x_dest - x2) |
| 215 |
arrow_size = 15.0 * edge.arrow_size |
| 216 |
arrow_width = 10.0 / edge.arrow_width |
| 217 |
aux_points = [ |
| 218 |
( |
| 219 |
x2 - arrow_size * cos(angle - pi / arrow_width), |
| 220 |
y2 - arrow_size * sin(angle - pi / arrow_width), |
| 221 |
), |
| 222 |
( |
| 223 |
x2 - arrow_size * cos(angle + pi / arrow_width), |
| 224 |
y2 - arrow_size * sin(angle + pi / arrow_width), |
| 225 |
), |
| 226 |
] |
| 227 |
|
| 228 |
# Midpoint of the base of the arrow triangle |
| 229 |
x_arrow_mid, y_arrow_mid = ( |
| 230 |
(aux_points[0][0] + aux_points[1][0]) / 2.0, |
| 231 |
(aux_points[0][1] + aux_points[1][1]) / 2.0, |
| 232 |
) |
| 233 |
|
| 234 |
# Draw the line |
| 235 |
ctx.line_to(x_arrow_mid, y_arrow_mid) |
| 236 |
|
| 237 |
# Draw the edge |
| 238 |
ctx.stroke() |
| 239 |
|
| 240 |
# Draw the arrow head |
| 241 |
ctx.move_to(x2, y2) |
| 242 |
ctx.line_to(*aux_points[0]) |
| 243 |
ctx.line_to(*aux_points[1]) |
| 244 |
ctx.line_to(x2, y2) |
| 245 |
ctx.fill() |
| 246 |
|
| 247 |
|
| 248 |
class TaperedEdgeDrawer(AbstractCairoEdgeDrawer): |
| 249 |
"""Edge drawer implementation that draws undirected edges as |
| 250 |
straight lines and directed edges as tapered lines that are |
| 251 |
wider at the source and narrow at the destination. |
| 252 |
""" |
| 253 |
|
| 254 |
def draw_directed_edge(self, edge, src_vertex, dest_vertex): |
| 255 |
if src_vertex == dest_vertex: # TODO |
| 256 |
return self.draw_loop_edge(edge, src_vertex) |
| 257 |
|
| 258 |
# Determine where the edge intersects the circumference of the |
| 259 |
# destination vertex. |
| 260 |
src_pos, dest_pos = src_vertex.position, dest_vertex.position |
| 261 |
dest_pos = dest_vertex.shape.intersection_point( |
| 262 |
dest_pos[0], dest_pos[1], src_pos[0], src_pos[1], dest_vertex.size |
| 263 |
) |
| 264 |
|
| 265 |
ctx = self.context |
| 266 |
|
| 267 |
# Draw the edge |
| 268 |
ctx.set_source_rgba(*edge.color) |
| 269 |
ctx.set_line_width(edge.width) |
| 270 |
angle = atan2(dest_pos[1] - src_pos[1], dest_pos[0] - src_pos[0]) |
| 271 |
arrow_size = src_vertex.size / 4.0 |
| 272 |
aux_points = [ |
| 273 |
( |
| 274 |
src_pos[0] + arrow_size * cos(angle + pi / 2), |
| 275 |
src_pos[1] + arrow_size * sin(angle + pi / 2), |
| 276 |
), |
| 277 |
( |
| 278 |
src_pos[0] + arrow_size * cos(angle - pi / 2), |
| 279 |
src_pos[1] + arrow_size * sin(angle - pi / 2), |
| 280 |
), |
| 281 |
] |
| 282 |
ctx.move_to(*dest_pos) |
| 283 |
ctx.line_to(*aux_points[0]) |
| 284 |
ctx.line_to(*aux_points[1]) |
| 285 |
ctx.line_to(*dest_pos) |
| 286 |
ctx.fill() |
| 287 |
|
| 288 |
|
| 289 |
class AlphaVaryingEdgeDrawer(AbstractCairoEdgeDrawer): |
| 290 |
"""Edge drawer implementation that draws undirected edges as |
| 291 |
straight lines and directed edges by varying the alpha value |
| 292 |
of the specified edge color between the source and the destination. |
| 293 |
""" |
| 294 |
|
| 295 |
def __init__(self, context, palette, alpha_at_src, alpha_at_dest): |
| 296 |
super().__init__(context, palette) |
| 297 |
self.alpha_at_src = (clamp(float(alpha_at_src), 0.0, 1.0),) |
| 298 |
self.alpha_at_dest = (clamp(float(alpha_at_dest), 0.0, 1.0),) |
| 299 |
|
| 300 |
def draw_directed_edge(self, edge, src_vertex, dest_vertex): |
| 301 |
if src_vertex == dest_vertex: # TODO |
| 302 |
return self.draw_loop_edge(edge, src_vertex) |
| 303 |
|
| 304 |
src_pos, dest_pos = src_vertex.position, dest_vertex.position |
| 305 |
ctx = self.context |
| 306 |
|
| 307 |
# Set up the gradient |
| 308 |
lg = cairo.LinearGradient(src_pos[0], src_pos[1], dest_pos[0], dest_pos[1]) |
| 309 |
edge_color = edge.color[:3] + self.alpha_at_src |
| 310 |
edge_color_end = edge_color[:3] + self.alpha_at_dest |
| 311 |
lg.add_color_stop_rgba(0, *edge_color) |
| 312 |
lg.add_color_stop_rgba(1, *edge_color_end) |
| 313 |
|
| 314 |
# Draw the edge |
| 315 |
ctx.set_source(lg) |
| 316 |
ctx.set_line_width(edge.width) |
| 317 |
ctx.move_to(*src_pos) |
| 318 |
ctx.line_to(*dest_pos) |
| 319 |
ctx.stroke() |
| 320 |
|
| 321 |
|
| 322 |
class LightToDarkEdgeDrawer(AlphaVaryingEdgeDrawer): |
| 323 |
"""Edge drawer implementation that draws undirected edges as |
| 324 |
straight lines and directed edges by using an alpha value of |
| 325 |
zero (total transparency) at the source and an alpha value of |
| 326 |
one (full opacity) at the destination. The alpha value is |
| 327 |
interpolated in-between. |
| 328 |
""" |
| 329 |
|
| 330 |
def __init__(self, context, palette): |
| 331 |
super().__init__(context, palette, 0.0, 1.0) |
| 332 |
|
| 333 |
|
| 334 |
class DarkToLightEdgeDrawer(AlphaVaryingEdgeDrawer): |
| 335 |
"""Edge drawer implementation that draws undirected edges as |
| 336 |
straight lines and directed edges by using an alpha value of |
| 337 |
one (full opacity) at the source and an alpha value of zero |
| 338 |
(total transparency) at the destination. The alpha value is |
| 339 |
interpolated in-between. |
| 340 |
""" |
| 341 |
|
| 342 |
def __init__(self, context, palette): |
| 343 |
super().__init__(context, palette, 1.0, 0.0) |