osageinit.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. /* FIX: handle cluster labels */
  11. /*
  12. * Written by Emden R. Gansner
  13. */
  14. #include <assert.h>
  15. #include <common/render.h>
  16. #include <common/utils.h>
  17. #include <cgraph/list.h>
  18. #include <limits.h>
  19. #include <osage/osage.h>
  20. #include <neatogen/neatoprocs.h>
  21. #include <pack/pack.h>
  22. #include <stdbool.h>
  23. #include <stddef.h>
  24. #include <util/alloc.h>
  25. #include <util/startswith.h>
  26. #define DFLT_SZ 18
  27. #define PARENT(n) ((Agraph_t*)ND_alg(n))
  28. static void
  29. indent (int i)
  30. {
  31. for (; i > 0; i--)
  32. fputs (" ", stderr);
  33. }
  34. DEFINE_LIST(clist, Agraph_t*)
  35. static void cluster_init_graph(graph_t * g)
  36. {
  37. Agnode_t *n;
  38. Agedge_t *e;
  39. setEdgeType (g, EDGETYPE_LINE);
  40. Ndim = GD_ndim(g)=2; /* The algorithm only makes sense in 2D */
  41. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  42. neato_init_node (n);
  43. }
  44. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  45. for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
  46. agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //edge custom data
  47. common_init_edge(e);
  48. }
  49. }
  50. }
  51. /* layout:
  52. */
  53. static void
  54. layout (Agraph_t* g, int depth)
  55. {
  56. int i, j, total, nv;
  57. int nvs = 0; /* no. of nodes in subclusters */
  58. Agnode_t* n;
  59. Agraph_t* subg;
  60. pointf* pts;
  61. boxf bb, rootbb;
  62. pointf p;
  63. pack_info pinfo;
  64. pack_mode pmode;
  65. double margin;
  66. Agsym_t* cattr = NULL;
  67. Agsym_t* vattr = NULL;
  68. Agraph_t* root = g->root;
  69. if (Verbose > 1) {
  70. indent (depth);
  71. fprintf (stderr, "layout %s\n", agnameof(g));
  72. }
  73. /* Lay out subclusters */
  74. for (i = 1; i <= GD_n_cluster(g); i++) {
  75. subg = GD_clust(g)[i];
  76. layout (subg, depth+1);
  77. nvs += agnnodes (subg);
  78. }
  79. nv = agnnodes(g);
  80. total = (nv - nvs) + GD_n_cluster(g);
  81. if ((total == 0) && (GD_label(g) == NULL)) {
  82. GD_bb(g).LL.x = GD_bb(g).LL.y = 0;
  83. GD_bb(g).UR.x = GD_bb(g).UR.y = DFLT_SZ;
  84. return;
  85. }
  86. pmode = getPackInfo(g, l_array, DFLT_MARGIN, &pinfo);
  87. if (pmode < l_graph) pinfo.mode = l_graph;
  88. /* add user sort values if necessary */
  89. if ((pinfo.mode == l_array) && (pinfo.flags & PK_USER_VALS)) {
  90. cattr = agattr(root, AGRAPH, "sortv", 0);
  91. vattr = agattr(root, AGNODE, "sortv", 0);
  92. if (cattr || vattr)
  93. pinfo.vals = gv_calloc(total, sizeof(packval_t));
  94. else
  95. agwarningf("Graph %s has array packing with user values but no \"sortv\" attributes are defined.",
  96. agnameof(g));
  97. }
  98. boxf *gs = gv_calloc(total, sizeof(boxf));
  99. void **children = gv_calloc(total, sizeof(void*));
  100. j = 0;
  101. for (i = 1; i <= GD_n_cluster(g); i++) {
  102. subg = GD_clust(g)[i];
  103. gs[j] = GD_bb(subg);
  104. if (pinfo.vals && cattr) {
  105. pinfo.vals[j] = late_int (subg, cattr, 0, 0);
  106. }
  107. children[j++] = subg;
  108. }
  109. if (nv-nvs > 0) {
  110. for (n = agfstnode (g); n; n = agnxtnode (g,n)) {
  111. if (ND_alg(n)) continue;
  112. ND_alg(n) = g;
  113. bb.LL.y = bb.LL.x = 0;
  114. bb.UR.x = ND_xsize(n);
  115. bb.UR.y = ND_ysize(n);
  116. gs[j] = bb;
  117. if (pinfo.vals && vattr) {
  118. pinfo.vals[j] = late_int (n, vattr, 0, 0);
  119. }
  120. children[j++] = n;
  121. }
  122. }
  123. /* pack rectangles */
  124. assert(total >= 0);
  125. pts = putRects((size_t)total, gs, &pinfo);
  126. free (pinfo.vals);
  127. rootbb.LL = (pointf){INT_MAX, INT_MAX};
  128. rootbb.UR = (pointf){-INT_MAX, -INT_MAX};
  129. /* reposition children relative to GD_bb(g) */
  130. for (j = 0; j < total; j++) {
  131. p = pts[j];
  132. bb = gs[j];
  133. bb.LL.x += p.x;
  134. bb.UR.x += p.x;
  135. bb.LL.y += p.y;
  136. bb.UR.y += p.y;
  137. EXPANDBB(rootbb, bb);
  138. if (j < GD_n_cluster(g)) {
  139. subg = children[j];
  140. GD_bb(subg) = bb;
  141. if (Verbose > 1) {
  142. indent (depth);
  143. fprintf (stderr, "%s : %f %f %f %f\n", agnameof(subg), bb.LL.x, bb.LL.y, bb.UR.x, bb.UR.y);
  144. }
  145. }
  146. else {
  147. n = children[j];
  148. ND_coord(n) = mid_pointf (bb.LL, bb.UR);
  149. if (Verbose > 1) {
  150. indent (depth);
  151. fprintf (stderr, "%s : %f %f\n", agnameof(n), ND_coord(n).x, ND_coord(n).y);
  152. }
  153. }
  154. }
  155. if (GD_label(g)) {
  156. double d;
  157. pointf pt = GD_label(g)->dimen;
  158. if (total == 0) {
  159. rootbb.LL.x = 0;
  160. rootbb.LL.y = 0;
  161. rootbb.UR.x = pt.x;
  162. rootbb.UR.y = pt.y;
  163. }
  164. d = pt.x - (rootbb.UR.x - rootbb.LL.x);
  165. if (d > 0) { /* height of label is added below */
  166. d /= 2;
  167. rootbb.LL.x -= d;
  168. rootbb.UR.x += d;
  169. }
  170. }
  171. if (depth > 0)
  172. margin = pinfo.margin/2.0;
  173. else
  174. margin = 0;
  175. rootbb.LL.x -= margin;
  176. rootbb.UR.x += margin;
  177. rootbb.LL.y -= (margin + GD_border(g)[BOTTOM_IX].y);
  178. rootbb.UR.y += (margin + GD_border(g)[TOP_IX].y);
  179. if (Verbose > 1) {
  180. indent (depth);
  181. fprintf (stderr, "%s : %f %f %f %f\n", agnameof(g), rootbb.LL.x, rootbb.LL.y, rootbb.UR.x, rootbb.UR.y);
  182. }
  183. /* Translate so that rootbb.LL is origin.
  184. * This makes the repositioning simpler; we just translate the clusters and nodes in g by
  185. * the final bb.ll of g.
  186. */
  187. for (j = 0; j < total; j++) {
  188. if (j < GD_n_cluster(g)) {
  189. subg = children[j];
  190. bb = GD_bb(subg);
  191. bb.LL = sub_pointf(bb.LL, rootbb.LL);
  192. bb.UR = sub_pointf(bb.UR, rootbb.LL);
  193. GD_bb(subg) = bb;
  194. if (Verbose > 1) {
  195. indent (depth);
  196. fprintf (stderr, "%s : %f %f %f %f\n", agnameof(subg), bb.LL.x, bb.LL.y, bb.UR.x, bb.UR.y);
  197. }
  198. }
  199. else {
  200. n = children[j];
  201. ND_coord(n) = sub_pointf (ND_coord(n), rootbb.LL);
  202. if (Verbose > 1) {
  203. indent (depth);
  204. fprintf (stderr, "%s : %f %f\n", agnameof(n), ND_coord(n).x, ND_coord(n).y);
  205. }
  206. }
  207. }
  208. rootbb.UR = sub_pointf(rootbb.UR, rootbb.LL);
  209. rootbb.LL = sub_pointf(rootbb.LL, rootbb.LL);
  210. GD_bb(g) = rootbb;
  211. if (Verbose > 1) {
  212. indent (depth);
  213. fprintf (stderr, "%s : %f %f %f %f\n", agnameof(g), rootbb.LL.x, rootbb.LL.y, rootbb.UR.x, rootbb.UR.y);
  214. }
  215. free (gs);
  216. free (children);
  217. free (pts);
  218. }
  219. static void
  220. reposition (Agraph_t* g, int depth)
  221. {
  222. boxf sbb, bb = GD_bb(g);
  223. Agnode_t* n;
  224. Agraph_t* subg;
  225. int i;
  226. if (Verbose > 1) {
  227. indent (depth);
  228. fprintf (stderr, "reposition %s\n", agnameof(g));
  229. }
  230. /* translate nodes in g but not in a subcluster */
  231. if (depth) {
  232. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  233. if (PARENT(n) != g)
  234. continue;
  235. ND_coord(n).x += bb.LL.x;
  236. ND_coord(n).y += bb.LL.y;
  237. if (Verbose > 1) {
  238. indent (depth);
  239. fprintf (stderr, "%s : %f %f\n", agnameof(n), ND_coord(n).x, ND_coord(n).y);
  240. }
  241. }
  242. }
  243. /* translate top-level clusters and recurse */
  244. for (i = 1; i <= GD_n_cluster(g); i++) {
  245. subg = GD_clust(g)[i];
  246. if (depth) {
  247. sbb = GD_bb(subg);
  248. sbb.LL.x += bb.LL.x;
  249. sbb.LL.y += bb.LL.y;
  250. sbb.UR.x += bb.LL.x;
  251. sbb.UR.y += bb.LL.y;
  252. if (Verbose > 1) {
  253. indent (depth);
  254. fprintf (stderr, "%s : %f %f %f %f\n", agnameof(subg), sbb.LL.x, sbb.LL.y, sbb.UR.x, sbb.UR.y);
  255. }
  256. GD_bb(subg) = sbb;
  257. }
  258. reposition (subg, depth+1);
  259. }
  260. }
  261. static void
  262. mkClusters (Agraph_t* g, clist_t* pclist, Agraph_t* parent)
  263. {
  264. graph_t* subg;
  265. clist_t list = {0};
  266. clist_t* clist;
  267. if (pclist == NULL) {
  268. // [0] is empty. The clusters are in [1..cnt].
  269. clist_append(&list, NULL);
  270. clist = &list;
  271. }
  272. else
  273. clist = pclist;
  274. for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
  275. if (is_a_cluster(subg)) {
  276. agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
  277. do_graph_label (subg);
  278. clist_append(clist, subg);
  279. mkClusters(subg, NULL, subg);
  280. }
  281. else {
  282. mkClusters(subg, clist, parent);
  283. }
  284. }
  285. if (pclist == NULL) {
  286. assert(clist_size(&list) - 1 <= INT_MAX);
  287. GD_n_cluster(g) = (int)(clist_size(&list) - 1);
  288. if (clist_size(&list) > 1) {
  289. clist_shrink_to_fit(&list);
  290. GD_clust(g) = clist_detach(&list);
  291. } else {
  292. clist_free(&list);
  293. }
  294. }
  295. }
  296. void osage_layout(Agraph_t *g)
  297. {
  298. cluster_init_graph(g);
  299. mkClusters(g, NULL, g);
  300. layout(g, 0);
  301. reposition (g, 0);
  302. if (GD_drawing(g)->ratio_kind) {
  303. Agnode_t* n;
  304. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  305. ND_pos(n)[0] = PS2INCH(ND_coord(n).x);
  306. ND_pos(n)[1] = PS2INCH(ND_coord(n).y);
  307. }
  308. spline_edges0(g, true);
  309. }
  310. else {
  311. int et = EDGE_TYPE (g);
  312. if (et != EDGETYPE_NONE) spline_edges1(g, et);
  313. }
  314. dotneato_postprocess(g);
  315. }
  316. static void cleanup_graphs (Agraph_t *g)
  317. {
  318. graph_t *subg;
  319. int i;
  320. for (i = 1; i <= GD_n_cluster(g); i++) {
  321. subg = GD_clust(g)[i];
  322. free_label(GD_label(subg));
  323. cleanup_graphs (subg);
  324. }
  325. free (GD_clust(g));
  326. }
  327. void osage_cleanup(Agraph_t *g)
  328. {
  329. node_t *n;
  330. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  331. for (edge_t *e = agfstout(g, n); e; e = agnxtout(g, e)) {
  332. gv_cleanup_edge(e);
  333. }
  334. gv_cleanup_node(n);
  335. }
  336. cleanup_graphs(g);
  337. }
  338. /**
  339. * @dir lib/osage
  340. * @brief clustered layout engine, API osage/osage.h
  341. * @ingroup engines
  342. *
  343. * [Osage layout user manual](https://graphviz.org/docs/layouts/osage/)
  344. *
  345. * Other @ref engines
  346. */