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