| 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 |
/* |
| 13 |
* position(g): set ND_coord(n) (x and y) for all nodes n of g, using GD_rank(g). |
| 14 |
* (the graph may be modified by merging certain edges with a common endpoint.) |
| 15 |
* the coordinates are computed by constructing and ranking an auxiliary graph. |
| 16 |
* then leaf nodes are inserted in the fast graph. cluster boundary nodes are |
| 17 |
* created and correctly separated. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
|
| 22 |
#include <common/geomprocs.h> |
| 23 |
#include <dotgen/dot.h> |
| 24 |
#include <dotgen/aspect.h> |
| 25 |
#include <math.h> |
| 26 |
#include <stdbool.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <util/alloc.h> |
| 29 |
#include <util/gv_math.h> |
| 30 |
|
| 31 |
static int nsiter2(graph_t * g); |
| 32 |
static void create_aux_edges(graph_t * g); |
| 33 |
static void remove_aux_edges(graph_t * g); |
| 34 |
static void set_xcoords(graph_t * g); |
| 35 |
static void set_ycoords(graph_t * g); |
| 36 |
static void set_aspect(graph_t *g); |
| 37 |
static void expand_leaves(graph_t * g); |
| 38 |
static void make_lrvn(graph_t * g); |
| 39 |
static void contain_nodes(graph_t * g); |
| 40 |
static bool idealsize(graph_t * g, double); |
| 41 |
|
| 42 |
#if defined(DEBUG) && DEBUG > 1 |
| 43 |
static void |
| 44 |
dumpNS (graph_t * g) |
| 45 |
{ |
| 46 |
node_t* n = GD_nlist(g); |
| 47 |
elist el; |
| 48 |
edge_t* e; |
| 49 |
|
| 50 |
while (n) { |
| 51 |
el = ND_out(n); |
| 52 |
for (size_t i = 0; i < el.size; i++) { |
| 53 |
e = el.list[i]; |
| 54 |
fprintf (stderr, "%s(%x) -> ", agnameof(agtail(e)),agtail(e)); |
| 55 |
fprintf (stderr, "%s(%x) : %d\n", agnameof(aghead(e)), aghead(e), |
| 56 |
ED_minlen(e)); |
| 57 |
} |
| 58 |
n = ND_next(n); |
| 59 |
} |
| 60 |
} |
| 61 |
#endif |
| 62 |
|
| 63 |
static double |
| 64 |
largeMinlen (double l) |
| 65 |
{ |
| 66 |
agerrorf( |
| 67 |
"Edge length %f larger than maximum %d allowed.\nCheck for overwide " |
| 68 |
"node(s).\n", |
| 69 |
l, INT_MAX); |
| 70 |
return INT_MAX; |
| 71 |
} |
| 72 |
|
| 73 |
/* When source and/or sink nodes are defined, it is possible that |
| 74 |
* after the auxiliary edges are added, the graph may still have 2 or |
| 75 |
* 3 components. To fix this, we put trivial constraints connecting the |
| 76 |
* first items of each rank. |
| 77 |
*/ |
| 78 |
static void |
| 79 |
connectGraph (graph_t* g) |
| 80 |
{ |
| 81 |
int i, j, r; |
| 82 |
bool found; |
| 83 |
node_t* tp; |
| 84 |
node_t* hp; |
| 85 |
node_t* sn; |
| 86 |
edge_t* e; |
| 87 |
rank_t* rp; |
| 88 |
|
| 89 |
for (r = GD_minrank(g); r <= GD_maxrank(g); r++) { |
| 90 |
rp = GD_rank(g)+r; |
| 91 |
found = false; |
| 92 |
tp = NULL; |
| 93 |
for (i = 0; i < rp->n; i++) { |
| 94 |
tp = rp->v[i]; |
| 95 |
if (ND_save_out(tp).list) { |
| 96 |
for (j = 0; (e = ND_save_out(tp).list[j]); j++) { |
| 97 |
if (ND_rank(aghead(e)) > r || ND_rank(agtail(e)) > r) { |
| 98 |
found = true; |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
if (found) break; |
| 103 |
} |
| 104 |
if (ND_save_in(tp).list) { |
| 105 |
for (j = 0; (e = ND_save_in(tp).list[j]); j++) { |
| 106 |
if (ND_rank(agtail(e)) > r || ND_rank(aghead(e)) > r) { |
| 107 |
found = true; |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
if (found) break; |
| 112 |
} |
| 113 |
} |
| 114 |
if (found || !tp) continue; |
| 115 |
tp = rp->v[0]; |
| 116 |
if (r < GD_maxrank(g)) hp = (rp+1)->v[0]; |
| 117 |
else hp = (rp-1)->v[0]; |
| 118 |
assert (hp); |
| 119 |
sn = virtual_node(g); |
| 120 |
ND_node_type(sn) = SLACKNODE; |
| 121 |
make_aux_edge(sn, tp, 0, 0); |
| 122 |
make_aux_edge(sn, hp, 0, 0); |
| 123 |
ND_rank(sn) = MIN(ND_rank(tp), ND_rank(hp)); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void dot_position(graph_t *g) { |
| 128 |
if (GD_nlist(g) == NULL) |
| 129 |
return; /* ignore empty graph */ |
| 130 |
mark_lowclusters(g); /* we could remove from splines.c now */ |
| 131 |
set_ycoords(g); |
| 132 |
if (Concentrate) |
| 133 |
dot_concentrate(g); |
| 134 |
expand_leaves(g); |
| 135 |
if (flat_edges(g)) |
| 136 |
set_ycoords(g); |
| 137 |
create_aux_edges(g); |
| 138 |
if (rank(g, 2, nsiter2(g))) { /* LR balance == 2 */ |
| 139 |
connectGraph (g); |
| 140 |
const int rank_result = rank(g, 2, nsiter2(g)); |
| 141 |
assert(rank_result == 0); |
| 142 |
(void)rank_result; |
| 143 |
} |
| 144 |
set_xcoords(g); |
| 145 |
set_aspect(g); |
| 146 |
remove_aux_edges(g); /* must come after set_aspect since we now |
| 147 |
* use GD_ln and GD_rn for bbox width. |
| 148 |
*/ |
| 149 |
} |
| 150 |
|
| 151 |
static int nsiter2(graph_t * g) |
| 152 |
{ |
| 153 |
int maxiter = INT_MAX; |
| 154 |
char *s; |
| 155 |
|
| 156 |
if ((s = agget(g, "nslimit"))) |
| 157 |
maxiter = scale_clamp(agnnodes(g), atof(s)); |
| 158 |
return maxiter; |
| 159 |
} |
| 160 |
|
| 161 |
static bool go(node_t *u, node_t *v) { |
| 162 |
int i; |
| 163 |
edge_t *e; |
| 164 |
|
| 165 |
if (u == v) |
| 166 |
return true; |
| 167 |
for (i = 0; (e = ND_out(u).list[i]); i++) { |
| 168 |
if (go(aghead(e), v)) |
| 169 |
return true; |
| 170 |
} |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
static bool canreach(node_t *u, node_t *v) { |
| 175 |
return go(u, v); |
| 176 |
} |
| 177 |
|
| 178 |
edge_t *make_aux_edge(node_t * u, node_t * v, double len, int wt) |
| 179 |
{ |
| 180 |
Agedgepair_t* e2 = gv_alloc(sizeof(Agedgepair_t)); |
| 181 |
AGTYPE(&e2->in) = AGINEDGE; |
| 182 |
AGTYPE(&e2->out) = AGOUTEDGE; |
| 183 |
e2->out.base.data = gv_alloc(sizeof(Agedgeinfo_t)); |
| 184 |
edge_t *const e = &e2->out; |
| 185 |
|
| 186 |
agtail(e) = u; |
| 187 |
aghead(e) = v; |
| 188 |
if (len > INT_MAX) |
| 189 |
len = largeMinlen (len); |
| 190 |
ED_minlen(e) = ROUND(len); |
| 191 |
ED_weight(e) = wt; |
| 192 |
fast_edge(e); |
| 193 |
return e; |
| 194 |
} |
| 195 |
|
| 196 |
static void allocate_aux_edges(graph_t * g) |
| 197 |
{ |
| 198 |
int i, j, n_in; |
| 199 |
node_t *n; |
| 200 |
|
| 201 |
/* allocate space for aux edge lists */ |
| 202 |
for (n = GD_nlist(g); n; n = ND_next(n)) { |
| 203 |
ND_save_in(n) = ND_in(n); |
| 204 |
ND_save_out(n) = ND_out(n); |
| 205 |
for (i = 0; ND_out(n).list[i]; i++); |
| 206 |
for (j = 0; ND_in(n).list[j]; j++); |
| 207 |
n_in = i + j; |
| 208 |
alloc_elist(n_in + 3, ND_in(n)); |
| 209 |
alloc_elist(3, ND_out(n)); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
make_LR_constraints(graph_t * g) |
| 215 |
{ |
| 216 |
int i, j; |
| 217 |
int m0; |
| 218 |
double width; |
| 219 |
int sep[2]; |
| 220 |
int nodesep; /* separation between nodes on same rank */ |
| 221 |
edge_t *e, *e0, *e1; |
| 222 |
node_t *u, *v, *t0, *h0; |
| 223 |
rank_t *rank = GD_rank(g); |
| 224 |
|
| 225 |
/* Use smaller separation on odd ranks if g has edge labels */ |
| 226 |
if (GD_has_labels(g->root) & EDGE_LABEL) { |
| 227 |
sep[0] = GD_nodesep(g); |
| 228 |
sep[1] = 5; |
| 229 |
} |
| 230 |
else { |
| 231 |
sep[1] = sep[0] = GD_nodesep(g); |
| 232 |
} |
| 233 |
/* make edges to constrain left-to-right ordering */ |
| 234 |
for (i = GD_minrank(g); i <= GD_maxrank(g); i++) { |
| 235 |
double last = ND_rank(rank[i].v[0]) = 0; |
| 236 |
nodesep = sep[i & 1]; |
| 237 |
for (j = 0; j < rank[i].n; j++) { |
| 238 |
u = rank[i].v[j]; |
| 239 |
ND_mval(u) = ND_rw(u); /* keep it somewhere safe */ |
| 240 |
if (ND_other(u).size > 0) { /* compute self size */ |
| 241 |
/* FIX: dot assumes all self-edges go to the right. This |
| 242 |
* is no longer true, though makeSelfEdge still attempts to |
| 243 |
* put as many as reasonable on the right. The dot code |
| 244 |
* should be modified to allow a box reflecting the placement |
| 245 |
* of all self-edges, and use that to reposition the nodes. |
| 246 |
* Note that this would not only affect left and right |
| 247 |
* positioning but may also affect interrank spacing. |
| 248 |
*/ |
| 249 |
double sw = 0; // self width |
| 250 |
for (size_t k = 0; (e = ND_other(u).list[k]); k++) { |
| 251 |
if (agtail(e) == aghead(e)) { |
| 252 |
sw += selfRightSpace (e); |
| 253 |
} |
| 254 |
} |
| 255 |
ND_rw(u) += sw; /* increment to include self edges */ |
| 256 |
} |
| 257 |
v = rank[i].v[j + 1]; |
| 258 |
if (v) { |
| 259 |
width = ND_rw(u) + ND_lw(v) + nodesep; |
| 260 |
e0 = make_aux_edge(u, v, width, 0); |
| 261 |
last = (ND_rank(v) = last + width); |
| 262 |
} |
| 263 |
|
| 264 |
/* constraints from labels of flat edges on previous rank */ |
| 265 |
if ((e = ND_alg(u))) { |
| 266 |
e0 = ND_save_out(u).list[0]; |
| 267 |
e1 = ND_save_out(u).list[1]; |
| 268 |
if (ND_order(aghead(e0)) > ND_order(aghead(e1))) { |
| 269 |
SWAP(&e0, &e1); |
| 270 |
} |
| 271 |
m0 = ED_minlen(e) * GD_nodesep(g) / 2; |
| 272 |
double m1 = m0 + ND_rw(aghead(e0)) + ND_lw(agtail(e0)); |
| 273 |
/* these guards are needed because the flat edges |
| 274 |
* work very poorly with cluster layout */ |
| 275 |
if (!canreach(agtail(e0), aghead(e0))) |
| 276 |
make_aux_edge(aghead(e0), agtail(e0), m1, |
| 277 |
ED_weight(e)); |
| 278 |
m1 = m0 + ND_rw(agtail(e1)) + ND_lw(aghead(e1)); |
| 279 |
if (!canreach(aghead(e1), agtail(e1))) |
| 280 |
make_aux_edge(agtail(e1), aghead(e1), m1, |
| 281 |
ED_weight(e)); |
| 282 |
} |
| 283 |
|
| 284 |
/* position flat edge endpoints */ |
| 285 |
for (size_t k = 0; k < ND_flat_out(u).size; k++) { |
| 286 |
e = ND_flat_out(u).list[k]; |
| 287 |
if (ND_order(agtail(e)) < ND_order(aghead(e))) { |
| 288 |
t0 = agtail(e); |
| 289 |
h0 = aghead(e); |
| 290 |
} else { |
| 291 |
t0 = aghead(e); |
| 292 |
h0 = agtail(e); |
| 293 |
} |
| 294 |
|
| 295 |
width = ND_rw(t0) + ND_lw(h0); |
| 296 |
m0 = ED_minlen(e) * GD_nodesep(g) + width; |
| 297 |
|
| 298 |
if ((e0 = find_fast_edge(t0, h0))) { |
| 299 |
/* flat edge between adjacent neighbors |
| 300 |
* ED_dist contains the largest label width. |
| 301 |
*/ |
| 302 |
m0 = MAX(m0, width + GD_nodesep(g) + ROUND(ED_dist(e))); |
| 303 |
ED_minlen(e0) = MAX(ED_minlen(e0), m0); |
| 304 |
ED_weight(e0) = MAX(ED_weight(e0), ED_weight(e)); |
| 305 |
} |
| 306 |
else if (!ED_label(e)) { |
| 307 |
/* unlabeled flat edge between non-neighbors |
| 308 |
* ED_minlen(e) is max of ED_minlen of all equivalent |
| 309 |
* edges. |
| 310 |
*/ |
| 311 |
make_aux_edge(t0, h0, m0, ED_weight(e)); |
| 312 |
} |
| 313 |
/* labeled flat edges between non-neighbors have already |
| 314 |
* been constrained by the label above. |
| 315 |
*/ |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
/// make virtual edge pairs corresponding to input edges |
| 322 |
static void make_edge_pairs(graph_t * g) |
| 323 |
{ |
| 324 |
int i, m0, m1; |
| 325 |
node_t *n, *sn; |
| 326 |
edge_t *e; |
| 327 |
|
| 328 |
for (n = GD_nlist(g); n; n = ND_next(n)) { |
| 329 |
if (ND_save_out(n).list) |
| 330 |
for (i = 0; (e = ND_save_out(n).list[i]); i++) { |
| 331 |
sn = virtual_node(g); |
| 332 |
ND_node_type(sn) = SLACKNODE; |
| 333 |
m0 = (ED_head_port(e).p.x - ED_tail_port(e).p.x); |
| 334 |
if (m0 > 0) |
| 335 |
m1 = 0; |
| 336 |
else { |
| 337 |
m1 = -m0; |
| 338 |
m0 = 0; |
| 339 |
} |
| 340 |
make_aux_edge(sn, agtail(e), m0 + 1, ED_weight(e)); |
| 341 |
make_aux_edge(sn, aghead(e), m1 + 1, ED_weight(e)); |
| 342 |
ND_rank(sn) = |
| 343 |
MIN(ND_rank(agtail(e)) - m0 - 1, |
| 344 |
ND_rank(aghead(e)) - m1 - 1); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
static void contain_clustnodes(graph_t * g) |
| 350 |
{ |
| 351 |
int c; |
| 352 |
edge_t *e; |
| 353 |
|
| 354 |
if (g != dot_root(g)) { |
| 355 |
contain_nodes(g); |
| 356 |
if ((e = find_fast_edge(GD_ln(g),GD_rn(g)))) /* maybe from lrvn()?*/ |
| 357 |
ED_weight(e) += 128; |
| 358 |
else |
| 359 |
make_aux_edge(GD_ln(g), GD_rn(g), 1, 128); /* clust compaction edge */ |
| 360 |
} |
| 361 |
for (c = 1; c <= GD_n_cluster(g); c++) |
| 362 |
contain_clustnodes(GD_clust(g)[c]); |
| 363 |
} |
| 364 |
|
| 365 |
static bool vnode_not_related_to(graph_t *g, node_t *v) { |
| 366 |
edge_t *e; |
| 367 |
|
| 368 |
if (ND_node_type(v) != VIRTUAL) |
| 369 |
return false; |
| 370 |
for (e = ND_save_out(v).list[0]; ED_to_orig(e); e = ED_to_orig(e)); |
| 371 |
if (agcontains(g, agtail(e))) |
| 372 |
return false; |
| 373 |
if (agcontains(g, aghead(e))) |
| 374 |
return false; |
| 375 |
return true; |
| 376 |
} |
| 377 |
|
| 378 |
/* Guarantee nodes outside the cluster g are placed outside of it. |
| 379 |
* This is done by adding constraints to make sure such nodes have |
| 380 |
* a gap of margin from the left or right bounding box node ln or rn. |
| 381 |
* |
| 382 |
* We could probably reduce some of these constraints by checking if |
| 383 |
* the node is in a cluster, since elsewhere we make constrain a |
| 384 |
* separate between clusters. Also, we should be able to skip the |
| 385 |
* first loop if g is the root graph. |
| 386 |
*/ |
| 387 |
static void keepout_othernodes(graph_t * g) |
| 388 |
{ |
| 389 |
int i, c, r, margin; |
| 390 |
node_t *u, *v; |
| 391 |
|
| 392 |
margin = late_int (g, G_margin, CL_OFFSET, 0); |
| 393 |
for (r = GD_minrank(g); r <= GD_maxrank(g); r++) { |
| 394 |
if (GD_rank(g)[r].n == 0) |
| 395 |
continue; |
| 396 |
v = GD_rank(g)[r].v[0]; |
| 397 |
if (v == NULL) |
| 398 |
continue; |
| 399 |
for (i = ND_order(v) - 1; i >= 0; i--) { |
| 400 |
u = GD_rank(dot_root(g))[r].v[i]; |
| 401 |
/* can't use "is_a_vnode_of" because elists are swapped */ |
| 402 |
if (ND_node_type(u) == NORMAL || vnode_not_related_to(g, u)) { |
| 403 |
make_aux_edge(u, GD_ln(g), margin + ND_rw(u), 0); |
| 404 |
break; |
| 405 |
} |
| 406 |
} |
| 407 |
for (i = ND_order(v) + GD_rank(g)[r].n; i < GD_rank(dot_root(g))[r].n; |
| 408 |
i++) { |
| 409 |
u = GD_rank(dot_root(g))[r].v[i]; |
| 410 |
if (ND_node_type(u) == NORMAL || vnode_not_related_to(g, u)) { |
| 411 |
make_aux_edge(GD_rn(g), u, margin + ND_lw(u), 0); |
| 412 |
break; |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
for (c = 1; c <= GD_n_cluster(g); c++) |
| 418 |
keepout_othernodes(GD_clust(g)[c]); |
| 419 |
} |
| 420 |
|
| 421 |
/* Make sure boxes of subclusters of g are offset from the |
| 422 |
* box of g. This is done by a constraint between the left and |
| 423 |
* right bounding box nodes ln and rn of g and a subcluster. |
| 424 |
* The gap needs to include any left or right labels. |
| 425 |
*/ |
| 426 |
static void contain_subclust(graph_t * g) |
| 427 |
{ |
| 428 |
int margin, c; |
| 429 |
graph_t *subg; |
| 430 |
|
| 431 |
margin = late_int (g, G_margin, CL_OFFSET, 0); |
| 432 |
make_lrvn(g); |
| 433 |
for (c = 1; c <= GD_n_cluster(g); c++) { |
| 434 |
subg = GD_clust(g)[c]; |
| 435 |
make_lrvn(subg); |
| 436 |
make_aux_edge(GD_ln(g), GD_ln(subg), |
| 437 |
margin + GD_border(g)[LEFT_IX].x, 0); |
| 438 |
make_aux_edge(GD_rn(subg), GD_rn(g), |
| 439 |
margin + GD_border(g)[RIGHT_IX].x, 0); |
| 440 |
contain_subclust(subg); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
/* Guarantee space between subcluster of g. |
| 445 |
* This is done by adding a constraint between the right bbox node rn |
| 446 |
* of the left cluster and the left bbox node ln of the right cluster. |
| 447 |
* This is only done if the two clusters overlap in some rank. |
| 448 |
*/ |
| 449 |
static void separate_subclust(graph_t * g) |
| 450 |
{ |
| 451 |
int i, j, margin; |
| 452 |
graph_t *low, *high; |
| 453 |
graph_t *left, *right; |
| 454 |
|
| 455 |
margin = late_int (g, G_margin, CL_OFFSET, 0); |
| 456 |
for (i = 1; i <= GD_n_cluster(g); i++) |
| 457 |
make_lrvn(GD_clust(g)[i]); |
| 458 |
for (i = 1; i <= GD_n_cluster(g); i++) { |
| 459 |
for (j = i + 1; j <= GD_n_cluster(g); j++) { |
| 460 |
low = GD_clust(g)[i]; |
| 461 |
high = GD_clust(g)[j]; |
| 462 |
if (GD_minrank(low) > GD_minrank(high)) { |
| 463 |
SWAP(&low, &high); |
| 464 |
} |
| 465 |
if (GD_maxrank(low) < GD_minrank(high)) |
| 466 |
continue; |
| 467 |
if (ND_order(GD_rank(low)[GD_minrank(high)].v[0]) |
| 468 |
< ND_order(GD_rank(high)[GD_minrank(high)].v[0])) { |
| 469 |
left = low; |
| 470 |
right = high; |
| 471 |
} else { |
| 472 |
left = high; |
| 473 |
right = low; |
| 474 |
} |
| 475 |
make_aux_edge(GD_rn(left), GD_ln(right), margin, 0); |
| 476 |
} |
| 477 |
separate_subclust(GD_clust(g)[i]); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/* create constraints for: |
| 482 |
* node containment in clusters, |
| 483 |
* cluster containment in clusters, |
| 484 |
* separation of sibling clusters. |
| 485 |
*/ |
| 486 |
static void pos_clusters(graph_t * g) |
| 487 |
{ |
| 488 |
if (GD_n_cluster(g) > 0) { |
| 489 |
contain_clustnodes(g); |
| 490 |
keepout_othernodes(g); |
| 491 |
contain_subclust(g); |
| 492 |
separate_subclust(g); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
static void compress_graph(graph_t * g) |
| 497 |
{ |
| 498 |
double x; |
| 499 |
pointf p; |
| 500 |
|
| 501 |
if (GD_drawing(g)->ratio_kind != R_COMPRESS) |
| 502 |
return; |
| 503 |
p = GD_drawing(g)->size; |
| 504 |
if (p.x * p.y <= 1) |
| 505 |
return; |
| 506 |
contain_nodes(g); |
| 507 |
if (!GD_flip(g)) |
| 508 |
x = p.x; |
| 509 |
else |
| 510 |
x = p.y; |
| 511 |
|
| 512 |
/* Guard against huge size attribute since max. edge length is USHRT_MAX |
| 513 |
* A warning might be called for. Also, one could check that the graph |
| 514 |
* already fits GD_drawing(g)->size and return immediately. |
| 515 |
*/ |
| 516 |
x = MIN(x,USHRT_MAX); |
| 517 |
make_aux_edge(GD_ln(g), GD_rn(g), x, 1000); |
| 518 |
} |
| 519 |
|
| 520 |
static void create_aux_edges(graph_t * g) |
| 521 |
{ |
| 522 |
allocate_aux_edges(g); |
| 523 |
make_LR_constraints(g); |
| 524 |
make_edge_pairs(g); |
| 525 |
pos_clusters(g); |
| 526 |
compress_graph(g); |
| 527 |
} |
| 528 |
|
| 529 |
static void remove_aux_edges(graph_t * g) |
| 530 |
{ |
| 531 |
int i; |
| 532 |
node_t *n, *nnext, *nprev; |
| 533 |
edge_t *e; |
| 534 |
|
| 535 |
for (n = GD_nlist(g); n; n = ND_next(n)) { |
| 536 |
for (i = 0; (e = ND_out(n).list[i]); i++) { |
| 537 |
free(e->base.data); |
| 538 |
free(e); |
| 539 |
} |
| 540 |
free_list(ND_out(n)); |
| 541 |
free_list(ND_in(n)); |
| 542 |
ND_out(n) = ND_save_out(n); |
| 543 |
ND_in(n) = ND_save_in(n); |
| 544 |
} |
| 545 |
/* cannot be merged with previous loop */ |
| 546 |
nprev = NULL; |
| 547 |
for (n = GD_nlist(g); n; n = nnext) { |
| 548 |
nnext = ND_next(n); |
| 549 |
if (ND_node_type(n) == SLACKNODE) { |
| 550 |
if (nprev) |
| 551 |
ND_next(nprev) = nnext; |
| 552 |
else |
| 553 |
GD_nlist(g) = nnext; |
| 554 |
if (nnext != NULL) { |
| 555 |
ND_prev(nnext) = nprev; |
| 556 |
} |
| 557 |
free(n->base.data); |
| 558 |
free(n); |
| 559 |
} else |
| 560 |
nprev = n; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
/// Set x coords of nodes. |
| 565 |
static void |
| 566 |
set_xcoords(graph_t * g) |
| 567 |
{ |
| 568 |
int i, j; |
| 569 |
node_t *v; |
| 570 |
rank_t *rank = GD_rank(g); |
| 571 |
|
| 572 |
for (i = GD_minrank(g); i <= GD_maxrank(g); i++) { |
| 573 |
for (j = 0; j < rank[i].n; j++) { |
| 574 |
v = rank[i].v[j]; |
| 575 |
ND_coord(v).x = ND_rank(v); |
| 576 |
ND_rank(v) = i; |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
/* Expand cluster height by delta, adding half to top |
| 582 |
* and half to bottom. If the bottom expansion exceeds the |
| 583 |
* ht1 of the rank, shift the ranks in the cluster up. |
| 584 |
* If the top expansion, including any shift from the bottom |
| 585 |
* expansion, exceeds to ht2 of the rank, shift the ranks above |
| 586 |
* the cluster up. |
| 587 |
* |
| 588 |
* FIX: There can be excess space between ranks. Not sure where this is |
| 589 |
* coming from but it could be cleaned up. |
| 590 |
*/ |
| 591 |
static void adjustSimple(graph_t *g, double delta, int margin_total) { |
| 592 |
int r; |
| 593 |
double deltop; |
| 594 |
graph_t *root = dot_root(g); |
| 595 |
rank_t *rank = GD_rank(root); |
| 596 |
int maxr = GD_maxrank(g); |
| 597 |
int minr = GD_minrank(g); |
| 598 |
|
| 599 |
const double bottom = (delta + 1) / 2; |
| 600 |
const double delbottom = GD_ht1(g) + bottom - (rank[maxr].ht1 - margin_total); |
| 601 |
if (delbottom > 0) { |
| 602 |
for (r = maxr; r >= minr; r--) { |
| 603 |
if (rank[r].n > 0) |
| 604 |
ND_coord(rank[r].v[0]).y += delbottom; |
| 605 |
} |
| 606 |
deltop = GD_ht2(g) + (delta-bottom) + delbottom - (rank[minr].ht2 - margin_total); |
| 607 |
} |
| 608 |
else |
| 609 |
deltop = GD_ht2(g) + (delta-bottom) - (rank[minr].ht2 - margin_total); |
| 610 |
if (deltop > 0) { |
| 611 |
for (r = minr-1; r >= GD_minrank(root); r--) { |
| 612 |
if (rank[r].n > 0) |
| 613 |
ND_coord(rank[r].v[0]).y += deltop; |
| 614 |
} |
| 615 |
} |
| 616 |
GD_ht2(g) += delta - bottom; |
| 617 |
GD_ht1(g) += bottom; |
| 618 |
} |
| 619 |
|
| 620 |
/* Recursively adjust ranks to take into account |
| 621 |
* wide cluster labels when rankdir=LR. |
| 622 |
* We divide the extra space between the top and bottom. |
| 623 |
* Adjust the ht1 and ht2 values in the process. |
| 624 |
*/ |
| 625 |
static void adjustRanks(graph_t * g, int margin_total) |
| 626 |
{ |
| 627 |
double lht; /* label height */ |
| 628 |
double rht; /* height between top and bottom ranks */ |
| 629 |
int maxr, minr, margin; |
| 630 |
int c; |
| 631 |
double delta, ht1, ht2; |
| 632 |
|
| 633 |
rank_t *rank = GD_rank(dot_root(g)); |
| 634 |
if (g == dot_root(g)) |
| 635 |
margin = 0; |
| 636 |
else |
| 637 |
margin = late_int (g, G_margin, CL_OFFSET, 0); |
| 638 |
|
| 639 |
ht1 = GD_ht1(g); |
| 640 |
ht2 = GD_ht2(g); |
| 641 |
|
| 642 |
for (c = 1; c <= GD_n_cluster(g); c++) { |
| 643 |
graph_t *subg = GD_clust(g)[c]; |
| 644 |
adjustRanks(subg, margin+margin_total); |
| 645 |
if (GD_maxrank(subg) == GD_maxrank(g)) |
| 646 |
ht1 = fmax(ht1, GD_ht1(subg) + margin); |
| 647 |
if (GD_minrank(subg) == GD_minrank(g)) |
| 648 |
ht2 = fmax(ht2, GD_ht2(subg) + margin); |
| 649 |
} |
| 650 |
|
| 651 |
GD_ht1(g) = ht1; |
| 652 |
GD_ht2(g) = ht2; |
| 653 |
|
| 654 |
if (g != dot_root(g) && GD_label(g)) { |
| 655 |
lht = MAX(GD_border(g)[LEFT_IX].y, GD_border(g)[RIGHT_IX].y); |
| 656 |
maxr = GD_maxrank(g); |
| 657 |
minr = GD_minrank(g); |
| 658 |
rht = ND_coord(rank[minr].v[0]).y - ND_coord(rank[maxr].v[0]).y; |
| 659 |
delta = lht - (rht + ht1 + ht2); |
| 660 |
if (delta > 0) { |
| 661 |
adjustSimple(g, delta, margin_total); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
/* update the global ranks */ |
| 666 |
if (g != dot_root(g)) { |
| 667 |
rank[GD_minrank(g)].ht2 = fmax(rank[GD_minrank(g)].ht2, GD_ht2(g)); |
| 668 |
rank[GD_maxrank(g)].ht1 = fmax(rank[GD_maxrank(g)].ht1, GD_ht1(g)); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
/* recursively compute cluster ht requirements. assumes GD_ht1(subg) and ht2 |
| 673 |
* are computed from primitive nodes only. updates ht1 and ht2 to reflect |
| 674 |
* cluster nesting and labels. also maintains global rank ht1 and ht2. |
| 675 |
* Return true if some cluster has a label. |
| 676 |
*/ |
| 677 |
static int clust_ht(Agraph_t * g) |
| 678 |
{ |
| 679 |
int c; |
| 680 |
double ht1, ht2; |
| 681 |
graph_t *subg; |
| 682 |
rank_t *rank = GD_rank(dot_root(g)); |
| 683 |
int margin, haveClustLabel = 0; |
| 684 |
|
| 685 |
if (g == dot_root(g)) |
| 686 |
margin = CL_OFFSET; |
| 687 |
else |
| 688 |
margin = late_int (g, G_margin, CL_OFFSET, 0); |
| 689 |
|
| 690 |
ht1 = GD_ht1(g); |
| 691 |
ht2 = GD_ht2(g); |
| 692 |
|
| 693 |
/* account for sub-clusters */ |
| 694 |
for (c = 1; c <= GD_n_cluster(g); c++) { |
| 695 |
subg = GD_clust(g)[c]; |
| 696 |
haveClustLabel |= clust_ht(subg); |
| 697 |
if (GD_maxrank(subg) == GD_maxrank(g)) |
| 698 |
ht1 = MAX(ht1, GD_ht1(subg) + margin); |
| 699 |
if (GD_minrank(subg) == GD_minrank(g)) |
| 700 |
ht2 = MAX(ht2, GD_ht2(subg) + margin); |
| 701 |
} |
| 702 |
|
| 703 |
/* account for a possible cluster label in clusters */ |
| 704 |
/* room for root graph label is handled in dotneato_postprocess */ |
| 705 |
if (g != dot_root(g) && GD_label(g)) { |
| 706 |
haveClustLabel = 1; |
| 707 |
if (!GD_flip(agroot(g))) { |
| 708 |
ht1 += GD_border(g)[BOTTOM_IX].y; |
| 709 |
ht2 += GD_border(g)[TOP_IX].y; |
| 710 |
} |
| 711 |
} |
| 712 |
GD_ht1(g) = ht1; |
| 713 |
GD_ht2(g) = ht2; |
| 714 |
|
| 715 |
/* update the global ranks */ |
| 716 |
if (g != dot_root(g)) { |
| 717 |
rank[GD_minrank(g)].ht2 = MAX(rank[GD_minrank(g)].ht2, ht2); |
| 718 |
rank[GD_maxrank(g)].ht1 = MAX(rank[GD_maxrank(g)].ht1, ht1); |
| 719 |
} |
| 720 |
|
| 721 |
return haveClustLabel; |
| 722 |
} |
| 723 |
|
| 724 |
/* set y coordinates of nodes, a rank at a time */ |
| 725 |
static void set_ycoords(graph_t * g) |
| 726 |
{ |
| 727 |
edge_t *e; |
| 728 |
rank_t *rank = GD_rank(g); |
| 729 |
graph_t *clust; |
| 730 |
|
| 731 |
/* scan ranks for tallest nodes. */ |
| 732 |
for (int r = GD_minrank(g); r <= GD_maxrank(g); r++) { |
| 733 |
for (int i = 0; i < rank[r].n; i++) { |
| 734 |
node_t *const n = rank[r].v[i]; |
| 735 |
|
| 736 |
/* assumes symmetry, ht1 = ht2 */ |
| 737 |
double ht2 = ND_ht(n) / 2; |
| 738 |
|
| 739 |
|
| 740 |
/* have to look for high self-edge labels, too */ |
| 741 |
if (ND_other(n).list) |
| 742 |
for (int j = 0; (e = ND_other(n).list[j]); j++) { |
| 743 |
if (agtail(e) == aghead(e)) { |
| 744 |
if (ED_label(e)) |
| 745 |
ht2 = fmax(ht2, ED_label(e)->dimen.y / 2); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
/* update global rank ht */ |
| 750 |
if (rank[r].pht2 < ht2) |
| 751 |
rank[r].pht2 = rank[r].ht2 = ht2; |
| 752 |
if (rank[r].pht1 < ht2) |
| 753 |
rank[r].pht1 = rank[r].ht1 = ht2; |
| 754 |
|
| 755 |
/* update nearest enclosing cluster rank ht */ |
| 756 |
if ((clust = ND_clust(n))) { |
| 757 |
int yoff = clust == g ? 0 : late_int (clust, G_margin, CL_OFFSET, 0); |
| 758 |
if (ND_rank(n) == GD_minrank(clust)) |
| 759 |
GD_ht2(clust) = fmax(GD_ht2(clust), ht2 + yoff); |
| 760 |
if (ND_rank(n) == GD_maxrank(clust)) |
| 761 |
GD_ht1(clust) = fmax(GD_ht1(clust), ht2 + yoff); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
/* scan sub-clusters */ |
| 767 |
const int lbl = clust_ht(g); |
| 768 |
|
| 769 |
/* make the initial assignment of ycoords to leftmost nodes by ranks */ |
| 770 |
double maxht = 0; |
| 771 |
int r = GD_maxrank(g); |
| 772 |
ND_coord(rank[r].v[0]).y = rank[r].ht1; |
| 773 |
while (--r >= GD_minrank(g)) { |
| 774 |
const double d0 = rank[r + 1].pht2 + rank[r].pht1 + GD_ranksep(g); // prim node sep |
| 775 |
const double d1 = rank[r + 1].ht2 + rank[r].ht1 + CL_OFFSET; // cluster sep |
| 776 |
const double delta = fmax(d0, d1); |
| 777 |
if (rank[r].n > 0) /* this may reflect some problem */ |
| 778 |
ND_coord(rank[r].v[0]).y = ND_coord(rank[r + 1].v[0]).y + delta; |
| 779 |
#ifdef DEBUG |
| 780 |
else |
| 781 |
fprintf(stderr, "dot set_ycoords: rank %d is empty\n", |
| 782 |
rank[r].n); |
| 783 |
#endif |
| 784 |
maxht = fmax(maxht, delta); |
| 785 |
} |
| 786 |
|
| 787 |
/* If there are cluster labels and the drawing is rotated, we need special processing to |
| 788 |
* allocate enough room. We use adjustRanks for this, and then recompute the maxht if |
| 789 |
* the ranks are to be equally spaced. This seems simpler and appears to work better than |
| 790 |
* handling equal spacing as a special case. |
| 791 |
*/ |
| 792 |
if (lbl && GD_flip(g)) { |
| 793 |
adjustRanks(g, 0); |
| 794 |
if (GD_exact_ranksep(g)) { /* recompute maxht */ |
| 795 |
maxht = 0; |
| 796 |
r = GD_maxrank(g); |
| 797 |
double d0 = ND_coord(rank[r].v[0]).y; |
| 798 |
while (--r >= GD_minrank(g)) { |
| 799 |
const double d1 = ND_coord(rank[r].v[0]).y; |
| 800 |
const double delta = d1 - d0; |
| 801 |
maxht = fmax(maxht, delta); |
| 802 |
d0 = d1; |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
/* re-assign if ranks are equally spaced */ |
| 808 |
if (GD_exact_ranksep(g)) { |
| 809 |
for (r = GD_maxrank(g) - 1; r >= GD_minrank(g); r--) |
| 810 |
if (rank[r].n > 0) /* this may reflect the same problem :-() */ |
| 811 |
ND_coord(rank[r].v[0]).y = ND_coord(rank[r + 1].v[0]).y + maxht; |
| 812 |
} |
| 813 |
|
| 814 |
/* copy ycoord assignment from leftmost nodes to others */ |
| 815 |
for (node_t *n = GD_nlist(g); n; n = ND_next(n)) |
| 816 |
ND_coord(n).y = ND_coord(rank[ND_rank(n)].v[0]).y; |
| 817 |
} |
| 818 |
|
| 819 |
/* Compute bounding box of g. |
| 820 |
* The x limits of clusters are given by the x positions of ln and rn. |
| 821 |
* This information is stored in the rank field, since it was calculated |
| 822 |
* using network simplex. |
| 823 |
* For the root graph, we don't enforce all the constraints on lr and |
| 824 |
* rn, so we traverse the nodes and subclusters. |
| 825 |
*/ |
| 826 |
static void dot_compute_bb(graph_t * g, graph_t * root) |
| 827 |
{ |
| 828 |
int r, c; |
| 829 |
double x, offset; |
| 830 |
node_t *v; |
| 831 |
pointf LL, UR; |
| 832 |
|
| 833 |
if (g == dot_root(g)) { |
| 834 |
LL.x = INT_MAX; |
| 835 |
UR.x = -INT_MAX; |
| 836 |
for (r = GD_minrank(g); r <= GD_maxrank(g); r++) { |
| 837 |
int rnkn = GD_rank(g)[r].n; |
| 838 |
if (rnkn == 0) |
| 839 |
continue; |
| 840 |
if ((v = GD_rank(g)[r].v[0]) == NULL) |
| 841 |
continue; |
| 842 |
for (c = 1; ND_node_type(v) != NORMAL && c < rnkn; c++) |
| 843 |
v = GD_rank(g)[r].v[c]; |
| 844 |
if (ND_node_type(v) == NORMAL) { |
| 845 |
x = ND_coord(v).x - ND_lw(v); |
| 846 |
LL.x = MIN(LL.x, x); |
| 847 |
} |
| 848 |
else continue; |
| 849 |
/* At this point, we know the rank contains a NORMAL node */ |
| 850 |
v = GD_rank(g)[r].v[rnkn - 1]; |
| 851 |
for (c = rnkn-2; ND_node_type(v) != NORMAL; c--) |
| 852 |
v = GD_rank(g)[r].v[c]; |
| 853 |
x = ND_coord(v).x + ND_rw(v); |
| 854 |
UR.x = MAX(UR.x, x); |
| 855 |
} |
| 856 |
offset = CL_OFFSET; |
| 857 |
for (c = 1; c <= GD_n_cluster(g); c++) { |
| 858 |
x = (double)(GD_bb(GD_clust(g)[c]).LL.x - offset); |
| 859 |
LL.x = MIN(LL.x, x); |
| 860 |
x = (double)(GD_bb(GD_clust(g)[c]).UR.x + offset); |
| 861 |
UR.x = MAX(UR.x, x); |
| 862 |
} |
| 863 |
} else { |
| 864 |
LL.x = ND_rank(GD_ln(g)); |
| 865 |
UR.x = ND_rank(GD_rn(g)); |
| 866 |
} |
| 867 |
LL.y = ND_coord(GD_rank(root)[GD_maxrank(g)].v[0]).y - GD_ht1(g); |
| 868 |
UR.y = ND_coord(GD_rank(root)[GD_minrank(g)].v[0]).y + GD_ht2(g); |
| 869 |
GD_bb(g).LL = LL; |
| 870 |
GD_bb(g).UR = UR; |
| 871 |
} |
| 872 |
|
| 873 |
static void rec_bb(graph_t * g, graph_t * root) |
| 874 |
{ |
| 875 |
int c; |
| 876 |
for (c = 1; c <= GD_n_cluster(g); c++) |
| 877 |
rec_bb(GD_clust(g)[c], root); |
| 878 |
dot_compute_bb(g, root); |
| 879 |
} |
| 880 |
|
| 881 |
/* Recursively rescale all bounding boxes using scale factors |
| 882 |
* xf and yf. We assume all the bboxes have been computed. |
| 883 |
*/ |
| 884 |
static void scale_bb(graph_t *g, double xf, double yf) { |
| 885 |
int c; |
| 886 |
|
| 887 |
for (c = 1; c <= GD_n_cluster(g); c++) |
| 888 |
scale_bb(GD_clust(g)[c], xf, yf); |
| 889 |
GD_bb(g).LL.x *= xf; |
| 890 |
GD_bb(g).LL.y *= yf; |
| 891 |
GD_bb(g).UR.x *= xf; |
| 892 |
GD_bb(g).UR.y *= yf; |
| 893 |
} |
| 894 |
|
| 895 |
/* Set bounding boxes and, if ratio is set, rescale graph. |
| 896 |
* Note that if some dimension shrinks, there may be problems |
| 897 |
* with labels. |
| 898 |
*/ |
| 899 |
static void set_aspect(graph_t *g) { |
| 900 |
double xf = 0.0, yf = 0.0, actual, desired; |
| 901 |
node_t *n; |
| 902 |
bool filled; |
| 903 |
|
| 904 |
rec_bb(g, g); |
| 905 |
if (GD_maxrank(g) > 0 && GD_drawing(g)->ratio_kind) { |
| 906 |
pointf sz = sub_pointf(GD_bb(g).UR, GD_bb(g).LL); // normalize |
| 907 |
if (GD_flip(g)) { |
| 908 |
sz = exch_xyf(sz); |
| 909 |
} |
| 910 |
bool scale_it = true; |
| 911 |
if (GD_drawing(g)->ratio_kind == R_AUTO) |
| 912 |
filled = idealsize(g, .5); |
| 913 |
else |
| 914 |
filled = GD_drawing(g)->ratio_kind == R_FILL; |
| 915 |
if (filled) { |
| 916 |
/* fill is weird because both X and Y can stretch */ |
| 917 |
if (GD_drawing(g)->size.x <= 0) |
| 918 |
scale_it = false; |
| 919 |
else { |
| 920 |
xf = GD_drawing(g)->size.x / sz.x; |
| 921 |
yf = GD_drawing(g)->size.y / sz.y; |
| 922 |
if (xf < 1.0 || yf < 1.0) { |
| 923 |
if (xf < yf) { |
| 924 |
yf /= xf; |
| 925 |
xf = 1.0; |
| 926 |
} else { |
| 927 |
xf /= yf; |
| 928 |
yf = 1.0; |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
} else if (GD_drawing(g)->ratio_kind == R_EXPAND) { |
| 933 |
if (GD_drawing(g)->size.x <= 0) |
| 934 |
scale_it = false; |
| 935 |
else { |
| 936 |
xf = GD_drawing(g)->size.x / GD_bb(g).UR.x; |
| 937 |
yf = GD_drawing(g)->size.y / GD_bb(g).UR.y; |
| 938 |
if (xf > 1.0 && yf > 1.0) { |
| 939 |
const double scale = fmin(xf, yf); |
| 940 |
xf = yf = scale; |
| 941 |
} else |
| 942 |
scale_it = false; |
| 943 |
} |
| 944 |
} else if (GD_drawing(g)->ratio_kind == R_VALUE) { |
| 945 |
desired = GD_drawing(g)->ratio; |
| 946 |
actual = sz.y / sz.x; |
| 947 |
if (actual < desired) { |
| 948 |
yf = desired / actual; |
| 949 |
xf = 1.0; |
| 950 |
} else { |
| 951 |
xf = actual / desired; |
| 952 |
yf = 1.0; |
| 953 |
} |
| 954 |
} else |
| 955 |
scale_it = false; |
| 956 |
if (scale_it) { |
| 957 |
if (GD_flip(g)) { |
| 958 |
SWAP(&xf, &yf); |
| 959 |
} |
| 960 |
for (n = GD_nlist(g); n; n = ND_next(n)) { |
| 961 |
ND_coord(n).x = round(ND_coord(n).x * xf); |
| 962 |
ND_coord(n).y = round(ND_coord(n).y * yf); |
| 963 |
} |
| 964 |
scale_bb(g, xf, yf); |
| 965 |
} |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
/* make space for the leaf nodes of each rank */ |
| 970 |
static void make_leafslots(graph_t * g) |
| 971 |
{ |
| 972 |
int i, j, r; |
| 973 |
node_t *v; |
| 974 |
|
| 975 |
for (r = GD_minrank(g); r <= GD_maxrank(g); r++) { |
| 976 |
j = 0; |
| 977 |
for (i = 0; i < GD_rank(g)[r].n; i++) { |
| 978 |
v = GD_rank(g)[r].v[i]; |
| 979 |
ND_order(v) = j; |
| 980 |
if (ND_ranktype(v) == LEAFSET) |
| 981 |
j = j + ND_UF_size(v); |
| 982 |
else |
| 983 |
j++; |
| 984 |
} |
| 985 |
if (j <= GD_rank(g)[r].n) |
| 986 |
continue; |
| 987 |
node_t **new_v = gv_calloc(j + 1, sizeof(node_t*)); |
| 988 |
for (i = GD_rank(g)[r].n - 1; i >= 0; i--) { |
| 989 |
v = GD_rank(g)[r].v[i]; |
| 990 |
new_v[ND_order(v)] = v; |
| 991 |
} |
| 992 |
GD_rank(g)[r].n = j; |
| 993 |
new_v[j] = NULL; |
| 994 |
free(GD_rank(g)[r].v); |
| 995 |
GD_rank(g)[r].v = new_v; |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
int ports_eq(edge_t * e, edge_t * f) |
| 1000 |
{ |
| 1001 |
return ED_head_port(e).defined == ED_head_port(f).defined |
| 1002 |
&& ((ED_head_port(e).p.x == ED_head_port(f).p.x && |
| 1003 |
ED_head_port(e).p.y == ED_head_port(f).p.y) |
| 1004 |
|| !ED_head_port(e).defined) |
| 1005 |
&& ((ED_tail_port(e).p.x == ED_tail_port(f).p.x && |
| 1006 |
ED_tail_port(e).p.y == ED_tail_port(f).p.y) |
| 1007 |
|| !ED_tail_port(e).defined); |
| 1008 |
} |
| 1009 |
|
| 1010 |
static void expand_leaves(graph_t * g) |
| 1011 |
{ |
| 1012 |
int i, d; |
| 1013 |
node_t *n; |
| 1014 |
edge_t *e, *f; |
| 1015 |
|
| 1016 |
make_leafslots(g); |
| 1017 |
for (n = GD_nlist(g); n; n = ND_next(n)) { |
| 1018 |
if (ND_other(n).list) |
| 1019 |
for (i = 0; (e = ND_other(n).list[i]); i++) { |
| 1020 |
if ((d = ND_rank(aghead(e)) - ND_rank(aghead(e))) == 0) |
| 1021 |
continue; |
| 1022 |
f = ED_to_orig(e); |
| 1023 |
if (!ports_eq(e, f)) { |
| 1024 |
zapinlist(&(ND_other(n)), e); |
| 1025 |
if (d == 1) |
| 1026 |
fast_edge(e); |
| 1027 |
/*else unitize(e); ### */ |
| 1028 |
i--; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
/* Add left and right slacknodes to a cluster which |
| 1035 |
* are used in the LP to constrain nodes not in g but |
| 1036 |
* sharing its ranks to be to the left or right of g |
| 1037 |
* by a specified amount. |
| 1038 |
* The slacknodes ln and rn give the x position of the |
| 1039 |
* left and right side of the cluster's bounding box. In |
| 1040 |
* particular, any cluster labels on the left or right side |
| 1041 |
* are inside. |
| 1042 |
* If a cluster has a label, and we have rankdir!=LR, we make |
| 1043 |
* sure the cluster is wide enough for the label. Note that |
| 1044 |
* if the label is wider than the cluster, the nodes in the |
| 1045 |
* cluster may not be centered. |
| 1046 |
*/ |
| 1047 |
static void make_lrvn(graph_t * g) |
| 1048 |
{ |
| 1049 |
node_t *ln, *rn; |
| 1050 |
|
| 1051 |
if (GD_ln(g)) |
| 1052 |
return; |
| 1053 |
ln = virtual_node(dot_root(g)); |
| 1054 |
ND_node_type(ln) = SLACKNODE; |
| 1055 |
rn = virtual_node(dot_root(g)); |
| 1056 |
ND_node_type(rn) = SLACKNODE; |
| 1057 |
|
| 1058 |
if (GD_label(g) && g != dot_root(g) && !GD_flip(agroot(g))) { |
| 1059 |
const double w = fmax(GD_border(g)[BOTTOM_IX].x, GD_border(g)[TOP_IX].x); |
| 1060 |
make_aux_edge(ln, rn, w, 0); |
| 1061 |
} |
| 1062 |
|
| 1063 |
GD_ln(g) = ln; |
| 1064 |
GD_rn(g) = rn; |
| 1065 |
} |
| 1066 |
|
| 1067 |
/* make left and right bounding box virtual nodes ln and rn |
| 1068 |
* constrain interior nodes |
| 1069 |
*/ |
| 1070 |
static void contain_nodes(graph_t * g) |
| 1071 |
{ |
| 1072 |
int margin, r; |
| 1073 |
node_t *ln, *rn, *v; |
| 1074 |
|
| 1075 |
margin = late_int (g, G_margin, CL_OFFSET, 0); |
| 1076 |
make_lrvn(g); |
| 1077 |
ln = GD_ln(g); |
| 1078 |
rn = GD_rn(g); |
| 1079 |
for (r = GD_minrank(g); r <= GD_maxrank(g); r++) { |
| 1080 |
if (GD_rank(g)[r].n == 0) |
| 1081 |
continue; |
| 1082 |
v = GD_rank(g)[r].v[0]; |
| 1083 |
if (v == NULL) { |
| 1084 |
agerrorf("contain_nodes clust %s rank %d missing node\n", |
| 1085 |
agnameof(g), r); |
| 1086 |
continue; |
| 1087 |
} |
| 1088 |
make_aux_edge(ln, v, |
| 1089 |
ND_lw(v) + margin + GD_border(g)[LEFT_IX].x, 0); |
| 1090 |
v = GD_rank(g)[r].v[GD_rank(g)[r].n - 1]; |
| 1091 |
make_aux_edge(v, rn, |
| 1092 |
ND_rw(v) + margin + GD_border(g)[RIGHT_IX].x, 0); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
/* set g->drawing->size to a reasonable default. |
| 1097 |
* returns a boolean to indicate if drawing is to |
| 1098 |
* be scaled and filled */ |
| 1099 |
static bool idealsize(graph_t * g, double minallowed) |
| 1100 |
{ |
| 1101 |
double xf, yf, f, R; |
| 1102 |
pointf b, relpage, margin; |
| 1103 |
|
| 1104 |
/* try for one page */ |
| 1105 |
relpage = GD_drawing(g)->page; |
| 1106 |
if (relpage.x < 0.001 || relpage.y < 0.001) |
| 1107 |
return false; /* no page was specified */ |
| 1108 |
margin = GD_drawing(g)->margin; |
| 1109 |
relpage = sub_pointf(relpage, margin); |
| 1110 |
relpage = sub_pointf(relpage, margin); |
| 1111 |
b.x = GD_bb(g).UR.x; |
| 1112 |
b.y = GD_bb(g).UR.y; |
| 1113 |
xf = relpage.x / b.x; |
| 1114 |
yf = relpage.y / b.y; |
| 1115 |
if (xf >= 1.0 && yf >= 1.0) |
| 1116 |
return false; /* fits on one page */ |
| 1117 |
|
| 1118 |
f = MIN(xf, yf); |
| 1119 |
xf = yf = MAX(f, minallowed); |
| 1120 |
|
| 1121 |
R = ceil(xf * b.x / relpage.x); |
| 1122 |
xf = R * relpage.x / b.x; |
| 1123 |
R = ceil(yf * b.y / relpage.y); |
| 1124 |
yf = R * relpage.y / b.y; |
| 1125 |
GD_drawing(g)->size.x = b.x * xf; |
| 1126 |
GD_drawing(g)->size.y = b.y * yf; |
| 1127 |
return true; |
| 1128 |
} |