Graphviz 16.1.3~dev.20261205.2032
Loading...
Searching...
No Matches
class2.c
Go to the documentation of this file.
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 v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11/* classify edges for mincross/nodepos/splines, using given ranks */
12
13#include "config.h"
14
15#include <dotgen/dot.h>
16#include <stdbool.h>
17#include <stdlib.h>
18#include <util/alloc.h>
19#include <util/gv_math.h>
20
21static node_t *label_vnode(graph_t *g, edge_t *orig) {
22 const pointf dimen = ED_label(orig)->dimen;
23 node_t *const v = virtual_node(g);
24 ND_label(v) = ED_label(orig);
25 ND_lw(v) = GD_nodesep(agroot(v));
26 if (!ED_label_ontop(orig)) {
27 if (GD_flip(agroot(g))) {
28 ND_ht(v) = dimen.x;
29 ND_rw(v) = dimen.y;
30 } else {
31 ND_ht(v) = dimen.y;
32 ND_rw(v) = dimen.x;
33 }
34 }
35 return v;
36}
37
38static void incr_width(graph_t *g, node_t *v) {
39 int width = GD_nodesep(g) / 2;
40 ND_lw(v) += width;
41 ND_rw(v) += width;
42}
43
45 node_t *const v = virtual_node(g);
46 incr_width(g, v);
47 return v;
48}
49
50static node_t *leader_of(node_t *v) {
51 graph_t *clust;
52 node_t *rv;
53
54 if (ND_ranktype(v) != CLUSTER) {
55 rv = UF_find(v);
56 } else {
57 clust = ND_clust(v);
58 rv = GD_rankleader(clust)[ND_rank(v)];
59 }
60 return rv;
61}
62
64static void make_chain(graph_t *g, node_t *from, node_t *to, edge_t *orig) {
65 int r, label_rank;
66 node_t *u, *v;
67 edge_t *e;
68
69 u = from;
70 if (ED_label(orig))
71 label_rank = (ND_rank(from) + ND_rank(to)) / 2;
72 else
73 label_rank = -1;
74 assert(ED_to_virt(orig) == NULL);
75 for (r = ND_rank(from) + 1; r <= ND_rank(to); r++) {
76 if (r < ND_rank(to)) {
77 if (r == label_rank)
78 v = label_vnode(g, orig);
79 else
80 v = plain_vnode(g);
81 ND_rank(v) = r;
82 } else
83 v = to;
84 e = virtual_edge(u, v, orig);
86 u = v;
87 }
88 assert(ED_to_virt(orig) != NULL);
89}
90
91static void interclrep(graph_t *g, edge_t *e) {
92 node_t *t, *h;
93 edge_t *ve;
94
95 t = leader_of(agtail(e));
96 h = leader_of(aghead(e));
97 if (ND_rank(t) > ND_rank(h)) {
98 SWAP(&t, &h);
99 }
100 if (ND_clust(t) != ND_clust(h)) {
101 if ((ve = find_fast_edge(t, h))) {
102 merge_chain(g, e, ve, true);
103 return;
104 }
105 if (ND_rank(t) == ND_rank(h))
106 return;
107 make_chain(g, t, h, e);
108
109 /* mark as cluster edge */
110 for (ve = ED_to_virt(e); ve && ND_rank(aghead(ve)) <= ND_rank(h);
111 ve = ND_out(aghead(ve)).list[0])
113 }
114 /* else ignore intra-cluster edges at this point */
115}
116
117static bool is_cluster_edge(edge_t *e) {
118 return ND_ranktype(agtail(e)) == CLUSTER || ND_ranktype(aghead(e)) == CLUSTER;
119}
120
121void merge_chain(graph_t *g, edge_t *e, edge_t *f, bool update_count) {
122 edge_t *rep;
123 int lastrank = MAX(ND_rank(agtail(e)), ND_rank(aghead(e)));
124
125 assert(ED_to_virt(e) == NULL);
126 ED_to_virt(e) = f;
127 rep = f;
128 do {
129 /* interclust multi-edges are not counted now */
130 if (update_count)
131 ED_count(rep) += ED_count(e);
132 ED_xpenalty(rep) += ED_xpenalty(e);
133 ED_weight(rep) += ED_weight(e);
134 if (ND_rank(aghead(rep)) == lastrank)
135 break;
136 incr_width(g, aghead(rep));
137 rep = ND_out(aghead(rep)).list[0];
138 } while (rep);
139}
140
141bool mergeable(edge_t *e, edge_t *f) {
142 return e && f && agtail(e) == agtail(f) && aghead(e) == aghead(f) &&
143 ED_label(e) == ED_label(f) && ports_eq(e, f);
144}
145
146void class2(graph_t *g) {
147 int c;
148 node_t *n, *t, *h;
149 edge_t *e, *prev, *opp;
150
151 GD_nlist(g) = NULL;
152
153 mark_clusters(g);
154 for (c = 1; c <= GD_n_cluster(g); c++)
155 build_skeleton(g, GD_clust(g)[c]);
156 for (n = agfstnode(g); n; n = agnxtnode(g, n))
157 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
158 if (ND_weight_class(aghead(e)) <= 2)
160 if (ND_weight_class(agtail(e)) <= 2)
162 }
163
164 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
165 if (ND_clust(n) == NULL && n == UF_find(n)) {
166 fast_node(g, n);
167 }
168 prev = NULL;
169 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
170
171 /* already processed */
172 if (ED_to_virt(e)) {
173 prev = e;
174 continue;
175 }
176
177 /* edges involving sub-clusters of g */
178 if (is_cluster_edge(e)) {
179 /* following is new cluster multi-edge code */
180 if (mergeable(prev, e)) {
181 if (ED_to_virt(prev)) {
182 merge_chain(g, e, ED_to_virt(prev), false);
183 other_edge(e);
184 } else if (ND_rank(agtail(e)) == ND_rank(aghead(e))) {
185 merge_oneway(e, prev);
186 other_edge(e);
187 }
188 /* else is an intra-cluster edge */
189 continue;
190 }
191 interclrep(g, e);
192 prev = e;
193 continue;
194 }
195 /* merge multi-edges */
196 if (prev && agtail(e) == agtail(prev) && aghead(e) == aghead(prev)) {
197 if (ND_rank(agtail(e)) == ND_rank(aghead(e))) {
198 merge_oneway(e, prev);
199 other_edge(e);
200 continue;
201 }
202 if (ED_label(e) == NULL && ED_label(prev) == NULL &&
203 ports_eq(e, prev)) {
204 if (Concentrate)
206 else {
207 merge_chain(g, e, ED_to_virt(prev), true);
208 other_edge(e);
209 }
210 continue;
211 }
212 /* parallel edges with different labels fall through here */
213 }
214
215 /* self edges */
216 if (agtail(e) == aghead(e)) {
217 other_edge(e);
218 prev = e;
219 continue;
220 }
221
222 t = UF_find(agtail(e));
223 h = UF_find(aghead(e));
224
225 /* non-leader leaf nodes */
226 if (agtail(e) != t || aghead(e) != h) {
227 /* FIX need to merge stuff */
228 continue;
229 }
230
231 /* flat edges */
232 if (ND_rank(agtail(e)) == ND_rank(aghead(e))) {
233 flat_edge(g, e);
234 prev = e;
235 continue;
236 }
237
238 /* forward edges */
239 if (ND_rank(aghead(e)) > ND_rank(agtail(e))) {
240 make_chain(g, agtail(e), aghead(e), e);
241 prev = e;
242 continue;
243 }
244
245 /* backward edges */
246 else {
247 /* avoid when opp==e in undirected graph */
248 for (opp = agfstout(g, aghead(e)); opp != NULL;
249 opp = agnxtout(g, opp)) {
250 if (aghead(opp) != agtail(e) || aghead(opp) == aghead(e) ||
251 ED_edge_type(opp) == IGNORED) {
252 continue;
253 }
254 /* shadows a forward edge */
255 if (ED_to_virt(opp) == NULL)
256 make_chain(g, agtail(opp), aghead(opp), opp);
257 if (ED_label(e) == NULL && ED_label(opp) == NULL &&
258 ports_eq(e, opp)) {
259 if (Concentrate) {
261 ED_conc_opp_flag(opp) = true;
262 } else { /* see above. this is getting out of hand */
263 other_edge(e);
264 merge_chain(g, e, ED_to_virt(opp), true);
265 }
266 break;
267 }
268 }
269 if (opp != NULL) {
270 continue;
271 }
272 make_chain(g, aghead(e), agtail(e), e);
273 prev = e;
274 }
275 }
276 }
277 /* since decompose() is not called on subgraphs */
278 if (g != dot_root(g)) {
279 free(GD_comp(g).list);
280 GD_comp(g).list = gv_alloc(sizeof(node_t *));
281 GD_comp(g).list[0] = GD_nlist(g);
282 }
283}
Memory allocation wrappers that exit on failure.
static void * gv_alloc(size_t size)
Definition alloc.h:47
#define MAX(a, b)
Definition arith.h:33
static void make_chain(graph_t *g, node_t *from, node_t *to, edge_t *orig)
create chain of dummy nodes for edge orig
Definition class2.c:64
void merge_chain(graph_t *g, edge_t *e, edge_t *f, bool update_count)
Definition class2.c:121
static void interclrep(graph_t *g, edge_t *e)
Definition class2.c:91
static node_t * leader_of(node_t *v)
Definition class2.c:50
static node_t * plain_vnode(graph_t *g)
Definition class2.c:44
static bool is_cluster_edge(edge_t *e)
Definition class2.c:117
void class2(graph_t *g)
Definition class2.c:146
bool mergeable(edge_t *e, edge_t *f)
Definition class2.c:141
static node_t * label_vnode(graph_t *g, edge_t *orig)
Definition class2.c:21
static void incr_width(graph_t *g, node_t *v)
Definition class2.c:38
node_t * UF_find(node_t *n)
Definition utils.c:106
#define IGNORED
Definition const.h:30
#define CLUSTER_EDGE
Definition const.h:29
#define CLUSTER
Definition const.h:40
Agraph_t * dot_root(void *p)
Definition dotinit.c:483
void other_edge(Agedge_t *)
Definition fastgr.c:109
Agedge_t * virtual_edge(Agnode_t *, Agnode_t *, Agedge_t *)
Definition fastgr.c:156
void virtual_weight(Agedge_t *)
Definition mincross.c:1704
void flat_edge(Agraph_t *, Agedge_t *)
Definition fastgr.c:198
void fast_node(Agraph_t *, Agnode_t *)
Definition fastgr.c:160
void merge_oneway(Agedge_t *, Agedge_t *)
Definition fastgr.c:223
int ports_eq(edge_t *, edge_t *)
Definition position.c:1026
Agedge_t * find_fast_edge(Agnode_t *, Agnode_t *)
Definition fastgr.c:43
Agnode_t * virtual_node(Agraph_t *)
Definition fastgr.c:183
bool Concentrate
Definition globals.h:62
void free(void *)
node NULL
Definition grammar.y:181
#define ED_conc_opp_flag(e)
Definition types.h:579
#define ED_label_ontop(e)
Definition types.h:591
#define ED_xpenalty(e)
Definition types.h:601
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:28
#define ED_count(e)
Definition types.h:580
#define agtail(e)
Definition cgraph.h:982
#define ED_edge_type(e)
Definition types.h:582
#define ED_weight(e)
Definition types.h:603
#define aghead(e)
Definition cgraph.h:983
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:42
#define ED_label(e)
Definition types.h:589
#define ED_to_virt(e)
Definition types.h:599
#define GD_clust(g)
Definition types.h:360
#define GD_nlist(g)
Definition types.h:393
#define GD_n_cluster(g)
Definition types.h:389
#define GD_comp(g)
Definition types.h:362
#define GD_nodesep(g)
Definition types.h:394
#define GD_flip(g)
Definition types.h:378
#define GD_rankleader(g)
Definition types.h:396
#define ND_rank(n)
Definition types.h:523
#define ND_ht(n)
Definition types.h:500
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:50
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:43
#define ND_clust(n)
Definition types.h:489
#define ND_label(n)
Definition types.h:502
#define ND_rw(n)
Definition types.h:525
#define ND_lw(n)
Definition types.h:506
#define ND_weight_class(n)
Definition types.h:535
#define ND_ranktype(n)
Definition types.h:524
#define ND_out(n)
Definition types.h:515
Agraph_t * agroot(void *obj)
Definition obj.c:170
Arithmetic helper functions.
#define SWAP(a, b)
Definition gv_math.h:133
$2 prev
Definition htmlparse.y:291
void build_skeleton(graph_t *g, graph_t *subg)
Definition cluster.c:329
void mark_clusters(graph_t *g)
Definition cluster.c:288
graph or subgraph
Definition cgraph.h:424
double x
Definition geom.h:29
double y
Definition geom.h:29