| 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/circular.h> |
| 14 |
#include <circogen/blocktree.h> |
| 15 |
#include <circogen/circpos.h> |
| 16 |
#include <util/agxbuf.h> |
| 17 |
|
| 18 |
#define MINDIST 1.0 |
| 19 |
|
| 20 |
/* Set attributes based on original root graph. |
| 21 |
* This is obtained by taking a node of g, finding its node |
| 22 |
* in the original graph, and finding that node's graph. |
| 23 |
*/ |
| 24 |
static void initGraphAttrs(Agraph_t * g, circ_state * state) |
| 25 |
{ |
| 26 |
node_t *n = agfstnode(g); |
| 27 |
|
| 28 |
Agraph_t *rootg = agraphof(ORIGN(n)); |
| 29 |
attrsym_t *G_mindist = agattr_text(rootg, AGRAPH, "mindist", NULL); |
| 30 |
attrsym_t *N_root = agattr_text(rootg, AGNODE, "root", NULL); |
| 31 |
char *rootname = agget(rootg, "root"); |
| 32 |
initBlocklist(&state->bl); |
| 33 |
state->orderCount = 1; |
| 34 |
state->min_dist = late_double(rootg, G_mindist, MINDIST, 0.0); |
| 35 |
state->N_root = N_root; |
| 36 |
state->rootname = rootname; |
| 37 |
} |
| 38 |
|
| 39 |
static block_t* |
| 40 |
createOneBlock(Agraph_t * g, circ_state * state) |
| 41 |
{ |
| 42 |
Agraph_t *subg; |
| 43 |
agxbuf name = {0}; |
| 44 |
block_t *bp; |
| 45 |
Agnode_t* n; |
| 46 |
|
| 47 |
agxbprint(&name, "_block_%d", state->blockCount++); |
| 48 |
subg = agsubg(g, agxbuse(&name), 1); |
| 49 |
agxbfree(&name); |
| 50 |
bp = mkBlock(subg); |
| 51 |
|
| 52 |
for (n = agfstnode(g); n; n = agnxtnode(g,n)) { |
| 53 |
agsubnode(bp->sub_graph, n, 1); |
| 54 |
BLOCK(n) = bp; |
| 55 |
} |
| 56 |
|
| 57 |
return bp; |
| 58 |
} |
| 59 |
|
| 60 |
/* Do circular layout of g. |
| 61 |
* Assume g is strict. |
| 62 |
* g is a "connected" component of the derived graph of the |
| 63 |
* original graph. |
| 64 |
* We make state static so that it keeps a record of block numbers used |
| 65 |
* in a graph; it gets reset when a new root graph is used. |
| 66 |
*/ |
| 67 |
void circularLayout(Agraph_t *g, Agraph_t *realg, int *blockCount) { |
| 68 |
block_t *root; |
| 69 |
|
| 70 |
if (agnnodes(g) == 1) { |
| 71 |
Agnode_t *n = agfstnode(g); |
| 72 |
ND_pos(n)[0] = 0; |
| 73 |
ND_pos(n)[1] = 0; |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
circ_state state = {.blockCount = *blockCount}; |
| 78 |
initGraphAttrs(g, &state); |
| 79 |
|
| 80 |
if (mapbool(agget(realg, "oneblock"))) |
| 81 |
root = createOneBlock(g, &state); |
| 82 |
else |
| 83 |
root = createBlocktree(g, &state); |
| 84 |
circPos(g, root, &state); |
| 85 |
|
| 86 |
/* cleanup: |
| 87 |
* We need to cleanup objects created in initGraphAttrs |
| 88 |
* and all blocks. All graph objects are components of the |
| 89 |
* initial derived graph and will be freed when it is closed. |
| 90 |
*/ |
| 91 |
freeBlocktree(root); |
| 92 |
|
| 93 |
*blockCount = state.blockCount; |
| 94 |
} |
| 95 |
|
| 96 |
#ifdef DEBUG |
| 97 |
void prGraph(Agraph_t * g) |
| 98 |
{ |
| 99 |
Agnode_t *n; |
| 100 |
Agedge_t *e; |
| 101 |
|
| 102 |
fprintf(stderr, "%s\n", agnameof(g)); |
| 103 |
for (n = agfstnode(g); n; n = agnxtnode(g, n)) { |
| 104 |
fprintf(stderr, "%s (%p)\n", agnameof(n), n); |
| 105 |
for (e = agfstout(g, n); e; e = agnxtout(g, e)) { |
| 106 |
fprintf(stderr, "%s", agnameof(n)); |
| 107 |
fprintf(stderr, " -- %s (%p)\n", agnameof(aghead(e)), e); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void prData(Agnode_t * n, int pass) |
| 113 |
{ |
| 114 |
char *pname; |
| 115 |
char *bname; |
| 116 |
char *tname; |
| 117 |
char *name1; |
| 118 |
char *name2; |
| 119 |
int dist1, dist2; |
| 120 |
|
| 121 |
if (PARENT(n)) |
| 122 |
pname = agnameof(PARENT(n)); |
| 123 |
else |
| 124 |
pname = "<P0>"; |
| 125 |
if (BLOCK(n)) |
| 126 |
bname = agnameof(BLOCK(n)->sub_graph); |
| 127 |
else { |
| 128 |
pname = "<B0>"; |
| 129 |
bname = ""; |
| 130 |
} |
| 131 |
fprintf(stderr, "%s: %x %s %s ", agnameof(n), FLAGS(n), pname, bname); |
| 132 |
switch (pass) { |
| 133 |
case 0: |
| 134 |
fprintf(stderr, "%d %d\n", VAL(n), LOWVAL(n)); |
| 135 |
break; |
| 136 |
case 1: |
| 137 |
if (TPARENT(n)) |
| 138 |
tname = agnameof(TPARENT(n)); |
| 139 |
else |
| 140 |
tname = "<ROOT>"; |
| 141 |
dist1 = DISTONE(n); |
| 142 |
if (dist1 > 0) |
| 143 |
name1 = agnameof(LEAFONE(n)); |
| 144 |
else |
| 145 |
name1 = "<null>"; |
| 146 |
dist2 = DISTTWO(n); |
| 147 |
if (dist2 > 0) |
| 148 |
name2 = agnameof(LEAFTWO(n)); |
| 149 |
else |
| 150 |
name2 = "<null>"; |
| 151 |
fprintf(stderr, "%s %s %d %s %d\n", tname, name1, dist1, name2, |
| 152 |
dist2); |
| 153 |
break; |
| 154 |
default: |
| 155 |
fprintf(stderr, "%d\n", POSITION(n)); |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
#endif |