Graphviz 13.0.0~dev.20250121.0651
Loading...
Searching...
No Matches
layout.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 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/* layout.c:
13 * Written by Emden R. Gansner
14 *
15 * This module provides the main bookkeeping for the fdp layout.
16 * In particular, it handles the recursion and the creation of
17 * ports and auxiliary graphs.
18 *
19 * TODO : can we use ports to aid in layout of edges? Note that
20 * at present, they are deleted.
21 *
22 * Can we delay all repositioning of nodes until evalPositions, so
23 * finalCC only sets the bounding boxes?
24 *
25 * Make sure multiple edges have an effect.
26 */
27
28/* uses PRIVATE interface */
29#define FDP_PRIVATE 1
30
31#include "config.h"
32#include <assert.h>
33#include <float.h>
34#include <limits.h>
35#include <inttypes.h>
36#include <assert.h>
37#include <common/render.h>
38#include <common/utils.h>
39#include <fdpgen/tlayout.h>
40#include <math.h>
41#include <neatogen/neatoprocs.h>
42#include <neatogen/adjust.h>
43#include <fdpgen/comp.h>
44#include <pack/pack.h>
45#include <fdpgen/clusteredges.h>
46#include <fdpgen/dbg.h>
47#include <stddef.h>
48#include <stdbool.h>
49#include <util/alloc.h>
50#include <util/list.h>
51
52typedef struct {
53 graph_t* rootg; /* logical root; graph passed in to fdp_layout */
57 int gid;
60
61typedef struct {
63 double alpha;
64 double dist2;
65} erec;
66
67#define NEW_EDGE(e) (ED_to_virt(e) == 0)
68
69/* finalCC:
70 * Set graph bounding box given list of connected
71 * components, each with its bounding box set.
72 * If c_cnt > 1, then pts != NULL and gives translations for components.
73 * Add margin about whole graph unless isRoot is true.
74 * Reposition nodes based on final position of
75 * node's connected component.
76 * Also, entire layout is translated to origin.
77 */
78static void finalCC(graph_t *g, size_t c_cnt, graph_t **cc, pointf *pts,
79 graph_t *rg, layout_info* infop) {
80 attrsym_t * G_width = infop->G_width;
81 attrsym_t * G_height = infop->G_height;
82 graph_t *cg;
83 boxf bb;
84 boxf bbf;
85 pointf pt;
86 int margin;
87 graph_t **cp = cc;
88 pointf *pp = pts;
89 int isRoot = rg == infop->rootg;
90 int isEmpty = 0;
91
92 /* compute graph bounding box in points */
93 if (c_cnt) {
94 cg = *cp++;
95 bb = GD_bb(cg);
96 if (c_cnt > 1) {
97 pt = *pp++;
98 bb.LL.x += pt.x;
99 bb.LL.y += pt.y;
100 bb.UR.x += pt.x;
101 bb.UR.y += pt.y;
102 while ((cg = *cp++)) {
103 boxf b = GD_bb(cg);
104 pt = *pp++;
105 b.LL.x += pt.x;
106 b.LL.y += pt.y;
107 b.UR.x += pt.x;
108 b.UR.y += pt.y;
109 bb.LL.x = fmin(bb.LL.x, b.LL.x);
110 bb.LL.y = fmin(bb.LL.y, b.LL.y);
111 bb.UR.x = fmax(bb.UR.x, b.UR.x);
112 bb.UR.y = fmax(bb.UR.y, b.UR.y);
113 }
114 }
115 } else { /* empty graph */
116 bb.LL.x = 0;
117 bb.LL.y = 0;
118 bb.UR.x = late_int(rg, G_width, POINTS(DEFAULT_NODEWIDTH), 3);
119 bb.UR.y = late_int(rg, G_height, POINTS(DEFAULT_NODEHEIGHT), 3);
120 isEmpty = 1;
121 }
122
123 if (GD_label(rg)) {
124 isEmpty = 0;
125 double d = round(GD_label(rg)->dimen.x) - (bb.UR.x - bb.LL.x);
126 if (d > 0) { /* height of label added below */
127 d /= 2;
128 bb.LL.x -= d;
129 bb.UR.x += d;
130 }
131 }
132
133 if (isRoot || isEmpty)
134 margin = 0;
135 else
136 margin = late_int (rg, G_margin, CL_OFFSET, 0);
137 pt.x = -bb.LL.x + margin;
138 pt.y = -bb.LL.y + margin + GD_border(rg)[BOTTOM_IX].y;
139 bb.LL.x = 0;
140 bb.LL.y = 0;
141 bb.UR.x += pt.x + margin;
142 bb.UR.y += pt.y + margin + GD_border(rg)[TOP_IX].y;
143
144 /* translate nodes */
145 if (c_cnt) {
146 cp = cc;
147 pp = pts;
148 while ((cg = *cp++)) {
149 pointf p;
150 node_t *n;
151 pointf del;
152
153 if (pp) {
154 p = *pp++;
155 p.x += pt.x;
156 p.y += pt.y;
157 } else {
158 p = pt;
159 }
160 del.x = PS2INCH(p.x);
161 del.y = PS2INCH(p.y);
162 for (n = agfstnode(cg); n; n = agnxtnode(cg, n)) {
163 ND_pos(n)[0] += del.x;
164 ND_pos(n)[1] += del.y;
165 }
166 }
167 }
168
169 bbf.LL.x = PS2INCH(bb.LL.x);
170 bbf.LL.y = PS2INCH(bb.LL.y);
171 bbf.UR.x = PS2INCH(bb.UR.x);
172 bbf.UR.y = PS2INCH(bb.UR.y);
173 BB(g) = bbf;
174
175}
176
177/* mkDeriveNode:
178 * Constructor for a node in a derived graph.
179 * Allocates dndata.
180 */
181static node_t *mkDeriveNode(graph_t * dg, char *name)
182{
183 node_t *dn;
184
185 dn = agnode(dg, name,1);
186 agbindrec(dn, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true); //node custom data
187 ND_alg(dn) = gv_alloc(sizeof(dndata)); // free in freeDeriveNode
188 ND_pos(dn) = gv_calloc(GD_ndim(dg), sizeof(double));
189 /* fprintf (stderr, "Creating %s\n", dn->name); */
190 return dn;
191}
192
193static void freeDeriveNode(node_t * n)
194{
195 free(ND_alg(n));
196 free(ND_pos(n));
197 agdelrec(n, "Agnodeinfo_t");
198}
199
200static void freeGData(graph_t * g)
201{
202 free(GD_alg(g));
203}
204
205static void freeDerivedGraph(graph_t * g, graph_t ** cc)
206{
207 graph_t *cg;
208 node_t *dn;
209 node_t *dnxt;
210 edge_t *e;
211
212 while ((cg = *cc++)) {
213 freeGData(cg);
214 agdelrec(cg, "Agraphinfo_t");
215 }
216 if (PORTS(g))
217 free(PORTS(g));
218 freeGData(g);
219 agdelrec(g, "Agraphinfo_t");
220 for (dn = agfstnode(g); dn; dn = dnxt) {
221 dnxt = agnxtnode(g, dn);
222 for (e = agfstout(g, dn); e; e = agnxtout(g, e)) {
223 free (ED_to_virt(e));
224 agdelrec(e, "Agedgeinfo_t");
225 }
226 freeDeriveNode(dn);
227 }
228 agclose(g);
229}
230
231/* evalPositions:
232 * The input is laid out, but node coordinates
233 * are relative to smallest containing cluster.
234 * Walk through all nodes and clusters, translating
235 * the positions to absolute coordinates.
236 * Assume that when called, g's bounding box is
237 * in absolute coordinates and that box of root graph
238 * has LL at origin.
239 */
240static void evalPositions(graph_t * g, graph_t* rootg)
241{
242 int i;
243 graph_t *subg;
244 node_t *n;
245 boxf bb;
246 boxf sbb;
247
248 bb = BB(g);
249
250 /* translate nodes in g */
251 if (g != rootg) {
252 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
253 if (PARENT(n) != g)
254 continue;
255 ND_pos(n)[0] += bb.LL.x;
256 ND_pos(n)[1] += bb.LL.y;
257 }
258 }
259
260 /* translate top-level clusters and recurse */
261 for (i = 1; i <= GD_n_cluster(g); i++) {
262 subg = GD_clust(g)[i];
263 if (g != rootg) {
264 sbb = BB(subg);
265 sbb.LL.x += bb.LL.x;
266 sbb.LL.y += bb.LL.y;
267 sbb.UR.x += bb.LL.x;
268 sbb.UR.y += bb.LL.y;
269 BB(subg) = sbb;
270 }
271 evalPositions(subg, rootg);
272 }
273}
274
275DEFINE_LIST(clist, graph_t*)
276
277#define BSZ 1000
278
279/* portName:
280 * Generate a name for a port.
281 * We use the ids of the nodes.
282 * This is for debugging. For production, just use edge id and some
283 * id for the graph. Note that all the graphs are subgraphs of the
284 * root graph.
285 */
286static char *portName(graph_t * g, bport_t * p)
287{
288 edge_t *e = p->e;
289 node_t *h = aghead(e);
290 node_t *t = agtail(e);
291 static char buf[BSZ + 1];
292
293 snprintf(buf, sizeof(buf), "_port_%s_(%d)_(%d)_%u",agnameof(g),
294 ND_id(t), ND_id(h), AGSEQ(e));
295 return buf;
296}
297
298/* chkPos:
299 * If cluster has coords attribute, use to supply initial position
300 * of derived node.
301 * Only called if G_coord is defined.
302 * We also look at the parent graph's G_coord attribute. If this
303 * is identical to the child graph, we have to assume the child
304 * inherited it.
305 */
306static void chkPos(graph_t* g, node_t* n, layout_info* infop, boxf* bbp)
307{
308 char *p;
309 char *pp;
310 boxf bb;
311 char c;
313 attrsym_t *G_coord = infop->G_coord;
314
315 p = agxget(g, G_coord);
316 if (p[0]) {
317 if (g != infop->rootg) {
318 parent =agparent(g);
319 pp = agxget(parent, G_coord);
320 if (!strcmp(p, pp))
321 return;
322 }
323 c = '\0';
324 if (sscanf(p, "%lf,%lf,%lf,%lf%c",
325 &bb.LL.x, &bb.LL.y, &bb.UR.x, &bb.UR.y, &c) >= 4) {
326 if (PSinputscale > 0.0) {
327 bb.LL.x /= PSinputscale;
328 bb.LL.y /= PSinputscale;
329 bb.UR.x /= PSinputscale;
330 bb.UR.y /= PSinputscale;
331 }
332 if (c == '!')
333 ND_pinned(n) = P_PIN;
334 else if (c == '?')
335 ND_pinned(n) = P_FIX;
336 else
337 ND_pinned(n) = P_SET;
338 *bbp = bb;
339 } else
340 agwarningf("graph %s, coord %s, expected four doubles\n",
341 agnameof(g), p);
342 }
343}
344
345/* addEdge:
346 * Add real edge e to its image de in the derived graph.
347 * We use the to_virt and count fields to store the list.
348 */
349static void addEdge(edge_t * de, edge_t * e)
350{
351 short cnt = ED_count(de);
352 edge_t **el;
353
354 el = (edge_t**)ED_to_virt(de);
355 el = gv_recalloc(el, cnt, cnt + 1, sizeof(edge_t*));
356 el[cnt] = e;
357 ED_to_virt(de) = (edge_t *) el;
358 ED_count(de)++;
359}
360
361/* copyAttr:
362 * Copy given attribute from g to dg.
363 */
364static void
365copyAttr (graph_t* g, graph_t* dg, char* attr)
366{
367 char* ov_val;
368 Agsym_t* ov;
369
370 if ((ov = agattr(g,AGRAPH, attr, NULL))) {
371 ov_val = agxget(g,ov);
372 ov = agattr(dg,AGRAPH, attr, NULL);
373 if (ov)
374 agxset (dg, ov, ov_val);
375 else {
376 const bool is_html = aghtmlstr(ov_val);
377 is_html ? agattr_html(dg, AGRAPH, attr, ov_val)
378 : agattr(dg, AGRAPH, attr, ov_val);
379 }
380 }
381}
382
383/* deriveGraph:
384 * Create derived graph of g by collapsing clusters into
385 * nodes. An edge is created between nodes if there is
386 * an edge between two nodes in the clusters of the base graph.
387 * Such edges record all corresponding real edges.
388 * In addition, we add a node and edge for each port.
389 */
391{
392 graph_t *dg;
393 node_t *dn;
394 graph_t *subg;
395 bport_t *pp;
396 node_t *n;
397 edge_t *de;
398 int i, id = 0;
399
400 if (Verbose >= 2)
401 fprintf(stderr, "derive graph _dg_%d of %s\n", infop->gid, agnameof(g));
402 infop->gid++;
403
404 dg = agopen("derived", Agstrictdirected,NULL);
405 agbindrec(dg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
406 GD_alg(dg) = gv_alloc(sizeof(gdata)); // freed in freeDeriveGraph
407#ifdef DEBUG
408 GORIG(dg) = g;
409#endif
410 GD_ndim(dg) = GD_ndim(agroot(g));
411
412 /* Copy attributes from g.
413 */
414 copyAttr(g,dg,"overlap");
415 copyAttr(g,dg,"sep");
416 copyAttr(g,dg,"K");
417
418 /* create derived nodes from clusters */
419 for (i = 1; i <= GD_n_cluster(g); i++) {
420 boxf fix_bb = {{DBL_MAX, DBL_MAX}, {-DBL_MAX, -DBL_MAX}};
421 subg = GD_clust(g)[i];
422
423 do_graph_label(subg);
424 dn = mkDeriveNode(dg, agnameof(subg));
425 ND_clust(dn) = subg;
426 ND_id(dn) = id++;
427 if (infop->G_coord)
428 chkPos(subg, dn, infop, &fix_bb);
429 for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) {
430 DNODE(n) = dn;
431 }
432 if (ND_pinned(dn)) {
433 ND_pos(dn)[0] = (fix_bb.LL.x + fix_bb.UR.x) / 2;
434 ND_pos(dn)[1] = (fix_bb.LL.y + fix_bb.UR.y) / 2;
435 }
436 }
437
438 /* create derived nodes from remaining nodes */
439 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
440 if (!DNODE(n)) {
441 if (PARENT(n) && PARENT(n) != GPARENT(g)) {
442 agerrorf("node \"%s\" is contained in two non-comparable clusters \"%s\" and \"%s\"\n", agnameof(n), agnameof(g), agnameof(PARENT(n)));
443 return NULL;
444 }
445 PARENT(n) = g;
446 if (IS_CLUST_NODE(n))
447 continue;
448 dn = mkDeriveNode(dg, agnameof(n));
449 DNODE(n) = dn;
450 ND_id(dn) = id++;
451 ND_width(dn) = ND_width(n);
452 ND_height(dn) = ND_height(n);
453 ND_lw(dn) = ND_lw(n);
454 ND_rw(dn) = ND_rw(n);
455 ND_ht(dn) = ND_ht(n);
456 ND_shape(dn) = ND_shape(n);
458 if (ND_pinned(n)) {
459 ND_pos(dn)[0] = ND_pos(n)[0];
460 ND_pos(dn)[1] = ND_pos(n)[1];
461 ND_pinned(dn) = ND_pinned(n);
462 }
463 ANODE(dn) = n;
464 }
465 }
466
467 /* add edges */
468 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
469 edge_t *e;
470 node_t *hd;
471 node_t *tl = DNODE(n);
472 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
473 hd = DNODE(aghead(e));
474 if (hd == tl)
475 continue;
476 if (hd > tl)
477 de = agedge(dg, tl, hd, NULL,1);
478 else
479 de = agedge(dg, hd, tl, NULL,1);
480 agbindrec(de, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true);
481 ED_dist(de) = ED_dist(e);
482 ED_factor(de) = ED_factor(e);
483 /* fprintf (stderr, "edge %s -- %s\n", tl->name, hd->name); */
484 WDEG(hd)++;
485 WDEG(tl)++;
486 if (NEW_EDGE(de)) {
487 DEG(hd)++;
488 DEG(tl)++;
489 }
490 addEdge(de, e);
491 }
492 }
493
494 /* transform ports */
495 if ((pp = PORTS(g))) {
496 bport_t *pq;
497 node_t *m;
498 int sz = NPORTS(g);
499
500 /* freed in freeDeriveGraph */
501 PORTS(dg) = pq = gv_calloc(sz + 1, sizeof(bport_t));
502 sz = 0;
503 while (pp->e) {
504 m = DNODE(pp->n);
505 /* Create port in derived graph only if hooks to internal node */
506 if (m) {
507 dn = mkDeriveNode(dg, portName(g, pp));
508 sz++;
509 ND_id(dn) = id++;
510 if (dn > m)
511 de = agedge(dg, m, dn, NULL,1);
512 else
513 de = agedge(dg, dn, m, NULL,1);
514 agbindrec(de, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true);
515 ED_dist(de) = ED_dist(pp->e);
516 ED_factor(de) = ED_factor(pp->e);
517 addEdge(de, pp->e);
518 WDEG(dn)++;
519 WDEG(m)++;
520 DEG(dn)++; /* ports are unique, so this will be the first and */
521 DEG(m)++; /* only time the edge is touched. */
522 pq->n = dn;
523 pq->alpha = pp->alpha;
524 pq->e = de;
525 pq++;
526 }
527 pp++;
528 }
529 NPORTS(dg) = sz;
530 }
531
532 return dg;
533}
534
535/* ecmp:
536 * Sort edges by angle, then distance.
537 */
538static int ecmp(const void *v1, const void *v2)
539{
540 const erec *e1 = v1;
541 const erec *e2 = v2;
542 if (e1->alpha > e2->alpha)
543 return 1;
544 else if (e1->alpha < e2->alpha)
545 return -1;
546 else if (e1->dist2 > e2->dist2)
547 return 1;
548 else if (e1->dist2 < e2->dist2)
549 return -1;
550 else
551 return 0;
552}
553
554#define ANG (M_PI/90) /* Maximum angular change: 2 degrees */
555
556/* getEdgeList:
557 * Generate list of edges in derived graph g using
558 * node n. The list is in counterclockwise order.
559 * This, of course, assumes we have an initial layout for g.
560 */
562{
563 int deg = DEG(n);
564 int i;
565 double dx, dy;
566 edge_t *e;
567 node_t *m;
568
569 /* freed in expandCluster */
570 erec *erecs = gv_calloc(deg + 1, sizeof(erec));
571 i = 0;
572 for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
573 if (aghead(e) == n)
574 m = agtail(e);
575 else
576 m = aghead(e);
577 dx = ND_pos(m)[0] - ND_pos(n)[0];
578 dy = ND_pos(m)[1] - ND_pos(n)[1];
579 erecs[i].e = e;
580 erecs[i].alpha = atan2(dy, dx);
581 erecs[i].dist2 = dx * dx + dy * dy;
582 i++;
583 }
584 assert(i == deg);
585 qsort(erecs, deg, sizeof(erec), ecmp);
586
587 /* ensure no two angles are equal */
588 if (deg >= 2) {
589 int j;
590 double a, inc, delta, bnd;
591
592 i = 0;
593 while (i < deg - 1) {
594 a = erecs[i].alpha;
595 j = i + 1;
596 while (j < deg && erecs[j].alpha == a)
597 j++;
598 if (j == i + 1)
599 i = j;
600 else {
601 if (j == deg)
602 bnd = M_PI; /* all values equal up to end */
603 else
604 bnd = erecs[j].alpha;
605 delta = fmin((bnd - a) / (j - i), ANG);
606 inc = 0;
607 for (; i < j; i++) {
608 erecs[i].alpha += inc;
609 inc += delta;
610 }
611 }
612 }
613 }
614
615 return erecs;
616}
617
618/* genPorts:
619 * Given list of edges with node n in derived graph, add corresponding
620 * ports to port list pp, starting at index idx. Return next index.
621 * If an edge in the derived graph corresponds to multiple real edges,
622 * add them in order if address of n is smaller than other node address.
623 * Otherwise, reverse order.
624 * Attach angles. The value bnd gives next angle after er->alpha.
625 */
626static int
627genPorts(node_t * n, erec * er, bport_t * pp, int idx, double bnd)
628{
629 node_t *other;
630 int cnt;
631 edge_t *e = er->e;
632 edge_t *el;
633 edge_t **ep;
634 double angle, delta;
635 int i, j, inc;
636
637 cnt = ED_count(e);
638
639 if (aghead(e) == n)
640 other = agtail(e);
641 else
642 other = aghead(e);
643
644 delta = fmin((bnd - er->alpha) / cnt, ANG);
645 angle = er->alpha;
646
647 if (n < other) {
648 i = idx;
649 inc = 1;
650 } else {
651 i = idx + cnt - 1;
652 inc = -1;
653 angle += delta * (cnt - 1);
654 delta = -delta;
655 }
656
657 ep = (edge_t **)ED_to_virt(e);
658 for (j = 0; j < ED_count(e); j++, ep++) {
659 el = *ep;
660 pp[i].e = el;
661 pp[i].n = DNODE(agtail(el)) == n ? agtail(el) : aghead(el);
662 pp[i].alpha = angle;
663 i += inc;
664 angle += delta;
665 }
666 return (idx + cnt);
667}
668
669/* expandCluster;
670 * Given positioned derived graph cg with node n which corresponds
671 * to a cluster, generate a graph containing the interior of the
672 * cluster, plus port information induced by the layout of cg.
673 * Basically, we use the cluster subgraph to which n corresponds,
674 * attached with port information.
675 */
677{
678 erec *es;
679 erec *ep;
680 erec *next;
681 graph_t *sg = ND_clust(n);
682 int sz = WDEG(n);
683 int idx = 0;
684 double bnd;
685
686 if (sz != 0) {
687 /* freed in cleanup_subgs */
688 bport_t *pp = gv_calloc(sz + 1, sizeof(bport_t));
689
690 /* create sorted list of edges of n */
691 es = ep = getEdgeList(n, cg);
692
693 /* generate ports from edges */
694 while (ep->e) {
695 next = ep + 1;
696 if (next->e)
697 bnd = next->alpha;
698 else
699 bnd = 2 * M_PI + es->alpha;
700 idx = genPorts(n, ep, pp, idx, bnd);
701 ep = next;
702 }
703 assert(idx == sz);
704
705 PORTS(sg) = pp;
706 NPORTS(sg) = sz;
707 free(es);
708 }
709 return sg;
710}
711
712/* setClustNodes:
713 * At present, cluster nodes are not assigned a position during layout,
714 * but positioned in the center of its associated cluster. Because the
715 * dummy edge associated with a cluster node may not occur at a sufficient
716 * level of cluster, the edge may not be used during layout and we cannot
717 * therefore rely find these nodes via ports.
718 *
719 * In this implementation, we just do a linear pass over all nodes in the
720 * root graph. At some point, we may use a better method, like having each
721 * cluster contain its list of cluster nodes, or have the graph keep a list.
722 *
723 * As nodes, we need to assign cluster nodes the coordinates in the
724 * coordinates of its cluster p. Note that p's bbox is in its parent's
725 * coordinates.
726 *
727 * If routing, we may decide to place on cluster boundary,
728 * and use polyline.
729 */
730static void
732{
733 boxf bb;
734 graph_t* p;
735 pointf ctr;
736 node_t *n;
737 double w, h, h_pts;
738 double h2, w2;
739 pointf *vertices;
740
741 for (n = agfstnode(root); n; n = agnxtnode(root, n)) {
742 if (!IS_CLUST_NODE(n)) continue;
743
744 p = PARENT(n);
745 bb = BB(p); /* bbox in parent cluster's coordinates */
746 w = bb.UR.x - bb.LL.x;
747 h = bb.UR.y - bb.LL.y;
748 ctr.x = w / 2.0;
749 ctr.y = h / 2.0;
750 w2 = INCH2PS(w / 2.0);
751 h2 = INCH2PS(h / 2.0);
752 h_pts = INCH2PS(h);
753 ND_pos(n)[0] = ctr.x;
754 ND_pos(n)[1] = ctr.y;
755 ND_width(n) = w;
756 ND_height(n) = h;
759 ND_outline_width(n) = w + penwidth;
761 /* ND_xsize(n) = POINTS(w); */
762 ND_lw(n) = ND_rw(n) = w2;
763 ND_ht(n) = h_pts;
764
765 vertices = ((polygon_t *) ND_shape_info(n))->vertices;
766 vertices[0].x = ND_rw(n);
767 vertices[0].y = h2;
768 vertices[1].x = -ND_lw(n);
769 vertices[1].y = h2;
770 vertices[2].x = -ND_lw(n);
771 vertices[2].y = -h2;
772 vertices[3].x = ND_rw(n);
773 vertices[3].y = -h2;
774 // allocate extra vertices representing the outline, i.e., the outermost
775 // periphery with penwidth taken into account
776 vertices[4].x = ND_rw(n) + penwidth / 2;
777 vertices[4].y = h2 + penwidth / 2;
778 vertices[5].x = -ND_lw(n) - penwidth / 2;
779 vertices[5].y = h2 + penwidth / 2;
780 vertices[6].x = -ND_lw(n) - penwidth / 2;
781 vertices[6].y = -h2 - penwidth / 2;
782 vertices[7].x = ND_rw(n) + penwidth / 2;
783 vertices[7].y = -h2 - penwidth / 2;
784 }
785}
786
787/* layout:
788 * Given g with ports:
789 * Derive g' from g by reducing clusters to points (deriveGraph)
790 * Compute connected components of g' (findCComp)
791 * For each cc of g':
792 * Layout cc (tLayout)
793 * For each node n in cc of g' <-> cluster c in g:
794 * Add ports based on layout of cc to get c' (expandCluster)
795 * Layout c' (recursion)
796 * Remove ports from cc
797 * Expand nodes of cc to reflect size of c' (xLayout)
798 * Pack connected components to get layout of g (putGraphs)
799 * Translate layout so that bounding box of layout + margin
800 * has the origin as LL corner.
801 * Set position of top level clusters and real nodes.
802 * Set bounding box of graph
803 *
804 * TODO:
805 *
806 * Possibly should modify so that only do connected components
807 * on top-level derived graph. Unconnected parts of a cluster
808 * could just rattle within cluster boundaries. This may mix
809 * up components but give a tighter packing.
810 *
811 * Add edges per components to get better packing, rather than
812 * wait until the end.
813 */
814static int layout(graph_t * g, layout_info * infop)
815{
816 pointf *pts = NULL;
817 graph_t *dg;
818 node_t *dn;
819 node_t *n;
820 graph_t *cg;
821 graph_t *sg;
822 graph_t **cc;
823 graph_t **pg;
824 int pinned;
825 xparams xpms;
826
827#ifdef DEBUG
828 incInd();
829#endif
830 if (Verbose) {
831#ifdef DEBUG
832 prIndent();
833#endif
834 fprintf (stderr, "layout %s\n", agnameof(g));
835 }
836 /* initialize derived node pointers */
837 for (n = agfstnode(g); n; n = agnxtnode(g, n))
838 DNODE(n) = 0;
839
840 dg = deriveGraph(g, infop);
841 if (dg == NULL) {
842 return -1;
843 }
844 size_t c_cnt;
845 cc = pg = findCComp(dg, &c_cnt, &pinned);
846
847 while ((cg = *pg++)) {
848 node_t* nxtnode;
849 fdp_tLayout(cg, &xpms);
850 for (n = agfstnode(cg); n; n = nxtnode) {
851 nxtnode = agnxtnode(cg, n);
852 if (ND_clust(n)) {
853 pointf pt;
854 sg = expandCluster(n, cg); /* attach ports to sg */
855 int r = layout(sg, infop);
856 if (r != 0) {
857 return r;
858 }
859 ND_width(n) = BB(sg).UR.x;
860 ND_height(n) = BB(sg).UR.y;
861 pt.x = POINTS_PER_INCH * BB(sg).UR.x;
862 pt.y = POINTS_PER_INCH * BB(sg).UR.y;
863 ND_rw(n) = ND_lw(n) = pt.x/2;
864 ND_ht(n) = pt.y;
865 } else if (IS_PORT(n))
866 agdelete(cg, n); /* remove ports from component */
867 }
868
869 /* Remove overlaps */
870 if (agnnodes(cg) >= 2) {
871 if (g == infop->rootg)
872 normalize (cg);
873 fdp_xLayout(cg, &xpms);
874 }
875 }
876
877 /* At this point, each connected component has its nodes correctly
878 * positioned. If we have multiple components, we pack them together.
879 * All nodes will be moved to their new positions.
880 * NOTE: packGraphs uses nodes in components, so if port nodes are
881 * not removed, it won't work.
882 */
883 /* Handle special cases well: no ports to real internal nodes
884 * Place cluster edges separately, after layout.
885 * How to combine parts, especially with disparate components?
886 */
887 if (c_cnt > 1) {
888 bool *bp;
889 if (pinned) {
890 bp = gv_calloc(c_cnt, sizeof(bool));
891 bp[0] = true;
892 } else
893 bp = NULL;
894 infop->pack.fixed = bp;
895 pts = putGraphs(c_cnt, cc, NULL, &infop->pack);
896 free(bp);
897 } else {
898 pts = NULL;
899 if (c_cnt == 1)
900 compute_bb(cc[0]);
901 }
902
903 /* set bounding box of dg and reposition nodes */
904 finalCC(dg, c_cnt, cc, pts, g, infop);
905 free (pts);
906
907 /* record positions from derived graph to input graph */
908 /* At present, this does not record port node info */
909 /* In fact, as noted above, we have removed port nodes */
910 for (dn = agfstnode(dg); dn; dn = agnxtnode(dg, dn)) {
911 if ((sg = ND_clust(dn))) {
912 BB(sg).LL.x = ND_pos(dn)[0] - ND_width(dn) / 2;
913 BB(sg).LL.y = ND_pos(dn)[1] - ND_height(dn) / 2;
914 BB(sg).UR.x = BB(sg).LL.x + ND_width(dn);
915 BB(sg).UR.y = BB(sg).LL.y + ND_height(dn);
916 } else if ((n = ANODE(dn))) {
917 ND_pos(n)[0] = ND_pos(dn)[0];
918 ND_pos(n)[1] = ND_pos(dn)[1];
919 }
920 }
921 BB(g) = BB(dg);
922#ifdef DEBUG
923 if (g == infop->rootg)
924 dump(g, 1);
925#endif
926
927 /* clean up temp graphs */
928 freeDerivedGraph(dg, cc);
929 free(cc);
930 if (Verbose) {
931#ifdef DEBUG
932 prIndent ();
933#endif
934 fprintf (stderr, "end %s\n", agnameof(g));
935 }
936#ifdef DEBUG
937 decInd();
938#endif
939
940 return 0;
941}
942
943/* setBB;
944 * Set point box g->bb from inch box BB(g).
945 */
946static void setBB(graph_t * g)
947{
948 int i;
949 boxf bb;
950
951 bb.LL.x = POINTS_PER_INCH * BB(g).LL.x;
952 bb.LL.y = POINTS_PER_INCH * BB(g).LL.y;
953 bb.UR.x = POINTS_PER_INCH * BB(g).UR.x;
954 bb.UR.y = POINTS_PER_INCH * BB(g).UR.y;
955 GD_bb(g) = bb;
956 for (i = 1; i <= GD_n_cluster(g); i++) {
957 setBB(GD_clust(g)[i]);
958 }
959}
960
961/* init_info:
962 * Initialize graph-dependent information and
963 * state variable.s
964 */
965static void init_info(graph_t * g, layout_info * infop)
966{
967 infop->G_coord = agattr(g, AGRAPH, "coords", NULL);
968 infop->G_width = agattr(g, AGRAPH, "width", NULL);
969 infop->G_height = agattr(g, AGRAPH, "height", NULL);
970 infop->rootg = g;
971 infop->gid = 0;
972 infop->pack.mode = getPackInfo(g, l_node, CL_OFFSET / 2, &infop->pack);
973}
974
975/* mkClusters:
976 * Attach list of immediate child clusters.
977 * NB: By convention, the indexing starts at 1.
978 * If pclist is NULL, the graph is the root graph or a cluster
979 * If pclist is non-NULL, we are recursively scanning a non-cluster
980 * subgraph for cluster children.
981 */
982static void
983mkClusters (graph_t * g, clist_t* pclist, graph_t* parent)
984{
985 graph_t* subg;
986 clist_t list = {0};
987 clist_t* clist;
988
989 if (pclist == NULL) {
990 // [0] is empty. The clusters are in [1..cnt].
991 clist_append(&list, NULL);
992 clist = &list;
993 }
994 else
995 clist = pclist;
996
997 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg))
998 {
999 if (is_a_cluster(subg)) {
1000 agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
1001 GD_alg(subg) = gv_alloc(sizeof(gdata)); // freed in cleanup_subgs
1002 GD_ndim(subg) = GD_ndim(agroot(parent));
1003 LEVEL(subg) = LEVEL(parent) + 1;
1004 GPARENT(subg) = parent;
1005 clist_append(clist, subg);
1006 mkClusters(subg, NULL, subg);
1007 }
1008 else {
1009 mkClusters(subg, clist, parent);
1010 }
1011 }
1012 if (pclist == NULL) {
1013 assert(clist_size(&list) - 1 <= INT_MAX);
1014 GD_n_cluster(g) = (int)(clist_size(&list) - 1);
1015 if (clist_size(&list) > 1) {
1016 clist_shrink_to_fit(&list);
1017 GD_clust(g) = clist_detach(&list);
1018 } else {
1019 clist_free(&list);
1020 }
1021 }
1022}
1023
1024static void fdp_init_graph(Agraph_t * g)
1025{
1027 GD_alg(g) = gv_alloc(sizeof(gdata)); // freed in cleanup_graph
1028 GD_ndim(agroot(g)) = late_int(g, agattr(g,AGRAPH, "dim", NULL), 2, 2);
1029 Ndim = GD_ndim(agroot(g)) = MIN(GD_ndim(agroot(g)), MAXDIM);
1030
1031 mkClusters (g, NULL, g);
1032 fdp_initParams(g);
1034}
1035
1036static int fdpLayout(graph_t * g)
1037{
1039
1040 init_info(g, &info);
1041 int r = layout(g, &info);
1042 if (r != 0) {
1043 return r;
1044 }
1045 setClustNodes(g);
1046 evalPositions(g,g);
1047
1048 /* Set bbox info for g and all clusters. This is needed for
1049 * spline drawing. We already know the graph bbox is at the origin.
1050 * On return from spline drawing, all bounding boxes should be correct.
1051 */
1052 setBB(g);
1053
1054 return 0;
1055}
1056
1057static void
1059{
1060 int trySplines = 0;
1061 int et = EDGE_TYPE(g);
1062
1063 if (et > EDGETYPE_ORTHO) {
1064 if (et == EDGETYPE_COMPOUND) {
1065 trySplines = splineEdges(g, compoundEdges, EDGETYPE_SPLINE);
1066 /* When doing the edges again, accept edges done by compoundEdges */
1067 if (trySplines)
1068 Nop = 2;
1069 }
1070 if (trySplines || et != EDGETYPE_COMPOUND) {
1071 if (HAS_CLUST_EDGE(g)) {
1072 agwarningf(
1073 "splines and cluster edges not supported - using line segments\n");
1074 et = EDGETYPE_LINE;
1075 } else {
1076 spline_edges1(g, et);
1077 }
1078 }
1079 Nop = 0;
1080 }
1081 if (State < GVSPLINES)
1082 spline_edges1(g, et);
1083}
1084
1086{
1087 double save_scale = PSinputscale;
1088
1090 fdp_init_graph(g);
1091 if (fdpLayout(g) != 0) {
1092 return;
1093 }
1095
1096 if (EDGE_TYPE(g) != EDGETYPE_NONE) fdpSplines (g);
1097
1098 gv_postprocess(g, 0);
1099 PSinputscale = save_scale;
1100}
int normalize(graph_t *g)
Definition adjust.c:739
Memory allocation wrappers that exit on failure.
static void * gv_recalloc(void *ptr, size_t old_nmemb, size_t new_nmemb, size_t size)
Definition alloc.h:73
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
#define MIN(a, b)
Definition arith.h:28
#define M_PI
Definition arith.h:41
#define DNODE(n)
Definition circular.h:75
#define PARENT(n)
Definition circular.h:84
#define parent(i)
Definition closest.c:80
int compoundEdges(graph_t *g, expand_t *pm, int edgetype)
void setEdgeType(graph_t *g, int defaultValue)
Definition utils.c:1434
int late_int(void *obj, attrsym_t *attr, int defaultValue, int minimum)
Definition utils.c:35
double late_double(void *obj, attrsym_t *attr, double defaultValue, double minimum)
Definition utils.c:50
double get_inputscale(graph_t *g)
Definition utils.c:73
void compute_bb(graph_t *g)
Definition utils.c:630
bool is_a_cluster(Agraph_t *g)
Definition utils.c:692
graph_t ** findCComp(graph_t *g, size_t *cnt, int *pinned)
Definition comp.c:61
#define P_PIN
Definition const.h:263
#define BOTTOM_IX
Definition const.h:111
#define P_SET
Definition const.h:261
#define TOP_IX
Definition const.h:113
#define EDGETYPE_SPLINE
Definition const.h:253
#define CL_OFFSET
Definition const.h:151
#define DEFAULT_NODEHEIGHT
Definition const.h:72
#define DEFAULT_NODEPENWIDTH
Definition const.h:77
#define DEFAULT_NODEWIDTH
Definition const.h:74
#define MAXDIM
Definition const.h:169
#define P_FIX
Definition const.h:262
#define EDGETYPE_ORTHO
Definition const.h:252
#define MIN_NODEPENWIDTH
Definition const.h:78
#define EDGETYPE_LINE
Definition const.h:249
#define EDGETYPE_NONE
Definition const.h:248
#define GVSPLINES
Definition const.h:173
#define EDGETYPE_COMPOUND
Definition const.h:254
static float dy
Definition draw.c:38
static float dx
Definition draw.c:37
static void del(Dict_t *d, Dtlink_t **set, Agedge_t *e)
Definition edge.c:157
void fdp_init_node_edge(Agraph_t *g)
Definition fdpinit.c:87
#define PS2INCH(a_points)
Definition geom.h:64
#define POINTS(a_inches)
Definition geom.h:62
#define POINTS_PER_INCH
Definition geom.h:58
#define INCH2PS(a_inches)
Definition geom.h:63
int State
Definition globals.h:62
int Nop
Definition globals.h:54
Agsym_t * G_margin
Definition globals.h:71
Agsym_t * N_penwidth
Definition globals.h:79
double PSinputscale
Definition globals.h:55
unsigned short Ndim
Definition globals.h:61
static bool Verbose
Definition gml2gv.c:23
void free(void *)
node NULL
Definition grammar.y:163
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:206
int agnnodes(Agraph_t *g)
Definition graph.c:165
Agsym_t * agattr(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up attributes of a graph
Definition attr.c:371
int agxset(void *obj, Agsym_t *sym, const char *value)
Definition attr.c:532
char * agxget(void *obj, Agsym_t *sym)
Definition attr.c:481
Agsym_t * agattr_html(Agraph_t *g, int kind, char *name, const char *value)
agattr, but creates HTML-like values
Definition attr.c:375
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Definition edge.c:256
#define ED_dist(e)
Definition types.h:602
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:24
#define ED_count(e)
Definition types.h:580
#define agtail(e)
Definition cgraph.h:888
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition edge.c:94
#define ED_factor(e)
Definition types.h:585
#define aghead(e)
Definition cgraph.h:889
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:39
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition edge.c:85
#define ED_to_virt(e)
Definition types.h:599
void agwarningf(const char *fmt,...)
Definition agerror.c:173
void agerrorf(const char *fmt,...)
Definition agerror.c:165
#define GD_border(g)
Definition types.h:359
#define GD_clust(g)
Definition types.h:360
#define GD_alg(g)
Definition types.h:358
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:99
#define GD_bb(g)
Definition types.h:354
Agdesc_t Agstrictdirected
strict directed. A strict graph cannot have multi-edges or self-arcs.
Definition graph.c:281
#define GD_n_cluster(g)
Definition types.h:389
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
Definition graph.c:44
#define GD_ndim(g)
Definition types.h:390
#define GD_label(g)
Definition types.h:374
Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
Definition node.c:140
#define ND_outline_width(n)
Definition types.h:516
#define ND_outline_height(n)
Definition types.h:517
#define ND_ht(n)
Definition types.h:500
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
#define ND_pinned(n)
Definition types.h:519
#define ND_clust(n)
Definition types.h:489
#define ND_alg(n)
Definition types.h:484
#define ND_rw(n)
Definition types.h:525
#define ND_height(n)
Definition types.h:498
#define ND_width(n)
Definition types.h:536
#define ND_lw(n)
Definition types.h:506
#define ND_shape_info(n)
Definition types.h:529
#define ND_pos(n)
Definition types.h:520
#define ND_shape(n)
Definition types.h:528
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:143
int agdelete(Agraph_t *g, void *obj)
deletes object. Equivalent to agclose, agdelnode, and agdeledge for obj being a graph,...
Definition obj.c:20
Agraph_t * agroot(void *obj)
Definition obj.c:168
#define AGSEQ(obj)
Definition cgraph.h:225
@ AGRAPH
Definition cgraph.h:207
void * agbindrec(void *obj, const char *name, unsigned int recsize, int move_to_front)
attaches a new record of the given size to the object
Definition rec.c:89
int agdelrec(void *obj, const char *name)
deletes a named record from one object
Definition rec.c:137
int aghtmlstr(const char *)
Definition refstr.c:401
Agraph_t * agparent(Agraph_t *g)
Definition subg.c:88
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:75
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:80
static double penwidth[]
void do_graph_label(graph_t *sg)
Set characteristics of graph label if it exists.
Definition input.c:829
static erec * getEdgeList(node_t *n, graph_t *g)
Definition layout.c:561
static void fdpSplines(graph_t *g)
Definition layout.c:1058
static void setBB(graph_t *g)
Definition layout.c:946
static void init_info(graph_t *g, layout_info *infop)
Definition layout.c:965
static void fdp_init_graph(Agraph_t *g)
Definition layout.c:1024
static char * portName(graph_t *g, bport_t *p)
Definition layout.c:286
void fdp_layout(graph_t *g)
Definition layout.c:1085
static void evalPositions(graph_t *g, graph_t *rootg)
Definition layout.c:240
static int genPorts(node_t *n, erec *er, bport_t *pp, int idx, double bnd)
Definition layout.c:627
static int ecmp(const void *v1, const void *v2)
Definition layout.c:538
static void addEdge(edge_t *de, edge_t *e)
Definition layout.c:349
static void chkPos(graph_t *g, node_t *n, layout_info *infop, boxf *bbp)
Definition layout.c:306
static node_t * mkDeriveNode(graph_t *dg, char *name)
Definition layout.c:181
static graph_t * expandCluster(node_t *n, graph_t *cg)
Definition layout.c:676
static void freeDerivedGraph(graph_t *g, graph_t **cc)
Definition layout.c:205
static void freeDeriveNode(node_t *n)
Definition layout.c:193
static int fdpLayout(graph_t *g)
Definition layout.c:1036
#define ANG
Definition layout.c:554
static void copyAttr(graph_t *g, graph_t *dg, char *attr)
Definition layout.c:365
static void freeGData(graph_t *g)
Definition layout.c:200
#define NEW_EDGE(e)
Definition layout.c:67
static void setClustNodes(graph_t *root)
Definition layout.c:731
static void mkClusters(graph_t *g, clist_t *pclist, graph_t *parent)
Definition layout.c:983
static void finalCC(graph_t *g, size_t c_cnt, graph_t **cc, pointf *pts, graph_t *rg, layout_info *infop)
Definition layout.c:78
#define BSZ
Definition layout.c:277
static int layout(graph_t *g, layout_info *infop)
Definition layout.c:814
static graph_t * deriveGraph(graph_t *g, layout_info *infop)
Definition layout.c:390
#define DEFINE_LIST(name, type)
Definition list.h:21
#define IS_CLUST_NODE(n)
Definition macros.h:23
#define EDGE_TYPE(g)
Definition macros.h:25
#define HAS_CLUST_EDGE(g)
Definition macros.h:24
#define delta
Definition maze.c:133
#define ND_id(n)
Definition mm2gv.c:39
NEATOPROCS_API bool neato_set_aspect(graph_t *g)
NEATOPROCS_API int splineEdges(graph_t *, int(*edgefn)(graph_t *, expand_t *, int), int)
NEATOPROCS_API int spline_edges1(graph_t *g, int)
pack_mode getPackInfo(Agraph_t *g, pack_mode dflt, int dfltMargin, pack_info *pinfo)
Definition pack.c:1282
pointf * putGraphs(size_t ng, Agraph_t **gs, Agraph_t *root, pack_info *pinfo)
Definition pack.c:889
support for connected components
@ l_node
Definition pack.h:55
void gv_postprocess(Agraph_t *g, int allowTranslation)
Definition postproc.c:598
#define alpha
Definition shapes.c:4058
static double cg(SparseMatrix A, const double *precond, int n, int dim, double *x0, double *rhs, double tol, double maxit)
graph or subgraph
Definition cgraph.h:424
string attribute descriptor symbol in Agattr_s.dict
Definition cgraph.h:641
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
Definition layout.c:61
double dist2
Definition layout.c:64
edge_t * e
Definition layout.c:62
double alpha
Definition layout.c:63
attrsym_t * G_coord
Definition layout.c:54
pack_info pack
Definition layout.c:58
attrsym_t * G_width
Definition layout.c:55
int gid
Definition layout.c:57
graph_t * rootg
Definition layout.c:53
attrsym_t * G_height
Definition layout.c:56
pack_mode mode
Definition pack.h:72
bool * fixed
Definition pack.h:73
double x
Definition geom.h:29
double y
Definition geom.h:29
Definition heap.c:19
void fdp_initParams(graph_t *g)
Definition tlayout.c:166
void fdp_tLayout(graph_t *g, xparams *xpms)
Definition tlayout.c:612
void fdp_xLayout(graph_t *g, xparams *xpms)
Definition xlayout.c:324