Code

e6b0173f87aa2350cebbd334c90f9d0e03635736
[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>
33 using namespace boost;
35 struct simple_point {
36         double x;
37         double y;
38 };
39 // create a typedef for the Graph type
40 typedef adjacency_list<vecS, vecS, undirectedS, no_property, 
41         property<edge_weight_t, double> > Graph;
42 typedef property_map<Graph, edge_weight_t>::type WeightMap;
43 typedef graph_traits<Graph>::vertex_descriptor Vertex;
44 typedef std::vector<simple_point> PositionVec;
45 typedef iterator_property_map<PositionVec::iterator, property_map<Graph, vertex_index_t>::type> PositionMap;
47 bool isConnector(SPItem *i) {
48         SPPath *path = NULL;
49         if(SP_IS_PATH(i)) {
50                 path = SP_PATH(i);
51         }
52         return path && path->connEndPair.isAutoRoutingConn();
53 }
54 #endif // HAVE_BOOST_GRAPH_LIB
55 /**
56 * Takes a list of inkscape items, extracts the graph defined by 
57 * connectors between them, and uses graph layout techniques to find
58 * a nice layout
59 */
60 void graphlayout(GSList const *const items) {
61         if(!items) {
62                 return;
63         }
64 #ifdef HAVE_BOOST_GRAPH_LIB
67         using Inkscape::Util::GSListConstIterator;
68         std::list<SPItem *> selected;
69         selected.insert<GSListConstIterator<SPItem *> >(selected.end(), items, NULL);
70         if (selected.empty()) 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         std::vector<std::string> labels;
78         for (std::list<SPItem *>::iterator it(selected.begin());
79                 it != selected.end();
80                 ++it)
81         {
82                 SPItem *u=*it;
83                 if(!isConnector(u)) {
84                         std::cout<<"Creating node for id: "<<u->id<<std::endl;
85                         nodelookup[u->id]=add_vertex(g);
86                         labels.push_back(u->id);
87                 }
88         }
90         int n=labels.size();
91         //Check 2 or more selected objects
92         if (n < 2) return;
94         WeightMap weightmap=get(edge_weight, g);
95         int i=0;
96         for (std::list<SPItem *>::iterator it(selected.begin());
97                 it != selected.end();
98                 ++it)
99         {
100                 using NR::X; using NR::Y;
101                 SPItem *itu=*it;
102                 Vertex u=nodelookup[itu->id];
103                 GSList *nlist=itu->avoidRef->getAttachedConnectors(Avoid::ConnRef::runningFrom);
104                 std::list<SPItem *> neighbours;
105                 neighbours.insert<GSListConstIterator<SPItem *> >(neighbours.end(),nlist,NULL);
106                 for (std::list<SPItem *>::iterator ne(neighbours.begin());
107                                 ne != neighbours.end();
108                                 ++ne) {
109                         
110                         SPItem *itv=*ne;
111                         Vertex v=nodelookup[itv->id];
112                         Graph::edge_descriptor e; bool inserted;
113                         tie(e, inserted)=add_edge(u,v,g);
114                         weightmap[e]=1.0;
115                 }
116                 if(nlist) {
117                         g_slist_free(nlist);
118                 }
119                 NR::Rect const item_box(sp_item_bbox_desktop(*it));
120                         
121                 NR::Point ll(item_box.min());
122                 minX=std::min(ll[0],minX);
123                 minY=std::min(ll[1],minY);
124                 NR::Point ur(item_box.max());
125                 maxX=std::max(ur[0],maxX);
126                 maxY=std::max(ur[1],maxY);
127         }
128         double width=maxX-minX;
129         double height=maxY-minY;
130         std::cout<<"Graph has |V|="<<num_vertices(g)<<" Width="<<width<<" Height="<<height<<std::endl;
131         PositionVec position_vec(num_vertices(g));
132         PositionMap position(position_vec.begin(), get(vertex_index, g));
133         //write_graphviz(std::cout, g, make_label_writer<std::vector<std::string>>(labels));
134         circle_graph_layout(g, position, width/2.0);
135         kamada_kawai_spring_layout(g, position, weightmap, side_length(width));
137         graph_traits<Graph>::vertex_iterator vi, vi_end;
138         i=0;
139         for (std::list<SPItem *>::iterator it(selected.begin());
140                 it != selected.end();
141                 ++it)
142         {
143                 SPItem *u=*it;
144                 if(!isConnector(u)) {
145                         NR::Rect const item_box(sp_item_bbox_desktop(u));
146                         NR::Point const curr(item_box.midpoint());
147                         NR::Point const dest(minX+width/2.0+position[nodelookup[u->id]].x,
148                                         minY+height/2.0+position[nodelookup[u->id]].y);
149                         sp_item_move_rel(u, NR::translate(dest - curr));
150                 }
151         }
152 #else
153         std::cout<<"Connector network layout not available!  Install boost graph library and recompile to enable."<<std::endl;
154 #endif // HAVE_BOOST_GRAPH_LIB