Code

dff414895517ea6897c352234e6e920729540744
[inkscape.git] / src / graphlayout / graphlayout.cpp
1 /** \file
2  * Interface between Inkscape code (SPItem) and graphlayout functions.
3  */
4 /*
5 * Authors:
6 *   Tim Dwyer <tgdwyer@gmail.com>
7 *
8 * Copyright (C) 2005 Authors
9 *
10 * Released under GNU GPL.  Read the file 'COPYING' for more information.
11 */
12 #include "graphlayout/graphlayout.h"
13 #include <iostream>
14 #include <config.h>
16 #ifdef HAVE_BOOST_GRAPH_LIB
17 #include "sp-path.h"
18 #include "sp-item.h"
19 #include "sp-item-transform.h"
20 #include "sp-conn-end-pair.h"
21 #include "conn-avoid-ref.h"
22 #include "libavoid/connector.h"
23 #include <boost/graph/kamada_kawai_spring_layout.hpp>
24 #include <boost/graph/circle_layout.hpp>
25 #include <boost/graph/adjacency_list.hpp>
26 #include <boost/graph/simple_point.hpp>
27 #include <boost/graph/graphviz.hpp>
28 #include <map>
29 #include <vector>
30 #include <algorithm>
31 #include <float.h>
32 #include <string.h>
34 using namespace boost;
35 // create a typedef for the Graph type
36 typedef adjacency_list<vecS, vecS, undirectedS, no_property, 
37         property<edge_weight_t, double> > Graph;
38 typedef property_map<Graph, edge_weight_t>::type WeightMap;
39 typedef graph_traits<Graph>::vertex_descriptor Vertex;
40 typedef std::vector<simple_point<double> > PositionVec;
41 typedef iterator_property_map<PositionVec::iterator, property_map<Graph, vertex_index_t>::type> PositionMap;
42 #endif // HAVE_BOOST_GRAPH_LIB
44 bool isConnector(SPItem *i) {
45         SPPath *path = NULL;
46         if(SP_IS_PATH(i)) {
47                 path = SP_PATH(i);
48         }
49         return path && path->connEndPair.isAutoRoutingConn();
50 }
51 /**
52 * Takes a list of inkscape items, extracts the graph defined by 
53 * connectors between them, and uses graph layout techniques to find
54 * a nice layout
55 */
56 void graphlayout(GSList const *const items) {
57         if(!items) {
58                 return;
59         }
60 #ifdef HAVE_BOOST_GRAPH_LIB
63         using Inkscape::Util::GSListConstIterator;
64         std::list<SPItem *> selected;
65         selected.insert<GSListConstIterator<SPItem *> >(selected.end(), items, NULL);
66         if (selected.empty()) return;
67         int n=selected.size();
69         //Check 2 or more selected objects
70         if (n < 2) return;
72         Graph g;
74         double minX=DBL_MAX, minY=DBL_MAX, maxX=-DBL_MAX, maxY=-DBL_MAX;
76         std::map<std::string,Vertex> nodelookup;
77         for (std::list<SPItem *>::iterator it(selected.begin());
78                 it != selected.end();
79                 ++it)
80         {
81                 SPItem *u=*it;
82                 if(!isConnector(u)) {
83                         std::cout<<"Creating node for id: "<<u->id<<std::endl;
84                         nodelookup[u->id]=add_vertex(g);
85                 }
86         }
88         WeightMap weightmap=get(edge_weight, g);
89         int i=0;
90         for (std::list<SPItem *>::iterator it(selected.begin());
91                 it != selected.end();
92                 ++it)
93         {
94                 using NR::X; using NR::Y;
95                 SPItem *itu=*it;
96                 Vertex u=nodelookup[itu->id];
97                 GSList *nlist=itu->avoidRef->getAttachedConnectors(Avoid::ConnRef::runningFrom);
98                 std::list<SPItem *> neighbours;
99                 neighbours.insert<GSListConstIterator<SPItem *> >(neighbours.end(),nlist,NULL);
100                 for (std::list<SPItem *>::iterator ne(neighbours.begin());
101                                 ne != neighbours.end();
102                                 ++ne) {
103                         
104                         SPItem *itv=*ne;
105                         Vertex v=nodelookup[itv->id];
106                         Graph::edge_descriptor e; bool inserted;
107                         tie(e, inserted)=add_edge(u,v,g);
108                         weightmap[e]=1.0;
109                 }
110                 if(nlist) {
111                         g_slist_free(nlist);
112                 }
113                 NR::Rect const item_box(sp_item_bbox_desktop(*it));
114                         
115                 NR::Point ll(item_box.min());
116                 minX=std::min(ll[0],minX);
117                 minY=std::min(ll[1],minY);
118                 NR::Point ur(item_box.max());
119                 maxX=std::max(ur[0],maxX);
120                 maxY=std::max(ur[1],maxY);
121         }
122         double width=maxX-minX;
123         double height=maxY-minY;
124         std::cout<<"Graph has |V|="<<num_vertices(g)<<" Width="<<width<<" Height="<<height<<std::endl;
125         PositionVec position_vec(num_vertices(g));
126         PositionMap position(position_vec.begin(), get(vertex_index, g));
127         write_graphviz(std::cout, g);
128         circle_graph_layout(g, position, width/2.0);
129         kamada_kawai_spring_layout(g, position, weightmap, side_length(width));
131         graph_traits<Graph>::vertex_iterator vi, vi_end;
132         i=0;
133         for (std::list<SPItem *>::iterator it(selected.begin());
134                 it != selected.end();
135                 ++it)
136         {
137                 SPItem *u=*it;
138                 if(!isConnector(u)) {
139                         NR::Rect const item_box(sp_item_bbox_desktop(u));
140                         NR::Point const curr(item_box.midpoint());
141                         NR::Point const dest(minX+width/2.0+position[nodelookup[u->id]].x,
142                                         minY+height/2.0+position[nodelookup[u->id]].y);
143                         sp_item_move_rel(u, NR::translate(dest - curr));
144                 }
145         }
146 #else
147         std::cout<<"Connector network layout not available!  Install boost graph library and recompile to enable."<<std::endl;
148 #endif // HAVE_BOOST_GRAPH_LIB