112a942a7ecd238ebad67190ca10b6bd5eafbed7
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 "libavoid/geomtypes.h"
24 #include <boost/graph/kamada_kawai_spring_layout.hpp>
25 #include <boost/graph/circle_layout.hpp>
26 #include <boost/graph/adjacency_list.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 // 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<Avoid::Point> PositionVec;
41 typedef iterator_property_map<PositionVec::iterator, property_map<Graph, vertex_index_t>::type> PositionMap;
43 bool isConnector(SPItem *i) {
44 SPPath *path = NULL;
45 if(SP_IS_PATH(i)) {
46 path = SP_PATH(i);
47 }
48 return path && path->connEndPair.isAutoRoutingConn();
49 }
50 #endif // HAVE_BOOST_GRAPH_LIB
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;
68 Graph g;
70 double minX=DBL_MAX, minY=DBL_MAX, maxX=-DBL_MAX, maxY=-DBL_MAX;
72 std::map<std::string,Vertex> nodelookup;
73 std::vector<std::string> labels;
74 for (std::list<SPItem *>::iterator it(selected.begin());
75 it != selected.end();
76 ++it)
77 {
78 SPItem *u=*it;
79 if(!isConnector(u)) {
80 std::cout<<"Creating node for id: "<<u->id<<std::endl;
81 nodelookup[u->id]=add_vertex(g);
82 labels.push_back(u->id);
83 }
84 }
86 int n=labels.size();
87 //Check 2 or more selected objects
88 if (n < 2) return;
90 WeightMap weightmap=get(edge_weight, g);
91 int i=0;
92 for (std::list<SPItem *>::iterator it(selected.begin());
93 it != selected.end();
94 ++it)
95 {
96 using NR::X; using NR::Y;
97 SPItem *itu=*it;
98 Vertex u=nodelookup[itu->id];
99 GSList *nlist=itu->avoidRef->getAttachedShapes(Avoid::ConnRef::runningFrom);
100 std::list<SPItem *> neighbours;
101 neighbours.insert<GSListConstIterator<SPItem *> >(neighbours.end(),nlist,NULL);
102 for (std::list<SPItem *>::iterator ne(neighbours.begin());
103 ne != neighbours.end();
104 ++ne) {
106 SPItem *itv=*ne;
107 Vertex v=nodelookup[itv->id];
108 Graph::edge_descriptor e; bool inserted;
109 tie(e, inserted)=add_edge(u,v,g);
110 weightmap[e]=1.0;
111 }
112 if(nlist) {
113 g_slist_free(nlist);
114 }
115 NR::Rect const item_box(sp_item_bbox_desktop(*it));
117 NR::Point ll(item_box.min());
118 minX=std::min(ll[0],minX);
119 minY=std::min(ll[1],minY);
120 NR::Point ur(item_box.max());
121 maxX=std::max(ur[0],maxX);
122 maxY=std::max(ur[1],maxY);
123 }
124 double width=maxX-minX;
125 double height=maxY-minY;
126 std::cout<<"Graph has |V|="<<num_vertices(g)<<" Width="<<width<<" Height="<<height<<std::endl;
127 PositionVec position_vec(num_vertices(g));
128 PositionMap position(position_vec.begin(), get(vertex_index, g));
129 //write_graphviz(std::cout, g, make_label_writer<std::vector<std::string>>(labels));
130 circle_graph_layout(g, position, width/2.0);
131 kamada_kawai_spring_layout(g, position, weightmap, side_length(width));
133 graph_traits<Graph>::vertex_iterator vi, vi_end;
134 i=0;
135 for (std::list<SPItem *>::iterator it(selected.begin());
136 it != selected.end();
137 ++it)
138 {
139 SPItem *u=*it;
140 if(!isConnector(u)) {
141 NR::Rect const item_box(sp_item_bbox_desktop(u));
142 NR::Point const curr(item_box.midpoint());
143 NR::Point const dest(minX+width/2.0+position[nodelookup[u->id]].x,
144 minY+height/2.0+position[nodelookup[u->id]].y);
145 sp_item_move_rel(u, NR::translate(dest - curr));
146 }
147 }
148 #else
149 std::cout<<"Connector network layout not available! Install boost graph library and recompile to enable."<<std::endl;
150 #endif // HAVE_BOOST_GRAPH_LIB
151 }