1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/** \file
* Interface between Inkscape code (SPItem) and graphlayout functions.
*/
/*
* Authors:
* Tim Dwyer <tgdwyer@gmail.com>
*
* Copyright (C) 2005 Authors
*
* Released under GNU GPL. Read the file 'COPYING' for more information.
*/
#include "graphlayout/graphlayout.h"
#include <iostream>
#include <config.h>
#ifdef HAVE_BOOST_GRAPH_LIB
#include "sp-path.h"
#include "sp-item.h"
#include "sp-item-transform.h"
#include "sp-conn-end-pair.h"
#include "conn-avoid-ref.h"
#include "libavoid/connector.h"
#include <boost/graph/kamada_kawai_spring_layout.hpp>
#include <boost/graph/circle_layout.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/simple_point.hpp>
#include <boost/graph/graphviz.hpp>
#include <map>
#include <vector>
#include <algorithm>
#include <float.h>
#include <string.h>
using namespace boost;
// create a typedef for the Graph type
typedef adjacency_list<vecS, vecS, undirectedS, no_property,
property<edge_weight_t, double> > Graph;
typedef property_map<Graph, edge_weight_t>::type WeightMap;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef std::vector<simple_point<double> > PositionVec;
typedef iterator_property_map<PositionVec::iterator, property_map<Graph, vertex_index_t>::type> PositionMap;
#endif // HAVE_BOOST_GRAPH_LIB
bool isConnector(SPItem *i) {
SPPath *path = NULL;
if(SP_IS_PATH(i)) {
path = SP_PATH(i);
}
return path && path->connEndPair.isAutoRoutingConn();
}
/**
* Takes a list of inkscape items, extracts the graph defined by
* connectors between them, and uses graph layout techniques to find
* a nice layout
*/
void graphlayout(GSList const *const items) {
if(!items) {
return;
}
#ifdef HAVE_BOOST_GRAPH_LIB
using Inkscape::Util::GSListConstIterator;
std::list<SPItem *> selected;
selected.insert<GSListConstIterator<SPItem *> >(selected.end(), items, NULL);
if (selected.empty()) return;
int n=selected.size();
//Check 2 or more selected objects
if (n < 2) return;
Graph g;
double minX=DBL_MAX, minY=DBL_MAX, maxX=-DBL_MAX, maxY=-DBL_MAX;
std::map<std::string,Vertex> nodelookup;
for (std::list<SPItem *>::iterator it(selected.begin());
it != selected.end();
++it)
{
SPItem *u=*it;
if(!isConnector(u)) {
std::cout<<"Creating node for id: "<<u->id<<std::endl;
nodelookup[u->id]=add_vertex(g);
}
}
WeightMap weightmap=get(edge_weight, g);
int i=0;
for (std::list<SPItem *>::iterator it(selected.begin());
it != selected.end();
++it)
{
using NR::X; using NR::Y;
SPItem *itu=*it;
Vertex u=nodelookup[itu->id];
GSList *nlist=itu->avoidRef->getAttachedConnectors(Avoid::ConnRef::runningFrom);
std::list<SPItem *> neighbours;
neighbours.insert<GSListConstIterator<SPItem *> >(neighbours.end(),nlist,NULL);
for (std::list<SPItem *>::iterator ne(neighbours.begin());
ne != neighbours.end();
++ne) {
SPItem *itv=*ne;
Vertex v=nodelookup[itv->id];
Graph::edge_descriptor e; bool inserted;
tie(e, inserted)=add_edge(u,v,g);
weightmap[e]=1.0;
}
if(nlist) {
g_slist_free(nlist);
}
NR::Rect const item_box(sp_item_bbox_desktop(*it));
NR::Point ll(item_box.min());
minX=std::min(ll[0],minX);
minY=std::min(ll[1],minY);
NR::Point ur(item_box.max());
maxX=std::max(ur[0],maxX);
maxY=std::max(ur[1],maxY);
}
double width=maxX-minX;
double height=maxY-minY;
std::cout<<"Graph has |V|="<<num_vertices(g)<<" Width="<<width<<" Height="<<height<<std::endl;
PositionVec position_vec(num_vertices(g));
PositionMap position(position_vec.begin(), get(vertex_index, g));
write_graphviz(std::cout, g);
circle_graph_layout(g, position, width/2.0);
kamada_kawai_spring_layout(g, position, weightmap, side_length(width));
graph_traits<Graph>::vertex_iterator vi, vi_end;
i=0;
for (std::list<SPItem *>::iterator it(selected.begin());
it != selected.end();
++it)
{
SPItem *u=*it;
if(!isConnector(u)) {
NR::Rect const item_box(sp_item_bbox_desktop(u));
NR::Point const curr(item_box.midpoint());
NR::Point const dest(minX+width/2.0+position[nodelookup[u->id]].x,
minY+height/2.0+position[nodelookup[u->id]].y);
sp_item_move_rel(u, NR::translate(dest - curr));
}
}
#else
std::cout<<"Connector network layout not available! Install boost graph library and recompile to enable."<<std::endl;
#endif // HAVE_BOOST_GRAPH_LIB
}
|