a99fdd720f6e9fdfeb09d8d3facdf6389c319fe5
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 /**
45 * Takes a list of inkscape items, extracts the graph defined by
46 * connectors between them, and uses graph layout techniques to find
47 * a nice layout
48 */
49 void graphlayout(GSList const *const items) {
50 if(!items) {
51 return;
52 }
53 #ifdef HAVE_BOOST_GRAPH_LIB
56 using Inkscape::Util::GSListConstIterator;
57 std::list<SPItem *> selected;
58 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), items, NULL);
59 if (selected.empty()) return;
60 int n=selected.size();
62 //Check 2 or more selected objects
63 if (n < 2) return;
65 Graph g;
67 double minX=DBL_MAX, minY=DBL_MAX, maxX=-DBL_MAX, maxY=-DBL_MAX;
69 std::map<std::string,Vertex> nodelookup;
70 for (std::list<SPItem *>::iterator it(selected.begin());
71 it != selected.end();
72 ++it)
73 {
74 SPItem *u=*it;
75 std::cout<<"id:"<<u->id<<std::endl;
76 SPPath *path = NULL;
77 if(SP_IS_PATH(u)) {
78 path = SP_PATH(u);
79 }
80 bool isConn = path && path->connEndPair.isAutoRoutingConn();
81 if(!isConn) {
82 std::cout<<" is a node."<<std::endl;
83 nodelookup[u->id]=add_vertex(g);
84 }
85 }
86 WeightMap weightmap=get(edge_weight, g);
87 std::cout<<"Added vertices: Graph has |V|="<<num_vertices(g)<<std::endl;
89 NR::Point const gap(0, 0);
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 std::cout<<" Node "<<itu->id<<" has "<<neighbours.size()<<" neighbours"<<std::endl;
102 for (std::list<SPItem *>::iterator ne(neighbours.begin());
103 ne != neighbours.end();
104 ++ne) {
106 SPItem *itv=*ne;
107 std::cout<<"neighbour: "<<itv->id<<std::endl;
108 Vertex v=nodelookup[itv->id];
109 std::cout<<" Neighbour: "<<v;
110 Graph::edge_descriptor e; bool inserted;
111 tie(e, inserted)=add_edge(u,v,g);
112 weightmap[e]=1.0;
113 }
114 if(nlist) {
115 g_slist_free(nlist);
116 }
117 NR::Rect const item_box(sp_item_bbox_desktop(*it));
119 NR::Point ll(item_box.min());
120 minX=std::min(ll[0],minX);
121 minY=std::min(ll[1],minY);
122 NR::Point ur(item_box.max());
123 maxX=std::max(ur[0],maxX);
124 maxY=std::max(ur[1],maxY);
125 }
126 double width=maxX-minX;
127 double height=maxY-minY;
128 std::cout<<"Graph has |V|="<<num_vertices(g)<<" Width="<<width<<" Height="<<height<<std::endl;
129 PositionVec position_vec(num_vertices(g));
130 PositionMap position(position_vec.begin(), get(vertex_index, g));
131 write_graphviz(std::cout, g);
132 circle_graph_layout(g, position, width/2.0);
133 kamada_kawai_spring_layout(g, position, weightmap, side_length(width));
135 graph_traits<Graph>::vertex_iterator vi, vi_end;
136 i=0;
137 for (std::list<SPItem *>::iterator it(selected.begin());
138 it != selected.end();
139 ++it)
140 {
141 SPItem *u=*it;
142 if(strncmp(u->id,"path",4)) {
143 NR::Rect const item_box(sp_item_bbox_desktop(u));
144 NR::Point const curr(item_box.midpoint());
145 NR::Point const dest(minX+width/2.0+position[nodelookup[u->id]].x,
146 minY+height/2.0+position[nodelookup[u->id]].y);
147 sp_item_move_rel(u, NR::translate(dest - curr));
148 }
149 }
150 #else
151 std::cout<<"Connector network layout not available! Install boost graph library and recompile to enable."<<std::endl;
152 #endif // HAVE_BOOST_GRAPH_LIB
153 }