Code

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