Daniel@0: To create a new layout plugin called xxx, you first need Daniel@0: to provide two functions: xxx_layout and xxx_cleanup. The Daniel@0: semantics of these are described below. Daniel@0: Daniel@0: ======================== Daniel@0: Daniel@0: void xxx_layout(Agraph_t * g) Daniel@0: Daniel@0: Initialize the graph. Daniel@0: - If the algorithm will use the common edge routing code, it should Daniel@0: call setEdgeType (g, ...); Daniel@0: Daniel@0: - For each node, call common_init_node and gv_nodesize. Daniel@0: Daniel@0: If the algorithm will use spline_edges() to route the edges, the Daniel@0: node coordinates need to be stored in ND_pos, so this should be Daniel@0: allocated here. This, and the two calls mentioned above, are all Daniel@0: handled by a call to neato_init_node(). Daniel@0: Daniel@0: - For each edge, call common_init_edge Daniel@0: Daniel@0: - The algorithm should allocate whatever other data structures it Daniel@0: needs. This can involve fields in the A*info_t fields. In addition, Daniel@0: each of these fields contains a void* alg; subfield that the algorithm Daniel@0: can use the store additional data. Daniel@0: Once we move to cgraph, this will all be replace with Daniel@0: algorithm specific records. Daniel@0: Daniel@0: Layout the graph. When finished, each node should have its coordinates Daniel@0: stored in points in ND_coord_i(n), each edge should have its layout Daniel@0: described in ED_spl(e). Daniel@0: (N.B. As of version 2.21, ND_coord_i has been replaced by ND_coord, Daniel@0: which are now floating point coordinates.) Daniel@0: Daniel@0: To add edges, there are 3 functions available: Daniel@0: Daniel@0: - spline_edges1 (Agraph_t*, int edgeType) Daniel@0: Assumes the node coordinates are stored in ND_coord_i, and that Daniel@0: GD_bb is set. For each edge, this function constructs the appropriate Daniel@0: data and stores it in ED_spl. Daniel@0: - spline_edges0 (Agraph_t*) Daniel@0: Assumes the node coordinates are stored in ND_pos, and that Daniel@0: GD_bb is set. This function uses the ratio attribute if set, Daniel@0: copies the values in ND_pos to ND_coord_i (converting from Daniel@0: inches to points); and calls spline_edges1 using the edge type Daniel@0: specified by setEdgeType(). Daniel@0: - spline_edges (Agraph_t*) Daniel@0: Assumes the node coordinates are stored in ND_pos. This Daniel@0: function calculates the bounding box of g and stores it in GD_bb, Daniel@0: then calls spline_edges0(). Daniel@0: Daniel@0: If the algorithm only works with connected components, the code can Daniel@0: use the pack library to get components, lay them out individually, and Daniel@0: pack them together based on user specifications. A typical schema is Daniel@0: given below. One can look at the code for twopi, circo, neato or fdp Daniel@0: for more detailed examples. Daniel@0: Daniel@0: Agraph_t **ccs; Daniel@0: Agraph_t *sg; Daniel@0: Agnode_t *c = NULL; Daniel@0: int ncc; Daniel@0: int i; Daniel@0: Daniel@0: ccs = ccomps(g, &ncc, 0); Daniel@0: if (ncc == 1) { Daniel@0: /* layout nodes of g */ Daniel@0: adjustNodes(g); /* if you need to remove overlaps */ Daniel@0: spline_edges(g); /* generic edge routing code */ Daniel@0: Daniel@0: } else { Daniel@0: pack_info pinfo; Daniel@0: pack_mode pmode = getPackMode(g, l_node); Daniel@0: Daniel@0: for (i = 0; i < ncc; i++) { Daniel@0: sg = ccs[i]; Daniel@0: /* layout sg */ Daniel@0: adjustNodes(sg); /* if you need to remove overlaps */ Daniel@0: } Daniel@0: spline_edges(g); /* generic edge routing */ Daniel@0: Daniel@0: /* initialize packing info, e.g. */ Daniel@0: pinfo.margin = getPack(g, CL_OFFSET, CL_OFFSET); Daniel@0: pinfo.doSplines = 1; Daniel@0: pinfo.mode = pmode; Daniel@0: pinfo.fixed = 0; Daniel@0: packSubgraphs(ncc, ccs, g, &pinfo); Daniel@0: } Daniel@0: for (i = 0; i < ncc; i++) { Daniel@0: agdelete(g, ccs[i]); Daniel@0: } Daniel@0: Daniel@0: free(ccs); Daniel@0: Daniel@0: Be careful in laying of subgraphs if you rely on attributes that have Daniel@0: only been set in the root graph. With connected components, edges can Daniel@0: be added with each component, before packing (as above) or after the Daniel@0: components have been packed (see circo). Daniel@0: Daniel@0: It good to check for trivial cases where the graph has 0 or 1 nodes, Daniel@0: or no edges. Daniel@0: Daniel@0: At the end of xxx_layout, call Daniel@0: Daniel@0: dotneato_postprocess(g); Daniel@0: Daniel@0: The following template will work in most cases, ignoring the problems of Daniel@0: handling disconnected graphs and removing node overlaps: Daniel@0: Daniel@0: static void Daniel@0: xxx_init_node(node_t * n) Daniel@0: { Daniel@0: neato_init_node(n); Daniel@0: /* add algorithm-specific data, if desired */ Daniel@0: } Daniel@0: Daniel@0: static void Daniel@0: xxx_init_edge(edge_t * e) Daniel@0: { Daniel@0: common_init_edge(e); Daniel@0: /* add algorithm-specific data, if desired */ Daniel@0: } Daniel@0: Daniel@0: static void Daniel@0: xxx_init_node_edge(graph_t * g) Daniel@0: { Daniel@0: node_t *n; Daniel@0: edge_t *e; Daniel@0: Daniel@0: for (n = agfstnode(g); n; n = agnxtnode(g, n)) { Daniel@0: xxx_init_node(n); Daniel@0: } Daniel@0: for (n = agfstnode(g); n; n = agnxtnode(g, n)) { Daniel@0: for (e = agfstout(g, n); e; e = agnxtout(g, e)){ Daniel@0: xxx_init_edge(e); Daniel@0: } Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: void Daniel@0: xxx_layout (Agraph_t* g) Daniel@0: { Daniel@0: xxx_init_node_edge(g); Daniel@0: /* Set ND_pos(n) for each node n */ Daniel@0: spline_edges(g); Daniel@0: dotneato_postprocess(g); Daniel@0: } Daniel@0: Daniel@0: ====================== Daniel@0: Daniel@0: void xxx_cleanup(Agraph_t * g) Daniel@0: Daniel@0: Free up any resources allocated in the layout. Daniel@0: Daniel@0: Finish with calls to gv_cleanup_node and gv_cleanup_edge for Daniel@0: each node and edge. This cleans up splines labels, ND_pos, shapes Daniel@0: and 0's out the A*info_t, so these have to occur last, but could be Daniel@0: part of explicit xxx_cleanup_node and xxx_cleanup_edge, if desired. Daniel@0: At the end, you should do Daniel@0: Daniel@0: if (g != g->root) memset(&(g->u), 0, sizeof(Agraphinfo_t)); Daniel@0: Daniel@0: This is necessary for the graph to be laid out again, as the layout Daniel@0: code assumes this structure is clean. Daniel@0: Daniel@0: libgvc does a final cleanup to the root graph, freeing any drawing, Daniel@0: freeing its label, and zeroing out Agraphinfo_t of the root graph. Daniel@0: Daniel@0: The following template will work in most cases: Daniel@0: Daniel@0: static void xxx_cleanup_graph(Agraph_t * g) Daniel@0: { Daniel@0: /* Free any algorithm-specific data attached to the graph */ Daniel@0: if (g != g->root) memset(&(g->u), 0, sizeof(Agraphinfo_t)); Daniel@0: } Daniel@0: Daniel@0: static void xxx_cleanup_edge (Agedge_t* e) Daniel@0: { Daniel@0: /* Free any algorithm-specific data attached to the edge */ Daniel@0: gv_cleanup_edge(e); Daniel@0: } Daniel@0: Daniel@0: static void xxx_cleanup_node (Agnode_t* n) Daniel@0: { Daniel@0: /* Free any algorithm-specific data attached to the node */ Daniel@0: gv_cleanup_node(e); Daniel@0: } Daniel@0: Daniel@0: void xxx_cleanup(Agraph_t * g) Daniel@0: { Daniel@0: Agnode_t *n; Daniel@0: Agedge_t *e; Daniel@0: Daniel@0: for (n = agfstnode(g); n; n = agnxtnode(g, n)) { Daniel@0: for (e = agfstout(g, n); e; e = agnxtout(g, e)) { Daniel@0: xxx_cleanup_edge(e); Daniel@0: } Daniel@0: xxx_cleanup_node(n); Daniel@0: } Daniel@0: xxx_cleanup_graph(g); Daniel@0: } Daniel@0: Daniel@0: ================== Daniel@0: Daniel@0: Most layouts use auxiliary routines similar to neato, so Daniel@0: the entry points can be added in plugin/neato_layout Daniel@0: Daniel@0: Add to gvlayout_neato_layout.c: Daniel@0: Daniel@0: gvlayout_engine_t xxxgen_engine = { Daniel@0: xxx_layout, Daniel@0: xxx_cleanup, Daniel@0: }; Daniel@0: Daniel@0: and the line Daniel@0: Daniel@0: {LAYOUT_XXX, "xxx", 0, &xxxgen_engine, &neatogen_features}, Daniel@0: Daniel@0: to gvlayout_neato_types and a new emum Daniel@0: Daniel@0: LAYOUT_XXX Daniel@0: Daniel@0: to layout_type in that file. Daniel@0: Daniel@0: The above allows the new layout to piggyback on top of the neato Daniel@0: plugin, but requires rebuilding the plugin. In general, a user Daniel@0: can (and probably should) build a layout plugin totally separately. Daniel@0: Daniel@0: To do this, after writing xxx_layout and xxx_cleanup, it is necessary to: Daniel@0: Daniel@0: - add the types and data structures Daniel@0: Daniel@0: typedef enum { LAYOUT_XXX } layout_type; Daniel@0: Daniel@0: static gvlayout_features_t xxxgen_features = { Daniel@0: 0 Daniel@0: }; Daniel@0: gvlayout_engine_t xxxgen_engine = { Daniel@0: xxx_layout, Daniel@0: xxx_cleanup, Daniel@0: }; Daniel@0: static gvplugin_installed_t gvlayout_xxx_types[] = { Daniel@0: {LAYOUT_XXX, "xxx", 0, &xxxgen_engine, &xxxgen_features}, Daniel@0: {0, NULL, 0, NULL, NULL} Daniel@0: }; Daniel@0: static gvplugin_api_t apis[] = { Daniel@0: {API_layout, &gvlayout_xxx_types}, Daniel@0: {(api_t)0, 0}, Daniel@0: }; Daniel@0: gvplugin_library_t gvplugin_xxx_layout_LTX_library = { "xxx_layout", apis }; Daniel@0: Daniel@0: - combine all of this into a dynamic library whose name contains the Daniel@0: string "gvplugin_" and install the library in the same directory as the Daniel@0: other Graphviz plugins. For example, on Linux systems, the dot layout Daniel@0: plugin is in the library libgvplugin_dot_layout.so. Daniel@0: Daniel@0: - run Daniel@0: dot -c Daniel@0: to regenerate the config file. Daniel@0: Daniel@0: NOTES: Daniel@0: - Additional layouts can be added as extra lines in gvlayout_xxx_types. Daniel@0: - Obviously, most of the names and strings can be arbitrary. One Daniel@0: constraint is that external identifier for the gvplugin_library_t Daniel@0: type must end in "_LTX_library". In addition, the string "xxx" in Daniel@0: each entry of gvlayout_xxx_types is the name used to identify the Daniel@0: layout algorithm, so needs to be distinct from any other layout name. Daniel@0: - The features of a layout algorithm are currently limited to a Daniel@0: flag of bits, and the only flag supported is LAYOUT_USES_RANKDIR, Daniel@0: which enables the layout to the rankdir attribute. Daniel@0: Daniel@0: Changes need to be made to any applications, such as gvedit, that Daniel@0: statically know about layout algorithms. Daniel@0: Daniel@0: ================== Daniel@0: Daniel@0: Software configuration - automake Daniel@0: Daniel@0: If you want to integrate your code into the Graphviz software Daniel@0: and use its build system, follow the instructions below. Daniel@0: You can certainly build and install your plugin using your own Daniel@0: build software. Daniel@0: Daniel@0: 0. Put your software in lib/xxxgen, and added the hooks describe above Daniel@0: into gvlayout_neato_layout.c Daniel@0: 1. In lib/xxxgen, provide a Makefile.am (based on a simple example Daniel@0: like lib/fdpgen/Makefile.am) Daniel@0: 3. In lib/Makefile.am, add xxxgen to SUBDIRS Daniel@0: 2. In configure.ac, add lib/xxxgen/Makefile to AC_CONFIG_FILES. Daniel@0: 4. In lib/plugin/neato_layout/Makefile.am, insert Daniel@0: $(top_builddir)/lib/xxxgen/libxxxgen_C.la Daniel@0: in libgvplugin_neato_layout_C_la_LIBADD Daniel@0: 5. Remember to run autogen.sh because on its own configure can guess wrong. Daniel@0: Daniel@0: This also assumes you have a good version of the various automake tools Daniel@0: on your system. Daniel@0: Daniel@0: