clusteredges.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /* clusteredges.c:
  11. * Written by Emden R. Gansner
  12. *
  13. * Code for handling spline edges around clusters.
  14. */
  15. /* uses PRIVATE interface */
  16. #define FDP_PRIVATE 1
  17. #include "config.h"
  18. #include <assert.h>
  19. #include <cgraph/list.h>
  20. #include <fdpgen/clusteredges.h>
  21. #include <fdpgen/fdp.h>
  22. #include <limits.h>
  23. #include <neatogen/neatoprocs.h>
  24. #include <pathplan/vispath.h>
  25. #include <pack/pack.h>
  26. #include <stdbool.h>
  27. #include <util/alloc.h>
  28. DEFINE_LIST(objlist, Ppoly_t*)
  29. #if defined(DEBUG) && DEBUG > 1
  30. static void dumpObj(Ppoly_t * p)
  31. {
  32. Ppoint_t pt;
  33. for (size_t j = 0; j < p->pn; j++) {
  34. pt = p->ps[j];
  35. fprintf(stderr, " %.5g %.5g", pt.x, pt.y);
  36. }
  37. fputs("\n", stderr);
  38. }
  39. static void dumpObjlist(const objlist_t *l) {
  40. for (size_t i = 0; i < objlist_size(l); i++) {
  41. dumpObj(objlist_get(l, i));
  42. }
  43. }
  44. #endif
  45. /* makeClustObs:
  46. * Create an obstacle corresponding to a cluster's bbox.
  47. */
  48. static Ppoly_t *makeClustObs(graph_t * g, expand_t* pm)
  49. {
  50. Ppoly_t *obs = gv_alloc(sizeof(Ppoly_t));
  51. boxf bb;
  52. boxf newbb;
  53. Ppoint_t ctr;
  54. bb = GD_bb(g);
  55. obs->pn = 4;
  56. obs->ps = gv_calloc(4, sizeof(Ppoint_t));
  57. ctr.x = (bb.UR.x + bb.LL.x) / 2.0;
  58. ctr.y = (bb.UR.y + bb.LL.y) / 2.0;
  59. if (pm->doAdd) {
  60. newbb.UR.x = bb.UR.x + pm->x;
  61. newbb.UR.y = bb.UR.y + pm->y;
  62. newbb.LL.x = bb.LL.x - pm->x;
  63. newbb.LL.y = bb.LL.y - pm->y;
  64. }
  65. else {
  66. double deltax = pm->x - 1.0;
  67. double deltay = pm->y - 1.0;
  68. newbb.UR.x = pm->x * bb.UR.x - deltax * ctr.x;
  69. newbb.UR.y = pm->y * bb.UR.y - deltay * ctr.y;
  70. newbb.LL.x = pm->x * bb.LL.x - deltax * ctr.x;
  71. newbb.LL.y = pm->y * bb.LL.y - deltay * ctr.y;
  72. }
  73. /* CW order */
  74. obs->ps[0].x = newbb.LL.x;
  75. obs->ps[0].y = newbb.LL.y;
  76. obs->ps[1].x = newbb.LL.x;
  77. obs->ps[1].y = newbb.UR.y;
  78. obs->ps[2].x = newbb.UR.x;
  79. obs->ps[2].y = newbb.UR.y;
  80. obs->ps[3].x = newbb.UR.x;
  81. obs->ps[3].y = newbb.LL.y;
  82. return obs;
  83. }
  84. /* addGraphObjs:
  85. * Add all top-level clusters and nodes with g as their smallest
  86. * containing graph to the list l.
  87. * Don't add any objects equal to tex or hex.
  88. * Return the list.
  89. */
  90. static void
  91. addGraphObjs(objlist_t *l, graph_t *g, void *tex, void *hex, expand_t *pm) {
  92. node_t *n;
  93. graph_t *sg;
  94. int i;
  95. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  96. if (PARENT(n) == g && n != tex && n != hex && !IS_CLUST_NODE(n)) {
  97. objlist_append(l, makeObstacle(n, pm, false));
  98. }
  99. }
  100. for (i = 1; i <= GD_n_cluster(g); i++) {
  101. sg = GD_clust(g)[i];
  102. if (sg != tex && sg != hex) {
  103. objlist_append(l, makeClustObs(sg, pm));
  104. }
  105. }
  106. }
  107. /* raiseLevel:
  108. * Add barrier objects for node n, in graph *gp of level maxlvl, up to
  109. * level minlvl.
  110. * Assume maxlvl > minlvl.
  111. * Return appended list, plus pass back last cluster processed in gp.
  112. */
  113. static void
  114. raiseLevel(objlist_t *l, int maxlvl, void *ex, int minlvl, graph_t **gp,
  115. expand_t* pm)
  116. {
  117. graph_t *g = *gp;
  118. int i;
  119. for (i = maxlvl; i > minlvl; i--) {
  120. addGraphObjs(l, g, ex, NULL, pm);
  121. ex = g;
  122. g = GPARENT(g);
  123. }
  124. *gp = ex;
  125. }
  126. /* objectList:
  127. * Create array of all objects (nodes and clusters) to be avoided
  128. * when routing edge e. Make sure it never adds the endpoints of the
  129. * edge, or any graph containing the endpoints.
  130. * Return the list.
  131. * Assume e is not a loop.
  132. */
  133. static objlist_t objectList(edge_t *ep, expand_t *pm) {
  134. node_t *h = aghead(ep);
  135. node_t *t = agtail(ep);
  136. graph_t *hg = PARENT(h);
  137. graph_t *tg = PARENT(t);
  138. int hlevel;
  139. int tlevel;
  140. void *hex; /* Objects to be excluded from list */
  141. void *tex;
  142. objlist_t list = {0};
  143. /* If either endpoint is a cluster node, we move up one level */
  144. if (IS_CLUST_NODE(h)) {
  145. hex = hg;
  146. hg = GPARENT(hg);
  147. } else
  148. hex = h;
  149. if (IS_CLUST_NODE(t)) {
  150. tex = tg;
  151. tg = GPARENT(tg);
  152. } else
  153. tex = t;
  154. hlevel = LEVEL(hg);
  155. tlevel = LEVEL(tg);
  156. if (hlevel > tlevel) {
  157. raiseLevel(&list, hlevel, hex, tlevel, &hg, pm);
  158. hex = hg;
  159. hg = GPARENT(hg);
  160. } else if (tlevel > hlevel) {
  161. raiseLevel(&list, tlevel, tex, hlevel, &tg, pm);
  162. tex = tg;
  163. tg = GPARENT(tg);
  164. }
  165. /* hg and tg always have the same level */
  166. while (hg != tg) {
  167. addGraphObjs(&list, hg, NULL, hex, pm);
  168. addGraphObjs(&list, tg, tex, NULL, pm);
  169. hex = hg;
  170. hg = GPARENT(hg);
  171. tex = tg;
  172. tg = GPARENT(tg);
  173. }
  174. addGraphObjs(&list, tg, tex, hex, pm);
  175. return list;
  176. }
  177. /* compoundEdges:
  178. * Construct edges as splines, avoiding clusters when required.
  179. * We still don't implement spline multiedges, so we just copy
  180. * one spline to all the other edges.
  181. * Returns 0 on success. Failure indicates the obstacle configuration
  182. * for some edge had overlaps.
  183. */
  184. int compoundEdges(graph_t * g, expand_t* pm, int edgetype)
  185. {
  186. (void)edgetype;
  187. node_t *n;
  188. node_t *head;
  189. edge_t *e;
  190. edge_t *e0;
  191. vconfig_t *vconfig = NULL;
  192. int rv = 0;
  193. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  194. for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
  195. head = aghead(e);
  196. if (n == head && ED_count(e)) { /* self arc */
  197. makeSelfArcs(e, GD_nodesep(g));
  198. } else if (ED_count(e)) {
  199. objlist_t objl = objectList(e, pm);
  200. assert(objlist_size(&objl) <= INT_MAX);
  201. objlist_sync(&objl);
  202. if (Plegal_arrangement(objlist_front(&objl), (int)objlist_size(&objl))) {
  203. vconfig = Pobsopen(objlist_front(&objl), (int)objlist_size(&objl));
  204. if (!vconfig) {
  205. agwarningf("compoundEdges: could not construct obstacles - falling back to straight line edges\n");
  206. rv = 1;
  207. objlist_free(&objl);
  208. continue;
  209. }
  210. }
  211. else {
  212. if (rv == 0) {
  213. expand_t margin = sepFactor(g);
  214. int pack = getPack (g, CL_OFFSET, CL_OFFSET);
  215. agwarningf("compoundEdges: nodes touch - falling back to straight line edges\n");
  216. if (pack <= pm->x || pack <= pm->y)
  217. agerr(AGPREV, "pack value %d is smaller than esep (%.03f,%.03f)\n", pack, pm->x, pm->y);
  218. else if (margin.x <= pm->x || margin.y <= pm->y)
  219. agerr(AGPREV, "sep value (%.03f,%.03f) is smaller than esep (%.03f,%.03f)\n",
  220. margin.x, margin.y, pm->x, pm->y);
  221. rv = 1;
  222. }
  223. objlist_free(&objl);
  224. continue;
  225. }
  226. /* For efficiency, it should be possible to copy the spline
  227. * from the first edge to the rest. However, one has to deal
  228. * with change in direction, different arrowheads, labels, etc.
  229. */
  230. for (e0 = e; e0; e0 = ED_to_virt(e0)) {
  231. ED_path(e0) = getPath(e0, vconfig, false);
  232. assert(objlist_size(&objl) <= INT_MAX);
  233. objlist_sync(&objl);
  234. makeSpline(e0, objlist_front(&objl), (int)objlist_size(&objl), false);
  235. }
  236. objlist_free(&objl);
  237. }
  238. }
  239. }
  240. if (vconfig != NULL) {
  241. Pobsclose(vconfig);
  242. }
  243. return rv;
  244. }