| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
|
| 12 |
/* [email protected], 9-Dec-1997 |
| 13 |
* merge edges with specified samehead/sametail onto the same port |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "config.h" |
| 17 |
|
| 18 |
#include <math.h> |
| 19 |
#include <dotgen/dot.h> |
| 20 |
#include <stdbool.h> |
| 21 |
#include <stddef.h> |
| 22 |
#include <util/list.h> |
| 23 |
#include <util/streq.h> |
| 24 |
|
| 25 |
typedef LIST(edge_t *) edge_list_t; |
| 26 |
|
| 27 |
typedef struct same_t { |
| 28 |
char *id; /* group id */ |
| 29 |
edge_list_t l; // edges in the group |
| 30 |
} same_t; |
| 31 |
|
| 32 |
static void free_same(same_t s) { |
| 33 |
LIST_FREE(&s.l); |
| 34 |
} |
| 35 |
|
| 36 |
typedef LIST(same_t) same_list_t; |
| 37 |
|
| 38 |
static void sameedge(same_list_t *same, edge_t *e, char *id); |
| 39 |
static void sameport(node_t *u, edge_list_t l); |
| 40 |
|
| 41 |
void dot_sameports(graph_t * g) |
| 42 |
/* merge edge ports in G */ |
| 43 |
{ |
| 44 |
node_t *n; |
| 45 |
edge_t *e; |
| 46 |
char *id; |
| 47 |
same_list_t samehead = {.dtor = free_same}; |
| 48 |
same_list_t sametail = {.dtor = free_same}; |
| 49 |
|
| 50 |
E_samehead = agattr_text(g, AGEDGE, "samehead", NULL); |
| 51 |
E_sametail = agattr_text(g, AGEDGE, "sametail", NULL); |
| 52 |
if (!(E_samehead || E_sametail)) |
| 53 |
return; |
| 54 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 55 |
for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) { |
| 56 |
if (aghead(e) == agtail(e)) continue; /* Don't support same* for loops */ |
| 57 |
if (aghead(e) == n && E_samehead && |
| 58 |
(id = agxget(e, E_samehead))[0]) |
| 59 |
sameedge(&samehead, e, id); |
| 60 |
else if (agtail(e) == n && E_sametail && |
| 61 |
(id = agxget(e, E_sametail))[0]) |
| 62 |
sameedge(&sametail, e, id); |
| 63 |
} |
| 64 |
for (size_t i = 0; i < LIST_SIZE(&samehead); i++) { |
| 65 |
if (LIST_SIZE(&LIST_AT(&samehead, i)->l) > 1) |
| 66 |
sameport(n, LIST_GET(&samehead, i).l); |
| 67 |
} |
| 68 |
LIST_CLEAR(&samehead); |
| 69 |
for (size_t i = 0; i < LIST_SIZE(&sametail); i++) { |
| 70 |
if (LIST_SIZE(&LIST_AT(&sametail, i)->l) > 1) |
| 71 |
sameport(n, LIST_GET(&sametail, i).l); |
| 72 |
} |
| 73 |
LIST_CLEAR(&sametail); |
| 74 |
} |
| 75 |
|
| 76 |
LIST_FREE(&samehead); |
| 77 |
LIST_FREE(&sametail); |
| 78 |
} |
| 79 |
|
| 80 |
/// register \p e in the \p same structure of the originating node under \p id |
| 81 |
static void sameedge(same_list_t *same, edge_t *e, char *id) { |
| 82 |
for (size_t i = 0; i < LIST_SIZE(same); i++) |
| 83 |
if (streq(LIST_GET(same, i).id, id)) { |
| 84 |
LIST_APPEND(&LIST_AT(same, i)->l, e); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
same_t to_append = {.id = id}; |
| 89 |
LIST_APPEND(&to_append.l, e); |
| 90 |
LIST_APPEND(same, to_append); |
| 91 |
} |
| 92 |
|
| 93 |
static void sameport(node_t *u, edge_list_t l) |
| 94 |
/* make all edges in L share the same port on U. The port is placed on the |
| 95 |
node boundary and the average angle between the edges. FIXME: this assumes |
| 96 |
naively that the edges are straight lines, which is wrong if they are long. |
| 97 |
In that case something like concentration could be done. |
| 98 |
|
| 99 |
An arr_port is also computed that's ARR_LEN away from the node boundary. |
| 100 |
It's used for edges that don't themselves have an arrow. |
| 101 |
*/ |
| 102 |
{ |
| 103 |
node_t *v; |
| 104 |
edge_t *f; |
| 105 |
double x = 0, y = 0, x1, y1, x2, y2, r; |
| 106 |
|
| 107 |
/* Compute the direction vector (x,y) of the average direction. We compute |
| 108 |
with direction vectors instead of angles because else we have to first |
| 109 |
bring the angles within PI of each other. av(a,b)!=av(a,b+2*PI) */ |
| 110 |
for (size_t i = 0; i < LIST_SIZE(&l); i++) { |
| 111 |
edge_t *e = LIST_GET(&l, i); |
| 112 |
if (aghead(e) == u) |
| 113 |
v = agtail(e); |
| 114 |
else |
| 115 |
v = aghead(e); |
| 116 |
x1 = ND_coord(v).x - ND_coord(u).x; |
| 117 |
y1 = ND_coord(v).y - ND_coord(u).y; |
| 118 |
r = hypot(x1, y1); |
| 119 |
x += x1 / r; |
| 120 |
y += y1 / r; |
| 121 |
} |
| 122 |
r = hypot(x, y); |
| 123 |
x /= r; |
| 124 |
y /= r; |
| 125 |
|
| 126 |
/* (x1,y1),(x2,y2) is a segment that must cross the node boundary */ |
| 127 |
x1 = ND_coord(u).x; |
| 128 |
y1 = ND_coord(u).y; /* center of node */ |
| 129 |
r = fmax(ND_lw(u) + ND_rw(u), ND_ht(u) + GD_ranksep(agraphof(u))); // far away |
| 130 |
x2 = x * r + ND_coord(u).x; |
| 131 |
y2 = y * r + ND_coord(u).y; |
| 132 |
{ /* now move (x1,y1) to the node boundary */ |
| 133 |
pointf curve[4]; /* Bézier control points for a straight line */ |
| 134 |
curve[0].x = x1; |
| 135 |
curve[0].y = y1; |
| 136 |
curve[1].x = (2 * x1 + x2) / 3; |
| 137 |
curve[1].y = (2 * y1 + y2) / 3; |
| 138 |
curve[2].x = (2 * x2 + x1) / 3; |
| 139 |
curve[2].y = (2 * y2 + y1) / 3; |
| 140 |
curve[3].x = x2; |
| 141 |
curve[3].y = y2; |
| 142 |
|
| 143 |
shape_clip(u, curve); |
| 144 |
x1 = curve[0].x - ND_coord(u).x; |
| 145 |
y1 = curve[0].y - ND_coord(u).y; |
| 146 |
} |
| 147 |
|
| 148 |
/* compute PORT on the boundary */ |
| 149 |
port prt = {.p = {.x = round(x1), .y = round(y1)}}; |
| 150 |
prt.bp = 0; |
| 151 |
prt.order = |
| 152 |
(MC_SCALE * (ND_lw(u) + prt.p.x)) / (ND_lw(u) + ND_rw(u)); |
| 153 |
prt.constrained = false; |
| 154 |
prt.defined = true; |
| 155 |
prt.clip = false; |
| 156 |
prt.dyna = false; |
| 157 |
prt.theta = 0; |
| 158 |
prt.side = 0; |
| 159 |
prt.name = NULL; |
| 160 |
|
| 161 |
/* assign one of the ports to every edge */ |
| 162 |
for (size_t i = 0; i < LIST_SIZE(&l); i++) { |
| 163 |
edge_t *e = LIST_GET(&l, i); |
| 164 |
for (; e; e = ED_to_virt(e)) { /* assign to all virt edges of e */ |
| 165 |
for (f = e; f; |
| 166 |
f = ED_edge_type(f) == VIRTUAL && |
| 167 |
ND_node_type(aghead(f)) == VIRTUAL && |
| 168 |
ND_out(aghead(f)).size == 1 ? |
| 169 |
ND_out(aghead(f)).list[0] : NULL) { |
| 170 |
if (aghead(f) == u) |
| 171 |
ED_head_port(f) = prt; |
| 172 |
if (agtail(f) == u) |
| 173 |
ED_tail_port(f) = prt; |
| 174 |
} |
| 175 |
for (f = e; f; |
| 176 |
f = ED_edge_type(f) == VIRTUAL && |
| 177 |
ND_node_type(agtail(f)) == VIRTUAL && |
| 178 |
ND_in(agtail(f)).size == 1 ? |
| 179 |
ND_in(agtail(f)).list[0] : NULL) { |
| 180 |
if (aghead(f) == u) |
| 181 |
ED_head_port(f) = prt; |
| 182 |
if (agtail(f) == u) |
| 183 |
ED_tail_port(f) = prt; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
ND_has_port(u) = true; /* kinda pointless, because mincross is already done */ |
| 189 |
} |