1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/* $Id: circularinit.c,v 1.23 2009/09/04 17:47:42 erg Exp $ $Revision: 1.23 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2004 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
/*
* Circular layout. Biconnected components are put on circles.
* block-cutnode tree is done recursively, with children placed
* about parent block.
* Based on:
* Six and Tollis, "A Framework for Circular Drawings of
* Networks", GD '99, LNCS 1731, pp. 107-116;
* Six and Tollis, "Circular Drawings of Biconnected Graphs",
* Proc. ALENEX '99, pp 57-73.
* Kaufmann and Wiese, "Maintaining the Mental Map for Circular
* Drawings", GD '02, LNCS 2528, pp. 12-22.
*/
#include "circular.h"
#include "adjust.h"
#include "pack.h"
#include "neatoprocs.h"
#include <string.h>
static void circular_init_edge(edge_t * e)
{
#ifdef WITH_CGRAPH
agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), TRUE); //node custom data
#endif /* WITH_CGRAPH */
common_init_edge(e);
ED_factor(e) = late_double(e, E_weight, 1.0, 0.0);
}
static void circular_init_node_edge(graph_t * g)
{
node_t *n;
edge_t *e;
int i = 0;
ndata* alg = N_NEW(agnnodes(g), ndata);
GD_neato_nlist(g) = N_NEW(agnnodes(g) + 1, node_t *);
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
neato_init_node(n);
ND_alg(n) = alg + i;
GD_neato_nlist(g)[i++] = n;
}
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
circular_init_edge(e);
}
}
}
void circo_init_graph(graph_t * g)
{
setEdgeType (g, ET_LINE);
/* GD_ndim(g) = late_int(g,agfindattr(g,"dim"),2,2); */
Ndim = GD_ndim(g) = 2; /* The algorithm only makes sense in 2D */
circular_init_node_edge(g);
}
/* makeDerivedNode:
* Make a node in the derived graph, with the given name.
* orig points to what it represents, either a real node or
* a cluster. Copy size info from original node; needed for
* adjustNodes and packSubgraphs.
*/
static node_t *makeDerivedNode(graph_t * dg, char *name, int isNode,
void *orig)
{
#ifndef WITH_CGRAPH
node_t *n = agnode(dg, name);
#else /* WITH_CGRAPH */
node_t *n = agnode(dg, name,1);
agbindrec(n, "Agnodeinfo_t", sizeof(Agnodeinfo_t), TRUE); //node custom data
#endif /* WITH_CGRAPH */
ND_alg(n) = (void *) NEW(cdata);
if (isNode) {
ND_pos(n) = N_NEW(Ndim, double);
ND_xsize(n) = ND_xsize((node_t *) orig);
ND_ysize(n) = ND_ysize((node_t *) orig);
ORIGN(n) = (node_t *) orig;
} else
ORIGG(n) = (graph_t *) orig;
return n;
}
/* circomps:
* Construct a strict, undirected graph with no loops from g.
* Construct the connected components with the provision that all
* nodes in a block subgraph are considered connected.
* Return array of components with number of components in cnt.
* Each component has its blocks as subgraphs.
* FIX: Check that blocks are disjoint.
*/
Agraph_t **circomps(Agraph_t * g, int *cnt)
{
int c_cnt;
Agraph_t **ccs;
Agraph_t *dg;
Agnode_t *n, *v, *dt, *dh;
Agedge_t *e;
Agraph_t *sg;
int i;
Agedge_t *ep;
Agnode_t *p;
#ifndef WITH_CGRAPH
dg = agopen("derived", AGFLAG_STRICT);
#else /* WITH_CGRAPH */
dg = agopen("derived", Agstrictundirected,NIL(Agdisc_t *));
#endif /* WITH_CGRAPH */
GD_alg(g) = dg; /* store derived graph for closing later */
for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
if (DNODE(v))
continue;
n = makeDerivedNode(dg, agnameof(v), 1, v);
DNODE(v) = n;
}
for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
for (e = agfstout(g, v); e; e = agnxtout(g, e)) {
dt = DNODE(agtail(e));
dh = DNODE(aghead(e));
if (dt != dh) {
#ifndef WITH_CGRAPH
agedge(dg, dt, dh);
#else /* WITH_CGRAPH */
agbindrec(agedge(dg, dt, dh, NULL, 1), "Agedgeinfo_t", sizeof(Agedgeinfo_t), TRUE); //node custom data
#endif /* WITH_CGRAPH */
}
}
}
ccs = ccomps(dg, &c_cnt, 0);
/* replace block nodes with block contents */
for (i = 0; i < c_cnt; i++) {
sg = ccs[i];
/* add edges: since sg is a union of components, all edges
* of any node should be added, except loops.
*/
for (n = agfstnode(sg); n; n = agnxtnode(sg, n)) {
p = ORIGN(n);
for (e = agfstout(g, p); e; e = agnxtout(g, e)) {
/* n = DNODE(agtail(e)); by construction since agtail(e) == p */
dh = DNODE(aghead(e));
if (n != dh) {
#ifndef WITH_CGRAPH
ep = agedge(dg, n, dh);
aginsert(sg, ep);
#else /* WITH_CGRAPH */
ep = agedge(dg, n, dh, NULL, 1);
agbindrec(ep, "Agedgeinfo_t", sizeof(Agedgeinfo_t), TRUE); //node custom data
agsubedge(sg,ep,1);
#endif /* WITH_CGRAPH */
}
}
}
}
/* Finally, add edge data to edges */
for (n = agfstnode(dg); n; n = agnxtnode(dg, n)) {
for (e = agfstout(dg, n); e; e = agnxtout(dg, e)) {
ED_alg(e) = NEW(edata);
}
}
*cnt = c_cnt;
return ccs;
}
/* closeDerivedGraph:
*/
static void closeDerivedGraph(graph_t * g)
{
node_t *n;
edge_t *e;
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
free(ED_alg(e));
}
free(ND_alg(n));
free(ND_pos(n));
}
agclose(g);
}
/* copyPosns:
* Copy position of nodes in given subgraph of derived graph
* to corresponding node in original graph.
* FIX: consider assigning from n directly to ORIG(n).
*/
static void copyPosns(graph_t * g)
{
node_t *n;
node_t *v;
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
v = ORIGN(n);
ND_pos(v)[0] = ND_pos(n)[0];
ND_pos(v)[1] = ND_pos(n)[1];
}
}
/* circoLayout:
*/
void circoLayout(Agraph_t * g)
{
Agraph_t **ccs;
Agraph_t *sg;
int ncc;
int i;
if (agnnodes(g)) {
ccs = circomps(g, &ncc);
if (ncc == 1) {
circularLayout(ccs[0]);
copyPosns(ccs[0]);
adjustNodes(g);
} else {
Agraph_t *dg = ccs[0]->root;
pack_info pinfo;
getPackInfo(g, l_node, CL_OFFSET, &pinfo);
for (i = 0; i < ncc; i++) {
sg = ccs[i];
circularLayout(sg);
adjustNodes(sg);
}
/* FIX: splines have not been calculated for dg
* To use, either do splines in dg and copy to g, or
* construct components of g from ccs and use that in packing.
*/
packSubgraphs(ncc, ccs, dg, &pinfo);
for (i = 0; i < ncc; i++)
copyPosns(ccs[i]);
}
free(ccs);
}
}
/* circo_layout:
*/
void circo_layout(Agraph_t * g)
{
if (agnnodes(g) == 0) return;
circo_init_graph(g);
circoLayout(g);
/* Release ND_alg as we need to reuse it during edge routing */
free(ND_alg(agfstnode(g)));
spline_edges(g);
dotneato_postprocess(g);
}
void circo_cleanup(graph_t * g)
{
node_t *n;
edge_t *e;
n = agfstnode(g);
if (n == NULL)
return; /* g is empty */
closeDerivedGraph((graph_t*)GD_alg(g)); /* delete derived graph */
for (; n; n = agnxtnode(g, n)) {
for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
gv_cleanup_edge(e);
}
gv_cleanup_node(n);
}
free(GD_neato_nlist(g));
if (g != agroot(g))
#ifndef WITH_CGRAPH
memset(&(g->u), 0, sizeof(Agraphinfo_t));
#else /* WITH_CGRAPH */
agclean (g,AGRAPH,"Agraphinfo_t");
#endif /* WITH_CGRAPH */
}
|