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