annotate toolboxes/graph_visualisation/share/graphviz/doc/addingLayout.txt @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 To create a new layout plugin called xxx, you first need
Daniel@0 2 to provide two functions: xxx_layout and xxx_cleanup. The
Daniel@0 3 semantics of these are described below.
Daniel@0 4
Daniel@0 5 ========================
Daniel@0 6
Daniel@0 7 void xxx_layout(Agraph_t * g)
Daniel@0 8
Daniel@0 9 Initialize the graph.
Daniel@0 10 - If the algorithm will use the common edge routing code, it should
Daniel@0 11 call setEdgeType (g, ...);
Daniel@0 12
Daniel@0 13 - For each node, call common_init_node and gv_nodesize.
Daniel@0 14
Daniel@0 15 If the algorithm will use spline_edges() to route the edges, the
Daniel@0 16 node coordinates need to be stored in ND_pos, so this should be
Daniel@0 17 allocated here. This, and the two calls mentioned above, are all
Daniel@0 18 handled by a call to neato_init_node().
Daniel@0 19
Daniel@0 20 - For each edge, call common_init_edge
Daniel@0 21
Daniel@0 22 - The algorithm should allocate whatever other data structures it
Daniel@0 23 needs. This can involve fields in the A*info_t fields. In addition,
Daniel@0 24 each of these fields contains a void* alg; subfield that the algorithm
Daniel@0 25 can use the store additional data.
Daniel@0 26 Once we move to cgraph, this will all be replace with
Daniel@0 27 algorithm specific records.
Daniel@0 28
Daniel@0 29 Layout the graph. When finished, each node should have its coordinates
Daniel@0 30 stored in points in ND_coord_i(n), each edge should have its layout
Daniel@0 31 described in ED_spl(e).
Daniel@0 32 (N.B. As of version 2.21, ND_coord_i has been replaced by ND_coord,
Daniel@0 33 which are now floating point coordinates.)
Daniel@0 34
Daniel@0 35 To add edges, there are 3 functions available:
Daniel@0 36
Daniel@0 37 - spline_edges1 (Agraph_t*, int edgeType)
Daniel@0 38 Assumes the node coordinates are stored in ND_coord_i, and that
Daniel@0 39 GD_bb is set. For each edge, this function constructs the appropriate
Daniel@0 40 data and stores it in ED_spl.
Daniel@0 41 - spline_edges0 (Agraph_t*)
Daniel@0 42 Assumes the node coordinates are stored in ND_pos, and that
Daniel@0 43 GD_bb is set. This function uses the ratio attribute if set,
Daniel@0 44 copies the values in ND_pos to ND_coord_i (converting from
Daniel@0 45 inches to points); and calls spline_edges1 using the edge type
Daniel@0 46 specified by setEdgeType().
Daniel@0 47 - spline_edges (Agraph_t*)
Daniel@0 48 Assumes the node coordinates are stored in ND_pos. This
Daniel@0 49 function calculates the bounding box of g and stores it in GD_bb,
Daniel@0 50 then calls spline_edges0().
Daniel@0 51
Daniel@0 52 If the algorithm only works with connected components, the code can
Daniel@0 53 use the pack library to get components, lay them out individually, and
Daniel@0 54 pack them together based on user specifications. A typical schema is
Daniel@0 55 given below. One can look at the code for twopi, circo, neato or fdp
Daniel@0 56 for more detailed examples.
Daniel@0 57
Daniel@0 58 Agraph_t **ccs;
Daniel@0 59 Agraph_t *sg;
Daniel@0 60 Agnode_t *c = NULL;
Daniel@0 61 int ncc;
Daniel@0 62 int i;
Daniel@0 63
Daniel@0 64 ccs = ccomps(g, &ncc, 0);
Daniel@0 65 if (ncc == 1) {
Daniel@0 66 /* layout nodes of g */
Daniel@0 67 adjustNodes(g); /* if you need to remove overlaps */
Daniel@0 68 spline_edges(g); /* generic edge routing code */
Daniel@0 69
Daniel@0 70 } else {
Daniel@0 71 pack_info pinfo;
Daniel@0 72 pack_mode pmode = getPackMode(g, l_node);
Daniel@0 73
Daniel@0 74 for (i = 0; i < ncc; i++) {
Daniel@0 75 sg = ccs[i];
Daniel@0 76 /* layout sg */
Daniel@0 77 adjustNodes(sg); /* if you need to remove overlaps */
Daniel@0 78 }
Daniel@0 79 spline_edges(g); /* generic edge routing */
Daniel@0 80
Daniel@0 81 /* initialize packing info, e.g. */
Daniel@0 82 pinfo.margin = getPack(g, CL_OFFSET, CL_OFFSET);
Daniel@0 83 pinfo.doSplines = 1;
Daniel@0 84 pinfo.mode = pmode;
Daniel@0 85 pinfo.fixed = 0;
Daniel@0 86 packSubgraphs(ncc, ccs, g, &pinfo);
Daniel@0 87 }
Daniel@0 88 for (i = 0; i < ncc; i++) {
Daniel@0 89 agdelete(g, ccs[i]);
Daniel@0 90 }
Daniel@0 91
Daniel@0 92 free(ccs);
Daniel@0 93
Daniel@0 94 Be careful in laying of subgraphs if you rely on attributes that have
Daniel@0 95 only been set in the root graph. With connected components, edges can
Daniel@0 96 be added with each component, before packing (as above) or after the
Daniel@0 97 components have been packed (see circo).
Daniel@0 98
Daniel@0 99 It good to check for trivial cases where the graph has 0 or 1 nodes,
Daniel@0 100 or no edges.
Daniel@0 101
Daniel@0 102 At the end of xxx_layout, call
Daniel@0 103
Daniel@0 104 dotneato_postprocess(g);
Daniel@0 105
Daniel@0 106 The following template will work in most cases, ignoring the problems of
Daniel@0 107 handling disconnected graphs and removing node overlaps:
Daniel@0 108
Daniel@0 109 static void
Daniel@0 110 xxx_init_node(node_t * n)
Daniel@0 111 {
Daniel@0 112 neato_init_node(n);
Daniel@0 113 /* add algorithm-specific data, if desired */
Daniel@0 114 }
Daniel@0 115
Daniel@0 116 static void
Daniel@0 117 xxx_init_edge(edge_t * e)
Daniel@0 118 {
Daniel@0 119 common_init_edge(e);
Daniel@0 120 /* add algorithm-specific data, if desired */
Daniel@0 121 }
Daniel@0 122
Daniel@0 123 static void
Daniel@0 124 xxx_init_node_edge(graph_t * g)
Daniel@0 125 {
Daniel@0 126 node_t *n;
Daniel@0 127 edge_t *e;
Daniel@0 128
Daniel@0 129 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
Daniel@0 130 xxx_init_node(n);
Daniel@0 131 }
Daniel@0 132 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
Daniel@0 133 for (e = agfstout(g, n); e; e = agnxtout(g, e)){
Daniel@0 134 xxx_init_edge(e);
Daniel@0 135 }
Daniel@0 136 }
Daniel@0 137 }
Daniel@0 138
Daniel@0 139 void
Daniel@0 140 xxx_layout (Agraph_t* g)
Daniel@0 141 {
Daniel@0 142 xxx_init_node_edge(g);
Daniel@0 143 /* Set ND_pos(n) for each node n */
Daniel@0 144 spline_edges(g);
Daniel@0 145 dotneato_postprocess(g);
Daniel@0 146 }
Daniel@0 147
Daniel@0 148 ======================
Daniel@0 149
Daniel@0 150 void xxx_cleanup(Agraph_t * g)
Daniel@0 151
Daniel@0 152 Free up any resources allocated in the layout.
Daniel@0 153
Daniel@0 154 Finish with calls to gv_cleanup_node and gv_cleanup_edge for
Daniel@0 155 each node and edge. This cleans up splines labels, ND_pos, shapes
Daniel@0 156 and 0's out the A*info_t, so these have to occur last, but could be
Daniel@0 157 part of explicit xxx_cleanup_node and xxx_cleanup_edge, if desired.
Daniel@0 158 At the end, you should do
Daniel@0 159
Daniel@0 160 if (g != g->root) memset(&(g->u), 0, sizeof(Agraphinfo_t));
Daniel@0 161
Daniel@0 162 This is necessary for the graph to be laid out again, as the layout
Daniel@0 163 code assumes this structure is clean.
Daniel@0 164
Daniel@0 165 libgvc does a final cleanup to the root graph, freeing any drawing,
Daniel@0 166 freeing its label, and zeroing out Agraphinfo_t of the root graph.
Daniel@0 167
Daniel@0 168 The following template will work in most cases:
Daniel@0 169
Daniel@0 170 static void xxx_cleanup_graph(Agraph_t * g)
Daniel@0 171 {
Daniel@0 172 /* Free any algorithm-specific data attached to the graph */
Daniel@0 173 if (g != g->root) memset(&(g->u), 0, sizeof(Agraphinfo_t));
Daniel@0 174 }
Daniel@0 175
Daniel@0 176 static void xxx_cleanup_edge (Agedge_t* e)
Daniel@0 177 {
Daniel@0 178 /* Free any algorithm-specific data attached to the edge */
Daniel@0 179 gv_cleanup_edge(e);
Daniel@0 180 }
Daniel@0 181
Daniel@0 182 static void xxx_cleanup_node (Agnode_t* n)
Daniel@0 183 {
Daniel@0 184 /* Free any algorithm-specific data attached to the node */
Daniel@0 185 gv_cleanup_node(e);
Daniel@0 186 }
Daniel@0 187
Daniel@0 188 void xxx_cleanup(Agraph_t * g)
Daniel@0 189 {
Daniel@0 190 Agnode_t *n;
Daniel@0 191 Agedge_t *e;
Daniel@0 192
Daniel@0 193 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
Daniel@0 194 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
Daniel@0 195 xxx_cleanup_edge(e);
Daniel@0 196 }
Daniel@0 197 xxx_cleanup_node(n);
Daniel@0 198 }
Daniel@0 199 xxx_cleanup_graph(g);
Daniel@0 200 }
Daniel@0 201
Daniel@0 202 ==================
Daniel@0 203
Daniel@0 204 Most layouts use auxiliary routines similar to neato, so
Daniel@0 205 the entry points can be added in plugin/neato_layout
Daniel@0 206
Daniel@0 207 Add to gvlayout_neato_layout.c:
Daniel@0 208
Daniel@0 209 gvlayout_engine_t xxxgen_engine = {
Daniel@0 210 xxx_layout,
Daniel@0 211 xxx_cleanup,
Daniel@0 212 };
Daniel@0 213
Daniel@0 214 and the line
Daniel@0 215
Daniel@0 216 {LAYOUT_XXX, "xxx", 0, &xxxgen_engine, &neatogen_features},
Daniel@0 217
Daniel@0 218 to gvlayout_neato_types and a new emum
Daniel@0 219
Daniel@0 220 LAYOUT_XXX
Daniel@0 221
Daniel@0 222 to layout_type in that file.
Daniel@0 223
Daniel@0 224 The above allows the new layout to piggyback on top of the neato
Daniel@0 225 plugin, but requires rebuilding the plugin. In general, a user
Daniel@0 226 can (and probably should) build a layout plugin totally separately.
Daniel@0 227
Daniel@0 228 To do this, after writing xxx_layout and xxx_cleanup, it is necessary to:
Daniel@0 229
Daniel@0 230 - add the types and data structures
Daniel@0 231
Daniel@0 232 typedef enum { LAYOUT_XXX } layout_type;
Daniel@0 233
Daniel@0 234 static gvlayout_features_t xxxgen_features = {
Daniel@0 235 0
Daniel@0 236 };
Daniel@0 237 gvlayout_engine_t xxxgen_engine = {
Daniel@0 238 xxx_layout,
Daniel@0 239 xxx_cleanup,
Daniel@0 240 };
Daniel@0 241 static gvplugin_installed_t gvlayout_xxx_types[] = {
Daniel@0 242 {LAYOUT_XXX, "xxx", 0, &xxxgen_engine, &xxxgen_features},
Daniel@0 243 {0, NULL, 0, NULL, NULL}
Daniel@0 244 };
Daniel@0 245 static gvplugin_api_t apis[] = {
Daniel@0 246 {API_layout, &gvlayout_xxx_types},
Daniel@0 247 {(api_t)0, 0},
Daniel@0 248 };
Daniel@0 249 gvplugin_library_t gvplugin_xxx_layout_LTX_library = { "xxx_layout", apis };
Daniel@0 250
Daniel@0 251 - combine all of this into a dynamic library whose name contains the
Daniel@0 252 string "gvplugin_" and install the library in the same directory as the
Daniel@0 253 other Graphviz plugins. For example, on Linux systems, the dot layout
Daniel@0 254 plugin is in the library libgvplugin_dot_layout.so.
Daniel@0 255
Daniel@0 256 - run
Daniel@0 257 dot -c
Daniel@0 258 to regenerate the config file.
Daniel@0 259
Daniel@0 260 NOTES:
Daniel@0 261 - Additional layouts can be added as extra lines in gvlayout_xxx_types.
Daniel@0 262 - Obviously, most of the names and strings can be arbitrary. One
Daniel@0 263 constraint is that external identifier for the gvplugin_library_t
Daniel@0 264 type must end in "_LTX_library". In addition, the string "xxx" in
Daniel@0 265 each entry of gvlayout_xxx_types is the name used to identify the
Daniel@0 266 layout algorithm, so needs to be distinct from any other layout name.
Daniel@0 267 - The features of a layout algorithm are currently limited to a
Daniel@0 268 flag of bits, and the only flag supported is LAYOUT_USES_RANKDIR,
Daniel@0 269 which enables the layout to the rankdir attribute.
Daniel@0 270
Daniel@0 271 Changes need to be made to any applications, such as gvedit, that
Daniel@0 272 statically know about layout algorithms.
Daniel@0 273
Daniel@0 274 ==================
Daniel@0 275
Daniel@0 276 Software configuration - automake
Daniel@0 277
Daniel@0 278 If you want to integrate your code into the Graphviz software
Daniel@0 279 and use its build system, follow the instructions below.
Daniel@0 280 You can certainly build and install your plugin using your own
Daniel@0 281 build software.
Daniel@0 282
Daniel@0 283 0. Put your software in lib/xxxgen, and added the hooks describe above
Daniel@0 284 into gvlayout_neato_layout.c
Daniel@0 285 1. In lib/xxxgen, provide a Makefile.am (based on a simple example
Daniel@0 286 like lib/fdpgen/Makefile.am)
Daniel@0 287 3. In lib/Makefile.am, add xxxgen to SUBDIRS
Daniel@0 288 2. In configure.ac, add lib/xxxgen/Makefile to AC_CONFIG_FILES.
Daniel@0 289 4. In lib/plugin/neato_layout/Makefile.am, insert
Daniel@0 290 $(top_builddir)/lib/xxxgen/libxxxgen_C.la
Daniel@0 291 in libgvplugin_neato_layout_C_la_LIBADD
Daniel@0 292 5. Remember to run autogen.sh because on its own configure can guess wrong.
Daniel@0 293
Daniel@0 294 This also assumes you have a good version of the various automake tools
Daniel@0 295 on your system.
Daniel@0 296
Daniel@0 297