Debian Codegraph @

lib/circogen/circularinit.c

raw
1 /**
2 * @file
3 * @brief API circogen/circo.h: @ref circo_init_graph, @ref circoLayout, @ref circo_layout, @ref circo_cleanup
4 */
5
6 /*************************************************************************
7 * Copyright (c) 2011 AT&T Intellectual Property
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * https://www.eclipse.org/legal/epl-v10.html
12 *
13 * Contributors: Details at https://graphviz.org
14 *************************************************************************/
15
16 #include "config.h"
17
18 #include <circogen/circular.h>
19 #include <neatogen/adjust.h>
20 #include <pack/pack.h>
21 #include <neatogen/neatoprocs.h>
22 #include <stddef.h>
23 #include <stdbool.h>
24 #include <string.h>
25 #include <util/alloc.h>
26
27 static void circular_init_edge(edge_t * e)
28 {
29 agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //node custom data
30 common_init_edge(e);
31
32 ED_factor(e) = late_double(e, E_weight, 1.0, 0.0);
33 }
34
35
36 static void circular_init_node_edge(graph_t * g)
37 {
38 node_t *n;
39 edge_t *e;
40 int i = 0;
41 ndata* alg = gv_calloc(agnnodes(g), sizeof(ndata));
42
43 GD_neato_nlist(g) = gv_calloc(agnnodes(g) + 1, sizeof(node_t*));
44 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
45 neato_init_node(n);
46 ND_alg(n) = alg + i;
47 GD_neato_nlist(g)[i++] = n;
48 }
49 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
50 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
51 circular_init_edge(e);
52 }
53 }
54 }
55
56
57 void circo_init_graph(graph_t * g)
58 {
59 setEdgeType (g, EDGETYPE_LINE);
60 /* GD_ndim(g) = late_int(g,agfindattr(g,"dim"),2,2); */
61 Ndim = GD_ndim(agroot(g)) = 2; /* The algorithm only makes sense in 2D */
62 circular_init_node_edge(g);
63 }
64
65 /* Make a node in the derived graph, with the given name.
66 * orig points to what it represents, either a real node or
67 * a cluster. Copy size info from original node; needed for
68 * adjustNodes and packSubgraphs.
69 */
70 static node_t *makeDerivedNode(graph_t * dg, char *name, int isNode,
71 void *orig)
72 {
73 node_t *n = agnode(dg, name,1);
74 agbindrec(n, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true); //node custom data
75 ND_alg(n) = gv_alloc(sizeof(cdata));
76 if (isNode) {
77 ND_pos(n) = gv_calloc(Ndim, sizeof(double));
78 ND_lw(n) = ND_lw(orig);
79 ND_rw(n) = ND_rw(orig);
80 ND_ht(n) = ND_ht(orig);
81 ORIGN(n) = orig;
82 } else
83 ORIGG(n) = orig;
84 return n;
85 }
86
87 /* Construct a strict, undirected graph with no loops from g.
88 * Construct the connected components with the provision that all
89 * nodes in a block subgraph are considered connected.
90 * Return array of components with number of components in cnt.
91 * Each component has its blocks as subgraphs.
92 * FIX: Check that blocks are disjoint.
93 */
94 static Agraph_t **circomps(Agraph_t *g, size_t *cnt) {
95 Agraph_t **ccs;
96 Agraph_t *dg;
97 Agnode_t *n, *v, *dt, *dh;
98 Agedge_t *e;
99 Agraph_t *sg;
100 Agedge_t *ep;
101 Agnode_t *p;
102
103 dg = agopen("derived", Agstrictundirected,NULL);
104 agbindrec (dg, "info", sizeof(Agraphinfo_t), true);
105 GD_alg(g) = dg; /* store derived graph for closing later */
106
107 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
108 if (DNODE(v))
109 continue;
110 n = makeDerivedNode(dg, agnameof(v), 1, v);
111 DNODE(v) = n;
112 }
113
114 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
115 for (e = agfstout(g, v); e; e = agnxtout(g, e)) {
116 dt = DNODE(agtail(e));
117 dh = DNODE(aghead(e));
118 if (dt != dh) {
119 agbindrec(agedge(dg, dt, dh, NULL, 1), "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //node custom data
120 }
121 }
122 }
123
124 size_t c_cnt;
125 ccs = ccomps(dg, &c_cnt, 0);
126
127 /* replace block nodes with block contents */
128 for (size_t i = 0; i < c_cnt; i++) {
129 sg = ccs[i];
130
131 /* add edges: since sg is a union of components, all edges
132 * of any node should be added, except loops.
133 */
134 for (n = agfstnode(sg); n; n = agnxtnode(sg, n)) {
135 p = ORIGN(n);
136 for (e = agfstout(g, p); e; e = agnxtout(g, e)) {
137 /* n = DNODE(agtail(e)); by construction since agtail(e) == p */
138 dh = DNODE(aghead(e));
139 if (n != dh) {
140 ep = agedge(dg, n, dh, NULL, 1);
141 agbindrec(ep, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //node custom data
142 agsubedge(sg,ep,1);
143 }
144 }
145 }
146 }
147
148 /* Finally, add edge data to edges */
149 for (n = agfstnode(dg); n; n = agnxtnode(dg, n)) {
150 for (e = agfstout(dg, n); e; e = agnxtout(dg, e)) {
151 ED_alg(e) = gv_alloc(sizeof(edata));
152 }
153 }
154
155 *cnt = c_cnt;
156 return ccs;
157 }
158
159 static void closeDerivedGraph(graph_t * g)
160 {
161 node_t *n;
162 edge_t *e;
163
164 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
165 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
166 free(ED_alg(e));
167 }
168 free(ND_alg(n));
169 free(ND_pos(n));
170 }
171 agclose(g);
172 }
173
174 /* Copy position of nodes in given subgraph of derived graph
175 * to corresponding node in original graph.
176 * FIX: consider assigning from n directly to ORIG(n).
177 */
178 static void copyPosns(graph_t * g)
179 {
180 node_t *n;
181 node_t *v;
182
183 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
184 v = ORIGN(n);
185 ND_pos(v)[0] = ND_pos(n)[0];
186 ND_pos(v)[1] = ND_pos(n)[1];
187 }
188 }
189
190 void circoLayout(Agraph_t * g)
191 {
192 Agraph_t **ccs;
193 Agraph_t *sg;
194
195 if (agnnodes(g)) {
196 size_t ncc;
197 ccs = circomps(g, &ncc);
198
199 int blockCount = 0;
200 if (ncc == 1) {
201 circularLayout(ccs[0], g, &blockCount);
202 copyPosns(ccs[0]);
203 adjustNodes(g);
204 } else {
205 Agraph_t *dg = ccs[0]->root;
206 pack_info pinfo;
207 getPackInfo(g, l_node, CL_OFFSET, &pinfo);
208
209 for (size_t i = 0; i < ncc; i++) {
210 sg = ccs[i];
211 circularLayout(sg, g, &blockCount);
212 adjustNodes(sg);
213 }
214 /* FIX: splines have not been calculated for dg
215 * To use, either do splines in dg and copy to g, or
216 * construct components of g from ccs and use that in packing.
217 */
218 packSubgraphs(ncc, ccs, dg, &pinfo);
219 for (size_t i = 0; i < ncc; i++)
220 copyPosns(ccs[i]);
221 }
222 free(ccs);
223 }
224 }
225
226 void circo_layout(Agraph_t * g)
227 {
228 if (agnnodes(g) == 0) return;
229 circo_init_graph(g);
230 circoLayout(g);
231 /* Release ND_alg as we need to reuse it during edge routing */
232 free(ND_alg(agfstnode(g)));
233 spline_edges(g);
234 dotneato_postprocess(g);
235 }
236
237 /// ND_alg is freed in circo_layout
238 void circo_cleanup(graph_t * g)
239 {
240 node_t *n;
241 edge_t *e;
242
243 n = agfstnode(g);
244 if (n == NULL)
245 return; /* g is empty */
246
247 closeDerivedGraph(GD_alg(g)); // delete derived graph
248
249 /* free (ND_alg(n)); */
250 for (; n; n = agnxtnode(g, n)) {
251 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
252 gv_cleanup_edge(e);
253 }
254 gv_cleanup_node(n);
255 }
256 free(GD_neato_nlist(g));
257 }
258
259 /**
260 * @dir lib/circogen
261 * @brief [circular layout](https://en.wikipedia.org/wiki/Circular_layout) engine, API circogen/circo.h
262 * @ingroup engines
263 *
264 * Biconnected components are put on circles.
265 * block-cutnode tree is done recursively, with children placed
266 * about parent block.
267 *
268 * [Circo layout user manual](https://graphviz.org/docs/layouts/circo/)
269 *
270 * Based on:
271 * - Six and Tollis, "A Framework for Circular Drawings of
272 * Networks", GD '99, LNCS 1731, pp. 107-116;
273 * - Six and Tollis, "Circular Drawings of Biconnected Graphs",
274 * Proc. ALENEX '99, pp 57-73.
275 * - Kaufmann and Wiese, "Maintaining the Mental Map for Circular
276 * Drawings", GD '02, LNCS 2528, pp. 12-22.
277 *
278 * Other @ref engines
279 */