layout.c 27 KB

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