| 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 |
#include "config.h" |
| 12 |
|
| 13 |
#include <circogen/blockpath.h> |
| 14 |
#include <circogen/circular.h> |
| 15 |
#include <circogen/edgelist.h> |
| 16 |
#include <stddef.h> |
| 17 |
#include <stdbool.h> |
| 18 |
#include <stdint.h> |
| 19 |
#include <util/agxbuf.h> |
| 20 |
#include <util/alloc.h> |
| 21 |
#include <util/list.h> |
| 22 |
|
| 23 |
/* The code below lays out a single block on a circle. |
| 24 |
*/ |
| 25 |
|
| 26 |
/* We use the unused fields order and to_orig in cloned nodes and edges */ |
| 27 |
#define ORIGE(e) (ED_to_orig(e)) |
| 28 |
|
| 29 |
/* clone_graph: |
| 30 |
* Create two copies of the argument graph |
| 31 |
* One is a subgraph, the other is an actual copy since we will be |
| 32 |
* adding edges to it. |
| 33 |
* |
| 34 |
* @param state Context containing a counter to use for graph copy naming |
| 35 |
*/ |
| 36 |
static Agraph_t *clone_graph(Agraph_t *ing, Agraph_t **xg, circ_state *state) { |
| 37 |
Agraph_t *clone; |
| 38 |
Agraph_t *xclone; |
| 39 |
Agnode_t *n; |
| 40 |
Agnode_t *xn; |
| 41 |
Agnode_t *xh; |
| 42 |
Agedge_t *e; |
| 43 |
Agedge_t *xe; |
| 44 |
agxbuf gname = {0}; |
| 45 |
|
| 46 |
agxbprint(&gname, "_clone_%d", state->graphCopyCount++); |
| 47 |
clone = agsubg(ing, agxbuse(&gname), 1); |
| 48 |
agbindrec(clone, "Agraphinfo_t", sizeof(Agraphinfo_t), true); //node custom data |
| 49 |
agxbprint(&gname, "_clone_%d", state->graphCopyCount++); |
| 50 |
xclone = agopen(agxbuse(&gname), ing->desc, NULL); |
| 51 |
agxbfree(&gname); |
| 52 |
for (n = agfstnode(ing); n; n = agnxtnode(ing, n)) { |
| 53 |
agsubnode(clone,n,1); |
| 54 |
xn = agnode(xclone, agnameof(n),1); |
| 55 |
agbindrec(xn, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true); //node custom data |
| 56 |
CLONE(n) = xn; |
| 57 |
} |
| 58 |
|
| 59 |
for (n = agfstnode(ing); n; n = agnxtnode(ing, n)) { |
| 60 |
xn = CLONE(n); |
| 61 |
for (e = agfstout(ing, n); e; e = agnxtout(ing, e)) { |
| 62 |
agsubedge(clone,e,1); |
| 63 |
xh = CLONE(aghead(e)); |
| 64 |
xe = agedge(xclone, xn, xh, NULL, 1); |
| 65 |
agbindrec(xe, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //node custom data |
| 66 |
ORIGE(xe) = e; |
| 67 |
DEGREE(xn) += 1; |
| 68 |
DEGREE(xh) += 1; |
| 69 |
} |
| 70 |
} |
| 71 |
*xg = xclone; |
| 72 |
return clone; |
| 73 |
} |
| 74 |
|
| 75 |
typedef LIST(Agnode_t *) deglist_t; |
| 76 |
|
| 77 |
/// comparison function for sorting nodes by degree, descending |
| 78 |
static int cmpDegree(const void *x, const void *y) { |
| 79 |
Agnode_t *const *a = x; |
| 80 |
Agnode_t *const *b = y; |
| 81 |
if (DEGREE(*a) < DEGREE(*b)) { |
| 82 |
return 1; |
| 83 |
} |
| 84 |
if (DEGREE(*a) > DEGREE(*b)) { |
| 85 |
return -1; |
| 86 |
} |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
/// Add nodes to deg_list, storing them by descending degree. |
| 91 |
static deglist_t getList(Agraph_t *g) { |
| 92 |
deglist_t dl = {0}; |
| 93 |
Agnode_t *n; |
| 94 |
|
| 95 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 96 |
LIST_APPEND(&dl, n); |
| 97 |
} |
| 98 |
LIST_SORT(&dl, cmpDegree); |
| 99 |
return dl; |
| 100 |
} |
| 101 |
|
| 102 |
static void find_pair_edges(Agraph_t * g, Agnode_t * n, Agraph_t * outg) |
| 103 |
{ |
| 104 |
int edge_cnt = 0; |
| 105 |
|
| 106 |
const int node_degree = DEGREE(n); |
| 107 |
LIST(Agnode_t *) neighbors_with = {0}; |
| 108 |
LIST(Agnode_t *) neighbors_without = {0}; |
| 109 |
|
| 110 |
for (Agedge_t *e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) { |
| 111 |
Agnode_t *n1 = aghead(e); |
| 112 |
if (n1 == n) |
| 113 |
n1 = agtail(e); |
| 114 |
bool has_pair_edge = false; |
| 115 |
for (Agedge_t *ep = agfstedge(g, n); ep; ep = agnxtedge(g, ep, n)) { |
| 116 |
if (ep == e) |
| 117 |
continue; |
| 118 |
Agnode_t *n2 = aghead(ep); |
| 119 |
if (n2 == n) |
| 120 |
n2 = agtail(ep); |
| 121 |
Agedge_t *const ex = agfindedge(g, n1, n2); |
| 122 |
if (ex) { |
| 123 |
has_pair_edge = true; |
| 124 |
if ((uintptr_t)n1 < (uintptr_t)n2) { // count edge only once |
| 125 |
edge_cnt++; |
| 126 |
if (ORIGE(ex)) { |
| 127 |
agdelete(outg, ORIGE(ex)); |
| 128 |
ORIGE(ex) = 0; /* delete only once */ |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
if (has_pair_edge) { |
| 134 |
LIST_APPEND(&neighbors_with, n1); |
| 135 |
} else { |
| 136 |
LIST_APPEND(&neighbors_without, n1); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
int diff = node_degree - 1 - edge_cnt; |
| 141 |
if (diff > 0) { |
| 142 |
if ((size_t)diff < LIST_SIZE(&neighbors_without)) { |
| 143 |
for (size_t mark = 0; mark + 1 < LIST_SIZE(&neighbors_without); mark += 2) { |
| 144 |
Agnode_t *const tp = LIST_GET(&neighbors_without, mark); |
| 145 |
Agnode_t *const hp = LIST_GET(&neighbors_without, mark + 1); |
| 146 |
agbindrec(agedge(g, tp, hp, NULL, 1), "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); // edge custom data |
| 147 |
DEGREE(tp)++; |
| 148 |
DEGREE(hp)++; |
| 149 |
diff--; |
| 150 |
} |
| 151 |
|
| 152 |
for (size_t mark = 2; diff > 0; ++mark, --diff) { |
| 153 |
Agnode_t *const tp = LIST_GET(&neighbors_without, 0); |
| 154 |
Agnode_t *const hp = LIST_GET(&neighbors_without, mark); |
| 155 |
agbindrec(agedge(g, tp, hp, NULL, 1), "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); // edge custom data |
| 156 |
DEGREE(tp)++; |
| 157 |
DEGREE(hp)++; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
else if ((size_t)diff == LIST_SIZE(&neighbors_without)) { |
| 162 |
Agnode_t *const tp = |
| 163 |
LIST_IS_EMPTY(&neighbors_with) ? NULL : LIST_GET(&neighbors_with, 0); |
| 164 |
for (size_t mark = 0; mark < LIST_SIZE(&neighbors_without); mark++) { |
| 165 |
Agnode_t *const hp = LIST_GET(&neighbors_without, mark); |
| 166 |
agbindrec(agedge(g, tp, hp, NULL, 1), "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //node custom data |
| 167 |
if (tp != NULL) { |
| 168 |
DEGREE(tp)++; |
| 169 |
} |
| 170 |
DEGREE(hp)++; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
LIST_FREE(&neighbors_without); |
| 176 |
LIST_FREE(&neighbors_with); |
| 177 |
} |
| 178 |
|
| 179 |
/// Create layout skeleton of ing. Why is returned graph connected? |
| 180 |
/// |
| 181 |
/// @param state Context containing a counter to use for graph copy naming |
| 182 |
static Agraph_t *remove_pair_edges(Agraph_t *ing, circ_state *state) { |
| 183 |
int nodeCount; |
| 184 |
Agraph_t *outg; |
| 185 |
Agraph_t *g; |
| 186 |
Agnode_t *currnode, *adjNode; |
| 187 |
Agedge_t *e; |
| 188 |
|
| 189 |
outg = clone_graph(ing, &g, state); |
| 190 |
nodeCount = agnnodes(g); |
| 191 |
deglist_t dl = getList(g); |
| 192 |
|
| 193 |
for (int counter = 0; counter < nodeCount - 3; ++counter) { |
| 194 |
currnode = LIST_IS_EMPTY(&dl) ? NULL : LIST_POP_BACK(&dl); |
| 195 |
|
| 196 |
/* Remove all adjacent nodes since they have to be reinserted */ |
| 197 |
for (e = agfstedge(g, currnode); e; e = agnxtedge(g, e, currnode)) { |
| 198 |
adjNode = aghead(e); |
| 199 |
if (currnode == adjNode) |
| 200 |
adjNode = agtail(e); |
| 201 |
LIST_REMOVE(&dl, adjNode); |
| 202 |
} |
| 203 |
|
| 204 |
find_pair_edges(g, currnode, outg); |
| 205 |
|
| 206 |
for (e = agfstedge(g, currnode); e; e = agnxtedge(g, e, currnode)) { |
| 207 |
adjNode = aghead(e); |
| 208 |
if (currnode == adjNode) |
| 209 |
adjNode = agtail(e); |
| 210 |
|
| 211 |
DEGREE(adjNode)--; |
| 212 |
LIST_APPEND(&dl, adjNode); |
| 213 |
} |
| 214 |
LIST_SORT(&dl, cmpDegree); |
| 215 |
|
| 216 |
agdelete(g, currnode); |
| 217 |
} |
| 218 |
|
| 219 |
agclose(g); |
| 220 |
LIST_FREE(&dl); |
| 221 |
return outg; |
| 222 |
} |
| 223 |
|
| 224 |
static void |
| 225 |
measure_distance(Agnode_t * n, Agnode_t * ancestor, int dist, |
| 226 |
Agnode_t * change) |
| 227 |
{ |
| 228 |
Agnode_t *parent; |
| 229 |
|
| 230 |
parent = TPARENT(ancestor); |
| 231 |
if (parent == NULL) |
| 232 |
return; |
| 233 |
|
| 234 |
dist++; |
| 235 |
|
| 236 |
/* check parent to see if it has other leaf paths at greater distance |
| 237 |
than the context node. |
| 238 |
set the path/distance of the leaf at this ancestor node */ |
| 239 |
|
| 240 |
if (DISTONE(parent) == 0) { |
| 241 |
LEAFONE(parent) = n; |
| 242 |
DISTONE(parent) = dist; |
| 243 |
} else if (dist > DISTONE(parent)) { |
| 244 |
if (LEAFONE(parent) != change) { |
| 245 |
if (!DISTTWO(parent) || LEAFTWO(parent) != change) |
| 246 |
change = LEAFONE(parent); |
| 247 |
LEAFTWO(parent) = LEAFONE(parent); |
| 248 |
DISTTWO(parent) = DISTONE(parent); |
| 249 |
} |
| 250 |
LEAFONE(parent) = n; |
| 251 |
DISTONE(parent) = dist; |
| 252 |
} else if (dist > DISTTWO(parent)) { |
| 253 |
LEAFTWO(parent) = n; |
| 254 |
DISTTWO(parent) = dist; |
| 255 |
return; |
| 256 |
} else |
| 257 |
return; |
| 258 |
|
| 259 |
measure_distance(n, parent, dist, change); |
| 260 |
} |
| 261 |
|
| 262 |
/// Find and return longest path in tree. |
| 263 |
static nodelist_t find_longest_path(Agraph_t *tree) { |
| 264 |
Agnode_t *n; |
| 265 |
Agedge_t *e; |
| 266 |
Agnode_t *common = 0; |
| 267 |
int maxlength = 0; |
| 268 |
int length; |
| 269 |
|
| 270 |
if (agnnodes(tree) == 1) { |
| 271 |
nodelist_t beginPath = {0}; |
| 272 |
n = agfstnode(tree); |
| 273 |
LIST_APPEND(&beginPath, n); |
| 274 |
SET_ONPATH(n); |
| 275 |
return beginPath; |
| 276 |
} |
| 277 |
|
| 278 |
for (n = agfstnode(tree); n; n = agnxtnode(tree, n)) { |
| 279 |
int count = 0; |
| 280 |
for (e = agfstedge(tree, n); e; e = agnxtedge(tree, e, n)) { |
| 281 |
count++; |
| 282 |
} |
| 283 |
if (count == 1) |
| 284 |
measure_distance(n, n, 0, NULL); |
| 285 |
} |
| 286 |
|
| 287 |
/* find the branch node rooted at the longest path */ |
| 288 |
for (n = agfstnode(tree); n; n = agnxtnode(tree, n)) { |
| 289 |
length = DISTONE(n) + DISTTWO(n); |
| 290 |
if (length > maxlength) { |
| 291 |
common = n; |
| 292 |
maxlength = length; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
nodelist_t beginPath = {0}; |
| 297 |
for (n = LEAFONE(common); n != common; n = TPARENT(n)) { |
| 298 |
LIST_APPEND(&beginPath, n); |
| 299 |
SET_ONPATH(n); |
| 300 |
} |
| 301 |
LIST_APPEND(&beginPath, common); |
| 302 |
SET_ONPATH(common); |
| 303 |
|
| 304 |
if (DISTTWO(common)) { /* 2nd path might be empty */ |
| 305 |
nodelist_t endPath = {0}; |
| 306 |
for (n = LEAFTWO(common); n != common; n = TPARENT(n)) { |
| 307 |
LIST_APPEND(&endPath, n); |
| 308 |
SET_ONPATH(n); |
| 309 |
} |
| 310 |
reverseAppend(&beginPath, &endPath); |
| 311 |
} |
| 312 |
|
| 313 |
return beginPath; |
| 314 |
} |
| 315 |
|
| 316 |
/// Simple depth first search, adding traversed edges to tree. |
| 317 |
static void dfs(Agraph_t * g, Agnode_t * n, Agraph_t * tree) |
| 318 |
{ |
| 319 |
Agedge_t *e; |
| 320 |
Agnode_t *neighbor; |
| 321 |
|
| 322 |
SET_VISITED(n); |
| 323 |
for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) { |
| 324 |
neighbor = aghead(e); |
| 325 |
if (neighbor == n) |
| 326 |
neighbor = agtail(e); |
| 327 |
|
| 328 |
if (!VISITED(neighbor)) { |
| 329 |
/* add the edge to the dfs tree */ |
| 330 |
agsubedge(tree,e,1); |
| 331 |
TPARENT(neighbor) = n; |
| 332 |
dfs(g, neighbor, tree); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
/// Construct spanning forest of g as subgraph |
| 338 |
/// |
| 339 |
/// @param state Context containing a counter to use for spanning tree naming |
| 340 |
static Agraph_t *spanning_tree(Agraph_t *g, circ_state *state) { |
| 341 |
Agnode_t *n; |
| 342 |
Agraph_t *tree; |
| 343 |
agxbuf gname = {0}; |
| 344 |
|
| 345 |
agxbprint(&gname, "_span_%d", state->spanningTreeCount++); |
| 346 |
tree = agsubg(g, agxbuse(&gname), 1); |
| 347 |
agxbfree(&gname); |
| 348 |
agbindrec(tree, "Agraphinfo_t", sizeof(Agraphinfo_t), true); //node custom data |
| 349 |
|
| 350 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 351 |
agsubnode(tree,n,1); |
| 352 |
DISTONE(n) = 0; |
| 353 |
DISTTWO(n) = 0; |
| 354 |
UNSET_VISITED(n); |
| 355 |
} |
| 356 |
|
| 357 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 358 |
if (!VISITED(n)) { |
| 359 |
TPARENT(n) = NULL; |
| 360 |
dfs(g, n, tree); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
return tree; |
| 365 |
} |
| 366 |
|
| 367 |
/// Add induced edges. |
| 368 |
static void block_graph(Agraph_t * g, block_t * sn) |
| 369 |
{ |
| 370 |
Agnode_t *n; |
| 371 |
Agedge_t *e; |
| 372 |
Agraph_t *subg = sn->sub_graph; |
| 373 |
|
| 374 |
for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) { |
| 375 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 376 |
if (BLOCK(aghead(e)) == sn) |
| 377 |
agsubedge(subg,e,1); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
static int count_all_crossings(nodelist_t * list, Agraph_t * subg) |
| 383 |
{ |
| 384 |
edgelist *openEdgeList = init_edgelist(); |
| 385 |
Agnode_t *n; |
| 386 |
Agedge_t *e; |
| 387 |
int crossings = 0; |
| 388 |
int order = 1; |
| 389 |
|
| 390 |
for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) { |
| 391 |
for (e = agfstout(subg, n); e; e = agnxtout(subg, e)) { |
| 392 |
EDGEORDER(e) = 0; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
for (size_t item = 0; item < LIST_SIZE(list); ++item) { |
| 397 |
n = LIST_GET(list, item); |
| 398 |
|
| 399 |
for (e = agfstedge(subg, n); e; e = agnxtedge(subg, e, n)) { |
| 400 |
if (EDGEORDER(e) > 0) { |
| 401 |
edgelistitem *eitem; |
| 402 |
Agedge_t *ep; |
| 403 |
|
| 404 |
for (eitem = dtfirst(openEdgeList); eitem; |
| 405 |
eitem = dtnext(openEdgeList, eitem)) { |
| 406 |
ep = eitem->edge; |
| 407 |
if (EDGEORDER(ep) > EDGEORDER(e)) { |
| 408 |
if (aghead(ep) != n && agtail(ep) != n) |
| 409 |
crossings++; |
| 410 |
} |
| 411 |
} |
| 412 |
remove_edge(openEdgeList, e); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
for (e = agfstedge(subg, n); e; e = agnxtedge(subg, e, n)) { |
| 417 |
if (EDGEORDER(e) == 0) { |
| 418 |
EDGEORDER(e) = order; |
| 419 |
add_edge(openEdgeList, e); |
| 420 |
} |
| 421 |
} |
| 422 |
order++; |
| 423 |
} |
| 424 |
|
| 425 |
free_edgelist(openEdgeList); |
| 426 |
return crossings; |
| 427 |
} |
| 428 |
|
| 429 |
#define CROSS_ITER 10 |
| 430 |
|
| 431 |
/* Attempt to reduce edge crossings by moving nodes. |
| 432 |
* Original crossing count is in cnt; final count is returned there. |
| 433 |
* list is the original list; return the best list found. |
| 434 |
*/ |
| 435 |
static nodelist_t reduce(nodelist_t list, Agraph_t *subg, int *cnt) { |
| 436 |
Agnode_t *curnode; |
| 437 |
Agedge_t *e; |
| 438 |
Agnode_t *neighbor; |
| 439 |
int crossings, j, newCrossings; |
| 440 |
|
| 441 |
crossings = *cnt; |
| 442 |
for (curnode = agfstnode(subg); curnode; |
| 443 |
curnode = agnxtnode(subg, curnode)) { |
| 444 |
/* move curnode next to its neighbors */ |
| 445 |
for (e = agfstedge(subg, curnode); e; |
| 446 |
e = agnxtedge(subg, e, curnode)) { |
| 447 |
neighbor = agtail(e); |
| 448 |
if (neighbor == curnode) |
| 449 |
neighbor = aghead(e); |
| 450 |
|
| 451 |
for (j = 0; j < 2; j++) { |
| 452 |
nodelist_t listCopy; |
| 453 |
LIST_COPY(&listCopy, &list); |
| 454 |
insertNodelist(&list, curnode, neighbor, j); |
| 455 |
newCrossings = count_all_crossings(&list, subg); |
| 456 |
if (newCrossings < crossings) { |
| 457 |
crossings = newCrossings; |
| 458 |
LIST_FREE(&listCopy); |
| 459 |
if (crossings == 0) { |
| 460 |
*cnt = 0; |
| 461 |
return list; |
| 462 |
} |
| 463 |
} else { |
| 464 |
LIST_FREE(&list); |
| 465 |
list = listCopy; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
*cnt = crossings; |
| 471 |
return list; |
| 472 |
} |
| 473 |
|
| 474 |
static nodelist_t reduce_edge_crossings(nodelist_t list, Agraph_t *subg) { |
| 475 |
int i, crossings, origCrossings; |
| 476 |
|
| 477 |
crossings = count_all_crossings(&list, subg); |
| 478 |
if (crossings == 0) |
| 479 |
return list; |
| 480 |
|
| 481 |
for (i = 0; i < CROSS_ITER; i++) { |
| 482 |
origCrossings = crossings; |
| 483 |
list = reduce(list, subg, &crossings); |
| 484 |
/* return if no crossings or no improvement */ |
| 485 |
if (origCrossings == crossings || crossings == 0) |
| 486 |
return list; |
| 487 |
} |
| 488 |
return list; |
| 489 |
} |
| 490 |
|
| 491 |
/// Return max dimension of nodes on list |
| 492 |
static double largest_nodesize(nodelist_t * list) |
| 493 |
{ |
| 494 |
double size = 0; |
| 495 |
|
| 496 |
for (size_t item = 0; item < LIST_SIZE(list); ++item) { |
| 497 |
Agnode_t *n = ORIGN(LIST_GET(list, item)); |
| 498 |
if (ND_width(n) > size) |
| 499 |
size = ND_width(n); |
| 500 |
if (ND_height(n) > size) |
| 501 |
size = ND_height(n); |
| 502 |
} |
| 503 |
return size; |
| 504 |
} |
| 505 |
|
| 506 |
/// Add n to list. By construction, n is not in list at start. |
| 507 |
static void place_node(Agraph_t * g, Agnode_t * n, nodelist_t * list) |
| 508 |
{ |
| 509 |
Agedge_t *e; |
| 510 |
bool placed = false; |
| 511 |
nodelist_t neighbors = {0}; |
| 512 |
|
| 513 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 514 |
LIST_APPEND(&neighbors, aghead(e)); |
| 515 |
SET_NEIGHBOR(aghead(e)); |
| 516 |
} |
| 517 |
for (e = agfstin(g, n); e; e = agnxtin(g, e)) { |
| 518 |
LIST_APPEND(&neighbors, agtail(e)); |
| 519 |
SET_NEIGHBOR(agtail(e)); |
| 520 |
} |
| 521 |
|
| 522 |
/* Look for 2 neighbors consecutive on list */ |
| 523 |
if (LIST_SIZE(&neighbors) >= 2) { |
| 524 |
for (size_t one = 0; one < LIST_SIZE(list); ++one) { |
| 525 |
const size_t two = (one + 1) % LIST_SIZE(list); |
| 526 |
|
| 527 |
if (NEIGHBOR(LIST_GET(list, one)) && NEIGHBOR(LIST_GET(list, two))) { |
| 528 |
appendNodelist(list, one + 1, n); |
| 529 |
placed = true; |
| 530 |
break; |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
/* Find any neighbor on list */ |
| 536 |
if (!placed && !LIST_IS_EMPTY(&neighbors)) { |
| 537 |
for (size_t one = 0; one < LIST_SIZE(list); ++one) { |
| 538 |
if (NEIGHBOR(LIST_GET(list, one))) { |
| 539 |
appendNodelist(list, one + 1, n); |
| 540 |
placed = true; |
| 541 |
break; |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
if (!placed) |
| 547 |
LIST_APPEND(list, n); |
| 548 |
|
| 549 |
for (size_t one = 0; one < LIST_SIZE(&neighbors); ++one) |
| 550 |
UNSET_NEIGHBOR(LIST_GET(&neighbors, one)); |
| 551 |
LIST_FREE(&neighbors); |
| 552 |
} |
| 553 |
|
| 554 |
/// Add nodes not in list to list. |
| 555 |
static void place_residual_nodes(Agraph_t * g, nodelist_t * list) |
| 556 |
{ |
| 557 |
Agnode_t *n; |
| 558 |
|
| 559 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 560 |
if (!ONPATH(n)) |
| 561 |
place_node(g, n, list); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/// @param state Context containing a counter to use for graph copy naming |
| 566 |
nodelist_t layout_block(Agraph_t *g, block_t *sn, double min_dist, |
| 567 |
circ_state *state) { |
| 568 |
Agraph_t *copyG, *tree, *subg; |
| 569 |
int k; |
| 570 |
double theta, radius; |
| 571 |
|
| 572 |
subg = sn->sub_graph; |
| 573 |
block_graph(g, sn); /* add induced edges */ |
| 574 |
|
| 575 |
copyG = remove_pair_edges(subg, state); |
| 576 |
|
| 577 |
tree = spanning_tree(copyG, state); |
| 578 |
nodelist_t longest_path = find_longest_path(tree); |
| 579 |
place_residual_nodes(subg, &longest_path); |
| 580 |
/* at this point, longest_path is a list of all nodes in the block */ |
| 581 |
|
| 582 |
/* apply crossing reduction algorithms here */ |
| 583 |
longest_path = reduce_edge_crossings(longest_path, subg); |
| 584 |
|
| 585 |
size_t N = LIST_SIZE(&longest_path); |
| 586 |
const double largest_node = largest_nodesize(&longest_path); |
| 587 |
/* N*(min_dist+largest_node) is roughly circumference of required circle */ |
| 588 |
if (N == 1) |
| 589 |
radius = 0; |
| 590 |
else |
| 591 |
radius = (double)N * (min_dist + largest_node) / (2 * M_PI); |
| 592 |
|
| 593 |
for (size_t item = 0; item < LIST_SIZE(&longest_path); ++item) { |
| 594 |
Agnode_t *n = LIST_GET(&longest_path, item); |
| 595 |
if (ISPARENT(n)) { |
| 596 |
/* QUESTION: Why is only one parent realigned? */ |
| 597 |
realignNodelist(&longest_path, item); |
| 598 |
break; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
k = 0; |
| 603 |
for (size_t item = 0; item < LIST_SIZE(&longest_path); ++item) { |
| 604 |
Agnode_t *n = LIST_GET(&longest_path, item); |
| 605 |
POSITION(n) = k; |
| 606 |
PSI(n) = 0.0; |
| 607 |
theta = k * (2.0 * M_PI / (double)N); |
| 608 |
|
| 609 |
ND_pos(n)[0] = radius * cos(theta); |
| 610 |
ND_pos(n)[1] = radius * sin(theta); |
| 611 |
|
| 612 |
k++; |
| 613 |
} |
| 614 |
|
| 615 |
if (N == 1) |
| 616 |
sn->radius = largest_node / 2; |
| 617 |
else |
| 618 |
sn->radius = radius; |
| 619 |
sn->rad0 = sn->radius; |
| 620 |
|
| 621 |
/* initialize parent pos */ |
| 622 |
sn->parent_pos = -1; |
| 623 |
|
| 624 |
agclose(copyG); |
| 625 |
return longest_path; |
| 626 |
} |
| 627 |
|
| 628 |
#ifdef DEBUG |
| 629 |
void prTree(Agraph_t * g) |
| 630 |
{ |
| 631 |
Agnode_t *n; |
| 632 |
|
| 633 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 634 |
if (TPARENT(n)) { |
| 635 |
fprintf(stderr, "%s ", agnameof(n)); |
| 636 |
fprintf(stderr, "-> %s\n", agnameof(TPARENT(n))); |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
#endif |