Debian Codegraph @

lib/patchwork/patchworkinit.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 #include "config.h"
12
13 #include <assert.h>
14 #include <common/render.h>
15 #include <common/utils.h>
16 #include <patchwork/patchwork.h>
17 #include <limits.h>
18 #include <neatogen/adjust.h>
19 #include <pack/pack.h>
20 #include <neatogen/neatoprocs.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <util/list.h>
24
25 /* the following code shamelessly copied from lib/fdpgen/layout.c
26 and should be extracted and made into a common function */
27
28 typedef LIST(graph_t *) clist_t;
29
30 /* mkClusters:
31 * Attach list of immediate child clusters.
32 * NB: By convention, the indexing starts at 1.
33 * If pclist is NULL, the graph is the root graph or a cluster
34 * If pclist is non-NULL, we are recursively scanning a non-cluster
35 * subgraph for cluster children.
36 */
37 static void
38 mkClusters (graph_t * g, clist_t* pclist, graph_t* parent)
39 {
40 graph_t* subg;
41 clist_t list = {0};
42 clist_t* clist;
43
44 if (pclist == NULL) {
45 // [0] is empty. The clusters are in [1..cnt].
46 LIST_APPEND(&list, NULL);
47 clist = &list;
48 }
49 else
50 clist = pclist;
51
52 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
53 if (is_a_cluster(subg)) {
54 agbindrec(subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
55 LIST_APPEND(clist, subg);
56 mkClusters(subg, NULL, subg);
57 }
58 else {
59 mkClusters(subg, clist, parent);
60 }
61 }
62 if (pclist == NULL) {
63 assert(LIST_SIZE(&list) - 1 <= INT_MAX);
64 GD_n_cluster(g) = (int)(LIST_SIZE(&list) - 1);
65 if (LIST_SIZE(&list) > 1) {
66 LIST_SHRINK_TO_FIT(&list);
67 LIST_DETACH(&list, &GD_clust(g), NULL);
68 } else {
69 LIST_FREE(&list);
70 }
71 }
72 }
73
74 static void patchwork_init_node(node_t * n)
75 {
76 agset(n,"shape","box");
77 }
78
79 static void patchwork_init_edge(edge_t * e)
80 {
81 agbindrec(e, "Agedgeinfo_t", sizeof(Agnodeinfo_t), true); // edge custom data
82 }
83
84 static void patchwork_init_node_edge(graph_t * g)
85 {
86 node_t *n;
87 edge_t *e;
88 int i = 0;
89 assert(agnnodes(g) >= 0);
90 const size_t nnodes = (size_t)agnnodes(g);
91 rdata* alg = gv_calloc(nnodes, sizeof(rdata));
92
93 GD_neato_nlist(g) = gv_calloc(nnodes + 1, sizeof(node_t*));
94 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
95 agbindrec(n, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true); // node custom data
96 ND_alg(n) = alg + i;
97 GD_neato_nlist(g)[i++] = n;
98 patchwork_init_node(n);
99
100 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
101 patchwork_init_edge(e);
102 }
103 }
104 }
105
106 static void patchwork_init_graph(graph_t * g)
107 {
108 N_shape = agattr_text(g, AGNODE, "shape","box");
109 setEdgeType (g, EDGETYPE_LINE);
110 Ndim = GD_ndim(g) = 2; /* The algorithm only makes sense in 2D */
111 mkClusters(g, NULL, g);
112 patchwork_init_node_edge(g);
113 }
114
115 /* patchwork_layout:
116 * The current version makes no use of edges, neither for a notion of connectivity
117 * nor during drawing.
118 */
119 void patchwork_layout(Agraph_t *g)
120 {
121 patchwork_init_graph(g);
122
123 if ((agnnodes(g) == 0) && (GD_n_cluster(g) == 0)) return;
124
125 patchworkLayout (g);
126
127 dotneato_postprocess(g);
128 }
129
130 static void patchwork_cleanup_graph(graph_t * g)
131 {
132 free(GD_neato_nlist(g));
133 free(GD_clust(g));
134 }
135
136 void patchwork_cleanup(graph_t * g)
137 {
138 node_t *n;
139 edge_t *e;
140
141 n = agfstnode(g);
142 if (!n) return;
143 free (ND_alg(n));
144 for (; n; n = agnxtnode(g, n)) {
145 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
146 gv_cleanup_edge(e);
147 }
148 gv_cleanup_node(n);
149 }
150 patchwork_cleanup_graph(g);
151 }
152
153 /**
154 * @dir lib/patchwork
155 * @brief squarified [treemap](https://en.wikipedia.org/wiki/Treemapping) layout engine, API patchwork/patchwork.h
156 * @ingroup engines
157 *
158 * [Patchwork layout user manual](https://graphviz.org/docs/layouts/patchwork/)
159 *
160 * Other @ref engines
161 */