Debian Codegraph @

lib/fdpgen/fdpinit.c

raw
1 /**
2 * @file
3 * @brief API fdpgen/fdp.h: @ref fdp_init_node_edge, @ref fdp_cleanup
4 */
5
6 /*************************************************************************
7 * Copyright (c) 2011 AT&T Intellectual Property
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * https://www.eclipse.org/legal/epl-v10.html
12 *
13 * Contributors: Details at https://graphviz.org
14 *************************************************************************/
15
16 #include "config.h"
17
18 /* fdpinit.c:
19 * Written by Emden R. Gansner
20 *
21 * Mostly boilerplate initialization and cleanup code.
22 */
23
24 /* uses PRIVATE interface */
25 #define FDP_PRIVATE 1
26
27 #include <fdpgen/tlayout.h>
28 #include <neatogen/neatoprocs.h>
29 #include <stdbool.h>
30 #include <util/agxbuf.h>
31 #include <util/alloc.h>
32
33 static void initialPositions(graph_t * g)
34 {
35 int i;
36 node_t *np;
37 attrsym_t *possym;
38 attrsym_t *pinsym;
39 double *pvec;
40 char *p;
41 char c;
42
43 possym = agattr_text(g,AGNODE, "pos", NULL);
44 if (!possym)
45 return;
46 pinsym = agattr_text(g,AGNODE, "pin", NULL);
47 for (i = 0; (np = GD_neato_nlist(g)[i]); i++) {
48 p = agxget(np, possym);
49 if (p[0]) {
50 pvec = ND_pos(np);
51 c = '\0';
52 if (sscanf(p, "%lf,%lf%c", pvec, pvec + 1, &c) >= 2) {
53 if (PSinputscale > 0.0) {
54 int j;
55 for (j = 0; j < NDIM; j++)
56 pvec[j] = pvec[j] / PSinputscale;
57 }
58 ND_pinned(np) = P_SET;
59 if (c == '!'
60 || (pinsym && mapbool(agxget(np, pinsym))))
61 ND_pinned(np) = P_PIN;
62 } else
63 fprintf(stderr,
64 "Warning: node %s, position %s, expected two floats\n",
65 agnameof(np), p);
66 }
67 }
68 }
69
70 /* init_edge:
71 */
72 static void init_edge(edge_t * e, attrsym_t * E_len)
73 {
74 agbindrec(e, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true); //node custom data
75 ED_factor(e) = late_double(e, E_weight, 1.0, 0.0);
76 ED_dist(e) = late_double(e, E_len, fdp_parms->K, 0.0);
77
78 common_init_edge(e);
79 }
80
81 static void init_node(node_t * n)
82 {
83 common_init_node(n);
84 ND_pos(n) = gv_calloc(GD_ndim(agraphof(n)), sizeof(double));
85 gv_nodesize(n, GD_flip(agraphof(n)));
86 }
87
88 void fdp_init_node_edge(graph_t * g)
89 {
90 attrsym_t *E_len;
91 node_t *n;
92 edge_t *e;
93 int nn;
94 int i;
95
96 aginit(g, AGNODE, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true);
97 processClusterEdges(g);
98
99 /* Get node count after processClusterEdges(), as this function may
100 * add new nodes.
101 */
102 nn = agnnodes(g);
103 GD_neato_nlist(g) = gv_calloc(nn + 1, sizeof(node_t*));
104
105 for (i = 0, n = agfstnode(g); n; n = agnxtnode(g, n)) {
106 init_node (n);
107 GD_neato_nlist(g)[i] = n;
108 ND_id(n) = i++;
109 }
110
111 E_len = agattr_text(g,AGEDGE, "len", NULL);
112 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
113 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
114 init_edge(e, E_len);
115 }
116 }
117 initialPositions(g);
118 }
119
120 static void cleanup_subgs(graph_t * g)
121 {
122 graph_t *subg;
123 int i;
124
125 for (i = 1; i <= GD_n_cluster(g); i++) {
126 subg = GD_clust(g)[i];
127 free_label(GD_label(subg));
128 if (GD_alg(subg)) {
129 free(PORTS(subg));
130 free(GD_alg(subg));
131 }
132 cleanup_subgs(subg);
133 }
134 free (GD_clust(g));
135 }
136
137 static void fdp_cleanup_graph(graph_t * g)
138 {
139 cleanup_subgs(g);
140 free(GD_neato_nlist(g));
141 free(GD_alg(g));
142 }
143
144 void fdp_cleanup(graph_t * g)
145 {
146 node_t *n;
147 edge_t *e;
148
149 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
150 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
151 gv_cleanup_edge(e);
152 }
153 gv_cleanup_node(n);
154 }
155 fdp_cleanup_graph(g);
156 }
157
158 /**
159 * @dir lib/fdpgen
160 * @brief [Force-Directed Placement](https://en.wikipedia.org/wiki/Force-directed_graph_drawing) layout engine, API fdpgen/fdp.h
161 * @ingroup engines
162 *
163 * [FDP layout user manual](https://graphviz.org/docs/layouts/fdp/)
164 *
165 * Other @ref engines
166 */