Debian Codegraph @

lib/twopigen/twopiinit.c

raw
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 * Written by Emden R. Gansner
14 * Derived from Graham Wills' algorithm described in GD'97.
15 */
16
17 #include "config.h"
18
19 #include <cgraph/cgraph.h>
20 #include <twopigen/circle.h>
21 #include <neatogen/adjust.h>
22 #include <pack/pack.h>
23 #include <neatogen/neatoprocs.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <util/alloc.h>
27
28 static void twopi_init_edge(edge_t * e)
29 {
30 agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //edge custom data
31 common_init_edge(e);
32 ED_factor(e) = late_double(e, E_weight, 1.0, 0.0);
33 }
34
35 static void twopi_init_node_edge(graph_t * g)
36 {
37 node_t *n;
38 edge_t *e;
39 int i = 0;
40 int n_nodes = agnnodes(g);
41
42 rdata* alg = gv_calloc(n_nodes, sizeof(rdata));
43 GD_neato_nlist(g) = gv_calloc(n_nodes + 1, sizeof(node_t*));
44 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
45 neato_init_node(n);
46 ND_alg(n) = alg + i;
47 GD_neato_nlist(g)[i++] = n;
48 }
49 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
50 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
51 twopi_init_edge(e);
52 }
53 }
54 }
55
56 void twopi_init_graph(graph_t * g)
57 {
58 setEdgeType (g, EDGETYPE_LINE);
59 /* GD_ndim(g) = late_int(g,agfindgraphattr(g,"dim"),2,2); */
60 Ndim = GD_ndim(agroot(g)) = 2; /* The algorithm only makes sense in 2D */
61 twopi_init_node_edge(g);
62 }
63
64 static Agnode_t* findRootNode (Agraph_t* sg, Agsym_t* rootattr)
65 {
66 Agnode_t* n;
67
68 for (n = agfstnode(sg); n; n = agnxtnode(sg,n)) {
69 if (mapbool(agxget(n,rootattr))) return n;
70 }
71 return NULL;
72
73 }
74
75 /* twopi_layout:
76 */
77 void twopi_layout(Agraph_t * g)
78 {
79 Agnode_t *ctr = 0;
80 char *s;
81 int setRoot = 0;
82 int setLocalRoot = 0;
83 pointf sc;
84 int r;
85 Agsym_t* rootattr;
86
87 if (agnnodes(g) == 0) return;
88
89 twopi_init_graph(g);
90 if ((s = agget(g, "root"))) {
91 if (*s) {
92 ctr = agfindnode(g, s);
93 if (!ctr) {
94 agwarningf("specified root node \"%s\" was not found.", s);
95 agerr(AGPREV, "Using default calculation for root node\n");
96 setRoot = 1;
97 }
98 }
99 else {
100 setRoot = 1;
101 }
102 }
103 if ((rootattr = agattr_text(g, AGNODE, "root", 0))) {
104 setLocalRoot = 1;
105 }
106
107 if ((s = agget(g, "scale")) && *s) {
108 if ((r = sscanf (s, "%lf,%lf",&sc.x,&sc.y))) {
109 if (r == 1) sc.y = sc.x;
110 }
111 }
112
113 if (agnnodes(g)) {
114 Agraph_t **ccs;
115 Agraph_t *sg;
116 Agnode_t *c = NULL;
117 Agnode_t *n;
118 Agnode_t* lctr;
119
120 size_t ncc;
121 ccs = ccomps(g, &ncc, 0);
122 if (ncc == 1) {
123 if (ctr)
124 lctr = ctr;
125 else if (!rootattr || !(lctr = findRootNode(g, rootattr)))
126 lctr = 0;
127 c = circleLayout(g, lctr);
128 if (setRoot && !ctr)
129 ctr = c;
130 if (setLocalRoot && !lctr)
131 agxset (c, rootattr, "1");
132 n = agfstnode(g);
133 free(ND_alg(n));
134 ND_alg(n) = NULL;
135 adjustNodes(g);
136 spline_edges(g);
137 } else {
138 pack_info pinfo;
139 getPackInfo (g, l_node, CL_OFFSET, &pinfo);
140 pinfo.doSplines = false;
141
142 for (size_t i = 0; i < ncc; i++) {
143 sg = ccs[i];
144 if (ctr && agcontains(sg, ctr))
145 lctr = ctr;
146 else if (!rootattr || !(lctr = findRootNode(sg, rootattr)))
147 lctr = 0;
148 (void)graphviz_node_induce(sg, NULL);
149 c = circleLayout(sg, lctr);
150 if (setRoot && !ctr)
151 ctr = c;
152 if (setLocalRoot && (!lctr || (lctr == ctr)))
153 agxset (c, rootattr, "1");
154 adjustNodes(sg);
155 }
156 n = agfstnode(g);
157 free(ND_alg(n));
158 ND_alg(n) = NULL;
159 packSubgraphs(ncc, ccs, g, &pinfo);
160 spline_edges(g);
161 }
162 for (size_t i = 0; i < ncc; i++) {
163 agdelete(g, ccs[i]);
164 }
165 free(ccs);
166 }
167 if (setRoot)
168 agset (g, "root", agnameof (ctr));
169 dotneato_postprocess(g);
170
171 }
172
173 static void twopi_cleanup_graph(graph_t * g)
174 {
175 free(GD_neato_nlist(g));
176 }
177
178 /* twopi_cleanup:
179 * The ND_alg data used by twopi is freed in twopi_layout
180 * before edge routing as edge routing may use this field.
181 */
182 void twopi_cleanup(graph_t * g)
183 {
184 node_t *n;
185 edge_t *e;
186
187 n = agfstnode (g);
188 if (!n) return; /* empty graph */
189 /* free (ND_alg(n)); */
190 for (; n; n = agnxtnode(g, n)) {
191 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
192 gv_cleanup_edge(e);
193 }
194 gv_cleanup_node(n);
195 }
196 twopi_cleanup_graph(g);
197 }
198
199 /**
200 * @dir lib/twopigen
201 * @brief radial layout engine, API twopigen/circle.h
202 * @ingroup engines
203 *
204 * Twopi means 2π.
205 *
206 * [Twopi layout user manual](https://graphviz.org/docs/layouts/twopi/)
207 *
208 * Other @ref engines
209 */