Debian Codegraph @

lib/dotgen/dotinit.c

raw
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 #include "config.h"
12
13 #include <assert.h>
14 #include <limits.h>
15 #include <time.h>
16 #include <dotgen/dot.h>
17 #include <pack/pack.h>
18 #include <dotgen/aspect.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <util/alloc.h>
24 #include <util/debug.h>
25 #include <util/itos.h>
26 #include <util/streq.h>
27
28 static void
29 dot_init_subg(graph_t * g, graph_t* droot)
30 {
31 graph_t* subg;
32
33 if ((g != agroot(g)))
34 agbindrec(g, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
35 if (g == droot)
36 GD_dotroot(agroot(g)) = droot;
37
38 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
39 dot_init_subg(subg, droot);
40 }
41 }
42
43
44 static void
45 dot_init_node(node_t * n)
46 {
47 agbindrec(n, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true); //graph custom data
48 common_init_node(n);
49 gv_nodesize(n, GD_flip(agraphof(n)));
50 alloc_elist(4, ND_in(n));
51 alloc_elist(4, ND_out(n));
52 alloc_elist(2, ND_flat_in(n));
53 alloc_elist(2, ND_flat_out(n));
54 alloc_elist(2, ND_other(n));
55 ND_UF_size(n) = 1;
56 }
57
58 static void
59 dot_init_edge(edge_t * e)
60 {
61 char *tailgroup, *headgroup;
62 agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //graph custom data
63 common_init_edge(e);
64
65 ED_weight(e) = late_int(e, E_weight, 1, 0);
66 tailgroup = late_string(agtail(e), N_group, "");
67 headgroup = late_string(aghead(e), N_group, "");
68 ED_count(e) = ED_xpenalty(e) = 1;
69 if (tailgroup[0] && (tailgroup == headgroup)) {
70 ED_xpenalty(e) = CL_CROSS;
71 ED_weight(e) *= 100;
72 }
73 if (nonconstraint_edge(e)) {
74 ED_xpenalty(e) = 0;
75 ED_weight(e) = 0;
76 }
77
78 {
79 int showboxes = late_int(e, E_showboxes, 0, 0);
80 if (showboxes > UCHAR_MAX) {
81 showboxes = UCHAR_MAX;
82 }
83 ED_showboxes(e) = (unsigned char)showboxes;
84 }
85 ED_minlen(e) = late_int(e, E_minlen, 1, 0);
86 }
87
88 void
89 dot_init_node_edge(graph_t * g)
90 {
91 node_t *n;
92 edge_t *e;
93
94 for (n = agfstnode(g); n; n = agnxtnode(g, n))
95 dot_init_node(n);
96 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
97 for (e = agfstout(g, n); e; e = agnxtout(g, e))
98 dot_init_edge(e);
99 }
100 }
101
102 static void
103 dot_cleanup_node(node_t * n)
104 {
105 free_list(ND_in(n));
106 free_list(ND_out(n));
107 free_list(ND_flat_out(n));
108 free_list(ND_flat_in(n));
109 free_list(ND_other(n));
110 free_label(ND_label(n));
111 free_label(ND_xlabel(n));
112 if (ND_shape(n))
113 ND_shape(n)->fns->freefn(n);
114 agdelrec(n, "Agnodeinfo_t");
115 }
116
117 static void free_virtual_edge_list(node_t * n)
118 {
119 edge_t *e;
120
121 for (size_t i = ND_in(n).size - 1; i != SIZE_MAX; i--) {
122 e = ND_in(n).list[i];
123 delete_fast_edge(e);
124 free(e->base.data);
125 free(e);
126 }
127 for (size_t i = ND_out(n).size - 1; i != SIZE_MAX; i--) {
128 e = ND_out(n).list[i];
129 delete_fast_edge(e);
130 free(e->base.data);
131 free(e);
132 }
133 }
134
135 static void free_virtual_node_list(node_t * vn)
136 {
137 node_t *next_vn;
138
139 while (vn) {
140 next_vn = ND_next(vn);
141 free_virtual_edge_list(vn);
142 if (ND_node_type(vn) == VIRTUAL) {
143 free_list(ND_out(vn));
144 free_list(ND_in(vn));
145 free(vn->base.data);
146 free(vn);
147 }
148 vn = next_vn;
149 }
150 }
151
152 static void
153 dot_cleanup_graph(graph_t * g)
154 {
155 int i;
156 graph_t *subg;
157 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
158 dot_cleanup_graph(subg);
159 }
160 if (! agbindrec(g, "Agraphinfo_t", 0, true)) return;
161 free(GD_drawing(g));
162 GD_drawing(g) = NULL;
163 free (GD_clust(g));
164 free (GD_rankleader(g));
165
166 free_list(GD_comp(g));
167 if (GD_rank(g)) {
168 for (i = GD_minrank(g); i <= GD_maxrank(g); i++)
169 free(GD_rank(g)[i].av);
170 if (GD_minrank(g) == -1)
171 free(GD_rank(g)-1);
172 else
173 free(GD_rank(g));
174 }
175 if (g != agroot(g)) {
176 free_label (GD_label(g));
177 }
178 }
179
180 /* delete the layout (but retain the underlying graph) */
181 void dot_cleanup(graph_t * g)
182 {
183 node_t *n;
184 edge_t *e;
185
186 free_virtual_node_list(GD_nlist(g));
187 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
188 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
189 gv_cleanup_edge(e);
190 }
191 dot_cleanup_node(n);
192 }
193 dot_cleanup_graph(g);
194 }
195
196 #ifdef DEBUG
197 int
198 fastn (graph_t * g)
199 {
200 node_t* u;
201 int cnt = 0;
202 for (u = GD_nlist(g); u; u = ND_next(u)) cnt++;
203 return cnt;
204 }
205
206 #if DEBUG > 1
207 static void
208 dumpRanks (graph_t * g)
209 {
210 int i, j;
211 node_t* u;
212 rank_t *rank = GD_rank(g);
213 int rcnt = 0;
214 for (i = GD_minrank(g); i <= GD_maxrank(g); i++) {
215 fprintf (stderr, "[%d] :", i);
216 for (j = 0; j < rank[i].n; j++) {
217 u = rank[i].v[j];
218 rcnt++;
219 if (streq(agnameof(u),"virtual"))
220 fprintf (stderr, " %x", u);
221 else
222 fprintf (stderr, " %s", agnameof(u));
223
224 }
225 fprintf (stderr, "\n");
226 }
227 fprintf (stderr, "count %d rank count = %d\n", fastn(g), rcnt);
228 }
229 #endif
230 #endif
231
232
233 static void
234 remove_from_rank (Agraph_t * g, Agnode_t* n)
235 {
236 Agnode_t* v = NULL;
237 int j, rk = ND_rank(n);
238
239 for (j = 0; j < GD_rank(g)[rk].n; j++) {
240 v = GD_rank(g)[rk].v[j];
241 if (v == n) {
242 for (j++; j < GD_rank(g)[rk].n; j++) {
243 GD_rank(g)[rk].v[j-1] = GD_rank(g)[rk].v[j];
244 }
245 GD_rank(g)[rk].n--;
246 break;
247 }
248 }
249 assert (v == n); /* if found */
250 }
251
252 /* removeFill:
253 * This removes all of the fill nodes added in mincross.
254 * It appears to be sufficient to remove them only from the
255 * rank array and fast node list of the root graph.
256 */
257 static void
258 removeFill (Agraph_t * g)
259 {
260 Agnode_t* n;
261 Agnode_t* nxt;
262 Agraph_t* sg = agsubg (g, "_new_rank", 0);
263
264 if (!sg) return;
265 for (n = agfstnode(sg); n; n = nxt) {
266 nxt = agnxtnode(sg, n);
267 delete_fast_node (g, n);
268 remove_from_rank (g, n);
269 dot_cleanup_node (n);
270 agdelnode(g, n);
271 }
272 agdelsubg (g, sg);
273
274 }
275
276 #define agnodeattr(g,n,v) agattr_text(g,AGNODE,n,v)
277
278 static void
279 attach_phase_attrs (Agraph_t * g, int maxphase)
280 {
281 Agsym_t* rk = agnodeattr(g,"rank","");
282 Agsym_t* order = agnodeattr(g,"order","");
283 Agnode_t* n;
284
285 for (n = agfstnode(g); n; n = agnxtnode(g,n)) {
286 if (maxphase >= 1) {
287 agxset(n, rk, ITOS(ND_rank(n)));
288 }
289 if (maxphase >= 2) {
290 agxset(n, order, ITOS(ND_order(n)));
291 }
292 }
293 }
294
295 /// @return 0 on success
296 static int dotLayout(Agraph_t *g) {
297 int maxphase = late_int(g, agfindgraphattr(g,"phase"), -1, 1);
298
299 setEdgeType (g, EDGETYPE_SPLINE);
300 setAspect(g);
301
302 dot_init_subg(g,g);
303 dot_init_node_edge(g);
304
305 GV_INFO("Starting phase 1 [dot_rank]");
306 dot_rank(g);
307 if (maxphase == 1) {
308 attach_phase_attrs (g, 1);
309 return 0;
310 }
311 GV_INFO("Starting phase 2 [dot_mincross]");
312 const int rc = dot_mincross(g);
313 if (rc != 0) {
314 return rc;
315 }
316 if (maxphase == 2) {
317 attach_phase_attrs (g, 2);
318 return 0;
319 }
320 GV_INFO("Starting phase 3 [dot_position]");
321 dot_position(g);
322 if (maxphase == 3) {
323 attach_phase_attrs (g, 2); /* positions will be attached on output */
324 return 0;
325 }
326 if (GD_flags(g) & NEW_RANK)
327 removeFill (g);
328 dot_sameports(g);
329 const int r = dot_splines(g);
330 if (r != 0) {
331 return r;
332 }
333 if (mapbool(agget(g, "compound")))
334 dot_compoundEdges(g);
335 return 0;
336 }
337
338 static void
339 initSubg (Agraph_t* sg, Agraph_t* g)
340 {
341 agbindrec(sg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
342 GD_drawing(sg) = gv_alloc(sizeof(layout_t));
343 GD_drawing(sg)->quantum = GD_drawing(g)->quantum;
344 GD_drawing(sg)->dpi = GD_drawing(g)->dpi;
345 GD_gvc(sg) = GD_gvc (g);
346 GD_charset(sg) = GD_charset (g);
347 GD_rankdir2(sg) = GD_rankdir2 (g);
348 GD_nodesep(sg) = GD_nodesep(g);
349 GD_ranksep(sg) = GD_ranksep(g);
350 GD_fontnames(sg) = GD_fontnames(g);
351 }
352
353 /* the packing library assumes all units are in inches stored in ND_pos, so we
354 * have to copy the position info there.
355 */
356 static void
357 attachPos (Agraph_t* g)
358 {
359 node_t* np;
360
361 for (np = agfstnode(g); np; np = agnxtnode(g, np)) {
362 ND_pos(np) = gv_calloc(2, sizeof(double));
363 ND_pos(np)[0] = PS2INCH(ND_coord(np).x);
364 ND_pos(np)[1] = PS2INCH(ND_coord(np).y);
365 }
366 }
367
368 /* Store new position info from pack library call, stored in ND_pos in inches,
369 * back to ND_coord in points.
370 */
371 static void
372 resetCoord (Agraph_t* g)
373 {
374 for (node_t *np = agfstnode(g); np; np = agnxtnode(g, np)) {
375 ND_coord(np).x = INCH2PS(ND_pos(np)[0]);
376 ND_coord(np).y = INCH2PS(ND_pos(np)[1]);
377 free(ND_pos(np));
378 ND_pos(np) = NULL;
379 }
380 }
381
382 static void
383 copyCluster (Agraph_t* scl, Agraph_t* cl)
384 {
385 int nclust, j;
386 Agraph_t* cg;
387
388 agbindrec(cl, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
389 GD_bb(cl) = GD_bb(scl);
390 GD_label_pos(cl) = GD_label_pos(scl);
391 memcpy(GD_border(cl), GD_border(scl), 4*sizeof(pointf));
392 nclust = GD_n_cluster(cl) = GD_n_cluster(scl);
393 GD_clust(cl) = gv_calloc(nclust + 1, sizeof(Agraph_t*));
394 for (j = 1; j <= nclust; j++) {
395 cg = mapClust(GD_clust(scl)[j]);
396 GD_clust(cl)[j] = cg;
397 copyCluster (GD_clust(scl)[j], cg);
398 }
399 /* transfer cluster label to original cluster */
400 GD_label(cl) = GD_label(scl);
401 GD_label(scl) = NULL;
402 }
403
404 /* Copy cluster tree and info from components to main graph.
405 * Note that the original clusters have no Agraphinfo_t at this time.
406 */
407 static void copyClusterInfo(size_t ncc, Agraph_t **ccs, Agraph_t *root) {
408 int j, nclust = 0;
409 Agraph_t* sg;
410 Agraph_t* cg;
411
412 for (size_t i = 0; i < ncc; i++)
413 nclust += GD_n_cluster(ccs[i]);
414
415 GD_n_cluster(root) = nclust;
416 GD_clust(root) = gv_calloc(nclust + 1, sizeof(Agraph_t*));
417 nclust = 1;
418 for (size_t i = 0; i < ncc; i++) {
419 sg = ccs[i];
420 for (j = 1; j <= GD_n_cluster(sg); j++) {
421 cg = mapClust(GD_clust(sg)[j]);
422 GD_clust(root)[nclust++] = cg;
423 copyCluster (GD_clust(sg)[j], cg);
424 }
425 }
426 }
427
428 /* Assume g has nodes.
429 *
430 * @return 0 on success
431 */
432 static int doDot(Agraph_t *g) {
433 Agraph_t **ccs;
434 Agraph_t *sg;
435 pack_info pinfo;
436 int Pack = getPack(g, -1, CL_OFFSET);
437 pack_mode mode = getPackModeInfo (g, l_undef, &pinfo);
438 getPackInfo(g, l_node, CL_OFFSET, &pinfo);
439
440 if (mode == l_undef && Pack < 0) {
441 /* No pack information; use old dot with components
442 * handled during layout
443 */
444 const int rc = dotLayout(g);
445 if (rc != 0) {
446 return rc;
447 }
448 } else {
449 /* fill in default values */
450 if (mode == l_undef)
451 pinfo.mode = l_graph;
452 else if (Pack < 0)
453 Pack = CL_OFFSET;
454 assert(Pack >= 0);
455 pinfo.margin = (unsigned)Pack;
456 pinfo.fixed = NULL;
457
458 /* components using clusters */
459 size_t ncc;
460 ccs = cccomps(g, &ncc, 0);
461 if (ncc == 1) {
462 const int rc = dotLayout(g);
463 if (rc != 0) {
464 free(ccs);
465 return rc;
466 }
467 } else if (GD_drawing(g)->ratio_kind == R_NONE) {
468 pinfo.doSplines = true;
469
470 for (size_t i = 0; i < ncc; i++) {
471 sg = ccs[i];
472 initSubg (sg, g);
473 const int rc = dotLayout (sg);
474 if (rc != 0) {
475 free(ccs);
476 return rc;
477 }
478 }
479 attachPos (g);
480 packSubgraphs(ncc, ccs, g, &pinfo);
481 resetCoord (g);
482 copyClusterInfo (ncc, ccs, g);
483 } else {
484 /* Not sure what semantics should be for non-trivial ratio
485 * attribute with multiple components.
486 * One possibility is to layout nodes, pack, then apply the ratio
487 * adjustment. We would then have to re-adjust all positions.
488 */
489 const int rc = dotLayout(g);
490 if (rc != 0) {
491 free(ccs);
492 return rc;
493 }
494 }
495
496 for (size_t i = 0; i < ncc; i++) {
497 dot_cleanup_graph(ccs[i]);
498 agdelete(g, ccs[i]);
499 }
500 free(ccs);
501 }
502 return 0;
503 }
504
505 void dot_layout(Agraph_t * g)
506 {
507 if (agnnodes(g)) {
508 if (doDot(g) != 0) { // error?
509 return;
510 }
511 }
512 dotneato_postprocess(g);
513 }
514
515 Agraph_t * dot_root (void* p)
516 {
517 return GD_dotroot(agroot(p));
518 }
519
520 /**
521 * @defgroup engines Graphviz layout engines
522 * @{
523 * @dir lib/dotgen
524 * @brief [hierarchical or layered](https://en.wikipedia.org/wiki/Layered_graph_drawing) layout engine, API dotgen/dotprocs.h
525 *
526 * [Dot layout user manual](https://graphviz.org/docs/layouts/dot/)
527 *
528 * Other @ref engines
529 * @}
530 */