Graphviz 13.0.0~dev.20241220.2304
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 <cgraph/list.h>
38#include <common/render.h>
39#include <common/utils.h>
40#include <fdpgen/tlayout.h>
41#include <math.h>
42#include <neatogen/neatoprocs.h>
43#include <neatogen/adjust.h>
44#include <fdpgen/comp.h>
45#include <pack/pack.h>
46#include <fdpgen/clusteredges.h>
47#include <fdpgen/dbg.h>
48#include <stddef.h>
49#include <stdbool.h>
50#include <util/alloc.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 agattr(dg, AGRAPH, attr, ov_val);
377 }
378}
379
380/* deriveGraph:
381 * Create derived graph of g by collapsing clusters into
382 * nodes. An edge is created between nodes if there is
383 * an edge between two nodes in the clusters of the base graph.
384 * Such edges record all corresponding real edges.
385 * In addition, we add a node and edge for each port.
386 */
388{
389 graph_t *dg;
390 node_t *dn;
391 graph_t *subg;
392 bport_t *pp;
393 node_t *n;
394 edge_t *de;
395 int i, id = 0;
396
397 if (Verbose >= 2)
398 fprintf(stderr, "derive graph _dg_%d of %s\n", infop->gid, agnameof(g));
399 infop->gid++;
400
401 dg = agopen("derived", Agstrictdirected,NULL);
402 agbindrec(dg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
403 GD_alg(dg) = gv_alloc(sizeof(gdata)); // freed in freeDeriveGraph
404#ifdef DEBUG
405 GORIG(dg) = g;
406#endif
407 GD_ndim(dg) = GD_ndim(agroot(g));
408
409 /* Copy attributes from g.
410 */
411 copyAttr(g,dg,"overlap");
412 copyAttr(g,dg,"sep");
413 copyAttr(g,dg,"K");
414
415 /* create derived nodes from clusters */
416 for (i = 1; i <= GD_n_cluster(g); i++) {
417 boxf fix_bb = {{DBL_MAX, DBL_MAX}, {-DBL_MAX, -DBL_MAX}};
418 subg = GD_clust(g)[i];
419
420 do_graph_label(subg);
421 dn = mkDeriveNode(dg, agnameof(subg));
422 ND_clust(dn) = subg;
423 ND_id(dn) = id++;
424 if (infop->G_coord)
425 chkPos(subg, dn, infop, &fix_bb);
426 for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) {
427 DNODE(n) = dn;
428 }
429 if (ND_pinned(dn)) {
430 ND_pos(dn)[0] = (fix_bb.LL.x + fix_bb.UR.x) / 2;
431 ND_pos(dn)[1] = (fix_bb.LL.y + fix_bb.UR.y) / 2;
432 }
433 }
434
435 /* create derived nodes from remaining nodes */
436 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
437 if (!DNODE(n)) {
438 if (PARENT(n) && PARENT(n) != GPARENT(g)) {
439 agerrorf("node \"%s\" is contained in two non-comparable clusters \"%s\" and \"%s\"\n", agnameof(n), agnameof(g), agnameof(PARENT(n)));
440 return NULL;
441 }
442 PARENT(n) = g;
443 if (IS_CLUST_NODE(n))
444 continue;
445 dn = mkDeriveNode(dg, agnameof(n));
446 DNODE(n) = dn;
447 ND_id(dn) = id++;
448 ND_width(dn) = ND_width(n);
449 ND_height(dn) = ND_height(n);
450 ND_lw(dn) = ND_lw(n);
451 ND_rw(dn) = ND_rw(n);
452 ND_ht(dn) = ND_ht(n);
453 ND_shape(dn) = ND_shape(n);
455 if (ND_pinned(n)) {
456 ND_pos(dn)[0] = ND_pos(n)[0];
457 ND_pos(dn)[1] = ND_pos(n)[1];
458 ND_pinned(dn) = ND_pinned(n);
459 }
460 ANODE(dn) = n;
461 }
462 }
463
464 /* add edges */
465 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
466 edge_t *e;
467 node_t *hd;
468 node_t *tl = DNODE(n);
469 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
470 hd = DNODE(aghead(e));
471 if (hd == tl)
472 continue;
473 if (hd > tl)
474 de = agedge(dg, tl, hd, NULL,1);
475 else
476 de = agedge(dg, hd, tl, NULL,1);
477 agbindrec(de, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true);
478 ED_dist(de) = ED_dist(e);
479 ED_factor(de) = ED_factor(e);
480 /* fprintf (stderr, "edge %s -- %s\n", tl->name, hd->name); */
481 WDEG(hd)++;
482 WDEG(tl)++;
483 if (NEW_EDGE(de)) {
484 DEG(hd)++;
485 DEG(tl)++;
486 }
487 addEdge(de, e);
488 }
489 }
490
491 /* transform ports */
492 if ((pp = PORTS(g))) {
493 bport_t *pq;
494 node_t *m;
495 int sz = NPORTS(g);
496
497 /* freed in freeDeriveGraph */
498 PORTS(dg) = pq = gv_calloc(sz + 1, sizeof(bport_t));
499 sz = 0;
500 while (pp->e) {
501 m = DNODE(pp->n);
502 /* Create port in derived graph only if hooks to internal node */
503 if (m) {
504 dn = mkDeriveNode(dg, portName(g, pp));
505 sz++;
506 ND_id(dn) = id++;
507 if (dn > m)
508 de = agedge(dg, m, dn, NULL,1);
509 else
510 de = agedge(dg, dn, m, NULL,1);
511 agbindrec(de, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true);
512 ED_dist(de) = ED_dist(pp->e);
513 ED_factor(de) = ED_factor(pp->e);
514 addEdge(de, pp->e);
515 WDEG(dn)++;
516 WDEG(m)++;
517 DEG(dn)++; /* ports are unique, so this will be the first and */
518 DEG(m)++; /* only time the edge is touched. */
519 pq->n = dn;
520 pq->alpha = pp->alpha;
521 pq->e = de;
522 pq++;
523 }
524 pp++;
525 }
526 NPORTS(dg) = sz;
527 }
528
529 return dg;
530}
531
532/* ecmp:
533 * Sort edges by angle, then distance.
534 */
535static int ecmp(const void *v1, const void *v2)
536{
537 const erec *e1 = v1;
538 const erec *e2 = v2;
539 if (e1->alpha > e2->alpha)
540 return 1;
541 else if (e1->alpha < e2->alpha)
542 return -1;
543 else if (e1->dist2 > e2->dist2)
544 return 1;
545 else if (e1->dist2 < e2->dist2)
546 return -1;
547 else
548 return 0;
549}
550
551#define ANG (M_PI/90) /* Maximum angular change: 2 degrees */
552
553/* getEdgeList:
554 * Generate list of edges in derived graph g using
555 * node n. The list is in counterclockwise order.
556 * This, of course, assumes we have an initial layout for g.
557 */
559{
560 int deg = DEG(n);
561 int i;
562 double dx, dy;
563 edge_t *e;
564 node_t *m;
565
566 /* freed in expandCluster */
567 erec *erecs = gv_calloc(deg + 1, sizeof(erec));
568 i = 0;
569 for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
570 if (aghead(e) == n)
571 m = agtail(e);
572 else
573 m = aghead(e);
574 dx = ND_pos(m)[0] - ND_pos(n)[0];
575 dy = ND_pos(m)[1] - ND_pos(n)[1];
576 erecs[i].e = e;
577 erecs[i].alpha = atan2(dy, dx);
578 erecs[i].dist2 = dx * dx + dy * dy;
579 i++;
580 }
581 assert(i == deg);
582 qsort(erecs, deg, sizeof(erec), ecmp);
583
584 /* ensure no two angles are equal */
585 if (deg >= 2) {
586 int j;
587 double a, inc, delta, bnd;
588
589 i = 0;
590 while (i < deg - 1) {
591 a = erecs[i].alpha;
592 j = i + 1;
593 while (j < deg && erecs[j].alpha == a)
594 j++;
595 if (j == i + 1)
596 i = j;
597 else {
598 if (j == deg)
599 bnd = M_PI; /* all values equal up to end */
600 else
601 bnd = erecs[j].alpha;
602 delta = fmin((bnd - a) / (j - i), ANG);
603 inc = 0;
604 for (; i < j; i++) {
605 erecs[i].alpha += inc;
606 inc += delta;
607 }
608 }
609 }
610 }
611
612 return erecs;
613}
614
615/* genPorts:
616 * Given list of edges with node n in derived graph, add corresponding
617 * ports to port list pp, starting at index idx. Return next index.
618 * If an edge in the derived graph corresponds to multiple real edges,
619 * add them in order if address of n is smaller than other node address.
620 * Otherwise, reverse order.
621 * Attach angles. The value bnd gives next angle after er->alpha.
622 */
623static int
624genPorts(node_t * n, erec * er, bport_t * pp, int idx, double bnd)
625{
626 node_t *other;
627 int cnt;
628 edge_t *e = er->e;
629 edge_t *el;
630 edge_t **ep;
631 double angle, delta;
632 int i, j, inc;
633
634 cnt = ED_count(e);
635
636 if (aghead(e) == n)
637 other = agtail(e);
638 else
639 other = aghead(e);
640
641 delta = fmin((bnd - er->alpha) / cnt, ANG);
642 angle = er->alpha;
643
644 if (n < other) {
645 i = idx;
646 inc = 1;
647 } else {
648 i = idx + cnt - 1;
649 inc = -1;
650 angle += delta * (cnt - 1);
651 delta = -delta;
652 }
653
654 ep = (edge_t **)ED_to_virt(e);
655 for (j = 0; j < ED_count(e); j++, ep++) {
656 el = *ep;
657 pp[i].e = el;
658 pp[i].n = DNODE(agtail(el)) == n ? agtail(el) : aghead(el);
659 pp[i].alpha = angle;
660 i += inc;
661 angle += delta;
662 }
663 return (idx + cnt);
664}
665
666/* expandCluster;
667 * Given positioned derived graph cg with node n which corresponds
668 * to a cluster, generate a graph containing the interior of the
669 * cluster, plus port information induced by the layout of cg.
670 * Basically, we use the cluster subgraph to which n corresponds,
671 * attached with port information.
672 */
674{
675 erec *es;
676 erec *ep;
677 erec *next;
678 graph_t *sg = ND_clust(n);
679 int sz = WDEG(n);
680 int idx = 0;
681 double bnd;
682
683 if (sz != 0) {
684 /* freed in cleanup_subgs */
685 bport_t *pp = gv_calloc(sz + 1, sizeof(bport_t));
686
687 /* create sorted list of edges of n */
688 es = ep = getEdgeList(n, cg);
689
690 /* generate ports from edges */
691 while (ep->e) {
692 next = ep + 1;
693 if (next->e)
694 bnd = next->alpha;
695 else
696 bnd = 2 * M_PI + es->alpha;
697 idx = genPorts(n, ep, pp, idx, bnd);
698 ep = next;
699 }
700 assert(idx == sz);
701
702 PORTS(sg) = pp;
703 NPORTS(sg) = sz;
704 free(es);
705 }
706 return sg;
707}
708
709/* setClustNodes:
710 * At present, cluster nodes are not assigned a position during layout,
711 * but positioned in the center of its associated cluster. Because the
712 * dummy edge associated with a cluster node may not occur at a sufficient
713 * level of cluster, the edge may not be used during layout and we cannot
714 * therefore rely find these nodes via ports.
715 *
716 * In this implementation, we just do a linear pass over all nodes in the
717 * root graph. At some point, we may use a better method, like having each
718 * cluster contain its list of cluster nodes, or have the graph keep a list.
719 *
720 * As nodes, we need to assign cluster nodes the coordinates in the
721 * coordinates of its cluster p. Note that p's bbox is in its parent's
722 * coordinates.
723 *
724 * If routing, we may decide to place on cluster boundary,
725 * and use polyline.
726 */
727static void
729{
730 boxf bb;
731 graph_t* p;
732 pointf ctr;
733 node_t *n;
734 double w, h, h_pts;
735 double h2, w2;
736 pointf *vertices;
737
738 for (n = agfstnode(root); n; n = agnxtnode(root, n)) {
739 if (!IS_CLUST_NODE(n)) continue;
740
741 p = PARENT(n);
742 bb = BB(p); /* bbox in parent cluster's coordinates */
743 w = bb.UR.x - bb.LL.x;
744 h = bb.UR.y - bb.LL.y;
745 ctr.x = w / 2.0;
746 ctr.y = h / 2.0;
747 w2 = INCH2PS(w / 2.0);
748 h2 = INCH2PS(h / 2.0);
749 h_pts = INCH2PS(h);
750 ND_pos(n)[0] = ctr.x;
751 ND_pos(n)[1] = ctr.y;
752 ND_width(n) = w;
753 ND_height(n) = h;
756 ND_outline_width(n) = w + penwidth;
758 /* ND_xsize(n) = POINTS(w); */
759 ND_lw(n) = ND_rw(n) = w2;
760 ND_ht(n) = h_pts;
761
762 vertices = ((polygon_t *) ND_shape_info(n))->vertices;
763 vertices[0].x = ND_rw(n);
764 vertices[0].y = h2;
765 vertices[1].x = -ND_lw(n);
766 vertices[1].y = h2;
767 vertices[2].x = -ND_lw(n);
768 vertices[2].y = -h2;
769 vertices[3].x = ND_rw(n);
770 vertices[3].y = -h2;
771 // allocate extra vertices representing the outline, i.e., the outermost
772 // periphery with penwidth taken into account
773 vertices[4].x = ND_rw(n) + penwidth / 2;
774 vertices[4].y = h2 + penwidth / 2;
775 vertices[5].x = -ND_lw(n) - penwidth / 2;
776 vertices[5].y = h2 + penwidth / 2;
777 vertices[6].x = -ND_lw(n) - penwidth / 2;
778 vertices[6].y = -h2 - penwidth / 2;
779 vertices[7].x = ND_rw(n) + penwidth / 2;
780 vertices[7].y = -h2 - penwidth / 2;
781 }
782}
783
784/* layout:
785 * Given g with ports:
786 * Derive g' from g by reducing clusters to points (deriveGraph)
787 * Compute connected components of g' (findCComp)
788 * For each cc of g':
789 * Layout cc (tLayout)
790 * For each node n in cc of g' <-> cluster c in g:
791 * Add ports based on layout of cc to get c' (expandCluster)
792 * Layout c' (recursion)
793 * Remove ports from cc
794 * Expand nodes of cc to reflect size of c' (xLayout)
795 * Pack connected components to get layout of g (putGraphs)
796 * Translate layout so that bounding box of layout + margin
797 * has the origin as LL corner.
798 * Set position of top level clusters and real nodes.
799 * Set bounding box of graph
800 *
801 * TODO:
802 *
803 * Possibly should modify so that only do connected components
804 * on top-level derived graph. Unconnected parts of a cluster
805 * could just rattle within cluster boundaries. This may mix
806 * up components but give a tighter packing.
807 *
808 * Add edges per components to get better packing, rather than
809 * wait until the end.
810 */
811static int layout(graph_t * g, layout_info * infop)
812{
813 pointf *pts = NULL;
814 graph_t *dg;
815 node_t *dn;
816 node_t *n;
817 graph_t *cg;
818 graph_t *sg;
819 graph_t **cc;
820 graph_t **pg;
821 int pinned;
822 xparams xpms;
823
824#ifdef DEBUG
825 incInd();
826#endif
827 if (Verbose) {
828#ifdef DEBUG
829 prIndent();
830#endif
831 fprintf (stderr, "layout %s\n", agnameof(g));
832 }
833 /* initialize derived node pointers */
834 for (n = agfstnode(g); n; n = agnxtnode(g, n))
835 DNODE(n) = 0;
836
837 dg = deriveGraph(g, infop);
838 if (dg == NULL) {
839 return -1;
840 }
841 size_t c_cnt;
842 cc = pg = findCComp(dg, &c_cnt, &pinned);
843
844 while ((cg = *pg++)) {
845 node_t* nxtnode;
846 fdp_tLayout(cg, &xpms);
847 for (n = agfstnode(cg); n; n = nxtnode) {
848 nxtnode = agnxtnode(cg, n);
849 if (ND_clust(n)) {
850 pointf pt;
851 sg = expandCluster(n, cg); /* attach ports to sg */
852 int r = layout(sg, infop);
853 if (r != 0) {
854 return r;
855 }
856 ND_width(n) = BB(sg).UR.x;
857 ND_height(n) = BB(sg).UR.y;
858 pt.x = POINTS_PER_INCH * BB(sg).UR.x;
859 pt.y = POINTS_PER_INCH * BB(sg).UR.y;
860 ND_rw(n) = ND_lw(n) = pt.x/2;
861 ND_ht(n) = pt.y;
862 } else if (IS_PORT(n))
863 agdelete(cg, n); /* remove ports from component */
864 }
865
866 /* Remove overlaps */
867 if (agnnodes(cg) >= 2) {
868 if (g == infop->rootg)
869 normalize (cg);
870 fdp_xLayout(cg, &xpms);
871 }
872 }
873
874 /* At this point, each connected component has its nodes correctly
875 * positioned. If we have multiple components, we pack them together.
876 * All nodes will be moved to their new positions.
877 * NOTE: packGraphs uses nodes in components, so if port nodes are
878 * not removed, it won't work.
879 */
880 /* Handle special cases well: no ports to real internal nodes
881 * Place cluster edges separately, after layout.
882 * How to combine parts, especially with disparate components?
883 */
884 if (c_cnt > 1) {
885 bool *bp;
886 if (pinned) {
887 bp = gv_calloc(c_cnt, sizeof(bool));
888 bp[0] = true;
889 } else
890 bp = NULL;
891 infop->pack.fixed = bp;
892 pts = putGraphs(c_cnt, cc, NULL, &infop->pack);
893 free(bp);
894 } else {
895 pts = NULL;
896 if (c_cnt == 1)
897 compute_bb(cc[0]);
898 }
899
900 /* set bounding box of dg and reposition nodes */
901 finalCC(dg, c_cnt, cc, pts, g, infop);
902 free (pts);
903
904 /* record positions from derived graph to input graph */
905 /* At present, this does not record port node info */
906 /* In fact, as noted above, we have removed port nodes */
907 for (dn = agfstnode(dg); dn; dn = agnxtnode(dg, dn)) {
908 if ((sg = ND_clust(dn))) {
909 BB(sg).LL.x = ND_pos(dn)[0] - ND_width(dn) / 2;
910 BB(sg).LL.y = ND_pos(dn)[1] - ND_height(dn) / 2;
911 BB(sg).UR.x = BB(sg).LL.x + ND_width(dn);
912 BB(sg).UR.y = BB(sg).LL.y + ND_height(dn);
913 } else if ((n = ANODE(dn))) {
914 ND_pos(n)[0] = ND_pos(dn)[0];
915 ND_pos(n)[1] = ND_pos(dn)[1];
916 }
917 }
918 BB(g) = BB(dg);
919#ifdef DEBUG
920 if (g == infop->rootg)
921 dump(g, 1);
922#endif
923
924 /* clean up temp graphs */
925 freeDerivedGraph(dg, cc);
926 free(cc);
927 if (Verbose) {
928#ifdef DEBUG
929 prIndent ();
930#endif
931 fprintf (stderr, "end %s\n", agnameof(g));
932 }
933#ifdef DEBUG
934 decInd();
935#endif
936
937 return 0;
938}
939
940/* setBB;
941 * Set point box g->bb from inch box BB(g).
942 */
943static void setBB(graph_t * g)
944{
945 int i;
946 boxf bb;
947
948 bb.LL.x = POINTS_PER_INCH * BB(g).LL.x;
949 bb.LL.y = POINTS_PER_INCH * BB(g).LL.y;
950 bb.UR.x = POINTS_PER_INCH * BB(g).UR.x;
951 bb.UR.y = POINTS_PER_INCH * BB(g).UR.y;
952 GD_bb(g) = bb;
953 for (i = 1; i <= GD_n_cluster(g); i++) {
954 setBB(GD_clust(g)[i]);
955 }
956}
957
958/* init_info:
959 * Initialize graph-dependent information and
960 * state variable.s
961 */
962static void init_info(graph_t * g, layout_info * infop)
963{
964 infop->G_coord = agattr(g, AGRAPH, "coords", NULL);
965 infop->G_width = agattr(g, AGRAPH, "width", NULL);
966 infop->G_height = agattr(g, AGRAPH, "height", NULL);
967 infop->rootg = g;
968 infop->gid = 0;
969 infop->pack.mode = getPackInfo(g, l_node, CL_OFFSET / 2, &infop->pack);
970}
971
972/* mkClusters:
973 * Attach list of immediate child clusters.
974 * NB: By convention, the indexing starts at 1.
975 * If pclist is NULL, the graph is the root graph or a cluster
976 * If pclist is non-NULL, we are recursively scanning a non-cluster
977 * subgraph for cluster children.
978 */
979static void
980mkClusters (graph_t * g, clist_t* pclist, graph_t* parent)
981{
982 graph_t* subg;
983 clist_t list = {0};
984 clist_t* clist;
985
986 if (pclist == NULL) {
987 // [0] is empty. The clusters are in [1..cnt].
988 clist_append(&list, NULL);
989 clist = &list;
990 }
991 else
992 clist = pclist;
993
994 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg))
995 {
996 if (is_a_cluster(subg)) {
997 agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
998 GD_alg(subg) = gv_alloc(sizeof(gdata)); // freed in cleanup_subgs
999 GD_ndim(subg) = GD_ndim(agroot(parent));
1000 LEVEL(subg) = LEVEL(parent) + 1;
1001 GPARENT(subg) = parent;
1002 clist_append(clist, subg);
1003 mkClusters(subg, NULL, subg);
1004 }
1005 else {
1006 mkClusters(subg, clist, parent);
1007 }
1008 }
1009 if (pclist == NULL) {
1010 assert(clist_size(&list) - 1 <= INT_MAX);
1011 GD_n_cluster(g) = (int)(clist_size(&list) - 1);
1012 if (clist_size(&list) > 1) {
1013 clist_shrink_to_fit(&list);
1014 GD_clust(g) = clist_detach(&list);
1015 } else {
1016 clist_free(&list);
1017 }
1018 }
1019}
1020
1021static void fdp_init_graph(Agraph_t * g)
1022{
1024 GD_alg(g) = gv_alloc(sizeof(gdata)); // freed in cleanup_graph
1025 GD_ndim(agroot(g)) = late_int(g, agattr(g,AGRAPH, "dim", NULL), 2, 2);
1026 Ndim = GD_ndim(agroot(g)) = MIN(GD_ndim(agroot(g)), MAXDIM);
1027
1028 mkClusters (g, NULL, g);
1029 fdp_initParams(g);
1031}
1032
1033static int fdpLayout(graph_t * g)
1034{
1036
1037 init_info(g, &info);
1038 int r = layout(g, &info);
1039 if (r != 0) {
1040 return r;
1041 }
1042 setClustNodes(g);
1043 evalPositions(g,g);
1044
1045 /* Set bbox info for g and all clusters. This is needed for
1046 * spline drawing. We already know the graph bbox is at the origin.
1047 * On return from spline drawing, all bounding boxes should be correct.
1048 */
1049 setBB(g);
1050
1051 return 0;
1052}
1053
1054static void
1056{
1057 int trySplines = 0;
1058 int et = EDGE_TYPE(g);
1059
1060 if (et > EDGETYPE_ORTHO) {
1061 if (et == EDGETYPE_COMPOUND) {
1062 trySplines = splineEdges(g, compoundEdges, EDGETYPE_SPLINE);
1063 /* When doing the edges again, accept edges done by compoundEdges */
1064 if (trySplines)
1065 Nop = 2;
1066 }
1067 if (trySplines || et != EDGETYPE_COMPOUND) {
1068 if (HAS_CLUST_EDGE(g)) {
1069 agwarningf(
1070 "splines and cluster edges not supported - using line segments\n");
1071 et = EDGETYPE_LINE;
1072 } else {
1073 spline_edges1(g, et);
1074 }
1075 }
1076 Nop = 0;
1077 }
1078 if (State < GVSPLINES)
1079 spline_edges1(g, et);
1080}
1081
1083{
1084 double save_scale = PSinputscale;
1085
1087 fdp_init_graph(g);
1088 if (fdpLayout(g) != 0) {
1089 return;
1090 }
1092
1093 if (EDGE_TYPE(g) != EDGETYPE_NONE) fdpSplines (g);
1094
1095 gv_postprocess(g, 0);
1096 PSinputscale = save_scale;
1097}
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:33
double late_double(void *obj, attrsym_t *attr, double defaultValue, double minimum)
Definition utils.c:48
double get_inputscale(graph_t *g)
Definition utils.c:71
void compute_bb(graph_t *g)
Definition utils.c:628
bool is_a_cluster(Agraph_t *g)
Definition utils.c:690
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:70
#define POINTS(a_inches)
Definition geom.h:68
#define POINTS_PER_INCH
Definition geom.h:64
#define INCH2PS(a_inches)
Definition geom.h:69
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:210
int agnnodes(Agraph_t *g)
Definition graph.c:169
Agsym_t * agattr(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up attributes of a graph
Definition attr.c:338
int agxset(void *obj, Agsym_t *sym, const char *value)
Definition attr.c:478
char * agxget(void *obj, Agsym_t *sym)
Definition attr.c:455
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Definition edge.c:260
#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:880
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:881
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:102
#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:285
#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:145
#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:158
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
Agraph_t * agparent(Agraph_t *g)
Definition subg.c:91
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:78
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:83
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:558
static void fdpSplines(graph_t *g)
Definition layout.c:1055
static void setBB(graph_t *g)
Definition layout.c:943
static void init_info(graph_t *g, layout_info *infop)
Definition layout.c:962
static void fdp_init_graph(Agraph_t *g)
Definition layout.c:1021
static char * portName(graph_t *g, bport_t *p)
Definition layout.c:286
void fdp_layout(graph_t *g)
Definition layout.c:1082
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:624
static int ecmp(const void *v1, const void *v2)
Definition layout.c:535
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:673
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:1033
#define ANG
Definition layout.c:551
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:728
static void mkClusters(graph_t *g, clist_t *pclist, graph_t *parent)
Definition layout.c:980
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:811
static graph_t * deriveGraph(graph_t *g, layout_info *infop)
Definition layout.c:387
#define DEFINE_LIST(name, type)
Definition list.h:26
#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:1281
pointf * putGraphs(size_t ng, Agraph_t **gs, Agraph_t *root, pack_info *pinfo)
Definition pack.c:888
support for connected components
@ l_node
Definition pack.h:55
void gv_postprocess(Agraph_t *g, int allowTranslation)
Definition postproc.c:597
#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:425
string attribute descriptor symbol in Agattr_s.dict
Definition cgraph.h:637
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