Debian Codegraph @

lib/sfdpgen/sfdpinit.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 #include "config.h"
13 #include <float.h>
14 #include <limits.h>
15 #include <sfdpgen/sfdp.h>
16 #include <neatogen/neato.h>
17 #include <neatogen/adjust.h>
18 #include <pack/pack.h>
19 #include <assert.h>
20 #include <sfdpgen/spring_electrical.h>
21 #include <neatogen/overlap.h>
22 #include <sfdpgen/stress_model.h>
23 #include <cgraph/cgraph.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <util/alloc.h>
27 #include <util/gv_ctype.h>
28 #include <util/strcasecmp.h>
29
30 static void sfdp_init_edge(edge_t * e)
31 {
32 agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //node custom data
33 common_init_edge(e);
34 }
35
36 static void sfdp_init_node_edge(graph_t * g)
37 {
38 node_t *n;
39 edge_t *e;
40
41 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
42 neato_init_node(n);
43 }
44 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
45 for (e = agfstout(g, n); e; e = agnxtout(g, e))
46 sfdp_init_edge(e);
47 }
48 }
49
50 static void sfdp_init_graph(Agraph_t * g)
51 {
52 int outdim;
53
54 setEdgeType(g, EDGETYPE_LINE);
55 outdim = late_int(g, agfindgraphattr(g, "dimen"), 2, 2);
56 GD_ndim(agroot(g)) = late_int(g, agfindgraphattr(g, "dim"), outdim, 2);
57 Ndim = GD_ndim(agroot(g)) = MIN(GD_ndim(agroot(g)), MAXDIM);
58 GD_odim(agroot(g)) = MIN(outdim, Ndim);
59 sfdp_init_node_edge(g);
60 }
61
62 /* getPos:
63 */
64 static double *getPos(Agraph_t * g)
65 {
66 Agnode_t *n;
67 double *pos = gv_calloc(Ndim * agnnodes(g), sizeof(double));
68 int ix, i;
69
70 if (agfindnodeattr(g, "pos") == NULL)
71 return pos;
72
73 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
74 i = ND_id(n);
75 if (hasPos(n)) {
76 for (ix = 0; ix < Ndim; ix++) {
77 pos[i * Ndim + ix] = ND_pos(n)[ix];
78 }
79 }
80 }
81
82 return pos;
83 }
84
85 static void sfdpLayout(graph_t * g, spring_electrical_control *ctrl,
86 pointf pad) {
87 double *sizes;
88 double *pos;
89 Agnode_t *n;
90 int flag, i;
91 int n_edge_label_nodes = 0, *edge_label_nodes = NULL;
92 SparseMatrix A = makeMatrix(g);
93
94 if (ctrl->overlap >= 0) {
95 if (ctrl->edge_labeling_scheme > 0)
96 sizes = getSizes(g, pad, &n_edge_label_nodes, &edge_label_nodes);
97 else
98 sizes = getSizes(g, pad, NULL, NULL);
99 }
100 else
101 sizes = NULL;
102 pos = getPos(g);
103
104 multilevel_spring_electrical_embedding(Ndim, A, ctrl, sizes, pos, n_edge_label_nodes, edge_label_nodes, &flag);
105
106 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
107 double *npos = pos + (Ndim * ND_id(n));
108 for (i = 0; i < Ndim; i++) {
109 ND_pos(n)[i] = npos[i];
110 }
111 }
112
113 free(sizes);
114 free(pos);
115 SparseMatrix_delete (A);
116 free(edge_label_nodes);
117 }
118
119 static int
120 late_smooth (graph_t* g, Agsym_t* sym, int dflt)
121 {
122 char* s;
123 int v;
124 int rv;
125
126 if (!sym) return dflt;
127 s = agxget (g, sym);
128 if (gv_isdigit(*s)) {
129 #if defined(HAVE_GTS)
130 if ((v = atoi (s)) <= SMOOTHING_RNG)
131 #else
132 if ((v = atoi (s)) <= SMOOTHING_SPRING)
133 #endif
134 rv = v;
135 else
136 rv = dflt;
137 }
138 else if (gv_isalpha(*s)) {
139 if (!strcasecmp(s, "avg_dist"))
140 rv = SMOOTHING_STRESS_MAJORIZATION_AVG_DIST;
141 else if (!strcasecmp(s, "graph_dist"))
142 rv = SMOOTHING_STRESS_MAJORIZATION_GRAPH_DIST;
143 else if (!strcasecmp(s, "none"))
144 rv = SMOOTHING_NONE;
145 else if (!strcasecmp(s, "power_dist"))
146 rv = SMOOTHING_STRESS_MAJORIZATION_POWER_DIST;
147 #if defined(HAVE_GTS)
148 else if (!strcasecmp(s, "rng"))
149 rv = SMOOTHING_RNG;
150 #endif
151 else if (!strcasecmp(s, "spring"))
152 rv = SMOOTHING_SPRING;
153 #if defined(HAVE_GTS)
154 else if (!strcasecmp(s, "triangle"))
155 rv = SMOOTHING_TRIANGLE;
156 #endif
157 else
158 rv = dflt;
159 }
160 else
161 rv = dflt;
162 return rv;
163 }
164
165 static int
166 late_quadtree_scheme (graph_t* g, Agsym_t* sym, int dflt)
167 {
168 char* s;
169 int v;
170 int rv;
171
172 if (!sym) return dflt;
173 s = agxget (g, sym);
174 if (gv_isdigit(*s)) {
175 if ((v = atoi (s)) <= QUAD_TREE_FAST && v >= QUAD_TREE_NONE){
176 rv = v;
177 } else {
178 rv = dflt;
179 }
180 } else if (gv_isalpha(*s)) {
181 if (!strcasecmp(s, "none") || !strcasecmp(s, "false") ){
182 rv = QUAD_TREE_NONE;
183 } else if (!strcasecmp(s, "normal") || !strcasecmp(s, "true") || !strcasecmp(s, "yes")){
184 rv = QUAD_TREE_NORMAL;
185 } else if (!strcasecmp(s, "fast")){
186 rv = QUAD_TREE_FAST;
187 } else {
188 rv = dflt;
189 }
190 } else {
191 rv = dflt;
192 }
193 return rv;
194 }
195
196
197 /* tuneControl:
198 * Use user values to reset control
199 */
200 static void tuneControl(graph_t *g, spring_electrical_control *ctrl) {
201 long seed;
202 int init;
203
204 seed = ctrl->random_seed;
205 init = setSeed (g, INIT_RANDOM, &seed);
206 if (init != INIT_RANDOM) {
207 agwarningf("sfdp only supports start=random\n");
208 }
209 ctrl->random_seed = seed;
210
211 ctrl->K = late_double(g, agfindgraphattr(g, "K"), -1.0, 0.0);
212 ctrl->p = -1.0*late_double(g, agfindgraphattr(g, "repulsiveforce"), -AUTOP, 0.0);
213 ctrl->multilevels = late_int(g, agfindgraphattr(g, "levels"), INT_MAX, 0);
214 ctrl->smoothing = late_smooth(g, agfindgraphattr(g, "smoothing"), SMOOTHING_NONE);
215 ctrl->tscheme = late_quadtree_scheme(g, agfindgraphattr(g, "quadtree"), QUAD_TREE_NORMAL);
216 ctrl->beautify_leaves = mapbool(agget(g, "beautify"));
217 ctrl->do_shrinking = mapBool(agget(g, "overlap_shrink"), true);
218 ctrl->rotation = late_double(g, agfindgraphattr(g, "rotation"), 0.0, -DBL_MAX);
219 ctrl->edge_labeling_scheme = late_int(g, agfindgraphattr(g, "label_scheme"), 0, 0);
220 if (ctrl->edge_labeling_scheme > 4) {
221 agwarningf("label_scheme = %d > 4 : ignoring\n", ctrl->edge_labeling_scheme);
222 ctrl->edge_labeling_scheme = 0;
223 }
224 }
225
226 void sfdp_layout(graph_t * g)
227 {
228 int doAdjust;
229 adjust_data am;
230 sfdp_init_graph(g);
231 doAdjust = (Ndim == 2);
232
233 if (agnnodes(g)) {
234 Agraph_t **ccs;
235 Agraph_t *sg;
236 expand_t sep;
237 pointf pad;
238 spring_electrical_control ctrl = spring_electrical_control_new();
239
240 tuneControl(g, &ctrl);
241 #if defined(HAVE_GTS)
242 graphAdjustMode(g, &am, "prism0");
243 #else
244 graphAdjustMode(g, &am, 0);
245 #endif
246
247 pad.x = PS2INCH(DFLT_MARGIN);
248 pad.y = PS2INCH(DFLT_MARGIN);
249
250 if ((am.mode == AM_PRISM) && doAdjust) {
251 doAdjust = 0; /* overlap removal done in sfdp */
252 ctrl.overlap = am.value;
253 ctrl.initial_scaling = am.scaling;
254 sep = sepFactor(g);
255 if (sep.doAdd) {
256 pad.x = PS2INCH(sep.x);
257 pad.y = PS2INCH(sep.y);
258 }
259 }
260 else {
261 /* Turn off overlap removal in sfdp if prism not used */
262 ctrl.overlap = -1;
263 }
264
265 if (Verbose)
266 spring_electrical_control_print(ctrl);
267
268 size_t ncc;
269 ccs = ccomps(g, &ncc, 0);
270 if (ncc == 1) {
271 sfdpLayout(g, &ctrl, pad);
272 if (doAdjust) removeOverlapWith(g, &am);
273 spline_edges(g);
274 } else {
275 pack_info pinfo;
276 getPackInfo(g, l_node, CL_OFFSET, &pinfo);
277 pinfo.doSplines = true;
278
279 for (size_t i = 0; i < ncc; i++) {
280 sg = ccs[i];
281 (void)graphviz_node_induce(sg, NULL);
282 sfdpLayout(sg, &ctrl, pad);
283 if (doAdjust) removeOverlapWith(sg, &am);
284 setEdgeType(sg, EDGETYPE_LINE);
285 spline_edges(sg);
286 }
287 packSubgraphs(ncc, ccs, g, &pinfo);
288 }
289 for (size_t i = 0; i < ncc; i++) {
290 agdelete(g, ccs[i]);
291 }
292 free(ccs);
293 }
294
295 dotneato_postprocess(g);
296 }
297
298 void sfdp_cleanup(graph_t * g)
299 {
300 node_t *n;
301 edge_t *e;
302
303 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
304 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
305 gv_cleanup_edge(e);
306 }
307 gv_cleanup_node(n);
308 }
309 }
310
311 /**
312 * @dir lib/sfdpgen
313 * @brief scalable [Force-Directed Placement](https://en.wikipedia.org/wiki/Force-directed_graph_drawing) layout engine, API sfdpgen/sfdp.h
314 * @ingroup engines
315 *
316 * [SFDP layout user manual](https://graphviz.org/docs/layouts/sfdp/)
317 *
318 * Other @ref engines
319 */