Code

12bca8891c12ed96d45fac4756b910eaf672112a
[inkscape.git] / src / graphlayout / graphlayout.cpp
1 /** @file
2  * @brief Interface between Inkscape code (SPItem) and graphlayout functions.
3  */
4 /*
5  * Authors:
6  *   Tim Dwyer <Tim.Dwyer@infotech.monash.edu.au>
7  *
8  * Copyright (C) 2005 Authors
9  *
10  * Released under GNU GPL.  Read the file 'COPYING' for more information.
11  */
13 #include <iostream>
14 #include <config.h>
15 #include <map>
16 #include <vector>
17 #include <algorithm>
18 #include <cstring>
19 #include <cstdlib>
20 #include <float.h>
22 #include "desktop.h"
23 #include "inkscape.h"
24 #include "sp-namedview.h"
25 #include "util/glib-list-iterators.h"
26 #include "graphlayout/graphlayout.h"
27 #include "sp-path.h"
28 #include "sp-item.h"
29 #include "sp-item-transform.h"
30 #include "sp-conn-end-pair.h"
31 #include "style.h"
32 #include "conn-avoid-ref.h"
33 #include "libavoid/connector.h"
34 #include "libavoid/geomtypes.h"
35 #include "libcola/cola.h"
36 #include "libvpsc/generate-constraints.h"
37 #include "preferences.h"
39 using namespace std;
40 using namespace cola;
41 using namespace vpsc;
42 /**
43  * Returns true if item is a connector
44  */
45 bool isConnector(SPItem const *const i) {
46         SPPath *path = NULL;
47         if(SP_IS_PATH(i)) {
48                 path = SP_PATH(i);
49         }
50         return path && path->connEndPair.isAutoRoutingConn();
51 }
53 struct CheckProgress : TestConvergence {
54     CheckProgress(double d,unsigned i,list<SPItem *>&
55             selected,vector<Rectangle*>& rs,map<string,unsigned>& nodelookup) :
56         TestConvergence(d,i), selected(selected), rs(rs), nodelookup(nodelookup) {}
57         bool operator()(double new_stress, double* X, double* Y) {
58         /* This is where, if we wanted to animate the layout, we would need to update
59          * the positions of all objects and redraw the canvas and maybe sleep a bit
60                 cout << "stress="<<new_stress<<endl;
61         cout << "x[0]="<<rs[0]->getMinX()<<endl;
62         for (list<SPItem *>::iterator it(selected.begin());
63             it != selected.end();
64             ++it)
65         {
66             SPItem *u=*it;
67             if(!isConnector(u)) {
68                 Rectangle* r=rs[nodelookup[u->id]];
69                 NR::Rect const item_box(sp_item_bbox_desktop(u));
70                 NR::Point const curr(item_box.midpoint());
71                 NR::Point const dest(r->getCentreX(),r->getCentreY());
72                 sp_item_move_rel(u, Geom::Translate(dest - curr));
73             }
74         }
75         */
76                 return TestConvergence::operator()(new_stress,X,Y);
77         }
78     list<SPItem *>& selected;
79     vector<Rectangle*>& rs;
80     map<string,unsigned>& nodelookup;
81 };
83 /**
84  * Scans the items list and places those items that are 
85  * not connectors in filtered
86  */
87 void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
88         for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
89                 SPItem *item=SP_ITEM(i->data);
90                 if(!isConnector(item)) {
91                         filtered.push_back(item);
92                 }
93         }
94 }
95 /**
96 * Takes a list of inkscape items, extracts the graph defined by 
97 * connectors between them, and uses graph layout techniques to find
98 * a nice layout
99 */
100 void graphlayout(GSList const *const items) {
101         if(!items) {
102                 return;
103         }
105         using Inkscape::Util::GSListConstIterator;
106         list<SPItem *> selected;
107         filterConnectors(items,selected);
108         if (selected.empty()) return;
110         const unsigned n=selected.size();
111         //Check 2 or more selected objects
112         if (n < 2) return;
114     // add the connector spacing to the size of node bounding boxes
115     // so that connectors can always be routed between shapes
116     SPDesktop* desktop = inkscape_active_desktop();
117     double spacing = 0;
118     if(desktop) spacing = desktop->namedview->connector_spacing+0.1;
120         map<string,unsigned> nodelookup;
121         vector<Rectangle*> rs;
122         vector<Edge> es;
123         for (list<SPItem *>::iterator i(selected.begin());
124                 i != selected.end();
125                 ++i)
126         {
127                 SPItem *u=*i;
128                 Geom::OptRect const item_box(sp_item_bbox_desktop(u));
129         if(item_box) {
130             Geom::Point ll(item_box->min());
131             Geom::Point ur(item_box->max());
132             nodelookup[u->id]=rs.size();
133             rs.push_back(new Rectangle(ll[0]-spacing,ur[0]+spacing,
134                         ll[1]-spacing,ur[1]+spacing));
135         } else {
136             // I'm not actually sure if it's possible for something with a
137             // NULL item-box to be attached to a connector in which case we
138             // should never get to here... but if such a null box can occur it's
139             // probably pretty safe to simply ignore
140             //fprintf(stderr,"NULL item_box found in graphlayout, ignoring!\n");
141         }
142         }
144     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
145         SimpleConstraints scx,scy;
146         double ideal_connector_length = prefs->getDouble("/tools/connector/length", 100.0);
147         double directed_edge_height_modifier = 1.0;
148         
149         bool directed =       prefs->getBool("/tools/connector/directedlayout");
150         bool avoid_overlaps = prefs->getBool("/tools/connector/avoidoverlaplayout");
152         for (list<SPItem *>::iterator i(selected.begin());
153                 i != selected.end();
154                 ++i)
155         {
156                 SPItem *iu=*i;
157         map<string,unsigned>::iterator i=nodelookup.find(iu->id);
158         if(i==nodelookup.end()) {
159             continue;
160         }
161         unsigned u=i->second;
162         GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
163         list<SPItem *> connectors;
164         
165         connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
166         for (list<SPItem *>::iterator j(connectors.begin());
167                 j != connectors.end();
168                 ++j) {
169             SPItem *conn=*j;
170             SPItem *iv;
171             SPItem *items[2];
172             assert(isConnector(conn));
173             SP_PATH(conn)->connEndPair.getAttachedItems(items);
174             if(items[0]==iu) {
175                 iv=items[1];
176             } else {
177                 iv=items[0];
178             }
179     
180             if (iv == NULL) {
181                 // The connector is not attached to anything at the 
182                 // other end so we should just ignore it.
183                 continue;
184             }
186             // If iv not in nodelookup we again treat the connector
187             // as disconnected and continue
188             map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
189             if(v_pair!=nodelookup.end()) {
190                 unsigned v=v_pair->second;
191                 //cout << "Edge: (" << u <<","<<v<<")"<<endl;
192                 es.push_back(make_pair(u,v));
193                 if(conn->style->marker[SP_MARKER_LOC_END].set) {
194                     if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
195                         scy.push_back(new SimpleConstraint(v, u, 
196                                     (ideal_connector_length * directed_edge_height_modifier)));
197                     }
198                 }
199             }
200         }
201                 if(nlist) {
202                         g_slist_free(nlist);
203                 }
204         }
205         const unsigned E = es.size();
206         double eweights[E];
207         fill(eweights,eweights+E,1);
208     vector<Component*> cs;
209     connectedComponents(rs,es,scx,scy,cs);
210     for(unsigned i=0;i<cs.size();i++) {
211         Component* c=cs[i];
212         if(c->edges.size()<2) continue;
213         CheckProgress test(0.0001,100,selected,rs,nodelookup);
214         ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length,test);
215         alg.setupConstraints(NULL,NULL,avoid_overlaps,
216                 NULL,NULL,&c->scx,&c->scy,NULL,NULL);
217         alg.run();
218     }
219     separateComponents(cs);
220         
221         for (list<SPItem *>::iterator it(selected.begin());
222                 it != selected.end();
223                 ++it)
224         {
225                 SPItem *u=*it;
226                 if(!isConnector(u)) {
227                         map<string,unsigned>::iterator i=nodelookup.find(u->id);
228                         if(i!=nodelookup.end()) {
229                 Rectangle* r=rs[i->second];
230                 Geom::OptRect item_box(sp_item_bbox_desktop(u));
231                 if(item_box) {
232                     Geom::Point const curr(item_box->midpoint());
233                     Geom::Point const dest(r->getCentreX(),r->getCentreY());
234                     sp_item_move_rel(u, Geom::Translate(dest - curr));
235                 }
236             }
237                 }
238         }
239     for(unsigned i=0;i<scx.size();i++) {
240         delete scx[i];
241     }
242     for(unsigned i=0;i<scy.size();i++) {
243         delete scy[i];
244     }
245     for(unsigned i=0;i<rs.size();i++) {
246         delete rs[i];
247     }
249 // vim: set cindent 
250 // vim: ts=4 sw=4 et tw=0 wm=0
252 /*
253   Local Variables:
254   mode:c++
255   c-file-style:"stroustrup"
256   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
257   indent-tabs-mode:nil
258   fill-column:99
259   End:
260 */
261 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :