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
|
/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Details at https://graphviz.org
*************************************************************************/
/*
* Written by Emden R. Gansner
* Derived from Graham Wills' algorithm described in GD'97.
*/
#include <cgraph/cgraph.h>
#include <twopigen/circle.h>
#include <neatogen/adjust.h>
#include <pack/pack.h>
#include <neatogen/neatoprocs.h>
#include <stdbool.h>
#include <stddef.h>
#include <util/alloc.h>
static void twopi_init_edge(edge_t * e)
{
agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //edge custom data
common_init_edge(e);
ED_factor(e) = late_double(e, E_weight, 1.0, 0.0);
}
static void twopi_init_node_edge(graph_t * g)
{
node_t *n;
edge_t *e;
int i = 0;
int n_nodes = agnnodes(g);
rdata* alg = gv_calloc(n_nodes, sizeof(rdata));
GD_neato_nlist(g) = gv_calloc(n_nodes + 1, sizeof(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)) {
twopi_init_edge(e);
}
}
}
void twopi_init_graph(graph_t * g)
{
setEdgeType (g, EDGETYPE_LINE);
/* GD_ndim(g) = late_int(g,agfindgraphattr(g,"dim"),2,2); */
Ndim = GD_ndim(agroot(g)) = 2; /* The algorithm only makes sense in 2D */
twopi_init_node_edge(g);
}
static Agnode_t* findRootNode (Agraph_t* sg, Agsym_t* rootattr)
{
Agnode_t* n;
for (n = agfstnode(sg); n; n = agnxtnode(sg,n)) {
if (mapbool(agxget(n,rootattr))) return n;
}
return NULL;
}
/* twopi_layout:
*/
void twopi_layout(Agraph_t * g)
{
Agnode_t *ctr = 0;
char *s;
int setRoot = 0;
int setLocalRoot = 0;
pointf sc;
int r;
Agsym_t* rootattr;
if (agnnodes(g) == 0) return;
twopi_init_graph(g);
if ((s = agget(g, "root"))) {
if (*s) {
ctr = agfindnode(g, s);
if (!ctr) {
agwarningf("specified root node \"%s\" was not found.", s);
agerr(AGPREV, "Using default calculation for root node\n");
setRoot = 1;
}
}
else {
setRoot = 1;
}
}
if ((rootattr = agattr_text(g, AGNODE, "root", 0))) {
setLocalRoot = 1;
}
if ((s = agget(g, "scale")) && *s) {
if ((r = sscanf (s, "%lf,%lf",&sc.x,&sc.y))) {
if (r == 1) sc.y = sc.x;
}
}
if (agnnodes(g)) {
Agraph_t **ccs;
Agraph_t *sg;
Agnode_t *c = NULL;
Agnode_t *n;
Agnode_t* lctr;
size_t ncc;
ccs = ccomps(g, &ncc, 0);
if (ncc == 1) {
if (ctr)
lctr = ctr;
else if (!rootattr || !(lctr = findRootNode(g, rootattr)))
lctr = 0;
c = circleLayout(g, lctr);
if (setRoot && !ctr)
ctr = c;
if (setLocalRoot && !lctr)
agxset (c, rootattr, "1");
n = agfstnode(g);
free(ND_alg(n));
ND_alg(n) = NULL;
adjustNodes(g);
spline_edges(g);
} else {
pack_info pinfo;
getPackInfo (g, l_node, CL_OFFSET, &pinfo);
pinfo.doSplines = false;
for (size_t i = 0; i < ncc; i++) {
sg = ccs[i];
if (ctr && agcontains(sg, ctr))
lctr = ctr;
else if (!rootattr || !(lctr = findRootNode(sg, rootattr)))
lctr = 0;
(void)graphviz_node_induce(sg, NULL);
c = circleLayout(sg, lctr);
if (setRoot && !ctr)
ctr = c;
if (setLocalRoot && (!lctr || (lctr == ctr)))
agxset (c, rootattr, "1");
adjustNodes(sg);
}
n = agfstnode(g);
free(ND_alg(n));
ND_alg(n) = NULL;
packSubgraphs(ncc, ccs, g, &pinfo);
spline_edges(g);
}
for (size_t i = 0; i < ncc; i++) {
agdelete(g, ccs[i]);
}
free(ccs);
}
if (setRoot)
agset (g, "root", agnameof (ctr));
dotneato_postprocess(g);
}
static void twopi_cleanup_graph(graph_t * g)
{
free(GD_neato_nlist(g));
}
/* twopi_cleanup:
* The ND_alg data used by twopi is freed in twopi_layout
* before edge routing as edge routing may use this field.
*/
void twopi_cleanup(graph_t * g)
{
node_t *n;
edge_t *e;
n = agfstnode (g);
if (!n) return; /* empty graph */
/* free (ND_alg(n)); */
for (; n; n = agnxtnode(g, n)) {
for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
gv_cleanup_edge(e);
}
gv_cleanup_node(n);
}
twopi_cleanup_graph(g);
}
/**
* @dir lib/twopigen
* @brief radial layout engine, API twopigen/circle.h
* @ingroup engines
*
* Twopi means 2π.
*
* [Twopi layout user manual](https://graphviz.org/docs/layouts/twopi/)
*
* Other @ref engines
*/
|