9828e22fe6f0d3a2dd22946b2f06f4ae5a00016c
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-item.h"
18 #include "sp-item-transform.h"
19 #include "sp-conn-end-pair.h"
20 #include "conn-avoid-ref.h"
21 #include "libavoid/connector.h"
22 #include <boost/graph/kamada_kawai_spring_layout.hpp>
23 #include <boost/graph/circle_layout.hpp>
24 #include <boost/graph/adjacency_list.hpp>
25 #include <boost/graph/simple_point.hpp>
26 #include <boost/graph/graphviz.hpp>
27 #include <map>
28 #include <vector>
29 #include <algorithm>
30 #include <float.h>
31 #include <string.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;
41 #endif // HAVE_BOOST_GRAPH_LIB
43 /**
44 * Takes a list of inkscape items, extracts the graph defined by
45 * connectors between them, and uses graph layout techniques to find
46 * a nice layout
47 */
48 void graphlayout(GSList const *const items) {
49 if(!items) {
50 return;
51 }
52 #ifdef HAVE_BOOST_GRAPH_LIB
55 using Inkscape::Util::GSListConstIterator;
56 std::list<SPItem *> selected;
57 selected.insert<GSListConstIterator<SPItem *> >(selected.end(), items, NULL);
58 if (selected.empty()) return;
59 int n=selected.size();
61 //Check 2 or more selected objects
62 if (n < 2) return;
64 Graph g;
66 std::cout<<"Building graph with "<<n<<" nodes"<<std::endl;
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 if(strncmp(u->id,"path",4)) {
77 nodelookup[u->id]=add_vertex(g);
78 }
79 }
80 WeightMap weightmap=get(edge_weight, g);
81 std::cout<<"Added vertices: Graph has |V|="<<num_vertices(g)<<std::endl;
83 NR::Point const gap(0, 0);
84 int i=0;
85 for (std::list<SPItem *>::iterator it(selected.begin());
86 it != selected.end();
87 ++it)
88 {
89 using NR::X; using NR::Y;
90 SPItem *itu=*it;
91 Vertex u=nodelookup[itu->id];
92 GSList *nlist=itu->avoidRef->getAttachedConnectors(Avoid::ConnRef::runningFrom);
93 std::list<SPItem *> neighbours;
94 neighbours.insert<GSListConstIterator<SPItem *> >(neighbours.end(),nlist,NULL);
95 std::cout<<" Node "<<itu->id<<" has "<<neighbours.size()<<" neighbours"<<std::endl;
96 for (std::list<SPItem *>::iterator ne(neighbours.begin());
97 ne != neighbours.end();
98 ++ne) {
100 SPItem *itv=*ne;
101 std::cout<<"neighbour: "<<itv->id<<std::endl;
102 Vertex v=nodelookup[itv->id];
103 std::cout<<" Neighbour: "<<v;
104 Graph::edge_descriptor e; bool inserted;
105 tie(e, inserted)=add_edge(u,v,g);
106 weightmap[e]=1.0;
107 }
108 if(nlist) {
109 g_slist_free(nlist);
110 }
111 NR::Rect const item_box(sp_item_bbox_desktop(*it));
113 NR::Point ll(item_box.min());
114 minX=std::min(ll[0],minX);
115 minY=std::min(ll[1],minY);
116 NR::Point ur(item_box.max());
117 maxX=std::max(ur[0],maxX);
118 maxY=std::max(ur[1],maxY);
119 }
120 double width=maxX-minX;
121 double height=maxY-minY;
122 std::cout<<"Graph has |V|="<<num_vertices(g)<<" Width="<<width<<" Height="<<height<<std::endl;
123 PositionVec position_vec(num_vertices(g));
124 PositionMap position(position_vec.begin(), get(vertex_index, g));
125 write_graphviz(std::cout, g);
126 circle_graph_layout(g, position, width/2.0);
127 kamada_kawai_spring_layout(g, position, weightmap, side_length(width));
129 graph_traits<Graph>::vertex_iterator vi, vi_end;
130 i=0;
131 for (std::list<SPItem *>::iterator it(selected.begin());
132 it != selected.end();
133 ++it)
134 {
135 SPItem *u=*it;
136 if(strncmp(u->id,"path",4)) {
137 NR::Rect const item_box(sp_item_bbox_desktop(u));
138 NR::Point const curr(item_box.midpoint());
139 NR::Point const dest(minX+width/2.0+position[nodelookup[u->id]].x,
140 minY+height/2.0+position[nodelookup[u->id]].y);
141 sp_item_move_rel(u, NR::translate(dest - curr));
142 }
143 }
144 #else
145 std::cout<<"Connector network layout not available! Install boost graph library and recompile to enable."<<std::endl;
146 #endif // HAVE_BOOST_GRAPH_LIB
147 }